From d253ccd0be27168c59231542cf39f8f82c0dead4 Mon Sep 17 00:00:00 2001 From: Johan Dahlin Date: Thu, 14 Feb 2008 14:07:01 +0000 Subject: Add an application example, which includes hoovering statusbar updates 2008-02-14 Johan Dahlin * examples/Makefile.am: * examples/gtk/application.py: Add an application example, which includes hoovering statusbar updates using a ui manager. svn path=/trunk/; revision=2963 --- ChangeLog | 7 ++ examples/Makefile.am | 1 + examples/gtk/application.py | 175 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 183 insertions(+) create mode 100644 examples/gtk/application.py diff --git a/ChangeLog b/ChangeLog index f21c29b9..19d2742f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2008-02-14 Johan Dahlin + + * examples/Makefile.am: + * examples/gtk/application.py: + Add an application example, which includes hoovering statusbar + updates using a ui manager. + 2008-02-06 Yevgen Muntyan * gtk/gdk.override: diff --git a/examples/Makefile.am b/examples/Makefile.am index 58d07ae1..43f33103 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -19,6 +19,7 @@ EXTRA_DIST = \ gobject/editable-interface.py \ gobject/properties.py \ gobject/signal.py \ + gtk/application.py \ gtk/bin.py \ gtk/customtreemodel.py \ gtk/filechooser.py \ diff --git a/examples/gtk/application.py b/examples/gtk/application.py new file mode 100644 index 00000000..10f0ea2f --- /dev/null +++ b/examples/gtk/application.py @@ -0,0 +1,175 @@ +# By Jarek Zgoda and Johan Dahlin + +import gtk + +ui_string = """ + + + + + + + + + + + + + + + + + + + + +""" + + +class Application(gtk.Window): + def __init__(self, title=''): + gtk.Window.__init__(self) + self.connect('delete-event', self._on_delete_event) + self.set_position(gtk.WIN_POS_CENTER) + self.set_size_request(400, 200) + self.set_title(title) + + main_vbox = gtk.VBox() + self.add(main_vbox) + main_vbox.show() + + uimgr = self._create_ui() + uimgr.connect('connect-proxy', + self._on_uimanager__connect_proxy) + uimgr.connect('disconnect-proxy', + self._on_uimanager__disconnect_proxy) + + menubar = uimgr.get_widget('/Menubar') + main_vbox.pack_start(menubar, expand=False) + menubar.show() + + toolbar = uimgr.get_widget('/Toolbar') + main_vbox.pack_start(toolbar, expand=False) + toolbar.realize() + toolbar.show() + + status = gtk.Statusbar() + main_vbox.pack_end(status, expand=False) + status.show() + self.statusbar = status + + self._menu_cix = -1 + + def _create_ui(self): + ag = gtk.ActionGroup('AppActions') + actions = [ + ('FileMenu', None, '_File'), + ('New', gtk.STOCK_NEW, '_New', 'N', + 'Create a new file', self._on_action_new), + ('Open', gtk.STOCK_OPEN, '_Open', 'O', + 'Open a file', self._on_action_open), + ('Save', gtk.STOCK_SAVE, '_Save', 'S', + 'Save a file', self._on_action_save), + ('Close', gtk.STOCK_CLOSE, '_Close', 'W', + 'Close the current window', self._on_action_close), + ('Quit', gtk.STOCK_QUIT, '_Quit', 'Q', + 'Quit application', self._on_action_quit), + ('HelpMenu', None, '_Help'), + ('About', None, '_About', None, 'About application', + self._on_action_about), + ] + ag.add_actions(actions) + ui = gtk.UIManager() + ui.insert_action_group(ag, 0) + ui.add_ui_from_string(ui_string) + self.add_accel_group(ui.get_accel_group()) + return ui + + def _on_uimanager__connect_proxy(self, uimgr, action, widget): + tooltip = action.get_property('tooltip') + if not tooltip: + return + + if isinstance(widget, gtk.MenuItem): + cid = widget.connect('select', self._on_menu_item__select, + tooltip) + cid2 = widget.connect('deselect', self._on_menu_item__deselect) + widget.set_data('pygtk-app::proxy-signal-ids', (cid, cid2)) + elif isinstance(widget, gtk.ToolButton): + cid = widget.child.connect('enter', self._on_tool_button__enter, + tooltip) + cid2 = widget.child.connect('leave', self._on_tool_button__leave) + widget.set_data('pygtk-app::proxy-signal-ids', (cid, cid2)) + + def _on_uimanager__disconnect_proxy(self, uimgr, action, widget): + cids = widget.get_data('pygtk-app::proxy-signal-ids') + if not cids: + return + + if isinstance(widget, gtk.ToolButton): + widget = widget.child + + for name, cid in cids: + widget.disconnect(cid) + + def _on_menu_item__select(self, menuitem, tooltip): + self.statusbar.push(self._menu_cix, tooltip) + + def _on_menu_item__deselect(self, menuitem): + self.statusbar.pop(self._menu_cix) + + def _on_tool_button__enter(self, toolbutton, tooltip): + self.statusbar.push(self._menu_cix, tooltip) + + def _on_tool_button__leave(self, toolbutton): + self.statusbar.pop(self._menu_cix) + + def _on_action_new(self, action): + self.new() + + def _on_action_open(self, action): + self.open() + + def _on_action_save(self, action): + self.save() + + def _on_action_close(self, action): + self.close() + + def _on_action_quit(self, action): + self.quit() + + def _on_action_about(self, action): + self.about() + + def _on_delete_event(self, window, event): + self.quit() + + # Override in subclass + + def new(self): + raise NotImplemented("Open") + + def open(self): + raise NotImplemented("Open") + + def save(self): + raise NotImplemented("Save") + + def close(self): + raise NotImplemented("Close") + + def about(self): + raise NotImplemented("About") + + def run(self): + self.show() + gtk.main() + + def quit(self): + gtk.main_quit() + +if __name__ == '__main__': + a = Application(title="TestApp") + a.run() + -- cgit v1.2.1