]> git.saurik.com Git - wxWidgets.git/blame_incremental - wxPython/demo/wxMenu.py
reSWIGged
[wxWidgets.git] / wxPython / demo / wxMenu.py
... / ...
CommitLineData
1#-------------------------------------------------------------------
2# essaimenu.py
3#
4# menus in wxPython 2.3.3
5#
6#-------------------------------------------------------------------
7# 11/20/2003 - Jeff Grimmett (grimmtooth@softhome.net)
8#
9# o Updated for wx namespace
10#
11
12import time
13import wx
14import images
15
16#-------------------------------------------------------------------
17
18class MyFrame(wx.Frame):
19
20 def __init__(self, parent, id, log):
21 wx.Frame.__init__(self, parent, id, 'Playing with menus', size=(400, 200))
22 self.log = log
23 self.CenterOnScreen()
24
25 self.CreateStatusBar()
26 self.SetStatusText("This is the statusbar")
27
28 tc = wx.TextCtrl(self, -1, """
29A bunch of bogus menus have been created for this frame. You
30can play around with them to see how they behave and then
31check the source for this sample to see how to implement them.
32""", style=wx.TE_READONLY|wx.TE_MULTILINE)
33
34 # Prepare the menu bar
35 menuBar = wx.MenuBar()
36
37 # 1st menu from left
38 menu1 = wx.Menu()
39 menu1.Append(101, "&Mercury", "This the text in the Statusbar")
40 menu1.Append(102, "&Venus", "")
41 menu1.Append(103, "&Earth", "You may select Earth too")
42 menu1.AppendSeparator()
43 menu1.Append(104, "&Close", "Close this frame")
44 # Add menu to the menu bar
45 menuBar.Append(menu1, "&Planets")
46
47 # 2nd menu from left
48 menu2 = wx.Menu()
49 menu2.Append(201, "Hydrogen")
50 menu2.Append(202, "Helium")
51 # a submenu in the 2nd menu
52 submenu = wx.Menu()
53 submenu.Append(2031,"Lanthanium")
54 submenu.Append(2032,"Cerium")
55 submenu.Append(2033,"Praseodymium")
56 menu2.AppendMenu(203, "Lanthanides", submenu)
57 # Append 2nd menu
58 menuBar.Append(menu2, "&Elements")
59
60 menu3 = wx.Menu()
61 # Radio items
62 menu3.Append(301, "IDLE", "a Python shell using tcl/tk as GUI", wx.ITEM_RADIO)
63 menu3.Append(302, "PyCrust", "a Python shell using wxPython as GUI", wx.ITEM_RADIO)
64 menu3.Append(303, "psi", "a simple Python shell using wxPython as GUI", wx.ITEM_RADIO)
65 menu3.AppendSeparator()
66 menu3.Append(304, "project1", "", wx.ITEM_NORMAL)
67 menu3.Append(305, "project2", "", wx.ITEM_NORMAL)
68 menuBar.Append(menu3, "&Shells")
69
70 menu4 = wx.Menu()
71 # Check menu items
72 menu4.Append(401, "letters", "abcde...", wx.ITEM_CHECK)
73 menu4.Append(402, "digits", "123...", wx.ITEM_CHECK)
74 menu4.Append(403, "letters and digits", "abcd... + 123...", wx.ITEM_CHECK)
75 menuBar.Append(menu4, "Chec&k")
76
77 menu5 = wx.Menu()
78 # Show how to put an icon in the menu
79 item = wx.MenuItem(menu5, 500, "&Smile!\tCtrl+S", "This one has an icon")
80 item.SetBitmap(images.getSmilesBitmap())
81 menu5.AppendItem(item)
82
83 # Shortcuts
84 menu5.Append(501, "Interesting thing\tCtrl+A", "Note the shortcut!")
85 menu5.AppendSeparator()
86 menu5.Append(502, "Hello\tShift+H")
87 menu5.AppendSeparator()
88 menu5.Append(503, "remove the submenu")
89 menu6 = wx.Menu()
90 menu6.Append(601, "Submenu Item")
91 menu5.AppendMenu(504, "submenu", menu6)
92 menu5.Append(505, "remove this menu")
93 menu5.Append(506, "this is updated")
94 menu5.Append(507, "insert after this...")
95 menu5.Append(508, "...and before this")
96 menuBar.Append(menu5, "&Fun")
97
98 self.SetMenuBar(menuBar)
99
100 # Menu events
101 self.Bind(wx.EVT_MENU_HIGHLIGHT_ALL, self.OnMenuHighlight)
102
103 self.Bind(wx.EVT_MENU, self.Menu101, id=101)
104 self.Bind(wx.EVT_MENU, self.Menu102, id=102)
105 self.Bind(wx.EVT_MENU, self.Menu103, id=103)
106 self.Bind(wx.EVT_MENU, self.CloseWindow, id=104)
107
108 self.Bind(wx.EVT_MENU, self.Menu201, id=201)
109 self.Bind(wx.EVT_MENU, self.Menu202, id=202)
110 self.Bind(wx.EVT_MENU, self.Menu2031, id=2031)
111 self.Bind(wx.EVT_MENU, self.Menu2032, id=2032)
112 self.Bind(wx.EVT_MENU, self.Menu2033, id=2033)
113
114 self.Bind(wx.EVT_MENU, self.Menu301To303, id=301)
115 self.Bind(wx.EVT_MENU, self.Menu301To303, id=302)
116 self.Bind(wx.EVT_MENU, self.Menu301To303, id=303)
117 self.Bind(wx.EVT_MENU, self.Menu304, id=304)
118 self.Bind(wx.EVT_MENU, self.Menu305, id=305)
119
120 # Range of menu items
121 self.Bind(wx.EVT_MENU_RANGE, self.Menu401To403, id=401, id2=403)
122
123 self.Bind(wx.EVT_MENU, self.Menu500, id=500)
124 self.Bind(wx.EVT_MENU, self.Menu501, id=501)
125 self.Bind(wx.EVT_MENU, self.Menu502, id=502)
126 self.Bind(wx.EVT_MENU, self.TestRemove, id=503)
127 self.Bind(wx.EVT_MENU, self.TestRemove2, id=505)
128 self.Bind(wx.EVT_MENU, self.TestInsert, id=507)
129 self.Bind(wx.EVT_MENU, self.TestInsert, id=508)
130
131 wx.GetApp().Bind(wx.EVT_UPDATE_UI, self.TestUpdateUI, id=506)
132
133 # Methods
134
135 def OnMenuHighlight(self, event):
136 # Show how to get menu item imfo from this event handler
137 id = event.GetMenuId()
138 item = self.GetMenuBar().FindItemById(id)
139 text = item.GetText()
140 help = item.GetHelp()
141
142 #print text, help
143 # but in this case just call Skip so the default is done
144 event.Skip()
145
146
147 def Menu101(self, event):
148 self.log.write('Welcome to Mercury\n')
149
150 def Menu102(self, event):
151 self.log.write('Welcome to Venus\n')
152
153 def Menu103(self, event):
154 self.log.write('Welcome to the Earth\n')
155
156 def CloseWindow(self, event):
157 self.Close()
158
159 def Menu201(self, event):
160 self.log.write('Chemical element number 1\n')
161
162 def Menu202(self, event):
163 self.log.write('Chemical element number 2\n')
164
165 def Menu2031(self, event):
166 self.log.write('Element number 57\n')
167
168 def Menu2032(self, event):
169 self.log.write('Element number 58\n')
170
171 def Menu2033(self, event):
172 self.log.write('Element number 59\n')
173
174 def Menu301To303(self, event):
175 id = event.GetId()
176 self.log.write('Event id: %d\n' % id)
177
178 def Menu304(self, event):
179 self.log.write('Not yet available\n')
180
181 def Menu305(self, event):
182 self.log.write('Still vapour\n')
183
184 def Menu401To403(self, event):
185 self.log.write('From a EVT_MENU_RANGE event\n')
186
187 def Menu500(self, event):
188 self.log.write('Have a happy day!\n')
189
190 def Menu501(self, event):
191 self.log.write('Look in the code how the shortcut has been realized\n')
192
193 def Menu502(self, event):
194 self.log.write('Hello from Jean-Michel\n')
195
196
197 def TestRemove(self, evt):
198 mb = self.GetMenuBar()
199 submenuItem = mb.FindItemById(601)
200
201 if not submenuItem:
202 return
203
204 submenu = submenuItem.GetMenu()
205 menu = submenu.GetParent()
206
207 # This works
208 #menu.Remove(504)
209
210 # this also works
211 menu.RemoveItem(mb.FindItemById(504))
212
213 # This doesn't work, as expected since submenuItem is not on menu
214 #menu.RemoveItem(submenuItem)
215
216
217 def TestRemove2(self, evt):
218 mb = self.GetMenuBar()
219 mb.Remove(4)
220
221
222 def TestUpdateUI(self, evt):
223 text = time.ctime()
224 evt.SetText(text)
225
226
227 def TestInsert(self, evt):
228 theID = 508
229 # get the menu
230 mb = self.GetMenuBar()
231 menuItem = mb.FindItemById(theID)
232 menu = menuItem.GetMenu()
233
234 # figure out the position to insert at
235 pos = 0
236
237 for i in menu.GetMenuItems():
238 if i.GetId() == theID:
239 break
240
241 pos += 1
242
243 # now insert the new item
244 ID = wx.NewId()
245 ##menu.Insert(pos, ID, "NewItem " + str(ID))
246 item = wx.MenuItem(menu)
247 item.SetId(ID)
248 item.SetText("NewItem " + str(ID))
249 menu.InsertItem(pos, item)
250
251
252#-------------------------------------------------------------------
253
254wx.RegisterId(10000)
255
256def runTest(frame, nb, log):
257 win = MyFrame(frame, -1, log)
258 frame.otherWin = win
259 win.Show(True)
260
261
262#-------------------------------------------------------------------
263
264
265overview = """\
266A demo of using wxMenuBar and wxMenu in various ways.
267
268A menu is a popup (or pull down) list of items, one of which may be selected
269before the menu goes away (clicking elsewhere dismisses the menu). Menus may be
270used to construct either menu bars or popup menus.
271
272A menu item has an integer ID associated with it which can be used to identify
273the selection, or to change the menu item in some way. A menu item with a special
274identifier -1 is a separator item and doesn't have an associated command but just
275makes a separator line appear in the menu.
276
277Menu items may be either normal items, check items or radio items. Normal items
278don't have any special properties while the check items have a boolean flag associated
279to them and they show a checkmark in the menu when the flag is set. wxWindows
280automatically toggles the flag value when the item is clicked and its value may
281be retrieved using either IsChecked method of wxMenu or wxMenuBar itself or by
282using wxEvent.IsChecked when you get the menu notification for the item in question.
283
284The radio items are similar to the check items except that all the other items
285in the same radio group are unchecked when a radio item is checked. The radio group
286is formed by a contiguous range of radio items, i.e. it starts at the first item of
287this kind and ends with the first item of a different kind (or the end of the menu).
288Notice that because the radio groups are defined in terms of the item positions
289inserting or removing the items in the menu containing the radio items risks to not
290work correctly. Finally note that the radio items are only supported under Windows
291and GTK+ currently.
292
293"""
294
295
296if __name__ == '__main__':
297 import sys,os
298 import run
299 run.main(['', os.path.basename(sys.argv[0])])
300