~ubuntu-branches/ubuntu/raring/gedit-plugins/raring

« back to all changes in this revision

Viewing changes to plugins/colorpicker/colorpicker.py

  • Committer: Package Import Robot
  • Author(s): Robert Ancell
  • Date: 2012-09-25 10:10:54 UTC
  • mfrom: (1.4.11)
  • Revision ID: package-import@ubuntu.com-20120925101054-y5gqrf29jt1evacd
Tags: 3.6.0-0ubuntu1
New upstream stable release

Show diffs side-by-side

added added

removed removed

Lines of Context:
210
210
 
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()
215
216
 
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)
220
218
 
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
228
226
 
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()
233
 
            if bounds != ():
234
 
                start, end = 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()
239
 
                alloc.x = x
240
 
                if y - nat_height > 0:
241
 
                    alloc.y = y - nat_height
242
 
                else:
243
 
                    alloc.y = y + location.height
244
 
                alloc.width = nat_width
245
 
                alloc.height = nat_height
246
 
 
247
 
                return True
248
 
 
249
 
        return False
250
 
 
251
227
    def on_buffer_mark_set(self, buf, location, mark):
252
228
 
253
229
        if not buf.get_has_selection():
260
236
            return
261
237
 
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:
264
 
            rgba = Gdk.RGBA()
265
 
            parsed = rgba.parse(rgba_str)
266
 
            if parsed:
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)
272
 
 
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:
 
240
            rgba = Gdk.RGBA()
 
241
            parsed = rgba.parse(rgba_str)
 
242
            if parsed:
 
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:
 
246
            rgba = Gdk.RGBA()
 
247
            parsed = rgba.parse(rgba_str)
 
248
            if parsed:
 
249
                self._rgba_str = rgba_str
 
250
 
 
251
                bounds = buf.get_selection_bounds()
 
252
                if 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)
 
258
 
 
259
                    start, end = bounds
 
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()
 
264
                    x = win_x
 
265
                    if win_y - nat_height > 0:
 
266
                        y = win_y - nat_height
 
267
                    else:
 
268
                        y = win_y + location.height
 
269
 
 
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