1 /* $Id: macpgen.c,v 1.2 2003/02/15 14:20:43 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_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 #pragma noreturn (cleanup_exit)
93 struct mac_windows windows;
95 struct mac_gestalts mac_gestalts;
97 int main (int argc, char **argv) {
103 #pragma noreturn (main)
105 static void mac_startup(void) {
109 #if !TARGET_API_MAC_CARBON
110 /* Init Memory Manager */
113 InitGraf(&qd.thePort);
114 /* Init Font Manager */
116 /* Init Window Manager */
118 /* Init Menu Manager */
122 /* Init Dialog Manager */
126 /* Get base system version (only used if there's no better selector) */
127 if (Gestalt(gestaltSystemVersion, &mac_gestalts.sysvers) != noErr ||
128 (mac_gestalts.sysvers &= 0xffff) < 0x700)
129 fatalbox("PuTTYgen requires System 7 or newer");
130 /* Find out if we've got Color Quickdraw */
131 if (Gestalt(gestaltQuickdrawVersion, &mac_gestalts.qdvers) != noErr)
132 mac_gestalts.qdvers = gestaltOriginalQD;
133 /* ... and the Appearance Manager? */
134 if (Gestalt(gestaltAppearanceVersion, &mac_gestalts.apprvers) != noErr)
135 if (Gestalt(gestaltAppearanceAttr, NULL) == noErr)
136 mac_gestalts.apprvers = 0x0100;
138 mac_gestalts.apprvers = 0;
139 #if TARGET_RT_MAC_CFM
140 /* Paranoia: Did we manage to pull in AppearanceLib? */
141 if (&RegisterAppearanceClient == kUnresolvedCFragSymbolAddress)
142 mac_gestalts.apprvers = 0;
145 mac_gestalts.cntlattr = 0;
146 mac_gestalts.windattr = 0;
148 /* Mac OS 8.5 Control Manager (proportional scrollbars)? */
149 if (Gestalt(gestaltControlMgrAttr, &mac_gestalts.cntlattr) != noErr ||
150 &SetControlViewSize == kUnresolvedCFragSymbolAddress)
151 mac_gestalts.cntlattr = 0;
152 /* Mac OS 8.5 Window Manager? */
153 if (Gestalt(gestaltWindowMgrAttr, &mac_gestalts.windattr) != noErr ||
154 &SetWindowContentColor == kUnresolvedCFragSymbolAddress)
155 mac_gestalts.windattr = 0;
157 /* Text Encoding Conversion Manager? */
159 #if TARGET_RT_MAC_CFM
160 &TECGetInfo == kUnresolvedCFragSymbolAddress ||
162 InitializeUnicodeConverter(NULL) != noErr ||
164 TECGetInfo(&ti) != noErr)
165 mac_gestalts.encvvers = 0;
167 mac_gestalts.encvvers = (*ti)->tecVersion;
168 mac_gestalts.uncvattr = (*ti)->tecUnicodeConverterFeatures;
169 DisposeHandle((Handle)ti);
171 /* Navigation Services? */
172 if (NavServicesAvailable())
173 mac_gestalts.navsvers = NavLibraryVersion();
175 mac_gestalts.navsvers = 0;
177 /* We've been tested with the Appearance Manager */
178 if (mac_gestalts.apprvers != 0)
179 RegisterAppearanceClient();
181 menuBar = GetNewMBar(128);
183 fatalbox("Unable to create menu bar.");
185 AppendResMenu(GetMenuHandle(mApple), 'DRVR');
189 windows.about = NULL;
190 windows.licence = NULL;
192 flags = FLAG_INTERACTIVE;
194 /* Install Apple Event handlers. */
196 AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
197 NewAEEventHandlerUPP(&mac_aevt_oapp), 0, FALSE);
198 AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
199 NewAEEventHandlerUPP(&mac_aevt_odoc), 0, FALSE);
200 AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
201 NewAEEventHandlerUPP(&mac_aevt_pdoc), 0, FALSE);
203 AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
204 NewAEEventHandlerUPP(&mac_aevt_quit), 0, FALSE);
207 static void mac_eventloop(void) {
214 mac_adjustcursor(cursrgn);
215 gotevent = WaitNextEvent(everyEvent, &event, LONG_MAX, cursrgn);
216 mac_adjustcursor(cursrgn);
225 static void mac_event(EventRecord *event) {
229 switch (event->what) {
231 part = FindWindow(event->where, &window);
235 mac_menucommand(MenuSelect(event->where));
237 #if !TARGET_API_MAC_CARBON
239 SystemClick(event, window);
243 if (window != FrontWindow())
244 /* XXX: check for movable modal dboxes? */
245 SelectWindow(window);
247 mac_contentclick(window, event);
250 if (TrackGoAway(window, event->where))
251 mac_closewindow(window);
254 /* XXX: moveable modal check? */
255 #if TARGET_API_MAC_CARBON
259 GetQDGlobalsScreenBits(&screenBits);
260 DragWindow(window, event->where, &screenBits.bounds);
263 DragWindow(window, event->where, &qd.screenBits.bounds);
267 mac_growwindow(window, event);
271 if (TrackBox(window, event->where, part))
272 mac_zoomwindow(window, part);
281 mac_activatewindow((WindowPtr)event->message, event);
284 mac_updatewindow((WindowPtr)event->message);
286 #if !TARGET_API_MAC_CARBON
288 if (HiWord(event->message) != noErr) {
291 SetPt(&pt, 120, 120);
292 DIBadMount(pt, event->message);
296 case kHighLevelEvent:
297 AEProcessAppleEvent(event); /* errors? */
302 static void mac_contentclick(WindowPtr window, EventRecord *event) {
306 switch (mac_windowtype(window)) {
308 dialog = GetDialogFromWindow(window);
309 if (DialogSelect(event, &dialog, &item))
319 static void mac_growwindow(WindowPtr window, EventRecord *event) {
321 switch (mac_windowtype(window)) {
325 static void mac_activatewindow(WindowPtr window, EventRecord *event) {
328 active = (event->modifiers & activeFlag) != 0;
330 switch (mac_windowtype(window)) {
332 mac_activateabout(window, event);
337 static void mac_activateabout(WindowPtr window, EventRecord *event) {
339 DialogItemType itemtype;
345 dialog = GetDialogFromWindow(window);
346 active = (event->modifiers & activeFlag) != 0;
347 GetDialogItem(dialog, wiAboutLicence, &itemtype, &itemhandle, &itemrect);
348 HiliteControl((ControlHandle)itemhandle, active ? 0 : 255);
349 DialogSelect(event, &dialog, &item);
352 static void mac_updatewindow(WindowPtr window)
354 #if TARGET_API_MAC_CARBON
358 switch (mac_windowtype(window)) {
361 #if TARGET_API_MAC_CARBON
363 GetPortVisibleRegion(GetWindowPort(window), rgn);
364 UpdateDialog(GetDialogFromWindow(window), rgn);
367 UpdateDialog(window, window->visRgn);
372 mac_updatelicence(window);
377 static void mac_updatelicence(WindowPtr window)
384 SetPort((GrafPtr)GetWindowPort(window));
386 fondsize = GetScriptVariable(smRoman, smScriptSmallFondSize);
387 TextFont(HiWord(fondsize));
388 TextSize(LoWord(fondsize));
389 h = Get1Resource('TEXT', wLicence);
390 len = GetResourceSizeOnDisk(h);
391 #if TARGET_API_MAC_CARBON
392 GetPortBounds(GetWindowPort(window), &textrect);
394 textrect = window->portRect;
398 TETextBox(*h, len, &textrect, teFlushDefault);
405 * Work out what kind of window we're dealing with.
407 static int mac_windowtype(WindowPtr window)
410 #if !TARGET_API_MAC_CARBON
411 if (GetWindowKind(window) < 0)
414 return ((WinInfo *)GetWRefCon(window))->wtype;
420 static void mac_keypress(EventRecord *event) {
423 window = FrontWindow();
424 if (event->what == keyDown && (event->modifiers & cmdKey)) {
426 mac_menucommand(MenuKey(event->message & charCodeMask));
428 switch (mac_windowtype(window)) {
433 static void mac_menucommand(long result) {
436 #if !TARGET_API_MAC_CARBON
440 menu = HiWord(result);
441 item = LoWord(result);
442 window = FrontWindow();
443 /* Things which do the same whatever window we're in. */
450 #if !TARGET_API_MAC_CARBON
452 GetMenuItemText(GetMenuHandle(mApple), item, da);
464 mac_closewindow(window);
472 /* If we get here, handling is up to window-specific code. */
473 switch (mac_windowtype(window)) {
479 static void mac_openabout(void) {
480 DialogItemType itemtype;
488 SelectWindow(windows.about);
491 GetDialogWindow(GetNewDialog(wAbout, NULL, (WindowPtr)-1));
492 wi = smalloc(sizeof(*wi));
495 SetWRefCon(windows.about, (long)wi);
496 vers = (VersRecHndl)Get1Resource('vers', 1);
497 if (vers != NULL && *vers != NULL) {
498 longvers = (*vers)->shortVersion + (*vers)->shortVersion[0] + 1;
499 GetDialogItem(GetDialogFromWindow(windows.about), wiAboutVersion,
500 &itemtype, &item, &box);
501 assert(itemtype & kStaticTextDialogItem);
502 SetDialogItemText(item, longvers);
504 ShowWindow(windows.about);
508 static void mac_openlicence(void) {
512 SelectWindow(windows.licence);
514 windows.licence = GetNewWindow(wLicence, NULL, (WindowPtr)-1);
515 wi = smalloc(sizeof(*wi));
517 wi->wtype = wLicence;
518 SetWRefCon(windows.licence, (long)wi);
519 ShowWindow(windows.licence);
523 static void mac_closewindow(WindowPtr window) {
525 switch (mac_windowtype(window)) {
526 #if !TARGET_API_MAC_CARBON
528 CloseDeskAcc(GetWindowKind(window));
532 windows.about = NULL;
533 DisposeDialog(GetDialogFromWindow(window));
536 windows.licence = NULL;
537 DisposeWindow(window);
542 static void mac_zoomwindow(WindowPtr window, short part) {
544 /* FIXME: do something */
548 * Make the menus look right before the user gets to see them.
550 #if TARGET_API_MAC_CARBON
551 #define EnableItem EnableMenuItem
552 #define DisableItem DisableMenuItem
554 static void mac_adjustmenus(void) {
558 window = FrontWindow();
559 menu = GetMenuHandle(mApple);
561 EnableItem(menu, iAbout);
563 menu = GetMenuHandle(mFile);
565 EnableItem(menu, iNew);
567 EnableItem(menu, iClose);
569 DisableItem(menu, iClose);
570 EnableItem(menu, iQuit);
572 switch (mac_windowtype(window)) {
574 DisableItem(menu, iSave);
575 DisableItem(menu, iSaveAs);
576 menu = GetMenuHandle(mEdit);
577 DisableItem(menu, 0);
578 menu = GetMenuHandle(mWindow);
579 DisableItem(menu, 0); /* Until we get more than 1 item on it. */
586 * Make sure the right cursor's being displayed.
588 static void mac_adjustcursor(RgnHandle cursrgn) {
590 WindowPtr window, front;
592 #if TARGET_API_MAC_CARBON
598 LocalToGlobal(&mouse);
599 part = FindWindow(mouse, &window);
600 front = FrontWindow();
601 if (part != inContent || window == NULL || window != front) {
602 /* Cursor isn't in the front window, so switch to arrow */
603 #if TARGET_API_MAC_CARBON
604 GetQDGlobalsArrow(&arrow);
607 SetCursor(&qd.arrow);
609 SetRectRgn(cursrgn, SHRT_MIN, SHRT_MIN, SHRT_MAX, SHRT_MAX);
611 #if TARGET_API_MAC_CARBON
613 GetPortVisibleRegion(GetWindowPort(front), visrgn);
614 DiffRgn(cursrgn, visrgn, cursrgn);
617 DiffRgn(cursrgn, front->visRgn, cursrgn);
621 switch (mac_windowtype(window)) {
623 #if TARGET_API_MAC_CARBON
624 GetQDGlobalsArrow(&arrow);
626 GetPortVisibleRegion(GetWindowPort(window), cursrgn);
628 SetCursor(&qd.arrow);
629 CopyRgn(window->visRgn, cursrgn);
636 pascal OSErr mac_aevt_quit(const AppleEvent *req, AppleEvent *reply,
642 if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard,
643 &type, NULL, 0, &size) == noErr)
644 return errAEParamMissed;
650 void cleanup_exit(int status)
653 #if !TARGET_RT_MAC_CFM
654 if (mac_gestalts.encvvers != 0)
655 TerminateUnicodeConverter();
662 * c-file-style: "simon"