From 54dcb672e563063b590775eb157b3b6c9f9af752 Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Wed, 18 Oct 2006 14:23:06 +0000 Subject: Move the menu positioning code inside menu shell and unify it. --- (limited to 'sugar') diff --git a/sugar/graphics/menuicon.py b/sugar/graphics/menuicon.py index 7ee6bce..8c0041e 100644 --- a/sugar/graphics/menuicon.py +++ b/sugar/graphics/menuicon.py @@ -17,14 +17,11 @@ import hippo import gobject +import logging from sugar.graphics.canvasicon import CanvasIcon from sugar.graphics.timeline import Timeline -class _MenuStrategy: - def get_menu_position(self, menu, item): - return item.get_context().translate_to_widget(item) - class MenuIcon(CanvasIcon): def __init__(self, menu_shell, **kwargs): CanvasIcon.__init__(self, **kwargs) @@ -32,7 +29,6 @@ class MenuIcon(CanvasIcon): self._menu_shell = menu_shell self._menu = None self._hover_menu = False - self._menu_strategy = _MenuStrategy() self._timeline = Timeline(self) self._timeline.add_tag('popup', 6, 6) @@ -41,9 +37,6 @@ class MenuIcon(CanvasIcon): self.connect('motion-notify-event', self._motion_notify_event_cb) - def set_menu_strategy(self, strategy): - self._menu_strategy = strategy - def do_popup(self, current, n_frames): if self._menu: return @@ -55,8 +48,7 @@ class MenuIcon(CanvasIcon): self._menu.connect('leave-notify-event', self._menu_leave_notify_event_cb) - strategy = self._menu_strategy - [x, y] = strategy.get_menu_position(self._menu, self) + [x, y] = self._menu_shell.get_position(self._menu, self) self._menu.move(x, y) self._menu.show() diff --git a/sugar/graphics/menushell.py b/sugar/graphics/menushell.py index e430e6f..bb25f9c 100644 --- a/sugar/graphics/menushell.py +++ b/sugar/graphics/menushell.py @@ -25,8 +25,10 @@ class MenuShell(gobject.GObject): gobject.TYPE_NONE, ([])), } - def __init__(self): + def __init__(self, parent_canvas): gobject.GObject.__init__(self) + + self._parent_canvas = parent_canvas self._menu_controller = None def is_active(self): @@ -41,3 +43,30 @@ class MenuShell(gobject.GObject): if self._menu_controller: self._menu_controller.popdown() self._menu_controller = controller + + def _get_item_origin(self, item): + [x, y] = item.get_context().translate_to_widget(item) + + [origin_x, origin_y] = self._parent_canvas.window.get_origin() + x += origin_x + y += origin_y + + return [x, y] + + def get_position(self, menu, item): + [x, y] = self._get_item_origin(item) + [width, height] = item.get_allocation() + + [canvas_x, canvas_y] = self._parent_canvas.window.get_origin() + canvas_rect = self._parent_canvas.get_allocation() + [menu_w, menu_h] = menu.size_request() + + menu_x = x + menu_y = y + height + + if (menu_x + menu_w > canvas_x) and \ + (menu_y < canvas_y + canvas_rect.height): + menu_x = x - menu_w + menu_y = y + + return [menu_x, menu_y] -- cgit v0.9.1