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