-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmenu.py
91 lines (74 loc) · 2.5 KB
/
menu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import wx
class MenuItem(object):
NORMAL = wx.ITEM_NORMAL
CHECK = wx.ITEM_CHECK
RADIO = wx.ITEM_RADIO
def __init__(self, id, label="", accel="", kind=NORMAL):
self.id = id
self.label = label
self.accel = accel
self.kind = kind
def Build(self, menu, hooks):
text = "%s\t%s" % (self.label, self.accel) if self.accel else self.label
menu.Append(self.id, text, kind=self.kind)
def GetAccelerators(self, hooks={}):
if self.accel:
accel = wx.AcceleratorEntry()
accel.FromString(self.accel)
accel.Set(accel.Flags, accel.KeyCode, self.id)
return [accel]
else:
return []
class MenuSeparator(object):
def Build(self, menu, hooks):
last = menu.GetMenuItemCount() - 1
if last >= 0 and menu.FindItemByPosition(last).GetKind() != wx.ITEM_SEPARATOR:
menu.AppendSeparator()
def GetAccelerators(self, hooks={}):
return []
class MenuHook(object):
def __init__(self, id):
self.id = id
def Build(self, menu, hooks):
for item in hooks.get(self.id, []):
item.Build(menu, hooks)
def GetAccelerators(self, hooks={}):
accels = []
for item in hooks.get(self.id, []):
accels.extend(item.GetAccelerators(hooks))
return accels
MenuSeparator = MenuSeparator()
class Menu(object):
def __init__(self, label, items):
self.label = label
self.items = items
def Build(self, menu, hooks):
menu.AppendSubMenu(self.Create(), self.label)
def Create(self, hooks={}):
menu = wx.Menu()
for item in self.items:
item.Build(menu, hooks)
last = menu.GetMenuItemCount() - 1
if last >= 0:
item = menu.FindItemByPosition(last)
if item.GetKind() == wx.ITEM_SEPARATOR:
menu.DeleteItem(item)
return menu
def GetAccelerators(self, hooks={}):
accels = []
for item in self.items:
accels.extend(item.GetAccelerators(hooks))
return accels
class MenuBar(object):
def __init__(self, menus):
self.menus = menus
def Create(self, hooks={}):
menubar = wx.MenuBar()
for menu in self.menus:
menubar.Append(menu.Create(hooks), menu.label)
return menubar
def GetAccelerators(self, hooks={}):
accels = []
for menu in self.menus:
accels.extend(menu.GetAccelerators(hooks))
return accels