1 /* $Id: mac.c,v 1.1.2.14 1999/03/11 21:40:31 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);
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) {
146 gotevent = WaitNextEvent(everyEvent, &event, LONG_MAX, NULL);
153 static void mac_event(EventRecord *event) {
158 switch (event->what) {
160 part = FindWindow(event->where, &window);
164 mac_menucommand(MenuSelect(event->where));
167 SystemClick(event, window);
170 if (window != FrontWindow())
171 /* XXX: check for movable modal dboxes? */
172 SelectWindow(window);
174 mac_contentclick(window, event);
177 if (TrackGoAway(window, event->where))
178 mac_closewindow(window);
181 /* XXX: moveable modal check? */
182 DragWindow(window, event->where, &qd.screenBits.bounds);
185 mac_growwindow(window, event);
189 if (TrackBox(window, event->where, part))
190 mac_zoomwindow(window, part);
199 mac_activatewindow((WindowPtr)event->message,
200 (event->modifiers & activeFlag) != 0);
203 mac_updatewindow((WindowPtr)event->message);
206 if (HiWord(event->message) != noErr) {
207 SetPt(&pt, 120, 120);
208 DIBadMount(pt, event->message);
214 static void mac_contentclick(WindowPtr window, EventRecord *event) {
217 switch (mac_windowtype(window)) {
219 mac_clickterm(window, event);
222 if (DialogSelect(event, &(DialogPtr)window, &item))
225 /* XXX: Do something */
232 static void mac_growwindow(WindowPtr window, EventRecord *event) {
234 switch (mac_windowtype(window)) {
236 mac_growterm(window, event);
240 static void mac_activatewindow(WindowPtr window, Boolean active) {
243 switch (mac_windowtype(window)) {
245 mac_activateterm(window, active);
250 static void mac_updatewindow(WindowPtr window) {
252 switch (mac_windowtype(window)) {
254 mac_updateterm(window);
258 UpdateDialog(window, window->visRgn);
268 * Work out what kind of window we're dealing with.
269 * Concept shamelessly nicked from SurfWriter.
271 static int mac_windowtype(WindowPtr window) {
276 kind = ((WindowPeek)window)->windowKind;
279 if (GetWVariant(window) == zoomDocProc)
281 return GetWRefCon(window);
287 static void mac_keypress(EventRecord *event) {
291 if (event->what == keyDown && (event->modifiers & cmdKey)) {
293 mac_menucommand(MenuKey(event->message & charCodeMask));
295 window = FrontWindow();
296 switch (mac_windowtype(window)) {
298 mac_keyterm(window, event);
304 static void mac_menucommand(long result) {
308 menu = HiWord(result);
309 item = LoWord(result);
314 GetNewDialog(wAbout, NULL, (GrafPort *)-1);
317 GetMenuItemText(GetMenuHandle(mApple), item, da);
328 mac_closewindow(FrontWindow());
339 static void mac_closewindow(WindowPtr window) {
341 switch (mac_windowtype(window)) {
343 CloseDeskAcc(((WindowPeek)window)->windowKind);
346 /* FIXME: end session and stuff */
354 static void mac_zoomwindow(WindowPtr window, short part) {
356 /* FIXME: do something */
360 * Make the menus look right before the user gets to see them.
362 static void mac_adjustmenus(void) {
366 window = FrontWindow();
367 menu = GetMenuHandle(mApple);
369 EnableItem(menu, iAbout);
371 menu = GetMenuHandle(mFile);
373 EnableItem(menu, iNew);
374 EnableItem(menu, iQuit);
376 switch (mac_windowtype(window)) {
378 mac_adjusttermmenus(window);
381 menu = GetMenuHandle(mEdit);
382 DisableItem(menu, 0);
389 * Make sure the right cursor's being displayed.
391 static void mac_adjustcursor(void) {
393 SetCursor(&qd.arrow);
396 void write_clip(void *data, int len) {
398 /* XXX: do something */
401 void get_clip(void **p, int *lenp) {
403 /* XXX: do something */
406 static void mac_shutdown(void) {
411 void fatalbox(const char *fmt, ...) {
416 /* We'd like stuff to be a Pascal string */
417 stuff[0] = vsprintf((char *)(&stuff[1]), fmt, ap);
419 ParamText(stuff, NULL, NULL, NULL);
426 * c-file-style: "simon"