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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
# python import
import logging
# ...
from functools import partial
from gettext import gettext as _
# gtk import
import gtk, glib
# sugar import
from sugar.activity import activity
# sugar import
from sugar.graphics.toolbutton import ToolButton
# server import
from server.flask import logger
def _cb_next(widget, toolbar, next_):
# update current screen
toolbar.activity.change_screen(next_)
BUTTONS = {
'post' : ['document-generic', _cb_next],
'ajax' : ['document-generic', _cb_next],
'separator' : [None, None],
'gallery' : ['document-generic', _cb_next]
}
TOOLBARS = {
'menu' : [
['post', 'ajax', 'gallery'],
[]
],
}
TITLES = {
'menu' : {
'toolbox': _('Debug'),
'buttons': {
'post' : _('Post'),
'ajax' : _('Ajax'),
'gallery': _('Gallery')
}
}
}
class Toolbar(gtk.Toolbar):
def __init__(self, activity, name='player'):
# init parent
gtk.Toolbar.__init__(self)
# keep the name
self.set_name(name)
# keep activity
self.activity = activity
# adjustment
self._adjustment = None
# init widget dict
self._button_dict = dict()
# init buttons
self._init_buttons()
# add tab in toolbox
_toolbox_name = TITLES[self.name]['toolbox']
if _toolbox_name is None:
pass
else:
_toolbox = self.activity.get_toolbox()
_toolbox.add_toolbar(_toolbox_name, self)
# connect focus event
self.connect('focus', self._on_focus)
# show
self.show()
def _switch(self, name):
# update toolbar
self._clear_buttons()
# switch name
self.set_name(name)
# reset
self._init_buttons()
def _clear_buttons(self):
# remove all
for _k in self._button_dict.keys():
self._remove_button(_k)
# remove separators
for _c in self.get_children():
# remove it
self.remove(_c)
# and destroy it
_c.destroy()
def _init_buttons(self):
# add buttons
_b_left, _b_right = TOOLBARS[self.name]
# place left buttons
for _b in _b_left:
# add button
self._add_button(_b)
# add expanded separator
_separator = gtk.SeparatorToolItem()
_separator.set_draw(False)
_separator.set_expand(True)
_separator.show()
self.add(_separator)
# place right buttons
for _b in _b_right:
# add button
self._add_button(_b)
def _on_focus(self, widget, direction):
# update current screen
self.activity.change_screen(self.name)
def _has_button(self, button_id):
return button_id in self._button_dict
def _add_button(self, button_id, index=None):
# get button icon and cb
_icon, _cb = BUTTONS[button_id]
# manage separator
if button_id == 'separator':
_buton = gtk.SeparatorToolItem()
_buton.set_draw(True)
# standard button
elif button_id in BUTTONS:
# get tooltip
_tooltip = TITLES[self.name]['buttons'][button_id]
# set icon
_buton = ToolButton(_icon)
# set tooltip
_buton.set_tooltip(_tooltip)
# do connect
if _cb is None:
pass
else:
_buton.connect('clicked', _cb, self, button_id)
# ??
else:
# ERROR
logger.error('[toolbar] _add_button - ??: %s' % button_id)
return
# update the button dict
self._button_dict[button_id] = _buton
# add to the toolbar
if index is None:
self.add(_buton)
else:
self.insert(_buton, index)
# show it
_buton.show()
def _remove_button(self, button_id):
# little check
if button_id in self._button_dict:
# get button
_buton = self._button_dict.pop(button_id)
# do remove - do it safe
glib.idle_add(partial(self.remove, _buton))
# and destroy - safe
glib.idle_add(_buton.destroy)
# ??
else:
# ERROR
logger.error('[toolbar] _remove_button - unknown: %s' % button_id)
def _replace_button(self, button_id_out, button_id_in):
# little check
if button_id_out in self._button_dict:
# ...
_index = self.get_item_index(self._button_dict[button_id_out])
# do remove
self._remove_button(button_id_out)
# do it safe
glib.idle_add(partial(self._add_button, button_id_in, _index))
# ??
else:
pass
|