1 /* $Id: mac.c,v 1.1.2.11 1999/03/03 22:03:54 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 */
48 #define PUTTY_DO_GLOBALS
57 struct mac_gestalts mac_gestalts;
59 static void mac_startup(void);
60 static void mac_eventloop(void);
61 static void mac_event(EventRecord *);
62 static void mac_contentclick(WindowPtr, EventRecord *);
63 static void mac_growwindow(WindowPtr, EventRecord *);
64 static void mac_activatewindow(WindowPtr, Boolean);
65 static void mac_updatewindow(WindowPtr);
66 static void mac_keypress(EventRecord *);
67 static int mac_windowtype(WindowPtr);
68 static void mac_menucommand(long);
69 static void mac_adjustcursor(void);
70 static void mac_adjustmenus(void);
71 static void mac_closewindow(WindowPtr);
72 static void mac_zoomwindow(WindowPtr, short);
73 static void mac_shutdown(void);
75 static void mac_newsession(void);
77 int main (int argc, char **argv) {
83 static void mac_startup(void) {
87 InitGraf(&qd.thePort);
88 /* Init Font Manager */
90 /* Init Window Manager */
92 /* Init Menu Manager */
96 /* Init Dialog Manager */
100 /* Find out if we've got Color Quickdraw */
101 if (Gestalt(gestaltQuickdrawVersion, &mac_gestalts.qdvers) != noErr)
102 mac_gestalts.qdvers = gestaltOriginalQD;
103 /* ... and the Appearance Manager? */
104 if (Gestalt(gestaltAppearanceVersion, &mac_gestalts.apprvers) != noErr)
105 if (Gestalt(gestaltAppearanceAttr, NULL) == noErr)
106 mac_gestalts.apprvers = 0x0100;
108 mac_gestalts.apprvers = 0;
109 /* Mac OS 8.5 Control Manager (proportional scrollbars)? */
110 if (Gestalt(gestaltControlMgrAttr, &mac_gestalts.cntlattr) != noErr)
111 mac_gestalts.cntlattr = 0;
113 /* We've been tested with the Appearance Manager */
114 if (mac_gestalts.apprvers != 0)
115 RegisterAppearanceClient();
117 menuBar = GetNewMBar(128);
119 fatalbox("Unable to create menu bar.");
121 AppendResMenu(GetMenuHandle(mApple), 'DRVR');
127 static void mac_eventloop(void) {
134 gotevent = WaitNextEvent(everyEvent, &event, LONG_MAX, NULL);
141 static void mac_event(EventRecord *event) {
146 switch (event->what) {
148 part = FindWindow(event->where, &window);
152 mac_menucommand(MenuSelect(event->where));
155 SystemClick(event, window);
158 if (window != FrontWindow())
159 /* XXX: check for movable modal dboxes? */
160 SelectWindow(window);
162 mac_contentclick(window, event);
165 if (TrackGoAway(window, event->where))
166 mac_closewindow(window);
169 /* XXX: moveable modal check? */
170 DragWindow(window, event->where, &qd.screenBits.bounds);
173 mac_growwindow(window, event);
177 if (TrackBox(window, event->where, part))
178 mac_zoomwindow(window, part);
187 mac_activatewindow((WindowPtr)event->message,
188 (event->modifiers & activeFlag) != 0);
191 mac_updatewindow((WindowPtr)event->message);
194 if (HiWord(event->message) != noErr) {
195 SetPt(&pt, 120, 120);
196 DIBadMount(pt, event->message);
202 static void mac_contentclick(WindowPtr window, EventRecord *event) {
205 switch (mac_windowtype(window)) {
207 mac_clickterm(window, event);
210 if (DialogSelect(event, &(DialogPtr)window, &item))
213 /* XXX: Do something */
220 static void mac_growwindow(WindowPtr window, EventRecord *event) {
222 switch (mac_windowtype(window)) {
224 mac_growterm(window, event);
228 static void mac_activatewindow(WindowPtr window, Boolean active) {
230 switch (mac_windowtype(window)) {
232 mac_activateterm(window, active);
237 static void mac_updatewindow(WindowPtr window) {
239 switch (mac_windowtype(window)) {
241 mac_updateterm(window);
245 UpdateDialog(window, window->visRgn);
255 * Work out what kind of window we're dealing with.
256 * Concept shamelessly nicked from SurfWriter.
258 static int mac_windowtype(WindowPtr window) {
263 kind = ((WindowPeek)window)->windowKind;
266 if (GetWVariant(window) == zoomDocProc)
268 return GetWRefCon(window);
274 static void mac_keypress(EventRecord *event) {
277 if (event->what == keyDown && (event->modifiers & cmdKey)) {
279 mac_menucommand(MenuKey(event->message & charCodeMask));
283 static void mac_menucommand(long result) {
287 menu = HiWord(result);
288 item = LoWord(result);
293 GetNewDialog(wAbout, NULL, (GrafPort *)-1);
296 GetMenuItemText(GetMenuHandle(mApple), item, da);
307 mac_closewindow(FrontWindow());
318 static void mac_closewindow(WindowPtr window) {
320 switch (mac_windowtype(window)) {
322 CloseDeskAcc(((WindowPeek)window)->windowKind);
325 /* FIXME: end session and stuff */
333 static void mac_zoomwindow(WindowPtr window, short part) {
335 /* FIXME: do something */
339 * Make the menus look right before the user gets to see them.
341 static void mac_adjustmenus(void) {
346 * Make sure the right cursor's being displayed.
348 static void mac_adjustcursor(void) {
350 SetCursor(&qd.arrow);
353 static void mac_shutdown(void) {
358 void fatalbox(const char *fmt, ...) {
363 /* We'd like stuff to be a Pascal string */
364 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
366 ParamText(stuff, NULL, NULL, NULL);