1 /* $Id: mac.c,v 1.1 2002/11/19 02:13:46 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>
45 #include <ToolUtils.h>
50 #include <stdlib.h> /* putty.h needs size_t */
51 #include <stdio.h> /* for vsprintf */
53 #define PUTTY_DO_GLOBALS
62 struct mac_gestalts mac_gestalts
;
64 static void mac_startup(void);
65 static void mac_eventloop(void);
66 #pragma noreturn (mac_eventloop)
67 static void mac_event(EventRecord
*);
68 static void mac_contentclick(WindowPtr
, EventRecord
*);
69 static void mac_growwindow(WindowPtr
, EventRecord
*);
70 static void mac_activatewindow(WindowPtr
, EventRecord
*);
71 static void mac_activateabout(WindowPtr
, EventRecord
*);
72 static void mac_updatewindow(WindowPtr
);
73 static void mac_keypress(EventRecord
*);
74 static int mac_windowtype(WindowPtr
);
75 static void mac_menucommand(long);
76 static void mac_openabout(void);
77 static void mac_adjustcursor(RgnHandle
);
78 static void mac_adjustmenus(void);
79 static void mac_closewindow(WindowPtr
);
80 static void mac_zoomwindow(WindowPtr
, short);
81 static void mac_shutdown(void);
82 #pragma noreturn (mac_shutdown)
89 struct mac_windows windows
;
91 int main (int argc
, char **argv
) {
97 #pragma noreturn (main)
99 static void mac_startup(void) {
102 /* Init Memory Manager */
105 InitGraf(&qd
.thePort
);
106 /* Init Font Manager */
108 /* Init Window Manager */
110 /* Init Menu Manager */
114 /* Init Dialog Manager */
118 /* Check for the Thread Manager. Bail out if it's not there. */
119 if (Gestalt(gestaltThreadMgrAttr
, &mac_gestalts
.thdsattr
) != noErr
||
120 !(mac_gestalts
.thdsattr
& (1 << gestaltThreadMgrPresent
)) ||
121 &NewThread
== kUnresolvedCFragSymbolAddress
)
122 fatalbox("PuTTY requires the Thread Manager in order to operate. "
123 "The Thread Manager can be obtained from Apple Software "
125 /* Find out if we've got Color Quickdraw */
126 if (Gestalt(gestaltQuickdrawVersion
, &mac_gestalts
.qdvers
) != noErr
)
127 mac_gestalts
.qdvers
= gestaltOriginalQD
;
128 /* ... and the Appearance Manager? */
129 if (Gestalt(gestaltAppearanceVersion
, &mac_gestalts
.apprvers
) != noErr
)
130 if (Gestalt(gestaltAppearanceAttr
, NULL
) == noErr
)
131 mac_gestalts
.apprvers
= 0x0100;
133 mac_gestalts
.apprvers
= 0;
134 #if TARGET_RT_MAC_CFM
135 /* Paranoia: Did we manage to pull in AppearanceLib? */
136 if (&RegisterAppearanceClient
== kUnresolvedCFragSymbolAddress
)
137 mac_gestalts
.apprvers
= 0;
139 /* Mac OS 8.5 Control Manager (proportional scrollbars)? */
140 if (Gestalt(gestaltControlMgrAttr
, &mac_gestalts
.cntlattr
) != noErr
)
141 mac_gestalts
.cntlattr
= 0;
142 /* Mac OS 8.5 Window Manager? */
143 if (Gestalt(gestaltWindowMgrAttr
, &mac_gestalts
.windattr
) != noErr
)
144 mac_gestalts
.windattr
= 0;
146 /* We've been tested with the Appearance Manager */
147 if (mac_gestalts
.apprvers
!= 0)
148 RegisterAppearanceClient();
150 menuBar
= GetNewMBar(128);
152 fatalbox("Unable to create menu bar.");
154 AppendResMenu(GetMenuHandle(mApple
), 'DRVR');
158 windows
.about
= NULL
;
159 windows
.licence
= NULL
;
164 static void mac_eventloop(void) {
171 mac_adjustcursor(cursrgn
);
172 gotevent
= WaitNextEvent(everyEvent
, &event
, LONG_MAX
, cursrgn
);
173 mac_adjustcursor(cursrgn
);
181 static void mac_event(EventRecord
*event
) {
186 switch (event
->what
) {
188 part
= FindWindow(event
->where
, &window
);
192 mac_menucommand(MenuSelect(event
->where
));
195 SystemClick(event
, window
);
198 if (window
!= FrontWindow())
199 /* XXX: check for movable modal dboxes? */
200 SelectWindow(window
);
202 mac_contentclick(window
, event
);
205 if (TrackGoAway(window
, event
->where
))
206 mac_closewindow(window
);
209 /* XXX: moveable modal check? */
210 DragWindow(window
, event
->where
, &qd
.screenBits
.bounds
);
213 mac_growwindow(window
, event
);
217 if (TrackBox(window
, event
->where
, part
))
218 mac_zoomwindow(window
, part
);
227 mac_activatewindow((WindowPtr
)event
->message
, event
);
230 mac_updatewindow((WindowPtr
)event
->message
);
233 if (HiWord(event
->message
) != noErr
) {
234 SetPt(&pt
, 120, 120);
235 DIBadMount(pt
, event
->message
);
241 static void mac_contentclick(WindowPtr window
, EventRecord
*event
) {
244 switch (mac_windowtype(window
)) {
246 mac_clickterm(window
, event
);
249 if (DialogSelect(event
, &(DialogPtr
)window
, &item
))
252 /* XXX: Do something */
259 static void mac_growwindow(WindowPtr window
, EventRecord
*event
) {
261 switch (mac_windowtype(window
)) {
263 mac_growterm(window
, event
);
267 static void mac_activatewindow(WindowPtr window
, EventRecord
*event
) {
270 active
= (event
->modifiers
& activeFlag
) != 0;
272 switch (mac_windowtype(window
)) {
274 mac_activateterm(window
, active
);
277 mac_activateabout(window
, event
);
282 static void mac_activateabout(WindowPtr window
, EventRecord
*event
) {
283 DialogItemType itemtype
;
289 active
= (event
->modifiers
& activeFlag
) != 0;
290 GetDialogItem(window
, wiAboutLicence
, &itemtype
, &itemhandle
, &itemrect
);
291 HiliteControl((ControlHandle
)itemhandle
, active ?
0 : 255);
292 DialogSelect(event
, &window
, &item
);
295 static void mac_updatewindow(WindowPtr window
) {
297 switch (mac_windowtype(window
)) {
299 mac_updateterm(window
);
303 UpdateDialog(window
, window
->visRgn
);
313 * Work out what kind of window we're dealing with.
314 * Concept shamelessly nicked from SurfWriter.
316 static int mac_windowtype(WindowPtr window
) {
321 kind
= ((WindowPeek
)window
)->windowKind
;
324 if (GetWVariant(window
) == zoomDocProc
)
326 return GetWRefCon(window
);
332 static void mac_keypress(EventRecord
*event
) {
335 window
= FrontWindow();
337 * Check for a command-key combination, but ignore it if it counts
338 * as a meta-key combination and we're in a terminal window.
340 if (event
->what
== keyDown
&& (event
->modifiers
& cmdKey
) /*&&
341 !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers &&
342 mac_windowtype(window) == wTerminal)*/) {
344 mac_menucommand(MenuKey(event
->message
& charCodeMask
));
346 switch (mac_windowtype(window
)) {
348 mac_keyterm(window
, event
);
354 static void mac_menucommand(long result
) {
359 menu
= HiWord(result
);
360 item
= LoWord(result
);
361 window
= FrontWindow();
362 /* Things which do the same whatever window we're in. */
370 GetMenuItemText(GetMenuHandle(mApple
), item
, da
);
381 mac_closewindow(window
);
389 /* If we get here, handling is up to window-specific code. */
390 switch (mac_windowtype(window
)) {
392 mac_menuterm(window
, menu
, item
);
399 static void mac_openabout(void) {
400 DialogItemType itemtype
;
407 SelectWindow(windows
.about
);
409 windows
.about
= GetNewDialog(wAbout
, NULL
, (WindowPtr
)-1);
410 /* XXX check we're using the right resource file? */
411 vers
= (VersRecHndl
)GetResource('vers', 1);
412 assert(vers
!= NULL
&& *vers
!= NULL
);
413 longvers
= (*vers
)->shortVersion
+ (*vers
)->shortVersion
[0] + 1;
414 GetDialogItem(windows
.about
, wiAboutVersion
, &itemtype
, &item
, &box
);
415 assert(itemtype
& kStaticTextDialogItem
);
416 SetDialogItemText(item
, longvers
);
417 ShowWindow(windows
.about
);
421 static void mac_closewindow(WindowPtr window
) {
423 switch (mac_windowtype(window
)) {
425 CloseDeskAcc(((WindowPeek
)window
)->windowKind
);
428 /* FIXME: end session and stuff */
431 windows
.about
= NULL
;
440 static void mac_zoomwindow(WindowPtr window
, short part
) {
442 /* FIXME: do something */
446 * Make the menus look right before the user gets to see them.
448 static void mac_adjustmenus(void) {
452 window
= FrontWindow();
453 menu
= GetMenuHandle(mApple
);
455 EnableItem(menu
, iAbout
);
457 menu
= GetMenuHandle(mFile
);
459 EnableItem(menu
, iNew
);
461 EnableItem(menu
, iClose
);
463 DisableItem(menu
, iClose
);
464 EnableItem(menu
, iQuit
);
466 switch (mac_windowtype(window
)) {
468 mac_adjusttermmenus(window
);
471 menu
= GetMenuHandle(mEdit
);
472 DisableItem(menu
, 0);
479 * Make sure the right cursor's being displayed.
481 static void mac_adjustcursor(RgnHandle cursrgn
) {
483 WindowPtr window
, front
;
487 LocalToGlobal(&mouse
);
488 part
= FindWindow(mouse
, &window
);
489 front
= FrontWindow();
490 if (part
!= inContent
|| window
== NULL
|| window
!= front
) {
491 /* Cursor isn't in the front window, so switch to arrow */
492 SetCursor(&qd
.arrow
);
493 SetRectRgn(cursrgn
, SHRT_MIN
, SHRT_MIN
, SHRT_MAX
, SHRT_MAX
);
495 DiffRgn(cursrgn
, front
->visRgn
, cursrgn
);
497 switch (mac_windowtype(window
)) {
499 mac_adjusttermcursor(window
, mouse
, cursrgn
);
502 SetCursor(&qd
.arrow
);
503 CopyRgn(window
->visRgn
, cursrgn
);
509 static void mac_shutdown(void) {
514 void fatalbox(char *fmt
, ...) {
519 /* We'd like stuff to be a Pascal string */
520 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
522 ParamText(stuff
, NULL
, NULL
, NULL
);
527 void modalfatalbox(char *fmt
, ...) {
532 /* We'd like stuff to be a Pascal string */
533 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
535 ParamText(stuff
, NULL
, NULL
, NULL
);
542 * c-file-style: "simon"