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