diff options
Diffstat (limited to 'sugar/graphics/palette.py')
-rw-r--r-- | sugar/graphics/palette.py | 142 |
1 files changed, 60 insertions, 82 deletions
diff --git a/sugar/graphics/palette.py b/sugar/graphics/palette.py index d87b0f6..4c52421 100644 --- a/sugar/graphics/palette.py +++ b/sugar/graphics/palette.py @@ -73,10 +73,6 @@ class Palette(gtk.Window): DEFAULT = 0 AT_CURSOR = 1 AROUND = 2 - BOTTOM = 3 - LEFT = 4 - RIGHT = 5 - TOP = 6 PRIMARY = 0 SECONDARY = 1 @@ -106,6 +102,7 @@ class Palette(gtk.Window): self.palette_state = self.PRIMARY + self._current_alignment = None self._old_alloc = None self._full_request = [0, 0] self._cursor_x = 0 @@ -278,21 +275,26 @@ class Palette(gtk.Window): self.window.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_DIALOG) self._update_accept_focus() - def _in_screen(self, x, y): - [width, height] = self._full_request + def _in_screen(self, rect): screen_area = self._invoker.get_screen_area() + return rect.x >= screen_area.x and \ + rect.y >= screen_area.y and \ + rect.x + rect.width <= screen_area.width and \ + rect.y + rect.height <= screen_area.height - return x >= screen_area.x and \ - y >= screen_area.y and \ - x + width <= screen_area.width and \ - y + height <= screen_area.height + def _get_rectangle(self, alignments, full=False, inv_rect=None): + palette_halign = alignments[0] + palette_valign = alignments[1] + invoker_halign = alignments[2] + invoker_valign = alignments[3] - def _get_position(self, palette_halign, palette_valign, - invoker_halign, invoker_valign, inv_rect=None): if inv_rect == None: inv_rect = self._invoker.get_rect() - palette_width, palette_height = self.size_request() + if full: + palette_width, palette_height = self._full_request + else: + palette_width, palette_height = self.size_request() x = inv_rect.x + inv_rect.width * invoker_halign + \ palette_width * palette_halign @@ -300,53 +302,24 @@ class Palette(gtk.Window): y = inv_rect.y + inv_rect.height * invoker_valign + \ palette_height * palette_valign - return int(x), int(y) - - def _get_left_position(self, inv_rect=None): - x, y = self._get_position(-1.0, 0.0, 0.0, 0.0, inv_rect) - if not self._in_screen(x, y): - x, y = self._get_position(-1.0, -1.0, 0.0, 1.0, inv_rect) - return x, y - - def _get_right_position(self, inv_rect=None): - x, y = self._get_position(0.0, 0.0, 1.0, 0.0, inv_rect) - if not self._in_screen(x, y): - x, y = self._get_position(0.0, -1.0, 1.0, 1.0, inv_rect) - return x, y - - def _get_top_position(self, inv_rect=None): - x, y = self._get_position(0.0, -1.0, 0.0, 0.0, inv_rect) - if not self._in_screen(x, y): - x, y = self._get_position(-1.0, -1.0, 1.0, 0.0, inv_rect) - return x, y - - def _get_bottom_position(self, inv_rect=None): - x, y = self._get_position(0.0, 0.0, 0.0, 1.0, inv_rect) - if not self._in_screen(x, y): - x, y = self._get_position(-1.0, 0.0, 1.0, 1.0, inv_rect) - return x, y - - def _get_around_position(self, inv_rect=None): - x, y = self._get_bottom_position(inv_rect) - if not self._in_screen(x, y): - x, y = self._get_right_position(inv_rect) - if not self._in_screen(x, y): - x, y = self._get_top_position(inv_rect) - if not self._in_screen(x, y): - x, y = self._get_left_position(inv_rect) - - return x, y - - def _get_at_cursor_position(self, inv_rect=None): - x, y = self._get_position(0.0, 0.0, 1.0, 1.0, inv_rect) - if not self._in_screen(x, y): - x, y = self._get_position(0.0, -1.0, 1.0, 0.0, inv_rect) - if not self._in_screen(x, y): - x, y = self._get_position(-1.0, -1.0, 0.0, 0.0, inv_rect) - if not self._in_screen(x, y): - x, y = self._get_position(-1.0, 0.0, 0.0, 1.0, inv_rect) - - return x, y + return gtk.gdk.Rectangle(int(x), int(y), + palette_width, palette_height) + + def _get_around_alignments(self): + return [(0.0, 0.0, 0.0, 1.0), + (-1.0, 0.0, 1.0, 1.0), + (0.0, 0.0, 1.0, 0.0), + (0.0, -1.0, 1.0, 1.0), + (0.0, -1.0, 0.0, 0.0), + (-1.0, -1.0, 1.0, 0.0), + (-1.0, 0.0, 0.0, 0.0), + (-1.0, -1.0, 0.0, 1.0)] + + def _get_at_cursor_alignments(self, inv_rect=None): + return [(0.0, 0.0, 1.0, 1.0), + (0.0, -1.0, 1.0, 0.0), + (-1.0, -1.0, 0.0, 0.0), + (-1.0, 0.0, 0.0, 1.0)] def _update_full_request(self): state = self.palette_state @@ -374,25 +347,24 @@ class Palette(gtk.Window): else: position = self._position + inv_rect = None if position == self.AT_CURSOR: dist = style.PALETTE_CURSOR_DISTANCE - rect = gtk.gdk.Rectangle(self._cursor_x - dist, - self._cursor_y - dist, - dist * 2, dist * 2) - - x, y = self._get_at_cursor_position(rect) - elif position == self.AROUND: - x, y = self._get_around_position() - elif position == self.BOTTOM: - x, y = self._get_bottom_position() - elif position == self.LEFT: - x, y = self._get_left_position() - elif position == self.RIGHT: - x, y = self._get_right_position() - elif position == self.TOP: - x, y = self._get_top_position() - - self.move(x, y) + inv_rect = gtk.gdk.Rectangle(self._cursor_x - dist, + self._cursor_y - dist, + dist * 2, dist * 2) + + alignments = self._get_around_alignments()[:] + if self._current_alignment is not None: + alignments.remove(self._current_alignment) + alignments.insert(0, self._current_alignment) + + for align in alignments: + rect = self._get_rectangle(align, inv_rect=inv_rect) + if self._in_screen(rect): + break + + self.move(rect.x, rect.y) def _show(self): if self._up: @@ -404,6 +376,12 @@ class Palette(gtk.Window): self._palette_popup_sid = _palette_observer.connect( 'popup', self._palette_observer_popup_cb) + for align in self._get_around_alignments(): + rect = self._get_rectangle(align, full=True) + if self._in_screen(rect): + self._current_alignment = align + break + self._update_position() self.menu.set_active(True) self.show() @@ -659,11 +637,11 @@ class CanvasInvoker(Invoker): context = self._item.get_context() if context: x, y = context.translate_to_screen(self._item) - - width, height = self._item.get_allocation() - - return gtk.gdk.Rectangle(x, y, width, height) - + width, height = self._item.get_allocation() + return gtk.gdk.Rectangle(x, y, width, height) + else: + return gtk.gdk.Rectangle() + def _motion_notify_event_cb(self, button, event): if event.detail == hippo.MOTION_DETAIL_ENTER: self.emit('mouse-enter') |