#!/usr/bin/env python # # hello.py # # A hello world application using the Bonobo UI handler # # Original Authors: # Michael Meeks # Murray Cumming # Havoc Pennington # # Converted to Python by: # Johan Dahlin # # Recent file support by: # Gustavo Carneiro # import sys import bonobo import bonobo.ui import gtk import egg.recent HELLO_UI_XML = "./Bonobo_Sample_Hello.xml" # Keep a list of all open application windows app_list = [] def strreverse (text): l = list (text) l.reverse () return ''.join (l) def show_nothing_dialog (widget): dialog = gtk.MessageDialog (widget, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_INFO, gtk.BUTTONS_OK, 'This does nothing; it is only a demonstration') dialog.run () dialog.destroy () def hello_on_menu_file_new (uic, verbname, win): hello = hello_new () hello.show_all () def hello_on_menu_file_open (uic, verbname, win): show_nothing_dialog (win) def hello_on_menu_file_save (uic, verbname, win): show_nothing_dialog (win) def hello_on_menu_file_saveas (uic, verbname, win): show_nothing_dialog (win) def hello_on_menu_file_exit (uic, verbname, win): sys.exit (0) def hello_on_menu_file_close (uic, verbname, win): app_list.remove (app) app.destroy () if not app_list: hello_on_menu_file_exit (uic, verbname, win) def hello_on_menu_edit_undo (uic, verbname, win): show_nothing_dialog (win) def hello_on_menu_edit_redo (uic, verbname, win): show_nothing_dialog (win) def hello_on_menu_help_about (uic, verbname, win): dialog = gtk.MessageDialog (win, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_INFO, gtk.BUTTONS_OK, 'BonoboUI-Hello') dialog.run () dialog.destroy () def hello_on_button_click (w, label): text = label.get_text () label.set_text (strreverse (text)) # These verb names are standard, see libonobobui/doc/std-ui.xml # to find a list of standard verb names. # The menu items are specified in Bonobo_Sample_Hello.xml and # given names which map to these verbs here. hello_verbs = [ ('FileNew', hello_on_menu_file_new), ('FileOpen', hello_on_menu_file_open), ('FileSave', hello_on_menu_file_save), ('FileSaveAs', hello_on_menu_file_saveas), ('FileClose', hello_on_menu_file_close), ('FileExit', hello_on_menu_file_exit), ('EditUndo', hello_on_menu_edit_undo), ('EditRedo', hello_on_menu_edit_redo), ('HelpAbout', hello_on_menu_help_about) ] def open_recent_cb(recent_view, item): dialog = gtk.MessageDialog (widget, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_INFO, gtk.BUTTONS_OK, 'I have a feeling you want to open the following recent' ' file:\n%s' % item.get_uri()) dialog.run () dialog.destroy () def hello_create_main_window (): window = bonobo.ui.Window ('Title', 'test') window.show_all () ui_container = window.get_ui_container () engine = window.get_ui_engine () engine.config_set_path ('/hello-app/UIConfig/kvps') ui_component = bonobo.ui.Component ('test') ui_component.set_container (ui_container.corba_objref ()) bonobo.ui.util_set_ui (ui_component, '', HELLO_UI_XML, 'bonobo-hello') ui_component.add_verb_list (hello_verbs, window) ## *** Here's the important stuff, add recent files to the UI ## ---------------------------------------------------------- ## Setup the EggRecentModel stuff model = egg.recent.RecentModel(egg.recent.RECENT_MODEL_SORT_MRU) #model.set_filter_groups(["Bonobo Test"]) # try these if you want #model.set_filter_mime_types(["text/plain"]) #model.set_filter_uri_schemes(["mailto", "http", "ftp"]) view = egg.recent.RecentViewBonobo(ui_component, "/menu/File/EggRecentDocuments/") view.show_icons(True) view.set_model(model) view.connect("activate", open_recent_cb) return window def delete_event_cb (window, event): return gtk.TRUE def hello_new (): win = hello_create_main_window () button = gtk.Button () button.set_border_width (10) label = gtk.Label ('Hello World') button.add (label) button.connect ('clicked', hello_on_button_click, label) win.set_size_request (250, 350) win.set_resizable (gtk.TRUE) win.set_property ('allow-shrink', False) frame = gtk.Frame () frame.set_shadow_type (gtk.SHADOW_IN) frame.add (button) win.set_contents (frame) win.connect ('delete_event', delete_event_cb) app_list.append (win) return win if __name__ == '__main__': app = hello_new () app.show_all () bonobo.main ()