1 /* $Id: mac.c,v 1.1.2.12 1999/03/07 23:20:20 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(void);
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);
78 int main (int argc, char **argv) {
84 static void mac_startup(void) {
88 InitGraf(&qd.thePort);
89 /* Init Font Manager */
91 /* Init Window Manager */
93 /* Init Menu Manager */
97 /* Init Dialog Manager */
101 /* Find out if we've got Color Quickdraw */
102 if (Gestalt(gestaltQuickdrawVersion, &mac_gestalts.qdvers) != noErr)
103 mac_gestalts.qdvers = gestaltOriginalQD;
104 /* ... and the Appearance Manager? */
105 if (Gestalt(gestaltAppearanceVersion, &mac_gestalts.apprvers) != noErr)
106 if (Gestalt(gestaltAppearanceAttr, NULL) == noErr)
107 mac_gestalts.apprvers = 0x0100;
109 mac_gestalts.apprvers = 0;
110 /* Mac OS 8.5 Control Manager (proportional scrollbars)? */
111 if (Gestalt(gestaltControlMgrAttr, &mac_gestalts.cntlattr) != noErr)
112 mac_gestalts.cntlattr = 0;
114 /* We've been tested with the Appearance Manager */
115 if (mac_gestalts.apprvers != 0)
116 RegisterAppearanceClient();
118 menuBar = GetNewMBar(128);
120 fatalbox("Unable to create menu bar.");
122 AppendResMenu(GetMenuHandle(mApple), 'DRVR');
128 static void mac_eventloop(void) {
135 gotevent = WaitNextEvent(everyEvent, &event, LONG_MAX, NULL);
142 static void mac_event(EventRecord *event) {
147 switch (event->what) {
149 part = FindWindow(event->where, &window);
153 mac_menucommand(MenuSelect(event->where));
156 SystemClick(event, window);
159 if (window != FrontWindow())
160 /* XXX: check for movable modal dboxes? */
161 SelectWindow(window);
163 mac_contentclick(window, event);
166 if (TrackGoAway(window, event->where))
167 mac_closewindow(window);
170 /* XXX: moveable modal check? */
171 DragWindow(window, event->where, &qd.screenBits.bounds);
174 mac_growwindow(window, event);
178 if (TrackBox(window, event->where, part))
179 mac_zoomwindow(window, part);
188 mac_activatewindow((WindowPtr)event->message,
189 (event->modifiers & activeFlag) != 0);
192 mac_updatewindow((WindowPtr)event->message);
195 if (HiWord(event->message) != noErr) {
196 SetPt(&pt, 120, 120);
197 DIBadMount(pt, event->message);
203 static void mac_contentclick(WindowPtr window, EventRecord *event) {
206 switch (mac_windowtype(window)) {
208 mac_clickterm(window, event);
211 if (DialogSelect(event, &(DialogPtr)window, &item))
214 /* XXX: Do something */
221 static void mac_growwindow(WindowPtr window, EventRecord *event) {
223 switch (mac_windowtype(window)) {
225 mac_growterm(window, event);
229 static void mac_activatewindow(WindowPtr window, Boolean active) {
231 switch (mac_windowtype(window)) {
233 mac_activateterm(window, active);
238 static void mac_updatewindow(WindowPtr window) {
240 switch (mac_windowtype(window)) {
242 mac_updateterm(window);
246 UpdateDialog(window, window->visRgn);
256 * Work out what kind of window we're dealing with.
257 * Concept shamelessly nicked from SurfWriter.
259 static int mac_windowtype(WindowPtr window) {
264 kind = ((WindowPeek)window)->windowKind;
267 if (GetWVariant(window) == zoomDocProc)
269 return GetWRefCon(window);
275 static void mac_keypress(EventRecord *event) {
279 if (event->what == keyDown && (event->modifiers & cmdKey)) {
281 mac_menucommand(MenuKey(event->message & charCodeMask));
283 window = FrontWindow();
284 switch (mac_windowtype(window)) {
286 mac_keyterm(window, event);
292 static void mac_menucommand(long result) {
296 menu = HiWord(result);
297 item = LoWord(result);
302 GetNewDialog(wAbout, NULL, (GrafPort *)-1);
305 GetMenuItemText(GetMenuHandle(mApple), item, da);
316 mac_closewindow(FrontWindow());
327 static void mac_closewindow(WindowPtr window) {
329 switch (mac_windowtype(window)) {
331 CloseDeskAcc(((WindowPeek)window)->windowKind);
334 /* FIXME: end session and stuff */
342 static void mac_zoomwindow(WindowPtr window, short part) {
344 /* FIXME: do something */
348 * Make the menus look right before the user gets to see them.
350 static void mac_adjustmenus(void) {
355 * Make sure the right cursor's being displayed.
357 static void mac_adjustcursor(void) {
359 SetCursor(&qd.arrow);
362 static void mac_shutdown(void) {
367 void fatalbox(const char *fmt, ...) {
372 /* We'd like stuff to be a Pascal string */
373 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
375 ParamText(stuff, NULL, NULL, NULL);