]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - mac.c
19636019a6c403f62afda6a96fdf9b8f35c1a6ec
[PuTTY.git] / mac.c
1 /* $Id: mac.c,v 1.1.2.11 1999/03/03 22:03:54 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 <Dialogs.h>
39 #include <Devices.h>
40 #include <DiskInit.h>
41 #include <Gestalt.h>
42 #include <ToolUtils.h>
43
44 #include <limits.h>
45 #include <stdarg.h>
46 #include <stdlib.h>             /* putty.h needs size_t */
47
48 #define PUTTY_DO_GLOBALS
49
50 #include "macresid.h"
51 #include "putty.h"
52 #include "mac.h"
53
54 QDGlobals qd;
55
56 static int cold = 1;
57 struct mac_gestalts mac_gestalts;
58
59 static void mac_startup(void);
60 static void mac_eventloop(void);
61 static void mac_event(EventRecord *);
62 static void mac_contentclick(WindowPtr, EventRecord *);
63 static void mac_growwindow(WindowPtr, EventRecord *);
64 static void mac_activatewindow(WindowPtr, Boolean);
65 static void mac_updatewindow(WindowPtr);
66 static void mac_keypress(EventRecord *);
67 static int mac_windowtype(WindowPtr);
68 static void mac_menucommand(long);
69 static void mac_adjustcursor(void);
70 static void mac_adjustmenus(void);
71 static void mac_closewindow(WindowPtr);
72 static void mac_zoomwindow(WindowPtr, short);
73 static void mac_shutdown(void);
74
75 static void mac_newsession(void);
76
77 int main (int argc, char **argv) {
78
79     mac_startup();
80     mac_eventloop();
81 }
82
83 static void mac_startup(void) {
84     Handle menuBar;
85
86     /* Init QuickDraw */
87     InitGraf(&qd.thePort);
88     /* Init Font Manager */
89     InitFonts();
90     /* Init Window Manager */
91     InitWindows();
92     /* Init Menu Manager */
93     InitMenus();
94     /* Init TextEdit */
95     TEInit();
96     /* Init Dialog Manager */
97     InitDialogs(nil);
98     cold = 0;
99     
100     /* Find out if we've got Color Quickdraw */
101     if (Gestalt(gestaltQuickdrawVersion, &mac_gestalts.qdvers) != noErr)
102         mac_gestalts.qdvers = gestaltOriginalQD;
103     /* ... and the Appearance Manager? */
104     if (Gestalt(gestaltAppearanceVersion, &mac_gestalts.apprvers) != noErr)
105         if (Gestalt(gestaltAppearanceAttr, NULL) == noErr)
106             mac_gestalts.apprvers = 0x0100;
107         else
108             mac_gestalts.apprvers = 0;
109     /* Mac OS 8.5 Control Manager (proportional scrollbars)? */
110     if (Gestalt(gestaltControlMgrAttr, &mac_gestalts.cntlattr) != noErr)
111         mac_gestalts.cntlattr = 0;
112
113     /* We've been tested with the Appearance Manager */
114     if (mac_gestalts.apprvers != 0)
115         RegisterAppearanceClient();
116     
117     menuBar = GetNewMBar(128);
118     if (menuBar == NULL)
119         fatalbox("Unable to create menu bar.");
120     SetMenuBar(menuBar);
121     AppendResMenu(GetMenuHandle(mApple), 'DRVR');
122     mac_adjustmenus();
123     DrawMenuBar();
124     InitCursor();
125 }
126
127 static void mac_eventloop(void) {
128     Boolean gotevent;
129     EventRecord event;
130     int i;
131
132     for (;;) {
133         mac_adjustcursor();
134         gotevent = WaitNextEvent(everyEvent, &event, LONG_MAX, NULL);
135         mac_adjustcursor();
136         if (gotevent)
137             mac_event(&event);
138     }
139 }
140
141 static void mac_event(EventRecord *event) {
142     short part;
143     WindowPtr window;
144     Point pt;
145
146     switch (event->what) {
147       case mouseDown:
148         part = FindWindow(event->where, &window);
149         switch (part) {
150           case inMenuBar:
151             mac_adjustmenus();
152             mac_menucommand(MenuSelect(event->where));
153             break;
154           case inSysWindow:
155             SystemClick(event, window);
156             break;
157           case inContent:
158             if (window != FrontWindow())
159                 /* XXX: check for movable modal dboxes? */
160                 SelectWindow(window);
161             else
162                 mac_contentclick(window, event);
163             break;
164           case inGoAway:
165             if (TrackGoAway(window, event->where))
166                 mac_closewindow(window);
167             break;
168           case inDrag:
169             /* XXX: moveable modal check? */
170             DragWindow(window, event->where, &qd.screenBits.bounds);
171             break;
172           case inGrow:
173             mac_growwindow(window, event);
174             break;
175           case inZoomIn:
176           case inZoomOut:
177             if (TrackBox(window, event->where, part))
178                 mac_zoomwindow(window, part);
179             break;
180         }
181         break;
182       case keyDown:
183       case autoKey:
184         mac_keypress(event);
185         break;
186       case activateEvt:
187         mac_activatewindow((WindowPtr)event->message,
188                            (event->modifiers & activeFlag) != 0);
189         break;
190       case updateEvt:
191         mac_updatewindow((WindowPtr)event->message);
192         break;
193       case diskEvt:
194         if (HiWord(event->message) != noErr) {
195             SetPt(&pt, 120, 120);
196             DIBadMount(pt, event->message);
197         }
198         break;
199     }
200 }
201
202 static void mac_contentclick(WindowPtr window, EventRecord *event) {
203     short item;
204
205     switch (mac_windowtype(window)) {
206       case wTerminal:
207         mac_clickterm(window, event);
208         break;
209       case wAbout:
210         if (DialogSelect(event, &(DialogPtr)window, &item))
211             switch (item) {
212               case wiAboutLicence:
213                 /* XXX: Do something */
214                 break;
215             }
216         break;
217     }
218 }
219
220 static void mac_growwindow(WindowPtr window, EventRecord *event) {
221
222     switch (mac_windowtype(window)) {
223       case wTerminal:
224         mac_growterm(window, event);
225     }
226 }
227
228 static void mac_activatewindow(WindowPtr window, Boolean active) {
229
230     switch (mac_windowtype(window)) {
231       case wTerminal:
232         mac_activateterm(window, active);
233         break;
234     }
235 }
236
237 static void mac_updatewindow(WindowPtr window) {
238
239     switch (mac_windowtype(window)) {
240       case wTerminal:
241         mac_updateterm(window);
242         break;
243       case wAbout:
244         BeginUpdate(window);
245         UpdateDialog(window, window->visRgn);
246         EndUpdate(window);
247         break;
248       case wLicence:
249         /* Do something */
250         break;
251     }
252 }
253
254 /*
255  * Work out what kind of window we're dealing with.
256  * Concept shamelessly nicked from SurfWriter.
257  */
258 static int mac_windowtype(WindowPtr window) {
259     int kind;
260
261     if (window == NULL)
262         return wNone;
263     kind = ((WindowPeek)window)->windowKind;
264     if (kind < 0)
265         return wDA;
266     if (GetWVariant(window) == zoomDocProc)
267         return wTerminal;
268     return GetWRefCon(window);
269 }
270
271 /*
272  * Handle a key press
273  */
274 static void mac_keypress(EventRecord *event) {
275     char key;
276
277     if (event->what == keyDown && (event->modifiers & cmdKey)) {
278         mac_adjustmenus();
279         mac_menucommand(MenuKey(event->message & charCodeMask));
280     }
281 }
282
283 static void mac_menucommand(long result) {
284     short menu, item;
285     Str255 da;
286
287     menu = HiWord(result);
288     item = LoWord(result);
289     switch (menu) {
290       case mApple:
291         switch (item) {
292           case iAbout:
293             GetNewDialog(wAbout, NULL, (GrafPort *)-1);
294             break;
295           default:
296             GetMenuItemText(GetMenuHandle(mApple), item, da);
297             OpenDeskAcc(da);
298             break;
299         }
300         break;
301       case mFile:
302         switch (item) {
303           case iNew:
304             mac_newsession();
305             break;
306           case iClose:
307             mac_closewindow(FrontWindow());
308             break;
309           case iQuit:
310             mac_shutdown();
311             break;
312         }
313         break;
314     }
315     HiliteMenu(0);
316 }
317
318 static void mac_closewindow(WindowPtr window) {
319
320     switch (mac_windowtype(window)) {
321       case wDA:
322         CloseDeskAcc(((WindowPeek)window)->windowKind);
323         break;
324       case wTerminal:
325         /* FIXME: end session and stuff */
326         break;
327       default:
328         CloseWindow(window);
329         break;
330     }
331 }
332
333 static void mac_zoomwindow(WindowPtr window, short part) {
334
335     /* FIXME: do something */
336 }
337
338 /*
339  * Make the menus look right before the user gets to see them.
340  */
341 static void mac_adjustmenus(void) {
342
343 }
344
345 /*
346  * Make sure the right cursor's being displayed.
347  */
348 static void mac_adjustcursor(void) {
349
350     SetCursor(&qd.arrow);
351 }
352
353 static void mac_shutdown(void) {
354
355     exit(0);
356 }
357
358 void fatalbox(const char *fmt, ...) {
359     va_list ap;
360     Str255 stuff;
361     
362     va_start(ap, fmt);
363     /* We'd like stuff to be a Pascal string */
364     stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
365     va_end(ap);
366     ParamText(stuff, NULL, NULL, NULL);
367     StopAlert(128, nil);
368     exit(1);
369 }