1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 '''Simple Menubar with built-in actions. Eventually, I suppose it should
22 be fully customizable, but I'm not even using this thing, so if somebody
23 likes menubars they can hack this plugin and send me the new version.
24
25 This plugin provides the following actions:
26 BindAction
27 To bind an action to a menu item in a specific menu or submenu
28 BindFunction
29 To bind a function to a menu item in a specific menu or submenu
30 AddSubmenu
31 Create a submenu in a menu for actions and functions to bind to
32
33 This plugin invokes the following actions:
34 NewView
35 NewFile
36 CloseFIle
37 OpenFile
38 SaveActive
39 SaveActiveAs
40 When a menu item is clicked.
41
42 This plugin listens for the following Events:
43 ViewCreated
44 To add a menubar to the newly created view'''
45
46 from pallavi import EventActionManager
47 import wx
48 import sys
49
50 extraActions = []
51 extraFunctions = []
52 extraSubmenus = []
60
62 menubar = wx.MenuBar()
63 file = wx.Menu()
64 BindAction(file, "NewView", view)
65 BindAction(file, "NewFile", view)
66 BindAction(file, "CloseFile", view)
67 BindAction(file, "OpenFile", view)
68 BindAction(file, "SaveActive", view)
69 BindAction(file, "SaveActiveAs", view)
70 BindFunction(file, "E&xit", lambda x:sys.exit(0), view)
71 menubar.Append(file, "&File")
72
73 edit = wx.Menu()
74 BindFunction(edit, "&Undo", lambda x:pallavi.focusedView.focusedTextView.Undo(), view)
75 BindFunction(edit, "&Redo", lambda x:pallavi.focusedView.focusedTextView.Redo(), view)
76 edit.AppendSeparator()
77 BindFunction(edit, "Cu&t", lambda x:pallavi.focusedView.focusedTextView.Cut(), view)
78 BindFunction(edit, "&Copy", lambda x:pallavi.focusedView.focusedTextView.Copy(), view)
79 BindFunction(edit, "&Paste", lambda x:pallavi.focusedView.focusedTextView.Paste(), view)
80 menubar.Append(edit, "&Edit")
81
82 view.SetMenuBar(menubar)
83
84 for menuname,submenuname in extraSubmenus:
85 AddSubmenu(menuname,submenuname,view)
86 for menuname, action, submenuname in extraActions:
87 BindMenuAction(menuname, action, view, submenuname)
88 for menuname, text, function in extraFunctions:
89 BindMenuFunction(menuname, text, function, view)
90
92 '''When called as and action, creates a submenu
93 with name submenuname in the menu with name menuname.
94 This action doesn't actually bind any actions, it just
95 creates the submenu. Use BindMenuAction with the
96 submenuname argument to bind an action to a submenu'''
97 if view == None: views = pallavi.views
98 else: views = [view]
99 for view in views:
100 menubar = view.GetMenuBar()
101 menu = None
102 try:
103 menu = menubar.GetMenu(menubar.FindMenu(menuname))
104 except wx._core.PyAssertionError:
105 menu = None
106 if menu == None:
107 menu = wx.Menu()
108 menubar.Append(menu, menuname)
109 submenu=wx.Menu()
110 menu.AppendMenu(wx.ID_ANY,submenuname,submenu)
111 if (menuname,submenuname) not in extraSubmenus:
112 extraSubmenus.append((menuname, submenuname))
113
115 '''Invoked by an action, it will locate the menu by name
116 or else create a new one with that name and bind it to
117 the menubar. Menuname is the name of the menu for the action
118 to be bound to, and submenuname is and optional argument
119 naming the submenu to bind an action to'''
120 if view == None: views = pallavi.views
121 else: views = [view]
122 for view in views:
123 menu = GetMenuOrSubmenu(menuname, submenuname, view)
124 BindAction(menu, action, view)
125 if (menuname, action, submenuname) not in extraActions:
126 extraActions.append((menuname, action, submenuname))
127
129 '''Invoked by an action, it will locate the menu by name
130 or else create a new one with that name and add it to
131 the menubar. It will then create a new menu item in that
132 menu with the text and bind it to a function'''
133 if view == None: views = pallavi.views
134 else: views = [view]
135 for view in views:
136 menubar = view.GetMenuBar()
137 menu = GetMenuOrSubmenu(menuname, submenuname, view)
138 BindFunction(menu, text, function, view)
139 extraFunctions.append((menuname, text, function, submenuname))
140
142 '''Convenience method to make the actions bound in the setup method
143 more readable. Menu is the menu to add a menuitem to. Action is the name
144 of an action to bind the item to.'''
145 BindFunction(menu, actions[action].label, lambda x:actions.Invoke(action), view)
146
148 '''Bind a function to a specific menu action. Menu is the menu to add
149 an item to. Text is the caption of the menu. function is the name of
150 the function to pass to it. Any remaining arguments will be passed
151 to that function.'''
152 if text == "&About":
153 menuItem = menu.Append(wx.ID_ABOUT, text)
154 else:
155 menuItem = menu.Append(wx.ID_ANY, text)
156 view.Bind(wx.EVT_MENU, function, menuItem)
157
159 '''Utility method called by the BindMenu* actions. Finds or creates
160 a menu or submenu that can have an item appended. Menuname is the
161 name of the menu, submenuname is the name of a submenu in that menu,
162 and view is the view to get a menubar for'''
163 menubar = view.GetMenuBar()
164 menu = None
165 try:
166 if submenuname == None:
167 menu = menubar.GetMenu(menubar.FindMenu(menuname))
168 else:
169
170
171 parent = menubar.GetMenu(menubar.FindMenu(menuname))
172 items = parent.GetMenuItems()
173 for a in items:
174 name = a.GetText()
175 if name == submenuname or name.replace("_", "&") == submenuname:
176 menu = a.GetSubMenu()
177
178
179
180 if not "menu" in locals():
181 menu = menubar.GetMenu(menubar.FindMenu(menuname))
182 except wx._core.PyAssertionError:
183 menu = None
184 if menu == None:
185 menu = wx.Menu()
186 menubar.Append(menu, menuname)
187 return menu
188