]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - mac/mac.c
2e4bfdf7aa8dc65282ef0b68a7377cd340ac051f
[PuTTY.git] / mac / mac.c
1 /* $Id: mac.c,v 1.2 2002/11/23 15:11:13 ben Exp $ */
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>
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     
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);
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);
401         /* XXX check we're using the right resource file? */
402         vers = (VersRecHndl)GetResource('vers', 1);
403         assert(vers != NULL && *vers != NULL);
404         longvers = (*vers)->shortVersion + (*vers)->shortVersion[0] + 1;
405         GetDialogItem(windows.about, wiAboutVersion, &itemtype, &item, &box);
406         assert(itemtype & kStaticTextDialogItem);
407         SetDialogItemText(item, longvers);
408         ShowWindow(windows.about);
409     }
410 }
411
412 static void mac_closewindow(WindowPtr window) {
413
414     switch (mac_windowtype(window)) {
415       case wDA:
416         CloseDeskAcc(((WindowPeek)window)->windowKind);
417         break;
418       case wTerminal:
419         /* FIXME: end session and stuff */
420         break;
421       case wAbout:
422         windows.about = NULL;
423         CloseWindow(window);
424         break;
425       default:
426         CloseWindow(window);
427         break;
428     }
429 }
430
431 static void mac_zoomwindow(WindowPtr window, short part) {
432
433     /* FIXME: do something */
434 }
435
436 /*
437  * Make the menus look right before the user gets to see them.
438  */
439 static void mac_adjustmenus(void) {
440     WindowPtr window;
441     MenuHandle menu;
442
443     window = FrontWindow();
444     menu = GetMenuHandle(mApple);
445     EnableItem(menu, 0);
446     EnableItem(menu, iAbout);
447
448     menu = GetMenuHandle(mFile);
449     EnableItem(menu, 0);
450     EnableItem(menu, iNew);
451     if (window != NULL)
452         EnableItem(menu, iClose);
453     else
454         DisableItem(menu, iClose);
455     EnableItem(menu, iQuit);
456
457     switch (mac_windowtype(window)) {
458       case wTerminal:
459         mac_adjusttermmenus(window);
460         break;
461       default:
462         menu = GetMenuHandle(mEdit);
463         DisableItem(menu, 0);
464         break;
465     }
466     DrawMenuBar();
467 }
468
469 /*
470  * Make sure the right cursor's being displayed.
471  */
472 static void mac_adjustcursor(RgnHandle cursrgn) {
473     Point mouse;
474     WindowPtr window, front;
475     short part;
476
477     GetMouse(&mouse);
478     LocalToGlobal(&mouse);
479     part = FindWindow(mouse, &window);
480     front = FrontWindow();
481     if (part != inContent || window == NULL || window != front) {
482         /* Cursor isn't in the front window, so switch to arrow */
483         SetCursor(&qd.arrow);
484         SetRectRgn(cursrgn, SHRT_MIN, SHRT_MIN, SHRT_MAX, SHRT_MAX);
485         if (front != NULL)
486             DiffRgn(cursrgn, front->visRgn, cursrgn);
487     } else {
488         switch (mac_windowtype(window)) {
489           case wTerminal:
490             mac_adjusttermcursor(window, mouse, cursrgn);
491             break;
492           default:
493             SetCursor(&qd.arrow);
494             CopyRgn(window->visRgn, cursrgn);
495             break;
496         }
497     }
498 }
499
500 static void mac_shutdown(void) {
501
502     exit(0);
503 }
504
505 void fatalbox(char *fmt, ...) {
506     va_list ap;
507     Str255 stuff;
508     
509     va_start(ap, fmt);
510     /* We'd like stuff to be a Pascal string */
511     stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
512     va_end(ap);
513     ParamText(stuff, NULL, NULL, NULL);
514     StopAlert(128, nil);
515     exit(1);
516 }
517
518 void modalfatalbox(char *fmt, ...) {
519     va_list ap;
520     Str255 stuff;
521     
522     va_start(ap, fmt);
523     /* We'd like stuff to be a Pascal string */
524     stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
525     va_end(ap);
526     ParamText(stuff, NULL, NULL, NULL);
527     StopAlert(128, nil);
528     exit(1);
529 }
530
531 /*
532  * Local Variables:
533  * c-file-style: "simon"
534  * End:
535  */