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