From e78200efea42e564d5aefef76627450a7988024f Mon Sep 17 00:00:00 2001 From: Huang Peng Date: Tue, 24 Jun 2008 10:32:48 +0800 Subject: [PATCH] WIP property. --- panel/languagebar.py | 71 +++------------------------ panel/menu.py | 134 +++++++++++++++++++++++++++++++++++++++++++++++++++ panel/toolitem.py | 37 ++++---------- 3 files changed, 150 insertions(+), 92 deletions(-) diff --git a/panel/languagebar.py b/panel/languagebar.py index eda5beb..814cdcd 100644 --- a/panel/languagebar.py +++ b/panel/languagebar.py @@ -25,8 +25,9 @@ import gobject import ibus from image import Image from handle import Handle -from menu import menu_position -from toolitem import ToolButton, ToggleToolButton +from toolitem import ToolButton,\ + ToggleToolButton, \ + MenuToolButton ICON_SIZE = gtk.ICON_SIZE_MENU @@ -106,10 +107,9 @@ class LanguageBar (gtk.Toolbar): widget.connect ("property-activate", lambda w, n, s: self.emit ("property-activate", n, s)) elif prop._type == ibus.PROP_TYPE_MENU: - widget = ToolButton (prop = prop) - menu = self._create_prop_menu (prop.get_sub_props ()) - widget.connect ("clicked", - self._property_menu_clicked, prop, menu) + widget = MenuToolButton (prop = prop) + widget.connect ("property-activate", + lambda w, n, s: self.emit ("property-activate", n, s)) elif prop._type == PROP_TYPE_SEPARATOR: widget = gtk.SeparatorToolItem () else: @@ -142,64 +142,5 @@ class LanguageBar (gtk.Toolbar): self._toplevel.hide_all () gtk.Toolbar.hide_all (self) - def _create_prop_menu (self, props): - menu = gtk.Menu () - menu.set_take_focus (False) - - radio_group = None - - for prop in props: - if prop._type == ibus.PROP_TYPE_NORMAL: - item = gtk.ImageMenuItem (prop._label) - if prop._icon: - item.set_image (gtk.image_new_from_icon_name (prop._icon, gtk.ICON_SIZE_MENU)) - item.connect ("activate", self._property_clicked, prop) - elif prop._type == ibus.PROP_TYPE_TOGGLE: - item = gtk.CheckMenuItem (label = prop._label) - item.set_active (prop._state == ibus.PROP_STATE_CHECKED) - item.connect ("toggled", self._property_clicked, prop) - elif prop._type == ibus.PROP_TYPE_RADIO: - item = gtk.RadioMenuItem (group = radio_group, label = prop._label) - item.set_active (prop._state == ibus.PROP_STATE_CHECKED) - if radio_group == None: - radio_group = item - item.connect ("toggled", self._property_clicked, prop) - elif prop._type == ibus.PROP_TYPE_SEPARATOR: - item = gtk.SeparatorMenuItem () - radio_group = None - elif prop._type == ibus.PROP_TYPE_MENU: - item = gtk.ImageMenuItem (prop._label) - if prop._icon: - item.set_image (gtk.image_new_from_icon_name (prop._icon, gtk.ICON_SIZE_MENU)) - item.set_submenu (self._create_prop_menu (prop.get_sub_props ())) - else: - assert Fasle - - - item.set_sensitive (prop._sensitive) - item.set_no_show_all (True) - if prop._visible: - item.show () - else: - item.hide () - - menu.append (item) - - menu.show_all () - - return menu - - def _property_clicked (self, widget, prop): - if prop._type in (ibus.PROP_TYPE_TOGGLE, ibus.PROP_TYPE_RADIO): - if widget.get_active (): - prop._state = ibus.PROP_STATE_CHECKED - else: - prop._state = ibus.PROP_STATE_UNCHECKED - self.emit ("property-activate", prop._name, prop._state) - - def _property_menu_clicked (self, widget, prop, menu): - menu.popup (None, None, menu_position, - 0, gtk.get_current_event_time (), widget) - gobject.type_register (LanguageBar, "IBusLanguageBar") diff --git a/panel/menu.py b/panel/menu.py index f57cc95..75cf914 100644 --- a/panel/menu.py +++ b/panel/menu.py @@ -19,6 +19,140 @@ # Free Software Foundation, Inc., 59 Temple Place, Suite 330, # Boston, MA 02111-1307 USA +import gtk +import gobject +import ibus + +class Menu (gtk.Menu): + __gsignals__ = { + "property-activate" : ( + gobject.SIGNAL_RUN_FIRST, + gobject.TYPE_NONE, + (gobject.TYPE_STRING, gobject.TYPE_INT)), + } + + def __init__ (self, prop): + self._prop = prop + self._items = [] + gtk.Menu.__init__ (self) + self.set_take_focus (False) + self._create_items (self._prop.get_sub_props ()) + self.show_all () + + def _create_items (self, props): + radio_group = None + + for prop in props: + if prop._type == ibus.PROP_TYPE_NORMAL: + item = gtk.ImageMenuItem (prop) + elif prop._type == ibus.PROP_TYPE_TOGGLE: + item = CheckMenuItem (prop) + elif prop._type == ibus.PROP_TYPE_RADIO: + item = RadioMenuItem (radio_group, prop) + radio_group = item + elif prop._type == ibus.PROP_TYPE_SEPARATOR: + item = SeparatorMenuItem () + radio_group = None + elif prop._type == ibus.PROP_TYPE_MENU: + item = gtk.ImageMenuItem (prop) + item.set_submenu (Menu (prop)) + else: + assert Fasle + + item.set_sensitive (prop._sensitive) + item.set_no_show_all (True) + if prop._visible: + item.show () + else: + item.hide () + + item.connect ("property-activate", lambda w,n,s: self.emit ("property-activate", n, s)) + + self.append (item) + self._items.append (item) + + def popup (self, button, active_time, widget): + gtk.Menu.popup (self, None, None, menu_position, + button, active_time, widget) + + def _property_clicked (self, item, prop): + pass + + +class ImageMenuItem (gtk.ImageMenuItem): + __gsignals__ = { + "property-activate" : ( + gobject.SIGNAL_RUN_FIRST, + gobject.TYPE_NONE, + (gobject.TYPE_STRING, gobject.TYPE_INT)), + } + + def __init__ (self, prop): + self._prop = prop + gtk.ImageMenuItem.__init__ (self, label = prop._label) + if prop._icon: + self.set_image (gtk.image_new_from_icon_name (prop._icon, gtk.ICON_SIZE_MENU)) + + def do_activate (self): + self.emit ("property-activate", self._prop._name, self._prop._state) + + +class CheckMenuItem (gtk.CheckMenuItem): + __gsignals__ = { + "property-activate" : ( + gobject.SIGNAL_RUN_FIRST, + gobject.TYPE_NONE, + (gobject.TYPE_STRING, gobject.TYPE_INT)), + } + + def __init__ (self, prop): + self._prop = prop + gtk.CheckMenuItem.__init__ (self, label = prop._label) + self.set_active (prop._state == ibus.PROP_STATE_CHECKED) + + def do_toggled (self): + if self.get_active (): + self._prop._state = ibus.PROP_STATE_CHECKED + else: + self._prop._state = ibus.PROP_STATE_UNCHECKED + self.emit ("property-activate", self._prop._name, self._prop._state) + + +class RadioMenuItem (gtk.RadioMenuItem): + __gsignals__ = { + "property-activate" : ( + gobject.SIGNAL_RUN_FIRST, + gobject.TYPE_NONE, + (gobject.TYPE_STRING, gobject.TYPE_INT)), + } + + def __init__ (self, group, prop): + self._prop = prop + gtk.RadioMenuItem.__init__ (self, group, label = prop._label) + self.set_active (prop._state == ibus.PROP_STATE_CHECKED) + + def do_toggled (self): + if self.get_active (): + self._prop._state = ibus.PROP_STATE_CHECKED + else: + self._prop._state = ibus.PROP_STATE_UNCHECKED + self.emit ("property-activate", self._prop._name, self._prop._state) + +class SeparatorMenuItem (gtk.SeparatorMenuItem): + __gsignals__ = { + "property-activate" : ( + gobject.SIGNAL_RUN_FIRST, + gobject.TYPE_NONE, + (gobject.TYPE_STRING, gobject.TYPE_INT)), + } + + +gobject.type_register (Menu, "IBusMenu") +gobject.type_register (ImageMenuItem, "IBusImageMenuItem") +gobject.type_register (CheckMenuItem, "IBusCheckMenuItem") +gobject.type_register (RadioMenuItem, "IBusRadioMenuItem") +gobject.type_register (SeparatorMenuItem, "IBusSeparatorMenuItem") + def menu_position (menu, button): screen = button.get_screen () monitor = screen.get_monitor_at_window (button.window) diff --git a/panel/toolitem.py b/panel/toolitem.py index 6f2ca58..869654b 100644 --- a/panel/toolitem.py +++ b/panel/toolitem.py @@ -22,6 +22,7 @@ import gtk import gobject import ibus +from menu import * class ToolButton (gtk.ToolButton): __gsignals__ = { @@ -33,7 +34,8 @@ class ToolButton (gtk.ToolButton): def __init__ (self, label = None, icon = None, prop = None): if prop == None: - prop = ibus.Property (name= "", label = label, icon = icon) + prop = ibus.Property (name= "", type = ibus.PROP_TYPE_NORMAL, + label = label, icon = icon) self._prop = prop gtk.ToolButton.__init__ (self, label = prop._label) @@ -64,7 +66,8 @@ class ToggleToolButton (gtk.ToggleToolButton): def __init__ (self, label = None, icon = None, state = ibus.PROP_STATE_UNCHECKED, prop = None): if prop == None: - prop = ibus.Property (name = "", label = label, icon = icon, state = state) + prop = ibus.Property (name = "", type = ibus.PROP_TYPE_TOGGLE, + label = label, icon = icon, state = state) self._prop = prop gtk.ToggleToolButton.__init__ (self) @@ -98,34 +101,14 @@ class ToggleToolButton (gtk.ToggleToolButton): gobject.type_register (ToggleToolButton, "IBusToggleToolButton") -class MenuToolButton (gtk.ToolButton): - __gsignals__ = { - "property-activate" : ( - gobject.SIGNAL_RUN_FIRST, - gobject.TYPE_NONE, - (gobject.TYPE_STRING, gobject.TYPE_INT)), - } - +class MenuToolButton (ToolButton): def __init__ (self, label = None, icon = None, prop = None): - if prop == None: - prop = ibus.Property (name= "", label = label, icon = icon) - self._prop = prop - - gtk.ToolButton.__init__ (self, label = prop._label) - self.set_icon_name (prop._icon) - - def set_icon_name (self, icon_name): - if icon_name: - gtk.ToolButton.set_icon_name (self, icon_name) - self.set_is_important (False) - else: - gtk.ToolButton.set_icon_name (self, None) - self.set_is_important (True) - - self._prop._icon = icon_name + ToolButton.__init__ (self, label, icon, prop) + self._menu = Menu (prop) + self._menu.connect ("property-activate", lambda w,n,s:self.emit ("property-activate", n, s)) def do_clicked (self): - self.emit ("property-activate", self._prop._name, self._prop._state) + self._menu.popup (0, gtk.get_current_event_time (), self) gobject.type_register (MenuToolButton, "MenuToolButton") -- 2.7.4