1 /* $Id: mac.c,v 1.1.2.17 1999/03/21 23:23:42 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>
42 #include <ToolUtils.h>
46 #include <stdlib.h> /* putty.h needs size_t */
47 #include <stdio.h> /* for vsprintf */
49 #define PUTTY_DO_GLOBALS
58 struct mac_gestalts mac_gestalts;
60 static void mac_startup(void);
61 static void mac_eventloop(void);
62 static void mac_event(EventRecord *);
63 static void mac_contentclick(WindowPtr, EventRecord *);
64 static void mac_growwindow(WindowPtr, EventRecord *);
65 static void mac_activatewindow(WindowPtr, Boolean);
66 static void mac_updatewindow(WindowPtr);
67 static void mac_keypress(EventRecord *);
68 static int mac_windowtype(WindowPtr);
69 static void mac_menucommand(long);
70 static void mac_adjustcursor(RgnHandle);
71 static void mac_adjustmenus(void);
72 static void mac_closewindow(WindowPtr);
73 static void mac_zoomwindow(WindowPtr, short);
74 static void mac_shutdown(void);
76 static void mac_newsession(void);
79 WindowPtr terminal; /* XXX: Temporary */
84 struct mac_windows windows;
86 int main (int argc, char **argv) {
92 static void mac_startup(void) {
96 InitGraf(&qd.thePort);
97 /* Init Font Manager */
99 /* Init Window Manager */
101 /* Init Menu Manager */
105 /* Init Dialog Manager */
109 /* Find out if we've got Color Quickdraw */
110 if (Gestalt(gestaltQuickdrawVersion, &mac_gestalts.qdvers) != noErr)
111 mac_gestalts.qdvers = gestaltOriginalQD;
112 /* ... and the Appearance Manager? */
113 if (Gestalt(gestaltAppearanceVersion, &mac_gestalts.apprvers) != noErr)
114 if (Gestalt(gestaltAppearanceAttr, NULL) == noErr)
115 mac_gestalts.apprvers = 0x0100;
117 mac_gestalts.apprvers = 0;
118 /* Mac OS 8.5 Control Manager (proportional scrollbars)? */
119 if (Gestalt(gestaltControlMgrAttr, &mac_gestalts.cntlattr) != noErr)
120 mac_gestalts.cntlattr = 0;
122 /* We've been tested with the Appearance Manager */
123 if (mac_gestalts.apprvers != 0)
124 RegisterAppearanceClient();
126 menuBar = GetNewMBar(128);
128 fatalbox("Unable to create menu bar.");
130 AppendResMenu(GetMenuHandle(mApple), 'DRVR');
134 windows.terminal = NULL;
135 windows.about = NULL;
136 windows.licence = NULL;
139 static void mac_eventloop(void) {
147 mac_adjustcursor(cursrgn);
148 gotevent = WaitNextEvent(everyEvent, &event, LONG_MAX, cursrgn);
149 mac_adjustcursor(cursrgn);
156 static void mac_event(EventRecord *event) {
161 switch (event->what) {
163 part = FindWindow(event->where, &window);
167 mac_menucommand(MenuSelect(event->where));
170 SystemClick(event, window);
173 if (window != FrontWindow())
174 /* XXX: check for movable modal dboxes? */
175 SelectWindow(window);
177 mac_contentclick(window, event);
180 if (TrackGoAway(window, event->where))
181 mac_closewindow(window);
184 /* XXX: moveable modal check? */
185 DragWindow(window, event->where, &qd.screenBits.bounds);
188 mac_growwindow(window, event);
192 if (TrackBox(window, event->where, part))
193 mac_zoomwindow(window, part);
202 mac_activatewindow((WindowPtr)event->message,
203 (event->modifiers & activeFlag) != 0);
206 mac_updatewindow((WindowPtr)event->message);
209 if (HiWord(event->message) != noErr) {
210 SetPt(&pt, 120, 120);
211 DIBadMount(pt, event->message);
217 static void mac_contentclick(WindowPtr window, EventRecord *event) {
220 switch (mac_windowtype(window)) {
222 mac_clickterm(window, event);
225 if (DialogSelect(event, &(DialogPtr)window, &item))
228 /* XXX: Do something */
235 static void mac_growwindow(WindowPtr window, EventRecord *event) {
237 switch (mac_windowtype(window)) {
239 mac_growterm(window, event);
243 static void mac_activatewindow(WindowPtr window, Boolean active) {
246 switch (mac_windowtype(window)) {
248 mac_activateterm(window, active);
253 static void mac_updatewindow(WindowPtr window) {
255 switch (mac_windowtype(window)) {
257 mac_updateterm(window);
261 UpdateDialog(window, window->visRgn);
271 * Work out what kind of window we're dealing with.
272 * Concept shamelessly nicked from SurfWriter.
274 static int mac_windowtype(WindowPtr window) {
279 kind = ((WindowPeek)window)->windowKind;
282 if (GetWVariant(window) == zoomDocProc)
284 return GetWRefCon(window);
290 static void mac_keypress(EventRecord *event) {
294 if (event->what == keyDown && (event->modifiers & cmdKey)) {
296 mac_menucommand(MenuKey(event->message & charCodeMask));
298 window = FrontWindow();
299 switch (mac_windowtype(window)) {
301 mac_keyterm(window, event);
307 static void mac_menucommand(long result) {
312 menu = HiWord(result);
313 item = LoWord(result);
314 window = FrontWindow();
315 /* Things which do the same whatever window we're in. */
321 SelectWindow(windows.about);
323 windows.about = GetNewDialog(wAbout, NULL, (WindowPtr)-1);
326 GetMenuItemText(GetMenuHandle(mApple), item, da);
337 mac_closewindow(window);
345 /* If we get here, handling is up to window-specific code. */
346 switch (mac_windowtype(window)) {
348 mac_menuterm(window, menu, item);
355 static void mac_closewindow(WindowPtr window) {
357 switch (mac_windowtype(window)) {
359 CloseDeskAcc(((WindowPeek)window)->windowKind);
362 /* FIXME: end session and stuff */
365 windows.about = NULL;
374 static void mac_zoomwindow(WindowPtr window, short part) {
376 /* FIXME: do something */
380 * Make the menus look right before the user gets to see them.
382 static void mac_adjustmenus(void) {
386 window = FrontWindow();
387 menu = GetMenuHandle(mApple);
389 EnableItem(menu, iAbout);
391 menu = GetMenuHandle(mFile);
393 EnableItem(menu, iNew);
395 EnableItem(menu, iClose);
397 DisableItem(menu, iClose);
398 EnableItem(menu, iQuit);
400 switch (mac_windowtype(window)) {
402 mac_adjusttermmenus(window);
405 menu = GetMenuHandle(mEdit);
406 DisableItem(menu, 0);
413 * Make sure the right cursor's being displayed.
415 static void mac_adjustcursor(RgnHandle cursrgn) {
417 WindowPtr window, front;
421 LocalToGlobal(&mouse);
422 part = FindWindow(mouse, &window);
423 front = FrontWindow();
424 if (part != inContent || window == NULL || window != front) {
425 /* Cursor isn't in the front window, so switch to arrow */
426 SetCursor(&qd.arrow);
427 SetRectRgn(cursrgn, SHRT_MIN, SHRT_MIN, SHRT_MAX, SHRT_MAX);
429 DiffRgn(cursrgn, front->visRgn, cursrgn);
431 switch (mac_windowtype(window)) {
433 mac_adjusttermcursor(window, mouse, cursrgn);
436 SetCursor(&qd.arrow);
437 CopyRgn(window->visRgn, cursrgn);
443 static void mac_shutdown(void) {
448 void fatalbox(const char *fmt, ...) {
453 /* We'd like stuff to be a Pascal string */
454 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
456 ParamText(stuff, NULL, NULL, NULL);
463 * c-file-style: "simon"