Test adding of toggle actions
authorEnrico Zini <enrico@enricozini.org>
Mon, 15 Jun 2009 08:53:14 +0000 (16:53 +0800)
committerEnrico Zini <enrico@enricozini.org>
Mon, 15 Jun 2009 08:53:14 +0000 (16:53 +0800)
zavai/__init__.py
zavai/app.py
zavai/menu.py
zavai/registry.py

index 0acc4774cbf2f7700f1861fff029f0ed4e25870e..1a3b58a947cd9d4ac809e96b84de7a710459711c 100644 (file)
@@ -19,7 +19,7 @@
 from conf import read_config
 from plugins import load_plugins
 from registry import Registry, Resource, get_parent
-from menu import Menu, MenuButton, LinkButton
+from menu import Menu, MenuButton, LinkButton, ToggleButton
 from app import Zavai, Applet
 from gps import GPS, GPX
 
index 537ff8516aae54a65fdf1c524dc41241f7fe7f16..29311de8ff6ad1279a81f5306d0f71b71cde9977 100644 (file)
@@ -26,9 +26,18 @@ class Zavai(gtk.Window, zavai.Resource):
         super(Zavai, self).__init__()
         self.registry = registry
         self.current = None
-        self.show_widget("menu.main")
+        self.activate_resource("menu.main")
 
-    def show_widget(self, name):
+    def activate_resource(self, name):
+        widget = self.registry.resource(name)
+        if widget is None:
+            widget = self.registry.resource("menu.main")
+        if isinstance(widget, gtk.Action):
+            widget.activate()
+        else:
+            self.show_widget(name, widget)
+
+    def show_widget(self, name, widget):
         # Remove the current widget.
         # If it is an Applet, stop it
         if self.current is not None:
@@ -39,9 +48,6 @@ class Zavai(gtk.Window, zavai.Resource):
             self.current = None
 
         # Add the new widget. If it is an applet, start it
-        widget = self.registry.resource(name)
-        if widget is None:
-            widget = self.registry.resource("menu.main")
         self.add(widget)
         self.current = name
         if isinstance(widget, zavai.Applet):
index 424ee07bfcdd32b7d36a23714219a4f42be63c1a..629495afda2c6551230517e45191ffe292032332 100644 (file)
@@ -42,11 +42,27 @@ class Menu(gtk.VBox, zavai.Resource):
         self.vbox.pack_start(widget, False, False)
 
 class LinkButton(MenuButton, zavai.Resource):
-    def __init__(self, registry, targetName, label):
-        super(LinkButton, self).__init__(label)
-        self.target = targetName
-        self.registry = registry
-        self.connect("clicked", self.on_click)
+    def __init__(self, registry, targetName, label=None, action=None):
+        if action is not None:
+            super(LinkButton, self).__init__()
+            self.target = targetName
+            self.registry = registry
+            self.action = action
+            action.connect_proxy(self)
+            self.connect("clicked", self.on_click)
+        else:
+            super(LinkButton, self).__init__(label)
+            self.target = targetName
+            self.registry = registry
+            self.action = None
+            self.connect("clicked", self.on_click)
 
     def on_click(self, *args):
-        self.registry.resource("app").show_widget(self.target)
+        self.registry.resource("app").activate_resource(self.target)
+
+class ToggleButton(gtk.ToggleButton):
+    def __init__(self, registry, targetName, action, *args, **kw):
+        super(ToggleButton, self).__init__()
+        # TODO: take this from the configuration
+        self.set_size_request(0, 80)
+        action.connect_proxy(self)
index ac46759b885a7b73e1cf28143b05c334edb5a474..63e9412d07ef89506fde50f19adedf568b168763 100644 (file)
@@ -17,6 +17,7 @@
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 import zavai
+import gtk
 
 def get_parent(s):
     "Get the parent name for s"
@@ -74,6 +75,26 @@ class Registry(object):
         if name.startswith("menu."):
             self.add_to_menu(name)
 
+    def register_action(self, name, obj):
+        """Register an object at the given path.
+
+        Name the path to this object, like "menu.gps.monitor".
+        """
+        if name in self.objects:
+            return KeyError("%s is already registered", name)
+        zavai.info("Registering action", name)
+        self.objects[name] = obj
+
+        if name.startswith("menu."):
+            parent = get_parent(name)
+            if parent is not None:
+                zavai.info("Add action to menu", name, parent)
+                menu = self.menu(parent)
+                if isinstance(obj, gtk.ToggleAction):
+                    menu.add_child(zavai.ToggleButton(self, name, action=obj))
+                else:
+                    menu.add_child(zavai.LinkButton(self, name, action=obj))
+
     def add_to_menu(self, name):
         "Add the applet with the given name to the menu structure"
         parent = get_parent(name)