1 /* $Id: macpgen.c,v 1.3 2003/02/16 14:27:37 ben Exp $ */
3 * Copyright (c) 1999, 2003 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
29 * macpgen.c - PuTTYgen for Mac OS
33 #include <AEDataModel.h>
34 #include <AppleEvents.h>
35 #include <Quickdraw.h>
37 #include <MacWindows.h>
40 #include <Appearance.h>
41 #include <CodeFragments.h>
47 #include <Navigation.h>
48 #include <Resources.h>
50 #include <TextCommon.h>
51 #include <ToolUtils.h>
52 #include <UnicodeConverter.h>
57 #include <stdlib.h> /* putty.h needs size_t */
58 #include <stdio.h> /* for vsprintf */
60 #define PUTTY_DO_GLOBALS
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_updatewindow(WindowPtr
);
75 static void mac_keypress(EventRecord
*);
76 static int mac_windowtype(WindowPtr
);
77 static void mac_menucommand(long);
78 static void mac_adjustcursor(RgnHandle
);
79 static void mac_adjustmenus(void);
80 static void mac_closewindow(WindowPtr
);
81 static void mac_zoomwindow(WindowPtr
, short);
82 #pragma noreturn (cleanup_exit)
89 struct mac_windows windows
;
91 struct mac_gestalts mac_gestalts
;
93 int main (int argc
, char **argv
) {
99 #pragma noreturn (main)
101 static void mac_startup(void) {
105 #if !TARGET_API_MAC_CARBON
106 /* Init Memory Manager */
109 InitGraf(&qd
.thePort
);
110 /* Init Font Manager */
112 /* Init Window Manager */
114 /* Init Menu Manager */
118 /* Init Dialog Manager */
122 /* Get base system version (only used if there's no better selector) */
123 if (Gestalt(gestaltSystemVersion
, &mac_gestalts
.sysvers
) != noErr
||
124 (mac_gestalts
.sysvers
&= 0xffff) < 0x700)
125 fatalbox("PuTTYgen requires System 7 or newer");
126 /* Find out if we've got Color Quickdraw */
127 if (Gestalt(gestaltQuickdrawVersion
, &mac_gestalts
.qdvers
) != noErr
)
128 mac_gestalts
.qdvers
= gestaltOriginalQD
;
129 /* ... and the Appearance Manager? */
130 if (Gestalt(gestaltAppearanceVersion
, &mac_gestalts
.apprvers
) != noErr
)
131 if (Gestalt(gestaltAppearanceAttr
, NULL
) == noErr
)
132 mac_gestalts
.apprvers
= 0x0100;
134 mac_gestalts
.apprvers
= 0;
135 #if TARGET_RT_MAC_CFM
136 /* Paranoia: Did we manage to pull in AppearanceLib? */
137 if (&RegisterAppearanceClient
== kUnresolvedCFragSymbolAddress
)
138 mac_gestalts
.apprvers
= 0;
141 mac_gestalts
.cntlattr
= 0;
142 mac_gestalts
.windattr
= 0;
144 /* Mac OS 8.5 Control Manager (proportional scrollbars)? */
145 if (Gestalt(gestaltControlMgrAttr
, &mac_gestalts
.cntlattr
) != noErr
||
146 &SetControlViewSize
== kUnresolvedCFragSymbolAddress
)
147 mac_gestalts
.cntlattr
= 0;
148 /* Mac OS 8.5 Window Manager? */
149 if (Gestalt(gestaltWindowMgrAttr
, &mac_gestalts
.windattr
) != noErr
||
150 &SetWindowContentColor
== kUnresolvedCFragSymbolAddress
)
151 mac_gestalts
.windattr
= 0;
153 /* Text Encoding Conversion Manager? */
155 #if TARGET_RT_MAC_CFM
156 &TECGetInfo
== kUnresolvedCFragSymbolAddress
||
158 InitializeUnicodeConverter(NULL
) != noErr
||
160 TECGetInfo(&ti
) != noErr
)
161 mac_gestalts
.encvvers
= 0;
163 mac_gestalts
.encvvers
= (*ti
)->tecVersion
;
164 mac_gestalts
.uncvattr
= (*ti
)->tecUnicodeConverterFeatures
;
165 DisposeHandle((Handle
)ti
);
167 /* Navigation Services? */
168 if (NavServicesAvailable())
169 mac_gestalts
.navsvers
= NavLibraryVersion();
171 mac_gestalts
.navsvers
= 0;
173 /* We've been tested with the Appearance Manager */
174 if (mac_gestalts
.apprvers
!= 0)
175 RegisterAppearanceClient();
177 menuBar
= GetNewMBar(128);
179 fatalbox("Unable to create menu bar.");
181 AppendResMenu(GetMenuHandle(mApple
), 'DRVR');
185 windows
.about
= NULL
;
186 windows
.licence
= NULL
;
188 flags
= FLAG_INTERACTIVE
;
190 /* Install Apple Event handlers. */
192 AEInstallEventHandler(kCoreEventClass
, kAEOpenApplication
,
193 NewAEEventHandlerUPP(&mac_aevt_oapp
), 0, FALSE
);
194 AEInstallEventHandler(kCoreEventClass
, kAEOpenDocuments
,
195 NewAEEventHandlerUPP(&mac_aevt_odoc
), 0, FALSE
);
196 AEInstallEventHandler(kCoreEventClass
, kAEPrintDocuments
,
197 NewAEEventHandlerUPP(&mac_aevt_pdoc
), 0, FALSE
);
199 AEInstallEventHandler(kCoreEventClass
, kAEQuitApplication
,
200 NewAEEventHandlerUPP(&mac_aevt_quit
), 0, FALSE
);
203 static void mac_eventloop(void) {
210 mac_adjustcursor(cursrgn
);
211 gotevent
= WaitNextEvent(everyEvent
, &event
, LONG_MAX
, cursrgn
);
212 mac_adjustcursor(cursrgn
);
221 static void mac_event(EventRecord
*event
) {
225 switch (event
->what
) {
227 part
= FindWindow(event
->where
, &window
);
231 mac_menucommand(MenuSelect(event
->where
));
233 #if !TARGET_API_MAC_CARBON
235 SystemClick(event
, window
);
239 if (window
!= FrontWindow())
240 /* XXX: check for movable modal dboxes? */
241 SelectWindow(window
);
243 mac_contentclick(window
, event
);
246 if (TrackGoAway(window
, event
->where
))
247 mac_closewindow(window
);
250 /* XXX: moveable modal check? */
251 #if TARGET_API_MAC_CARBON
255 GetQDGlobalsScreenBits(&screenBits
);
256 DragWindow(window
, event
->where
, &screenBits
.bounds
);
259 DragWindow(window
, event
->where
, &qd
.screenBits
.bounds
);
263 mac_growwindow(window
, event
);
267 if (TrackBox(window
, event
->where
, part
))
268 mac_zoomwindow(window
, part
);
277 mac_activatewindow((WindowPtr
)event
->message
, event
);
280 mac_updatewindow((WindowPtr
)event
->message
);
282 #if !TARGET_API_MAC_CARBON
284 if (HiWord(event
->message
) != noErr
) {
287 SetPt(&pt
, 120, 120);
288 DIBadMount(pt
, event
->message
);
292 case kHighLevelEvent
:
293 AEProcessAppleEvent(event
); /* errors? */
298 static void mac_contentclick(WindowPtr window
, EventRecord
*event
)
301 if (mac_wininfo(window
)->click
!= NULL
)
302 (*mac_wininfo(window
)->click
)(window
, event
);
305 static void mac_growwindow(WindowPtr window
, EventRecord
*event
)
308 if (mac_wininfo(window
)->grow
!= NULL
)
309 (*mac_wininfo(window
)->grow
)(window
, event
);
312 static void mac_activatewindow(WindowPtr window
, EventRecord
*event
)
316 if (mac_wininfo(window
)->activate
!= NULL
)
317 (*mac_wininfo(window
)->activate
)(window
, event
);
320 static void mac_updatewindow(WindowPtr window
)
323 if (mac_wininfo(window
)->update
!= NULL
)
324 (*mac_wininfo(window
)->update
)(window
);
328 * Work out what kind of window we're dealing with.
330 static int mac_windowtype(WindowPtr window
)
333 #if !TARGET_API_MAC_CARBON
334 if (GetWindowKind(window
) < 0)
337 return ((WinInfo
*)GetWRefCon(window
))->wtype
;
343 static void mac_keypress(EventRecord
*event
)
347 window
= FrontWindow();
348 if (event
->what
== keyDown
&& (event
->modifiers
& cmdKey
)) {
350 mac_menucommand(MenuKey(event
->message
& charCodeMask
));
352 if (mac_wininfo(window
)->key
!= NULL
)
353 (*mac_wininfo(window
)->key
)(window
, event
);
357 static void mac_menucommand(long result
)
361 #if !TARGET_API_MAC_CARBON
365 menu
= HiWord(result
);
366 item
= LoWord(result
);
367 window
= FrontWindow();
368 /* Things which do the same whatever window we're in. */
375 #if !TARGET_API_MAC_CARBON
377 GetMenuItemText(GetMenuHandle(mApple
), item
, da
);
389 mac_closewindow(window
);
397 /* If we get here, handling is up to window-specific code. */
398 if (mac_wininfo(window
)->menu
!= NULL
)
399 (*mac_wininfo(window
)->menu
)(window
, menu
, item
);
405 static void mac_closewindow(WindowPtr window
)
408 switch (mac_windowtype(window
)) {
409 #if !TARGET_API_MAC_CARBON
411 CloseDeskAcc(GetWindowKind(window
));
415 if (mac_wininfo(window
)->close
!= NULL
)
416 (*mac_wininfo(window
)->close
)(window
);
420 static void mac_zoomwindow(WindowPtr window
, short part
) {
422 /* FIXME: do something */
426 * Make the menus look right before the user gets to see them.
428 #if TARGET_API_MAC_CARBON
429 #define EnableItem EnableMenuItem
430 #define DisableItem DisableMenuItem
432 static void mac_adjustmenus(void) {
436 window
= FrontWindow();
437 menu
= GetMenuHandle(mApple
);
439 EnableItem(menu
, iAbout
);
441 menu
= GetMenuHandle(mFile
);
443 EnableItem(menu
, iNew
);
445 EnableItem(menu
, iClose
);
447 DisableItem(menu
, iClose
);
448 EnableItem(menu
, iQuit
);
450 if (mac_wininfo(window
)->adjustmenus
!= NULL
)
451 (*mac_wininfo(window
)->adjustmenus
)(window
);
453 DisableItem(menu
, iSave
);
454 DisableItem(menu
, iSaveAs
);
455 menu
= GetMenuHandle(mEdit
);
456 DisableItem(menu
, 0);
457 menu
= GetMenuHandle(mWindow
);
458 DisableItem(menu
, 0); /* Until we get more than 1 item on it. */
464 * Make sure the right cursor's being displayed.
466 static void mac_adjustcursor(RgnHandle cursrgn
)
469 WindowPtr window
, front
;
471 #if TARGET_API_MAC_CARBON
477 LocalToGlobal(&mouse
);
478 part
= FindWindow(mouse
, &window
);
479 front
= FrontWindow();
480 if (part
!= inContent
|| window
== NULL
|| window
!= front
) {
481 /* Cursor isn't in the front window, so switch to arrow */
482 #if TARGET_API_MAC_CARBON
483 GetQDGlobalsArrow(&arrow
);
486 SetCursor(&qd
.arrow
);
488 SetRectRgn(cursrgn
, SHRT_MIN
, SHRT_MIN
, SHRT_MAX
, SHRT_MAX
);
490 #if TARGET_API_MAC_CARBON
492 GetPortVisibleRegion(GetWindowPort(front
), visrgn
);
493 DiffRgn(cursrgn
, visrgn
, cursrgn
);
496 DiffRgn(cursrgn
, front
->visRgn
, cursrgn
);
500 if (mac_wininfo(window
)->adjustcursor
!= NULL
)
501 (*mac_wininfo(window
)->adjustcursor
)(window
, mouse
, cursrgn
);
503 #if TARGET_API_MAC_CARBON
504 GetQDGlobalsArrow(&arrow
);
506 GetPortVisibleRegion(GetWindowPort(window
), cursrgn
);
508 SetCursor(&qd
.arrow
);
509 CopyRgn(window
->visRgn
, cursrgn
);
515 pascal OSErr
mac_aevt_quit(const AppleEvent
*req
, AppleEvent
*reply
,
521 if (AEGetAttributePtr(req
, keyMissedKeywordAttr
, typeWildCard
,
522 &type
, NULL
, 0, &size
) == noErr
)
523 return errAEParamMissed
;
529 void cleanup_exit(int status
)
532 #if !TARGET_RT_MAC_CFM
533 if (mac_gestalts
.encvvers
!= 0)
534 TerminateUnicodeConverter();
541 * c-file-style: "simon"