blob: 647d9e4e469dedd866d133c9d2a5eae39a20ec6f [file] [log] [blame]
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +00001"A sort of application framework for the Mac"
2
Jack Jansen7a583361995-08-14 12:39:54 +00003DEBUG=0
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +00004
5import MacOS
6import traceback
7
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +00008from AE import *
9from AppleEvents import *
Jack Jansen7a583361995-08-14 12:39:54 +000010from Ctl import *
11from Controls import *
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000012from Dlg import *
13from Dialogs import *
14from Evt import *
15from Events import *
16from Menu import *
17from Menus import *
Jack Jansen7a583361995-08-14 12:39:54 +000018from Qd import *
19from QuickDraw import *
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000020#from Res import *
21#from Resources import *
22#from Snd import *
23#from Sound import *
24from Win import *
25from Windows import *
Jack Jansenb1667ef1996-09-26 16:17:08 +000026import types
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000027
28import EasyDialogs
29
30kHighLevelEvent = 23 # Don't know what header file this should come from
Jack Jansene3532151996-04-12 16:24:44 +000031SCROLLBARWIDTH = 16 # Again, not a clue...
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000032
33
34# Map event 'what' field to strings
35eventname = {}
36eventname[1] = 'mouseDown'
37eventname[2] = 'mouseUp'
38eventname[3] = 'keyDown'
39eventname[4] = 'keyUp'
40eventname[5] = 'autoKey'
41eventname[6] = 'updateEvt'
42eventname[7] = 'diskEvt'
43eventname[8] = 'activateEvt'
44eventname[15] = 'osEvt'
45eventname[23] = 'kHighLevelEvent'
46
47# Map part codes returned by WhichWindow() to strings
48partname = {}
49partname[0] = 'inDesk'
50partname[1] = 'inMenuBar'
51partname[2] = 'inSysWindow'
52partname[3] = 'inContent'
53partname[4] = 'inDrag'
54partname[5] = 'inGrow'
55partname[6] = 'inGoAway'
56partname[7] = 'inZoomIn'
57partname[8] = 'inZoomOut'
58
Jack Jansenc4eec9f1996-04-19 16:00:28 +000059#
60# The useable portion of the screen
Jack Jansended835c1996-07-26 14:01:07 +000061# ## but what happens with multiple screens? jvr
Jack Jansenc4eec9f1996-04-19 16:00:28 +000062screenbounds = qd.screenBits.bounds
63screenbounds = screenbounds[0]+4, screenbounds[1]+4, \
64 screenbounds[2]-4, screenbounds[3]-4
65
Jack Jansended835c1996-07-26 14:01:07 +000066next_window_x = 16 # jvr
67next_window_y = 44 # jvr
Jack Jansenc4eec9f1996-04-19 16:00:28 +000068
69def windowbounds(width, height):
70 "Return sensible window bounds"
71 global next_window_x, next_window_y
72 r, b = next_window_x+width, next_window_y+height
73 if r > screenbounds[2]:
Jack Jansended835c1996-07-26 14:01:07 +000074 next_window_x = 16
Jack Jansenc4eec9f1996-04-19 16:00:28 +000075 if b > screenbounds[3]:
Jack Jansended835c1996-07-26 14:01:07 +000076 next_window_y = 44
Jack Jansenc4eec9f1996-04-19 16:00:28 +000077 l, t = next_window_x, next_window_y
78 r, b = next_window_x+width, next_window_y+height
Jack Jansended835c1996-07-26 14:01:07 +000079 next_window_x, next_window_y = next_window_x + 8, next_window_y + 20 # jvr
Jack Jansenc4eec9f1996-04-19 16:00:28 +000080 return l, t, r, b
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000081
Jack Jansen46341301996-08-28 13:53:07 +000082_watch = None
83def setwatchcursor():
84 global _watch
85
86 if _watch == None:
87 _watch = GetCursor(4).data
88 SetCursor(_watch)
89
90def setarrowcursor():
91 SetCursor(qd.arrow)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000092
93class Application:
94
95 "Application framework -- your application should be a derived class"
96
Jack Jansen647535d1996-09-17 12:35:43 +000097 def __init__(self, nomenubar=0):
Jack Jansend080edd1997-06-20 16:24:24 +000098 self._doing_asyncevents = 0
Jack Jansen647535d1996-09-17 12:35:43 +000099 self.quitting = 0
Jack Jansenb1667ef1996-09-26 16:17:08 +0000100 self.needmenubarredraw = 0
Jack Jansen7e0da901995-08-17 14:18:20 +0000101 self._windows = {}
Jack Jansen647535d1996-09-17 12:35:43 +0000102 if nomenubar:
103 self.menubar = None
104 else:
105 self.makemenubar()
Jack Jansend080edd1997-06-20 16:24:24 +0000106
107 def __del__(self):
108 if self._doing_asyncevents:
109 self._doing_asyncevents = 0
110 MacOS.SetEventHandler()
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000111
112 def makemenubar(self):
Jack Jansenb1667ef1996-09-26 16:17:08 +0000113 self.menubar = MenuBar(self)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000114 AppleMenu(self.menubar, self.getabouttext(), self.do_about)
115 self.makeusermenus()
Jack Jansenc8a99491996-01-08 23:50:13 +0000116
117 def makeusermenus(self):
118 self.filemenu = m = Menu(self.menubar, "File")
119 self._quititem = MenuItem(m, "Quit", "Q", self._quit)
120
121 def _quit(self, *args):
Jack Jansen647535d1996-09-17 12:35:43 +0000122 self.quitting = 1
Jack Jansen7e0da901995-08-17 14:18:20 +0000123
Jack Jansenc75e1d01996-12-23 17:22:40 +0000124 def cleanup(self):
125 for w in self._windows.values():
126 w.do_close()
127 return self._windows == {}
128
Jack Jansen7e0da901995-08-17 14:18:20 +0000129 def appendwindow(self, wid, window):
130 self._windows[wid] = window
131
132 def removewindow(self, wid):
133 del self._windows[wid]
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000134
135 def getabouttext(self):
136 return "About %s..." % self.__class__.__name__
137
138 def do_about(self, id, item, window, event):
139 EasyDialogs.Message("Hello, world!" + "\015(%s)" % self.__class__.__name__)
140
141 # The main event loop is broken up in several simple steps.
142 # This is done so you can override each individual part,
143 # if you have a need to do extra processing independent of the
144 # event type.
145 # Normally, however, you'd just define handlers for individual
146 # events.
147 # (XXX I'm not sure if using default parameter values is the right
148 # way to define the mask and wait time passed to WaitNextEvent.)
149
150 def mainloop(self, mask = everyEvent, wait = 0):
Jack Jansen647535d1996-09-17 12:35:43 +0000151 self.quitting = 0
Jack Jansen3368cb71997-06-12 10:51:18 +0000152 saveparams = apply(MacOS.SchedParams, self.schedparams)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000153 try:
Jack Jansen647535d1996-09-17 12:35:43 +0000154 while not self.quitting:
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000155 try:
156 self.do1event(mask, wait)
157 except (Application, SystemExit):
Jack Jansen647535d1996-09-17 12:35:43 +0000158 # Note: the raising of "self" is old-fashioned idiom to
159 # exit the mainloop. Calling _quit() is better for new
160 # applications.
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000161 break
162 finally:
Jack Jansen3368cb71997-06-12 10:51:18 +0000163 apply(MacOS.SchedParams, self.schedparams)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000164
Jack Jansen3368cb71997-06-12 10:51:18 +0000165 schedparams = MacOS.SchedParams()
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000166
Jack Jansend080edd1997-06-20 16:24:24 +0000167 def dopendingevents(self, mask = everyEvent):
168 """dopendingevents - Handle all pending events"""
169 while self.do1event(mask, wait=0):
170 pass
171
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000172 def do1event(self, mask = everyEvent, wait = 0):
Jack Jansen13dc4f71995-08-31 13:38:01 +0000173 ok, event = self.getevent(mask, wait)
174 if IsDialogEvent(event):
175 if self.do_dialogevent(event):
176 return
177 if ok:
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000178 self.dispatch(event)
Jack Jansen38186781995-11-10 14:48:36 +0000179 else:
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000180 self.idle(event)
Jack Jansen38186781995-11-10 14:48:36 +0000181
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000182 def idle(self, event):
Jack Jansen38186781995-11-10 14:48:36 +0000183 pass
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000184
185 def getevent(self, mask = everyEvent, wait = 0):
Jack Jansenb1667ef1996-09-26 16:17:08 +0000186 if self.needmenubarredraw:
187 DrawMenuBar()
188 self.needmenubarredraw = 0
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000189 ok, event = WaitNextEvent(mask, wait)
Jack Jansen13dc4f71995-08-31 13:38:01 +0000190 return ok, event
191
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000192 def dispatch(self, event):
Jack Jansend080edd1997-06-20 16:24:24 +0000193 # The following appears to be double work (already done in do1event)
194 # but we need it for asynchronous event handling
195 if IsDialogEvent(event):
196 if self.do_dialogevent(event):
197 return
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000198 (what, message, when, where, modifiers) = event
199 if eventname.has_key(what):
200 name = "do_" + eventname[what]
201 else:
202 name = "do_%d" % what
203 try:
204 handler = getattr(self, name)
205 except AttributeError:
206 handler = self.do_unknownevent
207 handler(event)
Jack Jansen7e0da901995-08-17 14:18:20 +0000208
Jack Jansend080edd1997-06-20 16:24:24 +0000209 def asyncevents(self, onoff):
210 """asyncevents - Set asynchronous event handling on or off"""
211 old = self._doing_asyncevents
212 if old:
213 MacOS.SetEventHandler()
214 apply(MacOS.SchedParams, self.schedparams)
215 if onoff:
216 MacOS.SetEventHandler(self.dispatch)
217 doint, dummymask, benice, howoften, bgyield = \
218 self.schedparams
219 MacOS.SchedParams(doint, everyEvent, benice,
220 howoften, bgyield)
221 self._doing_asyncevents = onoff
222 return old
223
Jack Jansen7e0da901995-08-17 14:18:20 +0000224 def do_dialogevent(self, event):
225 gotone, window, item = DialogSelect(event)
226 if gotone:
227 if self._windows.has_key(window):
Jack Jansen13dc4f71995-08-31 13:38:01 +0000228 self._windows[window].do_itemhit(item, event)
Jack Jansen7e0da901995-08-17 14:18:20 +0000229 else:
230 print 'Dialog event for unknown dialog'
Jack Jansen13dc4f71995-08-31 13:38:01 +0000231 return 1
232 return 0
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000233
234 def do_mouseDown(self, event):
235 (what, message, when, where, modifiers) = event
Jack Jansen7e0da901995-08-17 14:18:20 +0000236 partcode, wid = FindWindow(where)
237
238 #
239 # Find the correct name.
240 #
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000241 if partname.has_key(partcode):
242 name = "do_" + partname[partcode]
243 else:
244 name = "do_%d" % partcode
Jack Jansen7e0da901995-08-17 14:18:20 +0000245
246 if wid == None:
247 # No window, or a non-python window
248 try:
249 handler = getattr(self, name)
250 except AttributeError:
251 # Not menubar or something, so assume someone
252 # else's window
253 MacOS.HandleEvent(event)
254 return
255 elif self._windows.has_key(wid):
256 # It is a window. Hand off to correct window.
257 window = self._windows[wid]
258 try:
259 handler = getattr(window, name)
260 except AttributeError:
261 handler = self.do_unknownpartcode
262 else:
263 # It is a python-toolbox window, but not ours.
264 handler = self.do_unknownwindow
265 handler(partcode, wid, event)
266
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000267 def do_inSysWindow(self, partcode, window, event):
Jack Jansen7a583361995-08-14 12:39:54 +0000268 MacOS.HandleEvent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000269
270 def do_inDesk(self, partcode, window, event):
Jack Jansen7a583361995-08-14 12:39:54 +0000271 MacOS.HandleEvent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000272
273 def do_inMenuBar(self, partcode, window, event):
Jack Jansen647535d1996-09-17 12:35:43 +0000274 if not self.menubar:
275 MacOS.HandleEvent(event)
276 return
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000277 (what, message, when, where, modifiers) = event
278 result = MenuSelect(where)
279 id = (result>>16) & 0xffff # Hi word
280 item = result & 0xffff # Lo word
281 self.do_rawmenu(id, item, window, event)
282
283 def do_rawmenu(self, id, item, window, event):
284 try:
285 self.do_menu(id, item, window, event)
286 finally:
287 HiliteMenu(0)
288
289 def do_menu(self, id, item, window, event):
290 self.menubar.dispatch(id, item, window, event)
291
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000292
293 def do_unknownpartcode(self, partcode, window, event):
294 (what, message, when, where, modifiers) = event
Jack Jansen7a583361995-08-14 12:39:54 +0000295 if DEBUG: print "Mouse down at global:", where
296 if DEBUG: print "\tUnknown part code:", partcode
Jack Jansen7e0da901995-08-17 14:18:20 +0000297 if DEBUG: print "\tEvent:", self.printevent(event)
298 MacOS.HandleEvent(event)
299
300 def do_unknownwindow(self, partcode, window, event):
301 if DEBUG: print 'Unknown window:', window
Jack Jansen7a583361995-08-14 12:39:54 +0000302 MacOS.HandleEvent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000303
304 def do_keyDown(self, event):
305 self.do_key(event)
306
307 def do_autoKey(self, event):
308 if not event[-1] & cmdKey:
309 self.do_key(event)
310
311 def do_key(self, event):
312 (what, message, when, where, modifiers) = event
313 c = chr(message & charCodeMask)
314 if modifiers & cmdKey:
315 if c == '.':
316 raise self
317 else:
Jack Jansen647535d1996-09-17 12:35:43 +0000318 if not self.menubar:
319 MacOS.HandleEvent(event)
320 return
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000321 result = MenuKey(ord(c))
322 id = (result>>16) & 0xffff # Hi word
323 item = result & 0xffff # Lo word
324 if id:
325 self.do_rawmenu(id, item, None, event)
Jack Jansen7e0da901995-08-17 14:18:20 +0000326# elif c == 'w':
327# w = FrontWindow()
328# if w:
329# self.do_close(w)
330# else:
331# if DEBUG: print 'Command-W without front window'
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000332 else:
Jack Jansen7a583361995-08-14 12:39:54 +0000333 if DEBUG: print "Command-" +`c`
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000334 else:
Jack Jansen7e0da901995-08-17 14:18:20 +0000335 # See whether the front window wants it
336 w = FrontWindow()
337 if w and self._windows.has_key(w):
338 window = self._windows[w]
339 try:
340 do_char = window.do_char
341 except AttributeError:
342 do_char = self.do_char
Jack Jansen6f47bf41995-12-12 15:03:35 +0000343 do_char(c, event)
344 # else it wasn't for us, sigh...
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000345
346 def do_char(self, c, event):
Jack Jansen7a583361995-08-14 12:39:54 +0000347 if DEBUG: print "Character", `c`
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000348
349 def do_updateEvt(self, event):
Jack Jansen7e0da901995-08-17 14:18:20 +0000350 (what, message, when, where, modifiers) = event
351 wid = WhichWindow(message)
352 if wid and self._windows.has_key(wid):
353 window = self._windows[wid]
354 window.do_rawupdate(wid, event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000355 else:
Jack Jansen7a583361995-08-14 12:39:54 +0000356 MacOS.HandleEvent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000357
Jack Jansen7e0da901995-08-17 14:18:20 +0000358 def do_activateEvt(self, event):
359 (what, message, when, where, modifiers) = event
Jack Jansenc8a99491996-01-08 23:50:13 +0000360 # XXXX Incorrect, should be fixed in suspendresume
361 if type(message) == type(1):
362 wid = WhichWindow(message)
363 else:
364 wid = message
Jack Jansen7e0da901995-08-17 14:18:20 +0000365 if wid and self._windows.has_key(wid):
366 window = self._windows[wid]
367 window.do_activate(modifiers & 1, event)
368 else:
369 MacOS.HandleEvent(event)
370
371 def do_osEvt(self, event):
372 (what, message, when, where, modifiers) = event
373 which = (message >> 24) & 0xff
374 if which == 1: # suspend/resume
375 self.do_suspendresume(event)
376 else:
377 if DEBUG:
378 print 'unknown osEvt:',
379 self.printevent(event)
380
381 def do_suspendresume(self, event):
382 # Is this a good idea???
383 (what, message, when, where, modifiers) = event
384 w = FrontWindow()
385 if w:
Jack Jansenc8a99491996-01-08 23:50:13 +0000386 # XXXX Incorrect, should stuff windowptr into message field
Jack Jansen7e0da901995-08-17 14:18:20 +0000387 nev = (activateEvt, w, when, where, message&1)
Jack Jansenc8a99491996-01-08 23:50:13 +0000388 self.do_activateEvt(nev)
Jack Jansen7e0da901995-08-17 14:18:20 +0000389
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000390 def do_kHighLevelEvent(self, event):
391 (what, message, when, where, modifiers) = event
Jack Jansen7a583361995-08-14 12:39:54 +0000392 if DEBUG:
393 print "High Level Event:",
394 self.printevent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000395 try:
396 AEProcessAppleEvent(event)
397 except:
398 print "AEProcessAppleEvent error:"
399 traceback.print_exc()
400
401 def do_unknownevent(self, event):
Jack Jansen7e0da901995-08-17 14:18:20 +0000402 if DEBUG:
403 print "Unhandled event:",
404 self.printevent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000405
406 def printevent(self, event):
407 (what, message, when, where, modifiers) = event
408 nicewhat = `what`
409 if eventname.has_key(what):
410 nicewhat = eventname[what]
411 print nicewhat,
412 if what == kHighLevelEvent:
413 h, v = where
414 print `ostypecode(message)`, hex(when), `ostypecode(h | (v<<16))`,
415 else:
416 print hex(message), hex(when), where,
417 print hex(modifiers)
418
419
420class MenuBar:
421 """Represent a set of menus in a menu bar.
422
423 Interface:
424
425 - (constructor)
426 - (destructor)
427 - addmenu
428 - addpopup (normally used internally)
429 - dispatch (called from Application)
430 """
431
432 nextid = 1 # Necessarily a class variable
433
434 def getnextid(self):
Jack Jansenb1667ef1996-09-26 16:17:08 +0000435 id = MenuBar.nextid
436 MenuBar.nextid = id+1
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000437 return id
438
Jack Jansenb1667ef1996-09-26 16:17:08 +0000439 def __init__(self, parent=None):
440 self.parent = parent
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000441 ClearMenuBar()
442 self.bar = GetMenuBar()
443 self.menus = {}
444
Jack Jansenb1667ef1996-09-26 16:17:08 +0000445 # XXX necessary?
446 def close(self):
447 self.parent = None
448 self.bar = None
Jack Jansen7b56aad1998-02-20 15:51:39 +0000449 self.menus = None
Jack Jansenb1667ef1996-09-26 16:17:08 +0000450
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000451 def addmenu(self, title, after = 0):
452 id = self.getnextid()
Jack Jansene3532151996-04-12 16:24:44 +0000453 if DEBUG: print 'Newmenu', title, id # XXXX
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000454 m = NewMenu(id, title)
455 m.InsertMenu(after)
Jack Jansenbb6193c1998-05-06 15:33:09 +0000456 if after >= 0:
457 if self.parent:
458 self.parent.needmenubarredraw = 1
459 else:
460 DrawMenuBar()
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000461 return id, m
Jack Jansendb9ff361996-03-12 13:32:03 +0000462
463 def delmenu(self, id):
Jack Jansene3532151996-04-12 16:24:44 +0000464 if DEBUG: print 'Delmenu', id # XXXX
Jack Jansendb9ff361996-03-12 13:32:03 +0000465 DeleteMenu(id)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000466
467 def addpopup(self, title = ''):
468 return self.addmenu(title, -1)
Jack Jansenb1667ef1996-09-26 16:17:08 +0000469
470# Useless:
471# def install(self):
472# if not self.bar: return
473# SetMenuBar(self.bar)
474# if self.parent:
475# self.parent.needmenubarredraw = 1
476# else:
477# DrawMenuBar()
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000478
Jack Jansenb1667ef1996-09-26 16:17:08 +0000479 def fixmenudimstate(self):
480 for m in self.menus.keys():
481 menu = self.menus[m]
482 if menu.__class__ == FrameWork.AppleMenu:
483 continue
484 for i in range(len(menu.items)):
485 label, shortcut, callback, kind = menu.items[i]
486 if type(callback) == types.StringType:
487 wid = Win.FrontWindow()
488 if wid and self.parent._windows.has_key(wid):
489 window = self.parent._windows[wid]
490 if hasattr(window, "domenu_" + callback):
491 menu.menu.EnableItem(i + 1)
492 elif hasattr(self.parent, "domenu_" + callback):
493 menu.menu.EnableItem(i + 1)
494 else:
495 menu.menu.DisableItem(i + 1)
496 elif hasattr(self.parent, "domenu_" + callback):
497 menu.menu.EnableItem(i + 1)
498 else:
499 menu.menu.DisableItem(i + 1)
500 elif callback:
501 pass
502
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000503 def dispatch(self, id, item, window, event):
504 if self.menus.has_key(id):
505 self.menus[id].dispatch(id, item, window, event)
506 else:
Jack Jansen7a583361995-08-14 12:39:54 +0000507 if DEBUG: print "MenuBar.dispatch(%d, %d, %s, %s)" % \
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000508 (id, item, window, event)
509
510
511# XXX Need a way to get menus as resources and bind them to callbacks
512
513class Menu:
514 "One menu."
515
516 def __init__(self, bar, title, after=0):
517 self.bar = bar
518 self.id, self.menu = self.bar.addmenu(title, after)
519 bar.menus[self.id] = self
520 self.items = []
Jack Jansendb9ff361996-03-12 13:32:03 +0000521
522 def delete(self):
523 self.bar.delmenu(self.id)
524 del self.bar.menus[self.id]
525 del self.bar
526 del self.items
527 del self.menu
528 del self.id
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000529
530 def additem(self, label, shortcut=None, callback=None, kind=None):
531 self.menu.AppendMenu('x') # add a dummy string
532 self.items.append(label, shortcut, callback, kind)
533 item = len(self.items)
Jack Jansene4b40381995-07-17 13:25:15 +0000534 self.menu.SetMenuItemText(item, label) # set the actual text
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000535 if shortcut:
536 self.menu.SetItemCmd(item, ord(shortcut))
537 return item
538
539 def addcheck(self, label, shortcut=None, callback=None):
540 return self.additem(label, shortcut, callback, 'check')
541
542 def addradio(self, label, shortcut=None, callback=None):
543 return self.additem(label, shortcut, callback, 'radio')
544
545 def addseparator(self):
546 self.menu.AppendMenu('(-')
547 self.items.append('', None, None, 'separator')
548
549 def addsubmenu(self, label, title=''):
550 sub = Menu(self.bar, title, -1)
551 item = self.additem(label, '\x1B', None, 'submenu')
552 self.menu.SetItemMark(item, sub.id)
553 return sub
554
555 def dispatch(self, id, item, window, event):
Jack Jansenb1667ef1996-09-26 16:17:08 +0000556 title, shortcut, callback, mtype = self.items[item-1]
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000557 if callback:
Jack Jansenb1667ef1996-09-26 16:17:08 +0000558 if not self.bar.parent or type(callback) <> types.StringType:
559 menuhandler = callback
560 else:
561 # callback is string
562 wid = Win.FrontWindow()
563 if wid and self.bar.parent._windows.has_key(wid):
564 window = self.bar.parent._windows[wid]
565 if hasattr(window, "domenu_" + callback):
566 menuhandler = getattr(window, "domenu_" + callback)
567 elif hasattr(self.bar.parent, "domenu_" + callback):
568 menuhandler = getattr(self.bar.parent, "domenu_" + callback)
569 else:
570 # nothing we can do. we shouldn't have come this far
571 # since the menu item should have been disabled...
572 return
573 elif hasattr(self.bar.parent, "domenu_" + callback):
574 menuhandler = getattr(self.bar.parent, "domenu_" + callback)
575 else:
576 # nothing we can do. we shouldn't have come this far
577 # since the menu item should have been disabled...
578 return
579 menuhandler(id, item, window, event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000580
Jack Jansencef2c591996-04-11 15:39:01 +0000581 def enable(self, onoff):
582 if onoff:
583 self.menu.EnableItem(0)
584 else:
585 self.menu.DisableItem(0)
Jack Jansenbb6193c1998-05-06 15:33:09 +0000586
587class PopupMenu(Menu):
588 def __init__(self, bar):
589 Menu.__init__(self, bar, '(popup)', -1)
590
591 def popup(self, x, y, event, default=1, window=None):
592 # NOTE that x and y are global coordinates, and they should probably
593 # be topleft of the button the user clicked (not mouse-coordinates),
594 # so the popup nicely overlaps.
595 reply = self.menu.PopUpMenuSelect(x, y, default)
596 if not reply:
597 return
598 id = (reply & 0xffff0000) >> 16
599 item = reply & 0xffff
600 if not window:
601 wid = Win.FrontWindow()
602 try:
603 window = self.bar.parent._windows[wid]
604 except:
605 pass # If we can't find the window we pass None
606 self.dispatch(id, item, window, event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000607
608class MenuItem:
609 def __init__(self, menu, title, shortcut=None, callback=None, kind=None):
610 self.item = menu.additem(title, shortcut, callback)
Jack Jansendb9ff361996-03-12 13:32:03 +0000611 self.menu = menu
612
613 def check(self, onoff):
614 self.menu.menu.CheckItem(self.item, onoff)
Jack Jansencef2c591996-04-11 15:39:01 +0000615
616 def enable(self, onoff):
617 if onoff:
618 self.menu.menu.EnableItem(self.item)
619 else:
620 self.menu.menu.DisableItem(self.item)
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000621
622 def settext(self, text):
623 self.menu.menu.SetMenuItemText(self.item, text)
Jack Jansendb9ff361996-03-12 13:32:03 +0000624
Jack Jansen0f6dc5b1996-04-23 16:18:33 +0000625 def setstyle(self, style):
626 self.menu.menu.SetItemStyle(self.item, style)
627
628 def seticon(self, icon):
629 self.menu.menu.SetItemIcon(self.item, icon)
630
631 def setcmd(self, cmd):
632 self.menu.menu.SetItemCmd(self.item, cmd)
633
634 def setmark(self, cmd):
635 self.menu.menu.SetItemMark(self.item, cmd)
636
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000637
638class RadioItem(MenuItem):
639 def __init__(self, menu, title, shortcut=None, callback=None):
640 MenuItem.__init__(self, menu, title, shortcut, callback, 'radio')
641
642class CheckItem(MenuItem):
643 def __init__(self, menu, title, shortcut=None, callback=None):
644 MenuItem.__init__(self, menu, title, shortcut, callback, 'check')
645
646def Separator(menu):
647 menu.addseparator()
648
649def SubMenu(menu, label, title=''):
650 return menu.addsubmenu(label, title)
651
652
653class AppleMenu(Menu):
654
655 def __init__(self, bar, abouttext="About me...", aboutcallback=None):
656 Menu.__init__(self, bar, "\024")
657 self.additem(abouttext, None, aboutcallback)
658 self.addseparator()
Jack Jansene4b40381995-07-17 13:25:15 +0000659 self.menu.AppendResMenu('DRVR')
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000660
661 def dispatch(self, id, item, window, event):
662 if item == 1:
663 Menu.dispatch(self, id, item, window, event)
664 else:
Jack Jansenc8a99491996-01-08 23:50:13 +0000665 name = self.menu.GetMenuItemText(item)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000666 OpenDeskAcc(name)
667
Jack Jansen7e0da901995-08-17 14:18:20 +0000668class Window:
669 """A single window belonging to an application"""
670
671 def __init__(self, parent):
672 self.wid = None
673 self.parent = parent
674
Jack Jansenc8a99491996-01-08 23:50:13 +0000675 def open(self, bounds=(40, 40, 400, 400), resid=None):
676 if resid <> None:
677 self.wid = GetNewWindow(resid, -1)
678 else:
679 self.wid = NewWindow(bounds, self.__class__.__name__, 1,
Jack Jansended835c1996-07-26 14:01:07 +0000680 8, -1, 1, 0) # changed to proc id 8 to include zoom box. jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000681 self.do_postopen()
682
683 def do_postopen(self):
684 """Tell our parent we exist"""
685 self.parent.appendwindow(self.wid, self)
686
687 def close(self):
Jack Jansen7e0da901995-08-17 14:18:20 +0000688 self.do_postclose()
689
690 def do_postclose(self):
691 self.parent.removewindow(self.wid)
692 self.parent = None
693 self.wid = None
Jack Jansenc8a99491996-01-08 23:50:13 +0000694
695 def SetPort(self):
696 # Convinience method
697 SetPort(self.wid)
Jack Jansen7e0da901995-08-17 14:18:20 +0000698
699 def do_inDrag(self, partcode, window, event):
700 where = event[3]
701 window.DragWindow(where, self.draglimit)
702
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000703 draglimit = screenbounds
Jack Jansen7e0da901995-08-17 14:18:20 +0000704
705 def do_inGoAway(self, partcode, window, event):
706 where = event[3]
707 if window.TrackGoAway(where):
708 self.close()
709
710 def do_inZoom(self, partcode, window, event):
711 (what, message, when, where, modifiers) = event
712 if window.TrackBox(where, partcode):
713 window.ZoomWindow(partcode, 1)
Jack Jansended835c1996-07-26 14:01:07 +0000714 rect = window.GetWindowUserState() # so that zoom really works... jvr
715 self.do_postresize(rect[2] - rect[0], rect[3] - rect[1], window) # jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000716
717 def do_inZoomIn(self, partcode, window, event):
718 SetPort(window) # !!!
719 self.do_inZoom(partcode, window, event)
720
721 def do_inZoomOut(self, partcode, window, event):
722 SetPort(window) # !!!
723 self.do_inZoom(partcode, window, event)
724
725 def do_inGrow(self, partcode, window, event):
726 (what, message, when, where, modifiers) = event
727 result = window.GrowWindow(where, self.growlimit)
728 if result:
729 height = (result>>16) & 0xffff # Hi word
730 width = result & 0xffff # Lo word
731 self.do_resize(width, height, window)
732
Jack Jansended835c1996-07-26 14:01:07 +0000733 growlimit = (50, 50, screenbounds[2] - screenbounds[0], screenbounds[3] - screenbounds[1]) # jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000734
735 def do_resize(self, width, height, window):
Jack Jansended835c1996-07-26 14:01:07 +0000736 l, t, r, b = self.wid.GetWindowPort().portRect # jvr, forGrowIcon
737 self.SetPort() # jvr
738 InvalRect((r - SCROLLBARWIDTH + 1, b - SCROLLBARWIDTH + 1, r, b)) # jvr
739 window.SizeWindow(width, height, 1) # changed updateFlag to true jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000740 self.do_postresize(width, height, window)
741
742 def do_postresize(self, width, height, window):
743 SetPort(window)
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000744 InvalRect(window.GetWindowPort().portRect)
Jack Jansen7e0da901995-08-17 14:18:20 +0000745
746 def do_inContent(self, partcode, window, event):
747 #
748 # If we're not frontmost, select ourselves and wait for
749 # the activate event.
750 #
751 if FrontWindow() <> window:
752 window.SelectWindow()
753 return
754 # We are. Handle the event.
755 (what, message, when, where, modifiers) = event
756 SetPort(window)
757 local = GlobalToLocal(where)
758 self.do_contentclick(local, modifiers, event)
759
760 def do_contentclick(self, local, modifiers, event):
Jack Jansended835c1996-07-26 14:01:07 +0000761 if DEBUG:
762 print 'Click in contents at %s, modifiers %s'%(local, modifiers)
Jack Jansen7e0da901995-08-17 14:18:20 +0000763
764 def do_rawupdate(self, window, event):
765 if DEBUG: print "raw update for", window
Jack Jansenda38f2d1995-11-14 10:15:42 +0000766 SetPort(window)
Jack Jansen7e0da901995-08-17 14:18:20 +0000767 window.BeginUpdate()
768 self.do_update(window, event)
769 window.EndUpdate()
770
771 def do_update(self, window, event):
Jack Jansended835c1996-07-26 14:01:07 +0000772 if DEBUG:
773 import time
774 for i in range(8):
775 time.sleep(0.1)
776 InvertRgn(window.GetWindowPort().visRgn)
777 FillRgn(window.GetWindowPort().visRgn, qd.gray)
778 else:
779 EraseRgn(window.GetWindowPort().visRgn)
Jack Jansen7e0da901995-08-17 14:18:20 +0000780
781 def do_activate(self, activate, event):
782 if DEBUG: print 'Activate %d for %s'%(activate, self.wid)
783
784class ControlsWindow(Window):
785
786 def do_rawupdate(self, window, event):
787 if DEBUG: print "raw update for", window
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000788 SetPort(window)
Jack Jansen7e0da901995-08-17 14:18:20 +0000789 window.BeginUpdate()
790 self.do_update(window, event)
Jack Jansended835c1996-07-26 14:01:07 +0000791 #DrawControls(window) # jvr
792 UpdateControls(window, window.GetWindowPort().visRgn) # jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000793 window.DrawGrowIcon()
794 window.EndUpdate()
795
796 def do_controlhit(self, window, control, pcode, event):
797 if DEBUG: print "control hit in", window, "on", control, "; pcode =", pcode
798
799 def do_inContent(self, partcode, window, event):
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000800 if FrontWindow() <> window:
801 window.SelectWindow()
802 return
Jack Jansen7e0da901995-08-17 14:18:20 +0000803 (what, message, when, where, modifiers) = event
Jack Jansenda38f2d1995-11-14 10:15:42 +0000804 SetPort(window) # XXXX Needed?
Jack Jansen7e0da901995-08-17 14:18:20 +0000805 local = GlobalToLocal(where)
Jack Jansen41e825a1998-05-28 14:22:48 +0000806 pcode, control = FindControl(local, window)
807 if pcode and control:
808 self.do_rawcontrolhit(window, control, pcode, local, event)
Jack Jansen7e0da901995-08-17 14:18:20 +0000809 else:
810 if DEBUG: print "FindControl(%s, %s) -> (%s, %s)" % \
Jack Jansen41e825a1998-05-28 14:22:48 +0000811 (local, window, pcode, control)
Jack Jansene3532151996-04-12 16:24:44 +0000812 self.do_contentclick(local, modifiers, event)
813
Jack Jansen41e825a1998-05-28 14:22:48 +0000814 def do_rawcontrolhit(self, window, control, pcode, local, event):
815 pcode = control.TrackControl(local)
816 if pcode:
817 self.do_controlhit(window, control, pcode, event)
818
Jack Jansene3532151996-04-12 16:24:44 +0000819class ScrolledWindow(ControlsWindow):
820 def __init__(self, parent):
821 self.barx = self.bary = None
Jack Jansen7bfc8751996-04-16 14:35:43 +0000822 self.barx_enabled = self.bary_enabled = 1
823 self.activated = 1
Jack Jansene3532151996-04-12 16:24:44 +0000824 ControlsWindow.__init__(self, parent)
825
826 def scrollbars(self, wantx=1, wanty=1):
827 SetPort(self.wid)
828 self.barx = self.bary = None
Jack Jansen7bfc8751996-04-16 14:35:43 +0000829 self.barx_enabled = self.bary_enabled = 1
Jack Jansene3532151996-04-12 16:24:44 +0000830 x0, y0, x1, y1 = self.wid.GetWindowPort().portRect
831 vx, vy = self.getscrollbarvalues()
Jack Jansen7bfc8751996-04-16 14:35:43 +0000832 if vx == None: self.barx_enabled, vx = 0, 0
833 if vy == None: self.bary_enabled, vy = 0, 0
Jack Jansene3532151996-04-12 16:24:44 +0000834 if wantx:
835 rect = x0-1, y1-(SCROLLBARWIDTH-1), x1-(SCROLLBARWIDTH-2), y1+1
836 self.barx = NewControl(self.wid, rect, "", 1, vx, 0, 32767, 16, 0)
Jack Jansen7bfc8751996-04-16 14:35:43 +0000837 if not self.barx_enabled: self.barx.HiliteControl(255)
838## InvalRect(rect)
Jack Jansene3532151996-04-12 16:24:44 +0000839 if wanty:
840 rect = x1-(SCROLLBARWIDTH-1), y0-1, x1+1, y1-(SCROLLBARWIDTH-2)
841 self.bary = NewControl(self.wid, rect, "", 1, vy, 0, 32767, 16, 0)
Jack Jansen7bfc8751996-04-16 14:35:43 +0000842 if not self.bary_enabled: self.bary.HiliteControl(255)
843## InvalRect(rect)
Jack Jansene3532151996-04-12 16:24:44 +0000844
845 def do_postclose(self):
846 self.barx = self.bary = None
847 ControlsWindow.do_postclose(self)
848
849 def do_activate(self, onoff, event):
Jack Jansen7bfc8751996-04-16 14:35:43 +0000850 self.activated = onoff
Jack Jansene3532151996-04-12 16:24:44 +0000851 if onoff:
Jack Jansen7bfc8751996-04-16 14:35:43 +0000852 if self.barx and self.barx_enabled:
Jack Jansended835c1996-07-26 14:01:07 +0000853 self.barx.ShowControl() # jvr
Jack Jansen7bfc8751996-04-16 14:35:43 +0000854 if self.bary and self.bary_enabled:
Jack Jansended835c1996-07-26 14:01:07 +0000855 self.bary.ShowControl() # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000856 else:
Jack Jansen7bfc8751996-04-16 14:35:43 +0000857 if self.barx:
Jack Jansended835c1996-07-26 14:01:07 +0000858 self.barx.HideControl() # jvr; An inactive window should have *hidden*
859 # scrollbars, not just dimmed (no matter what
860 # BBEdit does... look at the Finder)
Jack Jansen7bfc8751996-04-16 14:35:43 +0000861 if self.bary:
Jack Jansended835c1996-07-26 14:01:07 +0000862 self.bary.HideControl() # jvr
863 self.wid.DrawGrowIcon() # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000864
865 def do_postresize(self, width, height, window):
866 l, t, r, b = self.wid.GetWindowPort().portRect
Jack Jansended835c1996-07-26 14:01:07 +0000867 self.SetPort()
Jack Jansene3532151996-04-12 16:24:44 +0000868 if self.barx:
Jack Jansended835c1996-07-26 14:01:07 +0000869 self.barx.HideControl() # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000870 self.barx.MoveControl(l-1, b-(SCROLLBARWIDTH-1))
Jack Jansended835c1996-07-26 14:01:07 +0000871 self.barx.SizeControl((r-l)-(SCROLLBARWIDTH-3), SCROLLBARWIDTH) # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000872 if self.bary:
Jack Jansended835c1996-07-26 14:01:07 +0000873 self.bary.HideControl() # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000874 self.bary.MoveControl(r-(SCROLLBARWIDTH-1), t-1)
Jack Jansended835c1996-07-26 14:01:07 +0000875 self.bary.SizeControl(SCROLLBARWIDTH, (b-t)-(SCROLLBARWIDTH-3)) # jvr
876 if self.barx:
877 self.barx.ShowControl() # jvr
878 ValidRect((l, b - SCROLLBARWIDTH + 1, r - SCROLLBARWIDTH + 2, b)) # jvr
879 if self.bary:
880 self.bary.ShowControl() # jvr
881 ValidRect((r - SCROLLBARWIDTH + 1, t, r, b - SCROLLBARWIDTH + 2)) # jvr
882 InvalRect((r - SCROLLBARWIDTH + 1, b - SCROLLBARWIDTH + 1, r, b)) # jvr, growicon
Jack Jansene3532151996-04-12 16:24:44 +0000883
Jack Jansen41e825a1998-05-28 14:22:48 +0000884
885 def do_rawcontrolhit(self, window, control, pcode, local, event):
Jack Jansene3532151996-04-12 16:24:44 +0000886 if control == self.barx:
Jack Jansene3532151996-04-12 16:24:44 +0000887 which = 'x'
888 elif control == self.bary:
Jack Jansene3532151996-04-12 16:24:44 +0000889 which = 'y'
890 else:
891 return 0
Jack Jansen41e825a1998-05-28 14:22:48 +0000892 if pcode in (inUpButton, inDownButton, inPageUp, inPageDown):
893 # We do the work for the buttons and grey area in the tracker
894 dummy = control.TrackControl(local, self.do_controltrack)
895 else:
896 # but the thumb is handled here
897 pcode = control.TrackControl(local)
898 if pcode == inThumb:
899 value = control.GetControlValue()
900 print 'setbars', which, value #DBG
901 self.scrollbar_callback(which, 'set', value)
902 self.updatescrollbars()
903 else:
904 print 'funny part', pcode #DBG
905 return 1
906
907 def do_controltrack(self, control, pcode):
908 if control == self.barx:
909 which = 'x'
910 elif control == self.bary:
911 which = 'y'
912 else:
913 return
914
Jack Jansene3532151996-04-12 16:24:44 +0000915 if pcode == inUpButton:
916 what = '-'
917 elif pcode == inDownButton:
918 what = '+'
919 elif pcode == inPageUp:
920 what = '--'
921 elif pcode == inPageDown:
922 what = '++'
923 else:
Jack Jansen41e825a1998-05-28 14:22:48 +0000924 return
925 self.scrollbar_callback(which, what, None)
Jack Jansene3532151996-04-12 16:24:44 +0000926 self.updatescrollbars()
Jack Jansene3532151996-04-12 16:24:44 +0000927
928 def updatescrollbars(self):
929 SetPort(self.wid)
930 vx, vy = self.getscrollbarvalues()
931 if self.barx:
Jack Jansen7bfc8751996-04-16 14:35:43 +0000932 if vx == None:
933 self.barx.HiliteControl(255)
934 self.barx_enabled = 0
935 else:
936 if not self.barx_enabled:
937 self.barx_enabled = 1
938 if self.activated:
939 self.barx.HiliteControl(0)
940 self.barx.SetControlValue(vx)
Jack Jansene3532151996-04-12 16:24:44 +0000941 if self.bary:
Jack Jansen7bfc8751996-04-16 14:35:43 +0000942 if vy == None:
943 self.bary.HiliteControl(255)
944 self.bary_enabled = 0
945 else:
946 if not self.bary_enabled:
947 self.bary_enabled = 1
948 if self.activated:
949 self.bary.HiliteControl(0)
950 self.bary.SetControlValue(vy)
951
952 # Auxiliary function: convert standard text/image/etc coordinate
953 # to something palatable as getscrollbarvalues() return
954 def scalebarvalue(self, absmin, absmax, curmin, curmax):
955 if curmin <= absmin and curmax >= absmax:
956 return None
957 if curmin <= absmin:
958 return 0
959 if curmax >= absmax:
960 return 32767
961 perc = float(curmin-absmin)/float(absmax-absmin)
962 return int(perc*32767)
Jack Jansene3532151996-04-12 16:24:44 +0000963
964 # To be overridden:
965
966 def getscrollbarvalues(self):
967 return 0, 0
968
969 def scrollbar_callback(self, which, what, value):
970 print 'scroll', which, what, value
Jack Jansen7e0da901995-08-17 14:18:20 +0000971
972class DialogWindow(Window):
973 """A modeless dialog window"""
974
975 def open(self, resid):
976 self.wid = GetNewDialog(resid, -1)
977 self.do_postopen()
978
979 def close(self):
Jack Jansen7e0da901995-08-17 14:18:20 +0000980 self.do_postclose()
981
982 def do_itemhit(self, item, event):
983 print 'Dialog %s, item %d hit'%(self.wid, item)
984
985 def do_rawupdate(self, window, event):
986 pass
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000987
988def ostypecode(x):
989 "Convert a long int to the 4-character code it really is"
990 s = ''
991 for i in range(4):
992 x, c = divmod(x, 256)
993 s = chr(c) + s
994 return s
995
996
997class TestApp(Application):
998
999 "This class is used by the test() function"
1000
1001 def makeusermenus(self):
1002 self.filemenu = m = Menu(self.menubar, "File")
1003 self.saveitem = MenuItem(m, "Save", "S", self.save)
1004 Separator(m)
1005 self.optionsmenu = mm = SubMenu(m, "Options")
1006 self.opt1 = CheckItem(mm, "Arguments")
1007 self.opt2 = CheckItem(mm, "Being hit on the head lessons")
1008 self.opt3 = CheckItem(mm, "Complaints")
1009 Separator(m)
1010 self.quititem = MenuItem(m, "Quit", "Q", self.quit)
1011
1012 def save(self, *args):
1013 print "Save"
1014
1015 def quit(self, *args):
1016 raise self
1017
1018
1019def test():
1020 "Test program"
1021 app = TestApp()
1022 app.mainloop()
1023
1024
1025if __name__ == '__main__':
1026 test()