17
18
# You should have received a copy of the GNU General Public License
18
19
# along with this program; if not, write to the Free Software
19
# Foundation, Inc., 59 Temple Place, Suite 330,
20
# Boston, MA 02111-1307, USA.
20
# Foundation, Inc., 51 Franklin Street, Fifth Floor,
21
# Boston, MA 02110-1301, USA.
22
23
from gi.repository import GObject, Gtk, Gdk, Gedit
45
class ColorPickerPlugin(GObject.Object, Gedit.WindowActivatable):
46
__gtype_name__ = "ColorPickerPlugin"
48
window = GObject.property(type=Gedit.Window)
51
GObject.Object.__init__(self)
54
def do_activate(self):
58
def do_deactivate(self):
61
def do_update_state(self):
65
tab = self.window.get_active_tab()
66
self._action_group.set_sensitive(tab != None)
68
if not tab and self._dialog and \
69
self._dialog.get_transient_for() == self.window:
70
self._dialog.response(Gtk.ResponseType.CLOSE)
72
def _insert_menu(self):
73
manager = self.window.get_ui_manager()
74
self._action_group = Gtk.ActionGroup(name="GeditColorPickerPluginActions")
75
self._action_group.add_actions(
76
[("ColorPicker", None, _("Pick _Color..."), None,
77
_("Pick a color from a dialog"),
78
lambda a: self.on_color_picker_activate())])
80
manager.insert_action_group(self._action_group)
81
self._ui_id = manager.add_ui_from_string(ui_str)
83
def _remove_menu(self):
84
manager = self.window.get_ui_manager()
85
manager.remove_ui(self._ui_id)
86
manager.remove_action_group(self._action_group)
87
manager.ensure_update()
48
def scale_color_component(self, component):
49
return min(max(int(round(component * 255.)), 0), 255)
89
51
def skip_hex(self, buf, iter, next_char):
154
117
doc.end_user_action()
156
def scale_color_component(self, component):
157
return min(max(int(round(component * 255.)), 0), 255)
159
def get_current_color(self):
160
doc = self.window.get_active_document()
119
def get_current_color(self, doc, use_bounds):
165
bounds = self.get_rgba_position(doc)
123
bounds = self.get_rgba_position(doc, use_bounds)
168
126
return doc.get_text(bounds[0], bounds[1], False)
130
class ColorPickerWindowActivatable(GObject.Object, Gedit.WindowActivatable):
132
window = GObject.property(type=Gedit.Window)
135
GObject.Object.__init__(self)
137
self._color_helper = ColorHelper()
139
def do_activate(self):
143
def do_deactivate(self):
146
def do_update_state(self):
150
tab = self.window.get_active_tab()
151
self._action_group.set_sensitive(tab != None)
153
if not tab and self._dialog and \
154
self._dialog.get_transient_for() == self.window:
155
self._dialog.response(Gtk.ResponseType.CLOSE)
157
def _insert_menu(self):
158
manager = self.window.get_ui_manager()
159
self._action_group = Gtk.ActionGroup(name="GeditColorPickerPluginActions")
160
self._action_group.add_actions(
161
[("ColorPicker", None, _("Pick _Color..."), None,
162
_("Pick a color from a dialog"),
163
lambda a: self.on_color_picker_activate())])
165
manager.insert_action_group(self._action_group)
166
self._ui_id = manager.add_ui_from_string(ui_str)
168
def _remove_menu(self):
169
manager = self.window.get_ui_manager()
170
manager.remove_ui(self._ui_id)
171
manager.remove_action_group(self._action_group)
172
manager.ensure_update()
172
174
# Signal handlers
174
176
def on_color_picker_activate(self):
193
195
rgba = Gdk.RGBA()
194
196
dialog.get_rgba(rgba)
196
self.insert_color("%02x%02x%02x" % (self.scale_color_component(rgba.red), \
197
self.scale_color_component(rgba.green), \
198
self.scale_color_component(rgba.blue)))
198
self._color_helper.insert_color(self.window.get_active_view(),
199
"%02x%02x%02x" % (self._color_helper.scale_color_component(rgba.red), \
200
self._color_helper.scale_color_component(rgba.green), \
201
self._color_helper.scale_color_component(rgba.blue)))
200
203
self._dialog.destroy()
201
204
self._dialog = None
207
class ColorPickerViewActivatable(GObject.Object, Gedit.ViewActivatable):
209
view = GObject.property(type=Gedit.View)
212
GObject.Object.__init__(self)
213
self._color_button = None
214
self._color_helper = ColorHelper()
216
def do_activate(self):
217
# we do not have a direct accessor to the overlay
218
self.overlay = self.view.get_parent().get_parent()
219
self.overlay.connect('get-child-position', self.on_get_child_position)
221
buf = self.view.get_buffer()
222
buf.connect_after('mark-set', self.on_buffer_mark_set)
224
def do_deactivate(self):
225
if self._color_button is not None:
226
self._color_button.destroy()
227
self._color_button = None
229
def on_get_child_position(self, overlay, widget, alloc):
230
if widget == self._color_button:
231
buf = self.view.get_buffer()
232
bounds = buf.get_selection_bounds()
235
location = self.view.get_iter_location(start)
236
x, y = self.view.buffer_to_window_coords(Gtk.TextWindowType.TEXT, location.x, location.y)
237
min_width, nat_width = widget.get_preferred_width()
238
min_height, nat_height = widget.get_preferred_height()
240
if y - nat_height > 0:
241
alloc.y = y - nat_height
243
alloc.y = y + location.height
244
alloc.width = nat_width
245
alloc.height = nat_height
251
def on_buffer_mark_set(self, buf, location, mark):
253
if not buf.get_has_selection():
254
if self._color_button:
255
self._color_button.destroy()
256
self._color_button = None
259
if mark != buf.get_insert() and mark != buf.get_selection_bound():
262
rgba_str = self._color_helper.get_current_color(self.view.get_buffer(), True)
263
if rgba_str is not None and self._color_button is None:
265
parsed = rgba.parse(rgba_str)
267
self._color_button = Gtk.ColorButton.new_with_rgba(rgba)
268
self._color_button.set_halign(Gtk.Align.START)
269
self._color_button.set_valign(Gtk.Align.START)
270
self._color_button.show()
271
self._color_button.connect('color-set', self.on_color_set)
273
self.overlay.add_overlay(self._color_button)
274
elif not rgba_str and self._color_button is not None:
275
self._color_button.destroy()
276
self._color_button = None
278
def on_color_set(self, color_button):
280
color_button.get_rgba(rgba)
282
self._color_helper.insert_color(self.view,
283
"%02x%02x%02x" % (self._color_helper.scale_color_component(rgba.red), \
284
self._color_helper.scale_color_component(rgba.green), \
285
self._color_helper.scale_color_component(rgba.blue)))