blob: c073abd7f4d5cd47626764a902384a669ff29b4e [file] [log] [blame]
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +00001"A sort of application framework for the Mac"
2
Jack Jansen7a583361995-08-14 12:39:54 +00003DEBUG=0
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +00004
5import MacOS
6import traceback
7
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +00008from AE import *
9from AppleEvents import *
Jack Jansen7a583361995-08-14 12:39:54 +000010from Ctl import *
11from Controls import *
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000012from Dlg import *
13from Dialogs import *
14from Evt import *
15from Events import *
16from Menu import *
17from Menus import *
Jack Jansen7a583361995-08-14 12:39:54 +000018from Qd import *
19from QuickDraw import *
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000020#from Res import *
21#from Resources import *
22#from Snd import *
23#from Sound import *
24from Win import *
25from Windows import *
Jack Jansenb1667ef1996-09-26 16:17:08 +000026import types
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000027
28import EasyDialogs
29
30kHighLevelEvent = 23 # Don't know what header file this should come from
Jack Jansene3532151996-04-12 16:24:44 +000031SCROLLBARWIDTH = 16 # Again, not a clue...
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000032
33
34# Map event 'what' field to strings
35eventname = {}
36eventname[1] = 'mouseDown'
37eventname[2] = 'mouseUp'
38eventname[3] = 'keyDown'
39eventname[4] = 'keyUp'
40eventname[5] = 'autoKey'
41eventname[6] = 'updateEvt'
42eventname[7] = 'diskEvt'
43eventname[8] = 'activateEvt'
44eventname[15] = 'osEvt'
45eventname[23] = 'kHighLevelEvent'
46
47# Map part codes returned by WhichWindow() to strings
48partname = {}
49partname[0] = 'inDesk'
50partname[1] = 'inMenuBar'
51partname[2] = 'inSysWindow'
52partname[3] = 'inContent'
53partname[4] = 'inDrag'
54partname[5] = 'inGrow'
55partname[6] = 'inGoAway'
56partname[7] = 'inZoomIn'
57partname[8] = 'inZoomOut'
58
Jack Jansenc4eec9f1996-04-19 16:00:28 +000059#
60# The useable portion of the screen
Jack Jansended835c1996-07-26 14:01:07 +000061# ## but what happens with multiple screens? jvr
Jack Jansenc4eec9f1996-04-19 16:00:28 +000062screenbounds = qd.screenBits.bounds
63screenbounds = screenbounds[0]+4, screenbounds[1]+4, \
64 screenbounds[2]-4, screenbounds[3]-4
65
Jack Jansended835c1996-07-26 14:01:07 +000066next_window_x = 16 # jvr
67next_window_y = 44 # jvr
Jack Jansenc4eec9f1996-04-19 16:00:28 +000068
69def windowbounds(width, height):
70 "Return sensible window bounds"
71 global next_window_x, next_window_y
72 r, b = next_window_x+width, next_window_y+height
73 if r > screenbounds[2]:
Jack Jansended835c1996-07-26 14:01:07 +000074 next_window_x = 16
Jack Jansenc4eec9f1996-04-19 16:00:28 +000075 if b > screenbounds[3]:
Jack Jansended835c1996-07-26 14:01:07 +000076 next_window_y = 44
Jack Jansenc4eec9f1996-04-19 16:00:28 +000077 l, t = next_window_x, next_window_y
78 r, b = next_window_x+width, next_window_y+height
Jack Jansended835c1996-07-26 14:01:07 +000079 next_window_x, next_window_y = next_window_x + 8, next_window_y + 20 # jvr
Jack Jansenc4eec9f1996-04-19 16:00:28 +000080 return l, t, r, b
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000081
Jack Jansen46341301996-08-28 13:53:07 +000082_watch = None
83def setwatchcursor():
84 global _watch
85
86 if _watch == None:
87 _watch = GetCursor(4).data
88 SetCursor(_watch)
89
90def setarrowcursor():
91 SetCursor(qd.arrow)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000092
93class Application:
94
95 "Application framework -- your application should be a derived class"
96
Jack Jansen647535d1996-09-17 12:35:43 +000097 def __init__(self, nomenubar=0):
Jack Jansend080edd1997-06-20 16:24:24 +000098 self._doing_asyncevents = 0
Jack Jansen647535d1996-09-17 12:35:43 +000099 self.quitting = 0
Jack Jansenb1667ef1996-09-26 16:17:08 +0000100 self.needmenubarredraw = 0
Jack Jansen7e0da901995-08-17 14:18:20 +0000101 self._windows = {}
Jack Jansen647535d1996-09-17 12:35:43 +0000102 if nomenubar:
103 self.menubar = None
104 else:
105 self.makemenubar()
Jack Jansend080edd1997-06-20 16:24:24 +0000106
107 def __del__(self):
108 if self._doing_asyncevents:
109 self._doing_asyncevents = 0
110 MacOS.SetEventHandler()
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000111
112 def makemenubar(self):
Jack Jansenb1667ef1996-09-26 16:17:08 +0000113 self.menubar = MenuBar(self)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000114 AppleMenu(self.menubar, self.getabouttext(), self.do_about)
115 self.makeusermenus()
Jack Jansenc8a99491996-01-08 23:50:13 +0000116
117 def makeusermenus(self):
118 self.filemenu = m = Menu(self.menubar, "File")
119 self._quititem = MenuItem(m, "Quit", "Q", self._quit)
120
121 def _quit(self, *args):
Jack Jansen647535d1996-09-17 12:35:43 +0000122 self.quitting = 1
Jack Jansen7e0da901995-08-17 14:18:20 +0000123
Jack Jansenc75e1d01996-12-23 17:22:40 +0000124 def cleanup(self):
125 for w in self._windows.values():
126 w.do_close()
127 return self._windows == {}
128
Jack Jansen7e0da901995-08-17 14:18:20 +0000129 def appendwindow(self, wid, window):
130 self._windows[wid] = window
131
132 def removewindow(self, wid):
133 del self._windows[wid]
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000134
135 def getabouttext(self):
136 return "About %s..." % self.__class__.__name__
137
138 def do_about(self, id, item, window, event):
139 EasyDialogs.Message("Hello, world!" + "\015(%s)" % self.__class__.__name__)
140
141 # The main event loop is broken up in several simple steps.
142 # This is done so you can override each individual part,
143 # if you have a need to do extra processing independent of the
144 # event type.
145 # Normally, however, you'd just define handlers for individual
146 # events.
147 # (XXX I'm not sure if using default parameter values is the right
148 # way to define the mask and wait time passed to WaitNextEvent.)
149
150 def mainloop(self, mask = everyEvent, wait = 0):
Jack Jansen647535d1996-09-17 12:35:43 +0000151 self.quitting = 0
Jack Jansen3368cb71997-06-12 10:51:18 +0000152 saveparams = apply(MacOS.SchedParams, self.schedparams)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000153 try:
Jack Jansen647535d1996-09-17 12:35:43 +0000154 while not self.quitting:
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000155 try:
156 self.do1event(mask, wait)
157 except (Application, SystemExit):
Jack Jansen647535d1996-09-17 12:35:43 +0000158 # Note: the raising of "self" is old-fashioned idiom to
159 # exit the mainloop. Calling _quit() is better for new
160 # applications.
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000161 break
162 finally:
Jack Jansen0c3e4b61999-12-03 16:08:50 +0000163 apply(MacOS.SchedParams, saveparams)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000164
Jack Jansen0c3e4b61999-12-03 16:08:50 +0000165 schedparams = (0, 0) # By default disable Python's event handling
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000166
Jack Jansend080edd1997-06-20 16:24:24 +0000167 def dopendingevents(self, mask = everyEvent):
168 """dopendingevents - Handle all pending events"""
169 while self.do1event(mask, wait=0):
170 pass
171
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000172 def do1event(self, mask = everyEvent, wait = 0):
Jack Jansen13dc4f71995-08-31 13:38:01 +0000173 ok, event = self.getevent(mask, wait)
174 if IsDialogEvent(event):
175 if self.do_dialogevent(event):
176 return
177 if ok:
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000178 self.dispatch(event)
Jack Jansen38186781995-11-10 14:48:36 +0000179 else:
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000180 self.idle(event)
Jack Jansen38186781995-11-10 14:48:36 +0000181
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000182 def idle(self, event):
Jack Jansen38186781995-11-10 14:48:36 +0000183 pass
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000184
185 def getevent(self, mask = everyEvent, wait = 0):
Jack Jansenb1667ef1996-09-26 16:17:08 +0000186 if self.needmenubarredraw:
187 DrawMenuBar()
188 self.needmenubarredraw = 0
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000189 ok, event = WaitNextEvent(mask, wait)
Jack Jansen13dc4f71995-08-31 13:38:01 +0000190 return ok, event
191
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000192 def dispatch(self, event):
Jack Jansend080edd1997-06-20 16:24:24 +0000193 # The following appears to be double work (already done in do1event)
194 # but we need it for asynchronous event handling
195 if IsDialogEvent(event):
196 if self.do_dialogevent(event):
197 return
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000198 (what, message, when, where, modifiers) = event
199 if eventname.has_key(what):
200 name = "do_" + eventname[what]
201 else:
202 name = "do_%d" % what
203 try:
204 handler = getattr(self, name)
205 except AttributeError:
206 handler = self.do_unknownevent
207 handler(event)
Jack Jansen7e0da901995-08-17 14:18:20 +0000208
Jack Jansend080edd1997-06-20 16:24:24 +0000209 def asyncevents(self, onoff):
210 """asyncevents - Set asynchronous event handling on or off"""
211 old = self._doing_asyncevents
212 if old:
213 MacOS.SetEventHandler()
214 apply(MacOS.SchedParams, self.schedparams)
215 if onoff:
216 MacOS.SetEventHandler(self.dispatch)
217 doint, dummymask, benice, howoften, bgyield = \
218 self.schedparams
219 MacOS.SchedParams(doint, everyEvent, benice,
220 howoften, bgyield)
221 self._doing_asyncevents = onoff
222 return old
223
Jack Jansen7e0da901995-08-17 14:18:20 +0000224 def do_dialogevent(self, event):
225 gotone, window, item = DialogSelect(event)
226 if gotone:
227 if self._windows.has_key(window):
Jack Jansen13dc4f71995-08-31 13:38:01 +0000228 self._windows[window].do_itemhit(item, event)
Jack Jansen7e0da901995-08-17 14:18:20 +0000229 else:
230 print 'Dialog event for unknown dialog'
Jack Jansen13dc4f71995-08-31 13:38:01 +0000231 return 1
232 return 0
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000233
234 def do_mouseDown(self, event):
235 (what, message, when, where, modifiers) = event
Jack Jansen7e0da901995-08-17 14:18:20 +0000236 partcode, wid = FindWindow(where)
237
238 #
239 # Find the correct name.
240 #
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000241 if partname.has_key(partcode):
242 name = "do_" + partname[partcode]
243 else:
244 name = "do_%d" % partcode
Jack Jansen7e0da901995-08-17 14:18:20 +0000245
246 if wid == None:
247 # No window, or a non-python window
248 try:
249 handler = getattr(self, name)
250 except AttributeError:
251 # Not menubar or something, so assume someone
252 # else's window
253 MacOS.HandleEvent(event)
254 return
255 elif self._windows.has_key(wid):
256 # It is a window. Hand off to correct window.
257 window = self._windows[wid]
258 try:
259 handler = getattr(window, name)
260 except AttributeError:
261 handler = self.do_unknownpartcode
262 else:
263 # It is a python-toolbox window, but not ours.
264 handler = self.do_unknownwindow
265 handler(partcode, wid, event)
266
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000267 def do_inSysWindow(self, partcode, window, event):
Jack Jansen7a583361995-08-14 12:39:54 +0000268 MacOS.HandleEvent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000269
270 def do_inDesk(self, partcode, window, event):
Jack Jansen7a583361995-08-14 12:39:54 +0000271 MacOS.HandleEvent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000272
273 def do_inMenuBar(self, partcode, window, event):
Jack Jansen647535d1996-09-17 12:35:43 +0000274 if not self.menubar:
275 MacOS.HandleEvent(event)
276 return
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000277 (what, message, when, where, modifiers) = event
278 result = MenuSelect(where)
279 id = (result>>16) & 0xffff # Hi word
280 item = result & 0xffff # Lo word
281 self.do_rawmenu(id, item, window, event)
282
283 def do_rawmenu(self, id, item, window, event):
284 try:
285 self.do_menu(id, item, window, event)
286 finally:
287 HiliteMenu(0)
288
289 def do_menu(self, id, item, window, event):
290 self.menubar.dispatch(id, item, window, event)
291
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000292
293 def do_unknownpartcode(self, partcode, window, event):
294 (what, message, when, where, modifiers) = event
Jack Jansen7a583361995-08-14 12:39:54 +0000295 if DEBUG: print "Mouse down at global:", where
296 if DEBUG: print "\tUnknown part code:", partcode
Jack Jansen7e0da901995-08-17 14:18:20 +0000297 if DEBUG: print "\tEvent:", self.printevent(event)
298 MacOS.HandleEvent(event)
299
300 def do_unknownwindow(self, partcode, window, event):
301 if DEBUG: print 'Unknown window:', window
Jack Jansen7a583361995-08-14 12:39:54 +0000302 MacOS.HandleEvent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000303
304 def do_keyDown(self, event):
305 self.do_key(event)
306
307 def do_autoKey(self, event):
308 if not event[-1] & cmdKey:
309 self.do_key(event)
310
311 def do_key(self, event):
312 (what, message, when, where, modifiers) = event
313 c = chr(message & charCodeMask)
314 if modifiers & cmdKey:
315 if c == '.':
316 raise self
317 else:
Jack Jansen647535d1996-09-17 12:35:43 +0000318 if not self.menubar:
319 MacOS.HandleEvent(event)
320 return
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000321 result = MenuKey(ord(c))
322 id = (result>>16) & 0xffff # Hi word
323 item = result & 0xffff # Lo word
324 if id:
325 self.do_rawmenu(id, item, None, event)
Jack Jansen7e0da901995-08-17 14:18:20 +0000326# elif c == 'w':
327# w = FrontWindow()
328# if w:
329# self.do_close(w)
330# else:
331# if DEBUG: print 'Command-W without front window'
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000332 else:
Jack Jansen7a583361995-08-14 12:39:54 +0000333 if DEBUG: print "Command-" +`c`
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000334 else:
Jack Jansen7e0da901995-08-17 14:18:20 +0000335 # See whether the front window wants it
336 w = FrontWindow()
337 if w and self._windows.has_key(w):
338 window = self._windows[w]
339 try:
340 do_char = window.do_char
341 except AttributeError:
342 do_char = self.do_char
Jack Jansen6f47bf41995-12-12 15:03:35 +0000343 do_char(c, event)
344 # else it wasn't for us, sigh...
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000345
346 def do_char(self, c, event):
Jack Jansen7a583361995-08-14 12:39:54 +0000347 if DEBUG: print "Character", `c`
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000348
349 def do_updateEvt(self, event):
Jack Jansen7e0da901995-08-17 14:18:20 +0000350 (what, message, when, where, modifiers) = event
351 wid = WhichWindow(message)
352 if wid and self._windows.has_key(wid):
353 window = self._windows[wid]
354 window.do_rawupdate(wid, event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000355 else:
Jack Jansen7a583361995-08-14 12:39:54 +0000356 MacOS.HandleEvent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000357
Jack Jansen7e0da901995-08-17 14:18:20 +0000358 def do_activateEvt(self, event):
359 (what, message, when, where, modifiers) = event
Just van Rossum5763e071999-01-27 14:22:11 +0000360 wid = WhichWindow(message)
Jack Jansen7e0da901995-08-17 14:18:20 +0000361 if wid and self._windows.has_key(wid):
362 window = self._windows[wid]
363 window.do_activate(modifiers & 1, event)
364 else:
365 MacOS.HandleEvent(event)
Just van Rossum5763e071999-01-27 14:22:11 +0000366
Jack Jansen7e0da901995-08-17 14:18:20 +0000367 def do_osEvt(self, event):
368 (what, message, when, where, modifiers) = event
369 which = (message >> 24) & 0xff
370 if which == 1: # suspend/resume
371 self.do_suspendresume(event)
372 else:
373 if DEBUG:
374 print 'unknown osEvt:',
375 self.printevent(event)
Just van Rossum5763e071999-01-27 14:22:11 +0000376
Jack Jansen7e0da901995-08-17 14:18:20 +0000377 def do_suspendresume(self, event):
Jack Jansen7e0da901995-08-17 14:18:20 +0000378 (what, message, when, where, modifiers) = event
Just van Rossum5763e071999-01-27 14:22:11 +0000379 wid = FrontWindow()
380 if wid and self._windows.has_key(wid):
381 window = self._windows[wid]
Just van Rossum1a5eb041999-12-15 14:55:16 +0000382 window.do_activate(message & 1, event)
Just van Rossum5763e071999-01-27 14:22:11 +0000383
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000384 def do_kHighLevelEvent(self, event):
385 (what, message, when, where, modifiers) = event
Jack Jansen7a583361995-08-14 12:39:54 +0000386 if DEBUG:
387 print "High Level Event:",
388 self.printevent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000389 try:
390 AEProcessAppleEvent(event)
391 except:
392 print "AEProcessAppleEvent error:"
393 traceback.print_exc()
394
395 def do_unknownevent(self, event):
Jack Jansen7e0da901995-08-17 14:18:20 +0000396 if DEBUG:
397 print "Unhandled event:",
398 self.printevent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000399
400 def printevent(self, event):
401 (what, message, when, where, modifiers) = event
402 nicewhat = `what`
403 if eventname.has_key(what):
404 nicewhat = eventname[what]
405 print nicewhat,
406 if what == kHighLevelEvent:
407 h, v = where
408 print `ostypecode(message)`, hex(when), `ostypecode(h | (v<<16))`,
409 else:
410 print hex(message), hex(when), where,
411 print hex(modifiers)
412
413
414class MenuBar:
415 """Represent a set of menus in a menu bar.
416
417 Interface:
418
419 - (constructor)
420 - (destructor)
421 - addmenu
422 - addpopup (normally used internally)
423 - dispatch (called from Application)
424 """
425
426 nextid = 1 # Necessarily a class variable
427
428 def getnextid(self):
Jack Jansenb1667ef1996-09-26 16:17:08 +0000429 id = MenuBar.nextid
430 MenuBar.nextid = id+1
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000431 return id
432
Jack Jansenb1667ef1996-09-26 16:17:08 +0000433 def __init__(self, parent=None):
434 self.parent = parent
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000435 ClearMenuBar()
436 self.bar = GetMenuBar()
437 self.menus = {}
438
Jack Jansenb1667ef1996-09-26 16:17:08 +0000439 # XXX necessary?
440 def close(self):
441 self.parent = None
442 self.bar = None
Jack Jansen7b56aad1998-02-20 15:51:39 +0000443 self.menus = None
Jack Jansenb1667ef1996-09-26 16:17:08 +0000444
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000445 def addmenu(self, title, after = 0):
446 id = self.getnextid()
Jack Jansene3532151996-04-12 16:24:44 +0000447 if DEBUG: print 'Newmenu', title, id # XXXX
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000448 m = NewMenu(id, title)
449 m.InsertMenu(after)
Jack Jansenbb6193c1998-05-06 15:33:09 +0000450 if after >= 0:
451 if self.parent:
452 self.parent.needmenubarredraw = 1
453 else:
454 DrawMenuBar()
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000455 return id, m
Jack Jansendb9ff361996-03-12 13:32:03 +0000456
457 def delmenu(self, id):
Jack Jansene3532151996-04-12 16:24:44 +0000458 if DEBUG: print 'Delmenu', id # XXXX
Jack Jansendb9ff361996-03-12 13:32:03 +0000459 DeleteMenu(id)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000460
461 def addpopup(self, title = ''):
462 return self.addmenu(title, -1)
Jack Jansenb1667ef1996-09-26 16:17:08 +0000463
464# Useless:
465# def install(self):
466# if not self.bar: return
467# SetMenuBar(self.bar)
468# if self.parent:
469# self.parent.needmenubarredraw = 1
470# else:
471# DrawMenuBar()
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000472
Jack Jansenb1667ef1996-09-26 16:17:08 +0000473 def fixmenudimstate(self):
474 for m in self.menus.keys():
475 menu = self.menus[m]
476 if menu.__class__ == FrameWork.AppleMenu:
477 continue
478 for i in range(len(menu.items)):
479 label, shortcut, callback, kind = menu.items[i]
480 if type(callback) == types.StringType:
481 wid = Win.FrontWindow()
482 if wid and self.parent._windows.has_key(wid):
483 window = self.parent._windows[wid]
484 if hasattr(window, "domenu_" + callback):
485 menu.menu.EnableItem(i + 1)
486 elif hasattr(self.parent, "domenu_" + callback):
487 menu.menu.EnableItem(i + 1)
488 else:
489 menu.menu.DisableItem(i + 1)
490 elif hasattr(self.parent, "domenu_" + callback):
491 menu.menu.EnableItem(i + 1)
492 else:
493 menu.menu.DisableItem(i + 1)
494 elif callback:
495 pass
496
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000497 def dispatch(self, id, item, window, event):
498 if self.menus.has_key(id):
499 self.menus[id].dispatch(id, item, window, event)
500 else:
Jack Jansen7a583361995-08-14 12:39:54 +0000501 if DEBUG: print "MenuBar.dispatch(%d, %d, %s, %s)" % \
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000502 (id, item, window, event)
503
504
505# XXX Need a way to get menus as resources and bind them to callbacks
506
507class Menu:
508 "One menu."
509
510 def __init__(self, bar, title, after=0):
511 self.bar = bar
512 self.id, self.menu = self.bar.addmenu(title, after)
513 bar.menus[self.id] = self
514 self.items = []
Jack Jansen341d1fe1998-10-15 15:29:16 +0000515 self._parent = None
Jack Jansendb9ff361996-03-12 13:32:03 +0000516
517 def delete(self):
518 self.bar.delmenu(self.id)
519 del self.bar.menus[self.id]
520 del self.bar
521 del self.items
522 del self.menu
523 del self.id
Jack Jansen341d1fe1998-10-15 15:29:16 +0000524 del self._parent
Jack Jansen5c440271998-07-13 13:41:02 +0000525
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000526 def additem(self, label, shortcut=None, callback=None, kind=None):
527 self.menu.AppendMenu('x') # add a dummy string
528 self.items.append(label, shortcut, callback, kind)
529 item = len(self.items)
Jack Jansene4b40381995-07-17 13:25:15 +0000530 self.menu.SetMenuItemText(item, label) # set the actual text
Jack Jansen13681b71999-12-14 15:45:53 +0000531 if shortcut and type(shortcut) == type(()):
532 modifiers, char = shortcut[:2]
533 self.menu.SetItemCmd(item, ord(char))
534 self.menu.SetMenuItemModifiers(item, modifiers)
535 if len(shortcut) > 2:
536 self.menu.SetMenuItem
537 elif shortcut:
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000538 self.menu.SetItemCmd(item, ord(shortcut))
539 return item
Jack Jansen5c440271998-07-13 13:41:02 +0000540
541 def delitem(self, item):
542 if item != len(self.items):
543 raise 'Can only delete last item of a menu'
544 self.menu.DeleteMenuItem(item)
545 del self.items[item-1]
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000546
547 def addcheck(self, label, shortcut=None, callback=None):
548 return self.additem(label, shortcut, callback, 'check')
549
550 def addradio(self, label, shortcut=None, callback=None):
551 return self.additem(label, shortcut, callback, 'radio')
552
553 def addseparator(self):
554 self.menu.AppendMenu('(-')
555 self.items.append('', None, None, 'separator')
556
557 def addsubmenu(self, label, title=''):
558 sub = Menu(self.bar, title, -1)
559 item = self.additem(label, '\x1B', None, 'submenu')
560 self.menu.SetItemMark(item, sub.id)
Jack Jansen341d1fe1998-10-15 15:29:16 +0000561 sub._parent = self
562 sub._parent_item = item
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000563 return sub
564
565 def dispatch(self, id, item, window, event):
Jack Jansenb1667ef1996-09-26 16:17:08 +0000566 title, shortcut, callback, mtype = self.items[item-1]
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000567 if callback:
Jack Jansenb1667ef1996-09-26 16:17:08 +0000568 if not self.bar.parent or type(callback) <> types.StringType:
569 menuhandler = callback
570 else:
571 # callback is string
572 wid = Win.FrontWindow()
573 if wid and self.bar.parent._windows.has_key(wid):
574 window = self.bar.parent._windows[wid]
575 if hasattr(window, "domenu_" + callback):
576 menuhandler = getattr(window, "domenu_" + callback)
577 elif hasattr(self.bar.parent, "domenu_" + callback):
578 menuhandler = getattr(self.bar.parent, "domenu_" + callback)
579 else:
580 # nothing we can do. we shouldn't have come this far
581 # since the menu item should have been disabled...
582 return
583 elif hasattr(self.bar.parent, "domenu_" + callback):
584 menuhandler = getattr(self.bar.parent, "domenu_" + callback)
585 else:
586 # nothing we can do. we shouldn't have come this far
587 # since the menu item should have been disabled...
588 return
589 menuhandler(id, item, window, event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000590
Jack Jansencef2c591996-04-11 15:39:01 +0000591 def enable(self, onoff):
592 if onoff:
593 self.menu.EnableItem(0)
Jack Jansen341d1fe1998-10-15 15:29:16 +0000594 if self._parent:
595 self._parent.menu.EnableItem(self._parent_item)
Jack Jansencef2c591996-04-11 15:39:01 +0000596 else:
597 self.menu.DisableItem(0)
Jack Jansen341d1fe1998-10-15 15:29:16 +0000598 if self._parent:
599 self._parent.menu.DisableItem(self._parent_item)
Jack Jansen5c440271998-07-13 13:41:02 +0000600 if self.bar and self.bar.parent:
601 self.bar.parent.needmenubarredraw = 1
Jack Jansenbb6193c1998-05-06 15:33:09 +0000602
603class PopupMenu(Menu):
604 def __init__(self, bar):
605 Menu.__init__(self, bar, '(popup)', -1)
606
607 def popup(self, x, y, event, default=1, window=None):
608 # NOTE that x and y are global coordinates, and they should probably
609 # be topleft of the button the user clicked (not mouse-coordinates),
610 # so the popup nicely overlaps.
611 reply = self.menu.PopUpMenuSelect(x, y, default)
612 if not reply:
613 return
614 id = (reply & 0xffff0000) >> 16
615 item = reply & 0xffff
616 if not window:
617 wid = Win.FrontWindow()
618 try:
619 window = self.bar.parent._windows[wid]
620 except:
621 pass # If we can't find the window we pass None
622 self.dispatch(id, item, window, event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000623
624class MenuItem:
625 def __init__(self, menu, title, shortcut=None, callback=None, kind=None):
626 self.item = menu.additem(title, shortcut, callback)
Jack Jansendb9ff361996-03-12 13:32:03 +0000627 self.menu = menu
628
Jack Jansen5c440271998-07-13 13:41:02 +0000629 def delete(self):
630 self.menu.delitem(self.item)
631 del self.menu
632 del self.item
633
Jack Jansendb9ff361996-03-12 13:32:03 +0000634 def check(self, onoff):
635 self.menu.menu.CheckItem(self.item, onoff)
Jack Jansencef2c591996-04-11 15:39:01 +0000636
637 def enable(self, onoff):
638 if onoff:
639 self.menu.menu.EnableItem(self.item)
640 else:
641 self.menu.menu.DisableItem(self.item)
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000642
643 def settext(self, text):
644 self.menu.menu.SetMenuItemText(self.item, text)
Jack Jansendb9ff361996-03-12 13:32:03 +0000645
Jack Jansen0f6dc5b1996-04-23 16:18:33 +0000646 def setstyle(self, style):
647 self.menu.menu.SetItemStyle(self.item, style)
648
649 def seticon(self, icon):
650 self.menu.menu.SetItemIcon(self.item, icon)
651
652 def setcmd(self, cmd):
653 self.menu.menu.SetItemCmd(self.item, cmd)
654
655 def setmark(self, cmd):
656 self.menu.menu.SetItemMark(self.item, cmd)
657
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000658
659class RadioItem(MenuItem):
660 def __init__(self, menu, title, shortcut=None, callback=None):
661 MenuItem.__init__(self, menu, title, shortcut, callback, 'radio')
662
663class CheckItem(MenuItem):
664 def __init__(self, menu, title, shortcut=None, callback=None):
665 MenuItem.__init__(self, menu, title, shortcut, callback, 'check')
666
667def Separator(menu):
668 menu.addseparator()
669
670def SubMenu(menu, label, title=''):
671 return menu.addsubmenu(label, title)
672
673
674class AppleMenu(Menu):
675
676 def __init__(self, bar, abouttext="About me...", aboutcallback=None):
677 Menu.__init__(self, bar, "\024")
678 self.additem(abouttext, None, aboutcallback)
679 self.addseparator()
Jack Jansene4b40381995-07-17 13:25:15 +0000680 self.menu.AppendResMenu('DRVR')
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000681
682 def dispatch(self, id, item, window, event):
683 if item == 1:
684 Menu.dispatch(self, id, item, window, event)
685 else:
Jack Jansenc8a99491996-01-08 23:50:13 +0000686 name = self.menu.GetMenuItemText(item)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000687 OpenDeskAcc(name)
688
Jack Jansen7e0da901995-08-17 14:18:20 +0000689class Window:
690 """A single window belonging to an application"""
691
692 def __init__(self, parent):
693 self.wid = None
694 self.parent = parent
695
Jack Jansenc8a99491996-01-08 23:50:13 +0000696 def open(self, bounds=(40, 40, 400, 400), resid=None):
697 if resid <> None:
698 self.wid = GetNewWindow(resid, -1)
699 else:
700 self.wid = NewWindow(bounds, self.__class__.__name__, 1,
Jack Jansended835c1996-07-26 14:01:07 +0000701 8, -1, 1, 0) # changed to proc id 8 to include zoom box. jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000702 self.do_postopen()
703
704 def do_postopen(self):
705 """Tell our parent we exist"""
706 self.parent.appendwindow(self.wid, self)
707
708 def close(self):
Jack Jansen7e0da901995-08-17 14:18:20 +0000709 self.do_postclose()
710
711 def do_postclose(self):
712 self.parent.removewindow(self.wid)
713 self.parent = None
714 self.wid = None
Jack Jansenc8a99491996-01-08 23:50:13 +0000715
716 def SetPort(self):
717 # Convinience method
718 SetPort(self.wid)
Jack Jansen7e0da901995-08-17 14:18:20 +0000719
720 def do_inDrag(self, partcode, window, event):
721 where = event[3]
722 window.DragWindow(where, self.draglimit)
723
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000724 draglimit = screenbounds
Jack Jansen7e0da901995-08-17 14:18:20 +0000725
726 def do_inGoAway(self, partcode, window, event):
727 where = event[3]
728 if window.TrackGoAway(where):
729 self.close()
730
731 def do_inZoom(self, partcode, window, event):
732 (what, message, when, where, modifiers) = event
733 if window.TrackBox(where, partcode):
734 window.ZoomWindow(partcode, 1)
Jack Jansended835c1996-07-26 14:01:07 +0000735 rect = window.GetWindowUserState() # so that zoom really works... jvr
736 self.do_postresize(rect[2] - rect[0], rect[3] - rect[1], window) # jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000737
738 def do_inZoomIn(self, partcode, window, event):
739 SetPort(window) # !!!
740 self.do_inZoom(partcode, window, event)
741
742 def do_inZoomOut(self, partcode, window, event):
743 SetPort(window) # !!!
744 self.do_inZoom(partcode, window, event)
745
746 def do_inGrow(self, partcode, window, event):
747 (what, message, when, where, modifiers) = event
748 result = window.GrowWindow(where, self.growlimit)
749 if result:
750 height = (result>>16) & 0xffff # Hi word
751 width = result & 0xffff # Lo word
752 self.do_resize(width, height, window)
753
Jack Jansended835c1996-07-26 14:01:07 +0000754 growlimit = (50, 50, screenbounds[2] - screenbounds[0], screenbounds[3] - screenbounds[1]) # jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000755
756 def do_resize(self, width, height, window):
Jack Jansended835c1996-07-26 14:01:07 +0000757 l, t, r, b = self.wid.GetWindowPort().portRect # jvr, forGrowIcon
758 self.SetPort() # jvr
759 InvalRect((r - SCROLLBARWIDTH + 1, b - SCROLLBARWIDTH + 1, r, b)) # jvr
760 window.SizeWindow(width, height, 1) # changed updateFlag to true jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000761 self.do_postresize(width, height, window)
762
763 def do_postresize(self, width, height, window):
764 SetPort(window)
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000765 InvalRect(window.GetWindowPort().portRect)
Jack Jansen7e0da901995-08-17 14:18:20 +0000766
767 def do_inContent(self, partcode, window, event):
768 #
769 # If we're not frontmost, select ourselves and wait for
770 # the activate event.
771 #
772 if FrontWindow() <> window:
773 window.SelectWindow()
774 return
775 # We are. Handle the event.
776 (what, message, when, where, modifiers) = event
777 SetPort(window)
778 local = GlobalToLocal(where)
779 self.do_contentclick(local, modifiers, event)
780
781 def do_contentclick(self, local, modifiers, event):
Jack Jansended835c1996-07-26 14:01:07 +0000782 if DEBUG:
783 print 'Click in contents at %s, modifiers %s'%(local, modifiers)
Jack Jansen7e0da901995-08-17 14:18:20 +0000784
785 def do_rawupdate(self, window, event):
786 if DEBUG: print "raw update for", window
Jack Jansenda38f2d1995-11-14 10:15:42 +0000787 SetPort(window)
Jack Jansen7e0da901995-08-17 14:18:20 +0000788 window.BeginUpdate()
789 self.do_update(window, event)
790 window.EndUpdate()
791
792 def do_update(self, window, event):
Jack Jansended835c1996-07-26 14:01:07 +0000793 if DEBUG:
794 import time
795 for i in range(8):
796 time.sleep(0.1)
797 InvertRgn(window.GetWindowPort().visRgn)
798 FillRgn(window.GetWindowPort().visRgn, qd.gray)
799 else:
800 EraseRgn(window.GetWindowPort().visRgn)
Jack Jansen7e0da901995-08-17 14:18:20 +0000801
802 def do_activate(self, activate, event):
803 if DEBUG: print 'Activate %d for %s'%(activate, self.wid)
804
805class ControlsWindow(Window):
806
807 def do_rawupdate(self, window, event):
808 if DEBUG: print "raw update for", window
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000809 SetPort(window)
Jack Jansen7e0da901995-08-17 14:18:20 +0000810 window.BeginUpdate()
811 self.do_update(window, event)
Jack Jansended835c1996-07-26 14:01:07 +0000812 #DrawControls(window) # jvr
813 UpdateControls(window, window.GetWindowPort().visRgn) # jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000814 window.DrawGrowIcon()
815 window.EndUpdate()
816
817 def do_controlhit(self, window, control, pcode, event):
818 if DEBUG: print "control hit in", window, "on", control, "; pcode =", pcode
819
820 def do_inContent(self, partcode, window, event):
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000821 if FrontWindow() <> window:
822 window.SelectWindow()
823 return
Jack Jansen7e0da901995-08-17 14:18:20 +0000824 (what, message, when, where, modifiers) = event
Jack Jansenda38f2d1995-11-14 10:15:42 +0000825 SetPort(window) # XXXX Needed?
Jack Jansen7e0da901995-08-17 14:18:20 +0000826 local = GlobalToLocal(where)
Jack Jansen41e825a1998-05-28 14:22:48 +0000827 pcode, control = FindControl(local, window)
828 if pcode and control:
829 self.do_rawcontrolhit(window, control, pcode, local, event)
Jack Jansen7e0da901995-08-17 14:18:20 +0000830 else:
831 if DEBUG: print "FindControl(%s, %s) -> (%s, %s)" % \
Jack Jansen41e825a1998-05-28 14:22:48 +0000832 (local, window, pcode, control)
Jack Jansene3532151996-04-12 16:24:44 +0000833 self.do_contentclick(local, modifiers, event)
834
Jack Jansen41e825a1998-05-28 14:22:48 +0000835 def do_rawcontrolhit(self, window, control, pcode, local, event):
836 pcode = control.TrackControl(local)
837 if pcode:
838 self.do_controlhit(window, control, pcode, event)
839
Jack Jansene3532151996-04-12 16:24:44 +0000840class ScrolledWindow(ControlsWindow):
841 def __init__(self, parent):
842 self.barx = self.bary = None
Jack Jansen7bfc8751996-04-16 14:35:43 +0000843 self.barx_enabled = self.bary_enabled = 1
844 self.activated = 1
Jack Jansene3532151996-04-12 16:24:44 +0000845 ControlsWindow.__init__(self, parent)
846
847 def scrollbars(self, wantx=1, wanty=1):
848 SetPort(self.wid)
849 self.barx = self.bary = None
Jack Jansen7bfc8751996-04-16 14:35:43 +0000850 self.barx_enabled = self.bary_enabled = 1
Jack Jansene3532151996-04-12 16:24:44 +0000851 x0, y0, x1, y1 = self.wid.GetWindowPort().portRect
852 vx, vy = self.getscrollbarvalues()
Jack Jansen7bfc8751996-04-16 14:35:43 +0000853 if vx == None: self.barx_enabled, vx = 0, 0
854 if vy == None: self.bary_enabled, vy = 0, 0
Jack Jansene3532151996-04-12 16:24:44 +0000855 if wantx:
856 rect = x0-1, y1-(SCROLLBARWIDTH-1), x1-(SCROLLBARWIDTH-2), y1+1
857 self.barx = NewControl(self.wid, rect, "", 1, vx, 0, 32767, 16, 0)
Jack Jansen7bfc8751996-04-16 14:35:43 +0000858 if not self.barx_enabled: self.barx.HiliteControl(255)
859## InvalRect(rect)
Jack Jansene3532151996-04-12 16:24:44 +0000860 if wanty:
861 rect = x1-(SCROLLBARWIDTH-1), y0-1, x1+1, y1-(SCROLLBARWIDTH-2)
862 self.bary = NewControl(self.wid, rect, "", 1, vy, 0, 32767, 16, 0)
Jack Jansen7bfc8751996-04-16 14:35:43 +0000863 if not self.bary_enabled: self.bary.HiliteControl(255)
864## InvalRect(rect)
Jack Jansene3532151996-04-12 16:24:44 +0000865
866 def do_postclose(self):
867 self.barx = self.bary = None
868 ControlsWindow.do_postclose(self)
869
870 def do_activate(self, onoff, event):
Jack Jansen7bfc8751996-04-16 14:35:43 +0000871 self.activated = onoff
Jack Jansene3532151996-04-12 16:24:44 +0000872 if onoff:
Jack Jansen7bfc8751996-04-16 14:35:43 +0000873 if self.barx and self.barx_enabled:
Jack Jansended835c1996-07-26 14:01:07 +0000874 self.barx.ShowControl() # jvr
Jack Jansen7bfc8751996-04-16 14:35:43 +0000875 if self.bary and self.bary_enabled:
Jack Jansended835c1996-07-26 14:01:07 +0000876 self.bary.ShowControl() # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000877 else:
Jack Jansen7bfc8751996-04-16 14:35:43 +0000878 if self.barx:
Jack Jansended835c1996-07-26 14:01:07 +0000879 self.barx.HideControl() # jvr; An inactive window should have *hidden*
880 # scrollbars, not just dimmed (no matter what
881 # BBEdit does... look at the Finder)
Jack Jansen7bfc8751996-04-16 14:35:43 +0000882 if self.bary:
Jack Jansended835c1996-07-26 14:01:07 +0000883 self.bary.HideControl() # jvr
884 self.wid.DrawGrowIcon() # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000885
886 def do_postresize(self, width, height, window):
887 l, t, r, b = self.wid.GetWindowPort().portRect
Jack Jansended835c1996-07-26 14:01:07 +0000888 self.SetPort()
Jack Jansene3532151996-04-12 16:24:44 +0000889 if self.barx:
Jack Jansended835c1996-07-26 14:01:07 +0000890 self.barx.HideControl() # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000891 self.barx.MoveControl(l-1, b-(SCROLLBARWIDTH-1))
Jack Jansended835c1996-07-26 14:01:07 +0000892 self.barx.SizeControl((r-l)-(SCROLLBARWIDTH-3), SCROLLBARWIDTH) # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000893 if self.bary:
Jack Jansended835c1996-07-26 14:01:07 +0000894 self.bary.HideControl() # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000895 self.bary.MoveControl(r-(SCROLLBARWIDTH-1), t-1)
Jack Jansended835c1996-07-26 14:01:07 +0000896 self.bary.SizeControl(SCROLLBARWIDTH, (b-t)-(SCROLLBARWIDTH-3)) # jvr
897 if self.barx:
898 self.barx.ShowControl() # jvr
899 ValidRect((l, b - SCROLLBARWIDTH + 1, r - SCROLLBARWIDTH + 2, b)) # jvr
900 if self.bary:
901 self.bary.ShowControl() # jvr
902 ValidRect((r - SCROLLBARWIDTH + 1, t, r, b - SCROLLBARWIDTH + 2)) # jvr
903 InvalRect((r - SCROLLBARWIDTH + 1, b - SCROLLBARWIDTH + 1, r, b)) # jvr, growicon
Jack Jansene3532151996-04-12 16:24:44 +0000904
Jack Jansen41e825a1998-05-28 14:22:48 +0000905
906 def do_rawcontrolhit(self, window, control, pcode, local, event):
Jack Jansene3532151996-04-12 16:24:44 +0000907 if control == self.barx:
Jack Jansene3532151996-04-12 16:24:44 +0000908 which = 'x'
909 elif control == self.bary:
Jack Jansene3532151996-04-12 16:24:44 +0000910 which = 'y'
911 else:
912 return 0
Jack Jansen41e825a1998-05-28 14:22:48 +0000913 if pcode in (inUpButton, inDownButton, inPageUp, inPageDown):
914 # We do the work for the buttons and grey area in the tracker
915 dummy = control.TrackControl(local, self.do_controltrack)
916 else:
917 # but the thumb is handled here
918 pcode = control.TrackControl(local)
919 if pcode == inThumb:
920 value = control.GetControlValue()
921 print 'setbars', which, value #DBG
922 self.scrollbar_callback(which, 'set', value)
923 self.updatescrollbars()
924 else:
925 print 'funny part', pcode #DBG
926 return 1
927
928 def do_controltrack(self, control, pcode):
929 if control == self.barx:
930 which = 'x'
931 elif control == self.bary:
932 which = 'y'
933 else:
934 return
935
Jack Jansene3532151996-04-12 16:24:44 +0000936 if pcode == inUpButton:
937 what = '-'
938 elif pcode == inDownButton:
939 what = '+'
940 elif pcode == inPageUp:
941 what = '--'
942 elif pcode == inPageDown:
943 what = '++'
944 else:
Jack Jansen41e825a1998-05-28 14:22:48 +0000945 return
946 self.scrollbar_callback(which, what, None)
Jack Jansene3532151996-04-12 16:24:44 +0000947 self.updatescrollbars()
Jack Jansene3532151996-04-12 16:24:44 +0000948
949 def updatescrollbars(self):
950 SetPort(self.wid)
951 vx, vy = self.getscrollbarvalues()
952 if self.barx:
Jack Jansen7bfc8751996-04-16 14:35:43 +0000953 if vx == None:
954 self.barx.HiliteControl(255)
955 self.barx_enabled = 0
956 else:
957 if not self.barx_enabled:
958 self.barx_enabled = 1
959 if self.activated:
960 self.barx.HiliteControl(0)
961 self.barx.SetControlValue(vx)
Jack Jansene3532151996-04-12 16:24:44 +0000962 if self.bary:
Jack Jansen7bfc8751996-04-16 14:35:43 +0000963 if vy == None:
964 self.bary.HiliteControl(255)
965 self.bary_enabled = 0
966 else:
967 if not self.bary_enabled:
968 self.bary_enabled = 1
969 if self.activated:
970 self.bary.HiliteControl(0)
971 self.bary.SetControlValue(vy)
972
973 # Auxiliary function: convert standard text/image/etc coordinate
974 # to something palatable as getscrollbarvalues() return
975 def scalebarvalue(self, absmin, absmax, curmin, curmax):
976 if curmin <= absmin and curmax >= absmax:
977 return None
978 if curmin <= absmin:
979 return 0
980 if curmax >= absmax:
981 return 32767
982 perc = float(curmin-absmin)/float(absmax-absmin)
983 return int(perc*32767)
Jack Jansene3532151996-04-12 16:24:44 +0000984
985 # To be overridden:
986
987 def getscrollbarvalues(self):
988 return 0, 0
989
990 def scrollbar_callback(self, which, what, value):
991 print 'scroll', which, what, value
Jack Jansen7e0da901995-08-17 14:18:20 +0000992
993class DialogWindow(Window):
994 """A modeless dialog window"""
995
996 def open(self, resid):
997 self.wid = GetNewDialog(resid, -1)
998 self.do_postopen()
999
1000 def close(self):
Jack Jansen7e0da901995-08-17 14:18:20 +00001001 self.do_postclose()
1002
1003 def do_itemhit(self, item, event):
1004 print 'Dialog %s, item %d hit'%(self.wid, item)
1005
1006 def do_rawupdate(self, window, event):
1007 pass
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +00001008
1009def ostypecode(x):
1010 "Convert a long int to the 4-character code it really is"
1011 s = ''
1012 for i in range(4):
1013 x, c = divmod(x, 256)
1014 s = chr(c) + s
1015 return s
1016
1017
1018class TestApp(Application):
1019
1020 "This class is used by the test() function"
1021
1022 def makeusermenus(self):
1023 self.filemenu = m = Menu(self.menubar, "File")
1024 self.saveitem = MenuItem(m, "Save", "S", self.save)
1025 Separator(m)
1026 self.optionsmenu = mm = SubMenu(m, "Options")
Jack Jansen13681b71999-12-14 15:45:53 +00001027 self.opt1 = CheckItem(mm, "Arguments", "A")
1028 self.opt2 = CheckItem(mm, "Being hit on the head lessons", (kMenuOptionModifier, "A"))
1029 self.opt3 = CheckItem(mm, "Complaints", (kMenuOptionModifier|kMenuNoCommandModifier, "A"))
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +00001030 Separator(m)
1031 self.quititem = MenuItem(m, "Quit", "Q", self.quit)
1032
1033 def save(self, *args):
1034 print "Save"
1035
1036 def quit(self, *args):
1037 raise self
1038
1039
1040def test():
1041 "Test program"
1042 app = TestApp()
1043 app.mainloop()
1044
1045
1046if __name__ == '__main__':
1047 test()