1 /* $Id: mac.c,v 1.1.2.10 1999/03/01 22:26:49 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_activatewindow(WindowPtr, Boolean);
64 static void mac_updatewindow(WindowPtr);
65 static void mac_keypress(EventRecord *);
66 static int mac_windowtype(WindowPtr);
67 static void mac_menucommand(long);
68 static void mac_adjustcursor(void);
69 static void mac_adjustmenus(void);
70 static void mac_closewindow(WindowPtr);
71 static void mac_zoomwindow(WindowPtr, short);
72 static void mac_shutdown(void);
74 static void mac_newsession(void);
76 int main (int argc, char **argv) {
82 static void mac_startup(void) {
86 InitGraf(&qd.thePort);
87 /* Init Font Manager */
89 /* Init Window Manager */
91 /* Init Menu Manager */
95 /* Init Dialog Manager */
99 /* Find out if we've got Color Quickdraw */
100 if (Gestalt(gestaltQuickdrawVersion, &mac_gestalts.qdvers) != noErr)
101 mac_gestalts.qdvers = gestaltOriginalQD;
102 /* ... and the Appearance Manager? */
103 if (Gestalt(gestaltAppearanceVersion, &mac_gestalts.apprvers) != noErr)
104 if (Gestalt(gestaltAppearanceAttr, NULL) == noErr)
105 mac_gestalts.apprvers = 0x0100;
107 mac_gestalts.apprvers = 0;
108 /* Mac OS 8.5 Control Manager (proportional scrollbars)? */
109 if (Gestalt(gestaltControlMgrAttr, &mac_gestalts.cntlattr) != noErr)
110 mac_gestalts.cntlattr = 0;
112 /* We've been tested with the Appearance Manager */
113 if (mac_gestalts.apprvers != 0)
114 RegisterAppearanceClient();
116 menuBar = GetNewMBar(128);
118 fatalbox("Unable to create menu bar.");
120 AppendResMenu(GetMenuHandle(mApple), 'DRVR');
126 static void mac_eventloop(void) {
133 gotevent = WaitNextEvent(everyEvent, &event, LONG_MAX, NULL);
140 static void mac_event(EventRecord *event) {
145 switch (event->what) {
147 part = FindWindow(event->where, &window);
151 mac_menucommand(MenuSelect(event->where));
154 SystemClick(event, window);
157 if (window != FrontWindow())
158 /* XXX: check for movable modal dboxes? */
159 SelectWindow(window);
161 mac_contentclick(window, event);
164 if (TrackGoAway(window, event->where))
165 mac_closewindow(window);
168 /* XXX: moveable modal check? */
169 DragWindow(window, event->where, &qd.screenBits.bounds);
175 if (TrackBox(window, event->where, part))
176 mac_zoomwindow(window, part);
185 mac_activatewindow((WindowPtr)event->message,
186 (event->modifiers & activeFlag) != 0);
189 mac_updatewindow((WindowPtr)event->message);
192 if (HiWord(event->message) != noErr) {
193 SetPt(&pt, 120, 120);
194 DIBadMount(pt, event->message);
200 static void mac_contentclick(WindowPtr window, EventRecord *event) {
203 switch (mac_windowtype(window)) {
205 mac_clickterm(window, event);
208 if (DialogSelect(event, &(DialogPtr)window, &item))
211 /* XXX: Do something */
218 static void mac_activatewindow(WindowPtr window, Boolean active) {
220 switch (mac_windowtype(window)) {
222 mac_activateterm(window, active);
227 static void mac_updatewindow(WindowPtr window) {
229 switch (mac_windowtype(window)) {
231 mac_updateterm(window);
235 UpdateDialog(window, window->visRgn);
245 * Work out what kind of window we're dealing with.
246 * Concept shamelessly nicked from SurfWriter.
248 static int mac_windowtype(WindowPtr window) {
253 kind = ((WindowPeek)window)->windowKind;
256 if (GetWVariant(window) == zoomDocProc)
258 return GetWRefCon(window);
264 static void mac_keypress(EventRecord *event) {
267 if (event->what == keyDown && (event->modifiers & cmdKey)) {
269 mac_menucommand(MenuKey(event->message & charCodeMask));
273 static void mac_menucommand(long result) {
277 menu = HiWord(result);
278 item = LoWord(result);
283 GetNewDialog(wAbout, NULL, (GrafPort *)-1);
286 GetMenuItemText(GetMenuHandle(mApple), item, da);
297 mac_closewindow(FrontWindow());
308 static void mac_closewindow(WindowPtr window) {
310 switch (mac_windowtype(window)) {
312 CloseDeskAcc(((WindowPeek)window)->windowKind);
315 /* FIXME: end session and stuff */
323 static void mac_zoomwindow(WindowPtr window, short part) {
325 /* FIXME: do something */
329 * Make the menus look right before the user gets to see them.
331 static void mac_adjustmenus(void) {
336 * Make sure the right cursor's being displayed.
338 static void mac_adjustcursor(void) {
340 SetCursor(&qd.arrow);
343 static void mac_shutdown(void) {
348 void fatalbox(const char *fmt, ...) {
353 /* We'd like stuff to be a Pascal string */
354 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
356 ParamText(stuff, NULL, NULL, NULL);