2006-10-15 01:08:44 +02:00
|
|
|
# Copyright (C) 2006, Red Hat, Inc.
|
|
|
|
#
|
|
|
|
# This library is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
# version 2 of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this library; if not, write to the
|
|
|
|
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
# Boston, MA 02111-1307, USA.
|
|
|
|
|
2006-10-02 16:37:30 +02:00
|
|
|
import gtk
|
|
|
|
import hippo
|
|
|
|
import gobject
|
|
|
|
|
|
|
|
from sugar.graphics.canvasicon import CanvasIcon
|
2006-10-05 14:52:33 +02:00
|
|
|
from sugar.graphics import style
|
2006-10-02 16:37:30 +02:00
|
|
|
|
|
|
|
class Menu(gtk.Window):
|
2006-12-04 20:12:24 +01:00
|
|
|
__gsignals__ = {
|
|
|
|
'action': (gobject.SIGNAL_RUN_FIRST,
|
|
|
|
gobject.TYPE_NONE, ([int])),
|
|
|
|
}
|
2006-10-02 16:37:30 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def __init__(self, title=None, content_box=None):
|
|
|
|
gtk.Window.__init__(self, gtk.WINDOW_POPUP)
|
2006-10-02 16:37:30 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
canvas = hippo.Canvas()
|
|
|
|
self.add(canvas)
|
|
|
|
canvas.show()
|
2006-10-02 16:37:30 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
self._root = hippo.CanvasBox()
|
|
|
|
style.apply_stylesheet(self._root, 'menu')
|
|
|
|
canvas.set_root(self._root)
|
2006-10-02 16:37:30 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
if title:
|
|
|
|
self._title_item = hippo.CanvasText(text=title)
|
|
|
|
style.apply_stylesheet(self._title_item, 'menu.Title')
|
|
|
|
self._root.append(self._title_item)
|
|
|
|
else:
|
|
|
|
self._title_item = None
|
2006-10-02 16:37:30 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
if content_box:
|
|
|
|
separator = self._create_separator()
|
|
|
|
self._root.append(separator)
|
|
|
|
self._root.append(content_box)
|
2006-10-02 16:37:30 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
self._action_box = None
|
|
|
|
self._item_box = None
|
2006-10-05 16:29:55 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def _create_separator(self):
|
|
|
|
separator = hippo.CanvasBox()
|
|
|
|
style.apply_stylesheet(separator, 'menu.Separator')
|
|
|
|
return separator
|
2006-10-05 16:29:55 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def _create_item_box(self):
|
|
|
|
if self._title_item:
|
|
|
|
separator = self._create_separator()
|
|
|
|
self._root.append(separator)
|
2006-11-11 23:51:16 +01:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
self._item_box = hippo.CanvasBox(
|
|
|
|
orientation=hippo.ORIENTATION_VERTICAL)
|
|
|
|
self._root.append(self._item_box)
|
2006-11-11 23:51:16 +01:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def _create_action_box(self):
|
|
|
|
separator = self._create_separator()
|
|
|
|
self._root.append(separator)
|
2006-10-02 16:37:30 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
self._action_box = hippo.CanvasBox(
|
|
|
|
orientation=hippo.ORIENTATION_HORIZONTAL)
|
|
|
|
self._root.append(self._action_box)
|
2006-10-02 16:37:30 +02:00
|
|
|
|
2007-01-05 21:13:46 +01:00
|
|
|
def add_item(self, label, action_id=None):
|
2006-12-04 20:12:24 +01:00
|
|
|
if not self._item_box:
|
|
|
|
self._create_item_box()
|
2006-11-11 23:51:16 +01:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
text = hippo.CanvasText(text=label)
|
|
|
|
style.apply_stylesheet(text, 'menu.Item')
|
2006-11-11 23:51:16 +01:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
# FIXME need a way to make hippo items activable in python
|
2007-01-05 21:13:46 +01:00
|
|
|
if action_id:
|
|
|
|
text.connect('button-press-event', self._item_clicked_cb, action_id)
|
|
|
|
#text.connect('activated', self._action_clicked_cb, action_id)
|
2006-11-11 23:51:16 +01:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
self._item_box.append(text)
|
2006-11-11 23:51:16 +01:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def add_action(self, icon, action_id):
|
|
|
|
if not self._action_box:
|
|
|
|
self._create_action_box()
|
2006-10-05 16:29:55 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
style.apply_stylesheet(icon, 'menu.ActionIcon')
|
|
|
|
icon.connect('activated', self._action_clicked_cb, action_id)
|
|
|
|
self._action_box.append(icon)
|
2006-10-02 16:37:30 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def remove_action(self, icon):
|
|
|
|
self._action_box.remove(icon)
|
2006-11-15 13:56:19 +01:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def _item_clicked_cb(self, icon, event, action):
|
|
|
|
self.emit('action', action)
|
2006-11-11 23:51:16 +01:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def _action_clicked_cb(self, icon, action):
|
|
|
|
self.emit('action', action)
|
2007-01-05 21:13:46 +01:00
|
|
|
|
|
|
|
def set_title(self, title):
|
|
|
|
self._title_item.set_text(title)
|