blob: dc07f5e22c3a3d4c691773e2188832f67d0aef03 [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 *
26
27import EasyDialogs
28
29kHighLevelEvent = 23 # Don't know what header file this should come from
Jack Jansene3532151996-04-12 16:24:44 +000030SCROLLBARWIDTH = 16 # Again, not a clue...
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000031
32
33# Map event 'what' field to strings
34eventname = {}
35eventname[1] = 'mouseDown'
36eventname[2] = 'mouseUp'
37eventname[3] = 'keyDown'
38eventname[4] = 'keyUp'
39eventname[5] = 'autoKey'
40eventname[6] = 'updateEvt'
41eventname[7] = 'diskEvt'
42eventname[8] = 'activateEvt'
43eventname[15] = 'osEvt'
44eventname[23] = 'kHighLevelEvent'
45
46# Map part codes returned by WhichWindow() to strings
47partname = {}
48partname[0] = 'inDesk'
49partname[1] = 'inMenuBar'
50partname[2] = 'inSysWindow'
51partname[3] = 'inContent'
52partname[4] = 'inDrag'
53partname[5] = 'inGrow'
54partname[6] = 'inGoAway'
55partname[7] = 'inZoomIn'
56partname[8] = 'inZoomOut'
57
Jack Jansenc4eec9f1996-04-19 16:00:28 +000058#
59# The useable portion of the screen
Jack Jansended835c1996-07-26 14:01:07 +000060# ## but what happens with multiple screens? jvr
Jack Jansenc4eec9f1996-04-19 16:00:28 +000061screenbounds = qd.screenBits.bounds
62screenbounds = screenbounds[0]+4, screenbounds[1]+4, \
63 screenbounds[2]-4, screenbounds[3]-4
64
Jack Jansended835c1996-07-26 14:01:07 +000065next_window_x = 16 # jvr
66next_window_y = 44 # jvr
Jack Jansenc4eec9f1996-04-19 16:00:28 +000067
68def windowbounds(width, height):
69 "Return sensible window bounds"
70 global next_window_x, next_window_y
71 r, b = next_window_x+width, next_window_y+height
72 if r > screenbounds[2]:
Jack Jansended835c1996-07-26 14:01:07 +000073 next_window_x = 16
Jack Jansenc4eec9f1996-04-19 16:00:28 +000074 if b > screenbounds[3]:
Jack Jansended835c1996-07-26 14:01:07 +000075 next_window_y = 44
Jack Jansenc4eec9f1996-04-19 16:00:28 +000076 l, t = next_window_x, next_window_y
77 r, b = next_window_x+width, next_window_y+height
Jack Jansended835c1996-07-26 14:01:07 +000078 next_window_x, next_window_y = next_window_x + 8, next_window_y + 20 # jvr
Jack Jansenc4eec9f1996-04-19 16:00:28 +000079 return l, t, r, b
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000080
Jack Jansen46341301996-08-28 13:53:07 +000081_watch = None
82def setwatchcursor():
83 global _watch
84
85 if _watch == None:
86 _watch = GetCursor(4).data
87 SetCursor(_watch)
88
89def setarrowcursor():
90 SetCursor(qd.arrow)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000091
92class Application:
93
94 "Application framework -- your application should be a derived class"
95
Jack Jansen647535d1996-09-17 12:35:43 +000096 def __init__(self, nomenubar=0):
97 self.quitting = 0
Jack Jansen7e0da901995-08-17 14:18:20 +000098 self._windows = {}
Jack Jansen647535d1996-09-17 12:35:43 +000099 if nomenubar:
100 self.menubar = None
101 else:
102 self.makemenubar()
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000103
104 def makemenubar(self):
105 self.menubar = MenuBar()
106 AppleMenu(self.menubar, self.getabouttext(), self.do_about)
107 self.makeusermenus()
Jack Jansenc8a99491996-01-08 23:50:13 +0000108
109 def makeusermenus(self):
110 self.filemenu = m = Menu(self.menubar, "File")
111 self._quititem = MenuItem(m, "Quit", "Q", self._quit)
112
113 def _quit(self, *args):
Jack Jansen647535d1996-09-17 12:35:43 +0000114 self.quitting = 1
Jack Jansen7e0da901995-08-17 14:18:20 +0000115
116 def appendwindow(self, wid, window):
117 self._windows[wid] = window
118
119 def removewindow(self, wid):
120 del self._windows[wid]
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000121
122 def getabouttext(self):
123 return "About %s..." % self.__class__.__name__
124
125 def do_about(self, id, item, window, event):
126 EasyDialogs.Message("Hello, world!" + "\015(%s)" % self.__class__.__name__)
127
128 # The main event loop is broken up in several simple steps.
129 # This is done so you can override each individual part,
130 # if you have a need to do extra processing independent of the
131 # event type.
132 # Normally, however, you'd just define handlers for individual
133 # events.
134 # (XXX I'm not sure if using default parameter values is the right
135 # way to define the mask and wait time passed to WaitNextEvent.)
136
137 def mainloop(self, mask = everyEvent, wait = 0):
Jack Jansen647535d1996-09-17 12:35:43 +0000138 self.quitting = 0
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000139 saveyield = MacOS.EnableAppswitch(self.yield)
140 try:
Jack Jansen647535d1996-09-17 12:35:43 +0000141 while not self.quitting:
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000142 try:
143 self.do1event(mask, wait)
144 except (Application, SystemExit):
Jack Jansen647535d1996-09-17 12:35:43 +0000145 # Note: the raising of "self" is old-fashioned idiom to
146 # exit the mainloop. Calling _quit() is better for new
147 # applications.
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000148 break
149 finally:
150 MacOS.EnableAppswitch(saveyield)
151
152 yield = -1
153
154 def do1event(self, mask = everyEvent, wait = 0):
Jack Jansen13dc4f71995-08-31 13:38:01 +0000155 ok, event = self.getevent(mask, wait)
156 if IsDialogEvent(event):
157 if self.do_dialogevent(event):
158 return
159 if ok:
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000160 self.dispatch(event)
Jack Jansen38186781995-11-10 14:48:36 +0000161 else:
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000162 self.idle(event)
Jack Jansen38186781995-11-10 14:48:36 +0000163
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000164 def idle(self, event):
Jack Jansen38186781995-11-10 14:48:36 +0000165 pass
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000166
167 def getevent(self, mask = everyEvent, wait = 0):
168 ok, event = WaitNextEvent(mask, wait)
Jack Jansen13dc4f71995-08-31 13:38:01 +0000169 return ok, event
170
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000171 def dispatch(self, event):
172 (what, message, when, where, modifiers) = event
173 if eventname.has_key(what):
174 name = "do_" + eventname[what]
175 else:
176 name = "do_%d" % what
177 try:
178 handler = getattr(self, name)
179 except AttributeError:
180 handler = self.do_unknownevent
181 handler(event)
Jack Jansen7e0da901995-08-17 14:18:20 +0000182
183 def do_dialogevent(self, event):
184 gotone, window, item = DialogSelect(event)
185 if gotone:
186 if self._windows.has_key(window):
Jack Jansen13dc4f71995-08-31 13:38:01 +0000187 self._windows[window].do_itemhit(item, event)
Jack Jansen7e0da901995-08-17 14:18:20 +0000188 else:
189 print 'Dialog event for unknown dialog'
Jack Jansen13dc4f71995-08-31 13:38:01 +0000190 return 1
191 return 0
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000192
193 def do_mouseDown(self, event):
194 (what, message, when, where, modifiers) = event
Jack Jansen7e0da901995-08-17 14:18:20 +0000195 partcode, wid = FindWindow(where)
196
197 #
198 # Find the correct name.
199 #
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000200 if partname.has_key(partcode):
201 name = "do_" + partname[partcode]
202 else:
203 name = "do_%d" % partcode
Jack Jansen7e0da901995-08-17 14:18:20 +0000204
205 if wid == None:
206 # No window, or a non-python window
207 try:
208 handler = getattr(self, name)
209 except AttributeError:
210 # Not menubar or something, so assume someone
211 # else's window
212 MacOS.HandleEvent(event)
213 return
214 elif self._windows.has_key(wid):
215 # It is a window. Hand off to correct window.
216 window = self._windows[wid]
217 try:
218 handler = getattr(window, name)
219 except AttributeError:
220 handler = self.do_unknownpartcode
221 else:
222 # It is a python-toolbox window, but not ours.
223 handler = self.do_unknownwindow
224 handler(partcode, wid, event)
225
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000226 def do_inSysWindow(self, partcode, window, event):
Jack Jansen7a583361995-08-14 12:39:54 +0000227 MacOS.HandleEvent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000228
229 def do_inDesk(self, partcode, window, event):
Jack Jansen7a583361995-08-14 12:39:54 +0000230 MacOS.HandleEvent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000231
232 def do_inMenuBar(self, partcode, window, event):
Jack Jansen647535d1996-09-17 12:35:43 +0000233 if not self.menubar:
234 MacOS.HandleEvent(event)
235 return
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000236 (what, message, when, where, modifiers) = event
237 result = MenuSelect(where)
238 id = (result>>16) & 0xffff # Hi word
239 item = result & 0xffff # Lo word
240 self.do_rawmenu(id, item, window, event)
241
242 def do_rawmenu(self, id, item, window, event):
243 try:
244 self.do_menu(id, item, window, event)
245 finally:
246 HiliteMenu(0)
247
248 def do_menu(self, id, item, window, event):
249 self.menubar.dispatch(id, item, window, event)
250
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000251
252 def do_unknownpartcode(self, partcode, window, event):
253 (what, message, when, where, modifiers) = event
Jack Jansen7a583361995-08-14 12:39:54 +0000254 if DEBUG: print "Mouse down at global:", where
255 if DEBUG: print "\tUnknown part code:", partcode
Jack Jansen7e0da901995-08-17 14:18:20 +0000256 if DEBUG: print "\tEvent:", self.printevent(event)
257 MacOS.HandleEvent(event)
258
259 def do_unknownwindow(self, partcode, window, event):
260 if DEBUG: print 'Unknown window:', window
Jack Jansen7a583361995-08-14 12:39:54 +0000261 MacOS.HandleEvent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000262
263 def do_keyDown(self, event):
264 self.do_key(event)
265
266 def do_autoKey(self, event):
267 if not event[-1] & cmdKey:
268 self.do_key(event)
269
270 def do_key(self, event):
271 (what, message, when, where, modifiers) = event
272 c = chr(message & charCodeMask)
273 if modifiers & cmdKey:
274 if c == '.':
275 raise self
276 else:
Jack Jansen647535d1996-09-17 12:35:43 +0000277 if not self.menubar:
278 MacOS.HandleEvent(event)
279 return
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000280 result = MenuKey(ord(c))
281 id = (result>>16) & 0xffff # Hi word
282 item = result & 0xffff # Lo word
283 if id:
284 self.do_rawmenu(id, item, None, event)
Jack Jansen7e0da901995-08-17 14:18:20 +0000285# elif c == 'w':
286# w = FrontWindow()
287# if w:
288# self.do_close(w)
289# else:
290# if DEBUG: print 'Command-W without front window'
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000291 else:
Jack Jansen7a583361995-08-14 12:39:54 +0000292 if DEBUG: print "Command-" +`c`
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000293 else:
Jack Jansen7e0da901995-08-17 14:18:20 +0000294 # See whether the front window wants it
295 w = FrontWindow()
296 if w and self._windows.has_key(w):
297 window = self._windows[w]
298 try:
299 do_char = window.do_char
300 except AttributeError:
301 do_char = self.do_char
Jack Jansen6f47bf41995-12-12 15:03:35 +0000302 do_char(c, event)
303 # else it wasn't for us, sigh...
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000304
305 def do_char(self, c, event):
Jack Jansen7a583361995-08-14 12:39:54 +0000306 if DEBUG: print "Character", `c`
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000307
308 def do_updateEvt(self, event):
Jack Jansen7e0da901995-08-17 14:18:20 +0000309 (what, message, when, where, modifiers) = event
310 wid = WhichWindow(message)
311 if wid and self._windows.has_key(wid):
312 window = self._windows[wid]
313 window.do_rawupdate(wid, event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000314 else:
Jack Jansen7a583361995-08-14 12:39:54 +0000315 MacOS.HandleEvent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000316
Jack Jansen7e0da901995-08-17 14:18:20 +0000317 def do_activateEvt(self, event):
318 (what, message, when, where, modifiers) = event
Jack Jansenc8a99491996-01-08 23:50:13 +0000319 # XXXX Incorrect, should be fixed in suspendresume
320 if type(message) == type(1):
321 wid = WhichWindow(message)
322 else:
323 wid = message
Jack Jansen7e0da901995-08-17 14:18:20 +0000324 if wid and self._windows.has_key(wid):
325 window = self._windows[wid]
326 window.do_activate(modifiers & 1, event)
327 else:
328 MacOS.HandleEvent(event)
329
330 def do_osEvt(self, event):
331 (what, message, when, where, modifiers) = event
332 which = (message >> 24) & 0xff
333 if which == 1: # suspend/resume
334 self.do_suspendresume(event)
335 else:
336 if DEBUG:
337 print 'unknown osEvt:',
338 self.printevent(event)
339
340 def do_suspendresume(self, event):
341 # Is this a good idea???
342 (what, message, when, where, modifiers) = event
343 w = FrontWindow()
344 if w:
Jack Jansenc8a99491996-01-08 23:50:13 +0000345 # XXXX Incorrect, should stuff windowptr into message field
Jack Jansen7e0da901995-08-17 14:18:20 +0000346 nev = (activateEvt, w, when, where, message&1)
Jack Jansenc8a99491996-01-08 23:50:13 +0000347 self.do_activateEvt(nev)
Jack Jansen7e0da901995-08-17 14:18:20 +0000348
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000349 def do_kHighLevelEvent(self, event):
350 (what, message, when, where, modifiers) = event
Jack Jansen7a583361995-08-14 12:39:54 +0000351 if DEBUG:
352 print "High Level Event:",
353 self.printevent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000354 try:
355 AEProcessAppleEvent(event)
356 except:
357 print "AEProcessAppleEvent error:"
358 traceback.print_exc()
359
360 def do_unknownevent(self, event):
Jack Jansen7e0da901995-08-17 14:18:20 +0000361 if DEBUG:
362 print "Unhandled event:",
363 self.printevent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000364
365 def printevent(self, event):
366 (what, message, when, where, modifiers) = event
367 nicewhat = `what`
368 if eventname.has_key(what):
369 nicewhat = eventname[what]
370 print nicewhat,
371 if what == kHighLevelEvent:
372 h, v = where
373 print `ostypecode(message)`, hex(when), `ostypecode(h | (v<<16))`,
374 else:
375 print hex(message), hex(when), where,
376 print hex(modifiers)
377
378
379class MenuBar:
380 """Represent a set of menus in a menu bar.
381
382 Interface:
383
384 - (constructor)
385 - (destructor)
386 - addmenu
387 - addpopup (normally used internally)
388 - dispatch (called from Application)
389 """
390
391 nextid = 1 # Necessarily a class variable
392
393 def getnextid(self):
394 id = self.nextid
395 self.nextid = id+1
396 return id
397
398 def __init__(self):
399 ClearMenuBar()
400 self.bar = GetMenuBar()
401 self.menus = {}
402
403 def addmenu(self, title, after = 0):
404 id = self.getnextid()
Jack Jansene3532151996-04-12 16:24:44 +0000405 if DEBUG: print 'Newmenu', title, id # XXXX
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000406 m = NewMenu(id, title)
407 m.InsertMenu(after)
Jack Jansended835c1996-07-26 14:01:07 +0000408 DrawMenuBar() # XXX appears slow! better do this when we're done. jvr
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000409 return id, m
Jack Jansendb9ff361996-03-12 13:32:03 +0000410
411 def delmenu(self, id):
Jack Jansene3532151996-04-12 16:24:44 +0000412 if DEBUG: print 'Delmenu', id # XXXX
Jack Jansendb9ff361996-03-12 13:32:03 +0000413 DeleteMenu(id)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000414
415 def addpopup(self, title = ''):
416 return self.addmenu(title, -1)
417
418 def install(self):
419 self.bar.SetMenuBar()
420 DrawMenuBar()
421
422 def dispatch(self, id, item, window, event):
423 if self.menus.has_key(id):
424 self.menus[id].dispatch(id, item, window, event)
425 else:
Jack Jansen7a583361995-08-14 12:39:54 +0000426 if DEBUG: print "MenuBar.dispatch(%d, %d, %s, %s)" % \
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000427 (id, item, window, event)
428
429
430# XXX Need a way to get menus as resources and bind them to callbacks
431
432class Menu:
433 "One menu."
434
435 def __init__(self, bar, title, after=0):
436 self.bar = bar
437 self.id, self.menu = self.bar.addmenu(title, after)
438 bar.menus[self.id] = self
439 self.items = []
Jack Jansendb9ff361996-03-12 13:32:03 +0000440
441 def delete(self):
442 self.bar.delmenu(self.id)
443 del self.bar.menus[self.id]
444 del self.bar
445 del self.items
446 del self.menu
447 del self.id
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000448
449 def additem(self, label, shortcut=None, callback=None, kind=None):
450 self.menu.AppendMenu('x') # add a dummy string
451 self.items.append(label, shortcut, callback, kind)
452 item = len(self.items)
Jack Jansene4b40381995-07-17 13:25:15 +0000453 self.menu.SetMenuItemText(item, label) # set the actual text
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000454 if shortcut:
455 self.menu.SetItemCmd(item, ord(shortcut))
456 return item
457
458 def addcheck(self, label, shortcut=None, callback=None):
459 return self.additem(label, shortcut, callback, 'check')
460
461 def addradio(self, label, shortcut=None, callback=None):
462 return self.additem(label, shortcut, callback, 'radio')
463
464 def addseparator(self):
465 self.menu.AppendMenu('(-')
466 self.items.append('', None, None, 'separator')
467
468 def addsubmenu(self, label, title=''):
469 sub = Menu(self.bar, title, -1)
470 item = self.additem(label, '\x1B', None, 'submenu')
471 self.menu.SetItemMark(item, sub.id)
472 return sub
473
474 def dispatch(self, id, item, window, event):
475 title, shortcut, callback, type = self.items[item-1]
476 if callback:
477 callback(id, item, window, event)
478
Jack Jansencef2c591996-04-11 15:39:01 +0000479 def enable(self, onoff):
480 if onoff:
481 self.menu.EnableItem(0)
482 else:
483 self.menu.DisableItem(0)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000484
485class MenuItem:
486 def __init__(self, menu, title, shortcut=None, callback=None, kind=None):
487 self.item = menu.additem(title, shortcut, callback)
Jack Jansendb9ff361996-03-12 13:32:03 +0000488 self.menu = menu
489
490 def check(self, onoff):
491 self.menu.menu.CheckItem(self.item, onoff)
Jack Jansencef2c591996-04-11 15:39:01 +0000492
493 def enable(self, onoff):
494 if onoff:
495 self.menu.menu.EnableItem(self.item)
496 else:
497 self.menu.menu.DisableItem(self.item)
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000498
499 def settext(self, text):
500 self.menu.menu.SetMenuItemText(self.item, text)
Jack Jansendb9ff361996-03-12 13:32:03 +0000501
Jack Jansen0f6dc5b1996-04-23 16:18:33 +0000502 def setstyle(self, style):
503 self.menu.menu.SetItemStyle(self.item, style)
504
505 def seticon(self, icon):
506 self.menu.menu.SetItemIcon(self.item, icon)
507
508 def setcmd(self, cmd):
509 self.menu.menu.SetItemCmd(self.item, cmd)
510
511 def setmark(self, cmd):
512 self.menu.menu.SetItemMark(self.item, cmd)
513
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000514
515class RadioItem(MenuItem):
516 def __init__(self, menu, title, shortcut=None, callback=None):
517 MenuItem.__init__(self, menu, title, shortcut, callback, 'radio')
518
519class CheckItem(MenuItem):
520 def __init__(self, menu, title, shortcut=None, callback=None):
521 MenuItem.__init__(self, menu, title, shortcut, callback, 'check')
522
523def Separator(menu):
524 menu.addseparator()
525
526def SubMenu(menu, label, title=''):
527 return menu.addsubmenu(label, title)
528
529
530class AppleMenu(Menu):
531
532 def __init__(self, bar, abouttext="About me...", aboutcallback=None):
533 Menu.__init__(self, bar, "\024")
534 self.additem(abouttext, None, aboutcallback)
535 self.addseparator()
Jack Jansene4b40381995-07-17 13:25:15 +0000536 self.menu.AppendResMenu('DRVR')
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000537
538 def dispatch(self, id, item, window, event):
539 if item == 1:
540 Menu.dispatch(self, id, item, window, event)
541 else:
Jack Jansenc8a99491996-01-08 23:50:13 +0000542 name = self.menu.GetMenuItemText(item)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000543 OpenDeskAcc(name)
544
Jack Jansen7e0da901995-08-17 14:18:20 +0000545class Window:
546 """A single window belonging to an application"""
547
548 def __init__(self, parent):
549 self.wid = None
550 self.parent = parent
551
Jack Jansenc8a99491996-01-08 23:50:13 +0000552 def open(self, bounds=(40, 40, 400, 400), resid=None):
553 if resid <> None:
554 self.wid = GetNewWindow(resid, -1)
555 else:
556 self.wid = NewWindow(bounds, self.__class__.__name__, 1,
Jack Jansended835c1996-07-26 14:01:07 +0000557 8, -1, 1, 0) # changed to proc id 8 to include zoom box. jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000558 self.do_postopen()
559
560 def do_postopen(self):
561 """Tell our parent we exist"""
562 self.parent.appendwindow(self.wid, self)
563
564 def close(self):
Jack Jansen7e0da901995-08-17 14:18:20 +0000565 self.do_postclose()
566
567 def do_postclose(self):
568 self.parent.removewindow(self.wid)
569 self.parent = None
570 self.wid = None
Jack Jansenc8a99491996-01-08 23:50:13 +0000571
572 def SetPort(self):
573 # Convinience method
574 SetPort(self.wid)
Jack Jansen7e0da901995-08-17 14:18:20 +0000575
576 def do_inDrag(self, partcode, window, event):
577 where = event[3]
578 window.DragWindow(where, self.draglimit)
579
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000580 draglimit = screenbounds
Jack Jansen7e0da901995-08-17 14:18:20 +0000581
582 def do_inGoAway(self, partcode, window, event):
583 where = event[3]
584 if window.TrackGoAway(where):
585 self.close()
586
587 def do_inZoom(self, partcode, window, event):
588 (what, message, when, where, modifiers) = event
589 if window.TrackBox(where, partcode):
590 window.ZoomWindow(partcode, 1)
Jack Jansended835c1996-07-26 14:01:07 +0000591 rect = window.GetWindowUserState() # so that zoom really works... jvr
592 self.do_postresize(rect[2] - rect[0], rect[3] - rect[1], window) # jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000593
594 def do_inZoomIn(self, partcode, window, event):
595 SetPort(window) # !!!
596 self.do_inZoom(partcode, window, event)
597
598 def do_inZoomOut(self, partcode, window, event):
599 SetPort(window) # !!!
600 self.do_inZoom(partcode, window, event)
601
602 def do_inGrow(self, partcode, window, event):
603 (what, message, when, where, modifiers) = event
604 result = window.GrowWindow(where, self.growlimit)
605 if result:
606 height = (result>>16) & 0xffff # Hi word
607 width = result & 0xffff # Lo word
608 self.do_resize(width, height, window)
609
Jack Jansended835c1996-07-26 14:01:07 +0000610 growlimit = (50, 50, screenbounds[2] - screenbounds[0], screenbounds[3] - screenbounds[1]) # jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000611
612 def do_resize(self, width, height, window):
Jack Jansended835c1996-07-26 14:01:07 +0000613 l, t, r, b = self.wid.GetWindowPort().portRect # jvr, forGrowIcon
614 self.SetPort() # jvr
615 InvalRect((r - SCROLLBARWIDTH + 1, b - SCROLLBARWIDTH + 1, r, b)) # jvr
616 window.SizeWindow(width, height, 1) # changed updateFlag to true jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000617 self.do_postresize(width, height, window)
618
619 def do_postresize(self, width, height, window):
620 SetPort(window)
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000621 InvalRect(window.GetWindowPort().portRect)
Jack Jansen7e0da901995-08-17 14:18:20 +0000622
623 def do_inContent(self, partcode, window, event):
624 #
625 # If we're not frontmost, select ourselves and wait for
626 # the activate event.
627 #
628 if FrontWindow() <> window:
629 window.SelectWindow()
630 return
631 # We are. Handle the event.
632 (what, message, when, where, modifiers) = event
633 SetPort(window)
634 local = GlobalToLocal(where)
635 self.do_contentclick(local, modifiers, event)
636
637 def do_contentclick(self, local, modifiers, event):
Jack Jansended835c1996-07-26 14:01:07 +0000638 if DEBUG:
639 print 'Click in contents at %s, modifiers %s'%(local, modifiers)
Jack Jansen7e0da901995-08-17 14:18:20 +0000640
641 def do_rawupdate(self, window, event):
642 if DEBUG: print "raw update for", window
Jack Jansenda38f2d1995-11-14 10:15:42 +0000643 SetPort(window)
Jack Jansen7e0da901995-08-17 14:18:20 +0000644 window.BeginUpdate()
645 self.do_update(window, event)
646 window.EndUpdate()
647
648 def do_update(self, window, event):
Jack Jansended835c1996-07-26 14:01:07 +0000649 if DEBUG:
650 import time
651 for i in range(8):
652 time.sleep(0.1)
653 InvertRgn(window.GetWindowPort().visRgn)
654 FillRgn(window.GetWindowPort().visRgn, qd.gray)
655 else:
656 EraseRgn(window.GetWindowPort().visRgn)
Jack Jansen7e0da901995-08-17 14:18:20 +0000657
658 def do_activate(self, activate, event):
659 if DEBUG: print 'Activate %d for %s'%(activate, self.wid)
660
661class ControlsWindow(Window):
662
663 def do_rawupdate(self, window, event):
664 if DEBUG: print "raw update for", window
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000665 SetPort(window)
Jack Jansen7e0da901995-08-17 14:18:20 +0000666 window.BeginUpdate()
667 self.do_update(window, event)
Jack Jansended835c1996-07-26 14:01:07 +0000668 #DrawControls(window) # jvr
669 UpdateControls(window, window.GetWindowPort().visRgn) # jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000670 window.DrawGrowIcon()
671 window.EndUpdate()
672
673 def do_controlhit(self, window, control, pcode, event):
674 if DEBUG: print "control hit in", window, "on", control, "; pcode =", pcode
675
676 def do_inContent(self, partcode, window, event):
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000677 if FrontWindow() <> window:
678 window.SelectWindow()
679 return
Jack Jansen7e0da901995-08-17 14:18:20 +0000680 (what, message, when, where, modifiers) = event
Jack Jansenda38f2d1995-11-14 10:15:42 +0000681 SetPort(window) # XXXX Needed?
Jack Jansen7e0da901995-08-17 14:18:20 +0000682 local = GlobalToLocal(where)
683 ctltype, control = FindControl(local, window)
684 if ctltype and control:
685 pcode = control.TrackControl(local)
686 if pcode:
687 self.do_controlhit(window, control, pcode, event)
688 else:
689 if DEBUG: print "FindControl(%s, %s) -> (%s, %s)" % \
690 (local, window, ctltype, control)
Jack Jansene3532151996-04-12 16:24:44 +0000691 self.do_contentclick(local, modifiers, event)
692
693class ScrolledWindow(ControlsWindow):
694 def __init__(self, parent):
695 self.barx = self.bary = None
Jack Jansen7bfc8751996-04-16 14:35:43 +0000696 self.barx_enabled = self.bary_enabled = 1
697 self.activated = 1
Jack Jansene3532151996-04-12 16:24:44 +0000698 ControlsWindow.__init__(self, parent)
699
700 def scrollbars(self, wantx=1, wanty=1):
701 SetPort(self.wid)
702 self.barx = self.bary = None
Jack Jansen7bfc8751996-04-16 14:35:43 +0000703 self.barx_enabled = self.bary_enabled = 1
Jack Jansene3532151996-04-12 16:24:44 +0000704 x0, y0, x1, y1 = self.wid.GetWindowPort().portRect
705 vx, vy = self.getscrollbarvalues()
Jack Jansen7bfc8751996-04-16 14:35:43 +0000706 if vx == None: self.barx_enabled, vx = 0, 0
707 if vy == None: self.bary_enabled, vy = 0, 0
Jack Jansene3532151996-04-12 16:24:44 +0000708 if wantx:
709 rect = x0-1, y1-(SCROLLBARWIDTH-1), x1-(SCROLLBARWIDTH-2), y1+1
710 self.barx = NewControl(self.wid, rect, "", 1, vx, 0, 32767, 16, 0)
Jack Jansen7bfc8751996-04-16 14:35:43 +0000711 if not self.barx_enabled: self.barx.HiliteControl(255)
712## InvalRect(rect)
Jack Jansene3532151996-04-12 16:24:44 +0000713 if wanty:
714 rect = x1-(SCROLLBARWIDTH-1), y0-1, x1+1, y1-(SCROLLBARWIDTH-2)
715 self.bary = NewControl(self.wid, rect, "", 1, vy, 0, 32767, 16, 0)
Jack Jansen7bfc8751996-04-16 14:35:43 +0000716 if not self.bary_enabled: self.bary.HiliteControl(255)
717## InvalRect(rect)
Jack Jansene3532151996-04-12 16:24:44 +0000718
719 def do_postclose(self):
720 self.barx = self.bary = None
721 ControlsWindow.do_postclose(self)
722
723 def do_activate(self, onoff, event):
Jack Jansen7bfc8751996-04-16 14:35:43 +0000724 self.activated = onoff
Jack Jansene3532151996-04-12 16:24:44 +0000725 if onoff:
Jack Jansen7bfc8751996-04-16 14:35:43 +0000726 if self.barx and self.barx_enabled:
Jack Jansended835c1996-07-26 14:01:07 +0000727 self.barx.ShowControl() # jvr
Jack Jansen7bfc8751996-04-16 14:35:43 +0000728 if self.bary and self.bary_enabled:
Jack Jansended835c1996-07-26 14:01:07 +0000729 self.bary.ShowControl() # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000730 else:
Jack Jansen7bfc8751996-04-16 14:35:43 +0000731 if self.barx:
Jack Jansended835c1996-07-26 14:01:07 +0000732 self.barx.HideControl() # jvr; An inactive window should have *hidden*
733 # scrollbars, not just dimmed (no matter what
734 # BBEdit does... look at the Finder)
Jack Jansen7bfc8751996-04-16 14:35:43 +0000735 if self.bary:
Jack Jansended835c1996-07-26 14:01:07 +0000736 self.bary.HideControl() # jvr
737 self.wid.DrawGrowIcon() # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000738
739 def do_postresize(self, width, height, window):
740 l, t, r, b = self.wid.GetWindowPort().portRect
Jack Jansended835c1996-07-26 14:01:07 +0000741 self.SetPort()
Jack Jansene3532151996-04-12 16:24:44 +0000742 if self.barx:
Jack Jansended835c1996-07-26 14:01:07 +0000743 self.barx.HideControl() # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000744 self.barx.MoveControl(l-1, b-(SCROLLBARWIDTH-1))
Jack Jansended835c1996-07-26 14:01:07 +0000745 self.barx.SizeControl((r-l)-(SCROLLBARWIDTH-3), SCROLLBARWIDTH) # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000746 if self.bary:
Jack Jansended835c1996-07-26 14:01:07 +0000747 self.bary.HideControl() # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000748 self.bary.MoveControl(r-(SCROLLBARWIDTH-1), t-1)
Jack Jansended835c1996-07-26 14:01:07 +0000749 self.bary.SizeControl(SCROLLBARWIDTH, (b-t)-(SCROLLBARWIDTH-3)) # jvr
750 if self.barx:
751 self.barx.ShowControl() # jvr
752 ValidRect((l, b - SCROLLBARWIDTH + 1, r - SCROLLBARWIDTH + 2, b)) # jvr
753 if self.bary:
754 self.bary.ShowControl() # jvr
755 ValidRect((r - SCROLLBARWIDTH + 1, t, r, b - SCROLLBARWIDTH + 2)) # jvr
756 InvalRect((r - SCROLLBARWIDTH + 1, b - SCROLLBARWIDTH + 1, r, b)) # jvr, growicon
Jack Jansene3532151996-04-12 16:24:44 +0000757
758 def do_controlhit(self, window, control, pcode, event):
759 if control == self.barx:
760 bar = self.barx
761 which = 'x'
762 elif control == self.bary:
763 bar = self.bary
764 which = 'y'
765 else:
766 return 0
767 value = None
768 if pcode == inUpButton:
769 what = '-'
770 elif pcode == inDownButton:
771 what = '+'
772 elif pcode == inPageUp:
773 what = '--'
774 elif pcode == inPageDown:
775 what = '++'
776 else:
777 what = 'set'
778 value = bar.GetControlValue()
779 self.scrollbar_callback(which, what, value)
780 self.updatescrollbars()
781 return 1
782
783 def updatescrollbars(self):
784 SetPort(self.wid)
785 vx, vy = self.getscrollbarvalues()
786 if self.barx:
Jack Jansen7bfc8751996-04-16 14:35:43 +0000787 if vx == None:
788 self.barx.HiliteControl(255)
789 self.barx_enabled = 0
790 else:
791 if not self.barx_enabled:
792 self.barx_enabled = 1
793 if self.activated:
794 self.barx.HiliteControl(0)
795 self.barx.SetControlValue(vx)
Jack Jansene3532151996-04-12 16:24:44 +0000796 if self.bary:
Jack Jansen7bfc8751996-04-16 14:35:43 +0000797 if vy == None:
798 self.bary.HiliteControl(255)
799 self.bary_enabled = 0
800 else:
801 if not self.bary_enabled:
802 self.bary_enabled = 1
803 if self.activated:
804 self.bary.HiliteControl(0)
805 self.bary.SetControlValue(vy)
806
807 # Auxiliary function: convert standard text/image/etc coordinate
808 # to something palatable as getscrollbarvalues() return
809 def scalebarvalue(self, absmin, absmax, curmin, curmax):
810 if curmin <= absmin and curmax >= absmax:
811 return None
812 if curmin <= absmin:
813 return 0
814 if curmax >= absmax:
815 return 32767
816 perc = float(curmin-absmin)/float(absmax-absmin)
817 return int(perc*32767)
Jack Jansene3532151996-04-12 16:24:44 +0000818
819 # To be overridden:
820
821 def getscrollbarvalues(self):
822 return 0, 0
823
824 def scrollbar_callback(self, which, what, value):
825 print 'scroll', which, what, value
Jack Jansen7e0da901995-08-17 14:18:20 +0000826
827class DialogWindow(Window):
828 """A modeless dialog window"""
829
830 def open(self, resid):
831 self.wid = GetNewDialog(resid, -1)
832 self.do_postopen()
833
834 def close(self):
Jack Jansen7e0da901995-08-17 14:18:20 +0000835 self.do_postclose()
836
837 def do_itemhit(self, item, event):
838 print 'Dialog %s, item %d hit'%(self.wid, item)
839
840 def do_rawupdate(self, window, event):
841 pass
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000842
843def ostypecode(x):
844 "Convert a long int to the 4-character code it really is"
845 s = ''
846 for i in range(4):
847 x, c = divmod(x, 256)
848 s = chr(c) + s
849 return s
850
851
852class TestApp(Application):
853
854 "This class is used by the test() function"
855
856 def makeusermenus(self):
857 self.filemenu = m = Menu(self.menubar, "File")
858 self.saveitem = MenuItem(m, "Save", "S", self.save)
859 Separator(m)
860 self.optionsmenu = mm = SubMenu(m, "Options")
861 self.opt1 = CheckItem(mm, "Arguments")
862 self.opt2 = CheckItem(mm, "Being hit on the head lessons")
863 self.opt3 = CheckItem(mm, "Complaints")
864 Separator(m)
865 self.quititem = MenuItem(m, "Quit", "Q", self.quit)
866
867 def save(self, *args):
868 print "Save"
869
870 def quit(self, *args):
871 raise self
872
873
874def test():
875 "Test program"
876 app = TestApp()
877 app.mainloop()
878
879
880if __name__ == '__main__':
881 test()