1 /* $Id: mac.c,v 1.1.2.26 1999/08/02 22:32:38 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) {
102 InitGraf(&qd.thePort);
103 /* Init Font Manager */
105 /* Init Window Manager */
107 /* Init Menu Manager */
111 /* Init Dialog Manager */
115 /* Find out if we've got Color Quickdraw */
116 if (Gestalt(gestaltQuickdrawVersion, &mac_gestalts.qdvers) != noErr)
117 mac_gestalts.qdvers = gestaltOriginalQD;
118 /* ... and the Appearance Manager? */
119 if (Gestalt(gestaltAppearanceVersion, &mac_gestalts.apprvers) != noErr)
120 if (Gestalt(gestaltAppearanceAttr, NULL) == noErr)
121 mac_gestalts.apprvers = 0x0100;
123 mac_gestalts.apprvers = 0;
124 /* Paranoia: Did we manage to pull in AppearanceLib? */
125 if (&RegisterAppearanceClient == kUnresolvedCFragSymbolAddress)
126 mac_gestalts.apprvers = 0;
127 /* Mac OS 8.5 Control Manager (proportional scrollbars)? */
128 if (Gestalt(gestaltControlMgrAttr, &mac_gestalts.cntlattr) != noErr)
129 mac_gestalts.cntlattr = 0;
130 /* Mac OS 8.5 Window Manager? */
131 if (Gestalt(gestaltWindowMgrAttr, &mac_gestalts.windattr) != noErr)
132 mac_gestalts.windattr = 0;
134 /* We've been tested with the Appearance Manager */
135 if (mac_gestalts.apprvers != 0)
136 RegisterAppearanceClient();
138 menuBar = GetNewMBar(128);
140 fatalbox("Unable to create menu bar.");
142 AppendResMenu(GetMenuHandle(mApple), 'DRVR');
146 windows.about = NULL;
147 windows.licence = NULL;
149 /* Initialise networking */
150 #ifdef WITH_OPENTRANSPORT
151 if ((*otpt_stack.init)() == 0)
152 net_stack = &otpt_stack;
156 if ((*mtcp_stack.init)() == 0)
157 net_stack = &mtcp_stack;
160 fatalbox("No useful TCP/IP stack found");
166 static void mac_eventloop(void) {
173 mac_adjustcursor(cursrgn);
174 gotevent = WaitNextEvent(everyEvent, &event, LONG_MAX, cursrgn);
175 mac_adjustcursor(cursrgn);
183 static void mac_event(EventRecord *event) {
188 switch (event->what) {
190 part = FindWindow(event->where, &window);
194 mac_menucommand(MenuSelect(event->where));
197 SystemClick(event, window);
200 if (window != FrontWindow())
201 /* XXX: check for movable modal dboxes? */
202 SelectWindow(window);
204 mac_contentclick(window, event);
207 if (TrackGoAway(window, event->where))
208 mac_closewindow(window);
211 /* XXX: moveable modal check? */
212 DragWindow(window, event->where, &qd.screenBits.bounds);
215 mac_growwindow(window, event);
219 if (TrackBox(window, event->where, part))
220 mac_zoomwindow(window, part);
229 mac_activatewindow((WindowPtr)event->message, event);
232 mac_updatewindow((WindowPtr)event->message);
235 if (HiWord(event->message) != noErr) {
236 SetPt(&pt, 120, 120);
237 DIBadMount(pt, event->message);
243 static void mac_contentclick(WindowPtr window, EventRecord *event) {
246 switch (mac_windowtype(window)) {
248 mac_clickterm(window, event);
251 if (DialogSelect(event, &(DialogPtr)window, &item))
254 /* XXX: Do something */
261 static void mac_growwindow(WindowPtr window, EventRecord *event) {
263 switch (mac_windowtype(window)) {
265 mac_growterm(window, event);
269 static void mac_activatewindow(WindowPtr window, EventRecord *event) {
272 active = (event->modifiers & activeFlag) != 0;
274 switch (mac_windowtype(window)) {
276 mac_activateterm(window, active);
279 mac_activateabout(window, event);
284 static void mac_activateabout(WindowPtr window, EventRecord *event) {
285 DialogItemType itemtype;
291 active = (event->modifiers & activeFlag) != 0;
292 GetDialogItem(window, wiAboutLicence, &itemtype, &itemhandle, &itemrect);
293 HiliteControl((ControlHandle)itemhandle, active ? 0 : 255);
294 DialogSelect(event, &window, &item);
297 static void mac_updatewindow(WindowPtr window) {
299 switch (mac_windowtype(window)) {
301 mac_updateterm(window);
305 UpdateDialog(window, window->visRgn);
315 * Work out what kind of window we're dealing with.
316 * Concept shamelessly nicked from SurfWriter.
318 static int mac_windowtype(WindowPtr window) {
323 kind = ((WindowPeek)window)->windowKind;
326 if (GetWVariant(window) == zoomDocProc)
328 return GetWRefCon(window);
334 static void mac_keypress(EventRecord *event) {
337 window = FrontWindow();
339 * Check for a command-key combination, but ignore it if it counts
340 * as a meta-key combination and we're in a terminal window.
342 if (event->what == keyDown && (event->modifiers & cmdKey) /*&&
343 !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers &&
344 mac_windowtype(window) == wTerminal)*/) {
346 mac_menucommand(MenuKey(event->message & charCodeMask));
348 switch (mac_windowtype(window)) {
350 mac_keyterm(window, event);
356 static void mac_menucommand(long result) {
361 menu = HiWord(result);
362 item = LoWord(result);
363 window = FrontWindow();
364 /* Things which do the same whatever window we're in. */
372 GetMenuItemText(GetMenuHandle(mApple), item, da);
383 mac_closewindow(window);
391 /* If we get here, handling is up to window-specific code. */
392 switch (mac_windowtype(window)) {
394 mac_menuterm(window, menu, item);
401 static void mac_openabout(void) {
402 DialogItemType itemtype;
409 SelectWindow(windows.about);
411 windows.about = GetNewDialog(wAbout, NULL, (WindowPtr)-1);
412 /* XXX check we're using the right resource file? */
413 vers = (VersRecHndl)GetResource('vers', 1);
414 assert(vers != NULL && *vers != NULL);
415 longvers = (*vers)->shortVersion + (*vers)->shortVersion[0] + 1;
416 GetDialogItem(windows.about, wiAboutVersion, &itemtype, &item, &box);
417 assert(itemtype & kStaticTextDialogItem);
418 SetDialogItemText(item, longvers);
419 ShowWindow(windows.about);
423 static void mac_closewindow(WindowPtr window) {
425 switch (mac_windowtype(window)) {
427 CloseDeskAcc(((WindowPeek)window)->windowKind);
430 /* FIXME: end session and stuff */
433 windows.about = NULL;
442 static void mac_zoomwindow(WindowPtr window, short part) {
444 /* FIXME: do something */
448 * Make the menus look right before the user gets to see them.
450 static void mac_adjustmenus(void) {
454 window = FrontWindow();
455 menu = GetMenuHandle(mApple);
457 EnableItem(menu, iAbout);
459 menu = GetMenuHandle(mFile);
461 EnableItem(menu, iNew);
463 EnableItem(menu, iClose);
465 DisableItem(menu, iClose);
466 EnableItem(menu, iQuit);
468 switch (mac_windowtype(window)) {
470 mac_adjusttermmenus(window);
473 menu = GetMenuHandle(mEdit);
474 DisableItem(menu, 0);
481 * Make sure the right cursor's being displayed.
483 static void mac_adjustcursor(RgnHandle cursrgn) {
485 WindowPtr window, front;
489 LocalToGlobal(&mouse);
490 part = FindWindow(mouse, &window);
491 front = FrontWindow();
492 if (part != inContent || window == NULL || window != front) {
493 /* Cursor isn't in the front window, so switch to arrow */
494 SetCursor(&qd.arrow);
495 SetRectRgn(cursrgn, SHRT_MIN, SHRT_MIN, SHRT_MAX, SHRT_MAX);
497 DiffRgn(cursrgn, front->visRgn, cursrgn);
499 switch (mac_windowtype(window)) {
501 mac_adjusttermcursor(window, mouse, cursrgn);
504 SetCursor(&qd.arrow);
505 CopyRgn(window->visRgn, cursrgn);
511 static void mac_shutdown(void) {
517 void fatalbox(const char *fmt, ...) {
522 /* We'd like stuff to be a Pascal string */
523 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
525 ParamText(stuff, NULL, NULL, NULL);
532 * c-file-style: "simon"