Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugar/graphics/button.py
blob: 7b487ae1b28d97e395ced384e72f0c446acc17b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# 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 = color.BLACK.get_int()
        self._round_box.props.background_color = color.BLACK.get_int()
        self._round_box.props.padding_top = units.points_to_pixels(1)
        self._round_box.props.padding_bottom = units.points_to_pixels(1)
        self._round_box.props.spacing = units.points_to_pixels(3)
                
        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.BLACK,
                                            stroke_color=color.WHITE)
                    # 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