diff options
author | Benjamin Berg <benjamin@sipsolutions.net> | 2011-10-30 14:28:33 (GMT) |
---|---|---|
committer | Benjamin Berg <benjamin@sipsolutions.net> | 2011-10-30 14:29:05 (GMT) |
commit | fdb0daa01d20535a7b87cfd2c9c3be019cb20e9f (patch) | |
tree | e33356721fb5ded1a638ade91507fd5825411546 /src/sugar3/graphics/palettewindow.py | |
parent | 7371e143886021975903c972a77474593e080e62 (diff) |
Move the widget of the palette to a separate class.
Diffstat (limited to 'src/sugar3/graphics/palettewindow.py')
-rw-r--r-- | src/sugar3/graphics/palettewindow.py | 175 |
1 files changed, 92 insertions, 83 deletions
diff --git a/src/sugar3/graphics/palettewindow.py b/src/sugar3/graphics/palettewindow.py index f500d12..3406a10 100644 --- a/src/sugar3/graphics/palettewindow.py +++ b/src/sugar3/graphics/palettewindow.py @@ -65,6 +65,77 @@ def _calculate_gap(a, b): return False +class _PaletteWindowWidget(Gtk.Window): + + def __init__(self): + Gtk.Window.__init__(self) + + self.set_decorated(False) + self.set_resizable(False) + self.set_position(Gtk.WindowPosition.NONE) + + # TODO: How to implement this with GtkStyle/the theme + # Do not forget theme changes. + #self.set_border_width(self.get_style().xthickness) + + accel_group = Gtk.AccelGroup() + self.set_data('sugar-accel-group', accel_group) + self.add_accel_group(accel_group) + + self._old_alloc = None + + self._should_accept_focus = True + + def set_accept_focus(self, focus): + self._should_accept_focus = focus + if self.get_window() != None: + self.get_window().set_accept_focus(focus) + + def do_realize(self): + Gtk.Window.do_realize(self) + + self.get_window().set_accept_focus(self._should_accept_focus) + + def get_origin(self): + res, x, y = self.get_window().get_origin() + return x, y + + def do_realize(self): + Gtk.Window.do_realize(self) + self.set_type_hint(Gdk.WindowTypeHint.DIALOG) + + def do_size_request(self, requisition): + Gtk.Window.do_size_request(self, requisition) + requisition.width = max(requisition.width, style.GRID_CELL_SIZE * 2) + + def do_size_allocate(self, allocation): + Gtk.Window.do_size_allocate(self, allocation) + + if self._old_alloc is None or \ + self._old_alloc.x != allocation.x or \ + self._old_alloc.y != allocation.y or \ + self._old_alloc.width != allocation.width or \ + self._old_alloc.height != allocation.height: + self.queue_draw() + + # We need to store old allocation because when size_allocate + # is called widget.allocation is already updated. + # Gtk.Window resizing is different from normal containers: + # the X window is resized, widget.allocation is updated from + # the configure request handler and finally size_allocate is called. + self._old_alloc = allocation + + def do_draw(self, cr): + + # TODO: Draw the border with gap here. + cr.set_source_rgb(0.5, 0.5, 0.5) + cr.paint() + + # Fall trough to the container expose handler. + # (Leaving out the window expose handler which redraws everything) + Gtk.Bin.do_draw(self, cr) + + class MouseSpeedDetector(GObject.GObject): __gsignals__ = { @@ -131,14 +202,11 @@ class MouseSpeedDetector(GObject.GObject): return True -class PaletteWindow(Gtk.Window): - - __gtype_name__ = 'SugarPaletteWindow' +class PaletteWindow(GObject.GObject): __gsignals__ = { 'popup': (GObject.SignalFlags.RUN_FIRST, None, ([])), 'popdown': (GObject.SignalFlags.RUN_FIRST, None, ([])), - 'activate': (GObject.SignalFlags.RUN_FIRST, None, ([])), } def __init__(self, **kwargs): @@ -149,7 +217,6 @@ class PaletteWindow(Gtk.Window): self._cursor_y = 0 self._alignment = None self._up = False - self._old_alloc = None self._palette_state = None self._popup_anim = animator.Animator(.5, 10) @@ -160,26 +227,15 @@ class PaletteWindow(Gtk.Window): GObject.GObject.__init__(self, **kwargs) - self.set_decorated(False) - self.set_resizable(False) - self.set_position(Gtk.WindowPosition.NONE) - # Just assume xthickness and ythickness are the same - self.set_border_width(self.get_style().xthickness) - - accel_group = Gtk.AccelGroup() - self.set_data('sugar-accel-group', accel_group) - self.add_accel_group(accel_group) - self.set_group_id('default') - self.connect('show', self.__show_cb) - self.connect('hide', self.__hide_cb) - self.connect('realize', self.__realize_cb) - self.connect('destroy', self.__destroy_cb) - self.connect('enter-notify-event', self.__enter_notify_event_cb) - self.connect('leave-notify-event', self.__leave_notify_event_cb) + self.widget.connect('show', self.__show_cb) + self.widget.connect('hide', self.__hide_cb) + self.widget.connect('destroy', self.__destroy_cb) + self.widget.connect('enter-notify-event', self.__enter_notify_event_cb) + self.widget.connect('leave-notify-event', self.__leave_notify_event_cb) - self._mouse_detector = MouseSpeedDetector(self, 200, 5) + self._mouse_detector = MouseSpeedDetector(self.widget, 200, 5) self._mouse_detector.connect('motion-slow', self._mouse_slow_cb) def __destroy_cb(self, palette): @@ -207,9 +263,6 @@ class PaletteWindow(Gtk.Window): getter=get_invoker, setter=set_invoker) - def __realize_cb(self, widget): - self.set_type_hint(Gdk.WindowTypeHint.DIALOG) - def _mouse_slow_cb(self, widget): self._mouse_detector.stop() self._palette_do_popup() @@ -248,69 +301,21 @@ class PaletteWindow(Gtk.Window): getter=get_group_id, setter=set_group_id) - def do_size_request(self, requisition): - Gtk.Window.do_size_request(self, requisition) - requisition.width = max(requisition.width, style.GRID_CELL_SIZE * 2) - - def do_size_allocate(self, allocation): - Gtk.Window.do_size_allocate(self, allocation) - - if self._old_alloc is None or \ - self._old_alloc.x != allocation.x or \ - self._old_alloc.y != allocation.y or \ - self._old_alloc.width != allocation.width or \ - self._old_alloc.height != allocation.height: - self.queue_draw() - - # We need to store old allocation because when size_allocate - # is called widget.allocation is already updated. - # Gtk.Window resizing is different from normal containers: - # the X window is resized, widget.allocation is updated from - # the configure request handler and finally size_allocate is called. - self._old_alloc = allocation - - def do_expose_event(self, event): - # We want to draw a border with a beautiful gap - if self._invoker is not None and self._invoker.has_rectangle_gap(): - invoker = self._invoker.get_rect() - palette = self.get_rect() - - gap = _calculate_gap(palette, invoker) - else: - gap = False - - allocation = self.get_allocation() - wstyle = self.get_style() - - if gap: - wstyle.paint_box_gap(event.window, Gtk.StateType.PRELIGHT, - Gtk.ShadowType.IN, event.area, self, 'palette', - 0, 0, allocation.width, allocation.height, - gap[0], gap[1], gap[2]) - else: - wstyle.paint_box(event.window, Gtk.StateType.PRELIGHT, - Gtk.ShadowType.IN, event.area, self, 'palette', - 0, 0, allocation.width, allocation.height) - - # Fall trough to the container expose handler. - # (Leaving out the window expose handler which redraws everything) - Gtk.Bin.do_expose_event(self, event) - def update_position(self): invoker = self._invoker if invoker is None or self._alignment is None: logging.error('Cannot update the palette position.') return - rect = self.size_request() + rect = self.widget.size_request() position = invoker.get_position_for_alignment(self._alignment, rect) if position is None: position = invoker.get_position(rect) - self.move(position.x, position.y) + self.widget.move(position.x, position.y) def get_full_size_request(self): - return self.size_request() + return self.widget.size_request() def popup(self, immediate=False): if self._invoker is not None: @@ -318,7 +323,7 @@ class PaletteWindow(Gtk.Window): self._alignment = self._invoker.get_alignment(full_size_request) self.update_position() - self.set_transient_for(self._invoker.get_toplevel()) + self.widget.set_transient_for(self._invoker.get_toplevel()) self._popdown_anim.stop() @@ -326,7 +331,7 @@ class PaletteWindow(Gtk.Window): self._popup_anim.start() else: self._popup_anim.stop() - self.show() + self.widget.show() # we have to invoke update_position() twice # since WM could ignore first move() request self.update_position() @@ -339,8 +344,9 @@ class PaletteWindow(Gtk.Window): self._popdown_anim.start() else: self._popdown_anim.stop() - self.size_request() - self.hide() + # XXX: Please, someone remove or explain this :-) + self.widget.size_request() + self.widget.hide() def on_invoker_enter(self): self._popdown_anim.stop() @@ -386,14 +392,17 @@ class PaletteWindow(Gtk.Window): self.emit('popdown') def get_rect(self): - res, win_x, win_y = self.get_window().get_origin() + win_x, win_y = self.widget.get_origin() rectangle = self.get_allocation() x = win_x + rectangle.x y = win_y + rectangle.y - width, height = self.size_request() + requisition = self.widget.size_request() - return (x, y, width, height) + rect = Gdk.Rectangle() + rect.x, rect.y, rect.width, rect.height = (x, y, requisition.width, requisition.height) + + return rect def get_palette_state(self): return self._palette_state |