1 /* $Id: mac.c,v 1.15 2003/01/04 00:13:18 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>
44 #include <Resources.h>
46 #include <TextCommon.h>
47 #include <ToolUtils.h>
48 #include <UnicodeConverter.h>
53 #include <stdlib.h> /* putty.h needs size_t */
54 #include <stdio.h> /* for vsprintf */
56 #define PUTTY_DO_GLOBALS
65 struct mac_gestalts mac_gestalts
;
67 static void mac_startup(void);
68 static void mac_eventloop(void);
69 #pragma noreturn (mac_eventloop)
70 static void mac_event(EventRecord
*);
71 static void mac_contentclick(WindowPtr
, EventRecord
*);
72 static void mac_growwindow(WindowPtr
, EventRecord
*);
73 static void mac_activatewindow(WindowPtr
, EventRecord
*);
74 static void mac_activateabout(WindowPtr
, EventRecord
*);
75 static void mac_updatewindow(WindowPtr
);
76 static void mac_updatelicence(WindowPtr
);
77 static void mac_keypress(EventRecord
*);
78 static int mac_windowtype(WindowPtr
);
79 static void mac_menucommand(long);
80 static void mac_openabout(void);
81 static void mac_openlicence(void);
82 static void mac_adjustcursor(RgnHandle
);
83 static void mac_adjustmenus(void);
84 static void mac_closewindow(WindowPtr
);
85 static void mac_zoomwindow(WindowPtr
, short);
86 static void mac_shutdown(void);
87 #pragma noreturn (mac_shutdown)
94 struct mac_windows windows
;
96 int main (int argc
, char **argv
) {
102 #pragma noreturn (main)
104 static void mac_startup(void) {
108 /* Init Memory Manager */
111 InitGraf(&qd
.thePort
);
112 /* Init Font Manager */
114 /* Init Window Manager */
116 /* Init Menu Manager */
120 /* Init Dialog Manager */
124 /* Get base system version (only used if there's no better selector) */
125 if (Gestalt(gestaltSystemVersion
, &mac_gestalts
.sysvers
) != noErr
||
126 (mac_gestalts
.sysvers
& 0xffff) < 0x700)
127 fatalbox("PuTTY requires System 7 or newer");
128 mac_gestalts
.sysvers
&= 0xffff;
129 /* Find out if we've got Color Quickdraw */
130 if (Gestalt(gestaltQuickdrawVersion
, &mac_gestalts
.qdvers
) != noErr
)
131 mac_gestalts
.qdvers
= gestaltOriginalQD
;
132 /* ... and the Appearance Manager? */
133 if (Gestalt(gestaltAppearanceVersion
, &mac_gestalts
.apprvers
) != noErr
)
134 if (Gestalt(gestaltAppearanceAttr
, NULL
) == noErr
)
135 mac_gestalts
.apprvers
= 0x0100;
137 mac_gestalts
.apprvers
= 0;
138 #if TARGET_RT_MAC_CFM
139 /* Paranoia: Did we manage to pull in AppearanceLib? */
140 if (&RegisterAppearanceClient
== kUnresolvedCFragSymbolAddress
)
141 mac_gestalts
.apprvers
= 0;
144 mac_gestalts
.cntlattr
= 0;
145 mac_gestalts
.windattr
= 0;
147 /* Mac OS 8.5 Control Manager (proportional scrollbars)? */
148 if (Gestalt(gestaltControlMgrAttr
, &mac_gestalts
.cntlattr
) != noErr
||
149 &SetControlViewSize
== kUnresolvedCFragSymbolAddress
)
150 mac_gestalts
.cntlattr
= 0;
151 /* Mac OS 8.5 Window Manager? */
152 if (Gestalt(gestaltWindowMgrAttr
, &mac_gestalts
.windattr
) != noErr
||
153 &SetWindowContentColor
== kUnresolvedCFragSymbolAddress
)
154 mac_gestalts
.windattr
= 0;
156 /* Text Encoding Conversion Manager? */
158 #if TARGET_RT_MAC_CFM
159 &TECGetInfo
== kUnresolvedCFragSymbolAddress
||
161 InitializeUnicodeConverter(NULL
) != noErr
||
163 TECGetInfo(&ti
) != noErr
)
164 mac_gestalts
.encvvers
= 0;
166 mac_gestalts
.encvvers
= (*ti
)->tecVersion
;
167 mac_gestalts
.uncvattr
= (*ti
)->tecUnicodeConverterFeatures
;
168 DisposeHandle((Handle
)ti
);
171 /* We've been tested with the Appearance Manager */
172 if (mac_gestalts
.apprvers
!= 0)
173 RegisterAppearanceClient();
175 menuBar
= GetNewMBar(128);
177 fatalbox("Unable to create menu bar.");
179 AppendResMenu(GetMenuHandle(mApple
), 'DRVR');
183 windows
.about
= NULL
;
184 windows
.licence
= NULL
;
190 /* Set the default directory for loading and saving settings. */
191 /* XXX Should we create it? */
192 if (get_session_dir(FALSE
, &vol
, &dirid
) == noErr
) {
193 LMSetSFSaveDisk(-vol
);
194 LMSetCurDirStore(dirid
);
200 static void mac_eventloop(void) {
207 mac_adjustcursor(cursrgn
);
208 gotevent
= WaitNextEvent(everyEvent
, &event
, LONG_MAX
, cursrgn
);
209 mac_adjustcursor(cursrgn
);
216 static void mac_event(EventRecord
*event
) {
221 switch (event
->what
) {
223 part
= FindWindow(event
->where
, &window
);
227 mac_menucommand(MenuSelect(event
->where
));
230 SystemClick(event
, window
);
233 if (window
!= FrontWindow())
234 /* XXX: check for movable modal dboxes? */
235 SelectWindow(window
);
237 mac_contentclick(window
, event
);
240 if (TrackGoAway(window
, event
->where
))
241 mac_closewindow(window
);
244 /* XXX: moveable modal check? */
245 DragWindow(window
, event
->where
, &qd
.screenBits
.bounds
);
248 mac_growwindow(window
, event
);
252 if (TrackBox(window
, event
->where
, part
))
253 mac_zoomwindow(window
, part
);
262 mac_activatewindow((WindowPtr
)event
->message
, event
);
265 mac_updatewindow((WindowPtr
)event
->message
);
268 if (HiWord(event
->message
) != noErr
) {
269 SetPt(&pt
, 120, 120);
270 DIBadMount(pt
, event
->message
);
276 static void mac_contentclick(WindowPtr window
, EventRecord
*event
) {
279 switch (mac_windowtype(window
)) {
281 mac_clickterm(window
, event
);
284 if (DialogSelect(event
, &window
, &item
))
292 mac_clickdlg(window
, event
);
297 static void mac_growwindow(WindowPtr window
, EventRecord
*event
) {
299 switch (mac_windowtype(window
)) {
301 mac_growterm(window
, event
);
305 static void mac_activatewindow(WindowPtr window
, EventRecord
*event
) {
308 active
= (event
->modifiers
& activeFlag
) != 0;
310 switch (mac_windowtype(window
)) {
312 mac_activateterm(window
, active
);
315 mac_activatedlg(window
, event
);
318 mac_activateabout(window
, event
);
323 static void mac_activateabout(WindowPtr window
, EventRecord
*event
) {
324 DialogItemType itemtype
;
330 active
= (event
->modifiers
& activeFlag
) != 0;
331 GetDialogItem(window
, wiAboutLicence
, &itemtype
, &itemhandle
, &itemrect
);
332 HiliteControl((ControlHandle
)itemhandle
, active ?
0 : 255);
333 DialogSelect(event
, &window
, &item
);
336 static void mac_updatewindow(WindowPtr window
) {
338 switch (mac_windowtype(window
)) {
340 mac_updateterm(window
);
345 UpdateDialog(window
, window
->visRgn
);
349 mac_updatelicence(window
);
354 static void mac_updatelicence(WindowPtr window
)
362 fondsize
= GetScriptVariable(smRoman
, smScriptSmallFondSize
);
363 TextFont(HiWord(fondsize
));
364 TextSize(LoWord(fondsize
));
365 h
= Get1Resource('TEXT', wLicence
);
366 len
= GetResourceSizeOnDisk(h
);
369 TETextBox(*h
, len
, &window
->portRect
, teFlushDefault
);
376 * Work out what kind of window we're dealing with.
377 * Concept shamelessly nicked from SurfWriter.
379 static int mac_windowtype(WindowPtr window
) {
385 kind
= ((WindowPeek
)window
)->windowKind
;
388 if (GetWVariant(window
) == zoomDocProc
)
390 refcon
= GetWRefCon(window
);
400 static void mac_keypress(EventRecord
*event
) {
403 window
= FrontWindow();
405 * Check for a command-key combination, but ignore it if it counts
406 * as a meta-key combination and we're in a terminal window.
408 if (event
->what
== keyDown
&& (event
->modifiers
& cmdKey
) /*&&
409 !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers &&
410 mac_windowtype(window) == wTerminal)*/) {
412 mac_menucommand(MenuKey(event
->message
& charCodeMask
));
414 switch (mac_windowtype(window
)) {
416 mac_keyterm(window
, event
);
422 static void mac_menucommand(long result
) {
427 menu
= HiWord(result
);
428 item
= LoWord(result
);
429 window
= FrontWindow();
430 /* Things which do the same whatever window we're in. */
438 GetMenuItemText(GetMenuHandle(mApple
), item
, da
);
452 mac_closewindow(window
);
460 /* If we get here, handling is up to window-specific code. */
461 switch (mac_windowtype(window
)) {
463 mac_menuterm(window
, menu
, item
);
470 static void mac_openabout(void) {
471 DialogItemType itemtype
;
478 SelectWindow(windows
.about
);
480 windows
.about
= GetNewDialog(wAbout
, NULL
, (WindowPtr
)-1);
481 vers
= (VersRecHndl
)Get1Resource('vers', 1);
482 if (vers
!= NULL
&& *vers
!= NULL
) {
483 longvers
= (*vers
)->shortVersion
+ (*vers
)->shortVersion
[0] + 1;
484 GetDialogItem(windows
.about
, wiAboutVersion
,
485 &itemtype
, &item
, &box
);
486 assert(itemtype
& kStaticTextDialogItem
);
487 SetDialogItemText(item
, longvers
);
489 ShowWindow(windows
.about
);
493 static void mac_openlicence(void) {
496 SelectWindow(windows
.licence
);
498 windows
.licence
= GetNewWindow(wLicence
, NULL
, (WindowPtr
)-1);
499 ShowWindow(windows
.licence
);
503 static void mac_closewindow(WindowPtr window
) {
505 switch (mac_windowtype(window
)) {
507 CloseDeskAcc(((WindowPeek
)window
)->windowKind
);
510 /* FIXME: end session and stuff */
513 windows
.about
= NULL
;
517 windows
.licence
= NULL
;
526 static void mac_zoomwindow(WindowPtr window
, short part
) {
528 /* FIXME: do something */
532 * Make the menus look right before the user gets to see them.
534 static void mac_adjustmenus(void) {
538 window
= FrontWindow();
539 menu
= GetMenuHandle(mApple
);
541 EnableItem(menu
, iAbout
);
543 menu
= GetMenuHandle(mFile
);
545 EnableItem(menu
, iNew
);
547 EnableItem(menu
, iClose
);
549 DisableItem(menu
, iClose
);
550 EnableItem(menu
, iQuit
);
552 switch (mac_windowtype(window
)) {
554 mac_adjusttermmenus(window
);
557 menu
= GetMenuHandle(mEdit
);
558 DisableItem(menu
, 0);
565 * Make sure the right cursor's being displayed.
567 static void mac_adjustcursor(RgnHandle cursrgn
) {
569 WindowPtr window
, front
;
573 LocalToGlobal(&mouse
);
574 part
= FindWindow(mouse
, &window
);
575 front
= FrontWindow();
576 if (part
!= inContent
|| window
== NULL
|| window
!= front
) {
577 /* Cursor isn't in the front window, so switch to arrow */
578 SetCursor(&qd
.arrow
);
579 SetRectRgn(cursrgn
, SHRT_MIN
, SHRT_MIN
, SHRT_MAX
, SHRT_MAX
);
581 DiffRgn(cursrgn
, front
->visRgn
, cursrgn
);
583 switch (mac_windowtype(window
)) {
585 mac_adjusttermcursor(window
, mouse
, cursrgn
);
588 SetCursor(&qd
.arrow
);
589 CopyRgn(window
->visRgn
, cursrgn
);
595 static void mac_shutdown(void) {
597 #if !TARGET_RT_MAC_CFM
598 if (mac_gestalts
.encvvers
!= 0)
599 TerminateUnicodeConverter();
604 void fatalbox(char *fmt
, ...) {
609 /* We'd like stuff to be a Pascal string */
610 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
612 ParamText(stuff
, NULL
, NULL
, NULL
);
617 void modalfatalbox(char *fmt
, ...) {
622 /* We'd like stuff to be a Pascal string */
623 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
625 ParamText(stuff
, NULL
, NULL
, NULL
);
632 * c-file-style: "simon"