Start development series 2.1-post
[memo.git] / MenuWindow.py
blob1b204bbdd495a9056f1966c2935cec15d5160312
1 import rox
2 from rox import g
3 from rox.Menu import Menu
5 actionsMenu = [
6 (_('/Add Memo...'), 'new_memo', "<StockItem>", "", g.STOCK_ADD),
7 (_('/Show All...'), 'show_all_memos', '<StockItem>', "", g.STOCK_EDIT),
10 mainMenu = [
11 (_('/Options...'), 'show_options', "<StockItem>", "", g.STOCK_PREFERENCES),
12 (_('/Help'), 'show_help', "<StockItem>", "", g.STOCK_HELP),
13 (_('/Quit'), 'quit', "<StockItem>", "", g.STOCK_QUIT),
16 SEPARATOR = ('/', '', '<Separator>')
18 class MenuWindow( object ):
19 def __init__(self, attach=True, full=True, additions=None):
20 """Initializes the menu for this window.
21 'full' decides whether the whole menu (actions and main) or just the
22 actions menu is shown.
23 'additions' is an optional dictionary as follows:
24 { 'topActions': [ Menu items to attach before the 'actions' menu ],
25 'bottomActions': [ Menu items to attach after the 'actions' menu ],
26 'topMain': [ Menu items to attach before the 'main' menu ],
27 'bottomMain': [ Menu items to attach after the 'main' menu ] }
28 """
29 self.additions = { 'topActions': [],
30 'bottomActions': [],
31 'topMain': [],
32 'bottomMain': [] }
33 self.menu = None
34 self.show_all_box = None
35 self.attach = attach
36 self.full = full
37 if additions is not None:
38 self.addAdditions( additions )
39 else:
40 self.set_menu()
42 def addAdditions(self, additions):
43 changed = False
44 for (key, list) in self.additions.items():
45 if additions.has_key(key):
46 changed = True
47 list.extend(additions[key])
48 if changed:
49 self.set_menu()
51 def removeAdditions(self, additions):
52 changed = False
53 for (key, list) in self.additions.items():
54 if additions.has_key(key):
55 toRemove = additions[key]
56 for item in toRemove:
57 try:
58 list.remove(item)
59 changed = True
60 except ValueError:
61 # If they're not already in the list, that's okay
62 pass
63 if changed:
64 self.set_menu()
66 def popup_menu(self, event, position=None):
67 self.menu.popup(self, event, position)
69 def set_menu(self, attach=None, full=None):
70 if attach is not None:
71 self.attach = attach
73 if full is not None:
74 self.full = full
76 menuList = []
77 menuName = 'actions'
78 menuList.extend(self.additions['topActions'])
79 menuList.extend( actionsMenu )
80 menuList.extend(self.additions['bottomActions'])
81 if self.full:
82 menuName = 'main'
83 menuList.append( SEPARATOR )
84 menuList.extend(self.additions['topMain'])
85 menuList.extend( mainMenu )
86 menuList.extend(self.additions['bottomMain'])
87 self.menu = Menu(menuName, menuList)
88 if attach:
89 self.menu.attach(self, self)
91 def new_memo(self, widget=None):
92 from EditBox import EditBox
93 EditBox().show()
95 def show_all_memos(self, widget=None):
96 if self.show_all_box:
97 self.show_all_box.present()
98 return
99 def destroyed(widget): self.show_all_box = None
100 from ShowAll import ShowAll
101 self.show_all_box = ShowAll()
102 self.show_all_box.connect('destroy', destroyed)
103 self.show_all_box.show()
105 def show_options(self, widget=None):
106 rox.edit_options()
108 def show_help(self, widget=None):
109 from rox import filer
110 filer.open_dir(rox.app_dir + '/Help')
112 def quit(self, widget=None):
113 self.destroy()