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
|
"""
ImageToggleButton.py
August 29, 2010
A simple gtk widget for a clickable image.
"""
import glib
import gnomeapplet
import gobject
import gtk
import logging
logger = logging.getLogger("ImageToggleButton")
class ImageToggleButton(gtk.EventBox):
__gsignals__ = {
"toggled": (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ())
}
def __init__(self):
gtk.EventBox.__init__(self)
self.set_selected(False)
self.connect("button-press-event", self.on_button_press)
self.connect("button-release-event", self.on_button_release)
self._toggled = False
self._current_orient = gnomeapplet.ORIENT_DOWN
self._press_x = -1
self._press_y = -1
self._box = gtk.HBox()
self.add(self._box)
self._image = gtk.Image()
self.set_orient(self._current_orient)
self._box.pack_start(self._image, False, False, 2)
self._label = gtk.Label("")
self._box.pack_start(self._label, False, False, 2)
def set_label(self, text):
"""Sets the button label."""
self._label.set_text(text)
def get_active(self):
"""Returns True if the button is currently in the pressed position."""
return self._toggled
def set_active(self, active):
"""Set the button to the pressed position."""
self._toggled = active
self.set_selected(active)
def get_pixbuf(self):
"""Gets the current pixbuf image."""
return self._image.get_pixbuf()
def set_from_pixbuf(self, pixbuf):
"""Sets the current pixbuf image."""
self._image.set_from_pixbuf(pixbuf)
def is_inside(self, x, y):
"""Returns True if the (x, y) position is inside the button."""
alloc = self.get_allocation()
return x >= 0 and y >= 0 and x <= alloc.width and y <= alloc.height
def set_orient(self, orient):
"""Sets the orient of the button and updates the positions of the label and image."""
if self._current_orient == orient:
return
old_box = self._box
old_box.remove(self._image)
old_box.remove(self._label)
if orient in (gnomeapplet.ORIENT_RIGHT, gnomeapplet.ORIENT_LEFT):
self._image.set_padding(0, 5)
self._box = gtk.VBox()
else:
self._image.set_padding(5, 0)
self._box = gtk.HBox()
self.remove(old_box)
self.add(self._box)
self._box.pack_start(self._image, False, False, 2)
self._box.pack_start(self._label, False, False, 2)
self.show_all()
old_box.destroy()
self._current_orient = orient
def set_selected(self, selected):
"""Sets the selected state of the button's window."""
if selected:
self.set_state(gtk.STATE_SELECTED)
self.set_visible_window(True)
else:
self.set_state(gtk.STATE_NORMAL)
self.set_visible_window(False)
def on_button_press(self, widget, event):
"""Button pressed event listener."""
if event.button == 1:
if self.is_inside(event.x, event.y):
self._press_x = event.x
self._press_y = event.y
return True
return False
def on_button_release(self, widget, event):
"""Button released event listener."""
if event.button == 1:
if self.is_inside(event.x, event.y) and self.is_inside(self._press_x, self._press_y):
self.set_active(not self._toggled)
glib.idle_add(self.emit, "toggled")
return True
self._press_x = -1
self._press_y = -1
return False
|