2 # WimpWorks (for Python) (c) Andrew Flegg 2009.
3 # ~~~~~~~~~~~~~~~~~~~~~~ Released under the Artistic Licence.
7 import gtk, gobject, glib
12 # -- Work out environment...
32 gobject.threads_init()
37 '''A framework for creating easy-to-use graphical user interfaces using
38 GTK+, Python, DBus and more.
40 This is the base class. It should be constructed with a DBus name
43 Copyright (c) Andrew Flegg <andrew@bleb.org> 2009.
44 Released under the Artistic Licence.'''
47 # -----------------------------------------------------------------------
48 def __init__(self, application, version = '1.0.0', dbus_name = None):
49 '''Constructor. Initialises the gconf connection, DBus, OSSO and more.
51 @param application User-facing name of the application.
52 @param version Version string of the application.
53 @param dbus_name Name to register with DBus. If unspecified, no
54 DBus registration will be performed.'''
56 self.name = application
57 self.dbus_name = dbus_name
61 self.gconf = gnome.gconf.client_get_default()
64 self.app = hildon.Program()
65 self.main_window = hildon.Window()
66 gtk.set_application_name(application)
69 self.main_window = gtk.Window()
71 self.main_window.set_title(application)
72 self.main_window.connect("delete-event", gtk.main_quit)
74 if _have_osso and dbus_name:
75 self.osso_context = osso.Context(dbus_name, version, False)
78 self.app.add_window(self.main_window)
81 self._expose_hid = self.main_window.connect('expose-event', self._take_screenshot)
84 # -----------------------------------------------------------------------
85 def set_background(self, file, window = None):
86 '''Set the background of the given (or main) window to that contained in
89 @param file File name to set. If not an absolute path, typical application
90 directories will be checked.
91 @param window Window to set background of. If unset, will default to the
92 main application window.'''
94 # TODO Handle other forms of path
95 file = "/opt/%s/share/%s" % (re.sub('[^a-z0-9_]', '', self.name.lower()), file)
97 window = self.main_window
100 self._background, mask = gtk.gdk.pixbuf_new_from_file(file).render_pixmap_and_mask()
102 window.window.set_back_pixmap(self._background, False)
103 except glib.GError, e:
104 print "Couldn't find background:", e.message
107 # -----------------------------------------------------------------------
108 def add_menu_action(self, title, window = None):
109 '''Add a menu action to the given (or main) window. Once add_menu_action()
110 has been called with all the properties, 'self.menu.show_all()' should be
113 @param title The label of the action, and used to compute the callback
114 method. This should be the UN-i18n version: gettext is used
118 window = self.main_window
122 self.menu = hildon.AppMenu()
123 window.set_app_menu(self.menu)
125 raise Exception("Menu needs to be created, and no Hildon present")
127 button = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
128 button.set_label(_(title))
129 button.connect("clicked", self.callback, title)
130 self.menu.append(button)
133 # -----------------------------------------------------------------------
135 '''Once the application has been initialised, this will show the main window
136 and run the mainloop.'''
138 self.main_window.show_all()
142 # -----------------------------------------------------------------------
143 def _take_screenshot(self, event = None, data = None):
144 '''Used to provide a quick-loading screen.
146 @see http://maemo.org/api_refs/5.0/5.0-final/hildon/hildon-Additions-to-GTK+.html#hildon-gtk-window-take-screenshot'''
148 self.main_window.disconnect(self._expose_hid)
149 if not os.path.isfile("/home/user/.cache/launch/%s.pvr" % (self.dbus_name)):
150 gobject.timeout_add(80, hildon.hildon_gtk_window_take_screenshot, self.main_window, True)
153 # -----------------------------------------------------------------------
154 def callback(self, event, method):
155 '''Call a method on this object, using the given string to derive
156 the name. If no method is found, no action is taken.
158 @param event Event which triggered the callback.
159 @param method String which will be lowercased to form a method
160 called 'do_method'.'''
162 method = re.sub('[^a-z0-9_]', '', method.lower())
163 getattr(self, "do_%s" % (method))(event.window)
166 # -----------------------------------------------------------------------
167 def new_checkbox(self, label, box = None):
168 '''Create a new checkbox, adding it to the given container.
170 @param label Label for the checkbox.
171 @param box Optional container to add the created checkbox to.
172 @return The newly created checkbox.'''
174 checkbox = hildon.CheckButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
175 checkbox.set_label(label)
181 # -----------------------------------------------------------------------
182 def new_indent(self, box):
183 '''Create an indent which can be used to show items related to each other.
185 @param box Container to add the indent to.'''
189 outer.pack_start(indent, padding=48)
194 # -----------------------------------------------------------------------
195 def new_input(self, label, box = None, password = False):
196 '''Create a new input with the given label, optionally adding it to a
199 @param label Text describing the purpose of the input field.
200 @param box Optional container to add the input to.
201 @param password Boolean indicating if the input is used for passwords.
202 @return The newly created input.'''
204 input = hildon.Entry(gtk.HILDON_SIZE_FINGER_HEIGHT)
205 input.set_placeholder(label)
206 input.set_property('is-focus', False)
209 input.set_property('hildon-input-mode', gtk.HILDON_GTK_INPUT_MODE_FULL | gtk.HILDON_GTK_INPUT_MODE_INVISIBLE)
211 input.set_property('hildon-input-mode', gtk.HILDON_GTK_INPUT_MODE_FULL)
218 # -----------------------------------------------------------------------
219 def link_control(self, checkbox, ctrl, box = None):
220 '''Link a checkbox to a control, such that the editability of the
221 control is determined by the checkbox state.
223 @param checkbox Checkbox which will control the state.
224 @param ctrl Control to add.
225 @param box Optional container to add 'ctrl' to.
226 @return The added control.'''
231 self._sync_edit(checkbox, ctrl)
232 checkbox.connect('toggled', self._sync_edit, ctrl)
236 # -----------------------------------------------------------------------
237 def _sync_edit(self, checkbox, edit):
238 edit.set_property('sensitive', checkbox.get_active())
242 # -----------------------------------------------------------------------
243 class HildonMainScreenLayout():
244 '''Provides a mechanism for creating a traditional multi-button button
245 selection, as made popular by Maemo 5's Media Player, Clock, Application
248 This does *not* require Hildon, however.
251 # ---------------------------------------------------------------------
252 def __init__(self, container, offset = 0.5):
253 '''Create a new layout.
255 @param container Container to add layout to. If unspecified,
256 the application's main window will be used.
257 @param offset The vertical offset for the buttons. If unspecified,
258 they will be centred. Ranges from 0.0 (top) to 1.0 (bottom).'''
260 self._container = container
261 alignment = gtk.Alignment(xalign=0.5, yalign=0.8, xscale=0.8)
262 self._box = gtk.HButtonBox()
263 alignment.add(self._box)
264 container.main_window.add(alignment)
265 self._box.set_property('layout-style', gtk.BUTTONBOX_SPREAD)
268 # ---------------------------------------------------------------------
269 def add_button(self, title, subtitle = ''):
270 '''Add a button to the layout with the specified title. Upon clicking
271 the button, a method of the name 'do_title' will be invoked on the
274 @param title Value of the button, and used to derive the callback method. This
275 should be the UN-i18n version: gettext is used on the value.
276 @param subtitle An optional subtitle containing more information.'''
279 button = hildon.Button(gtk.HILDON_SIZE_THUMB_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL,
280 title = _(title), value = subtitle)
282 button = gtk.Button(label = _(title))
284 button.set_property('width-request', 250)
285 button.connect('clicked', self._container.callback, title)
286 self._box.add(button)