1 /* $Id: macpgen.c,v 1.1 2003/02/12 23:53:15 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("PuTTY 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);
461 mac_closewindow(window);
469 /* If we get here, handling is up to window-specific code. */
470 switch (mac_windowtype(window)) {
476 static void mac_openabout(void) {
477 DialogItemType itemtype;
485 SelectWindow(windows.about);
488 GetDialogWindow(GetNewDialog(wAbout, NULL, (WindowPtr)-1));
489 wi = smalloc(sizeof(*wi));
492 SetWRefCon(windows.about, (long)wi);
493 vers = (VersRecHndl)Get1Resource('vers', 1);
494 if (vers != NULL && *vers != NULL) {
495 longvers = (*vers)->shortVersion + (*vers)->shortVersion[0] + 1;
496 GetDialogItem(GetDialogFromWindow(windows.about), wiAboutVersion,
497 &itemtype, &item, &box);
498 assert(itemtype & kStaticTextDialogItem);
499 SetDialogItemText(item, longvers);
501 ShowWindow(windows.about);
505 static void mac_openlicence(void) {
509 SelectWindow(windows.licence);
511 windows.licence = GetNewWindow(wLicence, NULL, (WindowPtr)-1);
512 wi = smalloc(sizeof(*wi));
514 wi->wtype = wLicence;
515 SetWRefCon(windows.licence, (long)wi);
516 ShowWindow(windows.licence);
520 static void mac_closewindow(WindowPtr window) {
522 switch (mac_windowtype(window)) {
523 #if !TARGET_API_MAC_CARBON
525 CloseDeskAcc(GetWindowKind(window));
529 windows.about = NULL;
530 DisposeDialog(GetDialogFromWindow(window));
533 windows.licence = NULL;
534 DisposeWindow(window);
539 static void mac_zoomwindow(WindowPtr window, short part) {
541 /* FIXME: do something */
545 * Make the menus look right before the user gets to see them.
547 #if TARGET_API_MAC_CARBON
548 #define EnableItem EnableMenuItem
549 #define DisableItem DisableMenuItem
551 static void mac_adjustmenus(void) {
555 window = FrontWindow();
556 menu = GetMenuHandle(mApple);
558 EnableItem(menu, iAbout);
560 menu = GetMenuHandle(mFile);
562 EnableItem(menu, iNew);
564 EnableItem(menu, iClose);
566 DisableItem(menu, iClose);
567 EnableItem(menu, iQuit);
569 switch (mac_windowtype(window)) {
571 DisableItem(menu, iSave);
572 DisableItem(menu, iSaveAs);
573 menu = GetMenuHandle(mEdit);
574 DisableItem(menu, 0);
575 menu = GetMenuHandle(mWindow);
576 DisableItem(menu, 0); /* Until we get more than 1 item on it. */
583 * Make sure the right cursor's being displayed.
585 static void mac_adjustcursor(RgnHandle cursrgn) {
587 WindowPtr window, front;
589 #if TARGET_API_MAC_CARBON
595 LocalToGlobal(&mouse);
596 part = FindWindow(mouse, &window);
597 front = FrontWindow();
598 if (part != inContent || window == NULL || window != front) {
599 /* Cursor isn't in the front window, so switch to arrow */
600 #if TARGET_API_MAC_CARBON
601 GetQDGlobalsArrow(&arrow);
604 SetCursor(&qd.arrow);
606 SetRectRgn(cursrgn, SHRT_MIN, SHRT_MIN, SHRT_MAX, SHRT_MAX);
608 #if TARGET_API_MAC_CARBON
610 GetPortVisibleRegion(GetWindowPort(front), visrgn);
611 DiffRgn(cursrgn, visrgn, cursrgn);
614 DiffRgn(cursrgn, front->visRgn, cursrgn);
618 switch (mac_windowtype(window)) {
620 #if TARGET_API_MAC_CARBON
621 GetQDGlobalsArrow(&arrow);
623 GetPortVisibleRegion(GetWindowPort(window), cursrgn);
625 SetCursor(&qd.arrow);
626 CopyRgn(window->visRgn, cursrgn);
633 pascal OSErr mac_aevt_quit(const AppleEvent *req, AppleEvent *reply,
639 if (AEGetAttributePtr(req, keyMissedKeywordAttr, typeWildCard,
640 &type, NULL, 0, &size) == noErr)
641 return errAEParamMissed;
647 void cleanup_exit(int status)
650 #if !TARGET_RT_MAC_CFM
651 if (mac_gestalts.encvvers != 0)
652 TerminateUnicodeConverter();
659 * c-file-style: "simon"