Package pallavi :: Package plugins :: Module SimpleMenubar
[hide private]
[frames] | no frames]

Source Code for Module pallavi.plugins.SimpleMenubar

  1  # Copyright (c) 2006 Dusty Phillips 
  2   
  3  # Permission is hereby granted, free of charge, to any person obtaining a 
  4  # copy of this software and associated documentation files (the "Software"), 
  5  # to deal in the Software without restriction, including without limitation 
  6  # the rights to use, copy, modify, merge, publish, distribute, sublicense, 
  7  # and/or sell copies of the Software, and to permit persons to whom the 
  8  # Software is furnished to do so, subject to the following conditions: 
  9   
 10  # The above copyright notice and this permission notice shall be included in 
 11  # all copies or substantial portions of the Software. 
 12   
 13  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 14  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 15  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 16  # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 17  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 18  # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 19  # DEALINGS IN THE SOFTWARE. 
 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 = [] 
53 -def setup():
54 eventBus.AddListener("ViewCreated", NewView) 55 actions.AddAction(EventActionManager.Action("BindMenuAction",BindMenuAction)) 56 actions.AddAction(EventActionManager.Action("BindMenuFunction",BindMenuFunction)) 57 actions.AddAction(EventActionManager.Action("AddSubmenu",AddSubmenu)) 58 for view in pallavi.views: 59 NewView(None, view)
60
61 -def NewView(name, view):
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
91 -def AddSubmenu(menuname, submenuname, view = None):
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
114 -def BindMenuAction(menuname, action, view=None, submenuname=None):
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
128 -def BindMenuFunction(menuname, text, function, view=None, submenuname=None):
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
141 -def BindAction(menu, action, view):
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
147 -def BindFunction(menu, text, function, view):
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": # Internationalization Alert 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
158 -def GetMenuOrSubmenu(menuname, submenuname, view):
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 #This is a long drawn out way of getting a submenu from a menu. 170 #Is there any easier way to do this? 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: #workaround for wxpython bug, should be able to remove second part on more recent versions of wxpython 176 menu = a.GetSubMenu() 177 178 #Make sure that the above loop actually found something to assign 179 #to menu 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