1 /* $Id: mac.c,v 1.2 2002/11/23 15:11:13 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>
43 #include <Resources.h>
44 #include <ToolUtils.h>
49 #include <stdlib.h> /* putty.h needs size_t */
50 #include <stdio.h> /* for vsprintf */
52 #define PUTTY_DO_GLOBALS
61 struct mac_gestalts mac_gestalts;
63 static void mac_startup(void);
64 static void mac_eventloop(void);
65 #pragma noreturn (mac_eventloop)
66 static void mac_event(EventRecord *);
67 static void mac_contentclick(WindowPtr, EventRecord *);
68 static void mac_growwindow(WindowPtr, EventRecord *);
69 static void mac_activatewindow(WindowPtr, EventRecord *);
70 static void mac_activateabout(WindowPtr, EventRecord *);
71 static void mac_updatewindow(WindowPtr);
72 static void mac_keypress(EventRecord *);
73 static int mac_windowtype(WindowPtr);
74 static void mac_menucommand(long);
75 static void mac_openabout(void);
76 static void mac_adjustcursor(RgnHandle);
77 static void mac_adjustmenus(void);
78 static void mac_closewindow(WindowPtr);
79 static void mac_zoomwindow(WindowPtr, short);
80 static void mac_shutdown(void);
81 #pragma noreturn (mac_shutdown)
88 struct mac_windows windows;
90 int main (int argc, char **argv) {
96 #pragma noreturn (main)
98 static void mac_startup(void) {
101 /* Init Memory Manager */
104 InitGraf(&qd.thePort);
105 /* Init Font Manager */
107 /* Init Window Manager */
109 /* Init Menu Manager */
113 /* Init Dialog Manager */
117 /* Find out if we've got Color Quickdraw */
118 if (Gestalt(gestaltQuickdrawVersion, &mac_gestalts.qdvers) != noErr)
119 mac_gestalts.qdvers = gestaltOriginalQD;
120 /* ... and the Appearance Manager? */
121 if (Gestalt(gestaltAppearanceVersion, &mac_gestalts.apprvers) != noErr)
122 if (Gestalt(gestaltAppearanceAttr, NULL) == noErr)
123 mac_gestalts.apprvers = 0x0100;
125 mac_gestalts.apprvers = 0;
126 #if TARGET_RT_MAC_CFM
127 /* Paranoia: Did we manage to pull in AppearanceLib? */
128 if (&RegisterAppearanceClient == kUnresolvedCFragSymbolAddress)
129 mac_gestalts.apprvers = 0;
131 /* Mac OS 8.5 Control Manager (proportional scrollbars)? */
132 if (Gestalt(gestaltControlMgrAttr, &mac_gestalts.cntlattr) != noErr)
133 mac_gestalts.cntlattr = 0;
134 /* Mac OS 8.5 Window Manager? */
135 if (Gestalt(gestaltWindowMgrAttr, &mac_gestalts.windattr) != noErr)
136 mac_gestalts.windattr = 0;
138 /* We've been tested with the Appearance Manager */
139 if (mac_gestalts.apprvers != 0)
140 RegisterAppearanceClient();
142 menuBar = GetNewMBar(128);
144 fatalbox("Unable to create menu bar.");
146 AppendResMenu(GetMenuHandle(mApple), 'DRVR');
150 windows.about = NULL;
151 windows.licence = NULL;
156 static void mac_eventloop(void) {
163 mac_adjustcursor(cursrgn);
164 gotevent = WaitNextEvent(everyEvent, &event, LONG_MAX, cursrgn);
165 mac_adjustcursor(cursrgn);
172 static void mac_event(EventRecord *event) {
177 switch (event->what) {
179 part = FindWindow(event->where, &window);
183 mac_menucommand(MenuSelect(event->where));
186 SystemClick(event, window);
189 if (window != FrontWindow())
190 /* XXX: check for movable modal dboxes? */
191 SelectWindow(window);
193 mac_contentclick(window, event);
196 if (TrackGoAway(window, event->where))
197 mac_closewindow(window);
200 /* XXX: moveable modal check? */
201 DragWindow(window, event->where, &qd.screenBits.bounds);
204 mac_growwindow(window, event);
208 if (TrackBox(window, event->where, part))
209 mac_zoomwindow(window, part);
218 mac_activatewindow((WindowPtr)event->message, event);
221 mac_updatewindow((WindowPtr)event->message);
224 if (HiWord(event->message) != noErr) {
225 SetPt(&pt, 120, 120);
226 DIBadMount(pt, event->message);
232 static void mac_contentclick(WindowPtr window, EventRecord *event) {
235 switch (mac_windowtype(window)) {
237 mac_clickterm(window, event);
240 if (DialogSelect(event, &(DialogPtr)window, &item))
243 /* XXX: Do something */
250 static void mac_growwindow(WindowPtr window, EventRecord *event) {
252 switch (mac_windowtype(window)) {
254 mac_growterm(window, event);
258 static void mac_activatewindow(WindowPtr window, EventRecord *event) {
261 active = (event->modifiers & activeFlag) != 0;
263 switch (mac_windowtype(window)) {
265 mac_activateterm(window, active);
268 mac_activateabout(window, event);
273 static void mac_activateabout(WindowPtr window, EventRecord *event) {
274 DialogItemType itemtype;
280 active = (event->modifiers & activeFlag) != 0;
281 GetDialogItem(window, wiAboutLicence, &itemtype, &itemhandle, &itemrect);
282 HiliteControl((ControlHandle)itemhandle, active ? 0 : 255);
283 DialogSelect(event, &window, &item);
286 static void mac_updatewindow(WindowPtr window) {
288 switch (mac_windowtype(window)) {
290 mac_updateterm(window);
294 UpdateDialog(window, window->visRgn);
304 * Work out what kind of window we're dealing with.
305 * Concept shamelessly nicked from SurfWriter.
307 static int mac_windowtype(WindowPtr window) {
312 kind = ((WindowPeek)window)->windowKind;
315 if (GetWVariant(window) == zoomDocProc)
317 return GetWRefCon(window);
323 static void mac_keypress(EventRecord *event) {
326 window = FrontWindow();
328 * Check for a command-key combination, but ignore it if it counts
329 * as a meta-key combination and we're in a terminal window.
331 if (event->what == keyDown && (event->modifiers & cmdKey) /*&&
332 !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers &&
333 mac_windowtype(window) == wTerminal)*/) {
335 mac_menucommand(MenuKey(event->message & charCodeMask));
337 switch (mac_windowtype(window)) {
339 mac_keyterm(window, event);
345 static void mac_menucommand(long result) {
350 menu = HiWord(result);
351 item = LoWord(result);
352 window = FrontWindow();
353 /* Things which do the same whatever window we're in. */
361 GetMenuItemText(GetMenuHandle(mApple), item, da);
372 mac_closewindow(window);
380 /* If we get here, handling is up to window-specific code. */
381 switch (mac_windowtype(window)) {
383 mac_menuterm(window, menu, item);
390 static void mac_openabout(void) {
391 DialogItemType itemtype;
398 SelectWindow(windows.about);
400 windows.about = GetNewDialog(wAbout, NULL, (WindowPtr)-1);
401 /* XXX check we're using the right resource file? */
402 vers = (VersRecHndl)GetResource('vers', 1);
403 assert(vers != NULL && *vers != NULL);
404 longvers = (*vers)->shortVersion + (*vers)->shortVersion[0] + 1;
405 GetDialogItem(windows.about, wiAboutVersion, &itemtype, &item, &box);
406 assert(itemtype & kStaticTextDialogItem);
407 SetDialogItemText(item, longvers);
408 ShowWindow(windows.about);
412 static void mac_closewindow(WindowPtr window) {
414 switch (mac_windowtype(window)) {
416 CloseDeskAcc(((WindowPeek)window)->windowKind);
419 /* FIXME: end session and stuff */
422 windows.about = NULL;
431 static void mac_zoomwindow(WindowPtr window, short part) {
433 /* FIXME: do something */
437 * Make the menus look right before the user gets to see them.
439 static void mac_adjustmenus(void) {
443 window = FrontWindow();
444 menu = GetMenuHandle(mApple);
446 EnableItem(menu, iAbout);
448 menu = GetMenuHandle(mFile);
450 EnableItem(menu, iNew);
452 EnableItem(menu, iClose);
454 DisableItem(menu, iClose);
455 EnableItem(menu, iQuit);
457 switch (mac_windowtype(window)) {
459 mac_adjusttermmenus(window);
462 menu = GetMenuHandle(mEdit);
463 DisableItem(menu, 0);
470 * Make sure the right cursor's being displayed.
472 static void mac_adjustcursor(RgnHandle cursrgn) {
474 WindowPtr window, front;
478 LocalToGlobal(&mouse);
479 part = FindWindow(mouse, &window);
480 front = FrontWindow();
481 if (part != inContent || window == NULL || window != front) {
482 /* Cursor isn't in the front window, so switch to arrow */
483 SetCursor(&qd.arrow);
484 SetRectRgn(cursrgn, SHRT_MIN, SHRT_MIN, SHRT_MAX, SHRT_MAX);
486 DiffRgn(cursrgn, front->visRgn, cursrgn);
488 switch (mac_windowtype(window)) {
490 mac_adjusttermcursor(window, mouse, cursrgn);
493 SetCursor(&qd.arrow);
494 CopyRgn(window->visRgn, cursrgn);
500 static void mac_shutdown(void) {
505 void fatalbox(char *fmt, ...) {
510 /* We'd like stuff to be a Pascal string */
511 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
513 ParamText(stuff, NULL, NULL, NULL);
518 void modalfatalbox(char *fmt, ...) {
523 /* We'd like stuff to be a Pascal string */
524 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
526 ParamText(stuff, NULL, NULL, NULL);
533 * c-file-style: "simon"