1 #!/usr/bin/env python
2
3 # example itemfactory.py
4
5 import gtk
6
7 class ItemFactoryExample:
8 # Obligatory basic callback
9 def print_hello(self, w, data):
10 print "Hello, World!"
11
12 # This is the GtkItemFactoryEntry structure used to generate new menus.
13 # Item 1: The menu path. The letter after the underscore indicates an
14 # accelerator key once the menu is open.
15 # Item 2: The accelerator key for the entry
16 # Item 3: The callback.
17 # Item 4: The callback action. This changes the parameters with
18 # which the callback is called. The default is 0.
19 # Item 5: The item type, used to define what kind of an item it is.
20 # Here are the possible values:
21
22 # NULL -> "<Item>"
23 # "" -> "<Item>"
24 # "<Title>" -> create a title item
25 # "<Item>" -> create a simple item
26 # "<CheckItem>" -> create a check item
27 # "<ToggleItem>" -> create a toggle item
28 # "<RadioItem>" -> create a radio item
29 # <path> -> path of a radio item to link against
30 # "<Separator>" -> create a separator
31 # "<Branch>" -> create an item to hold sub items (optional)
32 # "<LastBranch>" -> create a right justified branch
33
34 def get_main_menu(self, window):
35 nmenu_items = len(self.menu_items)
36 accel_group = gtk.GtkAccelGroup()
37
38 # This function initializes the item factory.
39 # Param 1: The type of menu - can be GtkMenuBar, GtkMenu,
40 # or GtkOptionMenu.
41 # Param 2: The path of the menu.
42 # Param 3: A pointer to a gtk_accel_group. The item factory sets up
43 # the accelerator table while generating menus.
44 item_factory = gtk.GtkItemFactory(gtk.GtkMenuBar.get_type(), "<main>",
45 accel_group)
46
47 # This method generates the menu items. Pass to the item factory
48 # the list of menu items
49 item_factory.create_items(self.menu_items)
50
51 # Attach the new accelerator group to the window.
52 window.add_accel_group(accel_group)
53
54 # Finally, return the actual menu bar created by the item factory.
55 return item_factory.get_widget("<main>")
56
57 def __init__(self):
58 self.menu_items = (
59 ( "/_File", None, None, 0, "<Branch>" ),
60 ( "/File/_New", "<control>N", self.print_hello, 0, None ),
61 ( "/File/_Open", "<control>O", self.print_hello, 0, None ),
62 ( "/File/_Save", "<control>S", self.print_hello, 0, None ),
63 ( "/File/Save _As", None, None, 0, None ),
64 ( "/File/sep1", None, None, 0, "<Separator>" ),
65 ( "/File/Quit", "<control>Q", gtk.mainquit, 0, None ),
66 ( "/_Options", None, None, 0, "<Branch>" ),
67 ( "/Options/Test", None, None, 0, None ),
68 ( "/_Help", None, None, 0, "<LastBranch>" ),
69 ( "/_Help/About", None, None, 0, None ),
70 )
71 window = gtk.GtkWindow(gtk.WINDOW_TOPLEVEL)
72 window.connect("destroy", gtk.mainquit, "WM destroy")
73 window.set_title("Item Factory")
74 window.set_usize(300, 200)
75
76 main_vbox = gtk.GtkVBox(gtk.FALSE, 1)
77 main_vbox.set_border_width(1)
78 window.add(main_vbox)
79 main_vbox.show()
80
81 menubar = self.get_main_menu(window)
82 main_vbox.pack_start(menubar, gtk.FALSE, gtk.TRUE, 0)
83 menubar.show()
84 window.show()
85
86 def main():
87 gtk.mainloop()
88 return 0
89
90 if __name__ == "__main__":
91 ItemFactoryExample()
92 main()
|