211
211
def __init__(self):
212
212
GObject.Object.__init__(self)
213
self._rgba_str = None
213
214
self._color_button = None
214
215
self._color_helper = ColorHelper()
216
217
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
219
buf = self.view.get_buffer()
222
220
buf.connect_after('mark-set', self.on_buffer_mark_set)
226
224
self._color_button.destroy()
227
225
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
227
def on_buffer_mark_set(self, buf, location, mark):
253
229
if not buf.get_has_selection():
262
238
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)
239
if rgba_str is not None and rgba_str != self._rgba_str and self._color_button is not None:
241
parsed = rgba.parse(rgba_str)
243
self._rgba_str = rgba_str
244
self._color_button.set_rgba(rgba)
245
elif rgba_str is not None and self._color_button is None:
247
parsed = rgba.parse(rgba_str)
249
self._rgba_str = rgba_str
251
bounds = buf.get_selection_bounds()
253
self._color_button = Gtk.ColorButton.new_with_rgba(rgba)
254
self._color_button.set_halign(Gtk.Align.START)
255
self._color_button.set_valign(Gtk.Align.START)
256
self._color_button.show()
257
self._color_button.connect('color-set', self.on_color_set)
260
location = self.view.get_iter_location(start)
261
win_x, win_y = self.view.buffer_to_window_coords(Gtk.TextWindowType.TEXT, location.x, location.y)
262
min_width, nat_width = self._color_button.get_preferred_width()
263
min_height, nat_height = self._color_button.get_preferred_height()
265
if win_y - nat_height > 0:
266
y = win_y - nat_height
268
y = win_y + location.height
270
self.view.add_child_in_window(self._color_button, Gtk.TextWindowType.TEXT, x, y)
274
271
elif not rgba_str and self._color_button is not None:
275
272
self._color_button.destroy()
276
273
self._color_button = None