9 class _NullHildonModule(object):
14 import hildon as _hildon
15 hildon = _hildon # Dumb but gets around pyflakiness
16 except (ImportError, OSError):
17 hildon = _NullHildonModule
20 IS_HILDON_SUPPORTED = hildon is not _NullHildonModule
23 class _NullHildonProgram(object):
25 def add_window(self, window):
29 def _hildon_get_app_class():
33 def _null_get_app_class():
34 return _NullHildonProgram
37 if IS_HILDON_SUPPORTED:
38 get_app_class = _hildon_get_app_class
40 get_app_class = _null_get_app_class
43 def _hildon_set_application_title(window, title):
47 def _null_set_application_title(window, title):
48 window.set_title(title)
51 if IS_HILDON_SUPPORTED:
52 set_application_title = _hildon_set_application_title
54 set_application_title = _null_set_application_title
57 def _fremantle_hildonize_window(app, window):
59 newWindow = hildon.StackableWindow()
60 oldWindow.get_child().reparent(newWindow)
61 app.add_window(newWindow)
65 def _hildon_hildonize_window(app, window):
67 newWindow = hildon.Window()
68 oldWindow.get_child().reparent(newWindow)
69 app.add_window(newWindow)
73 def _null_hildonize_window(app, window):
78 hildon.StackableWindow
79 hildonize_window = _fremantle_hildonize_window
80 except AttributeError:
83 hildonize_window = _hildon_hildonize_window
84 except AttributeError:
85 hildonize_window = _null_hildonize_window
88 def _fremantle_hildonize_menu(window, gtkMenu, buttons):
89 appMenu = hildon.AppMenu()
90 for button in buttons:
91 appMenu.append(button)
92 window.set_app_menu(appMenu)
93 gtkMenu.get_parent().remove(gtkMenu)
97 def _hildon_hildonize_menu(window, gtkMenu, ignoredButtons):
98 hildonMenu = gtk.Menu()
99 for child in gtkMenu.get_children():
100 child.reparent(hildonMenu)
101 window.set_menu(hildonMenu)
106 def _null_hildonize_menu(window, gtkMenu, ignoredButtons):
112 GTK_MENU_USED = False
113 hildonize_menu = _fremantle_hildonize_menu
114 except AttributeError:
116 if IS_HILDON_SUPPORTED:
117 hildonize_menu = _hildon_hildonize_menu
119 hildonize_menu = _null_hildonize_menu
122 def _hildon_set_cell_thumb_selectable(renderer):
123 renderer.set_property("scale", 1.5)
126 def _null_set_cell_thumb_selectable(renderer):
130 if IS_HILDON_SUPPORTED:
131 set_cell_thumb_selectable = _hildon_set_cell_thumb_selectable
133 set_cell_thumb_selectable = _null_set_cell_thumb_selectable
136 def _hildon_hildonize_text_entry(textEntry):
137 textEntry.set_property('hildon-input-mode', 7)
140 def _null_hildonize_text_entry(textEntry):
144 if IS_HILDON_SUPPORTED:
145 hildonize_text_entry = _hildon_hildonize_text_entry
147 hildonize_text_entry = _null_hildonize_text_entry
150 def _hildon_mark_window_rotatable(window):
151 # gtk documentation is unclear whether this does a "=" or a "|="
152 window.set_flags(hildon.HILDON_PORTRAIT_MODE_SUPPORT)
155 def _null_mark_window_rotatable(window):
160 hildon.HILDON_PORTRAIT_MODE_SUPPORT
161 mark_window_rotatable = _hildon_mark_window_rotatable
162 except AttributeError:
163 mark_window_rotatable = _null_mark_window_rotatable
166 def _hildon_window_to_portrait(window):
167 # gtk documentation is unclear whether this does a "=" or a "|="
168 window.set_flags(hildon.HILDON_PORTRAIT_MODE_SUPPORT)
171 def _hildon_window_to_landscape(window):
172 # gtk documentation is unclear whether this does a "=" or a "&= ~"
173 window.unset_flags(hildon.HILDON_PORTRAIT_MODE_REQUEST)
176 def _null_window_to_portrait(window):
180 def _null_window_to_landscape(window):
185 hildon.HILDON_PORTRAIT_MODE_SUPPORT
186 hildon.HILDON_PORTRAIT_MODE_REQUEST
188 window_to_portrait = _hildon_window_to_portrait
189 window_to_landscape = _hildon_window_to_landscape
190 except AttributeError:
191 window_to_portrait = _null_window_to_portrait
192 window_to_landscape = _null_window_to_landscape
195 def get_device_orientation():
196 bus = dbus.SystemBus()
198 rawMceRequest = bus.get_object("com.nokia.mce", "/com/nokia/mce/request")
199 mceRequest = dbus.Interface(rawMceRequest, dbus_interface="com.nokia.mce.request")
200 orientation, standState, faceState, xAxis, yAxis, zAxis = mceRequest.get_device_orientation()
201 except dbus.exception.DBusException:
202 # catching for documentation purposes that when a system doesn't
203 # support this, this is what to expect
206 if orientation == "":
207 return gtk.ORIENTATION_HORIZONTAL
208 elif orientation == "":
209 return gtk.ORIENTATION_VERTICAL
211 raise RuntimeError("Unknown orientation: %s" % orientation)
214 def _hildon_hildonize_password_entry(textEntry):
215 textEntry.set_property('hildon-input-mode', 7 | (1 << 29))
218 def _null_hildonize_password_entry(textEntry):
222 if IS_HILDON_SUPPORTED:
223 hildonize_password_entry = _hildon_hildonize_password_entry
225 hildonize_password_entry = _null_hildonize_password_entry
228 def _hildon_hildonize_combo_entry(comboEntry):
229 comboEntry.set_property('hildon-input-mode', 1 << 4)
232 def _null_hildonize_combo_entry(textEntry):
236 if IS_HILDON_SUPPORTED:
237 hildonize_combo_entry = _hildon_hildonize_combo_entry
239 hildonize_combo_entry = _null_hildonize_combo_entry
242 def _fremantle_hildonize_scrollwindow(scrolledWindow):
243 pannableWindow = hildon.PannableArea()
245 child = scrolledWindow.get_child()
246 scrolledWindow.remove(child)
247 pannableWindow.add(child)
249 parent = scrolledWindow.get_parent()
250 parent.remove(scrolledWindow)
251 parent.add(pannableWindow)
253 return pannableWindow
256 def _hildon_hildonize_scrollwindow(scrolledWindow):
257 hildon.hildon_helper_set_thumb_scrollbar(scrolledWindow, True)
258 return scrolledWindow
261 def _null_hildonize_scrollwindow(scrolledWindow):
262 return scrolledWindow
267 hildonize_scrollwindow = _fremantle_hildonize_scrollwindow
268 hildonize_scrollwindow_with_viewport = _hildon_hildonize_scrollwindow
269 except AttributeError:
271 hildon.hildon_helper_hildonize_scrollwindow
272 hildonize_scrollwindow = _hildon_hildonize_scrollwindow
273 hildonize_scrollwindow_with_viewport = _hildon_hildonize_scrollwindow
274 except AttributeError:
275 hildonize_scrollwindow = _null_hildonize_scrollwindow
276 hildonize_scrollwindow_with_viewport = _null_hildonize_scrollwindow
279 def _hildon_request_number(parent, title, range, default):
280 spinner = hildon.NumberEditor(*range)
281 spinner.set_value(default)
286 gtk.DIALOG_MODAL|gtk.DIALOG_DESTROY_WITH_PARENT,
287 (gtk.STOCK_OK, gtk.RESPONSE_OK, gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL),
289 dialog.set_default_response(gtk.RESPONSE_CANCEL)
290 dialog.get_child().add(spinner)
294 response = dialog.run()
298 if response == gtk.RESPONSE_OK:
299 return spinner.get_value()
300 elif response == gtk.RESPONSE_CANCEL or response == gtk.RESPONSE_DELETE_EVENT:
301 raise RuntimeError("User cancelled request")
303 raise RuntimeError("Unrecognized response %r", response)
306 def _null_request_number(parent, title, range, default):
307 adjustment = gtk.Adjustment(default, range[0], range[1], 1, 5, 0)
308 spinner = gtk.SpinButton(adjustment, 0, 0)
309 spinner.set_wrap(False)
314 gtk.DIALOG_MODAL|gtk.DIALOG_DESTROY_WITH_PARENT,
315 (gtk.STOCK_OK, gtk.RESPONSE_OK, gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL),
317 dialog.set_default_response(gtk.RESPONSE_CANCEL)
318 dialog.get_child().add(spinner)
322 response = dialog.run()
326 if response == gtk.RESPONSE_OK:
327 return spinner.get_value_as_int()
328 elif response == gtk.RESPONSE_CANCEL or response == gtk.RESPONSE_DELETE_EVENT:
329 raise RuntimeError("User cancelled request")
331 raise RuntimeError("Unrecognized response %r", response)
335 hildon.NumberEditor # TODO deprecated in fremantle
336 request_number = _hildon_request_number
337 except AttributeError:
338 request_number = _null_request_number
341 def _hildon_touch_selector(parent, title, items, defaultIndex):
342 model = gtk.ListStore(gobject.TYPE_STRING)
344 model.append((item, ))
346 selector = hildon.TouchSelector()
347 selector.append_text_column(model, True)
348 selector.set_column_selection_mode(hildon.TOUCH_SELECTOR_SELECTION_MODE_SINGLE)
349 selector.set_active(0, defaultIndex)
351 dialog = hildon.PickerDialog(parent)
352 dialog.set_selector(selector)
356 response = dialog.run()
360 if response == gtk.RESPONSE_OK:
361 return selector.get_active(0)
362 elif response == gtk.RESPONSE_CANCEL or response == gtk.RESPONSE_DELETE_EVENT:
363 raise RuntimeError("User cancelled request")
365 raise RuntimeError("Unrecognized response %r", response)
368 def _null_touch_selector(parent, title, items, defaultIndex = -1):
369 model = gtk.ListStore(gobject.TYPE_STRING)
371 model.append((item, ))
373 cell = gtk.CellRendererText()
375 combo = gtk.ComboBox()
376 combo.set_model(model)
377 combo.pack_start(cell, True)
378 combo.add_attribute(cell, 'text', 0)
379 combo.set_active(defaultIndex)
384 gtk.DIALOG_MODAL|gtk.DIALOG_DESTROY_WITH_PARENT,
385 (gtk.STOCK_OK, gtk.RESPONSE_OK, gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL),
387 dialog.set_default_response(gtk.RESPONSE_CANCEL)
388 dialog.get_child().add(combo)
392 response = dialog.run()
396 if response == gtk.RESPONSE_OK:
397 return combo.get_active()
398 elif response == gtk.RESPONSE_CANCEL or response == gtk.RESPONSE_DELETE_EVENT:
399 raise RuntimeError("User cancelled request")
401 raise RuntimeError("Unrecognized response %r", response)
407 touch_selector = _hildon_touch_selector
408 except AttributeError:
409 touch_selector = _null_touch_selector
412 if __name__ == "__main__":
413 app = get_app_class()()
415 win = hildonize_window(app, win)
417 print touch_selector(win, "Test", ["1", "2", "3", "4"], 2)