1 /* $Id: mac.c,v 1.4 2002/11/24 15:08:52 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_updatelicence(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_openlicence(void);
78 static void mac_adjustcursor(RgnHandle
);
79 static void mac_adjustmenus(void);
80 static void mac_closewindow(WindowPtr
);
81 static void mac_zoomwindow(WindowPtr
, short);
82 static void mac_shutdown(void);
83 #pragma noreturn (mac_shutdown)
90 struct mac_windows windows
;
92 int main (int argc
, char **argv
) {
98 #pragma noreturn (main)
100 static void mac_startup(void) {
103 /* Init Memory Manager */
106 InitGraf(&qd
.thePort
);
107 /* Init Font Manager */
109 /* Init Window Manager */
111 /* Init Menu Manager */
115 /* Init Dialog Manager */
119 /* Find out if we've got Color Quickdraw */
120 if (Gestalt(gestaltQuickdrawVersion
, &mac_gestalts
.qdvers
) != noErr
)
121 mac_gestalts
.qdvers
= gestaltOriginalQD
;
122 /* ... and the Appearance Manager? */
123 if (Gestalt(gestaltAppearanceVersion
, &mac_gestalts
.apprvers
) != noErr
)
124 if (Gestalt(gestaltAppearanceAttr
, NULL
) == noErr
)
125 mac_gestalts
.apprvers
= 0x0100;
127 mac_gestalts
.apprvers
= 0;
128 #if TARGET_RT_MAC_CFM
129 /* Paranoia: Did we manage to pull in AppearanceLib? */
130 if (&RegisterAppearanceClient
== kUnresolvedCFragSymbolAddress
)
131 mac_gestalts
.apprvers
= 0;
133 /* Mac OS 8.5 Control Manager (proportional scrollbars)? */
134 if (Gestalt(gestaltControlMgrAttr
, &mac_gestalts
.cntlattr
) != noErr
)
135 mac_gestalts
.cntlattr
= 0;
136 /* Mac OS 8.5 Window Manager? */
137 if (Gestalt(gestaltWindowMgrAttr
, &mac_gestalts
.windattr
) != noErr
)
138 mac_gestalts
.windattr
= 0;
140 /* We've been tested with the Appearance Manager */
141 if (mac_gestalts
.apprvers
!= 0)
142 RegisterAppearanceClient();
144 menuBar
= GetNewMBar(128);
146 fatalbox("Unable to create menu bar.");
148 AppendResMenu(GetMenuHandle(mApple
), 'DRVR');
152 windows
.about
= NULL
;
153 windows
.licence
= NULL
;
158 static void mac_eventloop(void) {
165 mac_adjustcursor(cursrgn
);
166 gotevent
= WaitNextEvent(everyEvent
, &event
, LONG_MAX
, cursrgn
);
167 mac_adjustcursor(cursrgn
);
174 static void mac_event(EventRecord
*event
) {
179 switch (event
->what
) {
181 part
= FindWindow(event
->where
, &window
);
185 mac_menucommand(MenuSelect(event
->where
));
188 SystemClick(event
, window
);
191 if (window
!= FrontWindow())
192 /* XXX: check for movable modal dboxes? */
193 SelectWindow(window
);
195 mac_contentclick(window
, event
);
198 if (TrackGoAway(window
, event
->where
))
199 mac_closewindow(window
);
202 /* XXX: moveable modal check? */
203 DragWindow(window
, event
->where
, &qd
.screenBits
.bounds
);
206 mac_growwindow(window
, event
);
210 if (TrackBox(window
, event
->where
, part
))
211 mac_zoomwindow(window
, part
);
220 mac_activatewindow((WindowPtr
)event
->message
, event
);
223 mac_updatewindow((WindowPtr
)event
->message
);
226 if (HiWord(event
->message
) != noErr
) {
227 SetPt(&pt
, 120, 120);
228 DIBadMount(pt
, event
->message
);
234 static void mac_contentclick(WindowPtr window
, EventRecord
*event
) {
237 switch (mac_windowtype(window
)) {
239 mac_clickterm(window
, event
);
242 if (DialogSelect(event
, &(DialogPtr
)window
, &item
))
252 static void mac_growwindow(WindowPtr window
, EventRecord
*event
) {
254 switch (mac_windowtype(window
)) {
256 mac_growterm(window
, event
);
260 static void mac_activatewindow(WindowPtr window
, EventRecord
*event
) {
263 active
= (event
->modifiers
& activeFlag
) != 0;
265 switch (mac_windowtype(window
)) {
267 mac_activateterm(window
, active
);
270 mac_activateabout(window
, event
);
275 static void mac_activateabout(WindowPtr window
, EventRecord
*event
) {
276 DialogItemType itemtype
;
282 active
= (event
->modifiers
& activeFlag
) != 0;
283 GetDialogItem(window
, wiAboutLicence
, &itemtype
, &itemhandle
, &itemrect
);
284 HiliteControl((ControlHandle
)itemhandle
, active ?
0 : 255);
285 DialogSelect(event
, &window
, &item
);
288 static void mac_updatewindow(WindowPtr window
) {
290 switch (mac_windowtype(window
)) {
292 mac_updateterm(window
);
296 UpdateDialog(window
, window
->visRgn
);
300 mac_updatelicence(window
);
305 static void mac_updatelicence(WindowPtr window
)
314 h
= Get1Resource('TEXT', wLicence
);
315 len
= GetResourceSizeOnDisk(h
);
318 TETextBox(*h
, len
, &window
->portRect
, teFlushDefault
);
325 * Work out what kind of window we're dealing with.
326 * Concept shamelessly nicked from SurfWriter.
328 static int mac_windowtype(WindowPtr window
) {
333 kind
= ((WindowPeek
)window
)->windowKind
;
336 if (GetWVariant(window
) == zoomDocProc
)
338 return GetWRefCon(window
);
344 static void mac_keypress(EventRecord
*event
) {
347 window
= FrontWindow();
349 * Check for a command-key combination, but ignore it if it counts
350 * as a meta-key combination and we're in a terminal window.
352 if (event
->what
== keyDown
&& (event
->modifiers
& cmdKey
) /*&&
353 !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers &&
354 mac_windowtype(window) == wTerminal)*/) {
356 mac_menucommand(MenuKey(event
->message
& charCodeMask
));
358 switch (mac_windowtype(window
)) {
360 mac_keyterm(window
, event
);
366 static void mac_menucommand(long result
) {
371 menu
= HiWord(result
);
372 item
= LoWord(result
);
373 window
= FrontWindow();
374 /* Things which do the same whatever window we're in. */
382 GetMenuItemText(GetMenuHandle(mApple
), item
, da
);
393 mac_closewindow(window
);
401 /* If we get here, handling is up to window-specific code. */
402 switch (mac_windowtype(window
)) {
404 mac_menuterm(window
, menu
, item
);
411 static void mac_openabout(void) {
412 DialogItemType itemtype
;
419 SelectWindow(windows
.about
);
421 windows
.about
= GetNewDialog(wAbout
, NULL
, (WindowPtr
)-1);
422 vers
= (VersRecHndl
)Get1Resource('vers', 1);
423 if (vers
!= NULL
&& *vers
!= NULL
) {
424 longvers
= (*vers
)->shortVersion
+ (*vers
)->shortVersion
[0] + 1;
425 GetDialogItem(windows
.about
, wiAboutVersion
,
426 &itemtype
, &item
, &box
);
427 assert(itemtype
& kStaticTextDialogItem
);
428 SetDialogItemText(item
, longvers
);
430 ShowWindow(windows
.about
);
434 static void mac_openlicence(void) {
435 DialogItemType itemtype
;
442 SelectWindow(windows
.licence
);
444 windows
.licence
= GetNewWindow(wLicence
, NULL
, (WindowPtr
)-1);
445 ShowWindow(windows
.licence
);
449 static void mac_closewindow(WindowPtr window
) {
451 switch (mac_windowtype(window
)) {
453 CloseDeskAcc(((WindowPeek
)window
)->windowKind
);
456 /* FIXME: end session and stuff */
459 windows
.about
= NULL
;
463 windows
.licence
= NULL
;
472 static void mac_zoomwindow(WindowPtr window
, short part
) {
474 /* FIXME: do something */
478 * Make the menus look right before the user gets to see them.
480 static void mac_adjustmenus(void) {
484 window
= FrontWindow();
485 menu
= GetMenuHandle(mApple
);
487 EnableItem(menu
, iAbout
);
489 menu
= GetMenuHandle(mFile
);
491 EnableItem(menu
, iNew
);
493 EnableItem(menu
, iClose
);
495 DisableItem(menu
, iClose
);
496 EnableItem(menu
, iQuit
);
498 switch (mac_windowtype(window
)) {
500 mac_adjusttermmenus(window
);
503 menu
= GetMenuHandle(mEdit
);
504 DisableItem(menu
, 0);
511 * Make sure the right cursor's being displayed.
513 static void mac_adjustcursor(RgnHandle cursrgn
) {
515 WindowPtr window
, front
;
519 LocalToGlobal(&mouse
);
520 part
= FindWindow(mouse
, &window
);
521 front
= FrontWindow();
522 if (part
!= inContent
|| window
== NULL
|| window
!= front
) {
523 /* Cursor isn't in the front window, so switch to arrow */
524 SetCursor(&qd
.arrow
);
525 SetRectRgn(cursrgn
, SHRT_MIN
, SHRT_MIN
, SHRT_MAX
, SHRT_MAX
);
527 DiffRgn(cursrgn
, front
->visRgn
, cursrgn
);
529 switch (mac_windowtype(window
)) {
531 mac_adjusttermcursor(window
, mouse
, cursrgn
);
534 SetCursor(&qd
.arrow
);
535 CopyRgn(window
->visRgn
, cursrgn
);
541 static void mac_shutdown(void) {
546 void fatalbox(char *fmt
, ...) {
551 /* We'd like stuff to be a Pascal string */
552 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
554 ParamText(stuff
, NULL
, NULL
, NULL
);
559 void modalfatalbox(char *fmt
, ...) {
564 /* We'd like stuff to be a Pascal string */
565 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
567 ParamText(stuff
, NULL
, NULL
, NULL
);
574 * c-file-style: "simon"