4 @brief Menu classes for wxGUI
11 (C) 2010-2012 by the GRASS Development Team
13 This program is free software under the GNU General Public License
14 (>=v2). Read the file COPYING that comes with GRASS for details.
16 @author Martin Landa <landa.martin gmail.com>
17 @author Pawel Netzel (menu customization)
18 @author Milena Nowotarska (menu customization)
19 @author Robert Szczepanek (menu customization)
20 @author Vaclav Petras <wenzeslaus gmail.com> (menu customization)
25 from core
import globalvar
26 from core
import utils
30 from lmgr.menudata
import ManagerData
35 """!Creates menubar"""
36 wx.MenuBar.__init__(self)
41 self.
menustyle = UserSettings.Get(group =
'appearance', key =
'menustyle', subkey =
'selection')
43 for eachMenuData
in self.menudata.GetMenu():
44 for eachHeading
in eachMenuData:
45 menuLabel = eachHeading[0]
46 menuItems = eachHeading[1]
49 def _createMenu(self, menuData):
52 for eachItem
in menuData:
53 if len(eachItem) == 2:
56 menu.AppendMenu(wx.ID_ANY, label, subMenu)
64 def _createMenuItem(self, menu, menustyle, label, help, handler, gcmd, keywords,
65 shortcut =
'', wxId = wx.ID_ANY, kind = wx.ITEM_NORMAL):
66 """!Creates menu items
67 There are three menu styles (menu item text styles).
68 1 -- label only, 2 -- label and cmd name, 3 -- cmd name only
71 menu.AppendSeparator()
75 helpString = gcmd +
' -- ' + help
77 label +=
' [' + gcmd +
']'
79 label =
' [' + gcmd +
']'
84 label +=
'\t' + shortcut
86 menuItem = menu.Append(wxId, label, helpString, kind)
88 self.
menucmd[menuItem.GetId()] = gcmd
95 if cmd
and cmd[0]
not in globalvar.grassCmd:
96 menuItem.Enable(
False)
98 rhandler = eval(
'self.parent.' + handler)
100 self.parent.Bind(wx.EVT_MENU, rhandler, menuItem)
107 """!Get list of commands
109 @return list of commands
115 Default menu help handler
118 id = event.GetMenuId()
119 item = self.FindItemById(id)
121 text = item.GetText()
122 help = item.GetHelp()
128 """!Show menu tree"""
129 def __init__(self, parent, id = wx.ID_ANY, **kwargs):
132 wx.Panel.__init__(self, parent = parent, id = id, **kwargs)
134 self.
dataBox = wx.StaticBox(parent = self, id = wx.ID_ANY,
135 label =
" %s " % _(
"Menu tree (double-click or Ctrl-Enter to run command)"))
141 self.
search = SearchModuleWindow(parent = self, showChoice =
False)
144 self.
btnRun = wx.Button(self, id = wx.ID_OK, label = _(
"&Run"))
145 self.btnRun.SetToolTipString(_(
"Run selected command from the menu tree"))
146 self.btnRun.Enable(
False)
149 self.btnRun.Bind(wx.EVT_BUTTON, self.
OnRun)
153 self.search.GetCtrl().Bind(wx.EVT_KEY_UP, self.
OnKeyUp)
157 self.search.SetFocus()
160 """!Do dialog layout"""
161 sizer = wx.BoxSizer(wx.VERTICAL)
164 dataSizer = wx.StaticBoxSizer(self.
dataBox, wx.HORIZONTAL)
165 dataSizer.Add(item = self.
tree, proportion =1,
169 btnSizer = wx.BoxSizer(wx.HORIZONTAL)
170 btnSizer.Add(item = self.
btnRun, proportion = 0)
172 sizer.Add(item = dataSizer, proportion = 1,
173 flag = wx.EXPAND | wx.ALL, border = 5)
175 sizer.Add(item = self.
search, proportion = 0,
176 flag = wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM, border = 5)
178 sizer.Add(item = btnSizer, proportion = 0,
179 flag = wx.ALIGN_RIGHT | wx.BOTTOM | wx.RIGHT, border = 5)
182 sizer.SetSizeHints(self)
187 self.SetAutoLayout(
True)
195 """!Run selected command"""
196 if not self.tree.GetSelected():
199 data = self.tree.GetPyData(self.tree.GetSelected())
203 handler =
'self.parent.' + data[
'handler'].lstrip(
'self.')
204 if data[
'handler'] ==
'self.OnXTerm':
205 wx.MessageBox(parent = self,
206 message = _(
'You must run this command from the menu or command line',
207 'This command require an XTerm'),
208 caption = _(
'Message'), style = wx.OK | wx.ICON_ERROR | wx.CENTRE)
209 elif data[
'command']:
210 eval(handler)(event =
None, cmd = data[
'command'].
split())
215 if event.GetKeyCode() == wx.WXK_RETURN:
216 if event.ControlDown():
222 """!Show selected item"""
223 self.tree.OnShowItem(event)
224 if self.tree.GetSelected():
227 self.btnRun.Enable(
False)
230 """!Item activated (double-click)"""
231 item = event.GetItem()
232 if not item
or not item.IsOk():
235 data = self.tree.GetPyData(item)
236 if not data
or 'command' not in data:
239 self.tree.itemSelected = item
245 item = event.GetItem()
246 if not item
or not item.IsOk():
249 data = self.tree.GetPyData(item)
250 if not data
or 'command' not in data:
254 label = data[
'command'] +
' -- ' + data[
'description']
256 label = data[
'description']
258 self.parent.SetStatusText(label, 0)
261 """!Update statusbar text"""
262 element = self.search.GetSelection()
263 value = event.GetEventObject().
GetValue()
264 self.tree.SearchItems(element = element, value = value)
266 nItems = len(self.tree.itemsMarked)
268 self.parent.SetStatusText(_(
"%d modules match") % nItems, 0)
270 self.parent.SetStatusText(
"", 0)
275 """!Menu tree class"""
280 super(MenuTree, self).
__init__(parent, **kwargs)
282 self.
menustyle = UserSettings.Get(group =
'appearance', key =
'menustyle', subkey =
'selection')
285 """!Load menu data tree
287 @param data menu data (None to use self.menudata)
293 for eachMenuData
in data.GetMenu():
294 for label, items
in eachMenuData:
295 item = self.AppendItem(parentId = self.root,
296 text = label.replace(
'&',
''))
299 def __AppendItems(self, item, data):
300 """!Append items into tree (used by Load()
302 @param item tree item (parent)
303 @parent data menu data"""
304 for eachItem
in data:
305 if len(eachItem) == 2:
307 itemSub = self.AppendItem(parentId = item,
315 label +=
' [' + eachItem[3] +
']'
317 label =
'[' + eachItem[3] +
']'
319 itemNew = self.AppendItem(parentId = item,
322 data = {
'item' : eachItem[0],
323 'description' : eachItem[1],
324 'handler' : eachItem[2],
325 'command' : eachItem[3],
326 'keywords' : eachItem[4] }
328 self.SetPyData(itemNew, data)
def EncodeString
Return encoded string using system locales.
def split
Platform spefic shlex.split.