1 /* $Id: mac.c,v 1.14 2003/01/02 00:33: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>
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;
143 /* Mac OS 8.5 Control Manager (proportional scrollbars)? */
144 if (Gestalt(gestaltControlMgrAttr
, &mac_gestalts
.cntlattr
) != noErr
)
145 mac_gestalts
.cntlattr
= 0;
146 /* Mac OS 8.5 Window Manager? */
147 if (Gestalt(gestaltWindowMgrAttr
, &mac_gestalts
.windattr
) != noErr
)
148 mac_gestalts
.windattr
= 0;
149 /* Text Encoding Conversion Manager? */
151 #if TARGET_RT_MAC_CFM
152 &TECGetInfo
== kUnresolvedCFragSymbolAddress
||
154 InitializeUnicodeConverter(NULL
) != noErr
||
156 TECGetInfo(&ti
) != noErr
)
157 mac_gestalts
.encvvers
= 0;
159 mac_gestalts
.encvvers
= (*ti
)->tecVersion
;
160 mac_gestalts
.uncvattr
= (*ti
)->tecUnicodeConverterFeatures
;
161 DisposeHandle((Handle
)ti
);
164 /* We've been tested with the Appearance Manager */
165 if (mac_gestalts
.apprvers
!= 0)
166 RegisterAppearanceClient();
168 menuBar
= GetNewMBar(128);
170 fatalbox("Unable to create menu bar.");
172 AppendResMenu(GetMenuHandle(mApple
), 'DRVR');
176 windows
.about
= NULL
;
177 windows
.licence
= NULL
;
183 /* Set the default directory for loading and saving settings. */
184 /* XXX Should we create it? */
185 if (get_session_dir(FALSE
, &vol
, &dirid
) == noErr
) {
186 LMSetSFSaveDisk(-vol
);
187 LMSetCurDirStore(dirid
);
193 static void mac_eventloop(void) {
200 mac_adjustcursor(cursrgn
);
201 gotevent
= WaitNextEvent(everyEvent
, &event
, LONG_MAX
, cursrgn
);
202 mac_adjustcursor(cursrgn
);
209 static void mac_event(EventRecord
*event
) {
214 switch (event
->what
) {
216 part
= FindWindow(event
->where
, &window
);
220 mac_menucommand(MenuSelect(event
->where
));
223 SystemClick(event
, window
);
226 if (window
!= FrontWindow())
227 /* XXX: check for movable modal dboxes? */
228 SelectWindow(window
);
230 mac_contentclick(window
, event
);
233 if (TrackGoAway(window
, event
->where
))
234 mac_closewindow(window
);
237 /* XXX: moveable modal check? */
238 DragWindow(window
, event
->where
, &qd
.screenBits
.bounds
);
241 mac_growwindow(window
, event
);
245 if (TrackBox(window
, event
->where
, part
))
246 mac_zoomwindow(window
, part
);
255 mac_activatewindow((WindowPtr
)event
->message
, event
);
258 mac_updatewindow((WindowPtr
)event
->message
);
261 if (HiWord(event
->message
) != noErr
) {
262 SetPt(&pt
, 120, 120);
263 DIBadMount(pt
, event
->message
);
269 static void mac_contentclick(WindowPtr window
, EventRecord
*event
) {
272 switch (mac_windowtype(window
)) {
274 mac_clickterm(window
, event
);
277 if (DialogSelect(event
, &window
, &item
))
285 mac_clickdlg(window
, event
);
290 static void mac_growwindow(WindowPtr window
, EventRecord
*event
) {
292 switch (mac_windowtype(window
)) {
294 mac_growterm(window
, event
);
298 static void mac_activatewindow(WindowPtr window
, EventRecord
*event
) {
301 active
= (event
->modifiers
& activeFlag
) != 0;
303 switch (mac_windowtype(window
)) {
305 mac_activateterm(window
, active
);
308 mac_activatedlg(window
, event
);
311 mac_activateabout(window
, event
);
316 static void mac_activateabout(WindowPtr window
, EventRecord
*event
) {
317 DialogItemType itemtype
;
323 active
= (event
->modifiers
& activeFlag
) != 0;
324 GetDialogItem(window
, wiAboutLicence
, &itemtype
, &itemhandle
, &itemrect
);
325 HiliteControl((ControlHandle
)itemhandle
, active ?
0 : 255);
326 DialogSelect(event
, &window
, &item
);
329 static void mac_updatewindow(WindowPtr window
) {
331 switch (mac_windowtype(window
)) {
333 mac_updateterm(window
);
338 UpdateDialog(window
, window
->visRgn
);
342 mac_updatelicence(window
);
347 static void mac_updatelicence(WindowPtr window
)
355 fondsize
= GetScriptVariable(smRoman
, smScriptSmallFondSize
);
356 TextFont(HiWord(fondsize
));
357 TextSize(LoWord(fondsize
));
358 h
= Get1Resource('TEXT', wLicence
);
359 len
= GetResourceSizeOnDisk(h
);
362 TETextBox(*h
, len
, &window
->portRect
, teFlushDefault
);
369 * Work out what kind of window we're dealing with.
370 * Concept shamelessly nicked from SurfWriter.
372 static int mac_windowtype(WindowPtr window
) {
378 kind
= ((WindowPeek
)window
)->windowKind
;
381 if (GetWVariant(window
) == zoomDocProc
)
383 refcon
= GetWRefCon(window
);
393 static void mac_keypress(EventRecord
*event
) {
396 window
= FrontWindow();
398 * Check for a command-key combination, but ignore it if it counts
399 * as a meta-key combination and we're in a terminal window.
401 if (event
->what
== keyDown
&& (event
->modifiers
& cmdKey
) /*&&
402 !((event->modifiers & cfg.meta_modifiers) == cfg.meta_modifiers &&
403 mac_windowtype(window) == wTerminal)*/) {
405 mac_menucommand(MenuKey(event
->message
& charCodeMask
));
407 switch (mac_windowtype(window
)) {
409 mac_keyterm(window
, event
);
415 static void mac_menucommand(long result
) {
420 menu
= HiWord(result
);
421 item
= LoWord(result
);
422 window
= FrontWindow();
423 /* Things which do the same whatever window we're in. */
431 GetMenuItemText(GetMenuHandle(mApple
), item
, da
);
445 mac_closewindow(window
);
453 /* If we get here, handling is up to window-specific code. */
454 switch (mac_windowtype(window
)) {
456 mac_menuterm(window
, menu
, item
);
463 static void mac_openabout(void) {
464 DialogItemType itemtype
;
471 SelectWindow(windows
.about
);
473 windows
.about
= GetNewDialog(wAbout
, NULL
, (WindowPtr
)-1);
474 vers
= (VersRecHndl
)Get1Resource('vers', 1);
475 if (vers
!= NULL
&& *vers
!= NULL
) {
476 longvers
= (*vers
)->shortVersion
+ (*vers
)->shortVersion
[0] + 1;
477 GetDialogItem(windows
.about
, wiAboutVersion
,
478 &itemtype
, &item
, &box
);
479 assert(itemtype
& kStaticTextDialogItem
);
480 SetDialogItemText(item
, longvers
);
482 ShowWindow(windows
.about
);
486 static void mac_openlicence(void) {
489 SelectWindow(windows
.licence
);
491 windows
.licence
= GetNewWindow(wLicence
, NULL
, (WindowPtr
)-1);
492 ShowWindow(windows
.licence
);
496 static void mac_closewindow(WindowPtr window
) {
498 switch (mac_windowtype(window
)) {
500 CloseDeskAcc(((WindowPeek
)window
)->windowKind
);
503 /* FIXME: end session and stuff */
506 windows
.about
= NULL
;
510 windows
.licence
= NULL
;
519 static void mac_zoomwindow(WindowPtr window
, short part
) {
521 /* FIXME: do something */
525 * Make the menus look right before the user gets to see them.
527 static void mac_adjustmenus(void) {
531 window
= FrontWindow();
532 menu
= GetMenuHandle(mApple
);
534 EnableItem(menu
, iAbout
);
536 menu
= GetMenuHandle(mFile
);
538 EnableItem(menu
, iNew
);
540 EnableItem(menu
, iClose
);
542 DisableItem(menu
, iClose
);
543 EnableItem(menu
, iQuit
);
545 switch (mac_windowtype(window
)) {
547 mac_adjusttermmenus(window
);
550 menu
= GetMenuHandle(mEdit
);
551 DisableItem(menu
, 0);
558 * Make sure the right cursor's being displayed.
560 static void mac_adjustcursor(RgnHandle cursrgn
) {
562 WindowPtr window
, front
;
566 LocalToGlobal(&mouse
);
567 part
= FindWindow(mouse
, &window
);
568 front
= FrontWindow();
569 if (part
!= inContent
|| window
== NULL
|| window
!= front
) {
570 /* Cursor isn't in the front window, so switch to arrow */
571 SetCursor(&qd
.arrow
);
572 SetRectRgn(cursrgn
, SHRT_MIN
, SHRT_MIN
, SHRT_MAX
, SHRT_MAX
);
574 DiffRgn(cursrgn
, front
->visRgn
, cursrgn
);
576 switch (mac_windowtype(window
)) {
578 mac_adjusttermcursor(window
, mouse
, cursrgn
);
581 SetCursor(&qd
.arrow
);
582 CopyRgn(window
->visRgn
, cursrgn
);
588 static void mac_shutdown(void) {
590 #if !TARGET_RT_MAC_CFM
591 if (mac_gestalts
.encvvers
!= 0)
592 TerminateUnicodeConverter();
597 void fatalbox(char *fmt
, ...) {
602 /* We'd like stuff to be a Pascal string */
603 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
605 ParamText(stuff
, NULL
, NULL
, NULL
);
610 void modalfatalbox(char *fmt
, ...) {
615 /* We'd like stuff to be a Pascal string */
616 stuff
[0] = vsprintf((char *)(&stuff
[1]), fmt
, ap
);
618 ParamText(stuff
, NULL
, NULL
, NULL
);
625 * c-file-style: "simon"