From cda47a41f4fd06cc06f94eec1451fac33e423ade Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Fri, 9 Mar 2007 11:22:51 +0100 Subject: [PATCH] Implement ToggleIconButton --- sugar/graphics/Makefile.am | 1 + sugar/graphics/color.py | 4 +- sugar/graphics/iconbutton.py | 4 +- sugar/graphics/toggleiconbutton.py | 71 ++++++++++++++++++++++++++++++ tests/test-button.py | 8 ++++ 5 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 sugar/graphics/toggleiconbutton.py diff --git a/sugar/graphics/Makefile.am b/sugar/graphics/Makefile.am index 6a45b3c7..9479ea23 100644 --- a/sugar/graphics/Makefile.am +++ b/sugar/graphics/Makefile.am @@ -20,6 +20,7 @@ sugar_PYTHON = \ snowflakebox.py \ spreadbox.py \ timeline.py \ + toggleiconbutton.py \ toolbar.py \ units.py \ window.py \ diff --git a/sugar/graphics/color.py b/sugar/graphics/color.py index 068aa160..1aa0b916 100644 --- a/sugar/graphics/color.py +++ b/sugar/graphics/color.py @@ -21,7 +21,8 @@ _system_colors = { 'button-hover' : '#808080', 'button-background-hover' : '#000000', 'icon-stroke-inactive' : '#757575', - 'icon-fill-inactive' : '#9D9FA1' + 'icon-fill-inactive' : '#9D9FA1', + 'toggle-button-background' : '#A1A5A8' } def _html_to_rgb(html_color): @@ -100,3 +101,4 @@ BUTTON_HOVER = SystemColor('button-hover') BUTTON_BACKGROUND_HOVER = SystemColor('button-background-hover') ICON_FILL_INACTIVE = SystemColor('icon-fill-inactive') ICON_STROKE_INACTIVE = SystemColor('icon-stroke-inactive') +TOGGLE_BUTTON_BACKGROUND = SystemColor('toggle-button-background') diff --git a/sugar/graphics/iconbutton.py b/sugar/graphics/iconbutton.py index 822ddf1f..5c807a7f 100644 --- a/sugar/graphics/iconbutton.py +++ b/sugar/graphics/iconbutton.py @@ -45,8 +45,8 @@ class IconButton(CanvasIcon): self._set_size(STANDARD_SIZE) - self.connect('button-press-event', - self._icon_button_button_press_event_cb) + self.connect_after('button-press-event', + self._icon_button_button_press_event_cb) def _set_size(self, size): if size == SMALL_SIZE: diff --git a/sugar/graphics/toggleiconbutton.py b/sugar/graphics/toggleiconbutton.py new file mode 100644 index 00000000..a637fcd1 --- /dev/null +++ b/sugar/graphics/toggleiconbutton.py @@ -0,0 +1,71 @@ +# Copyright (C) 2007, Red Hat +# +# 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. + +import gobject + +from sugar.graphics.iconbutton import IconButton +from sugar.graphics import color + +class ToggleIconButton(IconButton): + __gtype_name__ = 'SugarToggleIconButton' + + __gproperties__ = { + 'toggled' : (bool, None, None, False, + gobject.PARAM_READWRITE) + } + + def __init__(self, **kwargs): + self._toggled = False + + IconButton.__init__(self, **kwargs) + + self.connect('button-press-event', + self._toggle_icon_button_press_event_cb) + + def _get_bg_color(self): + if self._toggled: + col = color.TOGGLE_BUTTON_BACKGROUND + else: + col = color.BUTTON_BACKGROUND_NORMAL + + return col.get_int() + + def _set_toggled(self, toggled): + self._toggled = toggled + self.props.background_color = self._get_bg_color() + + def do_set_property(self, pspec, value): + if pspec.name == 'toggled': + self._set_toggled(value) + else: + IconButton.do_set_property(self, pspec, value) + + def do_get_property(self, pspec): + if pspec.name == 'toggled': + return self._toggled + + return IconButton.do_get_property(self, pspec) + + def _toggle_icon_button_press_event_cb(self, widget, event): + self.props.toggled = not self._toggled + return True + + def prelight(self, enter): + if enter: + IconButton.prelight(self, enter) + else: + self.props.background_color = self._get_bg_color() diff --git a/tests/test-button.py b/tests/test-button.py index 0f3ded26..9cceb92d 100755 --- a/tests/test-button.py +++ b/tests/test-button.py @@ -23,12 +23,16 @@ import hippo from sugar.graphics.toolbar import Toolbar from sugar.graphics.iconbutton import IconButton +from sugar.graphics.toggleiconbutton import ToggleIconButton from sugar.graphics.button import Button from sugar.graphics.entry import Entry def _button_activated_cb(button): print "_button_activated_cb" +def _toggled_changed_cb(button, pspec): + print "Toggle state: %d" % button.props.toggled + window = gtk.Window() window.connect("destroy", lambda w: gtk.main_quit()) window.show() @@ -48,6 +52,10 @@ for i in [1, 2]: icon_button = IconButton(icon_name='theme:stock-close') toolbar.append(icon_button) + toggle = ToggleIconButton(icon_name='theme:stock-back') + toggle.connect('notify::toggled', _toggled_changed_cb) + toolbar.append(toggle) + button = Button(text='Click me!', icon_name='theme:stock-close') button.connect('activated', _button_activated_cb) toolbar.append(button)