5dbc118e |
1 | /* $Id: mac.c,v 1.3 2002/11/23 18:22:47 ben Exp $ */ |
d082ac49 |
2 | /* |
3 | * Copyright (c) 1999 Ben Harris |
4 | * All rights reserved. |
5 | * |
6 | * Permission is hereby granted, free of charge, to any person |
7 | * obtaining a copy of this software and associated documentation |
8 | * files (the "Software"), to deal in the Software without |
9 | * restriction, including without limitation the rights to use, |
10 | * copy, modify, merge, publish, distribute, sublicense, and/or |
11 | * sell copies of the Software, and to permit persons to whom the |
12 | * Software is furnished to do so, subject to the following |
13 | * conditions: |
14 | * |
15 | * The above copyright notice and this permission notice shall be |
16 | * included in all copies or substantial portions of the Software. |
17 | * |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR |
22 | * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
23 | * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
24 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
25 | * SOFTWARE. |
26 | */ |
27 | /* |
28 | * mac.c -- miscellaneous Mac-specific routines |
29 | */ |
30 | |
31 | #include <MacTypes.h> |
32 | #include <Quickdraw.h> |
33 | #include <Fonts.h> |
34 | #include <MacWindows.h> |
35 | #include <Menus.h> |
36 | #include <TextEdit.h> |
37 | #include <Appearance.h> |
38 | #include <CodeFragments.h> |
39 | #include <Dialogs.h> |
40 | #include <Devices.h> |
41 | #include <DiskInit.h> |
42 | #include <Gestalt.h> |
43 | #include <Resources.h> |
d082ac49 |
44 | #include <ToolUtils.h> |
45 | |
46 | #include <assert.h> |
47 | #include <limits.h> |
48 | #include <stdarg.h> |
49 | #include <stdlib.h> /* putty.h needs size_t */ |
50 | #include <stdio.h> /* for vsprintf */ |
51 | |
52 | #define PUTTY_DO_GLOBALS |
53 | |
54 | #include "macresid.h" |
55 | #include "putty.h" |
56 | #include "mac.h" |
57 | |
58 | QDGlobals qd; |
59 | |
60 | static int cold = 1; |
61 | struct mac_gestalts mac_gestalts; |
62 | |
63 | static void mac_startup(void); |
64 | static void mac_eventloop(void); |
65 | #pragma noreturn (mac_eventloop) |
66 | static void mac_event(EventRecord *); |
67 | static void mac_contentclick(WindowPtr, EventRecord *); |
68 | static void mac_growwindow(WindowPtr, EventRecord *); |
69 | static void mac_activatewindow(WindowPtr, EventRecord *); |
70 | static void mac_activateabout(WindowPtr, EventRecord *); |
71 | static void mac_updatewindow(WindowPtr); |
72 | static void mac_keypress(EventRecord *); |
73 | static int mac_windowtype(WindowPtr); |
74 | static void mac_menucommand(long); |
75 | static void mac_openabout(void); |
76 | static void mac_adjustcursor(RgnHandle); |
77 | static void mac_adjustmenus(void); |
78 | static void mac_closewindow(WindowPtr); |
79 | static void mac_zoomwindow(WindowPtr, short); |
80 | static void mac_shutdown(void); |
81 | #pragma noreturn (mac_shutdown) |
82 | |
83 | struct mac_windows { |
84 | WindowPtr about; |
85 | WindowPtr licence; |
86 | }; |
87 | |
88 | struct mac_windows windows; |
89 | |
90 | int main (int argc, char **argv) { |
91 | |
92 | mac_startup(); |
93 | mac_eventloop(); |
94 | } |
95 | |
96 | #pragma noreturn (main) |
97 | |
98 | static void mac_startup(void) { |
99 | Handle menuBar; |
100 | |
101 | /* Init Memory Manager */ |
102 | MaxApplZone(); |
103 | /* Init QuickDraw */ |
104 | InitGraf(&qd.thePort); |
105 | /* Init Font Manager */ |
106 | InitFonts(); |
107 | /* Init Window Manager */ |
108 | InitWindows(); |
109 | /* Init Menu Manager */ |
110 | InitMenus(); |
111 | /* Init TextEdit */ |
112 | TEInit(); |
113 | /* Init Dialog Manager */ |
114 | InitDialogs(nil); |
115 | cold = 0; |
116 | |
d082ac49 |
117 | /* Find out if we've got Color Quickdraw */ |
118 | if (Gestalt(gestaltQuickdrawVersion, &mac_gestalts.qdvers) != noErr) |
119 | mac_gestalts.qdvers = gestaltOriginalQD; |
120 | /* ... and the Appearance Manager? */ |
121 | if (Gestalt(gestaltAppearanceVersion, &mac_gestalts.apprvers) != noErr) |
122 | if (Gestalt(gestaltAppearanceAttr, NULL) == noErr) |
123 | mac_gestalts.apprvers = 0x0100; |
124 | else |
125 | mac_gestalts.apprvers = 0; |
126 | #if TARGET_RT_MAC_CFM |
127 | /* Paranoia: Did we manage to pull in AppearanceLib? */ |
128 | if (&RegisterAppearanceClient == kUnresolvedCFragSymbolAddress) |
129 | mac_gestalts.apprvers = 0; |
130 | #endif |
131 | /* Mac OS 8.5 Control Manager (proportional scrollbars)? */ |
132 | if (Gestalt(gestaltControlMgrAttr, &mac_gestalts.cntlattr) != noErr) |
133 | mac_gestalts.cntlattr = 0; |
134 | /* Mac OS 8.5 Window Manager? */ |
135 | if (Gestalt(gestaltWindowMgrAttr, &mac_gestalts.windattr) != noErr) |
136 | mac_gestalts.windattr = 0; |
137 | |
138 | /* We've been tested with the Appearance Manager */ |
139 | if (mac_gestalts.apprvers != 0) |
140 | RegisterAppearanceClient(); |
141 | |
142 | menuBar = GetNewMBar(128); |
143 | if (menuBar == NULL) |
144 | fatalbox("Unable to create menu bar."); |
145 | SetMenuBar(menuBar); |
146 | AppendResMenu(GetMenuHandle(mApple), 'DRVR'); |
147 | mac_adjustmenus(); |
148 | DrawMenuBar(); |
149 | InitCursor(); |
150 | windows.about = NULL; |
151 | windows.licence = NULL; |
152 | |
153 | init_ucs(); |
154 | } |
155 | |
156 | static void mac_eventloop(void) { |
157 | Boolean gotevent; |
158 | EventRecord event; |
159 | RgnHandle cursrgn; |
160 | |
161 | cursrgn = NewRgn(); |
162 | for (;;) { |
163 | mac_adjustcursor(cursrgn); |
164 | gotevent = WaitNextEvent(everyEvent, &event, LONG_MAX, cursrgn); |
165 | mac_adjustcursor(cursrgn); |
166 | if (gotevent) |
167 | mac_event(&event); |
d082ac49 |
168 | } |
169 | DisposeRgn(cursrgn); |
170 | } |
171 | |
172 | static void mac_event(EventRecord *event) { |
173 | short part; |
174 | WindowPtr window; |
175 | Point pt; |
176 | |
177 | switch (event->what) { |
178 | case mouseDown: |
179 | part = FindWindow(event->where, &window); |
180 | switch (part) { |
181 | case inMenuBar: |
182 | mac_adjustmenus(); |
183 | mac_menucommand(MenuSelect(event->where)); |
184 | break; |
185 | case inSysWindow: |
186 | SystemClick(event, window); |
187 | break; |
188 | case inContent: |
189 | if (window != FrontWindow()) |
190 | /* XXX: check for movable modal dboxes? */ |
191 | SelectWindow(window); |
192 | else |
193 | mac_contentclick(window, event); |
194 | break; |
195 | case inGoAway: |
196 | if (TrackGoAway(window, event->where)) |
197 | mac_closewindow(window); |
198 | break; |
199 | case inDrag: |
200 | /* XXX: moveable modal check? */ |
201 | DragWindow(window, event->where, &qd.screenBits.bounds); |
202 | break; |
203 | case inGrow: |
204 | mac_growwindow(window, event); |
205 | break; |
206 | case inZoomIn: |
207 | case inZoomOut: |
208 | if (TrackBox(window, event->where, part)) |
209 | mac_zoomwindow(window, part); |
210 | break; |
211 | } |
212 | break; |
213 | case keyDown: |
214 | case autoKey: |
215 | mac_keypress(event); |
216 | break; |
217 | case activateEvt: |
218 | mac_activatewindow((WindowPtr)event->message, event); |
219 | break; |
220 | case updateEvt: |
221 | mac_updatewindow((WindowPtr)event->message); |
222 | break; |
223 | case diskEvt: |
224 | if (HiWord(event->message) != noErr) { |
225 | SetPt(&pt, 120, 120); |
226 | DIBadMount(pt, event->message); |
227 | } |
228 | break; |
229 | } |
230 | } |
231 | |
232 | static void mac_contentclick(WindowPtr window, EventRecord *event) { |
233 | short item; |
234 | |
235 | switch (mac_windowtype(window)) { |
236 | case wTerminal: |
237 | mac_clickterm(window, event); |
238 | break; |
239 | case wAbout: |
240 | if (DialogSelect(event, &(DialogPtr)window, &item)) |
241 | switch (item) { |
242 | case wiAboutLicence: |
243 | /* XXX: Do something */ |
244 | break; |
245 | } |
246 | break; |
247 | } |
248 | } |
249 | |
250 | static void mac_growwindow(WindowPtr window, EventRecord *event) { |
251 | |
252 | switch (mac_windowtype(window)) { |
253 | case wTerminal: |
254 | mac_growterm(window, event); |
255 | } |
256 | } |
257 | |
258 | static void mac_activatewindow(WindowPtr window, EventRecord *event) { |
259 | int active; |
260 | |
261 | active = (event->modifiers & activeFlag) != 0; |
262 | mac_adjustmenus(); |
263 | switch (mac_windowtype(window)) { |
264 | case wTerminal: |
265 | mac_activateterm(window, active); |
266 | break; |
267 | case wAbout: |
268 | mac_activateabout(window, event); |
269 | break; |
270 | } |
271 | } |
272 | |
273 | static void mac_activateabout(WindowPtr window, EventRecord *event) { |
274 | DialogItemType itemtype; |
275 | Handle itemhandle; |
276 | short item; |
277 | Rect itemrect; |
278 | int active; |
279 | |
280 | active = (event->modifiers & activeFlag) != 0; |
281 | GetDialogItem(window, wiAboutLicence, &itemtype, &itemhandle, &itemrect); |
282 | HiliteControl((ControlHandle)itemhandle, active ? 0 : 255); |
283 | DialogSelect(event, &window, &item); |
284 | } |
285 | |
286 | static void mac_updatewindow(WindowPtr window) { |
287 | |
288 | switch (mac_windowtype(window)) { |
289 | case wTerminal: |
290 | mac_updateterm(window); |
291 | break; |
292 | case wAbout: |
293 | BeginUpdate(window); |
294 | UpdateDialog(window, window->visRgn); |
295 | EndUpdate(window); |
296 | break; |
297 | case wLicence: |
298 | /* Do something */ |
299 | break; |
300 | } |
301 | } |
302 | |
303 | /* |
304 | * Work out what kind of window we're dealing with. |
305 | * Concept shamelessly nicked from SurfWriter. |
306 | */ |
307 | static int mac_windowtype(WindowPtr window) { |
308 | int kind; |
309 | |
310 | if (window == NULL) |
311 | return wNone; |
312 | kind = ((WindowPeek)window)->windowKind; |
313 | if (kind < 0) |
314 | return wDA; |
315 | if (GetWVariant(window) == zoomDocProc) |
316 | return wTerminal; |
317 | return GetWRefCon(window); |
318 | } |
319 | |
320 | /* |
321 | * Handle a key press |
322 | */ |
323 | static void mac_keypress(EventRecord *event) { |
324 | WindowPtr window; |
325 | |
326 | window = FrontWindow(); |
327 | /* |
328 | * Check for a command-key combination, but ignore it if it counts |
329 | * as a meta-key combination and we're in a terminal window. |
330 | */ |
331 | if (event->what == keyDown && (event->modifiers & cmdKey) /*&& |
332 | !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers && |
333 | mac_windowtype(window) == wTerminal)*/) { |
334 | mac_adjustmenus(); |
335 | mac_menucommand(MenuKey(event->message & charCodeMask)); |
336 | } else { |
337 | switch (mac_windowtype(window)) { |
338 | case wTerminal: |
339 | mac_keyterm(window, event); |
340 | break; |
341 | } |
342 | } |
343 | } |
344 | |
345 | static void mac_menucommand(long result) { |
346 | short menu, item; |
347 | Str255 da; |
348 | WindowPtr window; |
349 | |
350 | menu = HiWord(result); |
351 | item = LoWord(result); |
352 | window = FrontWindow(); |
353 | /* Things which do the same whatever window we're in. */ |
354 | switch (menu) { |
355 | case mApple: |
356 | switch (item) { |
357 | case iAbout: |
358 | mac_openabout(); |
359 | goto done; |
360 | default: |
361 | GetMenuItemText(GetMenuHandle(mApple), item, da); |
362 | OpenDeskAcc(da); |
363 | goto done; |
364 | } |
365 | break; |
366 | case mFile: |
367 | switch (item) { |
368 | case iNew: |
369 | mac_newsession(); |
370 | goto done; |
371 | case iClose: |
372 | mac_closewindow(window); |
373 | goto done; |
374 | case iQuit: |
375 | mac_shutdown(); |
376 | goto done; |
377 | } |
378 | break; |
379 | } |
380 | /* If we get here, handling is up to window-specific code. */ |
381 | switch (mac_windowtype(window)) { |
382 | case wTerminal: |
383 | mac_menuterm(window, menu, item); |
384 | break; |
385 | } |
386 | done: |
387 | HiliteMenu(0); |
388 | } |
389 | |
390 | static void mac_openabout(void) { |
391 | DialogItemType itemtype; |
392 | Handle item; |
393 | VersRecHndl vers; |
394 | Rect box; |
395 | StringPtr longvers; |
396 | |
397 | if (windows.about) |
398 | SelectWindow(windows.about); |
399 | else { |
400 | windows.about = GetNewDialog(wAbout, NULL, (WindowPtr)-1); |
5dbc118e |
401 | vers = (VersRecHndl)Get1Resource('vers', 1); |
402 | if (vers != NULL && *vers != NULL) { |
403 | longvers = (*vers)->shortVersion + (*vers)->shortVersion[0] + 1; |
404 | GetDialogItem(windows.about, wiAboutVersion, |
405 | &itemtype, &item, &box); |
406 | assert(itemtype & kStaticTextDialogItem); |
407 | SetDialogItemText(item, longvers); |
408 | } |
d082ac49 |
409 | ShowWindow(windows.about); |
410 | } |
411 | } |
412 | |
413 | static void mac_closewindow(WindowPtr window) { |
414 | |
415 | switch (mac_windowtype(window)) { |
416 | case wDA: |
417 | CloseDeskAcc(((WindowPeek)window)->windowKind); |
418 | break; |
419 | case wTerminal: |
420 | /* FIXME: end session and stuff */ |
421 | break; |
422 | case wAbout: |
423 | windows.about = NULL; |
424 | CloseWindow(window); |
425 | break; |
426 | default: |
427 | CloseWindow(window); |
428 | break; |
429 | } |
430 | } |
431 | |
432 | static void mac_zoomwindow(WindowPtr window, short part) { |
433 | |
434 | /* FIXME: do something */ |
435 | } |
436 | |
437 | /* |
438 | * Make the menus look right before the user gets to see them. |
439 | */ |
440 | static void mac_adjustmenus(void) { |
441 | WindowPtr window; |
442 | MenuHandle menu; |
443 | |
444 | window = FrontWindow(); |
445 | menu = GetMenuHandle(mApple); |
446 | EnableItem(menu, 0); |
447 | EnableItem(menu, iAbout); |
448 | |
449 | menu = GetMenuHandle(mFile); |
450 | EnableItem(menu, 0); |
451 | EnableItem(menu, iNew); |
452 | if (window != NULL) |
453 | EnableItem(menu, iClose); |
454 | else |
455 | DisableItem(menu, iClose); |
456 | EnableItem(menu, iQuit); |
457 | |
458 | switch (mac_windowtype(window)) { |
459 | case wTerminal: |
460 | mac_adjusttermmenus(window); |
461 | break; |
462 | default: |
463 | menu = GetMenuHandle(mEdit); |
464 | DisableItem(menu, 0); |
465 | break; |
466 | } |
467 | DrawMenuBar(); |
468 | } |
469 | |
470 | /* |
471 | * Make sure the right cursor's being displayed. |
472 | */ |
473 | static void mac_adjustcursor(RgnHandle cursrgn) { |
474 | Point mouse; |
475 | WindowPtr window, front; |
476 | short part; |
477 | |
478 | GetMouse(&mouse); |
479 | LocalToGlobal(&mouse); |
480 | part = FindWindow(mouse, &window); |
481 | front = FrontWindow(); |
482 | if (part != inContent || window == NULL || window != front) { |
483 | /* Cursor isn't in the front window, so switch to arrow */ |
484 | SetCursor(&qd.arrow); |
485 | SetRectRgn(cursrgn, SHRT_MIN, SHRT_MIN, SHRT_MAX, SHRT_MAX); |
486 | if (front != NULL) |
487 | DiffRgn(cursrgn, front->visRgn, cursrgn); |
488 | } else { |
489 | switch (mac_windowtype(window)) { |
490 | case wTerminal: |
491 | mac_adjusttermcursor(window, mouse, cursrgn); |
492 | break; |
493 | default: |
494 | SetCursor(&qd.arrow); |
495 | CopyRgn(window->visRgn, cursrgn); |
496 | break; |
497 | } |
498 | } |
499 | } |
500 | |
501 | static void mac_shutdown(void) { |
502 | |
503 | exit(0); |
504 | } |
505 | |
506 | void fatalbox(char *fmt, ...) { |
507 | va_list ap; |
508 | Str255 stuff; |
509 | |
510 | va_start(ap, fmt); |
511 | /* We'd like stuff to be a Pascal string */ |
512 | stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap); |
513 | va_end(ap); |
514 | ParamText(stuff, NULL, NULL, NULL); |
515 | StopAlert(128, nil); |
516 | exit(1); |
517 | } |
518 | |
519 | void modalfatalbox(char *fmt, ...) { |
520 | va_list ap; |
521 | Str255 stuff; |
522 | |
523 | va_start(ap, fmt); |
524 | /* We'd like stuff to be a Pascal string */ |
525 | stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap); |
526 | va_end(ap); |
527 | ParamText(stuff, NULL, NULL, NULL); |
528 | StopAlert(128, nil); |
529 | exit(1); |
530 | } |
531 | |
532 | /* |
533 | * Local Variables: |
534 | * c-file-style: "simon" |
535 | * End: |
536 | */ |