1 /* $Id: mac.c,v 1.17 2003/01/04 12:45:11 ben Exp $ */
3 * Copyright (c) 1999 Ben Harris
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
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
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
28 * mac.c -- miscellaneous Mac-specific routines
32 #include <Quickdraw.h>
34 #include <MacWindows.h>
37 #include <Appearance.h>
38 #include <CodeFragments.h>
44 #include <Resources.h>
46 #include <TextCommon.h>
47 #include <ToolUtils.h>
48 #include <UnicodeConverter.h>
53 #include <stdlib.h> /* putty.h needs size_t */
54 #include <stdio.h> /* for vsprintf */
56 #define PUTTY_DO_GLOBALS
65 struct mac_gestalts mac_gestalts;
67 static void mac_startup(void);
68 static void mac_eventloop(void);
69 #pragma noreturn (mac_eventloop)
70 static void mac_event(EventRecord *);
71 static void mac_contentclick(WindowPtr, EventRecord *);
72 static void mac_growwindow(WindowPtr, EventRecord *);
73 static void mac_activatewindow(WindowPtr, EventRecord *);
74 static void mac_activateabout(WindowPtr, EventRecord *);
75 static void mac_updatewindow(WindowPtr);
76 static void mac_updatelicence(WindowPtr);
77 static void mac_keypress(EventRecord *);
78 static int mac_windowtype(WindowPtr);
79 static void mac_menucommand(long);
80 static void mac_openabout(void);
81 static void mac_openlicence(void);
82 static void mac_adjustcursor(RgnHandle);
83 static void mac_adjustmenus(void);
84 static void mac_closewindow(WindowPtr);
85 static void mac_zoomwindow(WindowPtr, short);
86 static void mac_shutdown(void);
87 #pragma noreturn (mac_shutdown)
94 struct mac_windows windows;
96 int main (int argc, char **argv) {
102 #pragma noreturn (main)
104 static void mac_startup(void) {
108 /* Init Memory Manager */
111 InitGraf(&qd.thePort);
112 /* Init Font Manager */
114 /* Init Window Manager */
116 /* Init Menu Manager */
120 /* Init Dialog Manager */
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 /* 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;
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;
143 mac_gestalts.cntlattr = 0;
144 mac_gestalts.windattr = 0;
146 /* Mac OS 8.5 Control Manager (proportional scrollbars)? */
147 if (Gestalt(gestaltControlMgrAttr, &mac_gestalts.cntlattr) != noErr ||
148 &SetControlViewSize == kUnresolvedCFragSymbolAddress)
149 mac_gestalts.cntlattr = 0;
150 /* Mac OS 8.5 Window Manager? */
151 if (Gestalt(gestaltWindowMgrAttr, &mac_gestalts.windattr) != noErr ||
152 &SetWindowContentColor == kUnresolvedCFragSymbolAddress)
153 mac_gestalts.windattr = 0;
155 /* Text Encoding Conversion Manager? */
157 #if TARGET_RT_MAC_CFM
158 &TECGetInfo == kUnresolvedCFragSymbolAddress ||
160 InitializeUnicodeConverter(NULL) != noErr ||
162 TECGetInfo(&ti) != noErr)
163 mac_gestalts.encvvers = 0;
165 mac_gestalts.encvvers = (*ti)->tecVersion;
166 mac_gestalts.uncvattr = (*ti)->tecUnicodeConverterFeatures;
167 DisposeHandle((Handle)ti);
170 /* We've been tested with the Appearance Manager */
171 if (mac_gestalts.apprvers != 0)
172 RegisterAppearanceClient();
174 menuBar = GetNewMBar(128);
176 fatalbox("Unable to create menu bar.");
178 AppendResMenu(GetMenuHandle(mApple), 'DRVR');
182 windows.about = NULL;
183 windows.licence = NULL;
189 /* Set the default directory for loading and saving settings. */
190 /* XXX Should we create it? */
191 if (get_session_dir(FALSE, &vol, &dirid) == noErr) {
192 LMSetSFSaveDisk(-vol);
193 LMSetCurDirStore(dirid);
199 static void mac_eventloop(void) {
206 mac_adjustcursor(cursrgn);
207 gotevent = WaitNextEvent(everyEvent, &event, LONG_MAX, cursrgn);
208 mac_adjustcursor(cursrgn);
215 static void mac_event(EventRecord *event) {
220 switch (event->what) {
222 part = FindWindow(event->where, &window);
226 mac_menucommand(MenuSelect(event->where));
229 SystemClick(event, window);
232 if (window != FrontWindow())
233 /* XXX: check for movable modal dboxes? */
234 SelectWindow(window);
236 mac_contentclick(window, event);
239 if (TrackGoAway(window, event->where))
240 mac_closewindow(window);
243 /* XXX: moveable modal check? */
244 DragWindow(window, event->where, &qd.screenBits.bounds);
247 mac_growwindow(window, event);
251 if (TrackBox(window, event->where, part))
252 mac_zoomwindow(window, part);
261 mac_activatewindow((WindowPtr)event->message, event);
264 mac_updatewindow((WindowPtr)event->message);
267 if (HiWord(event->message) != noErr) {
268 SetPt(&pt, 120, 120);
269 DIBadMount(pt, event->message);
275 static void mac_contentclick(WindowPtr window, EventRecord *event) {
278 switch (mac_windowtype(window)) {
280 mac_clickterm(window, event);
283 if (DialogSelect(event, &window, &item))
291 mac_clickdlg(window, event);
296 static void mac_growwindow(WindowPtr window, EventRecord *event) {
298 switch (mac_windowtype(window)) {
300 mac_growterm(window, event);
304 static void mac_activatewindow(WindowPtr window, EventRecord *event) {
307 active = (event->modifiers & activeFlag) != 0;
309 switch (mac_windowtype(window)) {
311 mac_activateterm(window, active);
314 mac_activatedlg(window, event);
317 mac_activateabout(window, event);
322 static void mac_activateabout(WindowPtr window, EventRecord *event) {
323 DialogItemType itemtype;
329 active = (event->modifiers & activeFlag) != 0;
330 GetDialogItem(window, wiAboutLicence, &itemtype, &itemhandle, &itemrect);
331 HiliteControl((ControlHandle)itemhandle, active ? 0 : 255);
332 DialogSelect(event, &window, &item);
335 static void mac_updatewindow(WindowPtr window) {
337 switch (mac_windowtype(window)) {
339 mac_updateterm(window);
344 UpdateDialog(window, window->visRgn);
348 mac_updatelicence(window);
353 static void mac_updatelicence(WindowPtr window)
361 fondsize = GetScriptVariable(smRoman, smScriptSmallFondSize);
362 TextFont(HiWord(fondsize));
363 TextSize(LoWord(fondsize));
364 h = Get1Resource('TEXT', wLicence);
365 len = GetResourceSizeOnDisk(h);
368 TETextBox(*h, len, &window->portRect, teFlushDefault);
375 * Work out what kind of window we're dealing with.
376 * Concept shamelessly nicked from SurfWriter.
378 static int mac_windowtype(WindowPtr window) {
384 kind = ((WindowPeek)window)->windowKind;
387 if (GetWVariant(window) == zoomDocProc)
389 refcon = GetWRefCon(window);
399 static void mac_keypress(EventRecord *event) {
402 window = FrontWindow();
404 * Check for a command-key combination, but ignore it if it counts
405 * as a meta-key combination and we're in a terminal window.
407 if (event->what == keyDown && (event->modifiers & cmdKey) /*&&
408 !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers &&
409 mac_windowtype(window) == wTerminal)*/) {
411 mac_menucommand(MenuKey(event->message & charCodeMask));
413 switch (mac_windowtype(window)) {
415 mac_keyterm(window, event);
421 static void mac_menucommand(long result) {
426 menu = HiWord(result);
427 item = LoWord(result);
428 window = FrontWindow();
429 /* Things which do the same whatever window we're in. */
437 GetMenuItemText(GetMenuHandle(mApple), item, da);
451 mac_closewindow(window);
459 /* If we get here, handling is up to window-specific code. */
460 switch (mac_windowtype(window)) {
462 mac_menuterm(window, menu, item);
469 static void mac_openabout(void) {
470 DialogItemType itemtype;
477 SelectWindow(windows.about);
479 windows.about = GetNewDialog(wAbout, NULL, (WindowPtr)-1);
480 vers = (VersRecHndl)Get1Resource('vers', 1);
481 if (vers != NULL && *vers != NULL) {
482 longvers = (*vers)->shortVersion + (*vers)->shortVersion[0] + 1;
483 GetDialogItem(windows.about, wiAboutVersion,
484 &itemtype, &item, &box);
485 assert(itemtype & kStaticTextDialogItem);
486 SetDialogItemText(item, longvers);
488 ShowWindow(windows.about);
492 static void mac_openlicence(void) {
495 SelectWindow(windows.licence);
497 windows.licence = GetNewWindow(wLicence, NULL, (WindowPtr)-1);
498 ShowWindow(windows.licence);
502 static void mac_closewindow(WindowPtr window) {
504 switch (mac_windowtype(window)) {
506 CloseDeskAcc(((WindowPeek)window)->windowKind);
509 /* FIXME: end session and stuff */
512 windows.about = NULL;
516 windows.licence = NULL;
525 static void mac_zoomwindow(WindowPtr window, short part) {
527 /* FIXME: do something */
531 * Make the menus look right before the user gets to see them.
533 static void mac_adjustmenus(void) {
537 window = FrontWindow();
538 menu = GetMenuHandle(mApple);
540 EnableItem(menu, iAbout);
542 menu = GetMenuHandle(mFile);
544 EnableItem(menu, iNew);
546 EnableItem(menu, iClose);
548 DisableItem(menu, iClose);
549 EnableItem(menu, iQuit);
551 switch (mac_windowtype(window)) {
553 mac_adjusttermmenus(window);
556 menu = GetMenuHandle(mEdit);
557 DisableItem(menu, 0);
564 * Make sure the right cursor's being displayed.
566 static void mac_adjustcursor(RgnHandle cursrgn) {
568 WindowPtr window, front;
572 LocalToGlobal(&mouse);
573 part = FindWindow(mouse, &window);
574 front = FrontWindow();
575 if (part != inContent || window == NULL || window != front) {
576 /* Cursor isn't in the front window, so switch to arrow */
577 SetCursor(&qd.arrow);
578 SetRectRgn(cursrgn, SHRT_MIN, SHRT_MIN, SHRT_MAX, SHRT_MAX);
580 DiffRgn(cursrgn, front->visRgn, cursrgn);
582 switch (mac_windowtype(window)) {
584 mac_adjusttermcursor(window, mouse, cursrgn);
587 SetCursor(&qd.arrow);
588 CopyRgn(window->visRgn, cursrgn);
594 static void mac_shutdown(void) {
596 #if !TARGET_RT_MAC_CFM
597 if (mac_gestalts.encvvers != 0)
598 TerminateUnicodeConverter();
603 void fatalbox(char *fmt, ...) {
608 /* We'd like stuff to be a Pascal string */
609 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
611 ParamText(stuff, NULL, NULL, NULL);
612 StopAlert(128, NULL);
616 void modalfatalbox(char *fmt, ...) {
621 /* We'd like stuff to be a Pascal string */
622 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
624 ParamText(stuff, NULL, NULL, NULL);
625 StopAlert(128, NULL);
631 * c-file-style: "simon"