1 /* $Id: mac.c,v 1.3 2002/11/23 18:22:47 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>
38 #include <CodeFragments.h>
43 #include <Resources.h>
44 #include <ToolUtils.h>
49 #include <stdlib.h> /* putty.h needs size_t */
50 #include <stdio.h> /* for vsprintf */
52 #define PUTTY_DO_GLOBALS
61 struct mac_gestalts mac_gestalts
;
63 static void mac_startup(void);
64 static void mac_eventloop(void);
65 #pragma noreturn (mac_eventloop)
66 static void mac_event(EventRecord
*);
67 static void mac_contentclick(WindowPtr
, EventRecord
*);
68 static void mac_growwindow(WindowPtr
, EventRecord
*);
69 static void mac_activatewindow(WindowPtr
, EventRecord
*);
70 static void mac_activateabout(WindowPtr
, EventRecord
*);
71 static void mac_updatewindow(WindowPtr
);
72 static void mac_keypress(EventRecord
*);
73 static int mac_windowtype(WindowPtr
);
74 static void mac_menucommand(long);
75 static void mac_openabout(void);
76 static void mac_adjustcursor(RgnHandle
);
77 static void mac_adjustmenus(void);
78 static void mac_closewindow(WindowPtr
);
79 static void mac_zoomwindow(WindowPtr
, short);
80 static void mac_shutdown(void);
81 #pragma noreturn (mac_shutdown)
88 struct mac_windows windows
;
90 int main (int argc
, char **argv
) {
96 #pragma noreturn (main)
98 static void mac_startup(void) {
101 /* Init Memory Manager */
104 InitGraf(&qd
.thePort
);
105 /* Init Font Manager */
107 /* Init Window Manager */
109 /* Init Menu Manager */
113 /* Init Dialog Manager */
117 /* Find out if we've got Color Quickdraw */
118 if (Gestalt(gestaltQuickdrawVersion
, &mac_gestalts
.qdvers
) != noErr
)
119 mac_gestalts
.qdvers
= gestaltOriginalQD
;
120 /* ... and the Appearance Manager? */
121 if (Gestalt(gestaltAppearanceVersion
, &mac_gestalts
.apprvers
) != noErr
)
122 if (Gestalt(gestaltAppearanceAttr
, NULL
) == noErr
)
123 mac_gestalts
.apprvers
= 0x0100;
125 mac_gestalts
.apprvers
= 0;
126 #if TARGET_RT_MAC_CFM
127 /* Paranoia: Did we manage to pull in AppearanceLib? */
128 if (&RegisterAppearanceClient
== kUnresolvedCFragSymbolAddress
)
129 mac_gestalts
.apprvers
= 0;
131 /* Mac OS 8.5 Control Manager (proportional scrollbars)? */
132 if (Gestalt(gestaltControlMgrAttr
, &mac_gestalts
.cntlattr
) != noErr
)
133 mac_gestalts
.cntlattr
= 0;
134 /* Mac OS 8.5 Window Manager? */
135 if (Gestalt(gestaltWindowMgrAttr
, &mac_gestalts
.windattr
) != noErr
)
136 mac_gestalts
.windattr
= 0;
138 /* We've been tested with the Appearance Manager */
139 if (mac_gestalts
.apprvers
!= 0)
140 RegisterAppearanceClient();
142 menuBar
= GetNewMBar(128);
144 fatalbox("Unable to create menu bar.");
146 AppendResMenu(GetMenuHandle(mApple
), 'DRVR');
150 windows
.about
= NULL
;
151 windows
.licence
= NULL
;
156 static void mac_eventloop(void) {
163 mac_adjustcursor(cursrgn
);
164 gotevent
= WaitNextEvent(everyEvent
, &event
, LONG_MAX
, cursrgn
);
165 mac_adjustcursor(cursrgn
);
172 static void mac_event(EventRecord
*event
) {
177 switch (event
->what
) {
179 part
= FindWindow(event
->where
, &window
);
183 mac_menucommand(MenuSelect(event
->where
));
186 SystemClick(event
, window
);
189 if (window
!= FrontWindow())
190 /* XXX: check for movable modal dboxes? */
191 SelectWindow(window
);
193 mac_contentclick(window
, event
);
196 if (TrackGoAway(window
, event
->where
))
197 mac_closewindow(window
);
200 /* XXX: moveable modal check? */
201 DragWindow(window
, event
->where
, &qd
.screenBits
.bounds
);
204 mac_growwindow(window
, event
);
208 if (TrackBox(window
, event
->where
, part
))
209 mac_zoomwindow(window
, part
);
218 mac_activatewindow((WindowPtr
)event
->message
, event
);
221 mac_updatewindow((WindowPtr
)event
->message
);
224 if (HiWord(event
->message
) != noErr
) {
225 SetPt(&pt
, 120, 120);
226 DIBadMount(pt
, event
->message
);
232 static void mac_contentclick(WindowPtr window
, EventRecord
*event
) {
235 switch (mac_windowtype(window
)) {
237 mac_clickterm(window
, event
);
240 if (DialogSelect(event
, &(DialogPtr
)window
, &item
))
243 /* XXX: Do something */
250 static void mac_growwindow(WindowPtr window
, EventRecord
*event
) {
252 switch (mac_windowtype(window
)) {
254 mac_growterm(window
, event
);
258 static void mac_activatewindow(WindowPtr window
, EventRecord
*event
) {
261 active
= (event
->modifiers
& activeFlag
) != 0;
263 switch (mac_windowtype(window
)) {
265 mac_activateterm(window
, active
);
268 mac_activateabout(window
, event
);
273 static void mac_activateabout(WindowPtr window
, EventRecord
*event
) {
274 DialogItemType itemtype
;
280 active
= (event
->modifiers
& activeFlag
) != 0;
281 GetDialogItem(window
, wiAboutLicence
, &itemtype
, &itemhandle
, &itemrect
);
282 HiliteControl((ControlHandle
)itemhandle
, active ?
0 : 255);
283 DialogSelect(event
, &window
, &item
);
286 static void mac_updatewindow(WindowPtr window
) {
288 switch (mac_windowtype(window
)) {
290 mac_updateterm(window
);
294 UpdateDialog(window
, window
->visRgn
);
304 * Work out what kind of window we're dealing with.
305 * Concept shamelessly nicked from SurfWriter.
307 static int mac_windowtype(WindowPtr window
) {
312 kind
= ((WindowPeek
)window
)->windowKind
;
315 if (GetWVariant(window
) == zoomDocProc
)
317 return GetWRefCon(window
);
323 static void mac_keypress(EventRecord
*event
) {
326 window
= FrontWindow();
328 * Check for a command-key combination, but ignore it if it counts
329 * as a meta-key combination and we're in a terminal window.
331 if (event
->what
== keyDown
&& (event
->modifiers
& cmdKey
) /*&&
332 !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers &&
333 mac_windowtype(window) == wTerminal)*/) {
335 mac_menucommand(MenuKey(event
->message
& charCodeMask
));
337 switch (mac_windowtype(window
)) {
339 mac_keyterm(window
, event
);
345 static void mac_menucommand(long result
) {
350 menu
= HiWord(result
);
351 item
= LoWord(result
);
352 window
= FrontWindow();
353 /* Things which do the same whatever window we're in. */
361 GetMenuItemText(GetMenuHandle(mApple
), item
, da
);
372 mac_closewindow(window
);
380 /* If we get here, handling is up to window-specific code. */
381 switch (mac_windowtype(window
)) {
383 mac_menuterm(window
, menu
, item
);
390 static void mac_openabout(void) {
391 DialogItemType itemtype
;
398 SelectWindow(windows
.about
);
400 windows
.about
= GetNewDialog(wAbout
, NULL
, (WindowPtr
)-1);
401 vers
= (VersRecHndl
)Get1Resource('vers', 1);
402 if (vers
!= NULL
&& *vers
!= NULL
) {
403 longvers
= (*vers
)->shortVersion
+ (*vers
)->shortVersion
[0] + 1;
404 GetDialogItem(windows
.about
, wiAboutVersion
,
405 &itemtype
, &item
, &box
);
406 assert(itemtype
& kStaticTextDialogItem
);
407 SetDialogItemText(item
, longvers
);
409 ShowWindow(windows
.about
);
413 static void mac_closewindow(WindowPtr window
) {
415 switch (mac_windowtype(window
)) {
417 CloseDeskAcc(((WindowPeek
)window
)->windowKind
);
420 /* FIXME: end session and stuff */
423 windows
.about
= NULL
;
432 static void mac_zoomwindow(WindowPtr window
, short part
) {
434 /* FIXME: do something */
438 * Make the menus look right before the user gets to see them.
440 static void mac_adjustmenus(void) {
444 window
= FrontWindow();
445 menu
= GetMenuHandle(mApple
);
447 EnableItem(menu
, iAbout
);
449 menu
= GetMenuHandle(mFile
);
451 EnableItem(menu
, iNew
);
453 EnableItem(menu
, iClose
);
455 DisableItem(menu
, iClose
);
456 EnableItem(menu
, iQuit
);
458 switch (mac_windowtype(window
)) {
460 mac_adjusttermmenus(window
);
463 menu
= GetMenuHandle(mEdit
);
464 DisableItem(menu
, 0);
471 * Make sure the right cursor's being displayed.
473 static void mac_adjustcursor(RgnHandle cursrgn
) {
475 WindowPtr window
, front
;
479 LocalToGlobal(&mouse
);
480 part
= FindWindow(mouse
, &window
);
481 front
= FrontWindow();
482 if (part
!= inContent
|| window
== NULL
|| window
!= front
) {
483 /* Cursor isn't in the front window, so switch to arrow */
484 SetCursor(&qd
.arrow
);
485 SetRectRgn(cursrgn
, SHRT_MIN
, SHRT_MIN
, SHRT_MAX
, SHRT_MAX
);
487 DiffRgn(cursrgn
, front
->visRgn
, cursrgn
);
489 switch (mac_windowtype(window
)) {
491 mac_adjusttermcursor(window
, mouse
, cursrgn
);
494 SetCursor(&qd
.arrow
);
495 CopyRgn(window
->visRgn
, cursrgn
);
501 static void mac_shutdown(void) {
506 void fatalbox(char *fmt
, ...) {
511 /* We'd like stuff to be a Pascal string */
512 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
514 ParamText(stuff
, NULL
, NULL
, NULL
);
519 void modalfatalbox(char *fmt
, ...) {
524 /* We'd like stuff to be a Pascal string */
525 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
527 ParamText(stuff
, NULL
, NULL
, NULL
);
534 * c-file-style: "simon"