Add an "open" command to the "file" (now "session") menu on the Mac to
[u/mdw/putty] / mac / mac.c
CommitLineData
ce283213 1/* $Id: mac.c,v 1.10 2002/12/30 18:21:17 ben Exp $ */
d082ac49 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 <CodeFragments.h>
39#include <Dialogs.h>
40#include <Devices.h>
41#include <DiskInit.h>
42#include <Gestalt.h>
43#include <Resources.h>
36577dc9 44#include <Script.h>
8768ce31 45#include <TextCommon.h>
d082ac49 46#include <ToolUtils.h>
8768ce31 47#include <UnicodeConverter.h>
d082ac49 48
49#include <assert.h>
50#include <limits.h>
51#include <stdarg.h>
52#include <stdlib.h> /* putty.h needs size_t */
53#include <stdio.h> /* for vsprintf */
54
55#define PUTTY_DO_GLOBALS
56
57#include "macresid.h"
58#include "putty.h"
59#include "mac.h"
60
61QDGlobals qd;
62
63static int cold = 1;
64struct mac_gestalts mac_gestalts;
65
66static void mac_startup(void);
67static void mac_eventloop(void);
68#pragma noreturn (mac_eventloop)
69static void mac_event(EventRecord *);
70static void mac_contentclick(WindowPtr, EventRecord *);
71static void mac_growwindow(WindowPtr, EventRecord *);
72static void mac_activatewindow(WindowPtr, EventRecord *);
73static void mac_activateabout(WindowPtr, EventRecord *);
74static void mac_updatewindow(WindowPtr);
ebd78b62 75static void mac_updatelicence(WindowPtr);
d082ac49 76static void mac_keypress(EventRecord *);
77static int mac_windowtype(WindowPtr);
78static void mac_menucommand(long);
79static void mac_openabout(void);
ebd78b62 80static void mac_openlicence(void);
d082ac49 81static void mac_adjustcursor(RgnHandle);
82static void mac_adjustmenus(void);
83static void mac_closewindow(WindowPtr);
84static void mac_zoomwindow(WindowPtr, short);
85static void mac_shutdown(void);
86#pragma noreturn (mac_shutdown)
87
88struct mac_windows {
89 WindowPtr about;
90 WindowPtr licence;
91};
92
93struct mac_windows windows;
94
95int main (int argc, char **argv) {
96
97 mac_startup();
98 mac_eventloop();
99}
100
101#pragma noreturn (main)
102
103static void mac_startup(void) {
104 Handle menuBar;
8768ce31 105 TECInfoHandle ti;
d082ac49 106
107 /* Init Memory Manager */
108 MaxApplZone();
109 /* Init QuickDraw */
110 InitGraf(&qd.thePort);
111 /* Init Font Manager */
112 InitFonts();
113 /* Init Window Manager */
114 InitWindows();
115 /* Init Menu Manager */
116 InitMenus();
117 /* Init TextEdit */
118 TEInit();
119 /* Init Dialog Manager */
120 InitDialogs(nil);
121 cold = 0;
122
56ed4cf7 123 /* Get base system version (only used if there's no better selector) */
124 if (Gestalt(gestaltSystemVersion, &mac_gestalts.sysvers) != noErr ||
125 (mac_gestalts.sysvers & 0xffff) < 0x700)
126 fatalbox("PuTTY requires System 7 or newer");
127 mac_gestalts.sysvers &= 0xffff;
d082ac49 128 /* Find out if we've got Color Quickdraw */
129 if (Gestalt(gestaltQuickdrawVersion, &mac_gestalts.qdvers) != noErr)
130 mac_gestalts.qdvers = gestaltOriginalQD;
131 /* ... and the Appearance Manager? */
132 if (Gestalt(gestaltAppearanceVersion, &mac_gestalts.apprvers) != noErr)
133 if (Gestalt(gestaltAppearanceAttr, NULL) == noErr)
134 mac_gestalts.apprvers = 0x0100;
135 else
136 mac_gestalts.apprvers = 0;
137#if TARGET_RT_MAC_CFM
138 /* Paranoia: Did we manage to pull in AppearanceLib? */
139 if (&RegisterAppearanceClient == kUnresolvedCFragSymbolAddress)
140 mac_gestalts.apprvers = 0;
141#endif
142 /* Mac OS 8.5 Control Manager (proportional scrollbars)? */
143 if (Gestalt(gestaltControlMgrAttr, &mac_gestalts.cntlattr) != noErr)
144 mac_gestalts.cntlattr = 0;
145 /* Mac OS 8.5 Window Manager? */
146 if (Gestalt(gestaltWindowMgrAttr, &mac_gestalts.windattr) != noErr)
147 mac_gestalts.windattr = 0;
8768ce31 148 /* Text Encoding Conversion Manager? */
149 if (
150#if TARGET_RT_MAC_CFM
151 &TECGetInfo == kUnresolvedCFragSymbolAddress ||
152#else
153 InitializeUnicodeConverter(NULL) != noErr ||
154#endif
155 TECGetInfo(&ti) != noErr)
156 mac_gestalts.encvvers = 0;
157 else {
158 mac_gestalts.encvvers = (*ti)->tecVersion;
159 DisposeHandle((Handle)ti);
160 }
d082ac49 161
162 /* We've been tested with the Appearance Manager */
163 if (mac_gestalts.apprvers != 0)
164 RegisterAppearanceClient();
165
166 menuBar = GetNewMBar(128);
167 if (menuBar == NULL)
168 fatalbox("Unable to create menu bar.");
169 SetMenuBar(menuBar);
170 AppendResMenu(GetMenuHandle(mApple), 'DRVR');
171 mac_adjustmenus();
172 DrawMenuBar();
173 InitCursor();
174 windows.about = NULL;
175 windows.licence = NULL;
176
177 init_ucs();
178}
179
180static void mac_eventloop(void) {
181 Boolean gotevent;
182 EventRecord event;
183 RgnHandle cursrgn;
184
185 cursrgn = NewRgn();
186 for (;;) {
187 mac_adjustcursor(cursrgn);
188 gotevent = WaitNextEvent(everyEvent, &event, LONG_MAX, cursrgn);
189 mac_adjustcursor(cursrgn);
190 if (gotevent)
191 mac_event(&event);
d082ac49 192 }
193 DisposeRgn(cursrgn);
194}
195
196static void mac_event(EventRecord *event) {
197 short part;
198 WindowPtr window;
199 Point pt;
200
201 switch (event->what) {
202 case mouseDown:
203 part = FindWindow(event->where, &window);
204 switch (part) {
205 case inMenuBar:
206 mac_adjustmenus();
207 mac_menucommand(MenuSelect(event->where));
208 break;
209 case inSysWindow:
210 SystemClick(event, window);
211 break;
212 case inContent:
213 if (window != FrontWindow())
214 /* XXX: check for movable modal dboxes? */
215 SelectWindow(window);
216 else
217 mac_contentclick(window, event);
218 break;
219 case inGoAway:
220 if (TrackGoAway(window, event->where))
221 mac_closewindow(window);
222 break;
223 case inDrag:
224 /* XXX: moveable modal check? */
225 DragWindow(window, event->where, &qd.screenBits.bounds);
226 break;
227 case inGrow:
228 mac_growwindow(window, event);
229 break;
230 case inZoomIn:
231 case inZoomOut:
232 if (TrackBox(window, event->where, part))
233 mac_zoomwindow(window, part);
234 break;
235 }
236 break;
237 case keyDown:
238 case autoKey:
239 mac_keypress(event);
240 break;
241 case activateEvt:
242 mac_activatewindow((WindowPtr)event->message, event);
243 break;
244 case updateEvt:
245 mac_updatewindow((WindowPtr)event->message);
246 break;
247 case diskEvt:
248 if (HiWord(event->message) != noErr) {
249 SetPt(&pt, 120, 120);
250 DIBadMount(pt, event->message);
251 }
252 break;
253 }
254}
255
256static void mac_contentclick(WindowPtr window, EventRecord *event) {
257 short item;
258
259 switch (mac_windowtype(window)) {
260 case wTerminal:
261 mac_clickterm(window, event);
262 break;
263 case wAbout:
264 if (DialogSelect(event, &(DialogPtr)window, &item))
265 switch (item) {
266 case wiAboutLicence:
ebd78b62 267 mac_openlicence();
d082ac49 268 break;
269 }
270 break;
271 }
272}
273
274static void mac_growwindow(WindowPtr window, EventRecord *event) {
275
276 switch (mac_windowtype(window)) {
277 case wTerminal:
278 mac_growterm(window, event);
279 }
280}
281
282static void mac_activatewindow(WindowPtr window, EventRecord *event) {
283 int active;
284
285 active = (event->modifiers & activeFlag) != 0;
286 mac_adjustmenus();
287 switch (mac_windowtype(window)) {
288 case wTerminal:
289 mac_activateterm(window, active);
290 break;
291 case wAbout:
292 mac_activateabout(window, event);
293 break;
294 }
295}
296
297static void mac_activateabout(WindowPtr window, EventRecord *event) {
298 DialogItemType itemtype;
299 Handle itemhandle;
300 short item;
301 Rect itemrect;
302 int active;
303
304 active = (event->modifiers & activeFlag) != 0;
305 GetDialogItem(window, wiAboutLicence, &itemtype, &itemhandle, &itemrect);
306 HiliteControl((ControlHandle)itemhandle, active ? 0 : 255);
307 DialogSelect(event, &window, &item);
308}
309
310static void mac_updatewindow(WindowPtr window) {
311
312 switch (mac_windowtype(window)) {
313 case wTerminal:
314 mac_updateterm(window);
315 break;
316 case wAbout:
317 BeginUpdate(window);
318 UpdateDialog(window, window->visRgn);
319 EndUpdate(window);
320 break;
321 case wLicence:
ebd78b62 322 mac_updatelicence(window);
323 break;
324 }
325}
326
327static void mac_updatelicence(WindowPtr window)
328{
329 Handle h;
330 int len;
36577dc9 331 long fondsize;
ebd78b62 332
333 SetPort(window);
334 BeginUpdate(window);
36577dc9 335 fondsize = GetScriptVariable(smRoman, smScriptSmallFondSize);
336 TextFont(HiWord(fondsize));
337 TextSize(LoWord(fondsize));
ebd78b62 338 h = Get1Resource('TEXT', wLicence);
339 len = GetResourceSizeOnDisk(h);
340 if (h != NULL) {
341 HLock(h);
342 TETextBox(*h, len, &window->portRect, teFlushDefault);
343 HUnlock(h);
d082ac49 344 }
ebd78b62 345 EndUpdate(window);
d082ac49 346}
347
348/*
349 * Work out what kind of window we're dealing with.
350 * Concept shamelessly nicked from SurfWriter.
351 */
352static int mac_windowtype(WindowPtr window) {
353 int kind;
354
355 if (window == NULL)
356 return wNone;
357 kind = ((WindowPeek)window)->windowKind;
358 if (kind < 0)
359 return wDA;
360 if (GetWVariant(window) == zoomDocProc)
361 return wTerminal;
362 return GetWRefCon(window);
363}
364
365/*
366 * Handle a key press
367 */
368static void mac_keypress(EventRecord *event) {
369 WindowPtr window;
370
371 window = FrontWindow();
372 /*
373 * Check for a command-key combination, but ignore it if it counts
374 * as a meta-key combination and we're in a terminal window.
375 */
376 if (event->what == keyDown && (event->modifiers & cmdKey) /*&&
377 !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers &&
378 mac_windowtype(window) == wTerminal)*/) {
379 mac_adjustmenus();
380 mac_menucommand(MenuKey(event->message & charCodeMask));
381 } else {
382 switch (mac_windowtype(window)) {
383 case wTerminal:
384 mac_keyterm(window, event);
385 break;
386 }
387 }
388}
389
390static void mac_menucommand(long result) {
391 short menu, item;
392 Str255 da;
393 WindowPtr window;
394
395 menu = HiWord(result);
396 item = LoWord(result);
397 window = FrontWindow();
398 /* Things which do the same whatever window we're in. */
399 switch (menu) {
400 case mApple:
401 switch (item) {
402 case iAbout:
403 mac_openabout();
404 goto done;
405 default:
406 GetMenuItemText(GetMenuHandle(mApple), item, da);
407 OpenDeskAcc(da);
408 goto done;
409 }
410 break;
411 case mFile:
412 switch (item) {
413 case iNew:
414 mac_newsession();
415 goto done;
ce283213 416 case iOpen:
417 mac_opensession();
418 goto done;
d082ac49 419 case iClose:
420 mac_closewindow(window);
421 goto done;
422 case iQuit:
423 mac_shutdown();
424 goto done;
425 }
426 break;
427 }
428 /* If we get here, handling is up to window-specific code. */
429 switch (mac_windowtype(window)) {
430 case wTerminal:
431 mac_menuterm(window, menu, item);
432 break;
433 }
434 done:
435 HiliteMenu(0);
436}
437
438static void mac_openabout(void) {
439 DialogItemType itemtype;
440 Handle item;
441 VersRecHndl vers;
442 Rect box;
443 StringPtr longvers;
444
445 if (windows.about)
446 SelectWindow(windows.about);
447 else {
448 windows.about = GetNewDialog(wAbout, NULL, (WindowPtr)-1);
5dbc118e 449 vers = (VersRecHndl)Get1Resource('vers', 1);
450 if (vers != NULL && *vers != NULL) {
451 longvers = (*vers)->shortVersion + (*vers)->shortVersion[0] + 1;
452 GetDialogItem(windows.about, wiAboutVersion,
453 &itemtype, &item, &box);
454 assert(itemtype & kStaticTextDialogItem);
455 SetDialogItemText(item, longvers);
456 }
d082ac49 457 ShowWindow(windows.about);
458 }
459}
460
ebd78b62 461static void mac_openlicence(void) {
ebd78b62 462
463 if (windows.licence)
464 SelectWindow(windows.licence);
465 else {
466 windows.licence = GetNewWindow(wLicence, NULL, (WindowPtr)-1);
467 ShowWindow(windows.licence);
468 }
469}
470
d082ac49 471static void mac_closewindow(WindowPtr window) {
472
473 switch (mac_windowtype(window)) {
474 case wDA:
475 CloseDeskAcc(((WindowPeek)window)->windowKind);
476 break;
477 case wTerminal:
478 /* FIXME: end session and stuff */
479 break;
480 case wAbout:
481 windows.about = NULL;
482 CloseWindow(window);
483 break;
ebd78b62 484 case wLicence:
485 windows.licence = NULL;
486 CloseWindow(window);
487 break;
d082ac49 488 default:
489 CloseWindow(window);
490 break;
491 }
492}
493
494static void mac_zoomwindow(WindowPtr window, short part) {
495
496 /* FIXME: do something */
497}
498
499/*
500 * Make the menus look right before the user gets to see them.
501 */
502static void mac_adjustmenus(void) {
503 WindowPtr window;
504 MenuHandle menu;
505
506 window = FrontWindow();
507 menu = GetMenuHandle(mApple);
508 EnableItem(menu, 0);
509 EnableItem(menu, iAbout);
510
511 menu = GetMenuHandle(mFile);
512 EnableItem(menu, 0);
513 EnableItem(menu, iNew);
514 if (window != NULL)
515 EnableItem(menu, iClose);
516 else
517 DisableItem(menu, iClose);
518 EnableItem(menu, iQuit);
519
520 switch (mac_windowtype(window)) {
521 case wTerminal:
522 mac_adjusttermmenus(window);
523 break;
524 default:
525 menu = GetMenuHandle(mEdit);
526 DisableItem(menu, 0);
527 break;
528 }
529 DrawMenuBar();
530}
531
532/*
533 * Make sure the right cursor's being displayed.
534 */
535static void mac_adjustcursor(RgnHandle cursrgn) {
536 Point mouse;
537 WindowPtr window, front;
538 short part;
539
540 GetMouse(&mouse);
541 LocalToGlobal(&mouse);
542 part = FindWindow(mouse, &window);
543 front = FrontWindow();
544 if (part != inContent || window == NULL || window != front) {
545 /* Cursor isn't in the front window, so switch to arrow */
546 SetCursor(&qd.arrow);
547 SetRectRgn(cursrgn, SHRT_MIN, SHRT_MIN, SHRT_MAX, SHRT_MAX);
548 if (front != NULL)
549 DiffRgn(cursrgn, front->visRgn, cursrgn);
550 } else {
551 switch (mac_windowtype(window)) {
552 case wTerminal:
553 mac_adjusttermcursor(window, mouse, cursrgn);
554 break;
555 default:
556 SetCursor(&qd.arrow);
557 CopyRgn(window->visRgn, cursrgn);
558 break;
559 }
560 }
561}
562
563static void mac_shutdown(void) {
564
713b8b7a 565#if !TARGET_RT_MAC_CFM
8768ce31 566 if (mac_gestalts.encvvers != 0)
567 TerminateUnicodeConverter();
713b8b7a 568#endif
d082ac49 569 exit(0);
570}
571
572void fatalbox(char *fmt, ...) {
573 va_list ap;
574 Str255 stuff;
575
576 va_start(ap, fmt);
577 /* We'd like stuff to be a Pascal string */
578 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
579 va_end(ap);
580 ParamText(stuff, NULL, NULL, NULL);
581 StopAlert(128, nil);
582 exit(1);
583}
584
585void modalfatalbox(char *fmt, ...) {
586 va_list ap;
587 Str255 stuff;
588
589 va_start(ap, fmt);
590 /* We'd like stuff to be a Pascal string */
591 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
592 va_end(ap);
593 ParamText(stuff, NULL, NULL, NULL);
594 StopAlert(128, nil);
595 exit(1);
596}
597
598/*
599 * Local Variables:
600 * c-file-style: "simon"
601 * End:
602 */