From a951b36347bfb5350c4c6031ba1126c25b94f44d Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Mon, 2 Oct 2006 16:37:30 +0200 Subject: [PATCH] Reimplement menu using hippo and hook it up for the activity menu. --- shell/view/frame/MenuStrategy.py | 33 +++++++-------------- shell/view/frame/ZoomBox.py | 20 ++++++------- sugar/graphics/__init__.py | 5 ++++ sugar/graphics/grid.py | 14 +++++---- sugar/graphics/menu.py | 50 ++++++++++++++++++++++++++++++++ sugar/graphics/menuicon.py | 28 +++--------------- 6 files changed, 88 insertions(+), 62 deletions(-) create mode 100644 sugar/graphics/menu.py diff --git a/shell/view/frame/MenuStrategy.py b/shell/view/frame/MenuStrategy.py index 314cb047..0519d234 100644 --- a/shell/view/frame/MenuStrategy.py +++ b/shell/view/frame/MenuStrategy.py @@ -1,28 +1,15 @@ +from sugar.graphics.grid import Grid + class MenuStrategy: - def get_menu_position(self, menu, grid_x1, grid_y1, grid_x2, grid_y2): - grid = menu.get_grid() + def get_menu_position(self, menu, x, y, width, height): + grid = Grid() - [x1, y1] = grid.micro_to_macro(grid_x1, grid_y1) - [x2, y2] = grid.micro_to_macro(grid_x2, grid_y2) + [grid_x1, grid_y1] = grid.fit_point(x, y) + [grid_x2, grid_y2] = grid.fit_point(x + width, y + height) - if x1 == 0: - x = x2 - y = y1 - elif x2 == grid.get_macro_cols(): - x = x1 - y = y1 - elif y2 == grid.get_macro_rows(): - x = x1 - y = y1 - else: - x = x1 - y = y2 + menu_grid_x = grid_x1 + menu_grid_y = grid_y2 - [grid_x, grid_y] = grid.macro_to_micro(x, y) + [menu_x, menu_y] = grid.point(menu_grid_x, menu_grid_y) - if x2 == grid.get_macro_cols(): - grid_x -= menu.get_width() - elif y2 == grid.get_macro_rows(): - grid_y -= menu.get_height() - - return [grid_x, grid_y] + return [menu_x, menu_y] diff --git a/shell/view/frame/ZoomBox.py b/shell/view/frame/ZoomBox.py index 8e78979e..26fda37c 100644 --- a/shell/view/frame/ZoomBox.py +++ b/shell/view/frame/ZoomBox.py @@ -3,9 +3,8 @@ import hippo from sugar.graphics.canvasicon import CanvasIcon from sugar.graphics.menuicon import MenuIcon +from sugar.graphics.menu import Menu from sugar.graphics import style -from sugar.canvas.Menu import Menu -from sugar.canvas.IconItem import IconItem from view.frame.MenuStrategy import MenuStrategy import sugar @@ -14,13 +13,14 @@ class ActivityMenu(Menu): ACTION_CLOSE = 2 def __init__(self, grid, activity_host): - title = activity_host.get_title() - Menu.__init__(self, grid, title) + Menu.__init__(self, activity_host.get_title()) - icon = IconItem(icon_name='stock-share-mesh') + icon = CanvasIcon(icon_name='stock-share-mesh') + style.apply_stylesheet(icon, 'menu-action-icon') self.add_action(icon, ActivityMenu.ACTION_SHARE) - icon = IconItem(icon_name='stock-close') + icon = CanvasIcon(icon_name='stock-close') + style.apply_stylesheet(icon, 'menu-action-icon') self.add_action(icon, ActivityMenu.ACTION_CLOSE) class ActivityIcon(MenuIcon): @@ -64,22 +64,22 @@ class ZoomBox(hippo.CanvasBox): icon = CanvasIcon(icon_name='stock-zoom-mesh') style.apply_stylesheet(icon, 'frame-zoom-icon') icon.connect('activated', self._level_clicked_cb, sugar.ZOOM_MESH) - self.append(icon, 0) + self.append(icon) icon = CanvasIcon(icon_name='stock-zoom-friends') style.apply_stylesheet(icon, 'frame-zoom-icon') icon.connect('activated', self._level_clicked_cb, sugar.ZOOM_FRIENDS) - self.append(icon, 0) + self.append(icon) icon = CanvasIcon(icon_name='stock-zoom-home') style.apply_stylesheet(icon, 'frame-zoom-icon') icon.connect('activated', self._level_clicked_cb, sugar.ZOOM_HOME) - self.append(icon, 0) + self.append(icon) icon = CanvasIcon(icon_name='stock-zoom-activity') style.apply_stylesheet(icon, 'frame-zoom-icon') icon.connect('activated', self._level_clicked_cb, sugar.ZOOM_ACTIVITY) - self.append(icon, 0) + self.append(icon) shell.connect('activity-changed', self._activity_changed_cb) self._set_current_activity(shell.get_current_activity()) diff --git a/sugar/graphics/__init__.py b/sugar/graphics/__init__.py index 8bfa7eb5..b494b9f9 100644 --- a/sugar/graphics/__init__.py +++ b/sugar/graphics/__init__.py @@ -18,3 +18,8 @@ _stylesheet = { 'size' : _medium_icon_size } style.register_stylesheet('frame-zoom-icon', _stylesheet) + +_stylesheet = { + 'size' : _medium_icon_size +} +style.register_stylesheet('menu-action-icon', _stylesheet) diff --git a/sugar/graphics/grid.py b/sugar/graphics/grid.py index 8ee124a3..8d8d2ab2 100644 --- a/sugar/graphics/grid.py +++ b/sugar/graphics/grid.py @@ -7,9 +7,13 @@ class Grid(object): def __init__(self): self._factor = gtk.gdk.screen_width() / COLS - def point(self, x, y): - return [x * self._factor, y * self._factor] + def point(self, grid_x, grid_y): + return [grid_x * self._factor, grid_y * self._factor] - def rectangle(self, x, y, width, height): - return [x * self._factor, y * self._factor, - width * self._factor, height * self._factor] + def rectangle(self, grid_x, grid_y, grid_w, grid_h): + return [grid_x * self._factor, grid_y * self._factor, + grid_w * self._factor, grid_h * self._factor] + + def fit_point(self, x, y): + return [int(x / self._factor), int(y / self._factor)] + diff --git a/sugar/graphics/menu.py b/sugar/graphics/menu.py new file mode 100644 index 00000000..9a85bcf4 --- /dev/null +++ b/sugar/graphics/menu.py @@ -0,0 +1,50 @@ +import gtk +import hippo +import gobject + +from sugar.graphics.canvasicon import CanvasIcon + +class Menu(gtk.Window): + __gsignals__ = { + 'action': (gobject.SIGNAL_RUN_FIRST, + gobject.TYPE_NONE, ([int])), + } + + def __init__(self, title, content_box=None): + gtk.Window.__init__(self, gtk.WINDOW_POPUP) + + canvas = hippo.Canvas() + self.add(canvas) + canvas.show() + + self._root = hippo.CanvasBox(background_color=0x000000FF, + spacing=6) + canvas.set_root(self._root) + + text = hippo.CanvasText(text=title, color=0xFFFFFFFF) + self._root.append(text) + + if content_box: + separator = self._create_separator() + self._root.append(separator) + self._root.append(content_box) + + separator = self._create_separator() + self._root.append(separator) + + self._action_box = hippo.CanvasBox( + orientation=hippo.ORIENTATION_HORIZONTAL) + self._root.append(self._action_box) + + def _create_separator(self): + separator = hippo.CanvasBox(background_color=0xFFFFFFFF, + border_left=6, border_right=6, + box_height=2) + return separator + + def add_action(self, icon, action_id): + icon.connect('activated', self._action_clicked_cb, action_id) + self._action_box.append(icon) + + def _action_clicked_cb(self, icon, action): + self.emit('action', action) diff --git a/sugar/graphics/menuicon.py b/sugar/graphics/menuicon.py index 88a8d561..3f6d4771 100644 --- a/sugar/graphics/menuicon.py +++ b/sugar/graphics/menuicon.py @@ -4,26 +4,14 @@ import gobject from sugar.graphics.canvasicon import CanvasIcon class _MenuStrategy: - def get_menu_position(self, menu, grid_x1, grid_y1, grid_x2, grid_y2): - grid_x = grid_x2 - if grid_x + menu.get_width() > Grid.COLS: - grid_x = grid_x1 - menu.get_width() + 1 - - grid_y = grid_y1 - - if grid_y < 0: - grid_y = 0 - if grid_y + menu.get_width() > Grid.ROWS: - grid_y = Grid.ROWS - menu.get_width() - - return [grid_x, grid_y] + def get_menu_position(self, menu, x1, y1, x2, y2): + return [x1, y1] class MenuIcon(CanvasIcon): def __init__(self, menu_shell, **kwargs): CanvasIcon.__init__(self, **kwargs) self._menu_shell = menu_shell - self._grid = menu_shell.get_grid() self._menu = None self._hover_menu = False self._popdown_on_leave = False @@ -46,24 +34,16 @@ class MenuIcon(CanvasIcon): self._menu_shell.set_active(None) - grid = self._shell.get_grid() self._menu = self.create_menu() self._menu.connect('enter-notify-event', self._menu_enter_notify_event_cb) self._menu.connect('leave-notify-event', self._menu_leave_notify_event_cb) - [grid_x1, grid_y1] = grid.convert_from_screen(x1, y1) - [grid_x2, grid_y2] = grid.convert_from_screen(x2, y2) - strategy = self._menu_strategy - [grid_x, grid_y] = strategy.get_menu_position(self._menu, - grid_x1, grid_y1, - grid_x2, grid_y2) - - grid.set_constraints(self._menu, grid_x, grid_y, - self._menu.get_width(), self._menu.get_height()) + [x, y] = strategy.get_menu_position(self._menu, x1, y1, x2, y2) + self._menu.move(x, y) self._menu.show() self._menu_shell.set_active(self)