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
|
### Copyright (C) 2002-2006 Stephen Kennedy <stevek@gnome.org>
### This program is free software; you can redistribute it and/or modify
### it under the terms of the GNU General Public License as published by
### the Free Software Foundation; either version 2 of the License, or
### (at your option) any later version.
### This program 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 General Public License for more details.
### You should have received a copy of the GNU General Public License
### along with this program; if not, write to the Free Software
### Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""Utility classes for working with glade files.
"""
# modified by Damon Lynch May 2009 to update i18n
import gtk
import gtk.glade
import gnome
import gnome.ui
#import gettext
import config
from common import Configi18n
class Base(object):
"""Base class for all glade objects.
This class handles loading the xml glade file and connects
all methods name 'on_*' to the signals in the glade file.
The handle to the xml file is stored in 'self.xml'. The
toplevel widget is stored in 'self.widget'.
In addition it calls widget.set_data("pyobject", self) - this
allows us to get the python object given only the 'raw' gtk+
object, which is sadly sometimes necessary.
"""
def __init__(self, file, root, override={}):
"""Load the widgets from the node 'root' in file 'file'.
Automatically connects signal handlers named 'on_*'.
"""
global _
_ = Configi18n._
if Configi18n.locale_path:
gtk.glade.bindtextdomain(config.APP_NAME, Configi18n.locale_path)
gtk.glade.textdomain(config.APP_NAME)
self.xml = gtk.glade.XML(file, root, typedict=override )
handlers = {}
for h in filter(lambda x:x.startswith("on_"), dir(self.__class__)):
handlers[h] = getattr(self, h)
self.xml.signal_autoconnect( handlers )
self.widget = getattr(self, root)
self.widget.set_data("pyobject", self)
def __getattr__(self, key):
"""Allow glade widgets to be accessed as self.widgetname.
"""
widget = self.xml.get_widget(key)
if widget: # cache lookups
setattr(self, key, widget)
return widget
raise AttributeError(key)
def flushevents(self):
"""Handle all the events currently in the main queue and return.
"""
while gtk.events_pending():
gtk.main_iteration();
def _map_widgets_into_lists(self, widgetnames):
"""Put sequentially numbered widgets into lists.
e.g. If an object had widgets self.button0, self.button1, ...,
then after a call to object._map_widgets_into_lists(["button"])
object has an attribute self.button == [self.button0, self.button1, ...]."
"""
for item in widgetnames:
setattr(self,item, [])
lst = getattr(self,item)
i = 0
while 1:
key = "%s%i"%(item,i)
try:
val = getattr(self, key)
except AttributeError:
break
lst.append(val)
i += 1
class Component(Base):
"""A convenience base class for widgets which use glade.
"""
def __init__(self, file, root, override={}):
Base.__init__(self, file, root, override)
class GtkApp(Base):
"""A convenience base class for gtk+ apps created in glade.
"""
def __init__(self, file, root=None):
Base.__init__(self, file, root)
def main(self):
"""Enter the gtk main loop.
"""
gtk.main()
def quit(self, *args):
"""Signal the gtk main loop to quit.
"""
gtk.main_quit()
class GnomeApp(GtkApp):
"""A convenience base class for apps created in glade.
"""
def __init__(self, name, version, file, root):
"""Initialise program 'name' and version from 'file' containing root node 'root'.
"""
self.program = gnome.program_init(name, version)
GtkApp.__init__(self,file,root)
if 0:
self.client = gnome.ui.Client()
self.client.disconnect()
def connected(*args):
print "CONNECTED", args
def cb(name):
def cb2(*args):
print name, args, "\n"
return cb2
self.client.connect("connect", cb("CON"))
self.client.connect("die", cb("DIE"))
self.client.connect("disconnect", cb("DIS"))
self.client.connect("save-yourself", cb("SAVE"))
self.client.connect("shutdown-cancelled", cb("CAN"))
self.client.connect_to_session_manager()
def load_pixbuf(fname, size=0):
"""Load an image from a file as a pixbuf, with optional resizing.
"""
image = gtk.Image()
image.set_from_file(fname)
image = image.get_pixbuf()
if size:
aspect = float(image.get_height()) / image.get_width()
image = image.scale_simple(size, int(aspect*size), 2)
return image
def url_show(url):
return gnome.url_show(url)
def FileEntry(*args):
return gnome.ui.FileEntry(*args)
|