diff options
Diffstat (limited to 'creactiweb/_templates/+package+/ui/toolbar.py_tmpl')
-rw-r--r-- | creactiweb/_templates/+package+/ui/toolbar.py_tmpl | 180 |
1 files changed, 180 insertions, 0 deletions
diff --git a/creactiweb/_templates/+package+/ui/toolbar.py_tmpl b/creactiweb/_templates/+package+/ui/toolbar.py_tmpl new file mode 100644 index 0000000..a088ab5 --- /dev/null +++ b/creactiweb/_templates/+package+/ui/toolbar.py_tmpl @@ -0,0 +1,180 @@ + +# python import +import logging +# ... +from functools import partial +from gettext import gettext as _ + +# gtk import +import gtk, glib + +# sugar import +from sugar.activity import activity + +# sugar import +from sugar.graphics.toolbutton import ToolButton + +# server import +from server.flask import logger + + +def _cb_next(widget, toolbar, next_): + # update current screen + toolbar.activity.change_screen(next_) + + +BUTTONS = { + 'ajax': ['document-generic', _cb_next], + 'gallery': ['document-generic', _cb_next], + } + +TOOLBARS = { + 'menu': [ + ['ajax', 'gallery'], + [] + ], + } + +TITLES = { + 'menu': { + 'toolbox': _('Demo'), + 'buttons': { + 'ajax': _('Ajax'), + 'gallery': _('Gallery'), + } + } + } + + +class Toolbar(gtk.Toolbar): + + def __init__(self, activity, name='default'): + # init parent + gtk.Toolbar.__init__(self) + # keep the name + self.set_name(name) + # keep activity + self.activity = activity + # adjustment + self._adjustment = None + # init widget dict + self._button_dict = dict() + # init buttons + self._init_buttons() + # add tab in toolbox + _toolbox_name = TITLES[self.name]['toolbox'] + if _toolbox_name is None: + pass + else: + _toolbox = self.activity.get_toolbox() + _toolbox.add_toolbar(_toolbox_name, self) + # connect focus event + self.connect('focus', self._on_focus) + # show + self.show() + + def _switch(self, name): + # update toolbar + self._clear_buttons() + # switch name + self.set_name(name) + # reset + self._init_buttons() + + def _clear_buttons(self): + # remove all + for _k in self._button_dict.keys(): + self._remove_button(_k) + # remove separators + for _c in self.get_children(): + # remove it + self.remove(_c) + # and destroy it + _c.destroy() + + def _init_buttons(self): + # add buttons + _b_left, _b_right = TOOLBARS[self.name] + # place left buttons + for _b in _b_left: + # add button + self._add_button(_b) + # add expanded separator + _separator = gtk.SeparatorToolItem() + _separator.set_draw(False) + _separator.set_expand(True) + _separator.show() + self.add(_separator) + # place right buttons + for _b in _b_right: + # add button + self._add_button(_b) + + def _on_focus(self, widget, direction): + # update current screen + self.activity.change_screen(self.name) + + def _has_button(self, button_id): + return button_id in self._button_dict + + def _add_button(self, button_id, index=None): + # get button icon and cb + _icon, _cb = BUTTONS[button_id] + # manage separator + if button_id == 'separator': + _buton = gtk.SeparatorToolItem() + _buton.set_draw(True) + # standard button + elif button_id in BUTTONS: + # get tooltip + _tooltip = TITLES[self.name]['buttons'][button_id] + # set icon + _buton = ToolButton(_icon) + # set tooltip + _buton.set_tooltip(_tooltip) + # do connect + if _cb is None: + pass + else: + _buton.connect('clicked', _cb, self, button_id) + # ?? + else: + # ERROR + logger.error('[toolbar] _add_button - ??: %s' % button_id) + return + # update the button dict + self._button_dict[button_id] = _buton + # add to the toolbar + if index is None: + self.add(_buton) + else: + self.insert(_buton, index) + # show it + _buton.show() + + def _remove_button(self, button_id): + # little check + if button_id in self._button_dict: + # get button + _buton = self._button_dict.pop(button_id) + # do remove - do it safe + glib.idle_add(partial(self.remove, _buton)) + # and destroy - safe + glib.idle_add(_buton.destroy) + # ?? + else: + # ERROR + logger.error('[toolbar] _remove_button - unknown: %s' % button_id) + + def _replace_button(self, button_id_out, button_id_in): + # little check + if button_id_out in self._button_dict: + # ... + _index = self.get_item_index(self._button_dict[button_id_out]) + # do remove + self._remove_button(button_id_out) + # do it safe + glib.idle_add(partial(self._add_button, button_id_in, _index)) + # ?? + else: + pass |