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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
|
# -*- Mode: Python; py-indent-offset: 4 -*-
# vim: tabstop=4 shiftwidth=4 expandtab
#
# Copyright (C) 2009 Johan Dahlin <johan@gnome.org>
# 2010 Simon van der Linden <svdlinden@src.gnome.org>
#
# 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.1 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
# USA
import sys
import gobject
from gi.repository import Gdk
from gi.repository import GObject
from ..types import override
from ..importer import modules
if sys.version_info >= (3, 0):
_basestring = str
_callable = lambda c: hasattr(c, '__call__')
else:
_basestring = basestring
_callable = callable
Gtk = modules['Gtk'].introspection_module
__all__ = []
class ActionGroup(Gtk.ActionGroup):
def add_actions(self, entries, user_data=None):
"""
The add_actions() method is a convenience method that creates a number
of gtk.Action objects based on the information in the list of action
entry tuples contained in entries and adds them to the action group.
The entry tuples can vary in size from one to six items with the
following information:
* The name of the action. Must be specified.
* The stock id for the action. Optional with a default value of None
if a label is specified.
* The label for the action. This field should typically be marked
for translation, see the set_translation_domain() method. Optional
with a default value of None if a stock id is specified.
* The accelerator for the action, in the format understood by the
gtk.accelerator_parse() function. Optional with a default value of
None.
* The tooltip for the action. This field should typically be marked
for translation, see the set_translation_domain() method. Optional
with a default value of None.
* The callback function invoked when the action is activated.
Optional with a default value of None.
The "activate" signals of the actions are connected to the callbacks and
their accel paths are set to <Actions>/group-name/action-name.
"""
try:
iter(entries)
except (TypeError):
raise TypeError('entries must be iterable')
def _process_action(name, stock_id=None, label=None, accelerator=None, tooltip=None, callback=None):
action = Gtk.Action(name=name, label=label, tooltip=tooltip, stock_id=stock_id)
if callback is not None:
action.connect('activate', callback, user_data)
self.add_action_with_accel(action, accelerator)
for e in entries:
# using inner function above since entries can leave out optional arguments
_process_action(*e)
def add_toggle_actions(self, entries, user_data=None):
"""
The add_toggle_actions() method is a convenience method that creates a
number of gtk.ToggleAction objects based on the information in the list
of action entry tuples contained in entries and adds them to the action
group. The toggle action entry tuples can vary in size from one to seven
items with the following information:
* The name of the action. Must be specified.
* The stock id for the action. Optional with a default value of None
if a label is specified.
* The label for the action. This field should typically be marked
for translation, see the set_translation_domain() method. Optional
with a default value of None if a stock id is specified.
* The accelerator for the action, in the format understood by the
gtk.accelerator_parse() function. Optional with a default value of
None.
* The tooltip for the action. This field should typically be marked
for translation, see the set_translation_domain() method. Optional
with a default value of None.
* The callback function invoked when the action is activated.
Optional with a default value of None.
* A flag indicating whether the toggle action is active. Optional
with a default value of False.
The "activate" signals of the actions are connected to the callbacks and
their accel paths are set to <Actions>/group-name/action-name.
"""
try:
iter(entries)
except (TypeError):
raise TypeError('entries must be iterable')
def _process_action(name, stock_id=None, label=None, accelerator=None, tooltip=None, callback=None, is_active=False):
action = Gtk.ToggleAction(name=name, label=label, tooltip=tooltip, stock_id=stock_id)
action.set_active(is_active)
if callback is not None:
action.connect('activate', callback, user_data)
self.add_action_with_accel(action, accelerator)
for e in entries:
# using inner function above since entries can leave out optional arguments
_process_action(*e)
def add_radio_actions(self, entries, value=None, on_change=None, user_data=None):
"""
The add_radio_actions() method is a convenience method that creates a
number of gtk.RadioAction objects based on the information in the list
of action entry tuples contained in entries and adds them to the action
group. The entry tuples can vary in size from one to six items with the
following information:
* The name of the action. Must be specified.
* The stock id for the action. Optional with a default value of None
if a label is specified.
* The label for the action. This field should typically be marked
for translation, see the set_translation_domain() method. Optional
with a default value of None if a stock id is specified.
* The accelerator for the action, in the format understood by the
gtk.accelerator_parse() function. Optional with a default value of
None.
* The tooltip for the action. This field should typically be marked
for translation, see the set_translation_domain() method. Optional
with a default value of None.
* The value to set on the radio action. Optional with a default
value of 0. Should be specified in applications.
The value parameter specifies the radio action that should be set
active. The "changed" signal of the first radio action is connected to
the on_change callback (if specified and not None) and the accel paths
of the actions are set to <Actions>/group-name/action-name.
"""
try:
iter(entries)
except (TypeError):
raise TypeError('entries must be iterable')
first_action = None
def _process_action(group_source, name, stock_id=None, label=None, accelerator=None, tooltip=None, entry_value=0):
action = Gtk.RadioAction(name=name, label=label, tooltip=tooltip, stock_id=stock_id, value=entry_value)
# FIXME: join_group is a patch to Gtk+ 3.0
# otherwise we can't effectively add radio actions to a
# group. Should we depend on 3.0 and error out here
# or should we offer the functionality via a compat
# C module?
if hasattr(action, 'join_group'):
action.join_group(group_source)
if value == entry_value:
action.set_active(True)
self.add_action_with_accel(action, accelerator)
return action
for e in entries:
# using inner function above since entries can leave out optional arguments
action = _process_action(first_action, *e)
if first_action is None:
first_action = action
if first_action is not None and on_change is not None:
first_action.connect('changed', on_change, user_data)
ActionGroup = override(ActionGroup)
__all__.append('ActionGroup')
class UIManager(Gtk.UIManager):
def add_ui_from_string(self, buffer):
if not isinstance(buffer, _basestring):
raise TypeError('buffer must be a string')
length = len(buffer)
return Gtk.UIManager.add_ui_from_string(self, buffer, length)
UIManager = override(UIManager)
__all__.append('UIManager')
class Builder(Gtk.Builder):
def connect_signals(self, obj_or_map):
def _full_callback(builder, gobj, signal_name, handler_name, connect_obj, flags, obj_or_map):
handler = None
if isinstance(obj_or_map, dict):
handler = obj_or_map.get(handler_name, None)
else:
handler = getattr(obj_or_map, handler_name, None)
if handler is None:
raise AttributeError('Handler %s not found' % handler_name)
if not _callable(handler):
raise TypeError('Handler %s is not a method or function' % handler_name)
after = flags or GObject.ConnectFlags.AFTER
if connect_obj is not None:
if after:
gobj.connect_object_after(signal_name, handler, connect_obj)
else:
gobj.connect_object(signal_name, handler, connect_obj)
else:
if after:
gobj.connect_after(signal_name, handler)
else:
gobj.connect(signal_name, handler)
self.connect_signals_full(_full_callback,
obj_or_map);
def add_from_string(self, buffer):
if not isinstance(buffer, _basestring):
raise TypeError('buffer must be a string')
length = len(buffer)
return Gtk.Builder.add_from_string(self, buffer, length)
def add_objects_from_string(self, buffer, object_ids):
if not isinstance(buffer, _basestring):
raise TypeError('buffer must be a string')
length = len(buffer)
return Gtk.Builder.add_objects_from_string(self, buffer, length, object_ids)
Builder = override(Builder)
__all__.append('Builder')
class Dialog(Gtk.Dialog):
def __init__(self, title=None, parent=None, flags=0, buttons=None):
Gtk.Dialog.__init__(self)
if title:
self.set_title(title)
if parent:
self.set_transient_for(parent)
if flags & Gtk.DialogFlags.MODAL:
self.set_modal(True)
if flags & Gtk.DialogFlags.DESTROY_WITH_PARENT:
self.set_destroy_with_parent(True)
# NO_SEPARATOR has been removed from Gtk 3
try:
if flags & Gtk.DialogFlags.NO_SEPARATOR:
self.set_has_separator(False)
except AttributeError:
pass
if buttons:
self.add_buttons(*buttons)
def add_buttons(self, *args):
"""
The add_buttons() method adds several buttons to the Gtk.Dialog using
the button data passed as arguments to the method. This method is the
same as calling the Gtk.Dialog.add_button() repeatedly. The button data
pairs - button text (or stock ID) and a response ID integer are passed
individually. For example:
>>> dialog.add_buttons(Gtk.STOCK_OPEN, 42, "Close", Gtk.ResponseType.CLOSE)
will add "Open" and "Close" buttons to dialog.
"""
def buttons(b):
while b:
t, r = b[0:2]
b = b[2:]
yield t, r
try:
for text, response in buttons(args):
self.add_button(text, response)
except (IndexError):
raise TypeError('Must pass an even number of arguments')
Dialog = override(Dialog)
__all__.append('Dialog')
class TextBuffer(Gtk.TextBuffer):
def _get_or_create_tag_table(self):
table = self.get_tag_table()
if table is None:
table = Gtk.TextTagTable()
self.set_tag_table(table)
return table
def create_tag(self, tag_name=None, **properties):
"""
@tag_name: name of the new tag, or None
@properties: keyword list of properties and their values
Creates a tag and adds it to the tag table of the TextBuffer.
Equivalent to creating a Gtk.TextTag and then adding the
tag to the buffer's tag table. The returned tag is owned by
the buffer's tag table.
If @tag_name is None, the tag is anonymous.
If @tag_name is not None, a tag called @tag_name must not already
exist in the tag table for this buffer.
Properties are passed as a keyword list of names and values (e.g.
foreground = 'DodgerBlue', weight = Pango.Weight.BOLD)
Return value: a new tag
"""
tag = Gtk.TextTag(name=tag_name, **properties)
self._get_or_create_tag_table().add(tag)
return tag
def insert(self, iter, text):
if not isinstance(text , _basestring):
raise TypeError('text must be a string, not %s' % type(text))
length = len(text)
Gtk.TextBuffer.insert(self, iter, text, length)
def insert_at_cursor(self, text):
if not isinstance(text , _basestring):
raise TypeError('text must be a string, not %s' % type(text))
length = len(text)
Gtk.TextBuffer.insert_at_cursor(self, text, length)
TextBuffer = override(TextBuffer)
__all__.append('TextBuffer')
class TreeModel(Gtk.TreeModel):
def __len__(self):
return self.iter_n_children(None)
TreeModel = override(TreeModel)
__all__.append('TreeModel')
class ListStore(Gtk.ListStore, TreeModel):
def __init__(self, *column_types):
Gtk.ListStore.__init__(self)
self.set_column_types(column_types)
def append(self, row):
treeiter = Gtk.ListStore.append(self)
n_columns = self.get_n_columns();
if len(row) != n_columns:
raise ValueError('row sequence has the incorrect number of elements')
for i in range(n_columns):
if row[i] is not None:
self.set_value(treeiter, i, row[i])
return treeiter
ListStore = override(ListStore)
__all__.append('ListStore')
class TreeStore(Gtk.TreeStore, TreeModel):
def __init__(self, *column_types):
Gtk.TreeStore.__init__(self)
self.set_column_types(column_types)
def append(self, parent, row):
treeiter = Gtk.TreeStore.append(self, parent)
n_columns = self.get_n_columns();
if len(row) != n_columns:
raise ValueError('row sequence has the incorrect number of elements')
for i in range(n_columns):
if row[i] is not None:
self.set_value(treeiter, i, row[i])
return treeiter
TreeStore = override(TreeStore)
__all__.append('TreeStore')
class TreeViewColumn(Gtk.TreeViewColumn):
def __init__(self, title='',
cell_renderer=None,
**attributes):
Gtk.TreeViewColumn.__init__(self, title=title)
if cell_renderer:
self.pack_start(cell_renderer, True)
for (name, value) in attributes.items():
self.add_attribute(cell_renderer, name, value)
TreeViewColumn = override(TreeViewColumn)
__all__.append('TreeViewColumn')
class Button(Gtk.Button):
def __init__(self, label=None, stock=None, use_underline=False):
if stock:
label = stock
use_stock = True
use_underline = True
else:
use_stock = False
Gtk.Button.__init__(self, label=label, use_stock=use_stock,
use_underline=use_underline)
Button = override(Button)
__all__.append('Button')
import sys
initialized, argv = Gtk.init_check(sys.argv)
sys.argv = list(argv)
if not initialized:
raise RuntimeError("Gtk couldn't be initialized")
|