blob: 866b8696443de9fc045123758b6ffa29f2f7338e [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
Jack Jansen5c440271998-07-13 13:41:02 +0000529
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000530 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
Jack Jansen5c440271998-07-13 13:41:02 +0000538
539 def delitem(self, item):
540 if item != len(self.items):
541 raise 'Can only delete last item of a menu'
542 self.menu.DeleteMenuItem(item)
543 del self.items[item-1]
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000544
545 def addcheck(self, label, shortcut=None, callback=None):
546 return self.additem(label, shortcut, callback, 'check')
547
548 def addradio(self, label, shortcut=None, callback=None):
549 return self.additem(label, shortcut, callback, 'radio')
550
551 def addseparator(self):
552 self.menu.AppendMenu('(-')
553 self.items.append('', None, None, 'separator')
554
555 def addsubmenu(self, label, title=''):
556 sub = Menu(self.bar, title, -1)
557 item = self.additem(label, '\x1B', None, 'submenu')
558 self.menu.SetItemMark(item, sub.id)
559 return sub
560
561 def dispatch(self, id, item, window, event):
Jack Jansenb1667ef1996-09-26 16:17:08 +0000562 title, shortcut, callback, mtype = self.items[item-1]
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000563 if callback:
Jack Jansenb1667ef1996-09-26 16:17:08 +0000564 if not self.bar.parent or type(callback) <> types.StringType:
565 menuhandler = callback
566 else:
567 # callback is string
568 wid = Win.FrontWindow()
569 if wid and self.bar.parent._windows.has_key(wid):
570 window = self.bar.parent._windows[wid]
571 if hasattr(window, "domenu_" + callback):
572 menuhandler = getattr(window, "domenu_" + callback)
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 elif hasattr(self.bar.parent, "domenu_" + callback):
580 menuhandler = getattr(self.bar.parent, "domenu_" + callback)
581 else:
582 # nothing we can do. we shouldn't have come this far
583 # since the menu item should have been disabled...
584 return
585 menuhandler(id, item, window, event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000586
Jack Jansencef2c591996-04-11 15:39:01 +0000587 def enable(self, onoff):
588 if onoff:
589 self.menu.EnableItem(0)
590 else:
591 self.menu.DisableItem(0)
Jack Jansen5c440271998-07-13 13:41:02 +0000592 if self.bar and self.bar.parent:
593 self.bar.parent.needmenubarredraw = 1
Jack Jansenbb6193c1998-05-06 15:33:09 +0000594
595class PopupMenu(Menu):
596 def __init__(self, bar):
597 Menu.__init__(self, bar, '(popup)', -1)
598
599 def popup(self, x, y, event, default=1, window=None):
600 # NOTE that x and y are global coordinates, and they should probably
601 # be topleft of the button the user clicked (not mouse-coordinates),
602 # so the popup nicely overlaps.
603 reply = self.menu.PopUpMenuSelect(x, y, default)
604 if not reply:
605 return
606 id = (reply & 0xffff0000) >> 16
607 item = reply & 0xffff
608 if not window:
609 wid = Win.FrontWindow()
610 try:
611 window = self.bar.parent._windows[wid]
612 except:
613 pass # If we can't find the window we pass None
614 self.dispatch(id, item, window, event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000615
616class MenuItem:
617 def __init__(self, menu, title, shortcut=None, callback=None, kind=None):
618 self.item = menu.additem(title, shortcut, callback)
Jack Jansendb9ff361996-03-12 13:32:03 +0000619 self.menu = menu
620
Jack Jansen5c440271998-07-13 13:41:02 +0000621 def delete(self):
622 self.menu.delitem(self.item)
623 del self.menu
624 del self.item
625
Jack Jansendb9ff361996-03-12 13:32:03 +0000626 def check(self, onoff):
627 self.menu.menu.CheckItem(self.item, onoff)
Jack Jansencef2c591996-04-11 15:39:01 +0000628
629 def enable(self, onoff):
630 if onoff:
631 self.menu.menu.EnableItem(self.item)
632 else:
633 self.menu.menu.DisableItem(self.item)
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000634
635 def settext(self, text):
636 self.menu.menu.SetMenuItemText(self.item, text)
Jack Jansendb9ff361996-03-12 13:32:03 +0000637
Jack Jansen0f6dc5b1996-04-23 16:18:33 +0000638 def setstyle(self, style):
639 self.menu.menu.SetItemStyle(self.item, style)
640
641 def seticon(self, icon):
642 self.menu.menu.SetItemIcon(self.item, icon)
643
644 def setcmd(self, cmd):
645 self.menu.menu.SetItemCmd(self.item, cmd)
646
647 def setmark(self, cmd):
648 self.menu.menu.SetItemMark(self.item, cmd)
649
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000650
651class RadioItem(MenuItem):
652 def __init__(self, menu, title, shortcut=None, callback=None):
653 MenuItem.__init__(self, menu, title, shortcut, callback, 'radio')
654
655class CheckItem(MenuItem):
656 def __init__(self, menu, title, shortcut=None, callback=None):
657 MenuItem.__init__(self, menu, title, shortcut, callback, 'check')
658
659def Separator(menu):
660 menu.addseparator()
661
662def SubMenu(menu, label, title=''):
663 return menu.addsubmenu(label, title)
664
665
666class AppleMenu(Menu):
667
668 def __init__(self, bar, abouttext="About me...", aboutcallback=None):
669 Menu.__init__(self, bar, "\024")
670 self.additem(abouttext, None, aboutcallback)
671 self.addseparator()
Jack Jansene4b40381995-07-17 13:25:15 +0000672 self.menu.AppendResMenu('DRVR')
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000673
674 def dispatch(self, id, item, window, event):
675 if item == 1:
676 Menu.dispatch(self, id, item, window, event)
677 else:
Jack Jansenc8a99491996-01-08 23:50:13 +0000678 name = self.menu.GetMenuItemText(item)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000679 OpenDeskAcc(name)
680
Jack Jansen7e0da901995-08-17 14:18:20 +0000681class Window:
682 """A single window belonging to an application"""
683
684 def __init__(self, parent):
685 self.wid = None
686 self.parent = parent
687
Jack Jansenc8a99491996-01-08 23:50:13 +0000688 def open(self, bounds=(40, 40, 400, 400), resid=None):
689 if resid <> None:
690 self.wid = GetNewWindow(resid, -1)
691 else:
692 self.wid = NewWindow(bounds, self.__class__.__name__, 1,
Jack Jansended835c1996-07-26 14:01:07 +0000693 8, -1, 1, 0) # changed to proc id 8 to include zoom box. jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000694 self.do_postopen()
695
696 def do_postopen(self):
697 """Tell our parent we exist"""
698 self.parent.appendwindow(self.wid, self)
699
700 def close(self):
Jack Jansen7e0da901995-08-17 14:18:20 +0000701 self.do_postclose()
702
703 def do_postclose(self):
704 self.parent.removewindow(self.wid)
705 self.parent = None
706 self.wid = None
Jack Jansenc8a99491996-01-08 23:50:13 +0000707
708 def SetPort(self):
709 # Convinience method
710 SetPort(self.wid)
Jack Jansen7e0da901995-08-17 14:18:20 +0000711
712 def do_inDrag(self, partcode, window, event):
713 where = event[3]
714 window.DragWindow(where, self.draglimit)
715
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000716 draglimit = screenbounds
Jack Jansen7e0da901995-08-17 14:18:20 +0000717
718 def do_inGoAway(self, partcode, window, event):
719 where = event[3]
720 if window.TrackGoAway(where):
721 self.close()
722
723 def do_inZoom(self, partcode, window, event):
724 (what, message, when, where, modifiers) = event
725 if window.TrackBox(where, partcode):
726 window.ZoomWindow(partcode, 1)
Jack Jansended835c1996-07-26 14:01:07 +0000727 rect = window.GetWindowUserState() # so that zoom really works... jvr
728 self.do_postresize(rect[2] - rect[0], rect[3] - rect[1], window) # jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000729
730 def do_inZoomIn(self, partcode, window, event):
731 SetPort(window) # !!!
732 self.do_inZoom(partcode, window, event)
733
734 def do_inZoomOut(self, partcode, window, event):
735 SetPort(window) # !!!
736 self.do_inZoom(partcode, window, event)
737
738 def do_inGrow(self, partcode, window, event):
739 (what, message, when, where, modifiers) = event
740 result = window.GrowWindow(where, self.growlimit)
741 if result:
742 height = (result>>16) & 0xffff # Hi word
743 width = result & 0xffff # Lo word
744 self.do_resize(width, height, window)
745
Jack Jansended835c1996-07-26 14:01:07 +0000746 growlimit = (50, 50, screenbounds[2] - screenbounds[0], screenbounds[3] - screenbounds[1]) # jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000747
748 def do_resize(self, width, height, window):
Jack Jansended835c1996-07-26 14:01:07 +0000749 l, t, r, b = self.wid.GetWindowPort().portRect # jvr, forGrowIcon
750 self.SetPort() # jvr
751 InvalRect((r - SCROLLBARWIDTH + 1, b - SCROLLBARWIDTH + 1, r, b)) # jvr
752 window.SizeWindow(width, height, 1) # changed updateFlag to true jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000753 self.do_postresize(width, height, window)
754
755 def do_postresize(self, width, height, window):
756 SetPort(window)
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000757 InvalRect(window.GetWindowPort().portRect)
Jack Jansen7e0da901995-08-17 14:18:20 +0000758
759 def do_inContent(self, partcode, window, event):
760 #
761 # If we're not frontmost, select ourselves and wait for
762 # the activate event.
763 #
764 if FrontWindow() <> window:
765 window.SelectWindow()
766 return
767 # We are. Handle the event.
768 (what, message, when, where, modifiers) = event
769 SetPort(window)
770 local = GlobalToLocal(where)
771 self.do_contentclick(local, modifiers, event)
772
773 def do_contentclick(self, local, modifiers, event):
Jack Jansended835c1996-07-26 14:01:07 +0000774 if DEBUG:
775 print 'Click in contents at %s, modifiers %s'%(local, modifiers)
Jack Jansen7e0da901995-08-17 14:18:20 +0000776
777 def do_rawupdate(self, window, event):
778 if DEBUG: print "raw update for", window
Jack Jansenda38f2d1995-11-14 10:15:42 +0000779 SetPort(window)
Jack Jansen7e0da901995-08-17 14:18:20 +0000780 window.BeginUpdate()
781 self.do_update(window, event)
782 window.EndUpdate()
783
784 def do_update(self, window, event):
Jack Jansended835c1996-07-26 14:01:07 +0000785 if DEBUG:
786 import time
787 for i in range(8):
788 time.sleep(0.1)
789 InvertRgn(window.GetWindowPort().visRgn)
790 FillRgn(window.GetWindowPort().visRgn, qd.gray)
791 else:
792 EraseRgn(window.GetWindowPort().visRgn)
Jack Jansen7e0da901995-08-17 14:18:20 +0000793
794 def do_activate(self, activate, event):
795 if DEBUG: print 'Activate %d for %s'%(activate, self.wid)
796
797class ControlsWindow(Window):
798
799 def do_rawupdate(self, window, event):
800 if DEBUG: print "raw update for", window
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000801 SetPort(window)
Jack Jansen7e0da901995-08-17 14:18:20 +0000802 window.BeginUpdate()
803 self.do_update(window, event)
Jack Jansended835c1996-07-26 14:01:07 +0000804 #DrawControls(window) # jvr
805 UpdateControls(window, window.GetWindowPort().visRgn) # jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000806 window.DrawGrowIcon()
807 window.EndUpdate()
808
809 def do_controlhit(self, window, control, pcode, event):
810 if DEBUG: print "control hit in", window, "on", control, "; pcode =", pcode
811
812 def do_inContent(self, partcode, window, event):
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000813 if FrontWindow() <> window:
814 window.SelectWindow()
815 return
Jack Jansen7e0da901995-08-17 14:18:20 +0000816 (what, message, when, where, modifiers) = event
Jack Jansenda38f2d1995-11-14 10:15:42 +0000817 SetPort(window) # XXXX Needed?
Jack Jansen7e0da901995-08-17 14:18:20 +0000818 local = GlobalToLocal(where)
Jack Jansen41e825a1998-05-28 14:22:48 +0000819 pcode, control = FindControl(local, window)
820 if pcode and control:
821 self.do_rawcontrolhit(window, control, pcode, local, event)
Jack Jansen7e0da901995-08-17 14:18:20 +0000822 else:
823 if DEBUG: print "FindControl(%s, %s) -> (%s, %s)" % \
Jack Jansen41e825a1998-05-28 14:22:48 +0000824 (local, window, pcode, control)
Jack Jansene3532151996-04-12 16:24:44 +0000825 self.do_contentclick(local, modifiers, event)
826
Jack Jansen41e825a1998-05-28 14:22:48 +0000827 def do_rawcontrolhit(self, window, control, pcode, local, event):
828 pcode = control.TrackControl(local)
829 if pcode:
830 self.do_controlhit(window, control, pcode, event)
831
Jack Jansene3532151996-04-12 16:24:44 +0000832class ScrolledWindow(ControlsWindow):
833 def __init__(self, parent):
834 self.barx = self.bary = None
Jack Jansen7bfc8751996-04-16 14:35:43 +0000835 self.barx_enabled = self.bary_enabled = 1
836 self.activated = 1
Jack Jansene3532151996-04-12 16:24:44 +0000837 ControlsWindow.__init__(self, parent)
838
839 def scrollbars(self, wantx=1, wanty=1):
840 SetPort(self.wid)
841 self.barx = self.bary = None
Jack Jansen7bfc8751996-04-16 14:35:43 +0000842 self.barx_enabled = self.bary_enabled = 1
Jack Jansene3532151996-04-12 16:24:44 +0000843 x0, y0, x1, y1 = self.wid.GetWindowPort().portRect
844 vx, vy = self.getscrollbarvalues()
Jack Jansen7bfc8751996-04-16 14:35:43 +0000845 if vx == None: self.barx_enabled, vx = 0, 0
846 if vy == None: self.bary_enabled, vy = 0, 0
Jack Jansene3532151996-04-12 16:24:44 +0000847 if wantx:
848 rect = x0-1, y1-(SCROLLBARWIDTH-1), x1-(SCROLLBARWIDTH-2), y1+1
849 self.barx = NewControl(self.wid, rect, "", 1, vx, 0, 32767, 16, 0)
Jack Jansen7bfc8751996-04-16 14:35:43 +0000850 if not self.barx_enabled: self.barx.HiliteControl(255)
851## InvalRect(rect)
Jack Jansene3532151996-04-12 16:24:44 +0000852 if wanty:
853 rect = x1-(SCROLLBARWIDTH-1), y0-1, x1+1, y1-(SCROLLBARWIDTH-2)
854 self.bary = NewControl(self.wid, rect, "", 1, vy, 0, 32767, 16, 0)
Jack Jansen7bfc8751996-04-16 14:35:43 +0000855 if not self.bary_enabled: self.bary.HiliteControl(255)
856## InvalRect(rect)
Jack Jansene3532151996-04-12 16:24:44 +0000857
858 def do_postclose(self):
859 self.barx = self.bary = None
860 ControlsWindow.do_postclose(self)
861
862 def do_activate(self, onoff, event):
Jack Jansen7bfc8751996-04-16 14:35:43 +0000863 self.activated = onoff
Jack Jansene3532151996-04-12 16:24:44 +0000864 if onoff:
Jack Jansen7bfc8751996-04-16 14:35:43 +0000865 if self.barx and self.barx_enabled:
Jack Jansended835c1996-07-26 14:01:07 +0000866 self.barx.ShowControl() # jvr
Jack Jansen7bfc8751996-04-16 14:35:43 +0000867 if self.bary and self.bary_enabled:
Jack Jansended835c1996-07-26 14:01:07 +0000868 self.bary.ShowControl() # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000869 else:
Jack Jansen7bfc8751996-04-16 14:35:43 +0000870 if self.barx:
Jack Jansended835c1996-07-26 14:01:07 +0000871 self.barx.HideControl() # jvr; An inactive window should have *hidden*
872 # scrollbars, not just dimmed (no matter what
873 # BBEdit does... look at the Finder)
Jack Jansen7bfc8751996-04-16 14:35:43 +0000874 if self.bary:
Jack Jansended835c1996-07-26 14:01:07 +0000875 self.bary.HideControl() # jvr
876 self.wid.DrawGrowIcon() # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000877
878 def do_postresize(self, width, height, window):
879 l, t, r, b = self.wid.GetWindowPort().portRect
Jack Jansended835c1996-07-26 14:01:07 +0000880 self.SetPort()
Jack Jansene3532151996-04-12 16:24:44 +0000881 if self.barx:
Jack Jansended835c1996-07-26 14:01:07 +0000882 self.barx.HideControl() # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000883 self.barx.MoveControl(l-1, b-(SCROLLBARWIDTH-1))
Jack Jansended835c1996-07-26 14:01:07 +0000884 self.barx.SizeControl((r-l)-(SCROLLBARWIDTH-3), SCROLLBARWIDTH) # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000885 if self.bary:
Jack Jansended835c1996-07-26 14:01:07 +0000886 self.bary.HideControl() # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000887 self.bary.MoveControl(r-(SCROLLBARWIDTH-1), t-1)
Jack Jansended835c1996-07-26 14:01:07 +0000888 self.bary.SizeControl(SCROLLBARWIDTH, (b-t)-(SCROLLBARWIDTH-3)) # jvr
889 if self.barx:
890 self.barx.ShowControl() # jvr
891 ValidRect((l, b - SCROLLBARWIDTH + 1, r - SCROLLBARWIDTH + 2, b)) # jvr
892 if self.bary:
893 self.bary.ShowControl() # jvr
894 ValidRect((r - SCROLLBARWIDTH + 1, t, r, b - SCROLLBARWIDTH + 2)) # jvr
895 InvalRect((r - SCROLLBARWIDTH + 1, b - SCROLLBARWIDTH + 1, r, b)) # jvr, growicon
Jack Jansene3532151996-04-12 16:24:44 +0000896
Jack Jansen41e825a1998-05-28 14:22:48 +0000897
898 def do_rawcontrolhit(self, window, control, pcode, local, event):
Jack Jansene3532151996-04-12 16:24:44 +0000899 if control == self.barx:
Jack Jansene3532151996-04-12 16:24:44 +0000900 which = 'x'
901 elif control == self.bary:
Jack Jansene3532151996-04-12 16:24:44 +0000902 which = 'y'
903 else:
904 return 0
Jack Jansen41e825a1998-05-28 14:22:48 +0000905 if pcode in (inUpButton, inDownButton, inPageUp, inPageDown):
906 # We do the work for the buttons and grey area in the tracker
907 dummy = control.TrackControl(local, self.do_controltrack)
908 else:
909 # but the thumb is handled here
910 pcode = control.TrackControl(local)
911 if pcode == inThumb:
912 value = control.GetControlValue()
913 print 'setbars', which, value #DBG
914 self.scrollbar_callback(which, 'set', value)
915 self.updatescrollbars()
916 else:
917 print 'funny part', pcode #DBG
918 return 1
919
920 def do_controltrack(self, control, pcode):
921 if control == self.barx:
922 which = 'x'
923 elif control == self.bary:
924 which = 'y'
925 else:
926 return
927
Jack Jansene3532151996-04-12 16:24:44 +0000928 if pcode == inUpButton:
929 what = '-'
930 elif pcode == inDownButton:
931 what = '+'
932 elif pcode == inPageUp:
933 what = '--'
934 elif pcode == inPageDown:
935 what = '++'
936 else:
Jack Jansen41e825a1998-05-28 14:22:48 +0000937 return
938 self.scrollbar_callback(which, what, None)
Jack Jansene3532151996-04-12 16:24:44 +0000939 self.updatescrollbars()
Jack Jansene3532151996-04-12 16:24:44 +0000940
941 def updatescrollbars(self):
942 SetPort(self.wid)
943 vx, vy = self.getscrollbarvalues()
944 if self.barx:
Jack Jansen7bfc8751996-04-16 14:35:43 +0000945 if vx == None:
946 self.barx.HiliteControl(255)
947 self.barx_enabled = 0
948 else:
949 if not self.barx_enabled:
950 self.barx_enabled = 1
951 if self.activated:
952 self.barx.HiliteControl(0)
953 self.barx.SetControlValue(vx)
Jack Jansene3532151996-04-12 16:24:44 +0000954 if self.bary:
Jack Jansen7bfc8751996-04-16 14:35:43 +0000955 if vy == None:
956 self.bary.HiliteControl(255)
957 self.bary_enabled = 0
958 else:
959 if not self.bary_enabled:
960 self.bary_enabled = 1
961 if self.activated:
962 self.bary.HiliteControl(0)
963 self.bary.SetControlValue(vy)
964
965 # Auxiliary function: convert standard text/image/etc coordinate
966 # to something palatable as getscrollbarvalues() return
967 def scalebarvalue(self, absmin, absmax, curmin, curmax):
968 if curmin <= absmin and curmax >= absmax:
969 return None
970 if curmin <= absmin:
971 return 0
972 if curmax >= absmax:
973 return 32767
974 perc = float(curmin-absmin)/float(absmax-absmin)
975 return int(perc*32767)
Jack Jansene3532151996-04-12 16:24:44 +0000976
977 # To be overridden:
978
979 def getscrollbarvalues(self):
980 return 0, 0
981
982 def scrollbar_callback(self, which, what, value):
983 print 'scroll', which, what, value
Jack Jansen7e0da901995-08-17 14:18:20 +0000984
985class DialogWindow(Window):
986 """A modeless dialog window"""
987
988 def open(self, resid):
989 self.wid = GetNewDialog(resid, -1)
990 self.do_postopen()
991
992 def close(self):
Jack Jansen7e0da901995-08-17 14:18:20 +0000993 self.do_postclose()
994
995 def do_itemhit(self, item, event):
996 print 'Dialog %s, item %d hit'%(self.wid, item)
997
998 def do_rawupdate(self, window, event):
999 pass
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +00001000
1001def ostypecode(x):
1002 "Convert a long int to the 4-character code it really is"
1003 s = ''
1004 for i in range(4):
1005 x, c = divmod(x, 256)
1006 s = chr(c) + s
1007 return s
1008
1009
1010class TestApp(Application):
1011
1012 "This class is used by the test() function"
1013
1014 def makeusermenus(self):
1015 self.filemenu = m = Menu(self.menubar, "File")
1016 self.saveitem = MenuItem(m, "Save", "S", self.save)
1017 Separator(m)
1018 self.optionsmenu = mm = SubMenu(m, "Options")
1019 self.opt1 = CheckItem(mm, "Arguments")
1020 self.opt2 = CheckItem(mm, "Being hit on the head lessons")
1021 self.opt3 = CheckItem(mm, "Complaints")
1022 Separator(m)
1023 self.quititem = MenuItem(m, "Quit", "Q", self.quit)
1024
1025 def save(self, *args):
1026 print "Save"
1027
1028 def quit(self, *args):
1029 raise self
1030
1031
1032def test():
1033 "Test program"
1034 app = TestApp()
1035 app.mainloop()
1036
1037
1038if __name__ == '__main__':
1039 test()