Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 1 | "A sort of application framework for the Mac" |
| 2 | |
Jack Jansen | 7a58336 | 1995-08-14 12:39:54 +0000 | [diff] [blame] | 3 | DEBUG=0 |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 4 | |
| 5 | import MacOS |
| 6 | import traceback |
| 7 | |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 8 | from AE import * |
| 9 | from AppleEvents import * |
Jack Jansen | 7a58336 | 1995-08-14 12:39:54 +0000 | [diff] [blame] | 10 | from Ctl import * |
| 11 | from Controls import * |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 12 | from Dlg import * |
| 13 | from Dialogs import * |
| 14 | from Evt import * |
| 15 | from Events import * |
| 16 | from Menu import * |
| 17 | from Menus import * |
Jack Jansen | 7a58336 | 1995-08-14 12:39:54 +0000 | [diff] [blame] | 18 | from Qd import * |
| 19 | from QuickDraw import * |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 20 | #from Res import * |
| 21 | #from Resources import * |
| 22 | #from Snd import * |
| 23 | #from Sound import * |
| 24 | from Win import * |
| 25 | from Windows import * |
Jack Jansen | b1667ef | 1996-09-26 16:17:08 +0000 | [diff] [blame] | 26 | import types |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 27 | |
| 28 | import EasyDialogs |
| 29 | |
| 30 | kHighLevelEvent = 23 # Don't know what header file this should come from |
Jack Jansen | e353215 | 1996-04-12 16:24:44 +0000 | [diff] [blame] | 31 | SCROLLBARWIDTH = 16 # Again, not a clue... |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 32 | |
| 33 | |
| 34 | # Map event 'what' field to strings |
| 35 | eventname = {} |
| 36 | eventname[1] = 'mouseDown' |
| 37 | eventname[2] = 'mouseUp' |
| 38 | eventname[3] = 'keyDown' |
| 39 | eventname[4] = 'keyUp' |
| 40 | eventname[5] = 'autoKey' |
| 41 | eventname[6] = 'updateEvt' |
| 42 | eventname[7] = 'diskEvt' |
| 43 | eventname[8] = 'activateEvt' |
| 44 | eventname[15] = 'osEvt' |
| 45 | eventname[23] = 'kHighLevelEvent' |
| 46 | |
| 47 | # Map part codes returned by WhichWindow() to strings |
| 48 | partname = {} |
| 49 | partname[0] = 'inDesk' |
| 50 | partname[1] = 'inMenuBar' |
| 51 | partname[2] = 'inSysWindow' |
| 52 | partname[3] = 'inContent' |
| 53 | partname[4] = 'inDrag' |
| 54 | partname[5] = 'inGrow' |
| 55 | partname[6] = 'inGoAway' |
| 56 | partname[7] = 'inZoomIn' |
| 57 | partname[8] = 'inZoomOut' |
| 58 | |
Jack Jansen | c4eec9f | 1996-04-19 16:00:28 +0000 | [diff] [blame] | 59 | # |
| 60 | # The useable portion of the screen |
Jack Jansen | ded835c | 1996-07-26 14:01:07 +0000 | [diff] [blame] | 61 | # ## but what happens with multiple screens? jvr |
Jack Jansen | c4eec9f | 1996-04-19 16:00:28 +0000 | [diff] [blame] | 62 | screenbounds = qd.screenBits.bounds |
| 63 | screenbounds = screenbounds[0]+4, screenbounds[1]+4, \ |
| 64 | screenbounds[2]-4, screenbounds[3]-4 |
| 65 | |
Jack Jansen | ded835c | 1996-07-26 14:01:07 +0000 | [diff] [blame] | 66 | next_window_x = 16 # jvr |
| 67 | next_window_y = 44 # jvr |
Jack Jansen | c4eec9f | 1996-04-19 16:00:28 +0000 | [diff] [blame] | 68 | |
| 69 | def 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 Jansen | ded835c | 1996-07-26 14:01:07 +0000 | [diff] [blame] | 74 | next_window_x = 16 |
Jack Jansen | c4eec9f | 1996-04-19 16:00:28 +0000 | [diff] [blame] | 75 | if b > screenbounds[3]: |
Jack Jansen | ded835c | 1996-07-26 14:01:07 +0000 | [diff] [blame] | 76 | next_window_y = 44 |
Jack Jansen | c4eec9f | 1996-04-19 16:00:28 +0000 | [diff] [blame] | 77 | l, t = next_window_x, next_window_y |
| 78 | r, b = next_window_x+width, next_window_y+height |
Jack Jansen | ded835c | 1996-07-26 14:01:07 +0000 | [diff] [blame] | 79 | next_window_x, next_window_y = next_window_x + 8, next_window_y + 20 # jvr |
Jack Jansen | c4eec9f | 1996-04-19 16:00:28 +0000 | [diff] [blame] | 80 | return l, t, r, b |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 81 | |
Jack Jansen | 4634130 | 1996-08-28 13:53:07 +0000 | [diff] [blame] | 82 | _watch = None |
| 83 | def setwatchcursor(): |
| 84 | global _watch |
| 85 | |
| 86 | if _watch == None: |
| 87 | _watch = GetCursor(4).data |
| 88 | SetCursor(_watch) |
| 89 | |
| 90 | def setarrowcursor(): |
| 91 | SetCursor(qd.arrow) |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 92 | |
| 93 | class Application: |
| 94 | |
| 95 | "Application framework -- your application should be a derived class" |
| 96 | |
Jack Jansen | 647535d | 1996-09-17 12:35:43 +0000 | [diff] [blame] | 97 | def __init__(self, nomenubar=0): |
| 98 | self.quitting = 0 |
Jack Jansen | b1667ef | 1996-09-26 16:17:08 +0000 | [diff] [blame] | 99 | self.needmenubarredraw = 0 |
Jack Jansen | 7e0da90 | 1995-08-17 14:18:20 +0000 | [diff] [blame] | 100 | self._windows = {} |
Jack Jansen | 647535d | 1996-09-17 12:35:43 +0000 | [diff] [blame] | 101 | if nomenubar: |
| 102 | self.menubar = None |
| 103 | else: |
| 104 | self.makemenubar() |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 105 | |
| 106 | def makemenubar(self): |
Jack Jansen | b1667ef | 1996-09-26 16:17:08 +0000 | [diff] [blame] | 107 | self.menubar = MenuBar(self) |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 108 | AppleMenu(self.menubar, self.getabouttext(), self.do_about) |
| 109 | self.makeusermenus() |
Jack Jansen | c8a9949 | 1996-01-08 23:50:13 +0000 | [diff] [blame] | 110 | |
| 111 | def makeusermenus(self): |
| 112 | self.filemenu = m = Menu(self.menubar, "File") |
| 113 | self._quititem = MenuItem(m, "Quit", "Q", self._quit) |
| 114 | |
| 115 | def _quit(self, *args): |
Jack Jansen | 647535d | 1996-09-17 12:35:43 +0000 | [diff] [blame] | 116 | self.quitting = 1 |
Jack Jansen | 7e0da90 | 1995-08-17 14:18:20 +0000 | [diff] [blame] | 117 | |
Jack Jansen | c75e1d0 | 1996-12-23 17:22:40 +0000 | [diff] [blame] | 118 | def cleanup(self): |
| 119 | for w in self._windows.values(): |
| 120 | w.do_close() |
| 121 | return self._windows == {} |
| 122 | |
Jack Jansen | 7e0da90 | 1995-08-17 14:18:20 +0000 | [diff] [blame] | 123 | def appendwindow(self, wid, window): |
| 124 | self._windows[wid] = window |
| 125 | |
| 126 | def removewindow(self, wid): |
| 127 | del self._windows[wid] |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 128 | |
| 129 | def getabouttext(self): |
| 130 | return "About %s..." % self.__class__.__name__ |
| 131 | |
| 132 | def do_about(self, id, item, window, event): |
| 133 | EasyDialogs.Message("Hello, world!" + "\015(%s)" % self.__class__.__name__) |
| 134 | |
| 135 | # The main event loop is broken up in several simple steps. |
| 136 | # This is done so you can override each individual part, |
| 137 | # if you have a need to do extra processing independent of the |
| 138 | # event type. |
| 139 | # Normally, however, you'd just define handlers for individual |
| 140 | # events. |
| 141 | # (XXX I'm not sure if using default parameter values is the right |
| 142 | # way to define the mask and wait time passed to WaitNextEvent.) |
| 143 | |
| 144 | def mainloop(self, mask = everyEvent, wait = 0): |
Jack Jansen | 647535d | 1996-09-17 12:35:43 +0000 | [diff] [blame] | 145 | self.quitting = 0 |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 146 | saveyield = MacOS.EnableAppswitch(self.yield) |
| 147 | try: |
Jack Jansen | 647535d | 1996-09-17 12:35:43 +0000 | [diff] [blame] | 148 | while not self.quitting: |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 149 | try: |
| 150 | self.do1event(mask, wait) |
| 151 | except (Application, SystemExit): |
Jack Jansen | 647535d | 1996-09-17 12:35:43 +0000 | [diff] [blame] | 152 | # Note: the raising of "self" is old-fashioned idiom to |
| 153 | # exit the mainloop. Calling _quit() is better for new |
| 154 | # applications. |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 155 | break |
| 156 | finally: |
| 157 | MacOS.EnableAppswitch(saveyield) |
| 158 | |
| 159 | yield = -1 |
| 160 | |
| 161 | def do1event(self, mask = everyEvent, wait = 0): |
Jack Jansen | 13dc4f7 | 1995-08-31 13:38:01 +0000 | [diff] [blame] | 162 | ok, event = self.getevent(mask, wait) |
| 163 | if IsDialogEvent(event): |
| 164 | if self.do_dialogevent(event): |
| 165 | return |
| 166 | if ok: |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 167 | self.dispatch(event) |
Jack Jansen | 3818678 | 1995-11-10 14:48:36 +0000 | [diff] [blame] | 168 | else: |
Jack Jansen | c4eec9f | 1996-04-19 16:00:28 +0000 | [diff] [blame] | 169 | self.idle(event) |
Jack Jansen | 3818678 | 1995-11-10 14:48:36 +0000 | [diff] [blame] | 170 | |
Jack Jansen | c4eec9f | 1996-04-19 16:00:28 +0000 | [diff] [blame] | 171 | def idle(self, event): |
Jack Jansen | 3818678 | 1995-11-10 14:48:36 +0000 | [diff] [blame] | 172 | pass |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 173 | |
| 174 | def getevent(self, mask = everyEvent, wait = 0): |
Jack Jansen | b1667ef | 1996-09-26 16:17:08 +0000 | [diff] [blame] | 175 | if self.needmenubarredraw: |
| 176 | DrawMenuBar() |
| 177 | self.needmenubarredraw = 0 |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 178 | ok, event = WaitNextEvent(mask, wait) |
Jack Jansen | 13dc4f7 | 1995-08-31 13:38:01 +0000 | [diff] [blame] | 179 | return ok, event |
| 180 | |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 181 | def dispatch(self, event): |
| 182 | (what, message, when, where, modifiers) = event |
| 183 | if eventname.has_key(what): |
| 184 | name = "do_" + eventname[what] |
| 185 | else: |
| 186 | name = "do_%d" % what |
| 187 | try: |
| 188 | handler = getattr(self, name) |
| 189 | except AttributeError: |
| 190 | handler = self.do_unknownevent |
| 191 | handler(event) |
Jack Jansen | 7e0da90 | 1995-08-17 14:18:20 +0000 | [diff] [blame] | 192 | |
| 193 | def do_dialogevent(self, event): |
| 194 | gotone, window, item = DialogSelect(event) |
| 195 | if gotone: |
| 196 | if self._windows.has_key(window): |
Jack Jansen | 13dc4f7 | 1995-08-31 13:38:01 +0000 | [diff] [blame] | 197 | self._windows[window].do_itemhit(item, event) |
Jack Jansen | 7e0da90 | 1995-08-17 14:18:20 +0000 | [diff] [blame] | 198 | else: |
| 199 | print 'Dialog event for unknown dialog' |
Jack Jansen | 13dc4f7 | 1995-08-31 13:38:01 +0000 | [diff] [blame] | 200 | return 1 |
| 201 | return 0 |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 202 | |
| 203 | def do_mouseDown(self, event): |
| 204 | (what, message, when, where, modifiers) = event |
Jack Jansen | 7e0da90 | 1995-08-17 14:18:20 +0000 | [diff] [blame] | 205 | partcode, wid = FindWindow(where) |
| 206 | |
| 207 | # |
| 208 | # Find the correct name. |
| 209 | # |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 210 | if partname.has_key(partcode): |
| 211 | name = "do_" + partname[partcode] |
| 212 | else: |
| 213 | name = "do_%d" % partcode |
Jack Jansen | 7e0da90 | 1995-08-17 14:18:20 +0000 | [diff] [blame] | 214 | |
| 215 | if wid == None: |
| 216 | # No window, or a non-python window |
| 217 | try: |
| 218 | handler = getattr(self, name) |
| 219 | except AttributeError: |
| 220 | # Not menubar or something, so assume someone |
| 221 | # else's window |
| 222 | MacOS.HandleEvent(event) |
| 223 | return |
| 224 | elif self._windows.has_key(wid): |
| 225 | # It is a window. Hand off to correct window. |
| 226 | window = self._windows[wid] |
| 227 | try: |
| 228 | handler = getattr(window, name) |
| 229 | except AttributeError: |
| 230 | handler = self.do_unknownpartcode |
| 231 | else: |
| 232 | # It is a python-toolbox window, but not ours. |
| 233 | handler = self.do_unknownwindow |
| 234 | handler(partcode, wid, event) |
| 235 | |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 236 | def do_inSysWindow(self, partcode, window, event): |
Jack Jansen | 7a58336 | 1995-08-14 12:39:54 +0000 | [diff] [blame] | 237 | MacOS.HandleEvent(event) |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 238 | |
| 239 | def do_inDesk(self, partcode, window, event): |
Jack Jansen | 7a58336 | 1995-08-14 12:39:54 +0000 | [diff] [blame] | 240 | MacOS.HandleEvent(event) |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 241 | |
| 242 | def do_inMenuBar(self, partcode, window, event): |
Jack Jansen | 647535d | 1996-09-17 12:35:43 +0000 | [diff] [blame] | 243 | if not self.menubar: |
| 244 | MacOS.HandleEvent(event) |
| 245 | return |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 246 | (what, message, when, where, modifiers) = event |
| 247 | result = MenuSelect(where) |
| 248 | id = (result>>16) & 0xffff # Hi word |
| 249 | item = result & 0xffff # Lo word |
| 250 | self.do_rawmenu(id, item, window, event) |
| 251 | |
| 252 | def do_rawmenu(self, id, item, window, event): |
| 253 | try: |
| 254 | self.do_menu(id, item, window, event) |
| 255 | finally: |
| 256 | HiliteMenu(0) |
| 257 | |
| 258 | def do_menu(self, id, item, window, event): |
| 259 | self.menubar.dispatch(id, item, window, event) |
| 260 | |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 261 | |
| 262 | def do_unknownpartcode(self, partcode, window, event): |
| 263 | (what, message, when, where, modifiers) = event |
Jack Jansen | 7a58336 | 1995-08-14 12:39:54 +0000 | [diff] [blame] | 264 | if DEBUG: print "Mouse down at global:", where |
| 265 | if DEBUG: print "\tUnknown part code:", partcode |
Jack Jansen | 7e0da90 | 1995-08-17 14:18:20 +0000 | [diff] [blame] | 266 | if DEBUG: print "\tEvent:", self.printevent(event) |
| 267 | MacOS.HandleEvent(event) |
| 268 | |
| 269 | def do_unknownwindow(self, partcode, window, event): |
| 270 | if DEBUG: print 'Unknown window:', window |
Jack Jansen | 7a58336 | 1995-08-14 12:39:54 +0000 | [diff] [blame] | 271 | MacOS.HandleEvent(event) |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 272 | |
| 273 | def do_keyDown(self, event): |
| 274 | self.do_key(event) |
| 275 | |
| 276 | def do_autoKey(self, event): |
| 277 | if not event[-1] & cmdKey: |
| 278 | self.do_key(event) |
| 279 | |
| 280 | def do_key(self, event): |
| 281 | (what, message, when, where, modifiers) = event |
| 282 | c = chr(message & charCodeMask) |
| 283 | if modifiers & cmdKey: |
| 284 | if c == '.': |
| 285 | raise self |
| 286 | else: |
Jack Jansen | 647535d | 1996-09-17 12:35:43 +0000 | [diff] [blame] | 287 | if not self.menubar: |
| 288 | MacOS.HandleEvent(event) |
| 289 | return |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 290 | result = MenuKey(ord(c)) |
| 291 | id = (result>>16) & 0xffff # Hi word |
| 292 | item = result & 0xffff # Lo word |
| 293 | if id: |
| 294 | self.do_rawmenu(id, item, None, event) |
Jack Jansen | 7e0da90 | 1995-08-17 14:18:20 +0000 | [diff] [blame] | 295 | # elif c == 'w': |
| 296 | # w = FrontWindow() |
| 297 | # if w: |
| 298 | # self.do_close(w) |
| 299 | # else: |
| 300 | # if DEBUG: print 'Command-W without front window' |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 301 | else: |
Jack Jansen | 7a58336 | 1995-08-14 12:39:54 +0000 | [diff] [blame] | 302 | if DEBUG: print "Command-" +`c` |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 303 | else: |
Jack Jansen | 7e0da90 | 1995-08-17 14:18:20 +0000 | [diff] [blame] | 304 | # See whether the front window wants it |
| 305 | w = FrontWindow() |
| 306 | if w and self._windows.has_key(w): |
| 307 | window = self._windows[w] |
| 308 | try: |
| 309 | do_char = window.do_char |
| 310 | except AttributeError: |
| 311 | do_char = self.do_char |
Jack Jansen | 6f47bf4 | 1995-12-12 15:03:35 +0000 | [diff] [blame] | 312 | do_char(c, event) |
| 313 | # else it wasn't for us, sigh... |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 314 | |
| 315 | def do_char(self, c, event): |
Jack Jansen | 7a58336 | 1995-08-14 12:39:54 +0000 | [diff] [blame] | 316 | if DEBUG: print "Character", `c` |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 317 | |
| 318 | def do_updateEvt(self, event): |
Jack Jansen | 7e0da90 | 1995-08-17 14:18:20 +0000 | [diff] [blame] | 319 | (what, message, when, where, modifiers) = event |
| 320 | wid = WhichWindow(message) |
| 321 | if wid and self._windows.has_key(wid): |
| 322 | window = self._windows[wid] |
| 323 | window.do_rawupdate(wid, event) |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 324 | else: |
Jack Jansen | 7a58336 | 1995-08-14 12:39:54 +0000 | [diff] [blame] | 325 | MacOS.HandleEvent(event) |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 326 | |
Jack Jansen | 7e0da90 | 1995-08-17 14:18:20 +0000 | [diff] [blame] | 327 | def do_activateEvt(self, event): |
| 328 | (what, message, when, where, modifiers) = event |
Jack Jansen | c8a9949 | 1996-01-08 23:50:13 +0000 | [diff] [blame] | 329 | # XXXX Incorrect, should be fixed in suspendresume |
| 330 | if type(message) == type(1): |
| 331 | wid = WhichWindow(message) |
| 332 | else: |
| 333 | wid = message |
Jack Jansen | 7e0da90 | 1995-08-17 14:18:20 +0000 | [diff] [blame] | 334 | if wid and self._windows.has_key(wid): |
| 335 | window = self._windows[wid] |
| 336 | window.do_activate(modifiers & 1, event) |
| 337 | else: |
| 338 | MacOS.HandleEvent(event) |
| 339 | |
| 340 | def do_osEvt(self, event): |
| 341 | (what, message, when, where, modifiers) = event |
| 342 | which = (message >> 24) & 0xff |
| 343 | if which == 1: # suspend/resume |
| 344 | self.do_suspendresume(event) |
| 345 | else: |
| 346 | if DEBUG: |
| 347 | print 'unknown osEvt:', |
| 348 | self.printevent(event) |
| 349 | |
| 350 | def do_suspendresume(self, event): |
| 351 | # Is this a good idea??? |
| 352 | (what, message, when, where, modifiers) = event |
| 353 | w = FrontWindow() |
| 354 | if w: |
Jack Jansen | c8a9949 | 1996-01-08 23:50:13 +0000 | [diff] [blame] | 355 | # XXXX Incorrect, should stuff windowptr into message field |
Jack Jansen | 7e0da90 | 1995-08-17 14:18:20 +0000 | [diff] [blame] | 356 | nev = (activateEvt, w, when, where, message&1) |
Jack Jansen | c8a9949 | 1996-01-08 23:50:13 +0000 | [diff] [blame] | 357 | self.do_activateEvt(nev) |
Jack Jansen | 7e0da90 | 1995-08-17 14:18:20 +0000 | [diff] [blame] | 358 | |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 359 | def do_kHighLevelEvent(self, event): |
| 360 | (what, message, when, where, modifiers) = event |
Jack Jansen | 7a58336 | 1995-08-14 12:39:54 +0000 | [diff] [blame] | 361 | if DEBUG: |
| 362 | print "High Level Event:", |
| 363 | self.printevent(event) |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 364 | try: |
| 365 | AEProcessAppleEvent(event) |
| 366 | except: |
| 367 | print "AEProcessAppleEvent error:" |
| 368 | traceback.print_exc() |
| 369 | |
| 370 | def do_unknownevent(self, event): |
Jack Jansen | 7e0da90 | 1995-08-17 14:18:20 +0000 | [diff] [blame] | 371 | if DEBUG: |
| 372 | print "Unhandled event:", |
| 373 | self.printevent(event) |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 374 | |
| 375 | def printevent(self, event): |
| 376 | (what, message, when, where, modifiers) = event |
| 377 | nicewhat = `what` |
| 378 | if eventname.has_key(what): |
| 379 | nicewhat = eventname[what] |
| 380 | print nicewhat, |
| 381 | if what == kHighLevelEvent: |
| 382 | h, v = where |
| 383 | print `ostypecode(message)`, hex(when), `ostypecode(h | (v<<16))`, |
| 384 | else: |
| 385 | print hex(message), hex(when), where, |
| 386 | print hex(modifiers) |
| 387 | |
| 388 | |
| 389 | class MenuBar: |
| 390 | """Represent a set of menus in a menu bar. |
| 391 | |
| 392 | Interface: |
| 393 | |
| 394 | - (constructor) |
| 395 | - (destructor) |
| 396 | - addmenu |
| 397 | - addpopup (normally used internally) |
| 398 | - dispatch (called from Application) |
| 399 | """ |
| 400 | |
| 401 | nextid = 1 # Necessarily a class variable |
| 402 | |
| 403 | def getnextid(self): |
Jack Jansen | b1667ef | 1996-09-26 16:17:08 +0000 | [diff] [blame] | 404 | id = MenuBar.nextid |
| 405 | MenuBar.nextid = id+1 |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 406 | return id |
| 407 | |
Jack Jansen | b1667ef | 1996-09-26 16:17:08 +0000 | [diff] [blame] | 408 | def __init__(self, parent=None): |
| 409 | self.parent = parent |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 410 | ClearMenuBar() |
| 411 | self.bar = GetMenuBar() |
| 412 | self.menus = {} |
| 413 | |
Jack Jansen | b1667ef | 1996-09-26 16:17:08 +0000 | [diff] [blame] | 414 | # XXX necessary? |
| 415 | def close(self): |
| 416 | self.parent = None |
| 417 | self.bar = None |
| 418 | |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 419 | def addmenu(self, title, after = 0): |
| 420 | id = self.getnextid() |
Jack Jansen | e353215 | 1996-04-12 16:24:44 +0000 | [diff] [blame] | 421 | if DEBUG: print 'Newmenu', title, id # XXXX |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 422 | m = NewMenu(id, title) |
| 423 | m.InsertMenu(after) |
Jack Jansen | b1667ef | 1996-09-26 16:17:08 +0000 | [diff] [blame] | 424 | if self.parent: |
| 425 | self.parent.needmenubarredraw = 1 |
| 426 | else: |
| 427 | DrawMenuBar() |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 428 | return id, m |
Jack Jansen | db9ff36 | 1996-03-12 13:32:03 +0000 | [diff] [blame] | 429 | |
| 430 | def delmenu(self, id): |
Jack Jansen | e353215 | 1996-04-12 16:24:44 +0000 | [diff] [blame] | 431 | if DEBUG: print 'Delmenu', id # XXXX |
Jack Jansen | db9ff36 | 1996-03-12 13:32:03 +0000 | [diff] [blame] | 432 | DeleteMenu(id) |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 433 | |
| 434 | def addpopup(self, title = ''): |
| 435 | return self.addmenu(title, -1) |
Jack Jansen | b1667ef | 1996-09-26 16:17:08 +0000 | [diff] [blame] | 436 | |
| 437 | # Useless: |
| 438 | # def install(self): |
| 439 | # if not self.bar: return |
| 440 | # SetMenuBar(self.bar) |
| 441 | # if self.parent: |
| 442 | # self.parent.needmenubarredraw = 1 |
| 443 | # else: |
| 444 | # DrawMenuBar() |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 445 | |
Jack Jansen | b1667ef | 1996-09-26 16:17:08 +0000 | [diff] [blame] | 446 | def fixmenudimstate(self): |
| 447 | for m in self.menus.keys(): |
| 448 | menu = self.menus[m] |
| 449 | if menu.__class__ == FrameWork.AppleMenu: |
| 450 | continue |
| 451 | for i in range(len(menu.items)): |
| 452 | label, shortcut, callback, kind = menu.items[i] |
| 453 | if type(callback) == types.StringType: |
| 454 | wid = Win.FrontWindow() |
| 455 | if wid and self.parent._windows.has_key(wid): |
| 456 | window = self.parent._windows[wid] |
| 457 | if hasattr(window, "domenu_" + callback): |
| 458 | menu.menu.EnableItem(i + 1) |
| 459 | elif hasattr(self.parent, "domenu_" + callback): |
| 460 | menu.menu.EnableItem(i + 1) |
| 461 | else: |
| 462 | menu.menu.DisableItem(i + 1) |
| 463 | elif hasattr(self.parent, "domenu_" + callback): |
| 464 | menu.menu.EnableItem(i + 1) |
| 465 | else: |
| 466 | menu.menu.DisableItem(i + 1) |
| 467 | elif callback: |
| 468 | pass |
| 469 | |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 470 | def dispatch(self, id, item, window, event): |
| 471 | if self.menus.has_key(id): |
| 472 | self.menus[id].dispatch(id, item, window, event) |
| 473 | else: |
Jack Jansen | 7a58336 | 1995-08-14 12:39:54 +0000 | [diff] [blame] | 474 | if DEBUG: print "MenuBar.dispatch(%d, %d, %s, %s)" % \ |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 475 | (id, item, window, event) |
| 476 | |
| 477 | |
| 478 | # XXX Need a way to get menus as resources and bind them to callbacks |
| 479 | |
| 480 | class Menu: |
| 481 | "One menu." |
| 482 | |
| 483 | def __init__(self, bar, title, after=0): |
| 484 | self.bar = bar |
| 485 | self.id, self.menu = self.bar.addmenu(title, after) |
| 486 | bar.menus[self.id] = self |
| 487 | self.items = [] |
Jack Jansen | db9ff36 | 1996-03-12 13:32:03 +0000 | [diff] [blame] | 488 | |
| 489 | def delete(self): |
| 490 | self.bar.delmenu(self.id) |
| 491 | del self.bar.menus[self.id] |
| 492 | del self.bar |
| 493 | del self.items |
| 494 | del self.menu |
| 495 | del self.id |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 496 | |
| 497 | def additem(self, label, shortcut=None, callback=None, kind=None): |
| 498 | self.menu.AppendMenu('x') # add a dummy string |
| 499 | self.items.append(label, shortcut, callback, kind) |
| 500 | item = len(self.items) |
Jack Jansen | e4b4038 | 1995-07-17 13:25:15 +0000 | [diff] [blame] | 501 | self.menu.SetMenuItemText(item, label) # set the actual text |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 502 | if shortcut: |
| 503 | self.menu.SetItemCmd(item, ord(shortcut)) |
| 504 | return item |
| 505 | |
| 506 | def addcheck(self, label, shortcut=None, callback=None): |
| 507 | return self.additem(label, shortcut, callback, 'check') |
| 508 | |
| 509 | def addradio(self, label, shortcut=None, callback=None): |
| 510 | return self.additem(label, shortcut, callback, 'radio') |
| 511 | |
| 512 | def addseparator(self): |
| 513 | self.menu.AppendMenu('(-') |
| 514 | self.items.append('', None, None, 'separator') |
| 515 | |
| 516 | def addsubmenu(self, label, title=''): |
| 517 | sub = Menu(self.bar, title, -1) |
| 518 | item = self.additem(label, '\x1B', None, 'submenu') |
| 519 | self.menu.SetItemMark(item, sub.id) |
| 520 | return sub |
| 521 | |
| 522 | def dispatch(self, id, item, window, event): |
Jack Jansen | b1667ef | 1996-09-26 16:17:08 +0000 | [diff] [blame] | 523 | title, shortcut, callback, mtype = self.items[item-1] |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 524 | if callback: |
Jack Jansen | b1667ef | 1996-09-26 16:17:08 +0000 | [diff] [blame] | 525 | if not self.bar.parent or type(callback) <> types.StringType: |
| 526 | menuhandler = callback |
| 527 | else: |
| 528 | # callback is string |
| 529 | wid = Win.FrontWindow() |
| 530 | if wid and self.bar.parent._windows.has_key(wid): |
| 531 | window = self.bar.parent._windows[wid] |
| 532 | if hasattr(window, "domenu_" + callback): |
| 533 | menuhandler = getattr(window, "domenu_" + callback) |
| 534 | elif hasattr(self.bar.parent, "domenu_" + callback): |
| 535 | menuhandler = getattr(self.bar.parent, "domenu_" + callback) |
| 536 | else: |
| 537 | # nothing we can do. we shouldn't have come this far |
| 538 | # since the menu item should have been disabled... |
| 539 | return |
| 540 | elif hasattr(self.bar.parent, "domenu_" + callback): |
| 541 | menuhandler = getattr(self.bar.parent, "domenu_" + callback) |
| 542 | else: |
| 543 | # nothing we can do. we shouldn't have come this far |
| 544 | # since the menu item should have been disabled... |
| 545 | return |
| 546 | menuhandler(id, item, window, event) |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 547 | |
Jack Jansen | cef2c59 | 1996-04-11 15:39:01 +0000 | [diff] [blame] | 548 | def enable(self, onoff): |
| 549 | if onoff: |
| 550 | self.menu.EnableItem(0) |
| 551 | else: |
| 552 | self.menu.DisableItem(0) |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 553 | |
| 554 | class MenuItem: |
| 555 | def __init__(self, menu, title, shortcut=None, callback=None, kind=None): |
| 556 | self.item = menu.additem(title, shortcut, callback) |
Jack Jansen | db9ff36 | 1996-03-12 13:32:03 +0000 | [diff] [blame] | 557 | self.menu = menu |
| 558 | |
| 559 | def check(self, onoff): |
| 560 | self.menu.menu.CheckItem(self.item, onoff) |
Jack Jansen | cef2c59 | 1996-04-11 15:39:01 +0000 | [diff] [blame] | 561 | |
| 562 | def enable(self, onoff): |
| 563 | if onoff: |
| 564 | self.menu.menu.EnableItem(self.item) |
| 565 | else: |
| 566 | self.menu.menu.DisableItem(self.item) |
Jack Jansen | c4eec9f | 1996-04-19 16:00:28 +0000 | [diff] [blame] | 567 | |
| 568 | def settext(self, text): |
| 569 | self.menu.menu.SetMenuItemText(self.item, text) |
Jack Jansen | db9ff36 | 1996-03-12 13:32:03 +0000 | [diff] [blame] | 570 | |
Jack Jansen | 0f6dc5b | 1996-04-23 16:18:33 +0000 | [diff] [blame] | 571 | def setstyle(self, style): |
| 572 | self.menu.menu.SetItemStyle(self.item, style) |
| 573 | |
| 574 | def seticon(self, icon): |
| 575 | self.menu.menu.SetItemIcon(self.item, icon) |
| 576 | |
| 577 | def setcmd(self, cmd): |
| 578 | self.menu.menu.SetItemCmd(self.item, cmd) |
| 579 | |
| 580 | def setmark(self, cmd): |
| 581 | self.menu.menu.SetItemMark(self.item, cmd) |
| 582 | |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 583 | |
| 584 | class RadioItem(MenuItem): |
| 585 | def __init__(self, menu, title, shortcut=None, callback=None): |
| 586 | MenuItem.__init__(self, menu, title, shortcut, callback, 'radio') |
| 587 | |
| 588 | class CheckItem(MenuItem): |
| 589 | def __init__(self, menu, title, shortcut=None, callback=None): |
| 590 | MenuItem.__init__(self, menu, title, shortcut, callback, 'check') |
| 591 | |
| 592 | def Separator(menu): |
| 593 | menu.addseparator() |
| 594 | |
| 595 | def SubMenu(menu, label, title=''): |
| 596 | return menu.addsubmenu(label, title) |
| 597 | |
| 598 | |
| 599 | class AppleMenu(Menu): |
| 600 | |
| 601 | def __init__(self, bar, abouttext="About me...", aboutcallback=None): |
| 602 | Menu.__init__(self, bar, "\024") |
| 603 | self.additem(abouttext, None, aboutcallback) |
| 604 | self.addseparator() |
Jack Jansen | e4b4038 | 1995-07-17 13:25:15 +0000 | [diff] [blame] | 605 | self.menu.AppendResMenu('DRVR') |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 606 | |
| 607 | def dispatch(self, id, item, window, event): |
| 608 | if item == 1: |
| 609 | Menu.dispatch(self, id, item, window, event) |
| 610 | else: |
Jack Jansen | c8a9949 | 1996-01-08 23:50:13 +0000 | [diff] [blame] | 611 | name = self.menu.GetMenuItemText(item) |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 612 | OpenDeskAcc(name) |
| 613 | |
Jack Jansen | 7e0da90 | 1995-08-17 14:18:20 +0000 | [diff] [blame] | 614 | class Window: |
| 615 | """A single window belonging to an application""" |
| 616 | |
| 617 | def __init__(self, parent): |
| 618 | self.wid = None |
| 619 | self.parent = parent |
| 620 | |
Jack Jansen | c8a9949 | 1996-01-08 23:50:13 +0000 | [diff] [blame] | 621 | def open(self, bounds=(40, 40, 400, 400), resid=None): |
| 622 | if resid <> None: |
| 623 | self.wid = GetNewWindow(resid, -1) |
| 624 | else: |
| 625 | self.wid = NewWindow(bounds, self.__class__.__name__, 1, |
Jack Jansen | ded835c | 1996-07-26 14:01:07 +0000 | [diff] [blame] | 626 | 8, -1, 1, 0) # changed to proc id 8 to include zoom box. jvr |
Jack Jansen | 7e0da90 | 1995-08-17 14:18:20 +0000 | [diff] [blame] | 627 | self.do_postopen() |
| 628 | |
| 629 | def do_postopen(self): |
| 630 | """Tell our parent we exist""" |
| 631 | self.parent.appendwindow(self.wid, self) |
| 632 | |
| 633 | def close(self): |
Jack Jansen | 7e0da90 | 1995-08-17 14:18:20 +0000 | [diff] [blame] | 634 | self.do_postclose() |
| 635 | |
| 636 | def do_postclose(self): |
| 637 | self.parent.removewindow(self.wid) |
| 638 | self.parent = None |
| 639 | self.wid = None |
Jack Jansen | c8a9949 | 1996-01-08 23:50:13 +0000 | [diff] [blame] | 640 | |
| 641 | def SetPort(self): |
| 642 | # Convinience method |
| 643 | SetPort(self.wid) |
Jack Jansen | 7e0da90 | 1995-08-17 14:18:20 +0000 | [diff] [blame] | 644 | |
| 645 | def do_inDrag(self, partcode, window, event): |
| 646 | where = event[3] |
| 647 | window.DragWindow(where, self.draglimit) |
| 648 | |
Jack Jansen | c4eec9f | 1996-04-19 16:00:28 +0000 | [diff] [blame] | 649 | draglimit = screenbounds |
Jack Jansen | 7e0da90 | 1995-08-17 14:18:20 +0000 | [diff] [blame] | 650 | |
| 651 | def do_inGoAway(self, partcode, window, event): |
| 652 | where = event[3] |
| 653 | if window.TrackGoAway(where): |
| 654 | self.close() |
| 655 | |
| 656 | def do_inZoom(self, partcode, window, event): |
| 657 | (what, message, when, where, modifiers) = event |
| 658 | if window.TrackBox(where, partcode): |
| 659 | window.ZoomWindow(partcode, 1) |
Jack Jansen | ded835c | 1996-07-26 14:01:07 +0000 | [diff] [blame] | 660 | rect = window.GetWindowUserState() # so that zoom really works... jvr |
| 661 | self.do_postresize(rect[2] - rect[0], rect[3] - rect[1], window) # jvr |
Jack Jansen | 7e0da90 | 1995-08-17 14:18:20 +0000 | [diff] [blame] | 662 | |
| 663 | def do_inZoomIn(self, partcode, window, event): |
| 664 | SetPort(window) # !!! |
| 665 | self.do_inZoom(partcode, window, event) |
| 666 | |
| 667 | def do_inZoomOut(self, partcode, window, event): |
| 668 | SetPort(window) # !!! |
| 669 | self.do_inZoom(partcode, window, event) |
| 670 | |
| 671 | def do_inGrow(self, partcode, window, event): |
| 672 | (what, message, when, where, modifiers) = event |
| 673 | result = window.GrowWindow(where, self.growlimit) |
| 674 | if result: |
| 675 | height = (result>>16) & 0xffff # Hi word |
| 676 | width = result & 0xffff # Lo word |
| 677 | self.do_resize(width, height, window) |
| 678 | |
Jack Jansen | ded835c | 1996-07-26 14:01:07 +0000 | [diff] [blame] | 679 | growlimit = (50, 50, screenbounds[2] - screenbounds[0], screenbounds[3] - screenbounds[1]) # jvr |
Jack Jansen | 7e0da90 | 1995-08-17 14:18:20 +0000 | [diff] [blame] | 680 | |
| 681 | def do_resize(self, width, height, window): |
Jack Jansen | ded835c | 1996-07-26 14:01:07 +0000 | [diff] [blame] | 682 | l, t, r, b = self.wid.GetWindowPort().portRect # jvr, forGrowIcon |
| 683 | self.SetPort() # jvr |
| 684 | InvalRect((r - SCROLLBARWIDTH + 1, b - SCROLLBARWIDTH + 1, r, b)) # jvr |
| 685 | window.SizeWindow(width, height, 1) # changed updateFlag to true jvr |
Jack Jansen | 7e0da90 | 1995-08-17 14:18:20 +0000 | [diff] [blame] | 686 | self.do_postresize(width, height, window) |
| 687 | |
| 688 | def do_postresize(self, width, height, window): |
| 689 | SetPort(window) |
Jack Jansen | c4eec9f | 1996-04-19 16:00:28 +0000 | [diff] [blame] | 690 | InvalRect(window.GetWindowPort().portRect) |
Jack Jansen | 7e0da90 | 1995-08-17 14:18:20 +0000 | [diff] [blame] | 691 | |
| 692 | def do_inContent(self, partcode, window, event): |
| 693 | # |
| 694 | # If we're not frontmost, select ourselves and wait for |
| 695 | # the activate event. |
| 696 | # |
| 697 | if FrontWindow() <> window: |
| 698 | window.SelectWindow() |
| 699 | return |
| 700 | # We are. Handle the event. |
| 701 | (what, message, when, where, modifiers) = event |
| 702 | SetPort(window) |
| 703 | local = GlobalToLocal(where) |
| 704 | self.do_contentclick(local, modifiers, event) |
| 705 | |
| 706 | def do_contentclick(self, local, modifiers, event): |
Jack Jansen | ded835c | 1996-07-26 14:01:07 +0000 | [diff] [blame] | 707 | if DEBUG: |
| 708 | print 'Click in contents at %s, modifiers %s'%(local, modifiers) |
Jack Jansen | 7e0da90 | 1995-08-17 14:18:20 +0000 | [diff] [blame] | 709 | |
| 710 | def do_rawupdate(self, window, event): |
| 711 | if DEBUG: print "raw update for", window |
Jack Jansen | da38f2d | 1995-11-14 10:15:42 +0000 | [diff] [blame] | 712 | SetPort(window) |
Jack Jansen | 7e0da90 | 1995-08-17 14:18:20 +0000 | [diff] [blame] | 713 | window.BeginUpdate() |
| 714 | self.do_update(window, event) |
| 715 | window.EndUpdate() |
| 716 | |
| 717 | def do_update(self, window, event): |
Jack Jansen | ded835c | 1996-07-26 14:01:07 +0000 | [diff] [blame] | 718 | if DEBUG: |
| 719 | import time |
| 720 | for i in range(8): |
| 721 | time.sleep(0.1) |
| 722 | InvertRgn(window.GetWindowPort().visRgn) |
| 723 | FillRgn(window.GetWindowPort().visRgn, qd.gray) |
| 724 | else: |
| 725 | EraseRgn(window.GetWindowPort().visRgn) |
Jack Jansen | 7e0da90 | 1995-08-17 14:18:20 +0000 | [diff] [blame] | 726 | |
| 727 | def do_activate(self, activate, event): |
| 728 | if DEBUG: print 'Activate %d for %s'%(activate, self.wid) |
| 729 | |
| 730 | class ControlsWindow(Window): |
| 731 | |
| 732 | def do_rawupdate(self, window, event): |
| 733 | if DEBUG: print "raw update for", window |
Jack Jansen | c4eec9f | 1996-04-19 16:00:28 +0000 | [diff] [blame] | 734 | SetPort(window) |
Jack Jansen | 7e0da90 | 1995-08-17 14:18:20 +0000 | [diff] [blame] | 735 | window.BeginUpdate() |
| 736 | self.do_update(window, event) |
Jack Jansen | ded835c | 1996-07-26 14:01:07 +0000 | [diff] [blame] | 737 | #DrawControls(window) # jvr |
| 738 | UpdateControls(window, window.GetWindowPort().visRgn) # jvr |
Jack Jansen | 7e0da90 | 1995-08-17 14:18:20 +0000 | [diff] [blame] | 739 | window.DrawGrowIcon() |
| 740 | window.EndUpdate() |
| 741 | |
| 742 | def do_controlhit(self, window, control, pcode, event): |
| 743 | if DEBUG: print "control hit in", window, "on", control, "; pcode =", pcode |
| 744 | |
| 745 | def do_inContent(self, partcode, window, event): |
Jack Jansen | c4eec9f | 1996-04-19 16:00:28 +0000 | [diff] [blame] | 746 | if FrontWindow() <> window: |
| 747 | window.SelectWindow() |
| 748 | return |
Jack Jansen | 7e0da90 | 1995-08-17 14:18:20 +0000 | [diff] [blame] | 749 | (what, message, when, where, modifiers) = event |
Jack Jansen | da38f2d | 1995-11-14 10:15:42 +0000 | [diff] [blame] | 750 | SetPort(window) # XXXX Needed? |
Jack Jansen | 7e0da90 | 1995-08-17 14:18:20 +0000 | [diff] [blame] | 751 | local = GlobalToLocal(where) |
| 752 | ctltype, control = FindControl(local, window) |
| 753 | if ctltype and control: |
| 754 | pcode = control.TrackControl(local) |
| 755 | if pcode: |
| 756 | self.do_controlhit(window, control, pcode, event) |
| 757 | else: |
| 758 | if DEBUG: print "FindControl(%s, %s) -> (%s, %s)" % \ |
| 759 | (local, window, ctltype, control) |
Jack Jansen | e353215 | 1996-04-12 16:24:44 +0000 | [diff] [blame] | 760 | self.do_contentclick(local, modifiers, event) |
| 761 | |
| 762 | class ScrolledWindow(ControlsWindow): |
| 763 | def __init__(self, parent): |
| 764 | self.barx = self.bary = None |
Jack Jansen | 7bfc875 | 1996-04-16 14:35:43 +0000 | [diff] [blame] | 765 | self.barx_enabled = self.bary_enabled = 1 |
| 766 | self.activated = 1 |
Jack Jansen | e353215 | 1996-04-12 16:24:44 +0000 | [diff] [blame] | 767 | ControlsWindow.__init__(self, parent) |
| 768 | |
| 769 | def scrollbars(self, wantx=1, wanty=1): |
| 770 | SetPort(self.wid) |
| 771 | self.barx = self.bary = None |
Jack Jansen | 7bfc875 | 1996-04-16 14:35:43 +0000 | [diff] [blame] | 772 | self.barx_enabled = self.bary_enabled = 1 |
Jack Jansen | e353215 | 1996-04-12 16:24:44 +0000 | [diff] [blame] | 773 | x0, y0, x1, y1 = self.wid.GetWindowPort().portRect |
| 774 | vx, vy = self.getscrollbarvalues() |
Jack Jansen | 7bfc875 | 1996-04-16 14:35:43 +0000 | [diff] [blame] | 775 | if vx == None: self.barx_enabled, vx = 0, 0 |
| 776 | if vy == None: self.bary_enabled, vy = 0, 0 |
Jack Jansen | e353215 | 1996-04-12 16:24:44 +0000 | [diff] [blame] | 777 | if wantx: |
| 778 | rect = x0-1, y1-(SCROLLBARWIDTH-1), x1-(SCROLLBARWIDTH-2), y1+1 |
| 779 | self.barx = NewControl(self.wid, rect, "", 1, vx, 0, 32767, 16, 0) |
Jack Jansen | 7bfc875 | 1996-04-16 14:35:43 +0000 | [diff] [blame] | 780 | if not self.barx_enabled: self.barx.HiliteControl(255) |
| 781 | ## InvalRect(rect) |
Jack Jansen | e353215 | 1996-04-12 16:24:44 +0000 | [diff] [blame] | 782 | if wanty: |
| 783 | rect = x1-(SCROLLBARWIDTH-1), y0-1, x1+1, y1-(SCROLLBARWIDTH-2) |
| 784 | self.bary = NewControl(self.wid, rect, "", 1, vy, 0, 32767, 16, 0) |
Jack Jansen | 7bfc875 | 1996-04-16 14:35:43 +0000 | [diff] [blame] | 785 | if not self.bary_enabled: self.bary.HiliteControl(255) |
| 786 | ## InvalRect(rect) |
Jack Jansen | e353215 | 1996-04-12 16:24:44 +0000 | [diff] [blame] | 787 | |
| 788 | def do_postclose(self): |
| 789 | self.barx = self.bary = None |
| 790 | ControlsWindow.do_postclose(self) |
| 791 | |
| 792 | def do_activate(self, onoff, event): |
Jack Jansen | 7bfc875 | 1996-04-16 14:35:43 +0000 | [diff] [blame] | 793 | self.activated = onoff |
Jack Jansen | e353215 | 1996-04-12 16:24:44 +0000 | [diff] [blame] | 794 | if onoff: |
Jack Jansen | 7bfc875 | 1996-04-16 14:35:43 +0000 | [diff] [blame] | 795 | if self.barx and self.barx_enabled: |
Jack Jansen | ded835c | 1996-07-26 14:01:07 +0000 | [diff] [blame] | 796 | self.barx.ShowControl() # jvr |
Jack Jansen | 7bfc875 | 1996-04-16 14:35:43 +0000 | [diff] [blame] | 797 | if self.bary and self.bary_enabled: |
Jack Jansen | ded835c | 1996-07-26 14:01:07 +0000 | [diff] [blame] | 798 | self.bary.ShowControl() # jvr |
Jack Jansen | e353215 | 1996-04-12 16:24:44 +0000 | [diff] [blame] | 799 | else: |
Jack Jansen | 7bfc875 | 1996-04-16 14:35:43 +0000 | [diff] [blame] | 800 | if self.barx: |
Jack Jansen | ded835c | 1996-07-26 14:01:07 +0000 | [diff] [blame] | 801 | self.barx.HideControl() # jvr; An inactive window should have *hidden* |
| 802 | # scrollbars, not just dimmed (no matter what |
| 803 | # BBEdit does... look at the Finder) |
Jack Jansen | 7bfc875 | 1996-04-16 14:35:43 +0000 | [diff] [blame] | 804 | if self.bary: |
Jack Jansen | ded835c | 1996-07-26 14:01:07 +0000 | [diff] [blame] | 805 | self.bary.HideControl() # jvr |
| 806 | self.wid.DrawGrowIcon() # jvr |
Jack Jansen | e353215 | 1996-04-12 16:24:44 +0000 | [diff] [blame] | 807 | |
| 808 | def do_postresize(self, width, height, window): |
| 809 | l, t, r, b = self.wid.GetWindowPort().portRect |
Jack Jansen | ded835c | 1996-07-26 14:01:07 +0000 | [diff] [blame] | 810 | self.SetPort() |
Jack Jansen | e353215 | 1996-04-12 16:24:44 +0000 | [diff] [blame] | 811 | if self.barx: |
Jack Jansen | ded835c | 1996-07-26 14:01:07 +0000 | [diff] [blame] | 812 | self.barx.HideControl() # jvr |
Jack Jansen | e353215 | 1996-04-12 16:24:44 +0000 | [diff] [blame] | 813 | self.barx.MoveControl(l-1, b-(SCROLLBARWIDTH-1)) |
Jack Jansen | ded835c | 1996-07-26 14:01:07 +0000 | [diff] [blame] | 814 | self.barx.SizeControl((r-l)-(SCROLLBARWIDTH-3), SCROLLBARWIDTH) # jvr |
Jack Jansen | e353215 | 1996-04-12 16:24:44 +0000 | [diff] [blame] | 815 | if self.bary: |
Jack Jansen | ded835c | 1996-07-26 14:01:07 +0000 | [diff] [blame] | 816 | self.bary.HideControl() # jvr |
Jack Jansen | e353215 | 1996-04-12 16:24:44 +0000 | [diff] [blame] | 817 | self.bary.MoveControl(r-(SCROLLBARWIDTH-1), t-1) |
Jack Jansen | ded835c | 1996-07-26 14:01:07 +0000 | [diff] [blame] | 818 | self.bary.SizeControl(SCROLLBARWIDTH, (b-t)-(SCROLLBARWIDTH-3)) # jvr |
| 819 | if self.barx: |
| 820 | self.barx.ShowControl() # jvr |
| 821 | ValidRect((l, b - SCROLLBARWIDTH + 1, r - SCROLLBARWIDTH + 2, b)) # jvr |
| 822 | if self.bary: |
| 823 | self.bary.ShowControl() # jvr |
| 824 | ValidRect((r - SCROLLBARWIDTH + 1, t, r, b - SCROLLBARWIDTH + 2)) # jvr |
| 825 | InvalRect((r - SCROLLBARWIDTH + 1, b - SCROLLBARWIDTH + 1, r, b)) # jvr, growicon |
Jack Jansen | e353215 | 1996-04-12 16:24:44 +0000 | [diff] [blame] | 826 | |
| 827 | def do_controlhit(self, window, control, pcode, event): |
| 828 | if control == self.barx: |
| 829 | bar = self.barx |
| 830 | which = 'x' |
| 831 | elif control == self.bary: |
| 832 | bar = self.bary |
| 833 | which = 'y' |
| 834 | else: |
| 835 | return 0 |
| 836 | value = None |
| 837 | if pcode == inUpButton: |
| 838 | what = '-' |
| 839 | elif pcode == inDownButton: |
| 840 | what = '+' |
| 841 | elif pcode == inPageUp: |
| 842 | what = '--' |
| 843 | elif pcode == inPageDown: |
| 844 | what = '++' |
| 845 | else: |
| 846 | what = 'set' |
| 847 | value = bar.GetControlValue() |
| 848 | self.scrollbar_callback(which, what, value) |
| 849 | self.updatescrollbars() |
| 850 | return 1 |
| 851 | |
| 852 | def updatescrollbars(self): |
| 853 | SetPort(self.wid) |
| 854 | vx, vy = self.getscrollbarvalues() |
| 855 | if self.barx: |
Jack Jansen | 7bfc875 | 1996-04-16 14:35:43 +0000 | [diff] [blame] | 856 | if vx == None: |
| 857 | self.barx.HiliteControl(255) |
| 858 | self.barx_enabled = 0 |
| 859 | else: |
| 860 | if not self.barx_enabled: |
| 861 | self.barx_enabled = 1 |
| 862 | if self.activated: |
| 863 | self.barx.HiliteControl(0) |
| 864 | self.barx.SetControlValue(vx) |
Jack Jansen | e353215 | 1996-04-12 16:24:44 +0000 | [diff] [blame] | 865 | if self.bary: |
Jack Jansen | 7bfc875 | 1996-04-16 14:35:43 +0000 | [diff] [blame] | 866 | if vy == None: |
| 867 | self.bary.HiliteControl(255) |
| 868 | self.bary_enabled = 0 |
| 869 | else: |
| 870 | if not self.bary_enabled: |
| 871 | self.bary_enabled = 1 |
| 872 | if self.activated: |
| 873 | self.bary.HiliteControl(0) |
| 874 | self.bary.SetControlValue(vy) |
| 875 | |
| 876 | # Auxiliary function: convert standard text/image/etc coordinate |
| 877 | # to something palatable as getscrollbarvalues() return |
| 878 | def scalebarvalue(self, absmin, absmax, curmin, curmax): |
| 879 | if curmin <= absmin and curmax >= absmax: |
| 880 | return None |
| 881 | if curmin <= absmin: |
| 882 | return 0 |
| 883 | if curmax >= absmax: |
| 884 | return 32767 |
| 885 | perc = float(curmin-absmin)/float(absmax-absmin) |
| 886 | return int(perc*32767) |
Jack Jansen | e353215 | 1996-04-12 16:24:44 +0000 | [diff] [blame] | 887 | |
| 888 | # To be overridden: |
| 889 | |
| 890 | def getscrollbarvalues(self): |
| 891 | return 0, 0 |
| 892 | |
| 893 | def scrollbar_callback(self, which, what, value): |
| 894 | print 'scroll', which, what, value |
Jack Jansen | 7e0da90 | 1995-08-17 14:18:20 +0000 | [diff] [blame] | 895 | |
| 896 | class DialogWindow(Window): |
| 897 | """A modeless dialog window""" |
| 898 | |
| 899 | def open(self, resid): |
| 900 | self.wid = GetNewDialog(resid, -1) |
| 901 | self.do_postopen() |
| 902 | |
| 903 | def close(self): |
Jack Jansen | 7e0da90 | 1995-08-17 14:18:20 +0000 | [diff] [blame] | 904 | self.do_postclose() |
| 905 | |
| 906 | def do_itemhit(self, item, event): |
| 907 | print 'Dialog %s, item %d hit'%(self.wid, item) |
| 908 | |
| 909 | def do_rawupdate(self, window, event): |
| 910 | pass |
Guido van Rossum | 8f4b6ad | 1995-04-05 09:18:35 +0000 | [diff] [blame] | 911 | |
| 912 | def ostypecode(x): |
| 913 | "Convert a long int to the 4-character code it really is" |
| 914 | s = '' |
| 915 | for i in range(4): |
| 916 | x, c = divmod(x, 256) |
| 917 | s = chr(c) + s |
| 918 | return s |
| 919 | |
| 920 | |
| 921 | class TestApp(Application): |
| 922 | |
| 923 | "This class is used by the test() function" |
| 924 | |
| 925 | def makeusermenus(self): |
| 926 | self.filemenu = m = Menu(self.menubar, "File") |
| 927 | self.saveitem = MenuItem(m, "Save", "S", self.save) |
| 928 | Separator(m) |
| 929 | self.optionsmenu = mm = SubMenu(m, "Options") |
| 930 | self.opt1 = CheckItem(mm, "Arguments") |
| 931 | self.opt2 = CheckItem(mm, "Being hit on the head lessons") |
| 932 | self.opt3 = CheckItem(mm, "Complaints") |
| 933 | Separator(m) |
| 934 | self.quititem = MenuItem(m, "Quit", "Q", self.quit) |
| 935 | |
| 936 | def save(self, *args): |
| 937 | print "Save" |
| 938 | |
| 939 | def quit(self, *args): |
| 940 | raise self |
| 941 | |
| 942 | |
| 943 | def test(): |
| 944 | "Test program" |
| 945 | app = TestApp() |
| 946 | app.mainloop() |
| 947 | |
| 948 | |
| 949 | if __name__ == '__main__': |
| 950 | test() |