blob: 4454c670216146d494436f8921af8fb56c7880fc [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):
Jack Jansened24cd22001-02-14 17:07:04 +0000225 gotone, dlg, item = DialogSelect(event)
Jack Jansen7e0da901995-08-17 14:18:20 +0000226 if gotone:
Jack Jansened24cd22001-02-14 17:07:04 +0000227 window = dlg.GetDialogWindow()
Jack Jansen7e0da901995-08-17 14:18:20 +0000228 if self._windows.has_key(window):
Jack Jansen13dc4f71995-08-31 13:38:01 +0000229 self._windows[window].do_itemhit(item, event)
Jack Jansen7e0da901995-08-17 14:18:20 +0000230 else:
231 print 'Dialog event for unknown dialog'
Jack Jansen13dc4f71995-08-31 13:38:01 +0000232 return 1
233 return 0
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000234
235 def do_mouseDown(self, event):
236 (what, message, when, where, modifiers) = event
Jack Jansen7e0da901995-08-17 14:18:20 +0000237 partcode, wid = FindWindow(where)
238
239 #
240 # Find the correct name.
241 #
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000242 if partname.has_key(partcode):
243 name = "do_" + partname[partcode]
244 else:
245 name = "do_%d" % partcode
Jack Jansen7e0da901995-08-17 14:18:20 +0000246
247 if wid == None:
248 # No window, or a non-python window
249 try:
250 handler = getattr(self, name)
251 except AttributeError:
252 # Not menubar or something, so assume someone
253 # else's window
254 MacOS.HandleEvent(event)
255 return
256 elif self._windows.has_key(wid):
257 # It is a window. Hand off to correct window.
258 window = self._windows[wid]
259 try:
260 handler = getattr(window, name)
261 except AttributeError:
262 handler = self.do_unknownpartcode
263 else:
264 # It is a python-toolbox window, but not ours.
265 handler = self.do_unknownwindow
266 handler(partcode, wid, event)
267
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000268 def do_inSysWindow(self, partcode, window, event):
Jack Jansen7a583361995-08-14 12:39:54 +0000269 MacOS.HandleEvent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000270
271 def do_inDesk(self, partcode, window, event):
Jack Jansen7a583361995-08-14 12:39:54 +0000272 MacOS.HandleEvent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000273
274 def do_inMenuBar(self, partcode, window, event):
Jack Jansen647535d1996-09-17 12:35:43 +0000275 if not self.menubar:
276 MacOS.HandleEvent(event)
277 return
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000278 (what, message, when, where, modifiers) = event
279 result = MenuSelect(where)
280 id = (result>>16) & 0xffff # Hi word
281 item = result & 0xffff # Lo word
282 self.do_rawmenu(id, item, window, event)
283
284 def do_rawmenu(self, id, item, window, event):
285 try:
286 self.do_menu(id, item, window, event)
287 finally:
288 HiliteMenu(0)
289
290 def do_menu(self, id, item, window, event):
Jack Jansenfd9925a2000-10-19 20:31:51 +0000291 MacOS.OutputSeen()
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000292 self.menubar.dispatch(id, item, window, event)
293
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000294
295 def do_unknownpartcode(self, partcode, window, event):
296 (what, message, when, where, modifiers) = event
Jack Jansen7a583361995-08-14 12:39:54 +0000297 if DEBUG: print "Mouse down at global:", where
298 if DEBUG: print "\tUnknown part code:", partcode
Jack Jansen7e0da901995-08-17 14:18:20 +0000299 if DEBUG: print "\tEvent:", self.printevent(event)
300 MacOS.HandleEvent(event)
301
302 def do_unknownwindow(self, partcode, window, event):
303 if DEBUG: print 'Unknown window:', window
Jack Jansen7a583361995-08-14 12:39:54 +0000304 MacOS.HandleEvent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000305
306 def do_keyDown(self, event):
307 self.do_key(event)
308
309 def do_autoKey(self, event):
310 if not event[-1] & cmdKey:
311 self.do_key(event)
312
313 def do_key(self, event):
314 (what, message, when, where, modifiers) = event
315 c = chr(message & charCodeMask)
Jack Jansenc15e43a1999-12-15 15:45:23 +0000316 if self.menubar:
317 result = MenuEvent(event)
318 id = (result>>16) & 0xffff # Hi word
319 item = result & 0xffff # Lo word
320 if id:
321 self.do_rawmenu(id, item, None, event)
322 return
323 # Otherwise we fall-through
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000324 if modifiers & cmdKey:
325 if c == '.':
326 raise self
327 else:
Jack Jansen647535d1996-09-17 12:35:43 +0000328 if not self.menubar:
329 MacOS.HandleEvent(event)
Jack Jansenc15e43a1999-12-15 15:45:23 +0000330 return
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000331 else:
Jack Jansen7e0da901995-08-17 14:18:20 +0000332 # See whether the front window wants it
333 w = FrontWindow()
334 if w and self._windows.has_key(w):
335 window = self._windows[w]
336 try:
337 do_char = window.do_char
338 except AttributeError:
339 do_char = self.do_char
Jack Jansen6f47bf41995-12-12 15:03:35 +0000340 do_char(c, event)
341 # else it wasn't for us, sigh...
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000342
343 def do_char(self, c, event):
Jack Jansen7a583361995-08-14 12:39:54 +0000344 if DEBUG: print "Character", `c`
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000345
346 def do_updateEvt(self, event):
Jack Jansen7e0da901995-08-17 14:18:20 +0000347 (what, message, when, where, modifiers) = event
348 wid = WhichWindow(message)
349 if wid and self._windows.has_key(wid):
350 window = self._windows[wid]
351 window.do_rawupdate(wid, event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000352 else:
Jack Jansen7a583361995-08-14 12:39:54 +0000353 MacOS.HandleEvent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000354
Jack Jansen7e0da901995-08-17 14:18:20 +0000355 def do_activateEvt(self, event):
356 (what, message, when, where, modifiers) = event
Just van Rossum5763e071999-01-27 14:22:11 +0000357 wid = WhichWindow(message)
Jack Jansen7e0da901995-08-17 14:18:20 +0000358 if wid and self._windows.has_key(wid):
359 window = self._windows[wid]
360 window.do_activate(modifiers & 1, event)
361 else:
362 MacOS.HandleEvent(event)
Just van Rossum5763e071999-01-27 14:22:11 +0000363
Jack Jansen7e0da901995-08-17 14:18:20 +0000364 def do_osEvt(self, event):
365 (what, message, when, where, modifiers) = event
366 which = (message >> 24) & 0xff
367 if which == 1: # suspend/resume
368 self.do_suspendresume(event)
369 else:
370 if DEBUG:
371 print 'unknown osEvt:',
372 self.printevent(event)
Just van Rossum5763e071999-01-27 14:22:11 +0000373
Jack Jansen7e0da901995-08-17 14:18:20 +0000374 def do_suspendresume(self, event):
Jack Jansen7e0da901995-08-17 14:18:20 +0000375 (what, message, when, where, modifiers) = event
Just van Rossum5763e071999-01-27 14:22:11 +0000376 wid = FrontWindow()
377 if wid and self._windows.has_key(wid):
378 window = self._windows[wid]
Just van Rossum1a5eb041999-12-15 14:55:16 +0000379 window.do_activate(message & 1, event)
Just van Rossum5763e071999-01-27 14:22:11 +0000380
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000381 def do_kHighLevelEvent(self, event):
382 (what, message, when, where, modifiers) = event
Jack Jansen7a583361995-08-14 12:39:54 +0000383 if DEBUG:
384 print "High Level Event:",
385 self.printevent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000386 try:
387 AEProcessAppleEvent(event)
388 except:
389 print "AEProcessAppleEvent error:"
390 traceback.print_exc()
391
392 def do_unknownevent(self, event):
Jack Jansen7e0da901995-08-17 14:18:20 +0000393 if DEBUG:
394 print "Unhandled event:",
395 self.printevent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000396
397 def printevent(self, event):
398 (what, message, when, where, modifiers) = event
399 nicewhat = `what`
400 if eventname.has_key(what):
401 nicewhat = eventname[what]
402 print nicewhat,
403 if what == kHighLevelEvent:
404 h, v = where
405 print `ostypecode(message)`, hex(when), `ostypecode(h | (v<<16))`,
406 else:
407 print hex(message), hex(when), where,
408 print hex(modifiers)
409
410
411class MenuBar:
412 """Represent a set of menus in a menu bar.
413
414 Interface:
415
416 - (constructor)
417 - (destructor)
418 - addmenu
419 - addpopup (normally used internally)
420 - dispatch (called from Application)
421 """
422
423 nextid = 1 # Necessarily a class variable
424
425 def getnextid(self):
Jack Jansenb1667ef1996-09-26 16:17:08 +0000426 id = MenuBar.nextid
427 MenuBar.nextid = id+1
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000428 return id
429
Jack Jansenb1667ef1996-09-26 16:17:08 +0000430 def __init__(self, parent=None):
431 self.parent = parent
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000432 ClearMenuBar()
433 self.bar = GetMenuBar()
434 self.menus = {}
435
Jack Jansenb1667ef1996-09-26 16:17:08 +0000436 # XXX necessary?
437 def close(self):
438 self.parent = None
439 self.bar = None
Jack Jansen7b56aad1998-02-20 15:51:39 +0000440 self.menus = None
Jack Jansenb1667ef1996-09-26 16:17:08 +0000441
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000442 def addmenu(self, title, after = 0):
443 id = self.getnextid()
Jack Jansene3532151996-04-12 16:24:44 +0000444 if DEBUG: print 'Newmenu', title, id # XXXX
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000445 m = NewMenu(id, title)
446 m.InsertMenu(after)
Jack Jansenbb6193c1998-05-06 15:33:09 +0000447 if after >= 0:
448 if self.parent:
449 self.parent.needmenubarredraw = 1
450 else:
451 DrawMenuBar()
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000452 return id, m
Jack Jansendb9ff361996-03-12 13:32:03 +0000453
454 def delmenu(self, id):
Jack Jansene3532151996-04-12 16:24:44 +0000455 if DEBUG: print 'Delmenu', id # XXXX
Jack Jansendb9ff361996-03-12 13:32:03 +0000456 DeleteMenu(id)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000457
458 def addpopup(self, title = ''):
459 return self.addmenu(title, -1)
Jack Jansenb1667ef1996-09-26 16:17:08 +0000460
461# Useless:
462# def install(self):
463# if not self.bar: return
464# SetMenuBar(self.bar)
465# if self.parent:
466# self.parent.needmenubarredraw = 1
467# else:
468# DrawMenuBar()
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000469
Jack Jansenb1667ef1996-09-26 16:17:08 +0000470 def fixmenudimstate(self):
471 for m in self.menus.keys():
472 menu = self.menus[m]
473 if menu.__class__ == FrameWork.AppleMenu:
474 continue
475 for i in range(len(menu.items)):
476 label, shortcut, callback, kind = menu.items[i]
477 if type(callback) == types.StringType:
478 wid = Win.FrontWindow()
479 if wid and self.parent._windows.has_key(wid):
480 window = self.parent._windows[wid]
481 if hasattr(window, "domenu_" + callback):
Jack Jansenafd0aa62001-01-29 13:29:47 +0000482 menu.menu.EnableMenuItem(i + 1)
Jack Jansenb1667ef1996-09-26 16:17:08 +0000483 elif hasattr(self.parent, "domenu_" + callback):
Jack Jansenafd0aa62001-01-29 13:29:47 +0000484 menu.menu.EnableMenuItem(i + 1)
Jack Jansenb1667ef1996-09-26 16:17:08 +0000485 else:
Jack Jansenafd0aa62001-01-29 13:29:47 +0000486 menu.menu.DisableMenuItem(i + 1)
Jack Jansenb1667ef1996-09-26 16:17:08 +0000487 elif hasattr(self.parent, "domenu_" + callback):
Jack Jansenafd0aa62001-01-29 13:29:47 +0000488 menu.menu.EnableMenuItem(i + 1)
Jack Jansenb1667ef1996-09-26 16:17:08 +0000489 else:
Jack Jansenafd0aa62001-01-29 13:29:47 +0000490 menu.menu.DisableMenuItem(i + 1)
Jack Jansenb1667ef1996-09-26 16:17:08 +0000491 elif callback:
492 pass
493
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000494 def dispatch(self, id, item, window, event):
495 if self.menus.has_key(id):
496 self.menus[id].dispatch(id, item, window, event)
497 else:
Jack Jansen7a583361995-08-14 12:39:54 +0000498 if DEBUG: print "MenuBar.dispatch(%d, %d, %s, %s)" % \
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000499 (id, item, window, event)
500
501
502# XXX Need a way to get menus as resources and bind them to callbacks
503
504class Menu:
505 "One menu."
506
507 def __init__(self, bar, title, after=0):
508 self.bar = bar
509 self.id, self.menu = self.bar.addmenu(title, after)
510 bar.menus[self.id] = self
511 self.items = []
Jack Jansen341d1fe1998-10-15 15:29:16 +0000512 self._parent = None
Jack Jansendb9ff361996-03-12 13:32:03 +0000513
514 def delete(self):
515 self.bar.delmenu(self.id)
516 del self.bar.menus[self.id]
Jack Jansen34d11f02000-03-07 23:40:13 +0000517 self.menu.DisposeMenu()
Jack Jansendb9ff361996-03-12 13:32:03 +0000518 del self.bar
519 del self.items
520 del self.menu
521 del self.id
Jack Jansen341d1fe1998-10-15 15:29:16 +0000522 del self._parent
Jack Jansen5c440271998-07-13 13:41:02 +0000523
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000524 def additem(self, label, shortcut=None, callback=None, kind=None):
525 self.menu.AppendMenu('x') # add a dummy string
Jack Jansen34d11f02000-03-07 23:40:13 +0000526 self.items.append((label, shortcut, callback, kind))
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000527 item = len(self.items)
Jack Jansene4b40381995-07-17 13:25:15 +0000528 self.menu.SetMenuItemText(item, label) # set the actual text
Jack Jansen13681b71999-12-14 15:45:53 +0000529 if shortcut and type(shortcut) == type(()):
530 modifiers, char = shortcut[:2]
531 self.menu.SetItemCmd(item, ord(char))
532 self.menu.SetMenuItemModifiers(item, modifiers)
533 if len(shortcut) > 2:
Jack Jansenc15e43a1999-12-15 15:45:23 +0000534 self.menu.SetMenuItemKeyGlyph(item, shortcut[2])
Jack Jansen13681b71999-12-14 15:45:53 +0000535 elif shortcut:
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000536 self.menu.SetItemCmd(item, ord(shortcut))
537 return item
Jack Jansen5c440271998-07-13 13:41:02 +0000538
539 def delitem(self, item):
540 if item != len(self.items):
541 raise 'Can only delete last item of a menu'
542 self.menu.DeleteMenuItem(item)
543 del self.items[item-1]
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000544
545 def addcheck(self, label, shortcut=None, callback=None):
546 return self.additem(label, shortcut, callback, 'check')
547
548 def addradio(self, label, shortcut=None, callback=None):
549 return self.additem(label, shortcut, callback, 'radio')
550
551 def addseparator(self):
552 self.menu.AppendMenu('(-')
Jack Jansen34d11f02000-03-07 23:40:13 +0000553 self.items.append(('', None, None, 'separator'))
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000554
555 def addsubmenu(self, label, title=''):
556 sub = Menu(self.bar, title, -1)
557 item = self.additem(label, '\x1B', None, 'submenu')
558 self.menu.SetItemMark(item, sub.id)
Jack Jansen341d1fe1998-10-15 15:29:16 +0000559 sub._parent = self
560 sub._parent_item = item
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000561 return sub
562
563 def dispatch(self, id, item, window, event):
Jack Jansenb1667ef1996-09-26 16:17:08 +0000564 title, shortcut, callback, mtype = self.items[item-1]
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000565 if callback:
Jack Jansenb1667ef1996-09-26 16:17:08 +0000566 if not self.bar.parent or type(callback) <> types.StringType:
567 menuhandler = callback
568 else:
569 # callback is string
570 wid = Win.FrontWindow()
571 if wid and self.bar.parent._windows.has_key(wid):
572 window = self.bar.parent._windows[wid]
573 if hasattr(window, "domenu_" + callback):
574 menuhandler = getattr(window, "domenu_" + callback)
575 elif hasattr(self.bar.parent, "domenu_" + callback):
576 menuhandler = getattr(self.bar.parent, "domenu_" + callback)
577 else:
578 # nothing we can do. we shouldn't have come this far
579 # since the menu item should have been disabled...
580 return
581 elif hasattr(self.bar.parent, "domenu_" + callback):
582 menuhandler = getattr(self.bar.parent, "domenu_" + callback)
583 else:
584 # nothing we can do. we shouldn't have come this far
585 # since the menu item should have been disabled...
586 return
587 menuhandler(id, item, window, event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000588
Jack Jansencef2c591996-04-11 15:39:01 +0000589 def enable(self, onoff):
590 if onoff:
Jack Jansenafd0aa62001-01-29 13:29:47 +0000591 self.menu.EnableMenuItem(0)
Jack Jansen341d1fe1998-10-15 15:29:16 +0000592 if self._parent:
Jack Jansenafd0aa62001-01-29 13:29:47 +0000593 self._parent.menu.EnableMenuItem(self._parent_item)
Jack Jansencef2c591996-04-11 15:39:01 +0000594 else:
Jack Jansenafd0aa62001-01-29 13:29:47 +0000595 self.menu.DisableMenuItem(0)
Jack Jansen341d1fe1998-10-15 15:29:16 +0000596 if self._parent:
Jack Jansenafd0aa62001-01-29 13:29:47 +0000597 self._parent.menu.DisableMenuItem(self._parent_item)
Jack Jansen5c440271998-07-13 13:41:02 +0000598 if self.bar and self.bar.parent:
599 self.bar.parent.needmenubarredraw = 1
Jack Jansenbb6193c1998-05-06 15:33:09 +0000600
601class PopupMenu(Menu):
602 def __init__(self, bar):
603 Menu.__init__(self, bar, '(popup)', -1)
604
605 def popup(self, x, y, event, default=1, window=None):
606 # NOTE that x and y are global coordinates, and they should probably
607 # be topleft of the button the user clicked (not mouse-coordinates),
608 # so the popup nicely overlaps.
609 reply = self.menu.PopUpMenuSelect(x, y, default)
610 if not reply:
611 return
612 id = (reply & 0xffff0000) >> 16
613 item = reply & 0xffff
614 if not window:
615 wid = Win.FrontWindow()
616 try:
617 window = self.bar.parent._windows[wid]
618 except:
619 pass # If we can't find the window we pass None
620 self.dispatch(id, item, window, event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000621
622class MenuItem:
623 def __init__(self, menu, title, shortcut=None, callback=None, kind=None):
624 self.item = menu.additem(title, shortcut, callback)
Jack Jansendb9ff361996-03-12 13:32:03 +0000625 self.menu = menu
626
Jack Jansen5c440271998-07-13 13:41:02 +0000627 def delete(self):
628 self.menu.delitem(self.item)
629 del self.menu
630 del self.item
631
Jack Jansendb9ff361996-03-12 13:32:03 +0000632 def check(self, onoff):
Jack Jansenafd0aa62001-01-29 13:29:47 +0000633 self.menu.menu.CheckMenuItem(self.item, onoff)
Jack Jansencef2c591996-04-11 15:39:01 +0000634
635 def enable(self, onoff):
636 if onoff:
Jack Jansenafd0aa62001-01-29 13:29:47 +0000637 self.menu.menu.EnableMenuItem(self.item)
Jack Jansencef2c591996-04-11 15:39:01 +0000638 else:
Jack Jansenafd0aa62001-01-29 13:29:47 +0000639 self.menu.menu.DisableMenuItem(self.item)
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000640
641 def settext(self, text):
642 self.menu.menu.SetMenuItemText(self.item, text)
Jack Jansendb9ff361996-03-12 13:32:03 +0000643
Jack Jansen0f6dc5b1996-04-23 16:18:33 +0000644 def setstyle(self, style):
645 self.menu.menu.SetItemStyle(self.item, style)
646
647 def seticon(self, icon):
648 self.menu.menu.SetItemIcon(self.item, icon)
649
650 def setcmd(self, cmd):
651 self.menu.menu.SetItemCmd(self.item, cmd)
652
653 def setmark(self, cmd):
654 self.menu.menu.SetItemMark(self.item, cmd)
655
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000656
657class RadioItem(MenuItem):
658 def __init__(self, menu, title, shortcut=None, callback=None):
659 MenuItem.__init__(self, menu, title, shortcut, callback, 'radio')
660
661class CheckItem(MenuItem):
662 def __init__(self, menu, title, shortcut=None, callback=None):
663 MenuItem.__init__(self, menu, title, shortcut, callback, 'check')
664
665def Separator(menu):
666 menu.addseparator()
667
668def SubMenu(menu, label, title=''):
669 return menu.addsubmenu(label, title)
670
671
672class AppleMenu(Menu):
673
674 def __init__(self, bar, abouttext="About me...", aboutcallback=None):
675 Menu.__init__(self, bar, "\024")
676 self.additem(abouttext, None, aboutcallback)
677 self.addseparator()
Jack Jansen01a2d9e2001-01-29 15:32:00 +0000678 if MacOS.runtimemodel == 'ppc':
679 self.menu.AppendResMenu('DRVR')
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000680
681 def dispatch(self, id, item, window, event):
682 if item == 1:
683 Menu.dispatch(self, id, item, window, event)
Jack Jansen01a2d9e2001-01-29 15:32:00 +0000684 elif MacOS.runtimemodel == 'ppc':
Jack Jansenc8a99491996-01-08 23:50:13 +0000685 name = self.menu.GetMenuItemText(item)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000686 OpenDeskAcc(name)
687
Jack Jansen7e0da901995-08-17 14:18:20 +0000688class Window:
689 """A single window belonging to an application"""
690
691 def __init__(self, parent):
692 self.wid = None
693 self.parent = parent
694
Jack Jansenc8a99491996-01-08 23:50:13 +0000695 def open(self, bounds=(40, 40, 400, 400), resid=None):
696 if resid <> None:
697 self.wid = GetNewWindow(resid, -1)
698 else:
699 self.wid = NewWindow(bounds, self.__class__.__name__, 1,
Jack Jansended835c1996-07-26 14:01:07 +0000700 8, -1, 1, 0) # changed to proc id 8 to include zoom box. jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000701 self.do_postopen()
702
703 def do_postopen(self):
704 """Tell our parent we exist"""
705 self.parent.appendwindow(self.wid, self)
706
707 def close(self):
Jack Jansen7e0da901995-08-17 14:18:20 +0000708 self.do_postclose()
709
710 def do_postclose(self):
711 self.parent.removewindow(self.wid)
712 self.parent = None
713 self.wid = None
Jack Jansenc8a99491996-01-08 23:50:13 +0000714
715 def SetPort(self):
716 # Convinience method
717 SetPort(self.wid)
Jack Jansen73023402001-01-23 14:58:20 +0000718
719 def GetWindow(self):
720 return self.wid
Jack Jansen7e0da901995-08-17 14:18:20 +0000721
722 def do_inDrag(self, partcode, window, event):
723 where = event[3]
724 window.DragWindow(where, self.draglimit)
725
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000726 draglimit = screenbounds
Jack Jansen7e0da901995-08-17 14:18:20 +0000727
728 def do_inGoAway(self, partcode, window, event):
729 where = event[3]
730 if window.TrackGoAway(where):
731 self.close()
732
733 def do_inZoom(self, partcode, window, event):
734 (what, message, when, where, modifiers) = event
735 if window.TrackBox(where, partcode):
736 window.ZoomWindow(partcode, 1)
Jack Jansended835c1996-07-26 14:01:07 +0000737 rect = window.GetWindowUserState() # so that zoom really works... jvr
738 self.do_postresize(rect[2] - rect[0], rect[3] - rect[1], window) # jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000739
740 def do_inZoomIn(self, partcode, window, event):
741 SetPort(window) # !!!
742 self.do_inZoom(partcode, window, event)
743
744 def do_inZoomOut(self, partcode, window, event):
745 SetPort(window) # !!!
746 self.do_inZoom(partcode, window, event)
747
748 def do_inGrow(self, partcode, window, event):
749 (what, message, when, where, modifiers) = event
750 result = window.GrowWindow(where, self.growlimit)
751 if result:
752 height = (result>>16) & 0xffff # Hi word
753 width = result & 0xffff # Lo word
754 self.do_resize(width, height, window)
755
Jack Jansended835c1996-07-26 14:01:07 +0000756 growlimit = (50, 50, screenbounds[2] - screenbounds[0], screenbounds[3] - screenbounds[1]) # jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000757
758 def do_resize(self, width, height, window):
Jack Jansended835c1996-07-26 14:01:07 +0000759 l, t, r, b = self.wid.GetWindowPort().portRect # jvr, forGrowIcon
760 self.SetPort() # jvr
Jack Jansen73023402001-01-23 14:58:20 +0000761 self.wid.InvalWindowRect((r - SCROLLBARWIDTH + 1, b - SCROLLBARWIDTH + 1, r, b)) # jvr
Jack Jansended835c1996-07-26 14:01:07 +0000762 window.SizeWindow(width, height, 1) # changed updateFlag to true jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000763 self.do_postresize(width, height, window)
764
765 def do_postresize(self, width, height, window):
766 SetPort(window)
Jack Jansen73023402001-01-23 14:58:20 +0000767 self.wid.InvalWindowRect(window.GetWindowPort().portRect)
Jack Jansen7e0da901995-08-17 14:18:20 +0000768
769 def do_inContent(self, partcode, window, event):
770 #
771 # If we're not frontmost, select ourselves and wait for
772 # the activate event.
773 #
774 if FrontWindow() <> window:
775 window.SelectWindow()
776 return
777 # We are. Handle the event.
778 (what, message, when, where, modifiers) = event
779 SetPort(window)
780 local = GlobalToLocal(where)
781 self.do_contentclick(local, modifiers, event)
782
783 def do_contentclick(self, local, modifiers, event):
Jack Jansended835c1996-07-26 14:01:07 +0000784 if DEBUG:
785 print 'Click in contents at %s, modifiers %s'%(local, modifiers)
Jack Jansen7e0da901995-08-17 14:18:20 +0000786
787 def do_rawupdate(self, window, event):
788 if DEBUG: print "raw update for", window
Jack Jansenda38f2d1995-11-14 10:15:42 +0000789 SetPort(window)
Jack Jansen7e0da901995-08-17 14:18:20 +0000790 window.BeginUpdate()
791 self.do_update(window, event)
792 window.EndUpdate()
793
794 def do_update(self, window, event):
Jack Jansended835c1996-07-26 14:01:07 +0000795 if DEBUG:
796 import time
797 for i in range(8):
798 time.sleep(0.1)
799 InvertRgn(window.GetWindowPort().visRgn)
800 FillRgn(window.GetWindowPort().visRgn, qd.gray)
801 else:
802 EraseRgn(window.GetWindowPort().visRgn)
Jack Jansen7e0da901995-08-17 14:18:20 +0000803
804 def do_activate(self, activate, event):
805 if DEBUG: print 'Activate %d for %s'%(activate, self.wid)
806
807class ControlsWindow(Window):
808
809 def do_rawupdate(self, window, event):
810 if DEBUG: print "raw update for", window
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000811 SetPort(window)
Jack Jansen7e0da901995-08-17 14:18:20 +0000812 window.BeginUpdate()
813 self.do_update(window, event)
Jack Jansended835c1996-07-26 14:01:07 +0000814 #DrawControls(window) # jvr
815 UpdateControls(window, window.GetWindowPort().visRgn) # jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000816 window.DrawGrowIcon()
817 window.EndUpdate()
818
819 def do_controlhit(self, window, control, pcode, event):
820 if DEBUG: print "control hit in", window, "on", control, "; pcode =", pcode
821
822 def do_inContent(self, partcode, window, event):
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000823 if FrontWindow() <> window:
824 window.SelectWindow()
825 return
Jack Jansen7e0da901995-08-17 14:18:20 +0000826 (what, message, when, where, modifiers) = event
Jack Jansenda38f2d1995-11-14 10:15:42 +0000827 SetPort(window) # XXXX Needed?
Jack Jansen7e0da901995-08-17 14:18:20 +0000828 local = GlobalToLocal(where)
Jack Jansen41e825a1998-05-28 14:22:48 +0000829 pcode, control = FindControl(local, window)
830 if pcode and control:
831 self.do_rawcontrolhit(window, control, pcode, local, event)
Jack Jansen7e0da901995-08-17 14:18:20 +0000832 else:
833 if DEBUG: print "FindControl(%s, %s) -> (%s, %s)" % \
Jack Jansen41e825a1998-05-28 14:22:48 +0000834 (local, window, pcode, control)
Jack Jansene3532151996-04-12 16:24:44 +0000835 self.do_contentclick(local, modifiers, event)
836
Jack Jansen41e825a1998-05-28 14:22:48 +0000837 def do_rawcontrolhit(self, window, control, pcode, local, event):
838 pcode = control.TrackControl(local)
839 if pcode:
840 self.do_controlhit(window, control, pcode, event)
841
Jack Jansene3532151996-04-12 16:24:44 +0000842class ScrolledWindow(ControlsWindow):
843 def __init__(self, parent):
844 self.barx = self.bary = None
Jack Jansen7bfc8751996-04-16 14:35:43 +0000845 self.barx_enabled = self.bary_enabled = 1
846 self.activated = 1
Jack Jansene3532151996-04-12 16:24:44 +0000847 ControlsWindow.__init__(self, parent)
848
849 def scrollbars(self, wantx=1, wanty=1):
850 SetPort(self.wid)
851 self.barx = self.bary = None
Jack Jansen7bfc8751996-04-16 14:35:43 +0000852 self.barx_enabled = self.bary_enabled = 1
Jack Jansene3532151996-04-12 16:24:44 +0000853 x0, y0, x1, y1 = self.wid.GetWindowPort().portRect
854 vx, vy = self.getscrollbarvalues()
Jack Jansen7bfc8751996-04-16 14:35:43 +0000855 if vx == None: self.barx_enabled, vx = 0, 0
856 if vy == None: self.bary_enabled, vy = 0, 0
Jack Jansene3532151996-04-12 16:24:44 +0000857 if wantx:
858 rect = x0-1, y1-(SCROLLBARWIDTH-1), x1-(SCROLLBARWIDTH-2), y1+1
859 self.barx = NewControl(self.wid, rect, "", 1, vx, 0, 32767, 16, 0)
Jack Jansen7bfc8751996-04-16 14:35:43 +0000860 if not self.barx_enabled: self.barx.HiliteControl(255)
Jack Jansen73023402001-01-23 14:58:20 +0000861## self.wid.InvalWindowRect(rect)
Jack Jansene3532151996-04-12 16:24:44 +0000862 if wanty:
863 rect = x1-(SCROLLBARWIDTH-1), y0-1, x1+1, y1-(SCROLLBARWIDTH-2)
864 self.bary = NewControl(self.wid, rect, "", 1, vy, 0, 32767, 16, 0)
Jack Jansen7bfc8751996-04-16 14:35:43 +0000865 if not self.bary_enabled: self.bary.HiliteControl(255)
Jack Jansen73023402001-01-23 14:58:20 +0000866## self.wid.InvalWindowRect(rect)
Jack Jansene3532151996-04-12 16:24:44 +0000867
868 def do_postclose(self):
869 self.barx = self.bary = None
870 ControlsWindow.do_postclose(self)
871
872 def do_activate(self, onoff, event):
Jack Jansen7bfc8751996-04-16 14:35:43 +0000873 self.activated = onoff
Jack Jansene3532151996-04-12 16:24:44 +0000874 if onoff:
Jack Jansen7bfc8751996-04-16 14:35:43 +0000875 if self.barx and self.barx_enabled:
Jack Jansended835c1996-07-26 14:01:07 +0000876 self.barx.ShowControl() # jvr
Jack Jansen7bfc8751996-04-16 14:35:43 +0000877 if self.bary and self.bary_enabled:
Jack Jansended835c1996-07-26 14:01:07 +0000878 self.bary.ShowControl() # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000879 else:
Jack Jansen7bfc8751996-04-16 14:35:43 +0000880 if self.barx:
Jack Jansended835c1996-07-26 14:01:07 +0000881 self.barx.HideControl() # jvr; An inactive window should have *hidden*
882 # scrollbars, not just dimmed (no matter what
883 # BBEdit does... look at the Finder)
Jack Jansen7bfc8751996-04-16 14:35:43 +0000884 if self.bary:
Jack Jansended835c1996-07-26 14:01:07 +0000885 self.bary.HideControl() # jvr
886 self.wid.DrawGrowIcon() # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000887
888 def do_postresize(self, width, height, window):
889 l, t, r, b = self.wid.GetWindowPort().portRect
Jack Jansended835c1996-07-26 14:01:07 +0000890 self.SetPort()
Jack Jansene3532151996-04-12 16:24:44 +0000891 if self.barx:
Jack Jansended835c1996-07-26 14:01:07 +0000892 self.barx.HideControl() # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000893 self.barx.MoveControl(l-1, b-(SCROLLBARWIDTH-1))
Jack Jansended835c1996-07-26 14:01:07 +0000894 self.barx.SizeControl((r-l)-(SCROLLBARWIDTH-3), SCROLLBARWIDTH) # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000895 if self.bary:
Jack Jansended835c1996-07-26 14:01:07 +0000896 self.bary.HideControl() # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000897 self.bary.MoveControl(r-(SCROLLBARWIDTH-1), t-1)
Jack Jansended835c1996-07-26 14:01:07 +0000898 self.bary.SizeControl(SCROLLBARWIDTH, (b-t)-(SCROLLBARWIDTH-3)) # jvr
899 if self.barx:
900 self.barx.ShowControl() # jvr
Jack Jansen73023402001-01-23 14:58:20 +0000901 self.wid.ValidWindowRect((l, b - SCROLLBARWIDTH + 1, r - SCROLLBARWIDTH + 2, b)) # jvr
Jack Jansended835c1996-07-26 14:01:07 +0000902 if self.bary:
903 self.bary.ShowControl() # jvr
Jack Jansen73023402001-01-23 14:58:20 +0000904 self.wid.ValidWindowRect((r - SCROLLBARWIDTH + 1, t, r, b - SCROLLBARWIDTH + 2)) # jvr
905 self.wid.InvalWindowRect((r - SCROLLBARWIDTH + 1, b - SCROLLBARWIDTH + 1, r, b)) # jvr, growicon
Jack Jansene3532151996-04-12 16:24:44 +0000906
Jack Jansen41e825a1998-05-28 14:22:48 +0000907
908 def do_rawcontrolhit(self, window, control, pcode, local, event):
Jack Jansene3532151996-04-12 16:24:44 +0000909 if control == self.barx:
Jack Jansene3532151996-04-12 16:24:44 +0000910 which = 'x'
911 elif control == self.bary:
Jack Jansene3532151996-04-12 16:24:44 +0000912 which = 'y'
913 else:
914 return 0
Jack Jansen41e825a1998-05-28 14:22:48 +0000915 if pcode in (inUpButton, inDownButton, inPageUp, inPageDown):
916 # We do the work for the buttons and grey area in the tracker
917 dummy = control.TrackControl(local, self.do_controltrack)
918 else:
919 # but the thumb is handled here
920 pcode = control.TrackControl(local)
921 if pcode == inThumb:
922 value = control.GetControlValue()
923 print 'setbars', which, value #DBG
924 self.scrollbar_callback(which, 'set', value)
925 self.updatescrollbars()
926 else:
927 print 'funny part', pcode #DBG
928 return 1
929
930 def do_controltrack(self, control, pcode):
931 if control == self.barx:
932 which = 'x'
933 elif control == self.bary:
934 which = 'y'
935 else:
936 return
937
Jack Jansene3532151996-04-12 16:24:44 +0000938 if pcode == inUpButton:
939 what = '-'
940 elif pcode == inDownButton:
941 what = '+'
942 elif pcode == inPageUp:
943 what = '--'
944 elif pcode == inPageDown:
945 what = '++'
946 else:
Jack Jansen41e825a1998-05-28 14:22:48 +0000947 return
948 self.scrollbar_callback(which, what, None)
Jack Jansene3532151996-04-12 16:24:44 +0000949 self.updatescrollbars()
Jack Jansene3532151996-04-12 16:24:44 +0000950
951 def updatescrollbars(self):
952 SetPort(self.wid)
953 vx, vy = self.getscrollbarvalues()
954 if self.barx:
Jack Jansen7bfc8751996-04-16 14:35:43 +0000955 if vx == None:
956 self.barx.HiliteControl(255)
957 self.barx_enabled = 0
958 else:
959 if not self.barx_enabled:
960 self.barx_enabled = 1
961 if self.activated:
962 self.barx.HiliteControl(0)
963 self.barx.SetControlValue(vx)
Jack Jansene3532151996-04-12 16:24:44 +0000964 if self.bary:
Jack Jansen7bfc8751996-04-16 14:35:43 +0000965 if vy == None:
966 self.bary.HiliteControl(255)
967 self.bary_enabled = 0
968 else:
969 if not self.bary_enabled:
970 self.bary_enabled = 1
971 if self.activated:
972 self.bary.HiliteControl(0)
973 self.bary.SetControlValue(vy)
974
975 # Auxiliary function: convert standard text/image/etc coordinate
976 # to something palatable as getscrollbarvalues() return
977 def scalebarvalue(self, absmin, absmax, curmin, curmax):
978 if curmin <= absmin and curmax >= absmax:
979 return None
980 if curmin <= absmin:
981 return 0
982 if curmax >= absmax:
983 return 32767
984 perc = float(curmin-absmin)/float(absmax-absmin)
985 return int(perc*32767)
Jack Jansene3532151996-04-12 16:24:44 +0000986
987 # To be overridden:
988
989 def getscrollbarvalues(self):
990 return 0, 0
991
992 def scrollbar_callback(self, which, what, value):
993 print 'scroll', which, what, value
Jack Jansen7e0da901995-08-17 14:18:20 +0000994
995class DialogWindow(Window):
996 """A modeless dialog window"""
997
998 def open(self, resid):
Jack Jansened24cd22001-02-14 17:07:04 +0000999 self.dlg = GetNewDialog(resid, -1)
1000 self.wid = self.dlg.GetDialogWindow()
Jack Jansen7e0da901995-08-17 14:18:20 +00001001 self.do_postopen()
1002
1003 def close(self):
Jack Jansen7e0da901995-08-17 14:18:20 +00001004 self.do_postclose()
1005
1006 def do_itemhit(self, item, event):
Jack Jansened24cd22001-02-14 17:07:04 +00001007 print 'Dialog %s, item %d hit'%(self.dlg, item)
Jack Jansen7e0da901995-08-17 14:18:20 +00001008
1009 def do_rawupdate(self, window, event):
1010 pass
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +00001011
1012def ostypecode(x):
1013 "Convert a long int to the 4-character code it really is"
1014 s = ''
1015 for i in range(4):
1016 x, c = divmod(x, 256)
1017 s = chr(c) + s
1018 return s
1019
1020
1021class TestApp(Application):
1022
1023 "This class is used by the test() function"
1024
1025 def makeusermenus(self):
1026 self.filemenu = m = Menu(self.menubar, "File")
1027 self.saveitem = MenuItem(m, "Save", "S", self.save)
1028 Separator(m)
1029 self.optionsmenu = mm = SubMenu(m, "Options")
Jack Jansen13681b71999-12-14 15:45:53 +00001030 self.opt1 = CheckItem(mm, "Arguments", "A")
1031 self.opt2 = CheckItem(mm, "Being hit on the head lessons", (kMenuOptionModifier, "A"))
1032 self.opt3 = CheckItem(mm, "Complaints", (kMenuOptionModifier|kMenuNoCommandModifier, "A"))
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +00001033 Separator(m)
1034 self.quititem = MenuItem(m, "Quit", "Q", self.quit)
1035
1036 def save(self, *args):
1037 print "Save"
1038
1039 def quit(self, *args):
1040 raise self
1041
1042
1043def test():
1044 "Test program"
1045 app = TestApp()
1046 app.mainloop()
1047
1048
1049if __name__ == '__main__':
1050 test()