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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
# !/usr/bin/python
# -*- coding: utf-8 -*-
# Glitter Toolkit
__authors__ = ["Jan Jokela <janjokela@gmail.com>"]
__licenses__ = ["LICENSE.LGPL"]
__description__ = "Button widget"
import gobject
from widget import *
from container import Container
from box import HBox
from label import Label
from image import Image
class Button(Container):
"""
A button widget features icon and label, signals for press and release.
"""
__gsignals__ = {
'clicked': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_BOOLEAN,
(gobject.TYPE_PYOBJECT,))
}
def __init__(self, icon=None, text=None):
""" Initialize button widget """
super(Button, self).__init__()
self._icon = icon
self._text = text
self.set_reactive(True)
self.connect('motion-event', self.on_motion_event)
self.connect('leave-event', self.on_leave_event)
self.connect('button-press-event', self.on_press_event)
self.connect('button-release-event', self.on_release_event)
self._init_elements()
self._update_style(self.style)
def _init_elements(self):
""" Initializes graphical elements """
self._background_image = Image()
self.add(self._background_image)
self._hbox = HBox()
self._hbox.natural_width = 0.0
self._hbox.h_offset = 0.2
self._hbox.v_offset = 0.05
self.add(self._hbox)
self._icon_image = None
self.icon = self._icon
self._label = None
if self._text:
self._label = Label(self._text)
self._hbox.pack(self._label)
self._update_layout()
def _update_style(self, props=None):
""" Updates style """
super(Button, self)._update_style(props)
for key, value in props:
if key == 'background-image':
self.background_image = value
self._background_image.hide()
self._background_image.set_source(self.background_image)
self._background_image._update_layout()
self._background_image.show()
def _update_layout(self):
""" Updates layout """
super(Button, self)._update_layout()
self._hbox._update_layout()
self.minimum_width = self._hbox.minimum_width
self.minimum_width += 2 * self._hbox.h_offset * self.minimum_width
super(Button, self)._update_layout()
self._background_image._update_layout()
self._hbox._update_layout()
def get_icon(self):
""" Retrieve icon """
return self._icon
def set_icon(self, icon):
""" Sets icon
icon -- (str) icon uri
"""
self._icon = icon
if icon == None:
if self._icon_image:
self._hbox.unpack(self._icon_image)
self._icon_image = None
else:
if not self._icon_image:
self._icon_image = Image(self._icon)
self._icon_image.size_ratio = 1.0
self._hbox.pack(self._icon_image)
else:
self._icon_image.source = self._icon
icon = property(get_icon, set_icon)
def get_text(self):
""" Retrieve text """
return self._text
def set_text(self, text):
""" Sets text
text -- (str) button label text
"""
self._text = text
if text == None:
if self._label:
self._hbox.unpack(self._label)
self._label = None
else:
if not self._label:
self._label = Label(self._text)
self._hbox.pack(self._label)
else:
self._label.text = self._text
text = property(get_text, set_text)
def on_motion_event(self, widget, event):
""" Cursor motion over widget """
if not self.state == STATE_SELECTED:
self.state = STATE_HIGHLIGHT
def on_leave_event(self, widget, event):
""" Cursor leave from widget """
self.state = STATE_NORMAL
def on_press_event(self, widget, event):
self.state = STATE_SELECTED
def on_release_event(self, widget, event):
self.state = STATE_HIGHLIGHT
self.emit("clicked", self)
|