1 /* $Id: mac.c,v 1.5 2002/12/06 00:09:34 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_updatelicence(WindowPtr
);
74 static void mac_keypress(EventRecord
*);
75 static int mac_windowtype(WindowPtr
);
76 static void mac_menucommand(long);
77 static void mac_openabout(void);
78 static void mac_openlicence(void);
79 static void mac_adjustcursor(RgnHandle
);
80 static void mac_adjustmenus(void);
81 static void mac_closewindow(WindowPtr
);
82 static void mac_zoomwindow(WindowPtr
, short);
83 static void mac_shutdown(void);
84 #pragma noreturn (mac_shutdown)
91 struct mac_windows windows
;
93 int main (int argc
, char **argv
) {
99 #pragma noreturn (main)
101 static void mac_startup(void) {
104 /* Init Memory Manager */
107 InitGraf(&qd
.thePort
);
108 /* Init Font Manager */
110 /* Init Window Manager */
112 /* Init Menu Manager */
116 /* Init Dialog Manager */
120 /* Find out if we've got Color Quickdraw */
121 if (Gestalt(gestaltQuickdrawVersion
, &mac_gestalts
.qdvers
) != noErr
)
122 mac_gestalts
.qdvers
= gestaltOriginalQD
;
123 /* ... and the Appearance Manager? */
124 if (Gestalt(gestaltAppearanceVersion
, &mac_gestalts
.apprvers
) != noErr
)
125 if (Gestalt(gestaltAppearanceAttr
, NULL
) == noErr
)
126 mac_gestalts
.apprvers
= 0x0100;
128 mac_gestalts
.apprvers
= 0;
129 #if TARGET_RT_MAC_CFM
130 /* Paranoia: Did we manage to pull in AppearanceLib? */
131 if (&RegisterAppearanceClient
== kUnresolvedCFragSymbolAddress
)
132 mac_gestalts
.apprvers
= 0;
134 /* Mac OS 8.5 Control Manager (proportional scrollbars)? */
135 if (Gestalt(gestaltControlMgrAttr
, &mac_gestalts
.cntlattr
) != noErr
)
136 mac_gestalts
.cntlattr
= 0;
137 /* Mac OS 8.5 Window Manager? */
138 if (Gestalt(gestaltWindowMgrAttr
, &mac_gestalts
.windattr
) != noErr
)
139 mac_gestalts
.windattr
= 0;
141 /* We've been tested with the Appearance Manager */
142 if (mac_gestalts
.apprvers
!= 0)
143 RegisterAppearanceClient();
145 menuBar
= GetNewMBar(128);
147 fatalbox("Unable to create menu bar.");
149 AppendResMenu(GetMenuHandle(mApple
), 'DRVR');
153 windows
.about
= NULL
;
154 windows
.licence
= NULL
;
159 static void mac_eventloop(void) {
166 mac_adjustcursor(cursrgn
);
167 gotevent
= WaitNextEvent(everyEvent
, &event
, LONG_MAX
, cursrgn
);
168 mac_adjustcursor(cursrgn
);
175 static void mac_event(EventRecord
*event
) {
180 switch (event
->what
) {
182 part
= FindWindow(event
->where
, &window
);
186 mac_menucommand(MenuSelect(event
->where
));
189 SystemClick(event
, window
);
192 if (window
!= FrontWindow())
193 /* XXX: check for movable modal dboxes? */
194 SelectWindow(window
);
196 mac_contentclick(window
, event
);
199 if (TrackGoAway(window
, event
->where
))
200 mac_closewindow(window
);
203 /* XXX: moveable modal check? */
204 DragWindow(window
, event
->where
, &qd
.screenBits
.bounds
);
207 mac_growwindow(window
, event
);
211 if (TrackBox(window
, event
->where
, part
))
212 mac_zoomwindow(window
, part
);
221 mac_activatewindow((WindowPtr
)event
->message
, event
);
224 mac_updatewindow((WindowPtr
)event
->message
);
227 if (HiWord(event
->message
) != noErr
) {
228 SetPt(&pt
, 120, 120);
229 DIBadMount(pt
, event
->message
);
235 static void mac_contentclick(WindowPtr window
, EventRecord
*event
) {
238 switch (mac_windowtype(window
)) {
240 mac_clickterm(window
, event
);
243 if (DialogSelect(event
, &(DialogPtr
)window
, &item
))
253 static void mac_growwindow(WindowPtr window
, EventRecord
*event
) {
255 switch (mac_windowtype(window
)) {
257 mac_growterm(window
, event
);
261 static void mac_activatewindow(WindowPtr window
, EventRecord
*event
) {
264 active
= (event
->modifiers
& activeFlag
) != 0;
266 switch (mac_windowtype(window
)) {
268 mac_activateterm(window
, active
);
271 mac_activateabout(window
, event
);
276 static void mac_activateabout(WindowPtr window
, EventRecord
*event
) {
277 DialogItemType itemtype
;
283 active
= (event
->modifiers
& activeFlag
) != 0;
284 GetDialogItem(window
, wiAboutLicence
, &itemtype
, &itemhandle
, &itemrect
);
285 HiliteControl((ControlHandle
)itemhandle
, active ?
0 : 255);
286 DialogSelect(event
, &window
, &item
);
289 static void mac_updatewindow(WindowPtr window
) {
291 switch (mac_windowtype(window
)) {
293 mac_updateterm(window
);
297 UpdateDialog(window
, window
->visRgn
);
301 mac_updatelicence(window
);
306 static void mac_updatelicence(WindowPtr window
)
314 fondsize
= GetScriptVariable(smRoman
, smScriptSmallFondSize
);
315 TextFont(HiWord(fondsize
));
316 TextSize(LoWord(fondsize
));
317 h
= Get1Resource('TEXT', wLicence
);
318 len
= GetResourceSizeOnDisk(h
);
321 TETextBox(*h
, len
, &window
->portRect
, teFlushDefault
);
328 * Work out what kind of window we're dealing with.
329 * Concept shamelessly nicked from SurfWriter.
331 static int mac_windowtype(WindowPtr window
) {
336 kind
= ((WindowPeek
)window
)->windowKind
;
339 if (GetWVariant(window
) == zoomDocProc
)
341 return GetWRefCon(window
);
347 static void mac_keypress(EventRecord
*event
) {
350 window
= FrontWindow();
352 * Check for a command-key combination, but ignore it if it counts
353 * as a meta-key combination and we're in a terminal window.
355 if (event
->what
== keyDown
&& (event
->modifiers
& cmdKey
) /*&&
356 !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers &&
357 mac_windowtype(window) == wTerminal)*/) {
359 mac_menucommand(MenuKey(event
->message
& charCodeMask
));
361 switch (mac_windowtype(window
)) {
363 mac_keyterm(window
, event
);
369 static void mac_menucommand(long result
) {
374 menu
= HiWord(result
);
375 item
= LoWord(result
);
376 window
= FrontWindow();
377 /* Things which do the same whatever window we're in. */
385 GetMenuItemText(GetMenuHandle(mApple
), item
, da
);
396 mac_closewindow(window
);
404 /* If we get here, handling is up to window-specific code. */
405 switch (mac_windowtype(window
)) {
407 mac_menuterm(window
, menu
, item
);
414 static void mac_openabout(void) {
415 DialogItemType itemtype
;
422 SelectWindow(windows
.about
);
424 windows
.about
= GetNewDialog(wAbout
, NULL
, (WindowPtr
)-1);
425 vers
= (VersRecHndl
)Get1Resource('vers', 1);
426 if (vers
!= NULL
&& *vers
!= NULL
) {
427 longvers
= (*vers
)->shortVersion
+ (*vers
)->shortVersion
[0] + 1;
428 GetDialogItem(windows
.about
, wiAboutVersion
,
429 &itemtype
, &item
, &box
);
430 assert(itemtype
& kStaticTextDialogItem
);
431 SetDialogItemText(item
, longvers
);
433 ShowWindow(windows
.about
);
437 static void mac_openlicence(void) {
438 DialogItemType itemtype
;
445 SelectWindow(windows
.licence
);
447 windows
.licence
= GetNewWindow(wLicence
, NULL
, (WindowPtr
)-1);
448 ShowWindow(windows
.licence
);
452 static void mac_closewindow(WindowPtr window
) {
454 switch (mac_windowtype(window
)) {
456 CloseDeskAcc(((WindowPeek
)window
)->windowKind
);
459 /* FIXME: end session and stuff */
462 windows
.about
= NULL
;
466 windows
.licence
= NULL
;
475 static void mac_zoomwindow(WindowPtr window
, short part
) {
477 /* FIXME: do something */
481 * Make the menus look right before the user gets to see them.
483 static void mac_adjustmenus(void) {
487 window
= FrontWindow();
488 menu
= GetMenuHandle(mApple
);
490 EnableItem(menu
, iAbout
);
492 menu
= GetMenuHandle(mFile
);
494 EnableItem(menu
, iNew
);
496 EnableItem(menu
, iClose
);
498 DisableItem(menu
, iClose
);
499 EnableItem(menu
, iQuit
);
501 switch (mac_windowtype(window
)) {
503 mac_adjusttermmenus(window
);
506 menu
= GetMenuHandle(mEdit
);
507 DisableItem(menu
, 0);
514 * Make sure the right cursor's being displayed.
516 static void mac_adjustcursor(RgnHandle cursrgn
) {
518 WindowPtr window
, front
;
522 LocalToGlobal(&mouse
);
523 part
= FindWindow(mouse
, &window
);
524 front
= FrontWindow();
525 if (part
!= inContent
|| window
== NULL
|| window
!= front
) {
526 /* Cursor isn't in the front window, so switch to arrow */
527 SetCursor(&qd
.arrow
);
528 SetRectRgn(cursrgn
, SHRT_MIN
, SHRT_MIN
, SHRT_MAX
, SHRT_MAX
);
530 DiffRgn(cursrgn
, front
->visRgn
, cursrgn
);
532 switch (mac_windowtype(window
)) {
534 mac_adjusttermcursor(window
, mouse
, cursrgn
);
537 SetCursor(&qd
.arrow
);
538 CopyRgn(window
->visRgn
, cursrgn
);
544 static void mac_shutdown(void) {
549 void fatalbox(char *fmt
, ...) {
554 /* We'd like stuff to be a Pascal string */
555 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
557 ParamText(stuff
, NULL
, NULL
, NULL
);
562 void modalfatalbox(char *fmt
, ...) {
567 /* We'd like stuff to be a Pascal string */
568 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
570 ParamText(stuff
, NULL
, NULL
, NULL
);
577 * c-file-style: "simon"