blob: c0692b941e5a797fb265a1bb6bd8be581f800ec3 [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
Jack Jansen5a6fdcd2001-08-25 12:15:04 +00008from Carbon.AE import *
9from Carbon.AppleEvents import *
10from Carbon.Ctl import *
11from Carbon.Controls import *
12from Carbon.Dlg import *
13from Carbon.Dialogs import *
14from Carbon.Evt import *
15from Carbon.Events import *
16from Carbon.Menu import *
17from Carbon.Menus import *
18from Carbon.Qd import *
19from Carbon.QuickDraw import *
20#from Carbon.Res import *
21#from Carbon.Resources import *
22#from Carbon.Snd import *
23#from Carbon.Sound import *
24from Carbon.Win import *
25from Carbon.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
Jack Jansen52306a72001-12-10 16:08:14 +000033# Trick to forestall a set of SIOUX menus being added to our menubar
34SIOUX_APPLEMENU_ID=32000
35
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000036
37# Map event 'what' field to strings
38eventname = {}
39eventname[1] = 'mouseDown'
40eventname[2] = 'mouseUp'
41eventname[3] = 'keyDown'
42eventname[4] = 'keyUp'
43eventname[5] = 'autoKey'
44eventname[6] = 'updateEvt'
45eventname[7] = 'diskEvt'
46eventname[8] = 'activateEvt'
47eventname[15] = 'osEvt'
48eventname[23] = 'kHighLevelEvent'
49
50# Map part codes returned by WhichWindow() to strings
51partname = {}
52partname[0] = 'inDesk'
53partname[1] = 'inMenuBar'
54partname[2] = 'inSysWindow'
55partname[3] = 'inContent'
56partname[4] = 'inDrag'
57partname[5] = 'inGrow'
58partname[6] = 'inGoAway'
59partname[7] = 'inZoomIn'
60partname[8] = 'inZoomOut'
61
Jack Jansenc4eec9f1996-04-19 16:00:28 +000062#
63# The useable portion of the screen
Jack Jansended835c1996-07-26 14:01:07 +000064# ## but what happens with multiple screens? jvr
Jack Jansenc4eec9f1996-04-19 16:00:28 +000065screenbounds = qd.screenBits.bounds
66screenbounds = screenbounds[0]+4, screenbounds[1]+4, \
67 screenbounds[2]-4, screenbounds[3]-4
68
Jack Jansended835c1996-07-26 14:01:07 +000069next_window_x = 16 # jvr
70next_window_y = 44 # jvr
Jack Jansenc4eec9f1996-04-19 16:00:28 +000071
72def windowbounds(width, height):
73 "Return sensible window bounds"
74 global next_window_x, next_window_y
75 r, b = next_window_x+width, next_window_y+height
76 if r > screenbounds[2]:
Jack Jansended835c1996-07-26 14:01:07 +000077 next_window_x = 16
Jack Jansenc4eec9f1996-04-19 16:00:28 +000078 if b > screenbounds[3]:
Jack Jansended835c1996-07-26 14:01:07 +000079 next_window_y = 44
Jack Jansenc4eec9f1996-04-19 16:00:28 +000080 l, t = next_window_x, next_window_y
81 r, b = next_window_x+width, next_window_y+height
Jack Jansended835c1996-07-26 14:01:07 +000082 next_window_x, next_window_y = next_window_x + 8, next_window_y + 20 # jvr
Jack Jansenc4eec9f1996-04-19 16:00:28 +000083 return l, t, r, b
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000084
Jack Jansen46341301996-08-28 13:53:07 +000085_watch = None
86def setwatchcursor():
87 global _watch
88
89 if _watch == None:
90 _watch = GetCursor(4).data
91 SetCursor(_watch)
92
93def setarrowcursor():
94 SetCursor(qd.arrow)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +000095
96class Application:
97
98 "Application framework -- your application should be a derived class"
99
Jack Jansen647535d1996-09-17 12:35:43 +0000100 def __init__(self, nomenubar=0):
Jack Jansend080edd1997-06-20 16:24:24 +0000101 self._doing_asyncevents = 0
Jack Jansen647535d1996-09-17 12:35:43 +0000102 self.quitting = 0
Jack Jansenb1667ef1996-09-26 16:17:08 +0000103 self.needmenubarredraw = 0
Jack Jansen7e0da901995-08-17 14:18:20 +0000104 self._windows = {}
Jack Jansen647535d1996-09-17 12:35:43 +0000105 if nomenubar:
106 self.menubar = None
107 else:
108 self.makemenubar()
Jack Jansend080edd1997-06-20 16:24:24 +0000109
110 def __del__(self):
111 if self._doing_asyncevents:
112 self._doing_asyncevents = 0
113 MacOS.SetEventHandler()
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000114
115 def makemenubar(self):
Jack Jansenb1667ef1996-09-26 16:17:08 +0000116 self.menubar = MenuBar(self)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000117 AppleMenu(self.menubar, self.getabouttext(), self.do_about)
118 self.makeusermenus()
Jack Jansenc8a99491996-01-08 23:50:13 +0000119
120 def makeusermenus(self):
121 self.filemenu = m = Menu(self.menubar, "File")
122 self._quititem = MenuItem(m, "Quit", "Q", self._quit)
123
124 def _quit(self, *args):
Jack Jansen647535d1996-09-17 12:35:43 +0000125 self.quitting = 1
Jack Jansen7e0da901995-08-17 14:18:20 +0000126
Jack Jansenc75e1d01996-12-23 17:22:40 +0000127 def cleanup(self):
128 for w in self._windows.values():
129 w.do_close()
130 return self._windows == {}
131
Jack Jansen7e0da901995-08-17 14:18:20 +0000132 def appendwindow(self, wid, window):
133 self._windows[wid] = window
134
135 def removewindow(self, wid):
136 del self._windows[wid]
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000137
138 def getabouttext(self):
139 return "About %s..." % self.__class__.__name__
140
141 def do_about(self, id, item, window, event):
142 EasyDialogs.Message("Hello, world!" + "\015(%s)" % self.__class__.__name__)
143
144 # The main event loop is broken up in several simple steps.
145 # This is done so you can override each individual part,
146 # if you have a need to do extra processing independent of the
147 # event type.
148 # Normally, however, you'd just define handlers for individual
149 # events.
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000150
Jack Jansenf2bd9ee2000-10-12 21:25:37 +0000151 schedparams = (0, 0) # By default disable Python's event handling
Jack Jansene5c94742001-03-08 23:10:37 +0000152 default_wait = None # By default we wait GetCaretTime in WaitNextEvent
Jack Jansenf2bd9ee2000-10-12 21:25:37 +0000153
Jack Jansene5c94742001-03-08 23:10:37 +0000154 def mainloop(self, mask = everyEvent, wait = None):
Jack Jansen647535d1996-09-17 12:35:43 +0000155 self.quitting = 0
Jack Jansen3368cb71997-06-12 10:51:18 +0000156 saveparams = apply(MacOS.SchedParams, self.schedparams)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000157 try:
Jack Jansen647535d1996-09-17 12:35:43 +0000158 while not self.quitting:
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000159 try:
160 self.do1event(mask, wait)
161 except (Application, SystemExit):
Jack Jansen647535d1996-09-17 12:35:43 +0000162 # Note: the raising of "self" is old-fashioned idiom to
163 # exit the mainloop. Calling _quit() is better for new
164 # applications.
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000165 break
166 finally:
Jack Jansen0c3e4b61999-12-03 16:08:50 +0000167 apply(MacOS.SchedParams, saveparams)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000168
Jack Jansend080edd1997-06-20 16:24:24 +0000169 def dopendingevents(self, mask = everyEvent):
170 """dopendingevents - Handle all pending events"""
171 while self.do1event(mask, wait=0):
172 pass
173
Jack Jansene5c94742001-03-08 23:10:37 +0000174 def do1event(self, mask = everyEvent, wait = None):
Jack Jansen13dc4f71995-08-31 13:38:01 +0000175 ok, event = self.getevent(mask, wait)
176 if IsDialogEvent(event):
177 if self.do_dialogevent(event):
178 return
179 if ok:
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000180 self.dispatch(event)
Jack Jansen38186781995-11-10 14:48:36 +0000181 else:
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000182 self.idle(event)
Jack Jansen38186781995-11-10 14:48:36 +0000183
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000184 def idle(self, event):
Jack Jansen38186781995-11-10 14:48:36 +0000185 pass
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000186
Jack Jansene5c94742001-03-08 23:10:37 +0000187 def getevent(self, mask = everyEvent, wait = None):
Jack Jansenb1667ef1996-09-26 16:17:08 +0000188 if self.needmenubarredraw:
189 DrawMenuBar()
190 self.needmenubarredraw = 0
Jack Jansene5c94742001-03-08 23:10:37 +0000191 if wait is None:
192 wait = self.default_wait
193 if wait is None:
194 wait = GetCaretTime()
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000195 ok, event = WaitNextEvent(mask, wait)
Jack Jansen13dc4f71995-08-31 13:38:01 +0000196 return ok, event
197
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000198 def dispatch(self, event):
Jack Jansend080edd1997-06-20 16:24:24 +0000199 # The following appears to be double work (already done in do1event)
200 # but we need it for asynchronous event handling
201 if IsDialogEvent(event):
202 if self.do_dialogevent(event):
203 return
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000204 (what, message, when, where, modifiers) = event
205 if eventname.has_key(what):
206 name = "do_" + eventname[what]
207 else:
208 name = "do_%d" % what
209 try:
210 handler = getattr(self, name)
211 except AttributeError:
212 handler = self.do_unknownevent
213 handler(event)
Jack Jansen7e0da901995-08-17 14:18:20 +0000214
Jack Jansend080edd1997-06-20 16:24:24 +0000215 def asyncevents(self, onoff):
216 """asyncevents - Set asynchronous event handling on or off"""
217 old = self._doing_asyncevents
218 if old:
219 MacOS.SetEventHandler()
220 apply(MacOS.SchedParams, self.schedparams)
221 if onoff:
222 MacOS.SetEventHandler(self.dispatch)
223 doint, dummymask, benice, howoften, bgyield = \
224 self.schedparams
225 MacOS.SchedParams(doint, everyEvent, benice,
226 howoften, bgyield)
227 self._doing_asyncevents = onoff
228 return old
229
Jack Jansen7e0da901995-08-17 14:18:20 +0000230 def do_dialogevent(self, event):
Jack Jansened24cd22001-02-14 17:07:04 +0000231 gotone, dlg, item = DialogSelect(event)
Jack Jansen7e0da901995-08-17 14:18:20 +0000232 if gotone:
Jack Jansened24cd22001-02-14 17:07:04 +0000233 window = dlg.GetDialogWindow()
Jack Jansen7e0da901995-08-17 14:18:20 +0000234 if self._windows.has_key(window):
Jack Jansen13dc4f71995-08-31 13:38:01 +0000235 self._windows[window].do_itemhit(item, event)
Jack Jansen7e0da901995-08-17 14:18:20 +0000236 else:
237 print 'Dialog event for unknown dialog'
Jack Jansen13dc4f71995-08-31 13:38:01 +0000238 return 1
239 return 0
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000240
241 def do_mouseDown(self, event):
242 (what, message, when, where, modifiers) = event
Jack Jansen7e0da901995-08-17 14:18:20 +0000243 partcode, wid = FindWindow(where)
244
245 #
246 # Find the correct name.
247 #
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000248 if partname.has_key(partcode):
249 name = "do_" + partname[partcode]
250 else:
251 name = "do_%d" % partcode
Jack Jansen7e0da901995-08-17 14:18:20 +0000252
253 if wid == None:
254 # No window, or a non-python window
255 try:
256 handler = getattr(self, name)
257 except AttributeError:
258 # Not menubar or something, so assume someone
259 # else's window
260 MacOS.HandleEvent(event)
261 return
262 elif self._windows.has_key(wid):
263 # It is a window. Hand off to correct window.
264 window = self._windows[wid]
265 try:
266 handler = getattr(window, name)
267 except AttributeError:
268 handler = self.do_unknownpartcode
269 else:
270 # It is a python-toolbox window, but not ours.
271 handler = self.do_unknownwindow
272 handler(partcode, wid, event)
273
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000274 def do_inSysWindow(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_inDesk(self, partcode, window, event):
Jack Jansen7a583361995-08-14 12:39:54 +0000278 MacOS.HandleEvent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000279
280 def do_inMenuBar(self, partcode, window, event):
Jack Jansen647535d1996-09-17 12:35:43 +0000281 if not self.menubar:
282 MacOS.HandleEvent(event)
283 return
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000284 (what, message, when, where, modifiers) = event
285 result = MenuSelect(where)
286 id = (result>>16) & 0xffff # Hi word
287 item = result & 0xffff # Lo word
288 self.do_rawmenu(id, item, window, event)
289
290 def do_rawmenu(self, id, item, window, event):
291 try:
292 self.do_menu(id, item, window, event)
293 finally:
294 HiliteMenu(0)
295
296 def do_menu(self, id, item, window, event):
Jack Jansenfd9925a2000-10-19 20:31:51 +0000297 MacOS.OutputSeen()
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000298 self.menubar.dispatch(id, item, window, event)
299
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000300
301 def do_unknownpartcode(self, partcode, window, event):
302 (what, message, when, where, modifiers) = event
Jack Jansen7a583361995-08-14 12:39:54 +0000303 if DEBUG: print "Mouse down at global:", where
304 if DEBUG: print "\tUnknown part code:", partcode
Jack Jansen7e0da901995-08-17 14:18:20 +0000305 if DEBUG: print "\tEvent:", self.printevent(event)
306 MacOS.HandleEvent(event)
307
308 def do_unknownwindow(self, partcode, window, event):
309 if DEBUG: print 'Unknown window:', window
Jack Jansen7a583361995-08-14 12:39:54 +0000310 MacOS.HandleEvent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000311
312 def do_keyDown(self, event):
313 self.do_key(event)
314
315 def do_autoKey(self, event):
316 if not event[-1] & cmdKey:
317 self.do_key(event)
318
319 def do_key(self, event):
320 (what, message, when, where, modifiers) = event
321 c = chr(message & charCodeMask)
Jack Jansenc15e43a1999-12-15 15:45:23 +0000322 if self.menubar:
323 result = MenuEvent(event)
324 id = (result>>16) & 0xffff # Hi word
325 item = result & 0xffff # Lo word
326 if id:
327 self.do_rawmenu(id, item, None, event)
328 return
329 # Otherwise we fall-through
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000330 if modifiers & cmdKey:
331 if c == '.':
332 raise self
333 else:
Jack Jansen647535d1996-09-17 12:35:43 +0000334 if not self.menubar:
335 MacOS.HandleEvent(event)
Jack Jansenc15e43a1999-12-15 15:45:23 +0000336 return
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000337 else:
Jack Jansen7e0da901995-08-17 14:18:20 +0000338 # See whether the front window wants it
339 w = FrontWindow()
340 if w and self._windows.has_key(w):
341 window = self._windows[w]
342 try:
343 do_char = window.do_char
344 except AttributeError:
345 do_char = self.do_char
Jack Jansen6f47bf41995-12-12 15:03:35 +0000346 do_char(c, event)
347 # else it wasn't for us, sigh...
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000348
349 def do_char(self, c, event):
Jack Jansen7a583361995-08-14 12:39:54 +0000350 if DEBUG: print "Character", `c`
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000351
352 def do_updateEvt(self, event):
Jack Jansen7e0da901995-08-17 14:18:20 +0000353 (what, message, when, where, modifiers) = event
354 wid = WhichWindow(message)
355 if wid and self._windows.has_key(wid):
356 window = self._windows[wid]
357 window.do_rawupdate(wid, event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000358 else:
Jack Jansen7a583361995-08-14 12:39:54 +0000359 MacOS.HandleEvent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000360
Jack Jansen7e0da901995-08-17 14:18:20 +0000361 def do_activateEvt(self, event):
362 (what, message, when, where, modifiers) = event
Just van Rossum5763e071999-01-27 14:22:11 +0000363 wid = WhichWindow(message)
Jack Jansen7e0da901995-08-17 14:18:20 +0000364 if wid and self._windows.has_key(wid):
365 window = self._windows[wid]
366 window.do_activate(modifiers & 1, event)
367 else:
368 MacOS.HandleEvent(event)
Just van Rossum5763e071999-01-27 14:22:11 +0000369
Jack Jansen7e0da901995-08-17 14:18:20 +0000370 def do_osEvt(self, event):
371 (what, message, when, where, modifiers) = event
372 which = (message >> 24) & 0xff
373 if which == 1: # suspend/resume
374 self.do_suspendresume(event)
375 else:
376 if DEBUG:
377 print 'unknown osEvt:',
378 self.printevent(event)
Just van Rossum5763e071999-01-27 14:22:11 +0000379
Jack Jansen7e0da901995-08-17 14:18:20 +0000380 def do_suspendresume(self, event):
Jack Jansen7e0da901995-08-17 14:18:20 +0000381 (what, message, when, where, modifiers) = event
Just van Rossum5763e071999-01-27 14:22:11 +0000382 wid = FrontWindow()
383 if wid and self._windows.has_key(wid):
384 window = self._windows[wid]
Just van Rossum1a5eb041999-12-15 14:55:16 +0000385 window.do_activate(message & 1, event)
Just van Rossum5763e071999-01-27 14:22:11 +0000386
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000387 def do_kHighLevelEvent(self, event):
388 (what, message, when, where, modifiers) = event
Jack Jansen7a583361995-08-14 12:39:54 +0000389 if DEBUG:
390 print "High Level Event:",
391 self.printevent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000392 try:
393 AEProcessAppleEvent(event)
394 except:
395 print "AEProcessAppleEvent error:"
396 traceback.print_exc()
397
398 def do_unknownevent(self, event):
Jack Jansen7e0da901995-08-17 14:18:20 +0000399 if DEBUG:
400 print "Unhandled event:",
401 self.printevent(event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000402
403 def printevent(self, event):
404 (what, message, when, where, modifiers) = event
405 nicewhat = `what`
406 if eventname.has_key(what):
407 nicewhat = eventname[what]
408 print nicewhat,
409 if what == kHighLevelEvent:
410 h, v = where
411 print `ostypecode(message)`, hex(when), `ostypecode(h | (v<<16))`,
412 else:
413 print hex(message), hex(when), where,
414 print hex(modifiers)
415
416
417class MenuBar:
418 """Represent a set of menus in a menu bar.
419
420 Interface:
421
422 - (constructor)
423 - (destructor)
424 - addmenu
425 - addpopup (normally used internally)
426 - dispatch (called from Application)
427 """
428
429 nextid = 1 # Necessarily a class variable
430
431 def getnextid(self):
Jack Jansenb1667ef1996-09-26 16:17:08 +0000432 id = MenuBar.nextid
433 MenuBar.nextid = id+1
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000434 return id
435
Jack Jansenb1667ef1996-09-26 16:17:08 +0000436 def __init__(self, parent=None):
437 self.parent = parent
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000438 ClearMenuBar()
439 self.bar = GetMenuBar()
440 self.menus = {}
441
Jack Jansenb1667ef1996-09-26 16:17:08 +0000442 # XXX necessary?
443 def close(self):
444 self.parent = None
445 self.bar = None
Jack Jansen7b56aad1998-02-20 15:51:39 +0000446 self.menus = None
Jack Jansenb1667ef1996-09-26 16:17:08 +0000447
Jack Jansen52306a72001-12-10 16:08:14 +0000448 def addmenu(self, title, after = 0, id=None):
449 if id == None:
450 id = self.getnextid()
Jack Jansene3532151996-04-12 16:24:44 +0000451 if DEBUG: print 'Newmenu', title, id # XXXX
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000452 m = NewMenu(id, title)
453 m.InsertMenu(after)
Jack Jansenbb6193c1998-05-06 15:33:09 +0000454 if after >= 0:
455 if self.parent:
456 self.parent.needmenubarredraw = 1
457 else:
458 DrawMenuBar()
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000459 return id, m
Jack Jansendb9ff361996-03-12 13:32:03 +0000460
461 def delmenu(self, id):
Jack Jansene3532151996-04-12 16:24:44 +0000462 if DEBUG: print 'Delmenu', id # XXXX
Jack Jansendb9ff361996-03-12 13:32:03 +0000463 DeleteMenu(id)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000464
465 def addpopup(self, title = ''):
466 return self.addmenu(title, -1)
Jack Jansenb1667ef1996-09-26 16:17:08 +0000467
468# Useless:
469# def install(self):
470# if not self.bar: return
471# SetMenuBar(self.bar)
472# if self.parent:
473# self.parent.needmenubarredraw = 1
474# else:
475# DrawMenuBar()
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000476
Jack Jansenb1667ef1996-09-26 16:17:08 +0000477 def fixmenudimstate(self):
478 for m in self.menus.keys():
479 menu = self.menus[m]
480 if menu.__class__ == FrameWork.AppleMenu:
481 continue
482 for i in range(len(menu.items)):
483 label, shortcut, callback, kind = menu.items[i]
484 if type(callback) == types.StringType:
485 wid = Win.FrontWindow()
486 if wid and self.parent._windows.has_key(wid):
487 window = self.parent._windows[wid]
488 if hasattr(window, "domenu_" + callback):
Jack Jansenafd0aa62001-01-29 13:29:47 +0000489 menu.menu.EnableMenuItem(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 hasattr(self.parent, "domenu_" + callback):
Jack Jansenafd0aa62001-01-29 13:29:47 +0000495 menu.menu.EnableMenuItem(i + 1)
Jack Jansenb1667ef1996-09-26 16:17:08 +0000496 else:
Jack Jansenafd0aa62001-01-29 13:29:47 +0000497 menu.menu.DisableMenuItem(i + 1)
Jack Jansenb1667ef1996-09-26 16:17:08 +0000498 elif callback:
499 pass
500
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000501 def dispatch(self, id, item, window, event):
502 if self.menus.has_key(id):
503 self.menus[id].dispatch(id, item, window, event)
504 else:
Jack Jansen7a583361995-08-14 12:39:54 +0000505 if DEBUG: print "MenuBar.dispatch(%d, %d, %s, %s)" % \
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000506 (id, item, window, event)
507
508
509# XXX Need a way to get menus as resources and bind them to callbacks
510
511class Menu:
512 "One menu."
513
Jack Jansen52306a72001-12-10 16:08:14 +0000514 def __init__(self, bar, title, after=0, id=None):
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000515 self.bar = bar
Jack Jansen52306a72001-12-10 16:08:14 +0000516 self.id, self.menu = self.bar.addmenu(title, after, id)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000517 bar.menus[self.id] = self
518 self.items = []
Jack Jansen341d1fe1998-10-15 15:29:16 +0000519 self._parent = None
Jack Jansendb9ff361996-03-12 13:32:03 +0000520
521 def delete(self):
522 self.bar.delmenu(self.id)
523 del self.bar.menus[self.id]
Jack Jansen34d11f02000-03-07 23:40:13 +0000524 self.menu.DisposeMenu()
Jack Jansendb9ff361996-03-12 13:32:03 +0000525 del self.bar
526 del self.items
527 del self.menu
528 del self.id
Jack Jansen341d1fe1998-10-15 15:29:16 +0000529 del self._parent
Jack Jansen5c440271998-07-13 13:41:02 +0000530
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000531 def additem(self, label, shortcut=None, callback=None, kind=None):
532 self.menu.AppendMenu('x') # add a dummy string
Jack Jansen34d11f02000-03-07 23:40:13 +0000533 self.items.append((label, shortcut, callback, kind))
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000534 item = len(self.items)
Jack Jansene4b40381995-07-17 13:25:15 +0000535 self.menu.SetMenuItemText(item, label) # set the actual text
Jack Jansen13681b71999-12-14 15:45:53 +0000536 if shortcut and type(shortcut) == type(()):
537 modifiers, char = shortcut[:2]
538 self.menu.SetItemCmd(item, ord(char))
539 self.menu.SetMenuItemModifiers(item, modifiers)
540 if len(shortcut) > 2:
Jack Jansenc15e43a1999-12-15 15:45:23 +0000541 self.menu.SetMenuItemKeyGlyph(item, shortcut[2])
Jack Jansen13681b71999-12-14 15:45:53 +0000542 elif shortcut:
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000543 self.menu.SetItemCmd(item, ord(shortcut))
544 return item
Jack Jansen5c440271998-07-13 13:41:02 +0000545
546 def delitem(self, item):
547 if item != len(self.items):
548 raise 'Can only delete last item of a menu'
549 self.menu.DeleteMenuItem(item)
550 del self.items[item-1]
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000551
552 def addcheck(self, label, shortcut=None, callback=None):
553 return self.additem(label, shortcut, callback, 'check')
554
555 def addradio(self, label, shortcut=None, callback=None):
556 return self.additem(label, shortcut, callback, 'radio')
557
558 def addseparator(self):
559 self.menu.AppendMenu('(-')
Jack Jansen34d11f02000-03-07 23:40:13 +0000560 self.items.append(('', None, None, 'separator'))
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000561
562 def addsubmenu(self, label, title=''):
563 sub = Menu(self.bar, title, -1)
564 item = self.additem(label, '\x1B', None, 'submenu')
565 self.menu.SetItemMark(item, sub.id)
Jack Jansen341d1fe1998-10-15 15:29:16 +0000566 sub._parent = self
567 sub._parent_item = item
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000568 return sub
569
570 def dispatch(self, id, item, window, event):
Jack Jansenb1667ef1996-09-26 16:17:08 +0000571 title, shortcut, callback, mtype = self.items[item-1]
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000572 if callback:
Jack Jansenb1667ef1996-09-26 16:17:08 +0000573 if not self.bar.parent or type(callback) <> types.StringType:
574 menuhandler = callback
575 else:
576 # callback is string
577 wid = Win.FrontWindow()
578 if wid and self.bar.parent._windows.has_key(wid):
579 window = self.bar.parent._windows[wid]
580 if hasattr(window, "domenu_" + callback):
581 menuhandler = getattr(window, "domenu_" + callback)
582 elif hasattr(self.bar.parent, "domenu_" + callback):
583 menuhandler = getattr(self.bar.parent, "domenu_" + callback)
584 else:
585 # nothing we can do. we shouldn't have come this far
586 # since the menu item should have been disabled...
587 return
588 elif hasattr(self.bar.parent, "domenu_" + callback):
589 menuhandler = getattr(self.bar.parent, "domenu_" + callback)
590 else:
591 # nothing we can do. we shouldn't have come this far
592 # since the menu item should have been disabled...
593 return
594 menuhandler(id, item, window, event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000595
Jack Jansencef2c591996-04-11 15:39:01 +0000596 def enable(self, onoff):
597 if onoff:
Jack Jansenafd0aa62001-01-29 13:29:47 +0000598 self.menu.EnableMenuItem(0)
Jack Jansen341d1fe1998-10-15 15:29:16 +0000599 if self._parent:
Jack Jansenafd0aa62001-01-29 13:29:47 +0000600 self._parent.menu.EnableMenuItem(self._parent_item)
Jack Jansencef2c591996-04-11 15:39:01 +0000601 else:
Jack Jansenafd0aa62001-01-29 13:29:47 +0000602 self.menu.DisableMenuItem(0)
Jack Jansen341d1fe1998-10-15 15:29:16 +0000603 if self._parent:
Jack Jansenafd0aa62001-01-29 13:29:47 +0000604 self._parent.menu.DisableMenuItem(self._parent_item)
Jack Jansen5c440271998-07-13 13:41:02 +0000605 if self.bar and self.bar.parent:
606 self.bar.parent.needmenubarredraw = 1
Jack Jansenbb6193c1998-05-06 15:33:09 +0000607
608class PopupMenu(Menu):
609 def __init__(self, bar):
610 Menu.__init__(self, bar, '(popup)', -1)
611
612 def popup(self, x, y, event, default=1, window=None):
613 # NOTE that x and y are global coordinates, and they should probably
614 # be topleft of the button the user clicked (not mouse-coordinates),
615 # so the popup nicely overlaps.
616 reply = self.menu.PopUpMenuSelect(x, y, default)
617 if not reply:
618 return
619 id = (reply & 0xffff0000) >> 16
620 item = reply & 0xffff
621 if not window:
622 wid = Win.FrontWindow()
623 try:
624 window = self.bar.parent._windows[wid]
625 except:
626 pass # If we can't find the window we pass None
627 self.dispatch(id, item, window, event)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000628
629class MenuItem:
630 def __init__(self, menu, title, shortcut=None, callback=None, kind=None):
631 self.item = menu.additem(title, shortcut, callback)
Jack Jansendb9ff361996-03-12 13:32:03 +0000632 self.menu = menu
633
Jack Jansen5c440271998-07-13 13:41:02 +0000634 def delete(self):
635 self.menu.delitem(self.item)
636 del self.menu
637 del self.item
638
Jack Jansendb9ff361996-03-12 13:32:03 +0000639 def check(self, onoff):
Jack Jansenafd0aa62001-01-29 13:29:47 +0000640 self.menu.menu.CheckMenuItem(self.item, onoff)
Jack Jansencef2c591996-04-11 15:39:01 +0000641
642 def enable(self, onoff):
643 if onoff:
Jack Jansenafd0aa62001-01-29 13:29:47 +0000644 self.menu.menu.EnableMenuItem(self.item)
Jack Jansencef2c591996-04-11 15:39:01 +0000645 else:
Jack Jansenafd0aa62001-01-29 13:29:47 +0000646 self.menu.menu.DisableMenuItem(self.item)
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000647
648 def settext(self, text):
649 self.menu.menu.SetMenuItemText(self.item, text)
Jack Jansendb9ff361996-03-12 13:32:03 +0000650
Jack Jansen0f6dc5b1996-04-23 16:18:33 +0000651 def setstyle(self, style):
652 self.menu.menu.SetItemStyle(self.item, style)
653
654 def seticon(self, icon):
655 self.menu.menu.SetItemIcon(self.item, icon)
656
657 def setcmd(self, cmd):
658 self.menu.menu.SetItemCmd(self.item, cmd)
659
660 def setmark(self, cmd):
661 self.menu.menu.SetItemMark(self.item, cmd)
662
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000663
664class RadioItem(MenuItem):
665 def __init__(self, menu, title, shortcut=None, callback=None):
666 MenuItem.__init__(self, menu, title, shortcut, callback, 'radio')
667
668class CheckItem(MenuItem):
669 def __init__(self, menu, title, shortcut=None, callback=None):
670 MenuItem.__init__(self, menu, title, shortcut, callback, 'check')
671
672def Separator(menu):
673 menu.addseparator()
674
675def SubMenu(menu, label, title=''):
676 return menu.addsubmenu(label, title)
677
678
679class AppleMenu(Menu):
680
681 def __init__(self, bar, abouttext="About me...", aboutcallback=None):
Jack Jansen52306a72001-12-10 16:08:14 +0000682 Menu.__init__(self, bar, "\024", id=SIOUX_APPLEMENU_ID)
Jack Jansen01a2d9e2001-01-29 15:32:00 +0000683 if MacOS.runtimemodel == 'ppc':
Jack Jansen6b498de2001-03-15 14:39:03 +0000684 self.additem(abouttext, None, aboutcallback)
685 self.addseparator()
Jack Jansen01a2d9e2001-01-29 15:32:00 +0000686 self.menu.AppendResMenu('DRVR')
Jack Jansen6b498de2001-03-15 14:39:03 +0000687 else:
688 # Additem()'s tricks do not work for "apple" menu under Carbon
689 self.menu.InsertMenuItem(abouttext, 0)
690 self.items.append((abouttext, None, aboutcallback, None))
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000691
692 def dispatch(self, id, item, window, event):
693 if item == 1:
694 Menu.dispatch(self, id, item, window, event)
Jack Jansen01a2d9e2001-01-29 15:32:00 +0000695 elif MacOS.runtimemodel == 'ppc':
Jack Jansenc8a99491996-01-08 23:50:13 +0000696 name = self.menu.GetMenuItemText(item)
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +0000697 OpenDeskAcc(name)
698
Jack Jansen7e0da901995-08-17 14:18:20 +0000699class Window:
700 """A single window belonging to an application"""
701
702 def __init__(self, parent):
703 self.wid = None
704 self.parent = parent
705
Jack Jansenc8a99491996-01-08 23:50:13 +0000706 def open(self, bounds=(40, 40, 400, 400), resid=None):
707 if resid <> None:
708 self.wid = GetNewWindow(resid, -1)
709 else:
710 self.wid = NewWindow(bounds, self.__class__.__name__, 1,
Jack Jansended835c1996-07-26 14:01:07 +0000711 8, -1, 1, 0) # changed to proc id 8 to include zoom box. jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000712 self.do_postopen()
713
714 def do_postopen(self):
715 """Tell our parent we exist"""
716 self.parent.appendwindow(self.wid, self)
717
718 def close(self):
Jack Jansen7e0da901995-08-17 14:18:20 +0000719 self.do_postclose()
720
721 def do_postclose(self):
722 self.parent.removewindow(self.wid)
723 self.parent = None
724 self.wid = None
Jack Jansenc8a99491996-01-08 23:50:13 +0000725
726 def SetPort(self):
727 # Convinience method
728 SetPort(self.wid)
Jack Jansen73023402001-01-23 14:58:20 +0000729
730 def GetWindow(self):
731 return self.wid
Jack Jansen7e0da901995-08-17 14:18:20 +0000732
733 def do_inDrag(self, partcode, window, event):
734 where = event[3]
735 window.DragWindow(where, self.draglimit)
736
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000737 draglimit = screenbounds
Jack Jansen7e0da901995-08-17 14:18:20 +0000738
739 def do_inGoAway(self, partcode, window, event):
740 where = event[3]
741 if window.TrackGoAway(where):
742 self.close()
743
744 def do_inZoom(self, partcode, window, event):
745 (what, message, when, where, modifiers) = event
746 if window.TrackBox(where, partcode):
747 window.ZoomWindow(partcode, 1)
Jack Jansended835c1996-07-26 14:01:07 +0000748 rect = window.GetWindowUserState() # so that zoom really works... jvr
749 self.do_postresize(rect[2] - rect[0], rect[3] - rect[1], window) # jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000750
751 def do_inZoomIn(self, partcode, window, event):
752 SetPort(window) # !!!
753 self.do_inZoom(partcode, window, event)
754
755 def do_inZoomOut(self, partcode, window, event):
756 SetPort(window) # !!!
757 self.do_inZoom(partcode, window, event)
758
759 def do_inGrow(self, partcode, window, event):
760 (what, message, when, where, modifiers) = event
761 result = window.GrowWindow(where, self.growlimit)
762 if result:
763 height = (result>>16) & 0xffff # Hi word
764 width = result & 0xffff # Lo word
765 self.do_resize(width, height, window)
766
Jack Jansended835c1996-07-26 14:01:07 +0000767 growlimit = (50, 50, screenbounds[2] - screenbounds[0], screenbounds[3] - screenbounds[1]) # jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000768
769 def do_resize(self, width, height, window):
Jack Jansended835c1996-07-26 14:01:07 +0000770 l, t, r, b = self.wid.GetWindowPort().portRect # jvr, forGrowIcon
771 self.SetPort() # jvr
Jack Jansen73023402001-01-23 14:58:20 +0000772 self.wid.InvalWindowRect((r - SCROLLBARWIDTH + 1, b - SCROLLBARWIDTH + 1, r, b)) # jvr
Jack Jansended835c1996-07-26 14:01:07 +0000773 window.SizeWindow(width, height, 1) # changed updateFlag to true jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000774 self.do_postresize(width, height, window)
775
776 def do_postresize(self, width, height, window):
777 SetPort(window)
Jack Jansen73023402001-01-23 14:58:20 +0000778 self.wid.InvalWindowRect(window.GetWindowPort().portRect)
Jack Jansen7e0da901995-08-17 14:18:20 +0000779
780 def do_inContent(self, partcode, window, event):
781 #
782 # If we're not frontmost, select ourselves and wait for
783 # the activate event.
784 #
785 if FrontWindow() <> window:
786 window.SelectWindow()
787 return
788 # We are. Handle the event.
789 (what, message, when, where, modifiers) = event
790 SetPort(window)
791 local = GlobalToLocal(where)
792 self.do_contentclick(local, modifiers, event)
793
794 def do_contentclick(self, local, modifiers, event):
Jack Jansended835c1996-07-26 14:01:07 +0000795 if DEBUG:
796 print 'Click in contents at %s, modifiers %s'%(local, modifiers)
Jack Jansen7e0da901995-08-17 14:18:20 +0000797
798 def do_rawupdate(self, window, event):
799 if DEBUG: print "raw update for", window
Jack Jansenda38f2d1995-11-14 10:15:42 +0000800 SetPort(window)
Jack Jansen7e0da901995-08-17 14:18:20 +0000801 window.BeginUpdate()
802 self.do_update(window, event)
803 window.EndUpdate()
804
805 def do_update(self, window, event):
Jack Jansended835c1996-07-26 14:01:07 +0000806 if DEBUG:
807 import time
808 for i in range(8):
809 time.sleep(0.1)
810 InvertRgn(window.GetWindowPort().visRgn)
811 FillRgn(window.GetWindowPort().visRgn, qd.gray)
812 else:
813 EraseRgn(window.GetWindowPort().visRgn)
Jack Jansen7e0da901995-08-17 14:18:20 +0000814
815 def do_activate(self, activate, event):
816 if DEBUG: print 'Activate %d for %s'%(activate, self.wid)
817
818class ControlsWindow(Window):
819
820 def do_rawupdate(self, window, event):
821 if DEBUG: print "raw update for", window
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000822 SetPort(window)
Jack Jansen7e0da901995-08-17 14:18:20 +0000823 window.BeginUpdate()
824 self.do_update(window, event)
Jack Jansended835c1996-07-26 14:01:07 +0000825 #DrawControls(window) # jvr
826 UpdateControls(window, window.GetWindowPort().visRgn) # jvr
Jack Jansen7e0da901995-08-17 14:18:20 +0000827 window.DrawGrowIcon()
828 window.EndUpdate()
829
830 def do_controlhit(self, window, control, pcode, event):
831 if DEBUG: print "control hit in", window, "on", control, "; pcode =", pcode
832
833 def do_inContent(self, partcode, window, event):
Jack Jansenc4eec9f1996-04-19 16:00:28 +0000834 if FrontWindow() <> window:
835 window.SelectWindow()
836 return
Jack Jansen7e0da901995-08-17 14:18:20 +0000837 (what, message, when, where, modifiers) = event
Jack Jansenda38f2d1995-11-14 10:15:42 +0000838 SetPort(window) # XXXX Needed?
Jack Jansen7e0da901995-08-17 14:18:20 +0000839 local = GlobalToLocal(where)
Jack Jansen41e825a1998-05-28 14:22:48 +0000840 pcode, control = FindControl(local, window)
841 if pcode and control:
842 self.do_rawcontrolhit(window, control, pcode, local, event)
Jack Jansen7e0da901995-08-17 14:18:20 +0000843 else:
844 if DEBUG: print "FindControl(%s, %s) -> (%s, %s)" % \
Jack Jansen41e825a1998-05-28 14:22:48 +0000845 (local, window, pcode, control)
Jack Jansene3532151996-04-12 16:24:44 +0000846 self.do_contentclick(local, modifiers, event)
847
Jack Jansen41e825a1998-05-28 14:22:48 +0000848 def do_rawcontrolhit(self, window, control, pcode, local, event):
849 pcode = control.TrackControl(local)
850 if pcode:
851 self.do_controlhit(window, control, pcode, event)
852
Jack Jansene3532151996-04-12 16:24:44 +0000853class ScrolledWindow(ControlsWindow):
854 def __init__(self, parent):
855 self.barx = self.bary = None
Jack Jansen7bfc8751996-04-16 14:35:43 +0000856 self.barx_enabled = self.bary_enabled = 1
857 self.activated = 1
Jack Jansene3532151996-04-12 16:24:44 +0000858 ControlsWindow.__init__(self, parent)
859
860 def scrollbars(self, wantx=1, wanty=1):
861 SetPort(self.wid)
862 self.barx = self.bary = None
Jack Jansen7bfc8751996-04-16 14:35:43 +0000863 self.barx_enabled = self.bary_enabled = 1
Jack Jansene3532151996-04-12 16:24:44 +0000864 x0, y0, x1, y1 = self.wid.GetWindowPort().portRect
865 vx, vy = self.getscrollbarvalues()
Jack Jansen7bfc8751996-04-16 14:35:43 +0000866 if vx == None: self.barx_enabled, vx = 0, 0
867 if vy == None: self.bary_enabled, vy = 0, 0
Jack Jansene3532151996-04-12 16:24:44 +0000868 if wantx:
869 rect = x0-1, y1-(SCROLLBARWIDTH-1), x1-(SCROLLBARWIDTH-2), y1+1
870 self.barx = NewControl(self.wid, rect, "", 1, vx, 0, 32767, 16, 0)
Jack Jansen7bfc8751996-04-16 14:35:43 +0000871 if not self.barx_enabled: self.barx.HiliteControl(255)
Jack Jansen73023402001-01-23 14:58:20 +0000872## self.wid.InvalWindowRect(rect)
Jack Jansene3532151996-04-12 16:24:44 +0000873 if wanty:
874 rect = x1-(SCROLLBARWIDTH-1), y0-1, x1+1, y1-(SCROLLBARWIDTH-2)
875 self.bary = NewControl(self.wid, rect, "", 1, vy, 0, 32767, 16, 0)
Jack Jansen7bfc8751996-04-16 14:35:43 +0000876 if not self.bary_enabled: self.bary.HiliteControl(255)
Jack Jansen73023402001-01-23 14:58:20 +0000877## self.wid.InvalWindowRect(rect)
Jack Jansene3532151996-04-12 16:24:44 +0000878
879 def do_postclose(self):
880 self.barx = self.bary = None
881 ControlsWindow.do_postclose(self)
882
883 def do_activate(self, onoff, event):
Jack Jansen7bfc8751996-04-16 14:35:43 +0000884 self.activated = onoff
Jack Jansene3532151996-04-12 16:24:44 +0000885 if onoff:
Jack Jansen7bfc8751996-04-16 14:35:43 +0000886 if self.barx and self.barx_enabled:
Jack Jansended835c1996-07-26 14:01:07 +0000887 self.barx.ShowControl() # jvr
Jack Jansen7bfc8751996-04-16 14:35:43 +0000888 if self.bary and self.bary_enabled:
Jack Jansended835c1996-07-26 14:01:07 +0000889 self.bary.ShowControl() # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000890 else:
Jack Jansen7bfc8751996-04-16 14:35:43 +0000891 if self.barx:
Jack Jansended835c1996-07-26 14:01:07 +0000892 self.barx.HideControl() # jvr; An inactive window should have *hidden*
893 # scrollbars, not just dimmed (no matter what
894 # BBEdit does... look at the Finder)
Jack Jansen7bfc8751996-04-16 14:35:43 +0000895 if self.bary:
Jack Jansended835c1996-07-26 14:01:07 +0000896 self.bary.HideControl() # jvr
897 self.wid.DrawGrowIcon() # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000898
899 def do_postresize(self, width, height, window):
900 l, t, r, b = self.wid.GetWindowPort().portRect
Jack Jansended835c1996-07-26 14:01:07 +0000901 self.SetPort()
Jack Jansene3532151996-04-12 16:24:44 +0000902 if self.barx:
Jack Jansended835c1996-07-26 14:01:07 +0000903 self.barx.HideControl() # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000904 self.barx.MoveControl(l-1, b-(SCROLLBARWIDTH-1))
Jack Jansended835c1996-07-26 14:01:07 +0000905 self.barx.SizeControl((r-l)-(SCROLLBARWIDTH-3), SCROLLBARWIDTH) # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000906 if self.bary:
Jack Jansended835c1996-07-26 14:01:07 +0000907 self.bary.HideControl() # jvr
Jack Jansene3532151996-04-12 16:24:44 +0000908 self.bary.MoveControl(r-(SCROLLBARWIDTH-1), t-1)
Jack Jansended835c1996-07-26 14:01:07 +0000909 self.bary.SizeControl(SCROLLBARWIDTH, (b-t)-(SCROLLBARWIDTH-3)) # jvr
910 if self.barx:
911 self.barx.ShowControl() # jvr
Jack Jansen73023402001-01-23 14:58:20 +0000912 self.wid.ValidWindowRect((l, b - SCROLLBARWIDTH + 1, r - SCROLLBARWIDTH + 2, b)) # jvr
Jack Jansended835c1996-07-26 14:01:07 +0000913 if self.bary:
914 self.bary.ShowControl() # jvr
Jack Jansen73023402001-01-23 14:58:20 +0000915 self.wid.ValidWindowRect((r - SCROLLBARWIDTH + 1, t, r, b - SCROLLBARWIDTH + 2)) # jvr
916 self.wid.InvalWindowRect((r - SCROLLBARWIDTH + 1, b - SCROLLBARWIDTH + 1, r, b)) # jvr, growicon
Jack Jansene3532151996-04-12 16:24:44 +0000917
Jack Jansen41e825a1998-05-28 14:22:48 +0000918
919 def do_rawcontrolhit(self, window, control, pcode, local, event):
Jack Jansene3532151996-04-12 16:24:44 +0000920 if control == self.barx:
Jack Jansene3532151996-04-12 16:24:44 +0000921 which = 'x'
922 elif control == self.bary:
Jack Jansene3532151996-04-12 16:24:44 +0000923 which = 'y'
924 else:
925 return 0
Jack Jansen41e825a1998-05-28 14:22:48 +0000926 if pcode in (inUpButton, inDownButton, inPageUp, inPageDown):
927 # We do the work for the buttons and grey area in the tracker
928 dummy = control.TrackControl(local, self.do_controltrack)
929 else:
930 # but the thumb is handled here
931 pcode = control.TrackControl(local)
932 if pcode == inThumb:
933 value = control.GetControlValue()
934 print 'setbars', which, value #DBG
935 self.scrollbar_callback(which, 'set', value)
936 self.updatescrollbars()
937 else:
938 print 'funny part', pcode #DBG
939 return 1
940
941 def do_controltrack(self, control, pcode):
942 if control == self.barx:
943 which = 'x'
944 elif control == self.bary:
945 which = 'y'
946 else:
947 return
948
Jack Jansene3532151996-04-12 16:24:44 +0000949 if pcode == inUpButton:
950 what = '-'
951 elif pcode == inDownButton:
952 what = '+'
953 elif pcode == inPageUp:
954 what = '--'
955 elif pcode == inPageDown:
956 what = '++'
957 else:
Jack Jansen41e825a1998-05-28 14:22:48 +0000958 return
959 self.scrollbar_callback(which, what, None)
Jack Jansene3532151996-04-12 16:24:44 +0000960 self.updatescrollbars()
Jack Jansene3532151996-04-12 16:24:44 +0000961
962 def updatescrollbars(self):
963 SetPort(self.wid)
964 vx, vy = self.getscrollbarvalues()
965 if self.barx:
Jack Jansen7bfc8751996-04-16 14:35:43 +0000966 if vx == None:
967 self.barx.HiliteControl(255)
968 self.barx_enabled = 0
969 else:
970 if not self.barx_enabled:
971 self.barx_enabled = 1
972 if self.activated:
973 self.barx.HiliteControl(0)
974 self.barx.SetControlValue(vx)
Jack Jansene3532151996-04-12 16:24:44 +0000975 if self.bary:
Jack Jansen7bfc8751996-04-16 14:35:43 +0000976 if vy == None:
977 self.bary.HiliteControl(255)
978 self.bary_enabled = 0
979 else:
980 if not self.bary_enabled:
981 self.bary_enabled = 1
982 if self.activated:
983 self.bary.HiliteControl(0)
984 self.bary.SetControlValue(vy)
985
986 # Auxiliary function: convert standard text/image/etc coordinate
987 # to something palatable as getscrollbarvalues() return
988 def scalebarvalue(self, absmin, absmax, curmin, curmax):
989 if curmin <= absmin and curmax >= absmax:
990 return None
991 if curmin <= absmin:
992 return 0
993 if curmax >= absmax:
994 return 32767
995 perc = float(curmin-absmin)/float(absmax-absmin)
996 return int(perc*32767)
Jack Jansene3532151996-04-12 16:24:44 +0000997
998 # To be overridden:
999
1000 def getscrollbarvalues(self):
1001 return 0, 0
1002
1003 def scrollbar_callback(self, which, what, value):
1004 print 'scroll', which, what, value
Jack Jansen7e0da901995-08-17 14:18:20 +00001005
1006class DialogWindow(Window):
1007 """A modeless dialog window"""
1008
1009 def open(self, resid):
Jack Jansened24cd22001-02-14 17:07:04 +00001010 self.dlg = GetNewDialog(resid, -1)
1011 self.wid = self.dlg.GetDialogWindow()
Jack Jansen7e0da901995-08-17 14:18:20 +00001012 self.do_postopen()
1013
1014 def close(self):
Jack Jansen7e0da901995-08-17 14:18:20 +00001015 self.do_postclose()
1016
Jack Jansencbed91b2001-08-03 13:31:36 +00001017 def do_postclose(self):
1018 self.dlg = None
1019 Window.do_postclose(self)
1020
Jack Jansen7e0da901995-08-17 14:18:20 +00001021 def do_itemhit(self, item, event):
Jack Jansened24cd22001-02-14 17:07:04 +00001022 print 'Dialog %s, item %d hit'%(self.dlg, item)
Jack Jansen7e0da901995-08-17 14:18:20 +00001023
1024 def do_rawupdate(self, window, event):
1025 pass
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +00001026
1027def ostypecode(x):
1028 "Convert a long int to the 4-character code it really is"
1029 s = ''
1030 for i in range(4):
1031 x, c = divmod(x, 256)
1032 s = chr(c) + s
1033 return s
1034
1035
1036class TestApp(Application):
1037
1038 "This class is used by the test() function"
1039
1040 def makeusermenus(self):
1041 self.filemenu = m = Menu(self.menubar, "File")
1042 self.saveitem = MenuItem(m, "Save", "S", self.save)
1043 Separator(m)
1044 self.optionsmenu = mm = SubMenu(m, "Options")
Jack Jansen13681b71999-12-14 15:45:53 +00001045 self.opt1 = CheckItem(mm, "Arguments", "A")
1046 self.opt2 = CheckItem(mm, "Being hit on the head lessons", (kMenuOptionModifier, "A"))
1047 self.opt3 = CheckItem(mm, "Complaints", (kMenuOptionModifier|kMenuNoCommandModifier, "A"))
Guido van Rossum8f4b6ad1995-04-05 09:18:35 +00001048 Separator(m)
1049 self.quititem = MenuItem(m, "Quit", "Q", self.quit)
1050
1051 def save(self, *args):
1052 print "Save"
1053
1054 def quit(self, *args):
1055 raise self
1056
1057
1058def test():
1059 "Test program"
1060 app = TestApp()
1061 app.mainloop()
1062
1063
1064if __name__ == '__main__':
1065 test()