From de902c41fde70b1a937d71381f702d7551311e90 Mon Sep 17 00:00:00 2001 From: Tomeu Vizoso Date: Mon, 26 Feb 2007 17:35:58 +0100 Subject: [PATCH] Added Button control. --- sugar/graphics/Makefile.am | 1 + sugar/graphics/button.py | 127 +++++++++++++++++++++++++++++++++++ sugar/graphics/iconbutton.py | 3 +- sugar/graphics/roundbox.py | 11 +-- tests/test-button.py | 62 +++++++++++++++++ 5 files changed, 197 insertions(+), 7 deletions(-) create mode 100644 sugar/graphics/button.py create mode 100755 tests/test-button.py diff --git a/sugar/graphics/Makefile.am b/sugar/graphics/Makefile.am index 375857f2..ea115fae 100644 --- a/sugar/graphics/Makefile.am +++ b/sugar/graphics/Makefile.am @@ -2,6 +2,7 @@ sugardir = $(pythondir)/sugar/graphics sugar_PYTHON = \ __init__.py \ bubble.py \ + button.py \ iconbutton.py \ canvasicon.py \ color.py \ diff --git a/sugar/graphics/button.py b/sugar/graphics/button.py new file mode 100644 index 00000000..ed354311 --- /dev/null +++ b/sugar/graphics/button.py @@ -0,0 +1,127 @@ +# Copyright (C) 2007, One Laptop Per Child +# +# 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 sys + +import gobject +import hippo + +from sugar.graphics.canvasicon import CanvasIcon +from sugar.graphics.roundbox import RoundBox +from sugar.graphics import units +from sugar.graphics import color +from sugar.graphics import font + +class Button(hippo.CanvasBox, hippo.CanvasItem): + __gtype_name__ = 'SugarButton' + + __gproperties__ = { + 'active' : (bool, None, None, True, + gobject.PARAM_READWRITE), + 'icon-name' : (str, None, None, None, + gobject.PARAM_READWRITE), + 'text' : (str, None, None, None, + gobject.PARAM_READWRITE) + } + + def __init__(self, **kwargs): + self._active = True + self._icon = None + + self._round_box = RoundBox() + self._round_box.props.border_color = 0 + self._round_box.props.background_color = color.BLACK.get_int() + + self._text_box = hippo.CanvasText() + self._text_box.props.font_desc = font.DEFAULT.get_pango_desc() + self._text_box.props.color = color.BUTTON_NORMAL.get_int() + self._round_box.append(self._text_box) + + hippo.CanvasBox.__init__(self, **kwargs) + self.props.yalign = hippo.ALIGNMENT_CENTER + + self.append(self._round_box) + + if not self.props.color: + self.props.color = color.BUTTON_NORMAL.get_int() + if not self.props.color: + self.props.background_color = \ + color.BUTTON_BACKGROUND_NORMAL.get_int() + + self._normal_color = self.props.color + self._normal_background_color = self.props.background_color + + self.connect('button-press-event', + self._button_button_press_event_cb) + + def do_set_property(self, pspec, value): + if pspec.name == 'active': + self._active = value + if self._active: + self.props.color = self._normal_color + self.props.background_color = self._normal_background_color + else: + self.props.color = color.BUTTON_INACTIVE.get_int() + self.props.background_color = \ + color.BUTTON_BACKGROUND_INACTIVE.get_int() + elif pspec.name == 'icon-name': + if value: + if self._icon: + self._icon.props.icon_name = value + else: + self._icon = CanvasIcon(icon_name=value, + scale=units.SMALL_ICON_SCALE, + fill_color=color.WHITE, + stroke_color=color.BLACK) + # Insert icon on the label's left + self._round_box.remove_all() + self._round_box.append(self._icon) + self._round_box.append(self._text_box) + else: + if self._icon: + self._round_box.remove(self._icon) + self._icon = None + elif pspec.name == 'text': + self._text_box.props.text = value + else: + hippo.CanvasBox.do_set_property(self, pspec, value) + + def do_get_property(self, pspec): + if pspec.name == 'active': + return self._active + elif pspec.name == 'icon-name': + if self._icon: + return self._icon.props.icon_name + else: + return None + elif pspec.name == 'text': + return self._text_box.props.text + else: + return hippo.CanvasBox.do_get_property(self, pspec) + + def _button_button_press_event_cb(self, widget, event): + if self._active: + self.emit_activated() + return True + + def prelight(self, enter): + if enter: + if self._active: + self.props.background_color = color.BLACK.get_int() + else: + if self._active: + self.props.background_color = self._normal_background_color diff --git a/sugar/graphics/iconbutton.py b/sugar/graphics/iconbutton.py index fc164519..cc70341f 100644 --- a/sugar/graphics/iconbutton.py +++ b/sugar/graphics/iconbutton.py @@ -20,10 +20,9 @@ import sys import gobject import hippo -from canvasicon import CanvasIcon +from sugar.graphics.canvasicon import CanvasIcon from sugar.graphics import units from sugar.graphics import color -from sugar import profile STANDARD_SIZE = 0 SMALL_SIZE = 1 diff --git a/sugar/graphics/roundbox.py b/sugar/graphics/roundbox.py index 1559ec2a..160bccb6 100644 --- a/sugar/graphics/roundbox.py +++ b/sugar/graphics/roundbox.py @@ -34,8 +34,7 @@ class RoundBox(hippo.CanvasBox, hippo.CanvasItem): self._radius = units.points_to_pixels(7) self.props.orientation = hippo.ORIENTATION_HORIZONTAL - self.props.border_top = self._BORDER_DEFAULT - self.props.border_bottom = self._BORDER_DEFAULT + self.props.border = self._BORDER_DEFAULT self.props.border_left = self._radius self.props.border_right = self._radius self.props.border_color = color.BLACK.get_int() @@ -61,6 +60,8 @@ class RoundBox(hippo.CanvasBox, hippo.CanvasItem): hippo.cairo_set_source_rgba32(cr, self.props.background_color) cr.fill_preserve(); - hippo.cairo_set_source_rgba32(cr, self.props.border_color) - cr.set_line_width(self._BORDER_DEFAULT) - cr.stroke() + # TODO: we should be more consistent here with the border properties. + if self.props.border_color: + hippo.cairo_set_source_rgba32(cr, self.props.border_color) + cr.set_line_width(self.props.border_top) + cr.stroke() diff --git a/tests/test-button.py b/tests/test-button.py new file mode 100755 index 00000000..e452811c --- /dev/null +++ b/tests/test-button.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python + +# Copyright (C) 2007, One Laptop Per Child +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +import sys +sys.path.insert(0, '/home/tomeu/sugar-jhbuild/source/sugar') + +import gtk +import hippo + +from sugar.graphics.toolbar import Toolbar +from sugar.graphics.iconbutton import IconButton +from sugar.graphics.button import Button +from sugar.graphics.entry import Entry + +def _button_activated_cb(button): + print "_button_activated_cb" + +import os +theme = gtk.icon_theme_get_default() +theme.prepend_search_path(os.path.join(os.path.dirname(__file__), 'data')) + +window = gtk.Window() +window.connect("destroy", lambda w: gtk.main_quit()) +window.show() + +canvas = hippo.Canvas() +window.add(canvas) +canvas.show() + +vbox = hippo.CanvasBox() +canvas.set_root(vbox) + +for i in [1, 2]: + toolbar = Toolbar() + toolbar.props.box_width = 400 + vbox.append(toolbar) + + icon_button = IconButton(icon_name='theme:stock-close') + toolbar.append(icon_button) + + button = Button(text='Click me!', icon_name='theme:stock-close') + button.connect('activated', _button_activated_cb) + toolbar.append(button) + + entry = Entry(text='mec') + toolbar.append(entry) + +gtk.main()