Use list.sort's keyword parameters
[joel/kofoto.git] / src / packages / kofoto / gkofoto / menuhandler.py
1 import gtk
2
3 class MenuGroup:
4     def __init__(self, label=""):
5         self.__label = label
6         self.__childItems = []
7         self.__childItemsMap = {}
8         self.__radioGroup = None
9
10     def addMenuItem(self, label, callback, callbackData=None):
11         item = gtk.MenuItem(label)
12         self.__addItem(item, label, callback, callbackData)
13
14     def addStockImageMenuItem(self, label, stockId, callback,
15                               callbackData=None):
16         item = gtk.ImageMenuItem(label)
17         image = gtk.Image()
18         image.set_from_stock(stockId, gtk.ICON_SIZE_MENU)
19         item.set_image(image)
20         self.__addItem(item, label, callback, callbackData)
21
22     def addImageMenuItem(self, label, imageFilename, callback,
23                          callbackData=None):
24         item = gtk.ImageMenuItem(label)
25         image = gtk.Image()
26         image.set_from_file(imageFilename)
27         item.set_image(image)
28         self.__addItem(item, label, callback, callbackData)
29
30     def addCheckedMenuItem(self, label, callback, callbackData=None):
31         item = gtk.CheckMenuItem(label)
32         self.__addItem(item, label, callback, callbackData)
33
34     def addRadioMenuItem(self, label, callback, callbackData=None):
35         item = gtk.RadioMenuItem(self.__radioGroup, label)
36         self.__addItem(item, label, callback, callbackData)
37         self.__radioGroup = item
38
39     def addSeparator(self):
40         separator = gtk.SeparatorMenuItem()
41         self.__childItems.append(separator)
42         separator.show()
43         self.__radioGroup = None
44
45     def __getitem__(self, key):
46         return self.__childItemsMap[key]
47
48     def createGroupMenu(self):
49         menu = gtk.Menu()
50         for item in self:
51             menu.append(item)
52         menu.show()
53         return menu
54
55     def createGroupMenuItem(self):
56         menuItem = gtk.MenuItem(self.__label)
57         subMenu = self.createGroupMenu()
58         if len(self) > 0:
59             menuItem.set_submenu(subMenu)
60         else:
61             menuItem.set_sensitive(False)
62         menuItem.show()
63         return menuItem
64
65     def __len__(self):
66         return len(self.__childItems)
67
68     def __iter__(self):
69         for child in self.__childItems:
70             yield child
71
72     def enable(self):
73         for child in self.__childItems:
74             child.set_sensitive(True)
75
76     def disable(self):
77         for child in self.__childItems:
78             child.set_sensitive(False)
79
80     def __addItem(self, item, label, callback, callbackData=None):
81         if callbackData == None:
82             key = label
83         else:
84             key = callbackData
85         self.__childItemsMap[key] = item
86         self.__childItems.append(item)
87         item.connect("activate", callback, callbackData)
88         item.show()