]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - mac.c
Lots of MacTCP networking stuff. I think all the code's written, but it
[PuTTY.git] / mac.c
1 /* $Id: mac.c,v 1.1.2.24 1999/04/04 18:23:33 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 QuickDraw */
102     InitGraf(&qd.thePort);
103     /* Init Font Manager */
104     InitFonts();
105     /* Init Window Manager */
106     InitWindows();
107     /* Init Menu Manager */
108     InitMenus();
109     /* Init TextEdit */
110     TEInit();
111     /* Init Dialog Manager */
112     InitDialogs(nil);
113     cold = 0;
114     
115     /* Find out if we've got Color Quickdraw */
116     if (Gestalt(gestaltQuickdrawVersion, &mac_gestalts.qdvers) != noErr)
117         mac_gestalts.qdvers = gestaltOriginalQD;
118     /* ... and the Appearance Manager? */
119     if (Gestalt(gestaltAppearanceVersion, &mac_gestalts.apprvers) != noErr)
120         if (Gestalt(gestaltAppearanceAttr, NULL) == noErr)
121             mac_gestalts.apprvers = 0x0100;
122         else
123             mac_gestalts.apprvers = 0;
124     /* Paranoia: Did we manage to pull in AppearanceLib? */
125     if (&RegisterAppearanceClient == kUnresolvedCFragSymbolAddress)
126         mac_gestalts.apprvers = 0;
127     /* Mac OS 8.5 Control Manager (proportional scrollbars)? */
128     if (Gestalt(gestaltControlMgrAttr, &mac_gestalts.cntlattr) != noErr)
129         mac_gestalts.cntlattr = 0;
130     /* Mac OS 8.5 Window Manager? */
131     if (Gestalt(gestaltWindowMgrAttr, &mac_gestalts.windattr) != noErr)
132         mac_gestalts.windattr = 0;
133
134     /* We've been tested with the Appearance Manager */
135     if (mac_gestalts.apprvers != 0)
136         RegisterAppearanceClient();
137     
138     menuBar = GetNewMBar(128);
139     if (menuBar == NULL)
140         fatalbox("Unable to create menu bar.");
141     SetMenuBar(menuBar);
142     AppendResMenu(GetMenuHandle(mApple), 'DRVR');
143     mac_adjustmenus();
144     DrawMenuBar();
145     InitCursor();
146     windows.about = NULL;
147     windows.licence = NULL;
148 }
149
150 static void mac_eventloop(void) {
151     Boolean gotevent;
152     EventRecord event;
153     RgnHandle cursrgn;
154
155     cursrgn = NewRgn();
156     for (;;) {
157         mac_adjustcursor(cursrgn);
158         gotevent = WaitNextEvent(everyEvent, &event, LONG_MAX, cursrgn);
159         mac_adjustcursor(cursrgn);
160         if (gotevent)
161             mac_event(&event);
162         macnet_eventcheck();
163     }
164     DisposeRgn(cursrgn);
165 }
166
167 static void mac_event(EventRecord *event) {
168     short part;
169     WindowPtr window;
170     Point pt;
171
172     switch (event->what) {
173       case mouseDown:
174         part = FindWindow(event->where, &window);
175         switch (part) {
176           case inMenuBar:
177             mac_adjustmenus();
178             mac_menucommand(MenuSelect(event->where));
179             break;
180           case inSysWindow:
181             SystemClick(event, window);
182             break;
183           case inContent:
184             if (window != FrontWindow())
185                 /* XXX: check for movable modal dboxes? */
186                 SelectWindow(window);
187             else
188                 mac_contentclick(window, event);
189             break;
190           case inGoAway:
191             if (TrackGoAway(window, event->where))
192                 mac_closewindow(window);
193             break;
194           case inDrag:
195             /* XXX: moveable modal check? */
196             DragWindow(window, event->where, &qd.screenBits.bounds);
197             break;
198           case inGrow:
199             mac_growwindow(window, event);
200             break;
201           case inZoomIn:
202           case inZoomOut:
203             if (TrackBox(window, event->where, part))
204                 mac_zoomwindow(window, part);
205             break;
206         }
207         break;
208       case keyDown:
209       case autoKey:
210         mac_keypress(event);
211         break;
212       case activateEvt:
213         mac_activatewindow((WindowPtr)event->message, event);
214         break;
215       case updateEvt:
216         mac_updatewindow((WindowPtr)event->message);
217         break;
218       case diskEvt:
219         if (HiWord(event->message) != noErr) {
220             SetPt(&pt, 120, 120);
221             DIBadMount(pt, event->message);
222         }
223         break;
224     }
225 }
226
227 static void mac_contentclick(WindowPtr window, EventRecord *event) {
228     short item;
229
230     switch (mac_windowtype(window)) {
231       case wTerminal:
232         mac_clickterm(window, event);
233         break;
234       case wAbout:
235         if (DialogSelect(event, &(DialogPtr)window, &item))
236             switch (item) {
237               case wiAboutLicence:
238                 /* XXX: Do something */
239                 break;
240             }
241         break;
242     }
243 }
244
245 static void mac_growwindow(WindowPtr window, EventRecord *event) {
246
247     switch (mac_windowtype(window)) {
248       case wTerminal:
249         mac_growterm(window, event);
250     }
251 }
252
253 static void mac_activatewindow(WindowPtr window, EventRecord *event) {
254     int active;
255
256     active = (event->modifiers & activeFlag) != 0;
257     mac_adjustmenus();
258     switch (mac_windowtype(window)) {
259       case wTerminal:
260         mac_activateterm(window, active);
261         break;
262       case wAbout:
263         mac_activateabout(window, event);
264         break;
265     }
266 }
267
268 static void mac_activateabout(WindowPtr window, EventRecord *event) {
269     DialogItemType itemtype;
270     Handle itemhandle;
271     short item;
272     Rect itemrect;
273     int active;
274
275     active = (event->modifiers & activeFlag) != 0;
276     GetDialogItem(window, wiAboutLicence, &itemtype, &itemhandle, &itemrect);
277     HiliteControl((ControlHandle)itemhandle, active ? 0 : 255);
278     DialogSelect(event, &window, &item);
279 }
280
281 static void mac_updatewindow(WindowPtr window) {
282
283     switch (mac_windowtype(window)) {
284       case wTerminal:
285         mac_updateterm(window);
286         break;
287       case wAbout:
288         BeginUpdate(window);
289         UpdateDialog(window, window->visRgn);
290         EndUpdate(window);
291         break;
292       case wLicence:
293         /* Do something */
294         break;
295     }
296 }
297
298 /*
299  * Work out what kind of window we're dealing with.
300  * Concept shamelessly nicked from SurfWriter.
301  */
302 static int mac_windowtype(WindowPtr window) {
303     int kind;
304
305     if (window == NULL)
306         return wNone;
307     kind = ((WindowPeek)window)->windowKind;
308     if (kind < 0)
309         return wDA;
310     if (GetWVariant(window) == zoomDocProc)
311         return wTerminal;
312     return GetWRefCon(window);
313 }
314
315 /*
316  * Handle a key press
317  */
318 static void mac_keypress(EventRecord *event) {
319     WindowPtr window;
320
321     window = FrontWindow();
322     /*
323      * Check for a command-key combination, but ignore it if it counts
324      * as a meta-key combination and we're in a terminal window.
325      */
326     if (event->what == keyDown && (event->modifiers & cmdKey) /*&&
327         !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers &&
328             mac_windowtype(window) == wTerminal)*/) {
329         mac_adjustmenus();
330         mac_menucommand(MenuKey(event->message & charCodeMask));
331     } else {
332         switch (mac_windowtype(window)) {
333           case wTerminal:
334             mac_keyterm(window, event);
335             break;
336         }
337     }       
338 }
339
340 static void mac_menucommand(long result) {
341     short menu, item;
342     Str255 da;
343     WindowPtr window;
344
345     menu = HiWord(result);
346     item = LoWord(result);
347     window = FrontWindow();
348     /* Things which do the same whatever window we're in. */
349     switch (menu) {
350       case mApple:
351         switch (item) {
352           case iAbout:
353             mac_openabout();
354             goto done;
355           default:
356             GetMenuItemText(GetMenuHandle(mApple), item, da);
357             OpenDeskAcc(da);
358             goto done;
359         }
360         break;
361       case mFile:
362         switch (item) {
363           case iNew:
364             mac_newsession();
365             goto done;
366           case iClose:
367             mac_closewindow(window);
368             goto done;
369           case iQuit:
370             mac_shutdown();
371             goto done;
372         }
373         break;
374     }
375     /* If we get here, handling is up to window-specific code. */
376     switch (mac_windowtype(window)) {
377       case wTerminal:
378         mac_menuterm(window, menu, item);
379         break;
380     }
381   done:
382     HiliteMenu(0);
383 }
384
385 static void mac_openabout(void) {
386     DialogItemType itemtype;
387     Handle item;
388     VersRecHndl vers;
389     Rect box;
390     StringPtr longvers;
391
392     if (windows.about)
393         SelectWindow(windows.about);
394     else {
395         windows.about = GetNewDialog(wAbout, NULL, (WindowPtr)-1);
396         /* XXX check we're using the right resource file? */
397         vers = (VersRecHndl)GetResource('vers', 1);
398         assert(vers != NULL && *vers != NULL);
399         longvers = (*vers)->shortVersion + (*vers)->shortVersion[0] + 1;
400         GetDialogItem(windows.about, wiAboutVersion, &itemtype, &item, &box);
401         assert(itemtype & kStaticTextDialogItem);
402         SetDialogItemText(item, longvers);
403         ShowWindow(windows.about);
404     }
405 }
406
407 static void mac_closewindow(WindowPtr window) {
408
409     switch (mac_windowtype(window)) {
410       case wDA:
411         CloseDeskAcc(((WindowPeek)window)->windowKind);
412         break;
413       case wTerminal:
414         /* FIXME: end session and stuff */
415         break;
416       case wAbout:
417         windows.about = NULL;
418         CloseWindow(window);
419         break;
420       default:
421         CloseWindow(window);
422         break;
423     }
424 }
425
426 static void mac_zoomwindow(WindowPtr window, short part) {
427
428     /* FIXME: do something */
429 }
430
431 /*
432  * Make the menus look right before the user gets to see them.
433  */
434 static void mac_adjustmenus(void) {
435     WindowPtr window;
436     MenuHandle menu;
437
438     window = FrontWindow();
439     menu = GetMenuHandle(mApple);
440     EnableItem(menu, 0);
441     EnableItem(menu, iAbout);
442
443     menu = GetMenuHandle(mFile);
444     EnableItem(menu, 0);
445     EnableItem(menu, iNew);
446     if (window != NULL)
447         EnableItem(menu, iClose);
448     else
449         DisableItem(menu, iClose);
450     EnableItem(menu, iQuit);
451
452     switch (mac_windowtype(window)) {
453       case wTerminal:
454         mac_adjusttermmenus(window);
455         break;
456       default:
457         menu = GetMenuHandle(mEdit);
458         DisableItem(menu, 0);
459         break;
460     }
461     DrawMenuBar();
462 }
463
464 /*
465  * Make sure the right cursor's being displayed.
466  */
467 static void mac_adjustcursor(RgnHandle cursrgn) {
468     Point mouse;
469     WindowPtr window, front;
470     short part;
471
472     GetMouse(&mouse);
473     LocalToGlobal(&mouse);
474     part = FindWindow(mouse, &window);
475     front = FrontWindow();
476     if (part != inContent || window == NULL || window != front) {
477         /* Cursor isn't in the front window, so switch to arrow */
478         SetCursor(&qd.arrow);
479         SetRectRgn(cursrgn, SHRT_MIN, SHRT_MIN, SHRT_MAX, SHRT_MAX);
480         if (front != NULL)
481             DiffRgn(cursrgn, front->visRgn, cursrgn);
482     } else {
483         switch (mac_windowtype(window)) {
484           case wTerminal:
485             mac_adjusttermcursor(window, mouse, cursrgn);
486             break;
487           default:
488             SetCursor(&qd.arrow);
489             CopyRgn(window->visRgn, cursrgn);
490             break;
491         }
492     }
493 }
494
495 static void mac_shutdown(void) {
496
497     exit(0);
498 }
499
500 void fatalbox(const char *fmt, ...) {
501     va_list ap;
502     Str255 stuff;
503     
504     va_start(ap, fmt);
505     /* We'd like stuff to be a Pascal string */
506     stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
507     va_end(ap);
508     ParamText(stuff, NULL, NULL, NULL);
509     StopAlert(128, nil);
510     exit(1);
511 }
512
513 /*
514  * Local Variables:
515  * c-file-style: "simon"
516  * End:
517  */