blob: 8a2ef17e960fe6b0a78b981bf7303859003720fb [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.
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000147
Jack Jansenf2bd9ee2000-10-12 21:25:37 +0000148 schedparams = (0, 0) # By default disable Python's event handling
Jack Jansene5c94742001-03-08 23:10:37 +0000149 default_wait = None # By default we wait GetCaretTime in WaitNextEvent
Jack Jansenf2bd9ee2000-10-12 21:25:37 +0000150
Jack Jansene5c94742001-03-08 23:10:37 +0000151 def mainloop(self, mask = everyEvent, wait = None):
Jack Jansen647535d1996-09-17 12:35:43 +0000152 self.quitting = 0
Jack Jansen3368cb71997-06-12 10:51:18 +0000153 saveparams = apply(MacOS.SchedParams, self.schedparams)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000154 try:
Jack Jansen647535d1996-09-17 12:35:43 +0000155 while not self.quitting:
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000156 try:
157 self.do1event(mask, wait)
158 except (Application, SystemExit):
Jack Jansen647535d1996-09-17 12:35:43 +0000159 # Note: the raising of "self" is old-fashioned idiom to
160 # exit the mainloop. Calling _quit() is better for new
161 # applications.
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000162 break
163 finally:
Jack Jansen0c3e4b61999-12-03 16:08:50 +0000164 apply(MacOS.SchedParams, saveparams)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000165
Jack Jansend080edd1997-06-20 16:24:24 +0000166 def dopendingevents(self, mask = everyEvent):
167 """dopendingevents - Handle all pending events"""
168 while self.do1event(mask, wait=0):
169 pass
170
Jack Jansene5c94742001-03-08 23:10:37 +0000171 def do1event(self, mask = everyEvent, wait = None):
Jack Jansen13dc4f71995-08-31 13:38:01 +0000172 ok, event = self.getevent(mask, wait)
173 if IsDialogEvent(event):
174 if self.do_dialogevent(event):
175 return
176 if ok:
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000177 self.dispatch(event)
Jack Jansen38186781995-11-10 14:48:36 +0000178 else:
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000179 self.idle(event)
Jack Jansen38186781995-11-10 14:48:36 +0000180
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000181 def idle(self, event):
Jack Jansen38186781995-11-10 14:48:36 +0000182 pass
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000183
Jack Jansene5c94742001-03-08 23:10:37 +0000184 def getevent(self, mask = everyEvent, wait = None):
Jack Jansenb1667ef1996-09-26 16:17:08 +0000185 if self.needmenubarredraw:
186 DrawMenuBar()
187 self.needmenubarredraw = 0
Jack Jansene5c94742001-03-08 23:10:37 +0000188 if wait is None:
189 wait = self.default_wait
190 if wait is None:
191 wait = GetCaretTime()
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000192 ok, event = WaitNextEvent(mask, wait)
Jack Jansen13dc4f71995-08-31 13:38:01 +0000193 return ok, event
194
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000195 def dispatch(self, event):
Jack Jansend080edd1997-06-20 16:24:24 +0000196 # The following appears to be double work (already done in do1event)
197 # but we need it for asynchronous event handling
198 if IsDialogEvent(event):
199 if self.do_dialogevent(event):
200 return
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000201 (what, message, when, where, modifiers) = event
202 if eventname.has_key(what):
203 name = "do_" + eventname[what]
204 else:
205 name = "do_%d" % what
206 try:
207 handler = getattr(self, name)
208 except AttributeError:
209 handler = self.do_unknownevent
210 handler(event)
Jack Jansen7e0da901995-08-17 14:18:20 +0000211
Jack Jansend080edd1997-06-20 16:24:24 +0000212 def asyncevents(self, onoff):
213 """asyncevents - Set asynchronous event handling on or off"""
214 old = self._doing_asyncevents
215 if old:
216 MacOS.SetEventHandler()
217 apply(MacOS.SchedParams, self.schedparams)
218 if onoff:
219 MacOS.SetEventHandler(self.dispatch)
220 doint, dummymask, benice, howoften, bgyield = \
221 self.schedparams
222 MacOS.SchedParams(doint, everyEvent, benice,
223 howoften, bgyield)
224 self._doing_asyncevents = onoff
225 return old
226
Jack Jansen7e0da901995-08-17 14:18:20 +0000227 def do_dialogevent(self, event):
Jack Jansened24cd22001-02-14 17:07:04 +0000228 gotone, dlg, item = DialogSelect(event)
Jack Jansen7e0da901995-08-17 14:18:20 +0000229 if gotone:
Jack Jansened24cd22001-02-14 17:07:04 +0000230 window = dlg.GetDialogWindow()
Jack Jansen7e0da901995-08-17 14:18:20 +0000231 if self._windows.has_key(window):
Jack Jansen13dc4f71995-08-31 13:38:01 +0000232 self._windows[window].do_itemhit(item, event)
Jack Jansen7e0da901995-08-17 14:18:20 +0000233 else:
234 print 'Dialog event for unknown dialog'
Jack Jansen13dc4f71995-08-31 13:38:01 +0000235 return 1
236 return 0
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000237
238 def do_mouseDown(self, event):
239 (what, message, when, where, modifiers) = event
Jack Jansen7e0da901995-08-17 14:18:20 +0000240 partcode, wid = FindWindow(where)
241
242 #
243 # Find the correct name.
244 #
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000245 if partname.has_key(partcode):
246 name = "do_" + partname[partcode]
247 else:
248 name = "do_%d" % partcode
Jack Jansen7e0da901995-08-17 14:18:20 +0000249
250 if wid == None:
251 # No window, or a non-python window
252 try:
253 handler = getattr(self, name)
254 except AttributeError:
255 # Not menubar or something, so assume someone
256 # else's window
257 MacOS.HandleEvent(event)
258 return
259 elif self._windows.has_key(wid):
260 # It is a window. Hand off to correct window.
261 window = self._windows[wid]
262 try:
263 handler = getattr(window, name)
264 except AttributeError:
265 handler = self.do_unknownpartcode
266 else:
267 # It is a python-toolbox window, but not ours.
268 handler = self.do_unknownwindow
269 handler(partcode, wid, event)
270
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000271 def do_inSysWindow(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_inDesk(self, partcode, window, event):
Jack Jansen7a583361995-08-14 12:39:54 +0000275 MacOS.HandleEvent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000276
277 def do_inMenuBar(self, partcode, window, event):
Jack Jansen647535d1996-09-17 12:35:43 +0000278 if not self.menubar:
279 MacOS.HandleEvent(event)
280 return
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000281 (what, message, when, where, modifiers) = event
282 result = MenuSelect(where)
283 id = (result>>16) & 0xffff # Hi word
284 item = result & 0xffff # Lo word
285 self.do_rawmenu(id, item, window, event)
286
287 def do_rawmenu(self, id, item, window, event):
288 try:
289 self.do_menu(id, item, window, event)
290 finally:
291 HiliteMenu(0)
292
293 def do_menu(self, id, item, window, event):
Jack Jansenfd9925a2000-10-19 20:31:51 +0000294 MacOS.OutputSeen()
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000295 self.menubar.dispatch(id, item, window, event)
296
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000297
298 def do_unknownpartcode(self, partcode, window, event):
299 (what, message, when, where, modifiers) = event
Jack Jansen7a583361995-08-14 12:39:54 +0000300 if DEBUG: print "Mouse down at global:", where
301 if DEBUG: print "\tUnknown part code:", partcode
Jack Jansen7e0da901995-08-17 14:18:20 +0000302 if DEBUG: print "\tEvent:", self.printevent(event)
303 MacOS.HandleEvent(event)
304
305 def do_unknownwindow(self, partcode, window, event):
306 if DEBUG: print 'Unknown window:', window
Jack Jansen7a583361995-08-14 12:39:54 +0000307 MacOS.HandleEvent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000308
309 def do_keyDown(self, event):
310 self.do_key(event)
311
312 def do_autoKey(self, event):
313 if not event[-1] & cmdKey:
314 self.do_key(event)
315
316 def do_key(self, event):
317 (what, message, when, where, modifiers) = event
318 c = chr(message & charCodeMask)
Jack Jansenc15e43a1999-12-15 15:45:23 +0000319 if self.menubar:
320 result = MenuEvent(event)
321 id = (result>>16) & 0xffff # Hi word
322 item = result & 0xffff # Lo word
323 if id:
324 self.do_rawmenu(id, item, None, event)
325 return
326 # Otherwise we fall-through
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000327 if modifiers & cmdKey:
328 if c == '.':
329 raise self
330 else:
Jack Jansen647535d1996-09-17 12:35:43 +0000331 if not self.menubar:
332 MacOS.HandleEvent(event)
Jack Jansenc15e43a1999-12-15 15:45:23 +0000333 return
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):
Jack Jansenafd0aa62001-01-29 13:29:47 +0000485 menu.menu.EnableMenuItem(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 hasattr(self.parent, "domenu_" + callback):
Jack Jansenafd0aa62001-01-29 13:29:47 +0000491 menu.menu.EnableMenuItem(i + 1)
Jack Jansenb1667ef1996-09-26 16:17:08 +0000492 else:
Jack Jansenafd0aa62001-01-29 13:29:47 +0000493 menu.menu.DisableMenuItem(i + 1)
Jack Jansenb1667ef1996-09-26 16:17:08 +0000494 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]
Jack Jansen34d11f02000-03-07 23:40:13 +0000520 self.menu.DisposeMenu()
Jack Jansendb9ff361996-03-12 13:32:03 +0000521 del self.bar
522 del self.items
523 del self.menu
524 del self.id
Jack Jansen341d1fe1998-10-15 15:29:16 +0000525 del self._parent
Jack Jansen5c440271998-07-13 13:41:02 +0000526
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000527 def additem(self, label, shortcut=None, callback=None, kind=None):
528 self.menu.AppendMenu('x') # add a dummy string
Jack Jansen34d11f02000-03-07 23:40:13 +0000529 self.items.append((label, shortcut, callback, kind))
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000530 item = len(self.items)
Jack Jansene4b40381995-07-17 13:25:15 +0000531 self.menu.SetMenuItemText(item, label) # set the actual text
Jack Jansen13681b71999-12-14 15:45:53 +0000532 if shortcut and type(shortcut) == type(()):
533 modifiers, char = shortcut[:2]
534 self.menu.SetItemCmd(item, ord(char))
535 self.menu.SetMenuItemModifiers(item, modifiers)
536 if len(shortcut) > 2:
Jack Jansenc15e43a1999-12-15 15:45:23 +0000537 self.menu.SetMenuItemKeyGlyph(item, shortcut[2])
Jack Jansen13681b71999-12-14 15:45:53 +0000538 elif shortcut:
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000539 self.menu.SetItemCmd(item, ord(shortcut))
540 return item
Jack Jansen5c440271998-07-13 13:41:02 +0000541
542 def delitem(self, item):
543 if item != len(self.items):
544 raise 'Can only delete last item of a menu'
545 self.menu.DeleteMenuItem(item)
546 del self.items[item-1]
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000547
548 def addcheck(self, label, shortcut=None, callback=None):
549 return self.additem(label, shortcut, callback, 'check')
550
551 def addradio(self, label, shortcut=None, callback=None):
552 return self.additem(label, shortcut, callback, 'radio')
553
554 def addseparator(self):
555 self.menu.AppendMenu('(-')
Jack Jansen34d11f02000-03-07 23:40:13 +0000556 self.items.append(('', None, None, 'separator'))
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000557
558 def addsubmenu(self, label, title=''):
559 sub = Menu(self.bar, title, -1)
560 item = self.additem(label, '\x1B', None, 'submenu')
561 self.menu.SetItemMark(item, sub.id)
Jack Jansen341d1fe1998-10-15 15:29:16 +0000562 sub._parent = self
563 sub._parent_item = item
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000564 return sub
565
566 def dispatch(self, id, item, window, event):
Jack Jansenb1667ef1996-09-26 16:17:08 +0000567 title, shortcut, callback, mtype = self.items[item-1]
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000568 if callback:
Jack Jansenb1667ef1996-09-26 16:17:08 +0000569 if not self.bar.parent or type(callback) <> types.StringType:
570 menuhandler = callback
571 else:
572 # callback is string
573 wid = Win.FrontWindow()
574 if wid and self.bar.parent._windows.has_key(wid):
575 window = self.bar.parent._windows[wid]
576 if hasattr(window, "domenu_" + callback):
577 menuhandler = getattr(window, "domenu_" + callback)
578 elif hasattr(self.bar.parent, "domenu_" + callback):
579 menuhandler = getattr(self.bar.parent, "domenu_" + callback)
580 else:
581 # nothing we can do. we shouldn't have come this far
582 # since the menu item should have been disabled...
583 return
584 elif hasattr(self.bar.parent, "domenu_" + callback):
585 menuhandler = getattr(self.bar.parent, "domenu_" + callback)
586 else:
587 # nothing we can do. we shouldn't have come this far
588 # since the menu item should have been disabled...
589 return
590 menuhandler(id, item, window, event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000591
Jack Jansencef2c591996-04-11 15:39:01 +0000592 def enable(self, onoff):
593 if onoff:
Jack Jansenafd0aa62001-01-29 13:29:47 +0000594 self.menu.EnableMenuItem(0)
Jack Jansen341d1fe1998-10-15 15:29:16 +0000595 if self._parent:
Jack Jansenafd0aa62001-01-29 13:29:47 +0000596 self._parent.menu.EnableMenuItem(self._parent_item)
Jack Jansencef2c591996-04-11 15:39:01 +0000597 else:
Jack Jansenafd0aa62001-01-29 13:29:47 +0000598 self.menu.DisableMenuItem(0)
Jack Jansen341d1fe1998-10-15 15:29:16 +0000599 if self._parent:
Jack Jansenafd0aa62001-01-29 13:29:47 +0000600 self._parent.menu.DisableMenuItem(self._parent_item)
Jack Jansen5c440271998-07-13 13:41:02 +0000601 if self.bar and self.bar.parent:
602 self.bar.parent.needmenubarredraw = 1
Jack Jansenbb6193c1998-05-06 15:33:09 +0000603
604class PopupMenu(Menu):
605 def __init__(self, bar):
606 Menu.__init__(self, bar, '(popup)', -1)
607
608 def popup(self, x, y, event, default=1, window=None):
609 # NOTE that x and y are global coordinates, and they should probably
610 # be topleft of the button the user clicked (not mouse-coordinates),
611 # so the popup nicely overlaps.
612 reply = self.menu.PopUpMenuSelect(x, y, default)
613 if not reply:
614 return
615 id = (reply & 0xffff0000) >> 16
616 item = reply & 0xffff
617 if not window:
618 wid = Win.FrontWindow()
619 try:
620 window = self.bar.parent._windows[wid]
621 except:
622 pass # If we can't find the window we pass None
623 self.dispatch(id, item, window, event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000624
625class MenuItem:
626 def __init__(self, menu, title, shortcut=None, callback=None, kind=None):
627 self.item = menu.additem(title, shortcut, callback)
Jack Jansendb9ff361996-03-12 13:32:03 +0000628 self.menu = menu
629
Jack Jansen5c440271998-07-13 13:41:02 +0000630 def delete(self):
631 self.menu.delitem(self.item)
632 del self.menu
633 del self.item
634
Jack Jansendb9ff361996-03-12 13:32:03 +0000635 def check(self, onoff):
Jack Jansenafd0aa62001-01-29 13:29:47 +0000636 self.menu.menu.CheckMenuItem(self.item, onoff)
Jack Jansencef2c591996-04-11 15:39:01 +0000637
638 def enable(self, onoff):
639 if onoff:
Jack Jansenafd0aa62001-01-29 13:29:47 +0000640 self.menu.menu.EnableMenuItem(self.item)
Jack Jansencef2c591996-04-11 15:39:01 +0000641 else:
Jack Jansenafd0aa62001-01-29 13:29:47 +0000642 self.menu.menu.DisableMenuItem(self.item)
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000643
644 def settext(self, text):
645 self.menu.menu.SetMenuItemText(self.item, text)
Jack Jansendb9ff361996-03-12 13:32:03 +0000646
Jack Jansen0f6dc5b1996-04-23 16:18:33 +0000647 def setstyle(self, style):
648 self.menu.menu.SetItemStyle(self.item, style)
649
650 def seticon(self, icon):
651 self.menu.menu.SetItemIcon(self.item, icon)
652
653 def setcmd(self, cmd):
654 self.menu.menu.SetItemCmd(self.item, cmd)
655
656 def setmark(self, cmd):
657 self.menu.menu.SetItemMark(self.item, cmd)
658
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000659
660class RadioItem(MenuItem):
661 def __init__(self, menu, title, shortcut=None, callback=None):
662 MenuItem.__init__(self, menu, title, shortcut, callback, 'radio')
663
664class CheckItem(MenuItem):
665 def __init__(self, menu, title, shortcut=None, callback=None):
666 MenuItem.__init__(self, menu, title, shortcut, callback, 'check')
667
668def Separator(menu):
669 menu.addseparator()
670
671def SubMenu(menu, label, title=''):
672 return menu.addsubmenu(label, title)
673
674
675class AppleMenu(Menu):
676
677 def __init__(self, bar, abouttext="About me...", aboutcallback=None):
678 Menu.__init__(self, bar, "\024")
679 self.additem(abouttext, None, aboutcallback)
680 self.addseparator()
Jack Jansen01a2d9e2001-01-29 15:32:00 +0000681 if MacOS.runtimemodel == 'ppc':
682 self.menu.AppendResMenu('DRVR')
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000683
684 def dispatch(self, id, item, window, event):
685 if item == 1:
686 Menu.dispatch(self, id, item, window, event)
Jack Jansen01a2d9e2001-01-29 15:32:00 +0000687 elif MacOS.runtimemodel == 'ppc':
Jack Jansenc8a99491996-01-08 23:50:13 +0000688 name = self.menu.GetMenuItemText(item)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000689 OpenDeskAcc(name)
690
Jack Jansen7e0da901995-08-17 14:18:20 +0000691class Window:
692 """A single window belonging to an application"""
693
694 def __init__(self, parent):
695 self.wid = None
696 self.parent = parent
697
Jack Jansenc8a99491996-01-08 23:50:13 +0000698 def open(self, bounds=(40, 40, 400, 400), resid=None):
699 if resid <> None:
700 self.wid = GetNewWindow(resid, -1)
701 else:
702 self.wid = NewWindow(bounds, self.__class__.__name__, 1,
Jack Jansended835c1996-07-26 14:01:07 +0000703 8, -1, 1, 0) # changed to proc id 8 to include zoom box. jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000704 self.do_postopen()
705
706 def do_postopen(self):
707 """Tell our parent we exist"""
708 self.parent.appendwindow(self.wid, self)
709
710 def close(self):
Jack Jansen7e0da901995-08-17 14:18:20 +0000711 self.do_postclose()
712
713 def do_postclose(self):
714 self.parent.removewindow(self.wid)
715 self.parent = None
716 self.wid = None
Jack Jansenc8a99491996-01-08 23:50:13 +0000717
718 def SetPort(self):
719 # Convinience method
720 SetPort(self.wid)
Jack Jansen73023402001-01-23 14:58:20 +0000721
722 def GetWindow(self):
723 return self.wid
Jack Jansen7e0da901995-08-17 14:18:20 +0000724
725 def do_inDrag(self, partcode, window, event):
726 where = event[3]
727 window.DragWindow(where, self.draglimit)
728
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000729 draglimit = screenbounds
Jack Jansen7e0da901995-08-17 14:18:20 +0000730
731 def do_inGoAway(self, partcode, window, event):
732 where = event[3]
733 if window.TrackGoAway(where):
734 self.close()
735
736 def do_inZoom(self, partcode, window, event):
737 (what, message, when, where, modifiers) = event
738 if window.TrackBox(where, partcode):
739 window.ZoomWindow(partcode, 1)
Jack Jansended835c1996-07-26 14:01:07 +0000740 rect = window.GetWindowUserState() # so that zoom really works... jvr
741 self.do_postresize(rect[2] - rect[0], rect[3] - rect[1], window) # jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000742
743 def do_inZoomIn(self, partcode, window, event):
744 SetPort(window) # !!!
745 self.do_inZoom(partcode, window, event)
746
747 def do_inZoomOut(self, partcode, window, event):
748 SetPort(window) # !!!
749 self.do_inZoom(partcode, window, event)
750
751 def do_inGrow(self, partcode, window, event):
752 (what, message, when, where, modifiers) = event
753 result = window.GrowWindow(where, self.growlimit)
754 if result:
755 height = (result>>16) & 0xffff # Hi word
756 width = result & 0xffff # Lo word
757 self.do_resize(width, height, window)
758
Jack Jansended835c1996-07-26 14:01:07 +0000759 growlimit = (50, 50, screenbounds[2] - screenbounds[0], screenbounds[3] - screenbounds[1]) # jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000760
761 def do_resize(self, width, height, window):
Jack Jansended835c1996-07-26 14:01:07 +0000762 l, t, r, b = self.wid.GetWindowPort().portRect # jvr, forGrowIcon
763 self.SetPort() # jvr
Jack Jansen73023402001-01-23 14:58:20 +0000764 self.wid.InvalWindowRect((r - SCROLLBARWIDTH + 1, b - SCROLLBARWIDTH + 1, r, b)) # jvr
Jack Jansended835c1996-07-26 14:01:07 +0000765 window.SizeWindow(width, height, 1) # changed updateFlag to true jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000766 self.do_postresize(width, height, window)
767
768 def do_postresize(self, width, height, window):
769 SetPort(window)
Jack Jansen73023402001-01-23 14:58:20 +0000770 self.wid.InvalWindowRect(window.GetWindowPort().portRect)
Jack Jansen7e0da901995-08-17 14:18:20 +0000771
772 def do_inContent(self, partcode, window, event):
773 #
774 # If we're not frontmost, select ourselves and wait for
775 # the activate event.
776 #
777 if FrontWindow() <> window:
778 window.SelectWindow()
779 return
780 # We are. Handle the event.
781 (what, message, when, where, modifiers) = event
782 SetPort(window)
783 local = GlobalToLocal(where)
784 self.do_contentclick(local, modifiers, event)
785
786 def do_contentclick(self, local, modifiers, event):
Jack Jansended835c1996-07-26 14:01:07 +0000787 if DEBUG:
788 print 'Click in contents at %s, modifiers %s'%(local, modifiers)
Jack Jansen7e0da901995-08-17 14:18:20 +0000789
790 def do_rawupdate(self, window, event):
791 if DEBUG: print "raw update for", window
Jack Jansenda38f2d1995-11-14 10:15:42 +0000792 SetPort(window)
Jack Jansen7e0da901995-08-17 14:18:20 +0000793 window.BeginUpdate()
794 self.do_update(window, event)
795 window.EndUpdate()
796
797 def do_update(self, window, event):
Jack Jansended835c1996-07-26 14:01:07 +0000798 if DEBUG:
799 import time
800 for i in range(8):
801 time.sleep(0.1)
802 InvertRgn(window.GetWindowPort().visRgn)
803 FillRgn(window.GetWindowPort().visRgn, qd.gray)
804 else:
805 EraseRgn(window.GetWindowPort().visRgn)
Jack Jansen7e0da901995-08-17 14:18:20 +0000806
807 def do_activate(self, activate, event):
808 if DEBUG: print 'Activate %d for %s'%(activate, self.wid)
809
810class ControlsWindow(Window):
811
812 def do_rawupdate(self, window, event):
813 if DEBUG: print "raw update for", window
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000814 SetPort(window)
Jack Jansen7e0da901995-08-17 14:18:20 +0000815 window.BeginUpdate()
816 self.do_update(window, event)
Jack Jansended835c1996-07-26 14:01:07 +0000817 #DrawControls(window) # jvr
818 UpdateControls(window, window.GetWindowPort().visRgn) # jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000819 window.DrawGrowIcon()
820 window.EndUpdate()
821
822 def do_controlhit(self, window, control, pcode, event):
823 if DEBUG: print "control hit in", window, "on", control, "; pcode =", pcode
824
825 def do_inContent(self, partcode, window, event):
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000826 if FrontWindow() <> window:
827 window.SelectWindow()
828 return
Jack Jansen7e0da901995-08-17 14:18:20 +0000829 (what, message, when, where, modifiers) = event
Jack Jansenda38f2d1995-11-14 10:15:42 +0000830 SetPort(window) # XXXX Needed?
Jack Jansen7e0da901995-08-17 14:18:20 +0000831 local = GlobalToLocal(where)
Jack Jansen41e825a1998-05-28 14:22:48 +0000832 pcode, control = FindControl(local, window)
833 if pcode and control:
834 self.do_rawcontrolhit(window, control, pcode, local, event)
Jack Jansen7e0da901995-08-17 14:18:20 +0000835 else:
836 if DEBUG: print "FindControl(%s, %s) -> (%s, %s)" % \
Jack Jansen41e825a1998-05-28 14:22:48 +0000837 (local, window, pcode, control)
Jack Jansene3532151996-04-12 16:24:44 +0000838 self.do_contentclick(local, modifiers, event)
839
Jack Jansen41e825a1998-05-28 14:22:48 +0000840 def do_rawcontrolhit(self, window, control, pcode, local, event):
841 pcode = control.TrackControl(local)
842 if pcode:
843 self.do_controlhit(window, control, pcode, event)
844
Jack Jansene3532151996-04-12 16:24:44 +0000845class ScrolledWindow(ControlsWindow):
846 def __init__(self, parent):
847 self.barx = self.bary = None
Jack Jansen7bfc8751996-04-16 14:35:43 +0000848 self.barx_enabled = self.bary_enabled = 1
849 self.activated = 1
Jack Jansene3532151996-04-12 16:24:44 +0000850 ControlsWindow.__init__(self, parent)
851
852 def scrollbars(self, wantx=1, wanty=1):
853 SetPort(self.wid)
854 self.barx = self.bary = None
Jack Jansen7bfc8751996-04-16 14:35:43 +0000855 self.barx_enabled = self.bary_enabled = 1
Jack Jansene3532151996-04-12 16:24:44 +0000856 x0, y0, x1, y1 = self.wid.GetWindowPort().portRect
857 vx, vy = self.getscrollbarvalues()
Jack Jansen7bfc8751996-04-16 14:35:43 +0000858 if vx == None: self.barx_enabled, vx = 0, 0
859 if vy == None: self.bary_enabled, vy = 0, 0
Jack Jansene3532151996-04-12 16:24:44 +0000860 if wantx:
861 rect = x0-1, y1-(SCROLLBARWIDTH-1), x1-(SCROLLBARWIDTH-2), y1+1
862 self.barx = NewControl(self.wid, rect, "", 1, vx, 0, 32767, 16, 0)
Jack Jansen7bfc8751996-04-16 14:35:43 +0000863 if not self.barx_enabled: self.barx.HiliteControl(255)
Jack Jansen73023402001-01-23 14:58:20 +0000864## self.wid.InvalWindowRect(rect)
Jack Jansene3532151996-04-12 16:24:44 +0000865 if wanty:
866 rect = x1-(SCROLLBARWIDTH-1), y0-1, x1+1, y1-(SCROLLBARWIDTH-2)
867 self.bary = NewControl(self.wid, rect, "", 1, vy, 0, 32767, 16, 0)
Jack Jansen7bfc8751996-04-16 14:35:43 +0000868 if not self.bary_enabled: self.bary.HiliteControl(255)
Jack Jansen73023402001-01-23 14:58:20 +0000869## self.wid.InvalWindowRect(rect)
Jack Jansene3532151996-04-12 16:24:44 +0000870
871 def do_postclose(self):
872 self.barx = self.bary = None
873 ControlsWindow.do_postclose(self)
874
875 def do_activate(self, onoff, event):
Jack Jansen7bfc8751996-04-16 14:35:43 +0000876 self.activated = onoff
Jack Jansene3532151996-04-12 16:24:44 +0000877 if onoff:
Jack Jansen7bfc8751996-04-16 14:35:43 +0000878 if self.barx and self.barx_enabled:
Jack Jansended835c1996-07-26 14:01:07 +0000879 self.barx.ShowControl() # jvr
Jack Jansen7bfc8751996-04-16 14:35:43 +0000880 if self.bary and self.bary_enabled:
Jack Jansended835c1996-07-26 14:01:07 +0000881 self.bary.ShowControl() # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000882 else:
Jack Jansen7bfc8751996-04-16 14:35:43 +0000883 if self.barx:
Jack Jansended835c1996-07-26 14:01:07 +0000884 self.barx.HideControl() # jvr; An inactive window should have *hidden*
885 # scrollbars, not just dimmed (no matter what
886 # BBEdit does... look at the Finder)
Jack Jansen7bfc8751996-04-16 14:35:43 +0000887 if self.bary:
Jack Jansended835c1996-07-26 14:01:07 +0000888 self.bary.HideControl() # jvr
889 self.wid.DrawGrowIcon() # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000890
891 def do_postresize(self, width, height, window):
892 l, t, r, b = self.wid.GetWindowPort().portRect
Jack Jansended835c1996-07-26 14:01:07 +0000893 self.SetPort()
Jack Jansene3532151996-04-12 16:24:44 +0000894 if self.barx:
Jack Jansended835c1996-07-26 14:01:07 +0000895 self.barx.HideControl() # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000896 self.barx.MoveControl(l-1, b-(SCROLLBARWIDTH-1))
Jack Jansended835c1996-07-26 14:01:07 +0000897 self.barx.SizeControl((r-l)-(SCROLLBARWIDTH-3), SCROLLBARWIDTH) # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000898 if self.bary:
Jack Jansended835c1996-07-26 14:01:07 +0000899 self.bary.HideControl() # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000900 self.bary.MoveControl(r-(SCROLLBARWIDTH-1), t-1)
Jack Jansended835c1996-07-26 14:01:07 +0000901 self.bary.SizeControl(SCROLLBARWIDTH, (b-t)-(SCROLLBARWIDTH-3)) # jvr
902 if self.barx:
903 self.barx.ShowControl() # jvr
Jack Jansen73023402001-01-23 14:58:20 +0000904 self.wid.ValidWindowRect((l, b - SCROLLBARWIDTH + 1, r - SCROLLBARWIDTH + 2, b)) # jvr
Jack Jansended835c1996-07-26 14:01:07 +0000905 if self.bary:
906 self.bary.ShowControl() # jvr
Jack Jansen73023402001-01-23 14:58:20 +0000907 self.wid.ValidWindowRect((r - SCROLLBARWIDTH + 1, t, r, b - SCROLLBARWIDTH + 2)) # jvr
908 self.wid.InvalWindowRect((r - SCROLLBARWIDTH + 1, b - SCROLLBARWIDTH + 1, r, b)) # jvr, growicon
Jack Jansene3532151996-04-12 16:24:44 +0000909
Jack Jansen41e825a1998-05-28 14:22:48 +0000910
911 def do_rawcontrolhit(self, window, control, pcode, local, event):
Jack Jansene3532151996-04-12 16:24:44 +0000912 if control == self.barx:
Jack Jansene3532151996-04-12 16:24:44 +0000913 which = 'x'
914 elif control == self.bary:
Jack Jansene3532151996-04-12 16:24:44 +0000915 which = 'y'
916 else:
917 return 0
Jack Jansen41e825a1998-05-28 14:22:48 +0000918 if pcode in (inUpButton, inDownButton, inPageUp, inPageDown):
919 # We do the work for the buttons and grey area in the tracker
920 dummy = control.TrackControl(local, self.do_controltrack)
921 else:
922 # but the thumb is handled here
923 pcode = control.TrackControl(local)
924 if pcode == inThumb:
925 value = control.GetControlValue()
926 print 'setbars', which, value #DBG
927 self.scrollbar_callback(which, 'set', value)
928 self.updatescrollbars()
929 else:
930 print 'funny part', pcode #DBG
931 return 1
932
933 def do_controltrack(self, control, pcode):
934 if control == self.barx:
935 which = 'x'
936 elif control == self.bary:
937 which = 'y'
938 else:
939 return
940
Jack Jansene3532151996-04-12 16:24:44 +0000941 if pcode == inUpButton:
942 what = '-'
943 elif pcode == inDownButton:
944 what = '+'
945 elif pcode == inPageUp:
946 what = '--'
947 elif pcode == inPageDown:
948 what = '++'
949 else:
Jack Jansen41e825a1998-05-28 14:22:48 +0000950 return
951 self.scrollbar_callback(which, what, None)
Jack Jansene3532151996-04-12 16:24:44 +0000952 self.updatescrollbars()
Jack Jansene3532151996-04-12 16:24:44 +0000953
954 def updatescrollbars(self):
955 SetPort(self.wid)
956 vx, vy = self.getscrollbarvalues()
957 if self.barx:
Jack Jansen7bfc8751996-04-16 14:35:43 +0000958 if vx == None:
959 self.barx.HiliteControl(255)
960 self.barx_enabled = 0
961 else:
962 if not self.barx_enabled:
963 self.barx_enabled = 1
964 if self.activated:
965 self.barx.HiliteControl(0)
966 self.barx.SetControlValue(vx)
Jack Jansene3532151996-04-12 16:24:44 +0000967 if self.bary:
Jack Jansen7bfc8751996-04-16 14:35:43 +0000968 if vy == None:
969 self.bary.HiliteControl(255)
970 self.bary_enabled = 0
971 else:
972 if not self.bary_enabled:
973 self.bary_enabled = 1
974 if self.activated:
975 self.bary.HiliteControl(0)
976 self.bary.SetControlValue(vy)
977
978 # Auxiliary function: convert standard text/image/etc coordinate
979 # to something palatable as getscrollbarvalues() return
980 def scalebarvalue(self, absmin, absmax, curmin, curmax):
981 if curmin <= absmin and curmax >= absmax:
982 return None
983 if curmin <= absmin:
984 return 0
985 if curmax >= absmax:
986 return 32767
987 perc = float(curmin-absmin)/float(absmax-absmin)
988 return int(perc*32767)
Jack Jansene3532151996-04-12 16:24:44 +0000989
990 # To be overridden:
991
992 def getscrollbarvalues(self):
993 return 0, 0
994
995 def scrollbar_callback(self, which, what, value):
996 print 'scroll', which, what, value
Jack Jansen7e0da901995-08-17 14:18:20 +0000997
998class DialogWindow(Window):
999 """A modeless dialog window"""
1000
1001 def open(self, resid):
Jack Jansened24cd22001-02-14 17:07:04 +00001002 self.dlg = GetNewDialog(resid, -1)
1003 self.wid = self.dlg.GetDialogWindow()
Jack Jansen7e0da901995-08-17 14:18:20 +00001004 self.do_postopen()
1005
1006 def close(self):
Jack Jansen7e0da901995-08-17 14:18:20 +00001007 self.do_postclose()
1008
1009 def do_itemhit(self, item, event):
Jack Jansened24cd22001-02-14 17:07:04 +00001010 print 'Dialog %s, item %d hit'%(self.dlg, item)
Jack Jansen7e0da901995-08-17 14:18:20 +00001011
1012 def do_rawupdate(self, window, event):
1013 pass
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +00001014
1015def ostypecode(x):
1016 "Convert a long int to the 4-character code it really is"
1017 s = ''
1018 for i in range(4):
1019 x, c = divmod(x, 256)
1020 s = chr(c) + s
1021 return s
1022
1023
1024class TestApp(Application):
1025
1026 "This class is used by the test() function"
1027
1028 def makeusermenus(self):
1029 self.filemenu = m = Menu(self.menubar, "File")
1030 self.saveitem = MenuItem(m, "Save", "S", self.save)
1031 Separator(m)
1032 self.optionsmenu = mm = SubMenu(m, "Options")
Jack Jansen13681b71999-12-14 15:45:53 +00001033 self.opt1 = CheckItem(mm, "Arguments", "A")
1034 self.opt2 = CheckItem(mm, "Being hit on the head lessons", (kMenuOptionModifier, "A"))
1035 self.opt3 = CheckItem(mm, "Complaints", (kMenuOptionModifier|kMenuNoCommandModifier, "A"))
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +00001036 Separator(m)
1037 self.quititem = MenuItem(m, "Quit", "Q", self.quit)
1038
1039 def save(self, *args):
1040 print "Save"
1041
1042 def quit(self, *args):
1043 raise self
1044
1045
1046def test():
1047 "Test program"
1048 app = TestApp()
1049 app.mainloop()
1050
1051
1052if __name__ == '__main__':
1053 test()