1 /* $Id: mac.c,v 1.6 2002/12/10 01:11:40 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 /* Get base system version (only used if there's no better selector) */
121 if (Gestalt(gestaltSystemVersion
, &mac_gestalts
.sysvers
) != noErr
||
122 (mac_gestalts
.sysvers
& 0xffff) < 0x700)
123 fatalbox("PuTTY requires System 7 or newer");
124 mac_gestalts
.sysvers
&= 0xffff;
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
);
180 static void mac_event(EventRecord
*event
) {
185 switch (event
->what
) {
187 part
= FindWindow(event
->where
, &window
);
191 mac_menucommand(MenuSelect(event
->where
));
194 SystemClick(event
, window
);
197 if (window
!= FrontWindow())
198 /* XXX: check for movable modal dboxes? */
199 SelectWindow(window
);
201 mac_contentclick(window
, event
);
204 if (TrackGoAway(window
, event
->where
))
205 mac_closewindow(window
);
208 /* XXX: moveable modal check? */
209 DragWindow(window
, event
->where
, &qd
.screenBits
.bounds
);
212 mac_growwindow(window
, event
);
216 if (TrackBox(window
, event
->where
, part
))
217 mac_zoomwindow(window
, part
);
226 mac_activatewindow((WindowPtr
)event
->message
, event
);
229 mac_updatewindow((WindowPtr
)event
->message
);
232 if (HiWord(event
->message
) != noErr
) {
233 SetPt(&pt
, 120, 120);
234 DIBadMount(pt
, event
->message
);
240 static void mac_contentclick(WindowPtr window
, EventRecord
*event
) {
243 switch (mac_windowtype(window
)) {
245 mac_clickterm(window
, event
);
248 if (DialogSelect(event
, &(DialogPtr
)window
, &item
))
258 static void mac_growwindow(WindowPtr window
, EventRecord
*event
) {
260 switch (mac_windowtype(window
)) {
262 mac_growterm(window
, event
);
266 static void mac_activatewindow(WindowPtr window
, EventRecord
*event
) {
269 active
= (event
->modifiers
& activeFlag
) != 0;
271 switch (mac_windowtype(window
)) {
273 mac_activateterm(window
, active
);
276 mac_activateabout(window
, event
);
281 static void mac_activateabout(WindowPtr window
, EventRecord
*event
) {
282 DialogItemType itemtype
;
288 active
= (event
->modifiers
& activeFlag
) != 0;
289 GetDialogItem(window
, wiAboutLicence
, &itemtype
, &itemhandle
, &itemrect
);
290 HiliteControl((ControlHandle
)itemhandle
, active ?
0 : 255);
291 DialogSelect(event
, &window
, &item
);
294 static void mac_updatewindow(WindowPtr window
) {
296 switch (mac_windowtype(window
)) {
298 mac_updateterm(window
);
302 UpdateDialog(window
, window
->visRgn
);
306 mac_updatelicence(window
);
311 static void mac_updatelicence(WindowPtr window
)
319 fondsize
= GetScriptVariable(smRoman
, smScriptSmallFondSize
);
320 TextFont(HiWord(fondsize
));
321 TextSize(LoWord(fondsize
));
322 h
= Get1Resource('TEXT', wLicence
);
323 len
= GetResourceSizeOnDisk(h
);
326 TETextBox(*h
, len
, &window
->portRect
, teFlushDefault
);
333 * Work out what kind of window we're dealing with.
334 * Concept shamelessly nicked from SurfWriter.
336 static int mac_windowtype(WindowPtr window
) {
341 kind
= ((WindowPeek
)window
)->windowKind
;
344 if (GetWVariant(window
) == zoomDocProc
)
346 return GetWRefCon(window
);
352 static void mac_keypress(EventRecord
*event
) {
355 window
= FrontWindow();
357 * Check for a command-key combination, but ignore it if it counts
358 * as a meta-key combination and we're in a terminal window.
360 if (event
->what
== keyDown
&& (event
->modifiers
& cmdKey
) /*&&
361 !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers &&
362 mac_windowtype(window) == wTerminal)*/) {
364 mac_menucommand(MenuKey(event
->message
& charCodeMask
));
366 switch (mac_windowtype(window
)) {
368 mac_keyterm(window
, event
);
374 static void mac_menucommand(long result
) {
379 menu
= HiWord(result
);
380 item
= LoWord(result
);
381 window
= FrontWindow();
382 /* Things which do the same whatever window we're in. */
390 GetMenuItemText(GetMenuHandle(mApple
), item
, da
);
401 mac_closewindow(window
);
409 /* If we get here, handling is up to window-specific code. */
410 switch (mac_windowtype(window
)) {
412 mac_menuterm(window
, menu
, item
);
419 static void mac_openabout(void) {
420 DialogItemType itemtype
;
427 SelectWindow(windows
.about
);
429 windows
.about
= GetNewDialog(wAbout
, NULL
, (WindowPtr
)-1);
430 vers
= (VersRecHndl
)Get1Resource('vers', 1);
431 if (vers
!= NULL
&& *vers
!= NULL
) {
432 longvers
= (*vers
)->shortVersion
+ (*vers
)->shortVersion
[0] + 1;
433 GetDialogItem(windows
.about
, wiAboutVersion
,
434 &itemtype
, &item
, &box
);
435 assert(itemtype
& kStaticTextDialogItem
);
436 SetDialogItemText(item
, longvers
);
438 ShowWindow(windows
.about
);
442 static void mac_openlicence(void) {
443 DialogItemType itemtype
;
450 SelectWindow(windows
.licence
);
452 windows
.licence
= GetNewWindow(wLicence
, NULL
, (WindowPtr
)-1);
453 ShowWindow(windows
.licence
);
457 static void mac_closewindow(WindowPtr window
) {
459 switch (mac_windowtype(window
)) {
461 CloseDeskAcc(((WindowPeek
)window
)->windowKind
);
464 /* FIXME: end session and stuff */
467 windows
.about
= NULL
;
471 windows
.licence
= NULL
;
480 static void mac_zoomwindow(WindowPtr window
, short part
) {
482 /* FIXME: do something */
486 * Make the menus look right before the user gets to see them.
488 static void mac_adjustmenus(void) {
492 window
= FrontWindow();
493 menu
= GetMenuHandle(mApple
);
495 EnableItem(menu
, iAbout
);
497 menu
= GetMenuHandle(mFile
);
499 EnableItem(menu
, iNew
);
501 EnableItem(menu
, iClose
);
503 DisableItem(menu
, iClose
);
504 EnableItem(menu
, iQuit
);
506 switch (mac_windowtype(window
)) {
508 mac_adjusttermmenus(window
);
511 menu
= GetMenuHandle(mEdit
);
512 DisableItem(menu
, 0);
519 * Make sure the right cursor's being displayed.
521 static void mac_adjustcursor(RgnHandle cursrgn
) {
523 WindowPtr window
, front
;
527 LocalToGlobal(&mouse
);
528 part
= FindWindow(mouse
, &window
);
529 front
= FrontWindow();
530 if (part
!= inContent
|| window
== NULL
|| window
!= front
) {
531 /* Cursor isn't in the front window, so switch to arrow */
532 SetCursor(&qd
.arrow
);
533 SetRectRgn(cursrgn
, SHRT_MIN
, SHRT_MIN
, SHRT_MAX
, SHRT_MAX
);
535 DiffRgn(cursrgn
, front
->visRgn
, cursrgn
);
537 switch (mac_windowtype(window
)) {
539 mac_adjusttermcursor(window
, mouse
, cursrgn
);
542 SetCursor(&qd
.arrow
);
543 CopyRgn(window
->visRgn
, cursrgn
);
549 static void mac_shutdown(void) {
554 void fatalbox(char *fmt
, ...) {
559 /* We'd like stuff to be a Pascal string */
560 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
562 ParamText(stuff
, NULL
, NULL
, NULL
);
567 void modalfatalbox(char *fmt
, ...) {
572 /* We'd like stuff to be a Pascal string */
573 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
575 ParamText(stuff
, NULL
, NULL
, NULL
);
582 * c-file-style: "simon"