File: hello.py

package info (click to toggle)
gnome-python 2.28.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,288 kB
  • sloc: sh: 10,219; ansic: 7,997; xml: 2,464; python: 1,886; makefile: 392
file content (144 lines) | stat: -rw-r--r-- 3,959 bytes parent folder | download | duplicates (7)
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
#!/usr/bin/env python
#
# hello.py
#
# A hello world application using the Bonobo UI handler
#
# Original Authors:
#      Michael Meeks    <michael@ximian.com>
#      Murray Cumming   <murrayc@usa.net>
#      Havoc Pennington <hp@redhat.com>
#
# Converted to Python by:
#      Johan Dahlin     <jdahlin@telia.com>
#
    
import sys
import bonobo
import bonobo.ui
import gtk

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 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)
    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', gtk.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 ()