8 from ...util.callback import callback, in_callback
9 from . import panel as panel
13 """A `Bind`able version of :class:`wx.Menu`.
15 From the `wxPython Style Guide`_, you can't do
16 wx.Menu().Bind(...), so we hack around it by bubbling the Bind up
17 to the closest parent :class:`wx.Frame`.
19 .. _wxPython Style Guide:
20 http://wiki.wxpython.org/wxPython%20Style%20Guide#line-101
22 def __init__(self, parent=None, **kwargs):
24 super(Menu, self).__init__(**kwargs)
26 def Bind(self, **kwargs):
27 assert 'id' in kwargs, kwargs
29 while not isinstance(obj, wx.Frame):
34 class MenuBar (wx.MenuBar):
35 """A `Bind`able version of :class:`wx.MenuBar`.
37 See :class:`Menu` for the motivation.
39 def __init__(self, parent=None, **kwargs):
41 super(MenuBar, self).__init__(**kwargs)
43 def Append(self, menu, title):
45 super(MenuBar, self).Append(menu, title)
48 class FileMenu (Menu):
49 def __init__(self, callbacks=None, **kwargs):
50 super(FileMenu, self).__init__(**kwargs)
53 self._callbacks = callbacks
54 self._c = {'exit': self.Append(wx.ID_EXIT)}
55 self.Bind(event=wx.EVT_MENU, handler=self.close, id=wx.ID_EXIT)
58 def close(self, event):
62 class ViewMenu (Menu):
63 def __init__(self, callbacks=None, **kwargs):
64 super(ViewMenu, self).__init__(**kwargs)
67 self._callbacks = callbacks
69 for i,panelname in enumerate(sorted(panel.PANELS.keys())):
70 text = '%s\tF%d' % (panelname.capitalize, i+5)
71 self._c[panelname] = self.AppendCheckItem(id=wx.ID_ANY, text=text)
72 for item in self._c.values():
76 class PerspectiveMenu (Menu):
77 def __init__(self, callbacks=None, **kwargs):
78 super(PerspectiveMenu, self).__init__(**kwargs)
81 self._callbacks = callbacks
84 def update(self, perspectives, selected, callback):
85 """Rebuild the perspectives menu.
87 for item in self.GetMenuItems():
88 self.UnBind(item.GetId)
91 'save': self.Append(id=wx.ID_ANY, text='Save Perspective'),
92 'delete': self.Append(id=wx.ID_ANY, text='Delete Perspective'),
94 self.AppendSeparator()
95 for label in perspectives:
96 self._c[label] = self.AppendRadioItem(id=wx.ID_ANY, text=label)
97 self.Bind(event=wx.EVT_MENU, handler=callback,
98 id=self._c[label].GetId())
100 self._c[label].Check(True)
103 class HelpMenu (Menu):
104 def __init__(self, callbacks=None, **kwargs):
105 super(HelpMenu, self).__init__(**kwargs)
106 if callbacks == None:
108 self._callbacks = callbacks
109 self._c = {'about': self.Append(id=wx.ID_ABOUT)}
110 self.Bind(event=wx.EVT_MENU, handler=self.about, id=wx.ID_ABOUT)
113 def about(self, event):
117 class HookeMenuBar (MenuBar):
118 def __init__(self, callbacks=None, **kwargs):
119 super(HookeMenuBar, self).__init__(**kwargs)
120 if callbacks == None:
122 self._callbacks = callbacks
125 # Attach *Menu() instances
126 for key in ['file', 'view', 'perspective', 'help']:
127 cap_key = key.capitalize()
128 _class = globals()['%sMenu' % cap_key]
129 self._c[key] = _class(parent=self, callbacks=callbacks)
130 self.Append(self._c[key], cap_key)