Fixing hildonization in case we hildonize before parenting
[gonvert] / src / hildonize.py
1 #!/usr/bin/env python
2
3 """
4 Open Issues
5         @bug Buttons are too small
6 """
7
8
9 import gobject
10 import gtk
11 import dbus
12
13
14 class _NullHildonModule(object):
15         pass
16
17
18 try:
19         import hildon as _hildon
20         hildon  = _hildon # Dumb but gets around pyflakiness
21 except (ImportError, OSError):
22         hildon = _NullHildonModule
23
24
25 IS_HILDON_SUPPORTED = hildon is not _NullHildonModule
26
27
28 class _NullHildonProgram(object):
29
30         def add_window(self, window):
31                 pass
32
33
34 def _hildon_get_app_class():
35         return hildon.Program
36
37
38 def _null_get_app_class():
39         return _NullHildonProgram
40
41
42 try:
43         hildon.Program
44         get_app_class = _hildon_get_app_class
45 except AttributeError:
46         get_app_class = _null_get_app_class
47
48
49 def _hildon_set_application_title(window, title):
50         pass
51
52
53 def _null_set_application_title(window, title):
54         window.set_title(title)
55
56
57 if IS_HILDON_SUPPORTED:
58         set_application_title = _hildon_set_application_title
59 else:
60         set_application_title = _null_set_application_title
61
62
63 def _fremantle_hildonize_window(app, window):
64         oldWindow = window
65         newWindow = hildon.StackableWindow()
66         oldWindow.get_child().reparent(newWindow)
67         app.add_window(newWindow)
68         return newWindow
69
70
71 def _hildon_hildonize_window(app, window):
72         oldWindow = window
73         newWindow = hildon.Window()
74         oldWindow.get_child().reparent(newWindow)
75         app.add_window(newWindow)
76         return newWindow
77
78
79 def _null_hildonize_window(app, window):
80         return window
81
82
83 try:
84         hildon.StackableWindow
85         hildonize_window = _fremantle_hildonize_window
86 except AttributeError:
87         try:
88                 hildon.Window
89                 hildonize_window = _hildon_hildonize_window
90         except AttributeError:
91                 hildonize_window = _null_hildonize_window
92
93
94 def _fremantle_hildonize_menu(window, gtkMenu, buttons):
95         appMenu = hildon.AppMenu()
96         for button in buttons:
97                 appMenu.append(button)
98         window.set_app_menu(appMenu)
99         gtkMenu.get_parent().remove(gtkMenu)
100         return appMenu
101
102
103 def _hildon_hildonize_menu(window, gtkMenu, ignoredButtons):
104         hildonMenu = gtk.Menu()
105         for child in gtkMenu.get_children():
106                 child.reparent(hildonMenu)
107         window.set_menu(hildonMenu)
108         gtkMenu.destroy()
109         return hildonMenu
110
111
112 def _null_hildonize_menu(window, gtkMenu, ignoredButtons):
113         return gtkMenu
114
115
116 try:
117         hildon.AppMenu
118         GTK_MENU_USED = False
119         IS_FREMANTLE_SUPPORTED = True
120         hildonize_menu = _fremantle_hildonize_menu
121 except AttributeError:
122         GTK_MENU_USED = True
123         IS_FREMANTLE_SUPPORTED = False
124         if IS_HILDON_SUPPORTED:
125                 hildonize_menu = _hildon_hildonize_menu
126         else:
127                 hildonize_menu = _null_hildonize_menu
128
129
130 def _hildon_set_button_auto_selectable(button):
131         button.set_theme_size(hildon.HILDON_SIZE_AUTO_HEIGHT)
132
133
134 def _null_set_button_auto_selectable(button):
135         pass
136
137
138 try:
139         hildon.HILDON_SIZE_AUTO_HEIGHT
140         gtk.Button.set_theme_size
141         set_button_auto_selectable = _hildon_set_button_auto_selectable
142 except AttributeError:
143         set_button_auto_selectable = _null_set_button_auto_selectable
144
145
146 def _hildon_set_button_finger_selectable(button):
147         button.set_theme_size(hildon.HILDON_SIZE_FINGER_HEIGHT)
148
149
150 def _null_set_button_finger_selectable(button):
151         pass
152
153
154 try:
155         hildon.HILDON_SIZE_FINGER_HEIGHT
156         gtk.Button.set_theme_size
157         set_button_finger_selectable = _hildon_set_button_finger_selectable
158 except AttributeError:
159         set_button_finger_selectable = _null_set_button_finger_selectable
160
161
162 def _hildon_set_button_thumb_selectable(button):
163         button.set_theme_size(hildon.HILDON_SIZE_THUMB_HEIGHT)
164
165
166 def _null_set_button_thumb_selectable(button):
167         pass
168
169
170 try:
171         hildon.HILDON_SIZE_THUMB_HEIGHT
172         gtk.Button.set_theme_size
173         set_button_thumb_selectable = _hildon_set_button_thumb_selectable
174 except AttributeError:
175         set_button_thumb_selectable = _null_set_button_thumb_selectable
176
177
178 def _hildon_set_cell_thumb_selectable(renderer):
179         renderer.set_property("scale", 1.5)
180
181
182 def _null_set_cell_thumb_selectable(renderer):
183         pass
184
185
186 if IS_HILDON_SUPPORTED:
187         set_cell_thumb_selectable = _hildon_set_cell_thumb_selectable
188 else:
189         set_cell_thumb_selectable = _null_set_cell_thumb_selectable
190
191
192 def _fremantle_show_information_banner(parent, message):
193         hildon.hildon_banner_show_information(parent, "", message)
194
195
196 def _hildon_show_information_banner(parent, message):
197         hildon.hildon_banner_show_information(parent, None, message)
198
199
200 def _null_show_information_banner(parent, message):
201         pass
202
203
204 if IS_FREMANTLE_SUPPORTED:
205         show_information_banner = _fremantle_show_information_banner
206 else:
207         try:
208                 hildon.hildon_banner_show_information
209                 show_information_banner = _hildon_show_information_banner
210         except AttributeError:
211                 show_information_banner = _null_show_information_banner
212
213
214 def _fremantle_show_busy_banner_start(parent, message):
215         hildon.hildon_gtk_window_set_progress_indicator(parent, True)
216         return parent
217
218
219 def _fremantle_show_busy_banner_end(banner):
220         hildon.hildon_gtk_window_set_progress_indicator(banner, False)
221
222
223 def _hildon_show_busy_banner_start(parent, message):
224         return hildon.hildon_banner_show_animation(parent, None, message)
225
226
227 def _hildon_show_busy_banner_end(banner):
228         banner.destroy()
229
230
231 def _null_show_busy_banner_start(parent, message):
232         return None
233
234
235 def _null_show_busy_banner_end(banner):
236         assert banner is None
237
238
239 try:
240         hildon.hildon_gtk_window_set_progress_indicator
241         show_busy_banner_start = _fremantle_show_busy_banner_start
242         show_busy_banner_end = _fremantle_show_busy_banner_end
243 except AttributeError:
244         try:
245                 hildon.hildon_banner_show_animation
246                 show_busy_banner_start = _hildon_show_busy_banner_start
247                 show_busy_banner_end = _hildon_show_busy_banner_end
248         except AttributeError:
249                 show_busy_banner_start = _null_show_busy_banner_start
250                 show_busy_banner_end = _null_show_busy_banner_end
251
252
253 def _hildon_hildonize_text_entry(textEntry):
254         textEntry.set_property('hildon-input-mode', 7)
255
256
257 def _null_hildonize_text_entry(textEntry):
258         pass
259
260
261 if IS_HILDON_SUPPORTED:
262         hildonize_text_entry = _hildon_hildonize_text_entry
263 else:
264         hildonize_text_entry = _null_hildonize_text_entry
265
266
267 def _hildon_mark_window_rotatable(window):
268         # gtk documentation is unclear whether this does a "=" or a "|="
269         window.set_flags(hildon.HILDON_PORTRAIT_MODE_SUPPORT)
270
271
272 def _null_mark_window_rotatable(window):
273         pass
274
275
276 try:
277         hildon.HILDON_PORTRAIT_MODE_SUPPORT
278         mark_window_rotatable = _hildon_mark_window_rotatable
279 except AttributeError:
280         mark_window_rotatable = _null_mark_window_rotatable
281
282
283 def _hildon_window_to_portrait(window):
284         # gtk documentation is unclear whether this does a "=" or a "|="
285         window.set_flags(hildon.HILDON_PORTRAIT_MODE_SUPPORT)
286
287
288 def _hildon_window_to_landscape(window):
289         # gtk documentation is unclear whether this does a "=" or a "&= ~"
290         window.unset_flags(hildon.HILDON_PORTRAIT_MODE_REQUEST)
291
292
293 def _null_window_to_portrait(window):
294         pass
295
296
297 def _null_window_to_landscape(window):
298         pass
299
300
301 try:
302         hildon.HILDON_PORTRAIT_MODE_SUPPORT
303         hildon.HILDON_PORTRAIT_MODE_REQUEST
304
305         window_to_portrait = _hildon_window_to_portrait
306         window_to_landscape = _hildon_window_to_landscape
307 except AttributeError:
308         window_to_portrait = _null_window_to_portrait
309         window_to_landscape = _null_window_to_landscape
310
311
312 def get_device_orientation():
313         bus = dbus.SystemBus()
314         try:
315                 rawMceRequest = bus.get_object("com.nokia.mce", "/com/nokia/mce/request")
316                 mceRequest = dbus.Interface(rawMceRequest, dbus_interface="com.nokia.mce.request")
317                 orientation, standState, faceState, xAxis, yAxis, zAxis = mceRequest.get_device_orientation()
318         except dbus.exception.DBusException:
319                 # catching for documentation purposes that when a system doesn't
320                 # support this, this is what to expect
321                 raise
322
323         if orientation == "":
324                 return gtk.ORIENTATION_HORIZONTAL
325         elif orientation == "":
326                 return gtk.ORIENTATION_VERTICAL
327         else:
328                 raise RuntimeError("Unknown orientation: %s" % orientation)
329
330
331 def _hildon_hildonize_password_entry(textEntry):
332         textEntry.set_property('hildon-input-mode', 7 | (1 << 29))
333
334
335 def _null_hildonize_password_entry(textEntry):
336         pass
337
338
339 if IS_HILDON_SUPPORTED:
340         hildonize_password_entry = _hildon_hildonize_password_entry
341 else:
342         hildonize_password_entry = _null_hildonize_password_entry
343
344
345 def _hildon_hildonize_combo_entry(comboEntry):
346         comboEntry.set_property('hildon-input-mode', 1 << 4)
347
348
349 def _null_hildonize_combo_entry(textEntry):
350         pass
351
352
353 if IS_HILDON_SUPPORTED:
354         hildonize_combo_entry = _hildon_hildonize_combo_entry
355 else:
356         hildonize_combo_entry = _null_hildonize_combo_entry
357
358
359 def _fremantle_hildonize_scrollwindow(scrolledWindow):
360         pannableWindow = hildon.PannableArea()
361
362         child = scrolledWindow.get_child()
363         scrolledWindow.remove(child)
364         pannableWindow.add(child)
365
366         parent = scrolledWindow.get_parent()
367         if parent is not None:
368                 parent.remove(scrolledWindow)
369                 parent.add(pannableWindow)
370
371         return pannableWindow
372
373
374 def _hildon_hildonize_scrollwindow(scrolledWindow):
375         hildon.hildon_helper_set_thumb_scrollbar(scrolledWindow, True)
376         return scrolledWindow
377
378
379 def _null_hildonize_scrollwindow(scrolledWindow):
380         return scrolledWindow
381
382
383 try:
384         hildon.PannableArea
385         hildonize_scrollwindow = _fremantle_hildonize_scrollwindow
386         hildonize_scrollwindow_with_viewport = _hildon_hildonize_scrollwindow
387 except AttributeError:
388         try:
389                 hildon.hildon_helper_set_thumb_scrollbar
390                 hildonize_scrollwindow = _hildon_hildonize_scrollwindow
391                 hildonize_scrollwindow_with_viewport = _hildon_hildonize_scrollwindow
392         except AttributeError:
393                 hildonize_scrollwindow = _null_hildonize_scrollwindow
394                 hildonize_scrollwindow_with_viewport = _null_hildonize_scrollwindow
395
396
397 def _hildon_request_number(parent, title, range, default):
398         spinner = hildon.NumberEditor(*range)
399         spinner.set_value(default)
400
401         dialog = gtk.Dialog(
402                 title,
403                 parent,
404                 gtk.DIALOG_MODAL|gtk.DIALOG_DESTROY_WITH_PARENT,
405                 (gtk.STOCK_OK, gtk.RESPONSE_OK, gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL),
406         )
407         dialog.set_default_response(gtk.RESPONSE_CANCEL)
408         dialog.get_child().add(spinner)
409
410         try:
411                 dialog.show_all()
412                 response = dialog.run()
413
414                 if response == gtk.RESPONSE_OK:
415                         return spinner.get_value()
416                 elif response == gtk.RESPONSE_CANCEL or response == gtk.RESPONSE_DELETE_EVENT:
417                         raise RuntimeError("User cancelled request")
418                 else:
419                         raise RuntimeError("Unrecognized response %r", response)
420         finally:
421                 dialog.hide()
422                 dialog.destroy()
423
424
425 def _null_request_number(parent, title, range, default):
426         adjustment = gtk.Adjustment(default, range[0], range[1], 1, 5, 0)
427         spinner = gtk.SpinButton(adjustment, 0, 0)
428         spinner.set_wrap(False)
429
430         dialog = gtk.Dialog(
431                 title,
432                 parent,
433                 gtk.DIALOG_MODAL|gtk.DIALOG_DESTROY_WITH_PARENT,
434                 (gtk.STOCK_OK, gtk.RESPONSE_OK, gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL),
435         )
436         dialog.set_default_response(gtk.RESPONSE_CANCEL)
437         dialog.get_child().add(spinner)
438
439         try:
440                 dialog.show_all()
441                 response = dialog.run()
442
443                 if response == gtk.RESPONSE_OK:
444                         return spinner.get_value_as_int()
445                 elif response == gtk.RESPONSE_CANCEL or response == gtk.RESPONSE_DELETE_EVENT:
446                         raise RuntimeError("User cancelled request")
447                 else:
448                         raise RuntimeError("Unrecognized response %r", response)
449         finally:
450                 dialog.hide()
451                 dialog.destroy()
452
453
454 try:
455         hildon.NumberEditor # TODO deprecated in fremantle
456         request_number = _hildon_request_number
457 except AttributeError:
458         request_number = _null_request_number
459
460
461 def _hildon_touch_selector(parent, title, items, defaultIndex):
462         model = gtk.ListStore(gobject.TYPE_STRING)
463         for item in items:
464                 model.append((item, ))
465
466         selector = hildon.TouchSelector()
467         selector.append_text_column(model, True)
468         selector.set_column_selection_mode(hildon.TOUCH_SELECTOR_SELECTION_MODE_SINGLE)
469         selector.set_active(0, defaultIndex)
470
471         dialog = hildon.PickerDialog(parent)
472         dialog.set_selector(selector)
473
474         try:
475                 dialog.show_all()
476                 response = dialog.run()
477
478                 if response == gtk.RESPONSE_OK:
479                         return selector.get_active(0)
480                 elif response == gtk.RESPONSE_CANCEL or response == gtk.RESPONSE_DELETE_EVENT:
481                         raise RuntimeError("User cancelled request")
482                 else:
483                         raise RuntimeError("Unrecognized response %r", response)
484         finally:
485                 dialog.hide()
486                 dialog.destroy()
487
488
489 def _on_null_touch_selector_activated(treeView, path, column, dialog, pathData):
490         dialog.response(gtk.RESPONSE_OK)
491         pathData[0] = path
492
493
494 def _null_touch_selector(parent, title, items, defaultIndex = -1):
495         parentSize = parent.get_size()
496
497         model = gtk.ListStore(gobject.TYPE_STRING)
498         for item in items:
499                 model.append((item, ))
500
501         cell = gtk.CellRendererText()
502         set_cell_thumb_selectable(cell)
503         column = gtk.TreeViewColumn(title)
504         column.pack_start(cell, expand=True)
505         column.add_attribute(cell, "text", 0)
506
507         treeView = gtk.TreeView()
508         treeView.set_model(model)
509         treeView.append_column(column)
510         selection = treeView.get_selection()
511         selection.set_mode(gtk.SELECTION_SINGLE)
512         if 0 < defaultIndex:
513                 selection.select_path((defaultIndex, ))
514
515         scrolledWin = gtk.ScrolledWindow()
516         scrolledWin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
517         scrolledWin.add(treeView)
518
519         dialog = gtk.Dialog(
520                 title,
521                 parent,
522                 gtk.DIALOG_MODAL|gtk.DIALOG_DESTROY_WITH_PARENT,
523                 (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL),
524         )
525         dialog.set_default_response(gtk.RESPONSE_CANCEL)
526         dialog.get_child().add(scrolledWin)
527         dialog.resize(parentSize[0], max(parentSize[1]-100, 100))
528
529         scrolledWin = hildonize_scrollwindow(scrolledWin)
530         pathData = [None]
531         treeView.connect("row-activated", _on_null_touch_selector_activated, dialog, pathData)
532
533         try:
534                 dialog.show_all()
535                 response = dialog.run()
536
537                 if response == gtk.RESPONSE_OK:
538                         if pathData[0] is None:
539                                 raise RuntimeError("No selection made")
540                         return pathData[0][0]
541                 elif response == gtk.RESPONSE_CANCEL or response == gtk.RESPONSE_DELETE_EVENT:
542                         raise RuntimeError("User cancelled request")
543                 else:
544                         raise RuntimeError("Unrecognized response %r", response)
545         finally:
546                 dialog.hide()
547                 dialog.destroy()
548
549
550 try:
551         hildon.PickerDialog
552         hildon.TouchSelector
553         touch_selector = _hildon_touch_selector
554 except AttributeError:
555         touch_selector = _null_touch_selector
556
557
558 def _hildon_touch_selector_entry(parent, title, items, defaultItem):
559         # Got a segfault when using append_text_column with TouchSelectorEntry, so using this way
560         try:
561                 selector = hildon.TouchSelectorEntry(text=True)
562         except TypeError:
563                 selector = hildon.hildon_touch_selector_entry_new_text()
564         defaultIndex = -1
565         for i, item in enumerate(items):
566                 selector.append_text(item)
567                 if item == defaultItem:
568                         defaultIndex = i
569
570         dialog = hildon.PickerDialog(parent)
571         dialog.set_selector(selector)
572
573         if 0 < defaultIndex:
574                 selector.set_active(0, defaultIndex)
575         else:
576                 selector.get_entry().set_text(defaultItem)
577
578         try:
579                 dialog.show_all()
580                 response = dialog.run()
581         finally:
582                 dialog.hide()
583
584         if response == gtk.RESPONSE_OK:
585                 return selector.get_entry().get_text()
586         elif response == gtk.RESPONSE_CANCEL or response == gtk.RESPONSE_DELETE_EVENT:
587                 raise RuntimeError("User cancelled request")
588         else:
589                 raise RuntimeError("Unrecognized response %r", response)
590
591
592 def _on_null_touch_selector_entry_entry_changed(entry, result, selection, defaultIndex):
593         custom = entry.get_text().strip()
594         if custom:
595                 result[0] = custom
596                 selection.unselect_all()
597         else:
598                 result[0] = None
599                 selection.select_path((defaultIndex, ))
600
601
602 def _on_null_touch_selector_entry_entry_activated(customEntry, dialog, result):
603         dialog.response(gtk.RESPONSE_OK)
604         result[0] = customEntry.get_text()
605
606
607 def _on_null_touch_selector_entry_tree_activated(treeView, path, column, dialog, result):
608         dialog.response(gtk.RESPONSE_OK)
609         model = treeView.get_model()
610         itr = model.get_iter(path)
611         if itr is not None:
612                 result[0] = model.get_value(itr, 0)
613
614
615 def _null_touch_selector_entry(parent, title, items, defaultItem):
616         parentSize = parent.get_size()
617
618         model = gtk.ListStore(gobject.TYPE_STRING)
619         defaultIndex = -1
620         for i, item in enumerate(items):
621                 model.append((item, ))
622                 if item == defaultItem:
623                         defaultIndex = i
624
625         cell = gtk.CellRendererText()
626         set_cell_thumb_selectable(cell)
627         column = gtk.TreeViewColumn(title)
628         column.pack_start(cell, expand=True)
629         column.add_attribute(cell, "text", 0)
630
631         treeView = gtk.TreeView()
632         treeView.set_model(model)
633         treeView.append_column(column)
634         selection = treeView.get_selection()
635         selection.set_mode(gtk.SELECTION_SINGLE)
636
637         scrolledWin = gtk.ScrolledWindow()
638         scrolledWin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
639         scrolledWin.add(treeView)
640
641         customEntry = gtk.Entry()
642
643         layout = gtk.VBox()
644         layout.pack_start(customEntry, expand=False)
645         layout.pack_start(scrolledWin)
646
647         dialog = gtk.Dialog(
648                 title,
649                 parent,
650                 gtk.DIALOG_MODAL|gtk.DIALOG_DESTROY_WITH_PARENT,
651                 (gtk.STOCK_OK, gtk.RESPONSE_OK, gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL),
652         )
653         dialog.set_default_response(gtk.RESPONSE_CANCEL)
654         dialog.get_child().add(layout)
655         dialog.resize(parentSize[0], max(parentSize[1]-100, 100))
656
657         scrolledWin = hildonize_scrollwindow(scrolledWin)
658
659         result = [None]
660         if 0 < defaultIndex:
661                 selection.select_path((defaultIndex, ))
662                 result[0] = defaultItem
663         else:
664                 customEntry.set_text(defaultItem)
665
666         customEntry.connect("activate", _on_null_touch_selector_entry_entry_activated, dialog, result)
667         customEntry.connect("changed", _on_null_touch_selector_entry_entry_changed, result, selection, defaultIndex)
668         treeView.connect("row-activated", _on_null_touch_selector_entry_tree_activated, dialog, result)
669
670         try:
671                 dialog.show_all()
672                 response = dialog.run()
673
674                 if response == gtk.RESPONSE_OK:
675                         _, itr = selection.get_selected()
676                         if itr is not None:
677                                 return model.get_value(itr, 0)
678                         else:
679                                 enteredText = customEntry.get_text().strip()
680                                 if enteredText:
681                                         return enteredText
682                                 elif result[0] is not None:
683                                         return result[0]
684                                 else:
685                                         raise RuntimeError("No selection made")
686                 elif response == gtk.RESPONSE_CANCEL or response == gtk.RESPONSE_DELETE_EVENT:
687                         raise RuntimeError("User cancelled request")
688                 else:
689                         raise RuntimeError("Unrecognized response %r", response)
690         finally:
691                 dialog.hide()
692                 dialog.destroy()
693
694
695 try:
696         hildon.PickerDialog
697         hildon.TouchSelectorEntry
698         touch_selector_entry = _hildon_touch_selector_entry
699 except AttributeError:
700         touch_selector_entry = _null_touch_selector_entry
701
702
703 if __name__ == "__main__":
704         app = get_app_class()()
705
706         label = gtk.Label("Hello World from a Label!")
707
708         win = gtk.Window()
709         win.add(label)
710         win = hildonize_window(app, win)
711         if False:
712                 print touch_selector(win, "Test", ["A", "B", "C", "D"], 2)
713         if True:
714                 print touch_selector_entry(win, "Test", ["A", "B", "C", "D"], "C")
715                 print touch_selector_entry(win, "Test", ["A", "B", "C", "D"], "Blah")
716         if False:
717                 import pprint
718                 name, value = "", ""
719                 goodLocals = [
720                         (name, value) for (name, value) in locals().iteritems()
721                         if not name.startswith("_")
722                 ]
723                 pprint.pprint(goodLocals)
724         if False:
725                 import time
726                 show_information_banner(win, "Hello World")
727                 time.sleep(5)
728         if False:
729                 import time
730                 banner = show_busy_banner_start(win, "Hello World")
731                 time.sleep(5)
732                 show_busy_banner_end(banner)