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