1 /* $Id: mac.c,v 1.9 2002/12/29 19:14:56 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 <TextCommon.h>
46 #include <ToolUtils.h>
47 #include <UnicodeConverter.h>
52 #include <stdlib.h> /* putty.h needs size_t */
53 #include <stdio.h> /* for vsprintf */
55 #define PUTTY_DO_GLOBALS
64 struct mac_gestalts mac_gestalts
;
66 static void mac_startup(void);
67 static void mac_eventloop(void);
68 #pragma noreturn (mac_eventloop)
69 static void mac_event(EventRecord
*);
70 static void mac_contentclick(WindowPtr
, EventRecord
*);
71 static void mac_growwindow(WindowPtr
, EventRecord
*);
72 static void mac_activatewindow(WindowPtr
, EventRecord
*);
73 static void mac_activateabout(WindowPtr
, EventRecord
*);
74 static void mac_updatewindow(WindowPtr
);
75 static void mac_updatelicence(WindowPtr
);
76 static void mac_keypress(EventRecord
*);
77 static int mac_windowtype(WindowPtr
);
78 static void mac_menucommand(long);
79 static void mac_openabout(void);
80 static void mac_openlicence(void);
81 static void mac_adjustcursor(RgnHandle
);
82 static void mac_adjustmenus(void);
83 static void mac_closewindow(WindowPtr
);
84 static void mac_zoomwindow(WindowPtr
, short);
85 static void mac_shutdown(void);
86 #pragma noreturn (mac_shutdown)
93 struct mac_windows windows
;
95 int main (int argc
, char **argv
) {
101 #pragma noreturn (main)
103 static void mac_startup(void) {
107 /* Init Memory Manager */
110 InitGraf(&qd
.thePort
);
111 /* Init Font Manager */
113 /* Init Window Manager */
115 /* Init Menu Manager */
119 /* Init Dialog Manager */
123 /* Get base system version (only used if there's no better selector) */
124 if (Gestalt(gestaltSystemVersion
, &mac_gestalts
.sysvers
) != noErr
||
125 (mac_gestalts
.sysvers
& 0xffff) < 0x700)
126 fatalbox("PuTTY requires System 7 or newer");
127 mac_gestalts
.sysvers
&= 0xffff;
128 /* Find out if we've got Color Quickdraw */
129 if (Gestalt(gestaltQuickdrawVersion
, &mac_gestalts
.qdvers
) != noErr
)
130 mac_gestalts
.qdvers
= gestaltOriginalQD
;
131 /* ... and the Appearance Manager? */
132 if (Gestalt(gestaltAppearanceVersion
, &mac_gestalts
.apprvers
) != noErr
)
133 if (Gestalt(gestaltAppearanceAttr
, NULL
) == noErr
)
134 mac_gestalts
.apprvers
= 0x0100;
136 mac_gestalts
.apprvers
= 0;
137 #if TARGET_RT_MAC_CFM
138 /* Paranoia: Did we manage to pull in AppearanceLib? */
139 if (&RegisterAppearanceClient
== kUnresolvedCFragSymbolAddress
)
140 mac_gestalts
.apprvers
= 0;
142 /* Mac OS 8.5 Control Manager (proportional scrollbars)? */
143 if (Gestalt(gestaltControlMgrAttr
, &mac_gestalts
.cntlattr
) != noErr
)
144 mac_gestalts
.cntlattr
= 0;
145 /* Mac OS 8.5 Window Manager? */
146 if (Gestalt(gestaltWindowMgrAttr
, &mac_gestalts
.windattr
) != noErr
)
147 mac_gestalts
.windattr
= 0;
148 /* Text Encoding Conversion Manager? */
150 #if TARGET_RT_MAC_CFM
151 &TECGetInfo
== kUnresolvedCFragSymbolAddress
||
153 InitializeUnicodeConverter(NULL
) != noErr
||
155 TECGetInfo(&ti
) != noErr
)
156 mac_gestalts
.encvvers
= 0;
158 mac_gestalts
.encvvers
= (*ti
)->tecVersion
;
159 DisposeHandle((Handle
)ti
);
162 /* We've been tested with the Appearance Manager */
163 if (mac_gestalts
.apprvers
!= 0)
164 RegisterAppearanceClient();
166 menuBar
= GetNewMBar(128);
168 fatalbox("Unable to create menu bar.");
170 AppendResMenu(GetMenuHandle(mApple
), 'DRVR');
174 windows
.about
= NULL
;
175 windows
.licence
= NULL
;
180 static void mac_eventloop(void) {
187 mac_adjustcursor(cursrgn
);
188 gotevent
= WaitNextEvent(everyEvent
, &event
, LONG_MAX
, cursrgn
);
189 mac_adjustcursor(cursrgn
);
196 static void mac_event(EventRecord
*event
) {
201 switch (event
->what
) {
203 part
= FindWindow(event
->where
, &window
);
207 mac_menucommand(MenuSelect(event
->where
));
210 SystemClick(event
, window
);
213 if (window
!= FrontWindow())
214 /* XXX: check for movable modal dboxes? */
215 SelectWindow(window
);
217 mac_contentclick(window
, event
);
220 if (TrackGoAway(window
, event
->where
))
221 mac_closewindow(window
);
224 /* XXX: moveable modal check? */
225 DragWindow(window
, event
->where
, &qd
.screenBits
.bounds
);
228 mac_growwindow(window
, event
);
232 if (TrackBox(window
, event
->where
, part
))
233 mac_zoomwindow(window
, part
);
242 mac_activatewindow((WindowPtr
)event
->message
, event
);
245 mac_updatewindow((WindowPtr
)event
->message
);
248 if (HiWord(event
->message
) != noErr
) {
249 SetPt(&pt
, 120, 120);
250 DIBadMount(pt
, event
->message
);
256 static void mac_contentclick(WindowPtr window
, EventRecord
*event
) {
259 switch (mac_windowtype(window
)) {
261 mac_clickterm(window
, event
);
264 if (DialogSelect(event
, &(DialogPtr
)window
, &item
))
274 static void mac_growwindow(WindowPtr window
, EventRecord
*event
) {
276 switch (mac_windowtype(window
)) {
278 mac_growterm(window
, event
);
282 static void mac_activatewindow(WindowPtr window
, EventRecord
*event
) {
285 active
= (event
->modifiers
& activeFlag
) != 0;
287 switch (mac_windowtype(window
)) {
289 mac_activateterm(window
, active
);
292 mac_activateabout(window
, event
);
297 static void mac_activateabout(WindowPtr window
, EventRecord
*event
) {
298 DialogItemType itemtype
;
304 active
= (event
->modifiers
& activeFlag
) != 0;
305 GetDialogItem(window
, wiAboutLicence
, &itemtype
, &itemhandle
, &itemrect
);
306 HiliteControl((ControlHandle
)itemhandle
, active ?
0 : 255);
307 DialogSelect(event
, &window
, &item
);
310 static void mac_updatewindow(WindowPtr window
) {
312 switch (mac_windowtype(window
)) {
314 mac_updateterm(window
);
318 UpdateDialog(window
, window
->visRgn
);
322 mac_updatelicence(window
);
327 static void mac_updatelicence(WindowPtr window
)
335 fondsize
= GetScriptVariable(smRoman
, smScriptSmallFondSize
);
336 TextFont(HiWord(fondsize
));
337 TextSize(LoWord(fondsize
));
338 h
= Get1Resource('TEXT', wLicence
);
339 len
= GetResourceSizeOnDisk(h
);
342 TETextBox(*h
, len
, &window
->portRect
, teFlushDefault
);
349 * Work out what kind of window we're dealing with.
350 * Concept shamelessly nicked from SurfWriter.
352 static int mac_windowtype(WindowPtr window
) {
357 kind
= ((WindowPeek
)window
)->windowKind
;
360 if (GetWVariant(window
) == zoomDocProc
)
362 return GetWRefCon(window
);
368 static void mac_keypress(EventRecord
*event
) {
371 window
= FrontWindow();
373 * Check for a command-key combination, but ignore it if it counts
374 * as a meta-key combination and we're in a terminal window.
376 if (event
->what
== keyDown
&& (event
->modifiers
& cmdKey
) /*&&
377 !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers &&
378 mac_windowtype(window) == wTerminal)*/) {
380 mac_menucommand(MenuKey(event
->message
& charCodeMask
));
382 switch (mac_windowtype(window
)) {
384 mac_keyterm(window
, event
);
390 static void mac_menucommand(long result
) {
395 menu
= HiWord(result
);
396 item
= LoWord(result
);
397 window
= FrontWindow();
398 /* Things which do the same whatever window we're in. */
406 GetMenuItemText(GetMenuHandle(mApple
), item
, da
);
417 mac_closewindow(window
);
425 /* If we get here, handling is up to window-specific code. */
426 switch (mac_windowtype(window
)) {
428 mac_menuterm(window
, menu
, item
);
435 static void mac_openabout(void) {
436 DialogItemType itemtype
;
443 SelectWindow(windows
.about
);
445 windows
.about
= GetNewDialog(wAbout
, NULL
, (WindowPtr
)-1);
446 vers
= (VersRecHndl
)Get1Resource('vers', 1);
447 if (vers
!= NULL
&& *vers
!= NULL
) {
448 longvers
= (*vers
)->shortVersion
+ (*vers
)->shortVersion
[0] + 1;
449 GetDialogItem(windows
.about
, wiAboutVersion
,
450 &itemtype
, &item
, &box
);
451 assert(itemtype
& kStaticTextDialogItem
);
452 SetDialogItemText(item
, longvers
);
454 ShowWindow(windows
.about
);
458 static void mac_openlicence(void) {
461 SelectWindow(windows
.licence
);
463 windows
.licence
= GetNewWindow(wLicence
, NULL
, (WindowPtr
)-1);
464 ShowWindow(windows
.licence
);
468 static void mac_closewindow(WindowPtr window
) {
470 switch (mac_windowtype(window
)) {
472 CloseDeskAcc(((WindowPeek
)window
)->windowKind
);
475 /* FIXME: end session and stuff */
478 windows
.about
= NULL
;
482 windows
.licence
= NULL
;
491 static void mac_zoomwindow(WindowPtr window
, short part
) {
493 /* FIXME: do something */
497 * Make the menus look right before the user gets to see them.
499 static void mac_adjustmenus(void) {
503 window
= FrontWindow();
504 menu
= GetMenuHandle(mApple
);
506 EnableItem(menu
, iAbout
);
508 menu
= GetMenuHandle(mFile
);
510 EnableItem(menu
, iNew
);
512 EnableItem(menu
, iClose
);
514 DisableItem(menu
, iClose
);
515 EnableItem(menu
, iQuit
);
517 switch (mac_windowtype(window
)) {
519 mac_adjusttermmenus(window
);
522 menu
= GetMenuHandle(mEdit
);
523 DisableItem(menu
, 0);
530 * Make sure the right cursor's being displayed.
532 static void mac_adjustcursor(RgnHandle cursrgn
) {
534 WindowPtr window
, front
;
538 LocalToGlobal(&mouse
);
539 part
= FindWindow(mouse
, &window
);
540 front
= FrontWindow();
541 if (part
!= inContent
|| window
== NULL
|| window
!= front
) {
542 /* Cursor isn't in the front window, so switch to arrow */
543 SetCursor(&qd
.arrow
);
544 SetRectRgn(cursrgn
, SHRT_MIN
, SHRT_MIN
, SHRT_MAX
, SHRT_MAX
);
546 DiffRgn(cursrgn
, front
->visRgn
, cursrgn
);
548 switch (mac_windowtype(window
)) {
550 mac_adjusttermcursor(window
, mouse
, cursrgn
);
553 SetCursor(&qd
.arrow
);
554 CopyRgn(window
->visRgn
, cursrgn
);
560 static void mac_shutdown(void) {
562 #if !TARGET_RT_MAC_CFM
563 if (mac_gestalts
.encvvers
!= 0)
564 TerminateUnicodeConverter();
569 void fatalbox(char *fmt
, ...) {
574 /* We'd like stuff to be a Pascal string */
575 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
577 ParamText(stuff
, NULL
, NULL
, NULL
);
582 void modalfatalbox(char *fmt
, ...) {
587 /* We'd like stuff to be a Pascal string */
588 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
590 ParamText(stuff
, NULL
, NULL
, NULL
);
597 * c-file-style: "simon"