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