blob: c002b1f9df59fcd84a341054e9a5ec53fae6152f [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 Jansenb1667ef1996-09-26 16:17:08 +0000456 if self.parent:
457 self.parent.needmenubarredraw = 1
458 else:
459 DrawMenuBar()
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000460 return id, m
Jack Jansendb9ff361996-03-12 13:32:03 +0000461
462 def delmenu(self, id):
Jack Jansene3532151996-04-12 16:24:44 +0000463 if DEBUG: print 'Delmenu', id # XXXX
Jack Jansendb9ff361996-03-12 13:32:03 +0000464 DeleteMenu(id)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000465
466 def addpopup(self, title = ''):
467 return self.addmenu(title, -1)
Jack Jansenb1667ef1996-09-26 16:17:08 +0000468
469# Useless:
470# def install(self):
471# if not self.bar: return
472# SetMenuBar(self.bar)
473# if self.parent:
474# self.parent.needmenubarredraw = 1
475# else:
476# DrawMenuBar()
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000477
Jack Jansenb1667ef1996-09-26 16:17:08 +0000478 def fixmenudimstate(self):
479 for m in self.menus.keys():
480 menu = self.menus[m]
481 if menu.__class__ == FrameWork.AppleMenu:
482 continue
483 for i in range(len(menu.items)):
484 label, shortcut, callback, kind = menu.items[i]
485 if type(callback) == types.StringType:
486 wid = Win.FrontWindow()
487 if wid and self.parent._windows.has_key(wid):
488 window = self.parent._windows[wid]
489 if hasattr(window, "domenu_" + callback):
490 menu.menu.EnableItem(i + 1)
491 elif hasattr(self.parent, "domenu_" + callback):
492 menu.menu.EnableItem(i + 1)
493 else:
494 menu.menu.DisableItem(i + 1)
495 elif hasattr(self.parent, "domenu_" + callback):
496 menu.menu.EnableItem(i + 1)
497 else:
498 menu.menu.DisableItem(i + 1)
499 elif callback:
500 pass
501
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000502 def dispatch(self, id, item, window, event):
503 if self.menus.has_key(id):
504 self.menus[id].dispatch(id, item, window, event)
505 else:
Jack Jansen7a583361995-08-14 12:39:54 +0000506 if DEBUG: print "MenuBar.dispatch(%d, %d, %s, %s)" % \
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000507 (id, item, window, event)
508
509
510# XXX Need a way to get menus as resources and bind them to callbacks
511
512class Menu:
513 "One menu."
514
515 def __init__(self, bar, title, after=0):
516 self.bar = bar
517 self.id, self.menu = self.bar.addmenu(title, after)
518 bar.menus[self.id] = self
519 self.items = []
Jack Jansendb9ff361996-03-12 13:32:03 +0000520
521 def delete(self):
522 self.bar.delmenu(self.id)
523 del self.bar.menus[self.id]
524 del self.bar
525 del self.items
526 del self.menu
527 del self.id
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000528
529 def additem(self, label, shortcut=None, callback=None, kind=None):
530 self.menu.AppendMenu('x') # add a dummy string
531 self.items.append(label, shortcut, callback, kind)
532 item = len(self.items)
Jack Jansene4b40381995-07-17 13:25:15 +0000533 self.menu.SetMenuItemText(item, label) # set the actual text
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000534 if shortcut:
535 self.menu.SetItemCmd(item, ord(shortcut))
536 return item
537
538 def addcheck(self, label, shortcut=None, callback=None):
539 return self.additem(label, shortcut, callback, 'check')
540
541 def addradio(self, label, shortcut=None, callback=None):
542 return self.additem(label, shortcut, callback, 'radio')
543
544 def addseparator(self):
545 self.menu.AppendMenu('(-')
546 self.items.append('', None, None, 'separator')
547
548 def addsubmenu(self, label, title=''):
549 sub = Menu(self.bar, title, -1)
550 item = self.additem(label, '\x1B', None, 'submenu')
551 self.menu.SetItemMark(item, sub.id)
552 return sub
553
554 def dispatch(self, id, item, window, event):
Jack Jansenb1667ef1996-09-26 16:17:08 +0000555 title, shortcut, callback, mtype = self.items[item-1]
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000556 if callback:
Jack Jansenb1667ef1996-09-26 16:17:08 +0000557 if not self.bar.parent or type(callback) <> types.StringType:
558 menuhandler = callback
559 else:
560 # callback is string
561 wid = Win.FrontWindow()
562 if wid and self.bar.parent._windows.has_key(wid):
563 window = self.bar.parent._windows[wid]
564 if hasattr(window, "domenu_" + callback):
565 menuhandler = getattr(window, "domenu_" + callback)
566 elif hasattr(self.bar.parent, "domenu_" + callback):
567 menuhandler = getattr(self.bar.parent, "domenu_" + callback)
568 else:
569 # nothing we can do. we shouldn't have come this far
570 # since the menu item should have been disabled...
571 return
572 elif hasattr(self.bar.parent, "domenu_" + callback):
573 menuhandler = getattr(self.bar.parent, "domenu_" + callback)
574 else:
575 # nothing we can do. we shouldn't have come this far
576 # since the menu item should have been disabled...
577 return
578 menuhandler(id, item, window, event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000579
Jack Jansencef2c591996-04-11 15:39:01 +0000580 def enable(self, onoff):
581 if onoff:
582 self.menu.EnableItem(0)
583 else:
584 self.menu.DisableItem(0)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000585
586class MenuItem:
587 def __init__(self, menu, title, shortcut=None, callback=None, kind=None):
588 self.item = menu.additem(title, shortcut, callback)
Jack Jansendb9ff361996-03-12 13:32:03 +0000589 self.menu = menu
590
591 def check(self, onoff):
592 self.menu.menu.CheckItem(self.item, onoff)
Jack Jansencef2c591996-04-11 15:39:01 +0000593
594 def enable(self, onoff):
595 if onoff:
596 self.menu.menu.EnableItem(self.item)
597 else:
598 self.menu.menu.DisableItem(self.item)
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000599
600 def settext(self, text):
601 self.menu.menu.SetMenuItemText(self.item, text)
Jack Jansendb9ff361996-03-12 13:32:03 +0000602
Jack Jansen0f6dc5b1996-04-23 16:18:33 +0000603 def setstyle(self, style):
604 self.menu.menu.SetItemStyle(self.item, style)
605
606 def seticon(self, icon):
607 self.menu.menu.SetItemIcon(self.item, icon)
608
609 def setcmd(self, cmd):
610 self.menu.menu.SetItemCmd(self.item, cmd)
611
612 def setmark(self, cmd):
613 self.menu.menu.SetItemMark(self.item, cmd)
614
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000615
616class RadioItem(MenuItem):
617 def __init__(self, menu, title, shortcut=None, callback=None):
618 MenuItem.__init__(self, menu, title, shortcut, callback, 'radio')
619
620class CheckItem(MenuItem):
621 def __init__(self, menu, title, shortcut=None, callback=None):
622 MenuItem.__init__(self, menu, title, shortcut, callback, 'check')
623
624def Separator(menu):
625 menu.addseparator()
626
627def SubMenu(menu, label, title=''):
628 return menu.addsubmenu(label, title)
629
630
631class AppleMenu(Menu):
632
633 def __init__(self, bar, abouttext="About me...", aboutcallback=None):
634 Menu.__init__(self, bar, "\024")
635 self.additem(abouttext, None, aboutcallback)
636 self.addseparator()
Jack Jansene4b40381995-07-17 13:25:15 +0000637 self.menu.AppendResMenu('DRVR')
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000638
639 def dispatch(self, id, item, window, event):
640 if item == 1:
641 Menu.dispatch(self, id, item, window, event)
642 else:
Jack Jansenc8a99491996-01-08 23:50:13 +0000643 name = self.menu.GetMenuItemText(item)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000644 OpenDeskAcc(name)
645
Jack Jansen7e0da901995-08-17 14:18:20 +0000646class Window:
647 """A single window belonging to an application"""
648
649 def __init__(self, parent):
650 self.wid = None
651 self.parent = parent
652
Jack Jansenc8a99491996-01-08 23:50:13 +0000653 def open(self, bounds=(40, 40, 400, 400), resid=None):
654 if resid <> None:
655 self.wid = GetNewWindow(resid, -1)
656 else:
657 self.wid = NewWindow(bounds, self.__class__.__name__, 1,
Jack Jansended835c1996-07-26 14:01:07 +0000658 8, -1, 1, 0) # changed to proc id 8 to include zoom box. jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000659 self.do_postopen()
660
661 def do_postopen(self):
662 """Tell our parent we exist"""
663 self.parent.appendwindow(self.wid, self)
664
665 def close(self):
Jack Jansen7e0da901995-08-17 14:18:20 +0000666 self.do_postclose()
667
668 def do_postclose(self):
669 self.parent.removewindow(self.wid)
670 self.parent = None
671 self.wid = None
Jack Jansenc8a99491996-01-08 23:50:13 +0000672
673 def SetPort(self):
674 # Convinience method
675 SetPort(self.wid)
Jack Jansen7e0da901995-08-17 14:18:20 +0000676
677 def do_inDrag(self, partcode, window, event):
678 where = event[3]
679 window.DragWindow(where, self.draglimit)
680
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000681 draglimit = screenbounds
Jack Jansen7e0da901995-08-17 14:18:20 +0000682
683 def do_inGoAway(self, partcode, window, event):
684 where = event[3]
685 if window.TrackGoAway(where):
686 self.close()
687
688 def do_inZoom(self, partcode, window, event):
689 (what, message, when, where, modifiers) = event
690 if window.TrackBox(where, partcode):
691 window.ZoomWindow(partcode, 1)
Jack Jansended835c1996-07-26 14:01:07 +0000692 rect = window.GetWindowUserState() # so that zoom really works... jvr
693 self.do_postresize(rect[2] - rect[0], rect[3] - rect[1], window) # jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000694
695 def do_inZoomIn(self, partcode, window, event):
696 SetPort(window) # !!!
697 self.do_inZoom(partcode, window, event)
698
699 def do_inZoomOut(self, partcode, window, event):
700 SetPort(window) # !!!
701 self.do_inZoom(partcode, window, event)
702
703 def do_inGrow(self, partcode, window, event):
704 (what, message, when, where, modifiers) = event
705 result = window.GrowWindow(where, self.growlimit)
706 if result:
707 height = (result>>16) & 0xffff # Hi word
708 width = result & 0xffff # Lo word
709 self.do_resize(width, height, window)
710
Jack Jansended835c1996-07-26 14:01:07 +0000711 growlimit = (50, 50, screenbounds[2] - screenbounds[0], screenbounds[3] - screenbounds[1]) # jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000712
713 def do_resize(self, width, height, window):
Jack Jansended835c1996-07-26 14:01:07 +0000714 l, t, r, b = self.wid.GetWindowPort().portRect # jvr, forGrowIcon
715 self.SetPort() # jvr
716 InvalRect((r - SCROLLBARWIDTH + 1, b - SCROLLBARWIDTH + 1, r, b)) # jvr
717 window.SizeWindow(width, height, 1) # changed updateFlag to true jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000718 self.do_postresize(width, height, window)
719
720 def do_postresize(self, width, height, window):
721 SetPort(window)
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000722 InvalRect(window.GetWindowPort().portRect)
Jack Jansen7e0da901995-08-17 14:18:20 +0000723
724 def do_inContent(self, partcode, window, event):
725 #
726 # If we're not frontmost, select ourselves and wait for
727 # the activate event.
728 #
729 if FrontWindow() <> window:
730 window.SelectWindow()
731 return
732 # We are. Handle the event.
733 (what, message, when, where, modifiers) = event
734 SetPort(window)
735 local = GlobalToLocal(where)
736 self.do_contentclick(local, modifiers, event)
737
738 def do_contentclick(self, local, modifiers, event):
Jack Jansended835c1996-07-26 14:01:07 +0000739 if DEBUG:
740 print 'Click in contents at %s, modifiers %s'%(local, modifiers)
Jack Jansen7e0da901995-08-17 14:18:20 +0000741
742 def do_rawupdate(self, window, event):
743 if DEBUG: print "raw update for", window
Jack Jansenda38f2d1995-11-14 10:15:42 +0000744 SetPort(window)
Jack Jansen7e0da901995-08-17 14:18:20 +0000745 window.BeginUpdate()
746 self.do_update(window, event)
747 window.EndUpdate()
748
749 def do_update(self, window, event):
Jack Jansended835c1996-07-26 14:01:07 +0000750 if DEBUG:
751 import time
752 for i in range(8):
753 time.sleep(0.1)
754 InvertRgn(window.GetWindowPort().visRgn)
755 FillRgn(window.GetWindowPort().visRgn, qd.gray)
756 else:
757 EraseRgn(window.GetWindowPort().visRgn)
Jack Jansen7e0da901995-08-17 14:18:20 +0000758
759 def do_activate(self, activate, event):
760 if DEBUG: print 'Activate %d for %s'%(activate, self.wid)
761
762class ControlsWindow(Window):
763
764 def do_rawupdate(self, window, event):
765 if DEBUG: print "raw update for", window
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000766 SetPort(window)
Jack Jansen7e0da901995-08-17 14:18:20 +0000767 window.BeginUpdate()
768 self.do_update(window, event)
Jack Jansended835c1996-07-26 14:01:07 +0000769 #DrawControls(window) # jvr
770 UpdateControls(window, window.GetWindowPort().visRgn) # jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000771 window.DrawGrowIcon()
772 window.EndUpdate()
773
774 def do_controlhit(self, window, control, pcode, event):
775 if DEBUG: print "control hit in", window, "on", control, "; pcode =", pcode
776
777 def do_inContent(self, partcode, window, event):
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000778 if FrontWindow() <> window:
779 window.SelectWindow()
780 return
Jack Jansen7e0da901995-08-17 14:18:20 +0000781 (what, message, when, where, modifiers) = event
Jack Jansenda38f2d1995-11-14 10:15:42 +0000782 SetPort(window) # XXXX Needed?
Jack Jansen7e0da901995-08-17 14:18:20 +0000783 local = GlobalToLocal(where)
784 ctltype, control = FindControl(local, window)
785 if ctltype and control:
786 pcode = control.TrackControl(local)
787 if pcode:
788 self.do_controlhit(window, control, pcode, event)
789 else:
790 if DEBUG: print "FindControl(%s, %s) -> (%s, %s)" % \
791 (local, window, ctltype, control)
Jack Jansene3532151996-04-12 16:24:44 +0000792 self.do_contentclick(local, modifiers, event)
793
794class ScrolledWindow(ControlsWindow):
795 def __init__(self, parent):
796 self.barx = self.bary = None
Jack Jansen7bfc8751996-04-16 14:35:43 +0000797 self.barx_enabled = self.bary_enabled = 1
798 self.activated = 1
Jack Jansene3532151996-04-12 16:24:44 +0000799 ControlsWindow.__init__(self, parent)
800
801 def scrollbars(self, wantx=1, wanty=1):
802 SetPort(self.wid)
803 self.barx = self.bary = None
Jack Jansen7bfc8751996-04-16 14:35:43 +0000804 self.barx_enabled = self.bary_enabled = 1
Jack Jansene3532151996-04-12 16:24:44 +0000805 x0, y0, x1, y1 = self.wid.GetWindowPort().portRect
806 vx, vy = self.getscrollbarvalues()
Jack Jansen7bfc8751996-04-16 14:35:43 +0000807 if vx == None: self.barx_enabled, vx = 0, 0
808 if vy == None: self.bary_enabled, vy = 0, 0
Jack Jansene3532151996-04-12 16:24:44 +0000809 if wantx:
810 rect = x0-1, y1-(SCROLLBARWIDTH-1), x1-(SCROLLBARWIDTH-2), y1+1
811 self.barx = NewControl(self.wid, rect, "", 1, vx, 0, 32767, 16, 0)
Jack Jansen7bfc8751996-04-16 14:35:43 +0000812 if not self.barx_enabled: self.barx.HiliteControl(255)
813## InvalRect(rect)
Jack Jansene3532151996-04-12 16:24:44 +0000814 if wanty:
815 rect = x1-(SCROLLBARWIDTH-1), y0-1, x1+1, y1-(SCROLLBARWIDTH-2)
816 self.bary = NewControl(self.wid, rect, "", 1, vy, 0, 32767, 16, 0)
Jack Jansen7bfc8751996-04-16 14:35:43 +0000817 if not self.bary_enabled: self.bary.HiliteControl(255)
818## InvalRect(rect)
Jack Jansene3532151996-04-12 16:24:44 +0000819
820 def do_postclose(self):
821 self.barx = self.bary = None
822 ControlsWindow.do_postclose(self)
823
824 def do_activate(self, onoff, event):
Jack Jansen7bfc8751996-04-16 14:35:43 +0000825 self.activated = onoff
Jack Jansene3532151996-04-12 16:24:44 +0000826 if onoff:
Jack Jansen7bfc8751996-04-16 14:35:43 +0000827 if self.barx and self.barx_enabled:
Jack Jansended835c1996-07-26 14:01:07 +0000828 self.barx.ShowControl() # jvr
Jack Jansen7bfc8751996-04-16 14:35:43 +0000829 if self.bary and self.bary_enabled:
Jack Jansended835c1996-07-26 14:01:07 +0000830 self.bary.ShowControl() # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000831 else:
Jack Jansen7bfc8751996-04-16 14:35:43 +0000832 if self.barx:
Jack Jansended835c1996-07-26 14:01:07 +0000833 self.barx.HideControl() # jvr; An inactive window should have *hidden*
834 # scrollbars, not just dimmed (no matter what
835 # BBEdit does... look at the Finder)
Jack Jansen7bfc8751996-04-16 14:35:43 +0000836 if self.bary:
Jack Jansended835c1996-07-26 14:01:07 +0000837 self.bary.HideControl() # jvr
838 self.wid.DrawGrowIcon() # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000839
840 def do_postresize(self, width, height, window):
841 l, t, r, b = self.wid.GetWindowPort().portRect
Jack Jansended835c1996-07-26 14:01:07 +0000842 self.SetPort()
Jack Jansene3532151996-04-12 16:24:44 +0000843 if self.barx:
Jack Jansended835c1996-07-26 14:01:07 +0000844 self.barx.HideControl() # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000845 self.barx.MoveControl(l-1, b-(SCROLLBARWIDTH-1))
Jack Jansended835c1996-07-26 14:01:07 +0000846 self.barx.SizeControl((r-l)-(SCROLLBARWIDTH-3), SCROLLBARWIDTH) # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000847 if self.bary:
Jack Jansended835c1996-07-26 14:01:07 +0000848 self.bary.HideControl() # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000849 self.bary.MoveControl(r-(SCROLLBARWIDTH-1), t-1)
Jack Jansended835c1996-07-26 14:01:07 +0000850 self.bary.SizeControl(SCROLLBARWIDTH, (b-t)-(SCROLLBARWIDTH-3)) # jvr
851 if self.barx:
852 self.barx.ShowControl() # jvr
853 ValidRect((l, b - SCROLLBARWIDTH + 1, r - SCROLLBARWIDTH + 2, b)) # jvr
854 if self.bary:
855 self.bary.ShowControl() # jvr
856 ValidRect((r - SCROLLBARWIDTH + 1, t, r, b - SCROLLBARWIDTH + 2)) # jvr
857 InvalRect((r - SCROLLBARWIDTH + 1, b - SCROLLBARWIDTH + 1, r, b)) # jvr, growicon
Jack Jansene3532151996-04-12 16:24:44 +0000858
859 def do_controlhit(self, window, control, pcode, event):
860 if control == self.barx:
861 bar = self.barx
862 which = 'x'
863 elif control == self.bary:
864 bar = self.bary
865 which = 'y'
866 else:
867 return 0
868 value = None
869 if pcode == inUpButton:
870 what = '-'
871 elif pcode == inDownButton:
872 what = '+'
873 elif pcode == inPageUp:
874 what = '--'
875 elif pcode == inPageDown:
876 what = '++'
877 else:
878 what = 'set'
879 value = bar.GetControlValue()
880 self.scrollbar_callback(which, what, value)
881 self.updatescrollbars()
882 return 1
883
884 def updatescrollbars(self):
885 SetPort(self.wid)
886 vx, vy = self.getscrollbarvalues()
887 if self.barx:
Jack Jansen7bfc8751996-04-16 14:35:43 +0000888 if vx == None:
889 self.barx.HiliteControl(255)
890 self.barx_enabled = 0
891 else:
892 if not self.barx_enabled:
893 self.barx_enabled = 1
894 if self.activated:
895 self.barx.HiliteControl(0)
896 self.barx.SetControlValue(vx)
Jack Jansene3532151996-04-12 16:24:44 +0000897 if self.bary:
Jack Jansen7bfc8751996-04-16 14:35:43 +0000898 if vy == None:
899 self.bary.HiliteControl(255)
900 self.bary_enabled = 0
901 else:
902 if not self.bary_enabled:
903 self.bary_enabled = 1
904 if self.activated:
905 self.bary.HiliteControl(0)
906 self.bary.SetControlValue(vy)
907
908 # Auxiliary function: convert standard text/image/etc coordinate
909 # to something palatable as getscrollbarvalues() return
910 def scalebarvalue(self, absmin, absmax, curmin, curmax):
911 if curmin <= absmin and curmax >= absmax:
912 return None
913 if curmin <= absmin:
914 return 0
915 if curmax >= absmax:
916 return 32767
917 perc = float(curmin-absmin)/float(absmax-absmin)
918 return int(perc*32767)
Jack Jansene3532151996-04-12 16:24:44 +0000919
920 # To be overridden:
921
922 def getscrollbarvalues(self):
923 return 0, 0
924
925 def scrollbar_callback(self, which, what, value):
926 print 'scroll', which, what, value
Jack Jansen7e0da901995-08-17 14:18:20 +0000927
928class DialogWindow(Window):
929 """A modeless dialog window"""
930
931 def open(self, resid):
932 self.wid = GetNewDialog(resid, -1)
933 self.do_postopen()
934
935 def close(self):
Jack Jansen7e0da901995-08-17 14:18:20 +0000936 self.do_postclose()
937
938 def do_itemhit(self, item, event):
939 print 'Dialog %s, item %d hit'%(self.wid, item)
940
941 def do_rawupdate(self, window, event):
942 pass
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000943
944def ostypecode(x):
945 "Convert a long int to the 4-character code it really is"
946 s = ''
947 for i in range(4):
948 x, c = divmod(x, 256)
949 s = chr(c) + s
950 return s
951
952
953class TestApp(Application):
954
955 "This class is used by the test() function"
956
957 def makeusermenus(self):
958 self.filemenu = m = Menu(self.menubar, "File")
959 self.saveitem = MenuItem(m, "Save", "S", self.save)
960 Separator(m)
961 self.optionsmenu = mm = SubMenu(m, "Options")
962 self.opt1 = CheckItem(mm, "Arguments")
963 self.opt2 = CheckItem(mm, "Being hit on the head lessons")
964 self.opt3 = CheckItem(mm, "Complaints")
965 Separator(m)
966 self.quititem = MenuItem(m, "Quit", "Q", self.quit)
967
968 def save(self, *args):
969 print "Save"
970
971 def quit(self, *args):
972 raise self
973
974
975def test():
976 "Test program"
977 app = TestApp()
978 app.mainloop()
979
980
981if __name__ == '__main__':
982 test()