blob: dc762cfdd4e513b704714388dd6260bfcdcf9b5d [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
Jack Jansenf2bd9ee2000-10-12 21:25:37 +0000150 schedparams = (0, 0) # By default disable Python's event handling
151
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000152 def mainloop(self, mask = everyEvent, wait = 0):
Jack Jansen647535d1996-09-17 12:35:43 +0000153 self.quitting = 0
Jack Jansen3368cb71997-06-12 10:51:18 +0000154 saveparams = apply(MacOS.SchedParams, self.schedparams)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000155 try:
Jack Jansen647535d1996-09-17 12:35:43 +0000156 while not self.quitting:
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000157 try:
158 self.do1event(mask, wait)
159 except (Application, SystemExit):
Jack Jansen647535d1996-09-17 12:35:43 +0000160 # Note: the raising of "self" is old-fashioned idiom to
161 # exit the mainloop. Calling _quit() is better for new
162 # applications.
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000163 break
164 finally:
Jack Jansen0c3e4b61999-12-03 16:08:50 +0000165 apply(MacOS.SchedParams, saveparams)
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):
Jack Jansenfd9925a2000-10-19 20:31:51 +0000290 MacOS.OutputSeen()
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000291 self.menubar.dispatch(id, item, window, event)
292
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000293
294 def do_unknownpartcode(self, partcode, window, event):
295 (what, message, when, where, modifiers) = event
Jack Jansen7a583361995-08-14 12:39:54 +0000296 if DEBUG: print "Mouse down at global:", where
297 if DEBUG: print "\tUnknown part code:", partcode
Jack Jansen7e0da901995-08-17 14:18:20 +0000298 if DEBUG: print "\tEvent:", self.printevent(event)
299 MacOS.HandleEvent(event)
300
301 def do_unknownwindow(self, partcode, window, event):
302 if DEBUG: print 'Unknown window:', window
Jack Jansen7a583361995-08-14 12:39:54 +0000303 MacOS.HandleEvent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000304
305 def do_keyDown(self, event):
306 self.do_key(event)
307
308 def do_autoKey(self, event):
309 if not event[-1] & cmdKey:
310 self.do_key(event)
311
312 def do_key(self, event):
313 (what, message, when, where, modifiers) = event
314 c = chr(message & charCodeMask)
Jack Jansenc15e43a1999-12-15 15:45:23 +0000315 if self.menubar:
316 result = MenuEvent(event)
317 id = (result>>16) & 0xffff # Hi word
318 item = result & 0xffff # Lo word
319 if id:
320 self.do_rawmenu(id, item, None, event)
321 return
322 # Otherwise we fall-through
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000323 if modifiers & cmdKey:
324 if c == '.':
325 raise self
326 else:
Jack Jansen647535d1996-09-17 12:35:43 +0000327 if not self.menubar:
328 MacOS.HandleEvent(event)
Jack Jansenc15e43a1999-12-15 15:45:23 +0000329 return
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000330 else:
Jack Jansen7e0da901995-08-17 14:18:20 +0000331 # See whether the front window wants it
332 w = FrontWindow()
333 if w and self._windows.has_key(w):
334 window = self._windows[w]
335 try:
336 do_char = window.do_char
337 except AttributeError:
338 do_char = self.do_char
Jack Jansen6f47bf41995-12-12 15:03:35 +0000339 do_char(c, event)
340 # else it wasn't for us, sigh...
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000341
342 def do_char(self, c, event):
Jack Jansen7a583361995-08-14 12:39:54 +0000343 if DEBUG: print "Character", `c`
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000344
345 def do_updateEvt(self, event):
Jack Jansen7e0da901995-08-17 14:18:20 +0000346 (what, message, when, where, modifiers) = event
347 wid = WhichWindow(message)
348 if wid and self._windows.has_key(wid):
349 window = self._windows[wid]
350 window.do_rawupdate(wid, event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000351 else:
Jack Jansen7a583361995-08-14 12:39:54 +0000352 MacOS.HandleEvent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000353
Jack Jansen7e0da901995-08-17 14:18:20 +0000354 def do_activateEvt(self, event):
355 (what, message, when, where, modifiers) = event
Just van Rossum5763e071999-01-27 14:22:11 +0000356 wid = WhichWindow(message)
Jack Jansen7e0da901995-08-17 14:18:20 +0000357 if wid and self._windows.has_key(wid):
358 window = self._windows[wid]
359 window.do_activate(modifiers & 1, event)
360 else:
361 MacOS.HandleEvent(event)
Just van Rossum5763e071999-01-27 14:22:11 +0000362
Jack Jansen7e0da901995-08-17 14:18:20 +0000363 def do_osEvt(self, event):
364 (what, message, when, where, modifiers) = event
365 which = (message >> 24) & 0xff
366 if which == 1: # suspend/resume
367 self.do_suspendresume(event)
368 else:
369 if DEBUG:
370 print 'unknown osEvt:',
371 self.printevent(event)
Just van Rossum5763e071999-01-27 14:22:11 +0000372
Jack Jansen7e0da901995-08-17 14:18:20 +0000373 def do_suspendresume(self, event):
Jack Jansen7e0da901995-08-17 14:18:20 +0000374 (what, message, when, where, modifiers) = event
Just van Rossum5763e071999-01-27 14:22:11 +0000375 wid = FrontWindow()
376 if wid and self._windows.has_key(wid):
377 window = self._windows[wid]
Just van Rossum1a5eb041999-12-15 14:55:16 +0000378 window.do_activate(message & 1, event)
Just van Rossum5763e071999-01-27 14:22:11 +0000379
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000380 def do_kHighLevelEvent(self, event):
381 (what, message, when, where, modifiers) = event
Jack Jansen7a583361995-08-14 12:39:54 +0000382 if DEBUG:
383 print "High Level Event:",
384 self.printevent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000385 try:
386 AEProcessAppleEvent(event)
387 except:
388 print "AEProcessAppleEvent error:"
389 traceback.print_exc()
390
391 def do_unknownevent(self, event):
Jack Jansen7e0da901995-08-17 14:18:20 +0000392 if DEBUG:
393 print "Unhandled event:",
394 self.printevent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000395
396 def printevent(self, event):
397 (what, message, when, where, modifiers) = event
398 nicewhat = `what`
399 if eventname.has_key(what):
400 nicewhat = eventname[what]
401 print nicewhat,
402 if what == kHighLevelEvent:
403 h, v = where
404 print `ostypecode(message)`, hex(when), `ostypecode(h | (v<<16))`,
405 else:
406 print hex(message), hex(when), where,
407 print hex(modifiers)
408
409
410class MenuBar:
411 """Represent a set of menus in a menu bar.
412
413 Interface:
414
415 - (constructor)
416 - (destructor)
417 - addmenu
418 - addpopup (normally used internally)
419 - dispatch (called from Application)
420 """
421
422 nextid = 1 # Necessarily a class variable
423
424 def getnextid(self):
Jack Jansenb1667ef1996-09-26 16:17:08 +0000425 id = MenuBar.nextid
426 MenuBar.nextid = id+1
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000427 return id
428
Jack Jansenb1667ef1996-09-26 16:17:08 +0000429 def __init__(self, parent=None):
430 self.parent = parent
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000431 ClearMenuBar()
432 self.bar = GetMenuBar()
433 self.menus = {}
434
Jack Jansenb1667ef1996-09-26 16:17:08 +0000435 # XXX necessary?
436 def close(self):
437 self.parent = None
438 self.bar = None
Jack Jansen7b56aad1998-02-20 15:51:39 +0000439 self.menus = None
Jack Jansenb1667ef1996-09-26 16:17:08 +0000440
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000441 def addmenu(self, title, after = 0):
442 id = self.getnextid()
Jack Jansene3532151996-04-12 16:24:44 +0000443 if DEBUG: print 'Newmenu', title, id # XXXX
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000444 m = NewMenu(id, title)
445 m.InsertMenu(after)
Jack Jansenbb6193c1998-05-06 15:33:09 +0000446 if after >= 0:
447 if self.parent:
448 self.parent.needmenubarredraw = 1
449 else:
450 DrawMenuBar()
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000451 return id, m
Jack Jansendb9ff361996-03-12 13:32:03 +0000452
453 def delmenu(self, id):
Jack Jansene3532151996-04-12 16:24:44 +0000454 if DEBUG: print 'Delmenu', id # XXXX
Jack Jansendb9ff361996-03-12 13:32:03 +0000455 DeleteMenu(id)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000456
457 def addpopup(self, title = ''):
458 return self.addmenu(title, -1)
Jack Jansenb1667ef1996-09-26 16:17:08 +0000459
460# Useless:
461# def install(self):
462# if not self.bar: return
463# SetMenuBar(self.bar)
464# if self.parent:
465# self.parent.needmenubarredraw = 1
466# else:
467# DrawMenuBar()
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000468
Jack Jansenb1667ef1996-09-26 16:17:08 +0000469 def fixmenudimstate(self):
470 for m in self.menus.keys():
471 menu = self.menus[m]
472 if menu.__class__ == FrameWork.AppleMenu:
473 continue
474 for i in range(len(menu.items)):
475 label, shortcut, callback, kind = menu.items[i]
476 if type(callback) == types.StringType:
477 wid = Win.FrontWindow()
478 if wid and self.parent._windows.has_key(wid):
479 window = self.parent._windows[wid]
480 if hasattr(window, "domenu_" + callback):
Jack Jansenafd0aa62001-01-29 13:29:47 +0000481 menu.menu.EnableMenuItem(i + 1)
Jack Jansenb1667ef1996-09-26 16:17:08 +0000482 elif hasattr(self.parent, "domenu_" + callback):
Jack Jansenafd0aa62001-01-29 13:29:47 +0000483 menu.menu.EnableMenuItem(i + 1)
Jack Jansenb1667ef1996-09-26 16:17:08 +0000484 else:
Jack Jansenafd0aa62001-01-29 13:29:47 +0000485 menu.menu.DisableMenuItem(i + 1)
Jack Jansenb1667ef1996-09-26 16:17:08 +0000486 elif hasattr(self.parent, "domenu_" + callback):
Jack Jansenafd0aa62001-01-29 13:29:47 +0000487 menu.menu.EnableMenuItem(i + 1)
Jack Jansenb1667ef1996-09-26 16:17:08 +0000488 else:
Jack Jansenafd0aa62001-01-29 13:29:47 +0000489 menu.menu.DisableMenuItem(i + 1)
Jack Jansenb1667ef1996-09-26 16:17:08 +0000490 elif callback:
491 pass
492
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000493 def dispatch(self, id, item, window, event):
494 if self.menus.has_key(id):
495 self.menus[id].dispatch(id, item, window, event)
496 else:
Jack Jansen7a583361995-08-14 12:39:54 +0000497 if DEBUG: print "MenuBar.dispatch(%d, %d, %s, %s)" % \
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000498 (id, item, window, event)
499
500
501# XXX Need a way to get menus as resources and bind them to callbacks
502
503class Menu:
504 "One menu."
505
506 def __init__(self, bar, title, after=0):
507 self.bar = bar
508 self.id, self.menu = self.bar.addmenu(title, after)
509 bar.menus[self.id] = self
510 self.items = []
Jack Jansen341d1fe1998-10-15 15:29:16 +0000511 self._parent = None
Jack Jansendb9ff361996-03-12 13:32:03 +0000512
513 def delete(self):
514 self.bar.delmenu(self.id)
515 del self.bar.menus[self.id]
Jack Jansen34d11f02000-03-07 23:40:13 +0000516 self.menu.DisposeMenu()
Jack Jansendb9ff361996-03-12 13:32:03 +0000517 del self.bar
518 del self.items
519 del self.menu
520 del self.id
Jack Jansen341d1fe1998-10-15 15:29:16 +0000521 del self._parent
Jack Jansen5c440271998-07-13 13:41:02 +0000522
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000523 def additem(self, label, shortcut=None, callback=None, kind=None):
524 self.menu.AppendMenu('x') # add a dummy string
Jack Jansen34d11f02000-03-07 23:40:13 +0000525 self.items.append((label, shortcut, callback, kind))
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000526 item = len(self.items)
Jack Jansene4b40381995-07-17 13:25:15 +0000527 self.menu.SetMenuItemText(item, label) # set the actual text
Jack Jansen13681b71999-12-14 15:45:53 +0000528 if shortcut and type(shortcut) == type(()):
529 modifiers, char = shortcut[:2]
530 self.menu.SetItemCmd(item, ord(char))
531 self.menu.SetMenuItemModifiers(item, modifiers)
532 if len(shortcut) > 2:
Jack Jansenc15e43a1999-12-15 15:45:23 +0000533 self.menu.SetMenuItemKeyGlyph(item, shortcut[2])
Jack Jansen13681b71999-12-14 15:45:53 +0000534 elif shortcut:
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000535 self.menu.SetItemCmd(item, ord(shortcut))
536 return item
Jack Jansen5c440271998-07-13 13:41:02 +0000537
538 def delitem(self, item):
539 if item != len(self.items):
540 raise 'Can only delete last item of a menu'
541 self.menu.DeleteMenuItem(item)
542 del self.items[item-1]
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000543
544 def addcheck(self, label, shortcut=None, callback=None):
545 return self.additem(label, shortcut, callback, 'check')
546
547 def addradio(self, label, shortcut=None, callback=None):
548 return self.additem(label, shortcut, callback, 'radio')
549
550 def addseparator(self):
551 self.menu.AppendMenu('(-')
Jack Jansen34d11f02000-03-07 23:40:13 +0000552 self.items.append(('', None, None, 'separator'))
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000553
554 def addsubmenu(self, label, title=''):
555 sub = Menu(self.bar, title, -1)
556 item = self.additem(label, '\x1B', None, 'submenu')
557 self.menu.SetItemMark(item, sub.id)
Jack Jansen341d1fe1998-10-15 15:29:16 +0000558 sub._parent = self
559 sub._parent_item = item
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000560 return sub
561
562 def dispatch(self, id, item, window, event):
Jack Jansenb1667ef1996-09-26 16:17:08 +0000563 title, shortcut, callback, mtype = self.items[item-1]
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000564 if callback:
Jack Jansenb1667ef1996-09-26 16:17:08 +0000565 if not self.bar.parent or type(callback) <> types.StringType:
566 menuhandler = callback
567 else:
568 # callback is string
569 wid = Win.FrontWindow()
570 if wid and self.bar.parent._windows.has_key(wid):
571 window = self.bar.parent._windows[wid]
572 if hasattr(window, "domenu_" + callback):
573 menuhandler = getattr(window, "domenu_" + callback)
574 elif hasattr(self.bar.parent, "domenu_" + callback):
575 menuhandler = getattr(self.bar.parent, "domenu_" + callback)
576 else:
577 # nothing we can do. we shouldn't have come this far
578 # since the menu item should have been disabled...
579 return
580 elif hasattr(self.bar.parent, "domenu_" + callback):
581 menuhandler = getattr(self.bar.parent, "domenu_" + callback)
582 else:
583 # nothing we can do. we shouldn't have come this far
584 # since the menu item should have been disabled...
585 return
586 menuhandler(id, item, window, event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000587
Jack Jansencef2c591996-04-11 15:39:01 +0000588 def enable(self, onoff):
589 if onoff:
Jack Jansenafd0aa62001-01-29 13:29:47 +0000590 self.menu.EnableMenuItem(0)
Jack Jansen341d1fe1998-10-15 15:29:16 +0000591 if self._parent:
Jack Jansenafd0aa62001-01-29 13:29:47 +0000592 self._parent.menu.EnableMenuItem(self._parent_item)
Jack Jansencef2c591996-04-11 15:39:01 +0000593 else:
Jack Jansenafd0aa62001-01-29 13:29:47 +0000594 self.menu.DisableMenuItem(0)
Jack Jansen341d1fe1998-10-15 15:29:16 +0000595 if self._parent:
Jack Jansenafd0aa62001-01-29 13:29:47 +0000596 self._parent.menu.DisableMenuItem(self._parent_item)
Jack Jansen5c440271998-07-13 13:41:02 +0000597 if self.bar and self.bar.parent:
598 self.bar.parent.needmenubarredraw = 1
Jack Jansenbb6193c1998-05-06 15:33:09 +0000599
600class PopupMenu(Menu):
601 def __init__(self, bar):
602 Menu.__init__(self, bar, '(popup)', -1)
603
604 def popup(self, x, y, event, default=1, window=None):
605 # NOTE that x and y are global coordinates, and they should probably
606 # be topleft of the button the user clicked (not mouse-coordinates),
607 # so the popup nicely overlaps.
608 reply = self.menu.PopUpMenuSelect(x, y, default)
609 if not reply:
610 return
611 id = (reply & 0xffff0000) >> 16
612 item = reply & 0xffff
613 if not window:
614 wid = Win.FrontWindow()
615 try:
616 window = self.bar.parent._windows[wid]
617 except:
618 pass # If we can't find the window we pass None
619 self.dispatch(id, item, window, event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000620
621class MenuItem:
622 def __init__(self, menu, title, shortcut=None, callback=None, kind=None):
623 self.item = menu.additem(title, shortcut, callback)
Jack Jansendb9ff361996-03-12 13:32:03 +0000624 self.menu = menu
625
Jack Jansen5c440271998-07-13 13:41:02 +0000626 def delete(self):
627 self.menu.delitem(self.item)
628 del self.menu
629 del self.item
630
Jack Jansendb9ff361996-03-12 13:32:03 +0000631 def check(self, onoff):
Jack Jansenafd0aa62001-01-29 13:29:47 +0000632 self.menu.menu.CheckMenuItem(self.item, onoff)
Jack Jansencef2c591996-04-11 15:39:01 +0000633
634 def enable(self, onoff):
635 if onoff:
Jack Jansenafd0aa62001-01-29 13:29:47 +0000636 self.menu.menu.EnableMenuItem(self.item)
Jack Jansencef2c591996-04-11 15:39:01 +0000637 else:
Jack Jansenafd0aa62001-01-29 13:29:47 +0000638 self.menu.menu.DisableMenuItem(self.item)
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000639
640 def settext(self, text):
641 self.menu.menu.SetMenuItemText(self.item, text)
Jack Jansendb9ff361996-03-12 13:32:03 +0000642
Jack Jansen0f6dc5b1996-04-23 16:18:33 +0000643 def setstyle(self, style):
644 self.menu.menu.SetItemStyle(self.item, style)
645
646 def seticon(self, icon):
647 self.menu.menu.SetItemIcon(self.item, icon)
648
649 def setcmd(self, cmd):
650 self.menu.menu.SetItemCmd(self.item, cmd)
651
652 def setmark(self, cmd):
653 self.menu.menu.SetItemMark(self.item, cmd)
654
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000655
656class RadioItem(MenuItem):
657 def __init__(self, menu, title, shortcut=None, callback=None):
658 MenuItem.__init__(self, menu, title, shortcut, callback, 'radio')
659
660class CheckItem(MenuItem):
661 def __init__(self, menu, title, shortcut=None, callback=None):
662 MenuItem.__init__(self, menu, title, shortcut, callback, 'check')
663
664def Separator(menu):
665 menu.addseparator()
666
667def SubMenu(menu, label, title=''):
668 return menu.addsubmenu(label, title)
669
670
671class AppleMenu(Menu):
672
673 def __init__(self, bar, abouttext="About me...", aboutcallback=None):
674 Menu.__init__(self, bar, "\024")
675 self.additem(abouttext, None, aboutcallback)
676 self.addseparator()
Jack Jansen01a2d9e2001-01-29 15:32:00 +0000677 if MacOS.runtimemodel == 'ppc':
678 self.menu.AppendResMenu('DRVR')
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000679
680 def dispatch(self, id, item, window, event):
681 if item == 1:
682 Menu.dispatch(self, id, item, window, event)
Jack Jansen01a2d9e2001-01-29 15:32:00 +0000683 elif MacOS.runtimemodel == 'ppc':
Jack Jansenc8a99491996-01-08 23:50:13 +0000684 name = self.menu.GetMenuItemText(item)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000685 OpenDeskAcc(name)
686
Jack Jansen7e0da901995-08-17 14:18:20 +0000687class Window:
688 """A single window belonging to an application"""
689
690 def __init__(self, parent):
691 self.wid = None
692 self.parent = parent
693
Jack Jansenc8a99491996-01-08 23:50:13 +0000694 def open(self, bounds=(40, 40, 400, 400), resid=None):
695 if resid <> None:
696 self.wid = GetNewWindow(resid, -1)
697 else:
698 self.wid = NewWindow(bounds, self.__class__.__name__, 1,
Jack Jansended835c1996-07-26 14:01:07 +0000699 8, -1, 1, 0) # changed to proc id 8 to include zoom box. jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000700 self.do_postopen()
701
702 def do_postopen(self):
703 """Tell our parent we exist"""
704 self.parent.appendwindow(self.wid, self)
705
706 def close(self):
Jack Jansen7e0da901995-08-17 14:18:20 +0000707 self.do_postclose()
708
709 def do_postclose(self):
710 self.parent.removewindow(self.wid)
711 self.parent = None
712 self.wid = None
Jack Jansenc8a99491996-01-08 23:50:13 +0000713
714 def SetPort(self):
715 # Convinience method
716 SetPort(self.wid)
Jack Jansen73023402001-01-23 14:58:20 +0000717
718 def GetWindow(self):
719 return self.wid
Jack Jansen7e0da901995-08-17 14:18:20 +0000720
721 def do_inDrag(self, partcode, window, event):
722 where = event[3]
723 window.DragWindow(where, self.draglimit)
724
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000725 draglimit = screenbounds
Jack Jansen7e0da901995-08-17 14:18:20 +0000726
727 def do_inGoAway(self, partcode, window, event):
728 where = event[3]
729 if window.TrackGoAway(where):
730 self.close()
731
732 def do_inZoom(self, partcode, window, event):
733 (what, message, when, where, modifiers) = event
734 if window.TrackBox(where, partcode):
735 window.ZoomWindow(partcode, 1)
Jack Jansended835c1996-07-26 14:01:07 +0000736 rect = window.GetWindowUserState() # so that zoom really works... jvr
737 self.do_postresize(rect[2] - rect[0], rect[3] - rect[1], window) # jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000738
739 def do_inZoomIn(self, partcode, window, event):
740 SetPort(window) # !!!
741 self.do_inZoom(partcode, window, event)
742
743 def do_inZoomOut(self, partcode, window, event):
744 SetPort(window) # !!!
745 self.do_inZoom(partcode, window, event)
746
747 def do_inGrow(self, partcode, window, event):
748 (what, message, when, where, modifiers) = event
749 result = window.GrowWindow(where, self.growlimit)
750 if result:
751 height = (result>>16) & 0xffff # Hi word
752 width = result & 0xffff # Lo word
753 self.do_resize(width, height, window)
754
Jack Jansended835c1996-07-26 14:01:07 +0000755 growlimit = (50, 50, screenbounds[2] - screenbounds[0], screenbounds[3] - screenbounds[1]) # jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000756
757 def do_resize(self, width, height, window):
Jack Jansended835c1996-07-26 14:01:07 +0000758 l, t, r, b = self.wid.GetWindowPort().portRect # jvr, forGrowIcon
759 self.SetPort() # jvr
Jack Jansen73023402001-01-23 14:58:20 +0000760 self.wid.InvalWindowRect((r - SCROLLBARWIDTH + 1, b - SCROLLBARWIDTH + 1, r, b)) # jvr
Jack Jansended835c1996-07-26 14:01:07 +0000761 window.SizeWindow(width, height, 1) # changed updateFlag to true jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000762 self.do_postresize(width, height, window)
763
764 def do_postresize(self, width, height, window):
765 SetPort(window)
Jack Jansen73023402001-01-23 14:58:20 +0000766 self.wid.InvalWindowRect(window.GetWindowPort().portRect)
Jack Jansen7e0da901995-08-17 14:18:20 +0000767
768 def do_inContent(self, partcode, window, event):
769 #
770 # If we're not frontmost, select ourselves and wait for
771 # the activate event.
772 #
773 if FrontWindow() <> window:
774 window.SelectWindow()
775 return
776 # We are. Handle the event.
777 (what, message, when, where, modifiers) = event
778 SetPort(window)
779 local = GlobalToLocal(where)
780 self.do_contentclick(local, modifiers, event)
781
782 def do_contentclick(self, local, modifiers, event):
Jack Jansended835c1996-07-26 14:01:07 +0000783 if DEBUG:
784 print 'Click in contents at %s, modifiers %s'%(local, modifiers)
Jack Jansen7e0da901995-08-17 14:18:20 +0000785
786 def do_rawupdate(self, window, event):
787 if DEBUG: print "raw update for", window
Jack Jansenda38f2d1995-11-14 10:15:42 +0000788 SetPort(window)
Jack Jansen7e0da901995-08-17 14:18:20 +0000789 window.BeginUpdate()
790 self.do_update(window, event)
791 window.EndUpdate()
792
793 def do_update(self, window, event):
Jack Jansended835c1996-07-26 14:01:07 +0000794 if DEBUG:
795 import time
796 for i in range(8):
797 time.sleep(0.1)
798 InvertRgn(window.GetWindowPort().visRgn)
799 FillRgn(window.GetWindowPort().visRgn, qd.gray)
800 else:
801 EraseRgn(window.GetWindowPort().visRgn)
Jack Jansen7e0da901995-08-17 14:18:20 +0000802
803 def do_activate(self, activate, event):
804 if DEBUG: print 'Activate %d for %s'%(activate, self.wid)
805
806class ControlsWindow(Window):
807
808 def do_rawupdate(self, window, event):
809 if DEBUG: print "raw update for", window
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000810 SetPort(window)
Jack Jansen7e0da901995-08-17 14:18:20 +0000811 window.BeginUpdate()
812 self.do_update(window, event)
Jack Jansended835c1996-07-26 14:01:07 +0000813 #DrawControls(window) # jvr
814 UpdateControls(window, window.GetWindowPort().visRgn) # jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000815 window.DrawGrowIcon()
816 window.EndUpdate()
817
818 def do_controlhit(self, window, control, pcode, event):
819 if DEBUG: print "control hit in", window, "on", control, "; pcode =", pcode
820
821 def do_inContent(self, partcode, window, event):
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000822 if FrontWindow() <> window:
823 window.SelectWindow()
824 return
Jack Jansen7e0da901995-08-17 14:18:20 +0000825 (what, message, when, where, modifiers) = event
Jack Jansenda38f2d1995-11-14 10:15:42 +0000826 SetPort(window) # XXXX Needed?
Jack Jansen7e0da901995-08-17 14:18:20 +0000827 local = GlobalToLocal(where)
Jack Jansen41e825a1998-05-28 14:22:48 +0000828 pcode, control = FindControl(local, window)
829 if pcode and control:
830 self.do_rawcontrolhit(window, control, pcode, local, event)
Jack Jansen7e0da901995-08-17 14:18:20 +0000831 else:
832 if DEBUG: print "FindControl(%s, %s) -> (%s, %s)" % \
Jack Jansen41e825a1998-05-28 14:22:48 +0000833 (local, window, pcode, control)
Jack Jansene3532151996-04-12 16:24:44 +0000834 self.do_contentclick(local, modifiers, event)
835
Jack Jansen41e825a1998-05-28 14:22:48 +0000836 def do_rawcontrolhit(self, window, control, pcode, local, event):
837 pcode = control.TrackControl(local)
838 if pcode:
839 self.do_controlhit(window, control, pcode, event)
840
Jack Jansene3532151996-04-12 16:24:44 +0000841class ScrolledWindow(ControlsWindow):
842 def __init__(self, parent):
843 self.barx = self.bary = None
Jack Jansen7bfc8751996-04-16 14:35:43 +0000844 self.barx_enabled = self.bary_enabled = 1
845 self.activated = 1
Jack Jansene3532151996-04-12 16:24:44 +0000846 ControlsWindow.__init__(self, parent)
847
848 def scrollbars(self, wantx=1, wanty=1):
849 SetPort(self.wid)
850 self.barx = self.bary = None
Jack Jansen7bfc8751996-04-16 14:35:43 +0000851 self.barx_enabled = self.bary_enabled = 1
Jack Jansene3532151996-04-12 16:24:44 +0000852 x0, y0, x1, y1 = self.wid.GetWindowPort().portRect
853 vx, vy = self.getscrollbarvalues()
Jack Jansen7bfc8751996-04-16 14:35:43 +0000854 if vx == None: self.barx_enabled, vx = 0, 0
855 if vy == None: self.bary_enabled, vy = 0, 0
Jack Jansene3532151996-04-12 16:24:44 +0000856 if wantx:
857 rect = x0-1, y1-(SCROLLBARWIDTH-1), x1-(SCROLLBARWIDTH-2), y1+1
858 self.barx = NewControl(self.wid, rect, "", 1, vx, 0, 32767, 16, 0)
Jack Jansen7bfc8751996-04-16 14:35:43 +0000859 if not self.barx_enabled: self.barx.HiliteControl(255)
Jack Jansen73023402001-01-23 14:58:20 +0000860## self.wid.InvalWindowRect(rect)
Jack Jansene3532151996-04-12 16:24:44 +0000861 if wanty:
862 rect = x1-(SCROLLBARWIDTH-1), y0-1, x1+1, y1-(SCROLLBARWIDTH-2)
863 self.bary = NewControl(self.wid, rect, "", 1, vy, 0, 32767, 16, 0)
Jack Jansen7bfc8751996-04-16 14:35:43 +0000864 if not self.bary_enabled: self.bary.HiliteControl(255)
Jack Jansen73023402001-01-23 14:58:20 +0000865## self.wid.InvalWindowRect(rect)
Jack Jansene3532151996-04-12 16:24:44 +0000866
867 def do_postclose(self):
868 self.barx = self.bary = None
869 ControlsWindow.do_postclose(self)
870
871 def do_activate(self, onoff, event):
Jack Jansen7bfc8751996-04-16 14:35:43 +0000872 self.activated = onoff
Jack Jansene3532151996-04-12 16:24:44 +0000873 if onoff:
Jack Jansen7bfc8751996-04-16 14:35:43 +0000874 if self.barx and self.barx_enabled:
Jack Jansended835c1996-07-26 14:01:07 +0000875 self.barx.ShowControl() # jvr
Jack Jansen7bfc8751996-04-16 14:35:43 +0000876 if self.bary and self.bary_enabled:
Jack Jansended835c1996-07-26 14:01:07 +0000877 self.bary.ShowControl() # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000878 else:
Jack Jansen7bfc8751996-04-16 14:35:43 +0000879 if self.barx:
Jack Jansended835c1996-07-26 14:01:07 +0000880 self.barx.HideControl() # jvr; An inactive window should have *hidden*
881 # scrollbars, not just dimmed (no matter what
882 # BBEdit does... look at the Finder)
Jack Jansen7bfc8751996-04-16 14:35:43 +0000883 if self.bary:
Jack Jansended835c1996-07-26 14:01:07 +0000884 self.bary.HideControl() # jvr
885 self.wid.DrawGrowIcon() # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000886
887 def do_postresize(self, width, height, window):
888 l, t, r, b = self.wid.GetWindowPort().portRect
Jack Jansended835c1996-07-26 14:01:07 +0000889 self.SetPort()
Jack Jansene3532151996-04-12 16:24:44 +0000890 if self.barx:
Jack Jansended835c1996-07-26 14:01:07 +0000891 self.barx.HideControl() # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000892 self.barx.MoveControl(l-1, b-(SCROLLBARWIDTH-1))
Jack Jansended835c1996-07-26 14:01:07 +0000893 self.barx.SizeControl((r-l)-(SCROLLBARWIDTH-3), SCROLLBARWIDTH) # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000894 if self.bary:
Jack Jansended835c1996-07-26 14:01:07 +0000895 self.bary.HideControl() # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000896 self.bary.MoveControl(r-(SCROLLBARWIDTH-1), t-1)
Jack Jansended835c1996-07-26 14:01:07 +0000897 self.bary.SizeControl(SCROLLBARWIDTH, (b-t)-(SCROLLBARWIDTH-3)) # jvr
898 if self.barx:
899 self.barx.ShowControl() # jvr
Jack Jansen73023402001-01-23 14:58:20 +0000900 self.wid.ValidWindowRect((l, b - SCROLLBARWIDTH + 1, r - SCROLLBARWIDTH + 2, b)) # jvr
Jack Jansended835c1996-07-26 14:01:07 +0000901 if self.bary:
902 self.bary.ShowControl() # jvr
Jack Jansen73023402001-01-23 14:58:20 +0000903 self.wid.ValidWindowRect((r - SCROLLBARWIDTH + 1, t, r, b - SCROLLBARWIDTH + 2)) # jvr
904 self.wid.InvalWindowRect((r - SCROLLBARWIDTH + 1, b - SCROLLBARWIDTH + 1, r, b)) # jvr, growicon
Jack Jansene3532151996-04-12 16:24:44 +0000905
Jack Jansen41e825a1998-05-28 14:22:48 +0000906
907 def do_rawcontrolhit(self, window, control, pcode, local, event):
Jack Jansene3532151996-04-12 16:24:44 +0000908 if control == self.barx:
Jack Jansene3532151996-04-12 16:24:44 +0000909 which = 'x'
910 elif control == self.bary:
Jack Jansene3532151996-04-12 16:24:44 +0000911 which = 'y'
912 else:
913 return 0
Jack Jansen41e825a1998-05-28 14:22:48 +0000914 if pcode in (inUpButton, inDownButton, inPageUp, inPageDown):
915 # We do the work for the buttons and grey area in the tracker
916 dummy = control.TrackControl(local, self.do_controltrack)
917 else:
918 # but the thumb is handled here
919 pcode = control.TrackControl(local)
920 if pcode == inThumb:
921 value = control.GetControlValue()
922 print 'setbars', which, value #DBG
923 self.scrollbar_callback(which, 'set', value)
924 self.updatescrollbars()
925 else:
926 print 'funny part', pcode #DBG
927 return 1
928
929 def do_controltrack(self, control, pcode):
930 if control == self.barx:
931 which = 'x'
932 elif control == self.bary:
933 which = 'y'
934 else:
935 return
936
Jack Jansene3532151996-04-12 16:24:44 +0000937 if pcode == inUpButton:
938 what = '-'
939 elif pcode == inDownButton:
940 what = '+'
941 elif pcode == inPageUp:
942 what = '--'
943 elif pcode == inPageDown:
944 what = '++'
945 else:
Jack Jansen41e825a1998-05-28 14:22:48 +0000946 return
947 self.scrollbar_callback(which, what, None)
Jack Jansene3532151996-04-12 16:24:44 +0000948 self.updatescrollbars()
Jack Jansene3532151996-04-12 16:24:44 +0000949
950 def updatescrollbars(self):
951 SetPort(self.wid)
952 vx, vy = self.getscrollbarvalues()
953 if self.barx:
Jack Jansen7bfc8751996-04-16 14:35:43 +0000954 if vx == None:
955 self.barx.HiliteControl(255)
956 self.barx_enabled = 0
957 else:
958 if not self.barx_enabled:
959 self.barx_enabled = 1
960 if self.activated:
961 self.barx.HiliteControl(0)
962 self.barx.SetControlValue(vx)
Jack Jansene3532151996-04-12 16:24:44 +0000963 if self.bary:
Jack Jansen7bfc8751996-04-16 14:35:43 +0000964 if vy == None:
965 self.bary.HiliteControl(255)
966 self.bary_enabled = 0
967 else:
968 if not self.bary_enabled:
969 self.bary_enabled = 1
970 if self.activated:
971 self.bary.HiliteControl(0)
972 self.bary.SetControlValue(vy)
973
974 # Auxiliary function: convert standard text/image/etc coordinate
975 # to something palatable as getscrollbarvalues() return
976 def scalebarvalue(self, absmin, absmax, curmin, curmax):
977 if curmin <= absmin and curmax >= absmax:
978 return None
979 if curmin <= absmin:
980 return 0
981 if curmax >= absmax:
982 return 32767
983 perc = float(curmin-absmin)/float(absmax-absmin)
984 return int(perc*32767)
Jack Jansene3532151996-04-12 16:24:44 +0000985
986 # To be overridden:
987
988 def getscrollbarvalues(self):
989 return 0, 0
990
991 def scrollbar_callback(self, which, what, value):
992 print 'scroll', which, what, value
Jack Jansen7e0da901995-08-17 14:18:20 +0000993
994class DialogWindow(Window):
995 """A modeless dialog window"""
996
997 def open(self, resid):
998 self.wid = GetNewDialog(resid, -1)
999 self.do_postopen()
1000
1001 def close(self):
Jack Jansen7e0da901995-08-17 14:18:20 +00001002 self.do_postclose()
1003
1004 def do_itemhit(self, item, event):
1005 print 'Dialog %s, item %d hit'%(self.wid, item)
1006
1007 def do_rawupdate(self, window, event):
1008 pass
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +00001009
1010def ostypecode(x):
1011 "Convert a long int to the 4-character code it really is"
1012 s = ''
1013 for i in range(4):
1014 x, c = divmod(x, 256)
1015 s = chr(c) + s
1016 return s
1017
1018
1019class TestApp(Application):
1020
1021 "This class is used by the test() function"
1022
1023 def makeusermenus(self):
1024 self.filemenu = m = Menu(self.menubar, "File")
1025 self.saveitem = MenuItem(m, "Save", "S", self.save)
1026 Separator(m)
1027 self.optionsmenu = mm = SubMenu(m, "Options")
Jack Jansen13681b71999-12-14 15:45:53 +00001028 self.opt1 = CheckItem(mm, "Arguments", "A")
1029 self.opt2 = CheckItem(mm, "Being hit on the head lessons", (kMenuOptionModifier, "A"))
1030 self.opt3 = CheckItem(mm, "Complaints", (kMenuOptionModifier|kMenuNoCommandModifier, "A"))
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +00001031 Separator(m)
1032 self.quititem = MenuItem(m, "Quit", "Q", self.quit)
1033
1034 def save(self, *args):
1035 print "Save"
1036
1037 def quit(self, *args):
1038 raise self
1039
1040
1041def test():
1042 "Test program"
1043 app = TestApp()
1044 app.mainloop()
1045
1046
1047if __name__ == '__main__':
1048 test()