summaryrefslogtreecommitdiff
path: root/examples/gtk/uimanager.py
blob: 2fd639944f021c53d120b8394fc00ba4c4bf4568 (plain)
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
#
# Small example of the new GtkUIManager
#
# Johan Dahlin <johan@gnome.org>, 2004
#

import pygtk
pygtk.require('2.0')

import gtk

if gtk.pygtk_version < (2,3,90):
    print "PyGtk 2.3.90 or later required for this example"
    raise SystemExit

window_actions = [
    ('FileMenu', None, '_File'),
    ('New',      gtk.STOCK_NEW, '_New', '<control>N', 'Create a new file', 'file_new_cb'),
    ('Open',     gtk.STOCK_OPEN, '_Open', '<control>O', 'Open a file', 'file_open_cb'),
    ('Close',    gtk.STOCK_CLOSE, '_Close', '<control>W', 'Close the current window', 'file_close_cb'),
    ('Quit',     gtk.STOCK_QUIT, '_Quit', '<control>Q', 'Quit application', 'file_quit_cb'),
    ('HelpMenu', None, '_Help'),
    ('About',    None, '_About', None, 'About application', 'help_about_cb'),
    ]

ui_string = """<ui>
  <menubar name='Menubar'>
    <menu action='FileMenu'>
      <menuitem action='New'/>
      <menuitem action='Open'/>
      <separator/>
      <menuitem action='Close'/>
      <menuitem action='Quit'/>
    </menu>
    <menu action='HelpMenu'>
      <menuitem action='About'/>
    </menu>
  </menubar>
  <toolbar name='Toolbar'>
    <toolitem action='New'/>
    <toolitem action='Open'/>
    <separator/>
    <toolitem action='Quit'/>
  </toolbar>
</ui>"""

def fix_actions(actions, instance):
    "Helper function to map methods to an instance"
    retval = []
    
    for i in range(len(actions)):
        curr = actions[i]
        if len(curr) > 5:
            curr = list(curr)
            curr[5] = getattr(instance, curr[5])
            curr = tuple(curr)
            
        retval.append(curr)
    return retval

class Window(gtk.Window):
    def __init__(self):
        gtk.Window.__init__(self)
        self.set_position(gtk.WIN_POS_CENTER)
        self.set_title('GtkUIManager test app')
        self.connect('delete-event', self.delete_event_cb)
        self.set_size_request(400, 200)
        vbox = gtk.VBox()
        self.add(vbox)

        self.create_ui()
        vbox.pack_start(self.ui.get_widget('/Menubar'), expand=False)
        vbox.pack_start(self.ui.get_widget('/Toolbar'), expand=False)

        sw = gtk.ScrolledWindow()
        sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        vbox.pack_start(sw)

        textview = gtk.TextView()
        self.buffer = textview.get_buffer()
        sw.add(textview)
        
        status = gtk.Statusbar()
        vbox.pack_end(status, expand=False)
        
    def create_ui(self):
        ag = gtk.ActionGroup('WindowActions')

        actions = fix_actions(window_actions, self)
        ag.add_actions(actions)
        self.ui = gtk.UIManager()
        self.ui.insert_action_group(ag, 0)
        self.ui.add_ui_from_string(ui_string)
        self.add_accel_group(self.ui.get_accel_group())

    def file_new_cb(self, action):
        w = Window()
        w.show_all()
        gtk.main()
        
    def file_open_cb(self, action):
        dialog = gtk.FileChooserDialog("Open..", self,
                                       gtk.FILE_CHOOSER_ACTION_OPEN,
                                       (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                                        gtk.STOCK_OPEN, gtk.RESPONSE_OK))
        dialog.set_default_response(gtk.RESPONSE_OK)

        filter = gtk.FileFilter()
        filter.set_name("All files")
        filter.add_pattern("*")
        dialog.add_filter(filter)

        dialog.hide()

        if dialog.run() == gtk.RESPONSE_OK:
            filename = dialog.get_filename()
            self.buffer.set_text(file(filename).read())

        dialog.destroy()
        
    def file_close_cb(self, action):
        self.hide()
        gtk.main_quit()

    def file_quit_cb(self, action):
        raise SystemExit

    def help_about_cb(self, action):
        dialog = gtk.MessageDialog(self,
                                   gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
                                   gtk.MESSAGE_INFO, gtk.BUTTONS_OK,
                                   "Small example of the new GtkUIManger")
        dialog.run()
        dialog.destroy()

    def delete_event_cb(self, window, event):
        gtk.main_quit()

if __name__ == '__main__':
    w = Window()
    w.show_all()
    gtk.main()