blob: da07d139224f29ca7df36c7ec2ba5e434ba8aa87 [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):
98 self.quitting = 0
Jack Jansenb1667ef1996-09-26 16:17:08 +000099 self.needmenubarredraw = 0
Jack Jansen7e0da901995-08-17 14:18:20 +0000100 self._windows = {}
Jack Jansen647535d1996-09-17 12:35:43 +0000101 if nomenubar:
102 self.menubar = None
103 else:
104 self.makemenubar()
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000105
106 def makemenubar(self):
Jack Jansenb1667ef1996-09-26 16:17:08 +0000107 self.menubar = MenuBar(self)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000108 AppleMenu(self.menubar, self.getabouttext(), self.do_about)
109 self.makeusermenus()
Jack Jansenc8a99491996-01-08 23:50:13 +0000110
111 def makeusermenus(self):
112 self.filemenu = m = Menu(self.menubar, "File")
113 self._quititem = MenuItem(m, "Quit", "Q", self._quit)
114
115 def _quit(self, *args):
Jack Jansen647535d1996-09-17 12:35:43 +0000116 self.quitting = 1
Jack Jansen7e0da901995-08-17 14:18:20 +0000117
Jack Jansenc75e1d01996-12-23 17:22:40 +0000118 def cleanup(self):
119 for w in self._windows.values():
120 w.do_close()
121 return self._windows == {}
122
Jack Jansen7e0da901995-08-17 14:18:20 +0000123 def appendwindow(self, wid, window):
124 self._windows[wid] = window
125
126 def removewindow(self, wid):
127 del self._windows[wid]
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000128
129 def getabouttext(self):
130 return "About %s..." % self.__class__.__name__
131
132 def do_about(self, id, item, window, event):
133 EasyDialogs.Message("Hello, world!" + "\015(%s)" % self.__class__.__name__)
134
135 # The main event loop is broken up in several simple steps.
136 # This is done so you can override each individual part,
137 # if you have a need to do extra processing independent of the
138 # event type.
139 # Normally, however, you'd just define handlers for individual
140 # events.
141 # (XXX I'm not sure if using default parameter values is the right
142 # way to define the mask and wait time passed to WaitNextEvent.)
143
144 def mainloop(self, mask = everyEvent, wait = 0):
Jack Jansen647535d1996-09-17 12:35:43 +0000145 self.quitting = 0
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000146 saveyield = MacOS.EnableAppswitch(self.yield)
147 try:
Jack Jansen647535d1996-09-17 12:35:43 +0000148 while not self.quitting:
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000149 try:
150 self.do1event(mask, wait)
151 except (Application, SystemExit):
Jack Jansen647535d1996-09-17 12:35:43 +0000152 # Note: the raising of "self" is old-fashioned idiom to
153 # exit the mainloop. Calling _quit() is better for new
154 # applications.
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000155 break
156 finally:
157 MacOS.EnableAppswitch(saveyield)
158
159 yield = -1
160
161 def do1event(self, mask = everyEvent, wait = 0):
Jack Jansen13dc4f71995-08-31 13:38:01 +0000162 ok, event = self.getevent(mask, wait)
163 if IsDialogEvent(event):
164 if self.do_dialogevent(event):
165 return
166 if ok:
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000167 self.dispatch(event)
Jack Jansen38186781995-11-10 14:48:36 +0000168 else:
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000169 self.idle(event)
Jack Jansen38186781995-11-10 14:48:36 +0000170
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000171 def idle(self, event):
Jack Jansen38186781995-11-10 14:48:36 +0000172 pass
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000173
174 def getevent(self, mask = everyEvent, wait = 0):
Jack Jansenb1667ef1996-09-26 16:17:08 +0000175 if self.needmenubarredraw:
176 DrawMenuBar()
177 self.needmenubarredraw = 0
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000178 ok, event = WaitNextEvent(mask, wait)
Jack Jansen13dc4f71995-08-31 13:38:01 +0000179 return ok, event
180
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000181 def dispatch(self, event):
182 (what, message, when, where, modifiers) = event
183 if eventname.has_key(what):
184 name = "do_" + eventname[what]
185 else:
186 name = "do_%d" % what
187 try:
188 handler = getattr(self, name)
189 except AttributeError:
190 handler = self.do_unknownevent
191 handler(event)
Jack Jansen7e0da901995-08-17 14:18:20 +0000192
193 def do_dialogevent(self, event):
194 gotone, window, item = DialogSelect(event)
195 if gotone:
196 if self._windows.has_key(window):
Jack Jansen13dc4f71995-08-31 13:38:01 +0000197 self._windows[window].do_itemhit(item, event)
Jack Jansen7e0da901995-08-17 14:18:20 +0000198 else:
199 print 'Dialog event for unknown dialog'
Jack Jansen13dc4f71995-08-31 13:38:01 +0000200 return 1
201 return 0
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000202
203 def do_mouseDown(self, event):
204 (what, message, when, where, modifiers) = event
Jack Jansen7e0da901995-08-17 14:18:20 +0000205 partcode, wid = FindWindow(where)
206
207 #
208 # Find the correct name.
209 #
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000210 if partname.has_key(partcode):
211 name = "do_" + partname[partcode]
212 else:
213 name = "do_%d" % partcode
Jack Jansen7e0da901995-08-17 14:18:20 +0000214
215 if wid == None:
216 # No window, or a non-python window
217 try:
218 handler = getattr(self, name)
219 except AttributeError:
220 # Not menubar or something, so assume someone
221 # else's window
222 MacOS.HandleEvent(event)
223 return
224 elif self._windows.has_key(wid):
225 # It is a window. Hand off to correct window.
226 window = self._windows[wid]
227 try:
228 handler = getattr(window, name)
229 except AttributeError:
230 handler = self.do_unknownpartcode
231 else:
232 # It is a python-toolbox window, but not ours.
233 handler = self.do_unknownwindow
234 handler(partcode, wid, event)
235
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000236 def do_inSysWindow(self, partcode, window, event):
Jack Jansen7a583361995-08-14 12:39:54 +0000237 MacOS.HandleEvent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000238
239 def do_inDesk(self, partcode, window, event):
Jack Jansen7a583361995-08-14 12:39:54 +0000240 MacOS.HandleEvent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000241
242 def do_inMenuBar(self, partcode, window, event):
Jack Jansen647535d1996-09-17 12:35:43 +0000243 if not self.menubar:
244 MacOS.HandleEvent(event)
245 return
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000246 (what, message, when, where, modifiers) = event
247 result = MenuSelect(where)
248 id = (result>>16) & 0xffff # Hi word
249 item = result & 0xffff # Lo word
250 self.do_rawmenu(id, item, window, event)
251
252 def do_rawmenu(self, id, item, window, event):
253 try:
254 self.do_menu(id, item, window, event)
255 finally:
256 HiliteMenu(0)
257
258 def do_menu(self, id, item, window, event):
259 self.menubar.dispatch(id, item, window, event)
260
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000261
262 def do_unknownpartcode(self, partcode, window, event):
263 (what, message, when, where, modifiers) = event
Jack Jansen7a583361995-08-14 12:39:54 +0000264 if DEBUG: print "Mouse down at global:", where
265 if DEBUG: print "\tUnknown part code:", partcode
Jack Jansen7e0da901995-08-17 14:18:20 +0000266 if DEBUG: print "\tEvent:", self.printevent(event)
267 MacOS.HandleEvent(event)
268
269 def do_unknownwindow(self, partcode, window, event):
270 if DEBUG: print 'Unknown window:', window
Jack Jansen7a583361995-08-14 12:39:54 +0000271 MacOS.HandleEvent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000272
273 def do_keyDown(self, event):
274 self.do_key(event)
275
276 def do_autoKey(self, event):
277 if not event[-1] & cmdKey:
278 self.do_key(event)
279
280 def do_key(self, event):
281 (what, message, when, where, modifiers) = event
282 c = chr(message & charCodeMask)
283 if modifiers & cmdKey:
284 if c == '.':
285 raise self
286 else:
Jack Jansen647535d1996-09-17 12:35:43 +0000287 if not self.menubar:
288 MacOS.HandleEvent(event)
289 return
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000290 result = MenuKey(ord(c))
291 id = (result>>16) & 0xffff # Hi word
292 item = result & 0xffff # Lo word
293 if id:
294 self.do_rawmenu(id, item, None, event)
Jack Jansen7e0da901995-08-17 14:18:20 +0000295# elif c == 'w':
296# w = FrontWindow()
297# if w:
298# self.do_close(w)
299# else:
300# if DEBUG: print 'Command-W without front window'
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000301 else:
Jack Jansen7a583361995-08-14 12:39:54 +0000302 if DEBUG: print "Command-" +`c`
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000303 else:
Jack Jansen7e0da901995-08-17 14:18:20 +0000304 # See whether the front window wants it
305 w = FrontWindow()
306 if w and self._windows.has_key(w):
307 window = self._windows[w]
308 try:
309 do_char = window.do_char
310 except AttributeError:
311 do_char = self.do_char
Jack Jansen6f47bf41995-12-12 15:03:35 +0000312 do_char(c, event)
313 # else it wasn't for us, sigh...
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000314
315 def do_char(self, c, event):
Jack Jansen7a583361995-08-14 12:39:54 +0000316 if DEBUG: print "Character", `c`
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000317
318 def do_updateEvt(self, event):
Jack Jansen7e0da901995-08-17 14:18:20 +0000319 (what, message, when, where, modifiers) = event
320 wid = WhichWindow(message)
321 if wid and self._windows.has_key(wid):
322 window = self._windows[wid]
323 window.do_rawupdate(wid, event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000324 else:
Jack Jansen7a583361995-08-14 12:39:54 +0000325 MacOS.HandleEvent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000326
Jack Jansen7e0da901995-08-17 14:18:20 +0000327 def do_activateEvt(self, event):
328 (what, message, when, where, modifiers) = event
Jack Jansenc8a99491996-01-08 23:50:13 +0000329 # XXXX Incorrect, should be fixed in suspendresume
330 if type(message) == type(1):
331 wid = WhichWindow(message)
332 else:
333 wid = message
Jack Jansen7e0da901995-08-17 14:18:20 +0000334 if wid and self._windows.has_key(wid):
335 window = self._windows[wid]
336 window.do_activate(modifiers & 1, event)
337 else:
338 MacOS.HandleEvent(event)
339
340 def do_osEvt(self, event):
341 (what, message, when, where, modifiers) = event
342 which = (message >> 24) & 0xff
343 if which == 1: # suspend/resume
344 self.do_suspendresume(event)
345 else:
346 if DEBUG:
347 print 'unknown osEvt:',
348 self.printevent(event)
349
350 def do_suspendresume(self, event):
351 # Is this a good idea???
352 (what, message, when, where, modifiers) = event
353 w = FrontWindow()
354 if w:
Jack Jansenc8a99491996-01-08 23:50:13 +0000355 # XXXX Incorrect, should stuff windowptr into message field
Jack Jansen7e0da901995-08-17 14:18:20 +0000356 nev = (activateEvt, w, when, where, message&1)
Jack Jansenc8a99491996-01-08 23:50:13 +0000357 self.do_activateEvt(nev)
Jack Jansen7e0da901995-08-17 14:18:20 +0000358
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000359 def do_kHighLevelEvent(self, event):
360 (what, message, when, where, modifiers) = event
Jack Jansen7a583361995-08-14 12:39:54 +0000361 if DEBUG:
362 print "High Level Event:",
363 self.printevent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000364 try:
365 AEProcessAppleEvent(event)
366 except:
367 print "AEProcessAppleEvent error:"
368 traceback.print_exc()
369
370 def do_unknownevent(self, event):
Jack Jansen7e0da901995-08-17 14:18:20 +0000371 if DEBUG:
372 print "Unhandled event:",
373 self.printevent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000374
375 def printevent(self, event):
376 (what, message, when, where, modifiers) = event
377 nicewhat = `what`
378 if eventname.has_key(what):
379 nicewhat = eventname[what]
380 print nicewhat,
381 if what == kHighLevelEvent:
382 h, v = where
383 print `ostypecode(message)`, hex(when), `ostypecode(h | (v<<16))`,
384 else:
385 print hex(message), hex(when), where,
386 print hex(modifiers)
387
388
389class MenuBar:
390 """Represent a set of menus in a menu bar.
391
392 Interface:
393
394 - (constructor)
395 - (destructor)
396 - addmenu
397 - addpopup (normally used internally)
398 - dispatch (called from Application)
399 """
400
401 nextid = 1 # Necessarily a class variable
402
403 def getnextid(self):
Jack Jansenb1667ef1996-09-26 16:17:08 +0000404 id = MenuBar.nextid
405 MenuBar.nextid = id+1
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000406 return id
407
Jack Jansenb1667ef1996-09-26 16:17:08 +0000408 def __init__(self, parent=None):
409 self.parent = parent
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000410 ClearMenuBar()
411 self.bar = GetMenuBar()
412 self.menus = {}
413
Jack Jansenb1667ef1996-09-26 16:17:08 +0000414 # XXX necessary?
415 def close(self):
416 self.parent = None
417 self.bar = None
418
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000419 def addmenu(self, title, after = 0):
420 id = self.getnextid()
Jack Jansene3532151996-04-12 16:24:44 +0000421 if DEBUG: print 'Newmenu', title, id # XXXX
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000422 m = NewMenu(id, title)
423 m.InsertMenu(after)
Jack Jansenb1667ef1996-09-26 16:17:08 +0000424 if self.parent:
425 self.parent.needmenubarredraw = 1
426 else:
427 DrawMenuBar()
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000428 return id, m
Jack Jansendb9ff361996-03-12 13:32:03 +0000429
430 def delmenu(self, id):
Jack Jansene3532151996-04-12 16:24:44 +0000431 if DEBUG: print 'Delmenu', id # XXXX
Jack Jansendb9ff361996-03-12 13:32:03 +0000432 DeleteMenu(id)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000433
434 def addpopup(self, title = ''):
435 return self.addmenu(title, -1)
Jack Jansenb1667ef1996-09-26 16:17:08 +0000436
437# Useless:
438# def install(self):
439# if not self.bar: return
440# SetMenuBar(self.bar)
441# if self.parent:
442# self.parent.needmenubarredraw = 1
443# else:
444# DrawMenuBar()
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000445
Jack Jansenb1667ef1996-09-26 16:17:08 +0000446 def fixmenudimstate(self):
447 for m in self.menus.keys():
448 menu = self.menus[m]
449 if menu.__class__ == FrameWork.AppleMenu:
450 continue
451 for i in range(len(menu.items)):
452 label, shortcut, callback, kind = menu.items[i]
453 if type(callback) == types.StringType:
454 wid = Win.FrontWindow()
455 if wid and self.parent._windows.has_key(wid):
456 window = self.parent._windows[wid]
457 if hasattr(window, "domenu_" + callback):
458 menu.menu.EnableItem(i + 1)
459 elif hasattr(self.parent, "domenu_" + callback):
460 menu.menu.EnableItem(i + 1)
461 else:
462 menu.menu.DisableItem(i + 1)
463 elif hasattr(self.parent, "domenu_" + callback):
464 menu.menu.EnableItem(i + 1)
465 else:
466 menu.menu.DisableItem(i + 1)
467 elif callback:
468 pass
469
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000470 def dispatch(self, id, item, window, event):
471 if self.menus.has_key(id):
472 self.menus[id].dispatch(id, item, window, event)
473 else:
Jack Jansen7a583361995-08-14 12:39:54 +0000474 if DEBUG: print "MenuBar.dispatch(%d, %d, %s, %s)" % \
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000475 (id, item, window, event)
476
477
478# XXX Need a way to get menus as resources and bind them to callbacks
479
480class Menu:
481 "One menu."
482
483 def __init__(self, bar, title, after=0):
484 self.bar = bar
485 self.id, self.menu = self.bar.addmenu(title, after)
486 bar.menus[self.id] = self
487 self.items = []
Jack Jansendb9ff361996-03-12 13:32:03 +0000488
489 def delete(self):
490 self.bar.delmenu(self.id)
491 del self.bar.menus[self.id]
492 del self.bar
493 del self.items
494 del self.menu
495 del self.id
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000496
497 def additem(self, label, shortcut=None, callback=None, kind=None):
498 self.menu.AppendMenu('x') # add a dummy string
499 self.items.append(label, shortcut, callback, kind)
500 item = len(self.items)
Jack Jansene4b40381995-07-17 13:25:15 +0000501 self.menu.SetMenuItemText(item, label) # set the actual text
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000502 if shortcut:
503 self.menu.SetItemCmd(item, ord(shortcut))
504 return item
505
506 def addcheck(self, label, shortcut=None, callback=None):
507 return self.additem(label, shortcut, callback, 'check')
508
509 def addradio(self, label, shortcut=None, callback=None):
510 return self.additem(label, shortcut, callback, 'radio')
511
512 def addseparator(self):
513 self.menu.AppendMenu('(-')
514 self.items.append('', None, None, 'separator')
515
516 def addsubmenu(self, label, title=''):
517 sub = Menu(self.bar, title, -1)
518 item = self.additem(label, '\x1B', None, 'submenu')
519 self.menu.SetItemMark(item, sub.id)
520 return sub
521
522 def dispatch(self, id, item, window, event):
Jack Jansenb1667ef1996-09-26 16:17:08 +0000523 title, shortcut, callback, mtype = self.items[item-1]
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000524 if callback:
Jack Jansenb1667ef1996-09-26 16:17:08 +0000525 if not self.bar.parent or type(callback) <> types.StringType:
526 menuhandler = callback
527 else:
528 # callback is string
529 wid = Win.FrontWindow()
530 if wid and self.bar.parent._windows.has_key(wid):
531 window = self.bar.parent._windows[wid]
532 if hasattr(window, "domenu_" + callback):
533 menuhandler = getattr(window, "domenu_" + callback)
534 elif hasattr(self.bar.parent, "domenu_" + callback):
535 menuhandler = getattr(self.bar.parent, "domenu_" + callback)
536 else:
537 # nothing we can do. we shouldn't have come this far
538 # since the menu item should have been disabled...
539 return
540 elif hasattr(self.bar.parent, "domenu_" + callback):
541 menuhandler = getattr(self.bar.parent, "domenu_" + callback)
542 else:
543 # nothing we can do. we shouldn't have come this far
544 # since the menu item should have been disabled...
545 return
546 menuhandler(id, item, window, event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000547
Jack Jansencef2c591996-04-11 15:39:01 +0000548 def enable(self, onoff):
549 if onoff:
550 self.menu.EnableItem(0)
551 else:
552 self.menu.DisableItem(0)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000553
554class MenuItem:
555 def __init__(self, menu, title, shortcut=None, callback=None, kind=None):
556 self.item = menu.additem(title, shortcut, callback)
Jack Jansendb9ff361996-03-12 13:32:03 +0000557 self.menu = menu
558
559 def check(self, onoff):
560 self.menu.menu.CheckItem(self.item, onoff)
Jack Jansencef2c591996-04-11 15:39:01 +0000561
562 def enable(self, onoff):
563 if onoff:
564 self.menu.menu.EnableItem(self.item)
565 else:
566 self.menu.menu.DisableItem(self.item)
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000567
568 def settext(self, text):
569 self.menu.menu.SetMenuItemText(self.item, text)
Jack Jansendb9ff361996-03-12 13:32:03 +0000570
Jack Jansen0f6dc5b1996-04-23 16:18:33 +0000571 def setstyle(self, style):
572 self.menu.menu.SetItemStyle(self.item, style)
573
574 def seticon(self, icon):
575 self.menu.menu.SetItemIcon(self.item, icon)
576
577 def setcmd(self, cmd):
578 self.menu.menu.SetItemCmd(self.item, cmd)
579
580 def setmark(self, cmd):
581 self.menu.menu.SetItemMark(self.item, cmd)
582
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000583
584class RadioItem(MenuItem):
585 def __init__(self, menu, title, shortcut=None, callback=None):
586 MenuItem.__init__(self, menu, title, shortcut, callback, 'radio')
587
588class CheckItem(MenuItem):
589 def __init__(self, menu, title, shortcut=None, callback=None):
590 MenuItem.__init__(self, menu, title, shortcut, callback, 'check')
591
592def Separator(menu):
593 menu.addseparator()
594
595def SubMenu(menu, label, title=''):
596 return menu.addsubmenu(label, title)
597
598
599class AppleMenu(Menu):
600
601 def __init__(self, bar, abouttext="About me...", aboutcallback=None):
602 Menu.__init__(self, bar, "\024")
603 self.additem(abouttext, None, aboutcallback)
604 self.addseparator()
Jack Jansene4b40381995-07-17 13:25:15 +0000605 self.menu.AppendResMenu('DRVR')
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000606
607 def dispatch(self, id, item, window, event):
608 if item == 1:
609 Menu.dispatch(self, id, item, window, event)
610 else:
Jack Jansenc8a99491996-01-08 23:50:13 +0000611 name = self.menu.GetMenuItemText(item)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000612 OpenDeskAcc(name)
613
Jack Jansen7e0da901995-08-17 14:18:20 +0000614class Window:
615 """A single window belonging to an application"""
616
617 def __init__(self, parent):
618 self.wid = None
619 self.parent = parent
620
Jack Jansenc8a99491996-01-08 23:50:13 +0000621 def open(self, bounds=(40, 40, 400, 400), resid=None):
622 if resid <> None:
623 self.wid = GetNewWindow(resid, -1)
624 else:
625 self.wid = NewWindow(bounds, self.__class__.__name__, 1,
Jack Jansended835c1996-07-26 14:01:07 +0000626 8, -1, 1, 0) # changed to proc id 8 to include zoom box. jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000627 self.do_postopen()
628
629 def do_postopen(self):
630 """Tell our parent we exist"""
631 self.parent.appendwindow(self.wid, self)
632
633 def close(self):
Jack Jansen7e0da901995-08-17 14:18:20 +0000634 self.do_postclose()
635
636 def do_postclose(self):
637 self.parent.removewindow(self.wid)
638 self.parent = None
639 self.wid = None
Jack Jansenc8a99491996-01-08 23:50:13 +0000640
641 def SetPort(self):
642 # Convinience method
643 SetPort(self.wid)
Jack Jansen7e0da901995-08-17 14:18:20 +0000644
645 def do_inDrag(self, partcode, window, event):
646 where = event[3]
647 window.DragWindow(where, self.draglimit)
648
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000649 draglimit = screenbounds
Jack Jansen7e0da901995-08-17 14:18:20 +0000650
651 def do_inGoAway(self, partcode, window, event):
652 where = event[3]
653 if window.TrackGoAway(where):
654 self.close()
655
656 def do_inZoom(self, partcode, window, event):
657 (what, message, when, where, modifiers) = event
658 if window.TrackBox(where, partcode):
659 window.ZoomWindow(partcode, 1)
Jack Jansended835c1996-07-26 14:01:07 +0000660 rect = window.GetWindowUserState() # so that zoom really works... jvr
661 self.do_postresize(rect[2] - rect[0], rect[3] - rect[1], window) # jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000662
663 def do_inZoomIn(self, partcode, window, event):
664 SetPort(window) # !!!
665 self.do_inZoom(partcode, window, event)
666
667 def do_inZoomOut(self, partcode, window, event):
668 SetPort(window) # !!!
669 self.do_inZoom(partcode, window, event)
670
671 def do_inGrow(self, partcode, window, event):
672 (what, message, when, where, modifiers) = event
673 result = window.GrowWindow(where, self.growlimit)
674 if result:
675 height = (result>>16) & 0xffff # Hi word
676 width = result & 0xffff # Lo word
677 self.do_resize(width, height, window)
678
Jack Jansended835c1996-07-26 14:01:07 +0000679 growlimit = (50, 50, screenbounds[2] - screenbounds[0], screenbounds[3] - screenbounds[1]) # jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000680
681 def do_resize(self, width, height, window):
Jack Jansended835c1996-07-26 14:01:07 +0000682 l, t, r, b = self.wid.GetWindowPort().portRect # jvr, forGrowIcon
683 self.SetPort() # jvr
684 InvalRect((r - SCROLLBARWIDTH + 1, b - SCROLLBARWIDTH + 1, r, b)) # jvr
685 window.SizeWindow(width, height, 1) # changed updateFlag to true jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000686 self.do_postresize(width, height, window)
687
688 def do_postresize(self, width, height, window):
689 SetPort(window)
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000690 InvalRect(window.GetWindowPort().portRect)
Jack Jansen7e0da901995-08-17 14:18:20 +0000691
692 def do_inContent(self, partcode, window, event):
693 #
694 # If we're not frontmost, select ourselves and wait for
695 # the activate event.
696 #
697 if FrontWindow() <> window:
698 window.SelectWindow()
699 return
700 # We are. Handle the event.
701 (what, message, when, where, modifiers) = event
702 SetPort(window)
703 local = GlobalToLocal(where)
704 self.do_contentclick(local, modifiers, event)
705
706 def do_contentclick(self, local, modifiers, event):
Jack Jansended835c1996-07-26 14:01:07 +0000707 if DEBUG:
708 print 'Click in contents at %s, modifiers %s'%(local, modifiers)
Jack Jansen7e0da901995-08-17 14:18:20 +0000709
710 def do_rawupdate(self, window, event):
711 if DEBUG: print "raw update for", window
Jack Jansenda38f2d1995-11-14 10:15:42 +0000712 SetPort(window)
Jack Jansen7e0da901995-08-17 14:18:20 +0000713 window.BeginUpdate()
714 self.do_update(window, event)
715 window.EndUpdate()
716
717 def do_update(self, window, event):
Jack Jansended835c1996-07-26 14:01:07 +0000718 if DEBUG:
719 import time
720 for i in range(8):
721 time.sleep(0.1)
722 InvertRgn(window.GetWindowPort().visRgn)
723 FillRgn(window.GetWindowPort().visRgn, qd.gray)
724 else:
725 EraseRgn(window.GetWindowPort().visRgn)
Jack Jansen7e0da901995-08-17 14:18:20 +0000726
727 def do_activate(self, activate, event):
728 if DEBUG: print 'Activate %d for %s'%(activate, self.wid)
729
730class ControlsWindow(Window):
731
732 def do_rawupdate(self, window, event):
733 if DEBUG: print "raw update for", window
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000734 SetPort(window)
Jack Jansen7e0da901995-08-17 14:18:20 +0000735 window.BeginUpdate()
736 self.do_update(window, event)
Jack Jansended835c1996-07-26 14:01:07 +0000737 #DrawControls(window) # jvr
738 UpdateControls(window, window.GetWindowPort().visRgn) # jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000739 window.DrawGrowIcon()
740 window.EndUpdate()
741
742 def do_controlhit(self, window, control, pcode, event):
743 if DEBUG: print "control hit in", window, "on", control, "; pcode =", pcode
744
745 def do_inContent(self, partcode, window, event):
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000746 if FrontWindow() <> window:
747 window.SelectWindow()
748 return
Jack Jansen7e0da901995-08-17 14:18:20 +0000749 (what, message, when, where, modifiers) = event
Jack Jansenda38f2d1995-11-14 10:15:42 +0000750 SetPort(window) # XXXX Needed?
Jack Jansen7e0da901995-08-17 14:18:20 +0000751 local = GlobalToLocal(where)
752 ctltype, control = FindControl(local, window)
753 if ctltype and control:
754 pcode = control.TrackControl(local)
755 if pcode:
756 self.do_controlhit(window, control, pcode, event)
757 else:
758 if DEBUG: print "FindControl(%s, %s) -> (%s, %s)" % \
759 (local, window, ctltype, control)
Jack Jansene3532151996-04-12 16:24:44 +0000760 self.do_contentclick(local, modifiers, event)
761
762class ScrolledWindow(ControlsWindow):
763 def __init__(self, parent):
764 self.barx = self.bary = None
Jack Jansen7bfc8751996-04-16 14:35:43 +0000765 self.barx_enabled = self.bary_enabled = 1
766 self.activated = 1
Jack Jansene3532151996-04-12 16:24:44 +0000767 ControlsWindow.__init__(self, parent)
768
769 def scrollbars(self, wantx=1, wanty=1):
770 SetPort(self.wid)
771 self.barx = self.bary = None
Jack Jansen7bfc8751996-04-16 14:35:43 +0000772 self.barx_enabled = self.bary_enabled = 1
Jack Jansene3532151996-04-12 16:24:44 +0000773 x0, y0, x1, y1 = self.wid.GetWindowPort().portRect
774 vx, vy = self.getscrollbarvalues()
Jack Jansen7bfc8751996-04-16 14:35:43 +0000775 if vx == None: self.barx_enabled, vx = 0, 0
776 if vy == None: self.bary_enabled, vy = 0, 0
Jack Jansene3532151996-04-12 16:24:44 +0000777 if wantx:
778 rect = x0-1, y1-(SCROLLBARWIDTH-1), x1-(SCROLLBARWIDTH-2), y1+1
779 self.barx = NewControl(self.wid, rect, "", 1, vx, 0, 32767, 16, 0)
Jack Jansen7bfc8751996-04-16 14:35:43 +0000780 if not self.barx_enabled: self.barx.HiliteControl(255)
781## InvalRect(rect)
Jack Jansene3532151996-04-12 16:24:44 +0000782 if wanty:
783 rect = x1-(SCROLLBARWIDTH-1), y0-1, x1+1, y1-(SCROLLBARWIDTH-2)
784 self.bary = NewControl(self.wid, rect, "", 1, vy, 0, 32767, 16, 0)
Jack Jansen7bfc8751996-04-16 14:35:43 +0000785 if not self.bary_enabled: self.bary.HiliteControl(255)
786## InvalRect(rect)
Jack Jansene3532151996-04-12 16:24:44 +0000787
788 def do_postclose(self):
789 self.barx = self.bary = None
790 ControlsWindow.do_postclose(self)
791
792 def do_activate(self, onoff, event):
Jack Jansen7bfc8751996-04-16 14:35:43 +0000793 self.activated = onoff
Jack Jansene3532151996-04-12 16:24:44 +0000794 if onoff:
Jack Jansen7bfc8751996-04-16 14:35:43 +0000795 if self.barx and self.barx_enabled:
Jack Jansended835c1996-07-26 14:01:07 +0000796 self.barx.ShowControl() # jvr
Jack Jansen7bfc8751996-04-16 14:35:43 +0000797 if self.bary and self.bary_enabled:
Jack Jansended835c1996-07-26 14:01:07 +0000798 self.bary.ShowControl() # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000799 else:
Jack Jansen7bfc8751996-04-16 14:35:43 +0000800 if self.barx:
Jack Jansended835c1996-07-26 14:01:07 +0000801 self.barx.HideControl() # jvr; An inactive window should have *hidden*
802 # scrollbars, not just dimmed (no matter what
803 # BBEdit does... look at the Finder)
Jack Jansen7bfc8751996-04-16 14:35:43 +0000804 if self.bary:
Jack Jansended835c1996-07-26 14:01:07 +0000805 self.bary.HideControl() # jvr
806 self.wid.DrawGrowIcon() # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000807
808 def do_postresize(self, width, height, window):
809 l, t, r, b = self.wid.GetWindowPort().portRect
Jack Jansended835c1996-07-26 14:01:07 +0000810 self.SetPort()
Jack Jansene3532151996-04-12 16:24:44 +0000811 if self.barx:
Jack Jansended835c1996-07-26 14:01:07 +0000812 self.barx.HideControl() # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000813 self.barx.MoveControl(l-1, b-(SCROLLBARWIDTH-1))
Jack Jansended835c1996-07-26 14:01:07 +0000814 self.barx.SizeControl((r-l)-(SCROLLBARWIDTH-3), SCROLLBARWIDTH) # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000815 if self.bary:
Jack Jansended835c1996-07-26 14:01:07 +0000816 self.bary.HideControl() # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000817 self.bary.MoveControl(r-(SCROLLBARWIDTH-1), t-1)
Jack Jansended835c1996-07-26 14:01:07 +0000818 self.bary.SizeControl(SCROLLBARWIDTH, (b-t)-(SCROLLBARWIDTH-3)) # jvr
819 if self.barx:
820 self.barx.ShowControl() # jvr
821 ValidRect((l, b - SCROLLBARWIDTH + 1, r - SCROLLBARWIDTH + 2, b)) # jvr
822 if self.bary:
823 self.bary.ShowControl() # jvr
824 ValidRect((r - SCROLLBARWIDTH + 1, t, r, b - SCROLLBARWIDTH + 2)) # jvr
825 InvalRect((r - SCROLLBARWIDTH + 1, b - SCROLLBARWIDTH + 1, r, b)) # jvr, growicon
Jack Jansene3532151996-04-12 16:24:44 +0000826
827 def do_controlhit(self, window, control, pcode, event):
828 if control == self.barx:
829 bar = self.barx
830 which = 'x'
831 elif control == self.bary:
832 bar = self.bary
833 which = 'y'
834 else:
835 return 0
836 value = None
837 if pcode == inUpButton:
838 what = '-'
839 elif pcode == inDownButton:
840 what = '+'
841 elif pcode == inPageUp:
842 what = '--'
843 elif pcode == inPageDown:
844 what = '++'
845 else:
846 what = 'set'
847 value = bar.GetControlValue()
848 self.scrollbar_callback(which, what, value)
849 self.updatescrollbars()
850 return 1
851
852 def updatescrollbars(self):
853 SetPort(self.wid)
854 vx, vy = self.getscrollbarvalues()
855 if self.barx:
Jack Jansen7bfc8751996-04-16 14:35:43 +0000856 if vx == None:
857 self.barx.HiliteControl(255)
858 self.barx_enabled = 0
859 else:
860 if not self.barx_enabled:
861 self.barx_enabled = 1
862 if self.activated:
863 self.barx.HiliteControl(0)
864 self.barx.SetControlValue(vx)
Jack Jansene3532151996-04-12 16:24:44 +0000865 if self.bary:
Jack Jansen7bfc8751996-04-16 14:35:43 +0000866 if vy == None:
867 self.bary.HiliteControl(255)
868 self.bary_enabled = 0
869 else:
870 if not self.bary_enabled:
871 self.bary_enabled = 1
872 if self.activated:
873 self.bary.HiliteControl(0)
874 self.bary.SetControlValue(vy)
875
876 # Auxiliary function: convert standard text/image/etc coordinate
877 # to something palatable as getscrollbarvalues() return
878 def scalebarvalue(self, absmin, absmax, curmin, curmax):
879 if curmin <= absmin and curmax >= absmax:
880 return None
881 if curmin <= absmin:
882 return 0
883 if curmax >= absmax:
884 return 32767
885 perc = float(curmin-absmin)/float(absmax-absmin)
886 return int(perc*32767)
Jack Jansene3532151996-04-12 16:24:44 +0000887
888 # To be overridden:
889
890 def getscrollbarvalues(self):
891 return 0, 0
892
893 def scrollbar_callback(self, which, what, value):
894 print 'scroll', which, what, value
Jack Jansen7e0da901995-08-17 14:18:20 +0000895
896class DialogWindow(Window):
897 """A modeless dialog window"""
898
899 def open(self, resid):
900 self.wid = GetNewDialog(resid, -1)
901 self.do_postopen()
902
903 def close(self):
Jack Jansen7e0da901995-08-17 14:18:20 +0000904 self.do_postclose()
905
906 def do_itemhit(self, item, event):
907 print 'Dialog %s, item %d hit'%(self.wid, item)
908
909 def do_rawupdate(self, window, event):
910 pass
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000911
912def ostypecode(x):
913 "Convert a long int to the 4-character code it really is"
914 s = ''
915 for i in range(4):
916 x, c = divmod(x, 256)
917 s = chr(c) + s
918 return s
919
920
921class TestApp(Application):
922
923 "This class is used by the test() function"
924
925 def makeusermenus(self):
926 self.filemenu = m = Menu(self.menubar, "File")
927 self.saveitem = MenuItem(m, "Save", "S", self.save)
928 Separator(m)
929 self.optionsmenu = mm = SubMenu(m, "Options")
930 self.opt1 = CheckItem(mm, "Arguments")
931 self.opt2 = CheckItem(mm, "Being hit on the head lessons")
932 self.opt3 = CheckItem(mm, "Complaints")
933 Separator(m)
934 self.quititem = MenuItem(m, "Quit", "Q", self.quit)
935
936 def save(self, *args):
937 print "Save"
938
939 def quit(self, *args):
940 raise self
941
942
943def test():
944 "Test program"
945 app = TestApp()
946 app.mainloop()
947
948
949if __name__ == '__main__':
950 test()