Rather than increasing the size of my switch statements yet further, have
[u/mdw/putty] / mac / macabout.c
CommitLineData
f854b482 1/* $Id: macabout.c,v 1.1 2003/02/15 16:22:15 ben Exp $ */
2/*
3 * Copyright (c) 1999, 2002, 2003 Ben Harris
4 * All rights reserved.
5 *
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
13 * conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
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
25 * SOFTWARE.
26 */
27
28#include <MacTypes.h>
29#include <Dialogs.h>
30#include <MacWindows.h>
31#include <Resources.h>
32#include <Script.h>
33#include <ToolUtils.h>
34
35#include <assert.h>
36#include <stdlib.h>
37
38#include "putty.h"
39#include "mac.h"
40#include "macresid.h"
41
42static struct mac_windows {
43 WindowPtr about;
44 WindowPtr licence;
45} windows;
46
47static void mac_openlicence(void);
48
49static void mac_clickabout(WindowPtr window, EventRecord *event)
50{
51 short item;
52 DialogRef dialog;
53
54 dialog = GetDialogFromWindow(window);
55 if (DialogSelect(event, &dialog, &item))
56 switch (item) {
57 case wiAboutLicence:
58 mac_openlicence();
59 break;
60 }
61}
62
63static void mac_activateabout(WindowPtr window, EventRecord *event)
64{
65 DialogRef dialog;
66 DialogItemType itemtype;
67 Handle itemhandle;
68 short item;
69 Rect itemrect;
70 int active;
71
72 dialog = GetDialogFromWindow(window);
73 active = (event->modifiers & activeFlag) != 0;
74 GetDialogItem(dialog, wiAboutLicence, &itemtype, &itemhandle, &itemrect);
75 HiliteControl((ControlHandle)itemhandle, active ? 0 : 255);
76 DialogSelect(event, &dialog, &item);
77}
78
79static void mac_updateabout(WindowPtr window)
80{
81#if TARGET_API_MAC_CARBON
82 RgnHandle rgn;
83#endif
84
85 BeginUpdate(window);
86#if TARGET_API_MAC_CARBON
87 rgn = NewRgn();
88 GetPortVisibleRegion(GetWindowPort(window), rgn);
89 UpdateDialog(GetDialogFromWindow(window), rgn);
90 DisposeRgn(rgn);
91#else
92 UpdateDialog(window, window->visRgn);
93#endif
94 EndUpdate(window);
95}
96
97static void mac_updatelicence(WindowPtr window)
98{
99 Handle h;
100 int len;
101 long fondsize;
102 Rect textrect;
103
104 SetPort((GrafPtr)GetWindowPort(window));
105 BeginUpdate(window);
106 fondsize = GetScriptVariable(smRoman, smScriptSmallFondSize);
107 TextFont(HiWord(fondsize));
108 TextSize(LoWord(fondsize));
109 h = Get1Resource('TEXT', wLicence);
110 len = GetResourceSizeOnDisk(h);
111#if TARGET_API_MAC_CARBON
112 GetPortBounds(GetWindowPort(window), &textrect);
113#else
114 textrect = window->portRect;
115#endif
116 if (h != NULL) {
117 HLock(h);
118 TETextBox(*h, len, &textrect, teFlushDefault);
119 HUnlock(h);
120 }
121 EndUpdate(window);
122}
123
124void mac_openabout(void)
125{
126 DialogItemType itemtype;
127 Handle item;
128 VersRecHndl vers;
129 Rect box;
130 StringPtr longvers;
131 WinInfo *wi;
132
133 if (windows.about)
134 SelectWindow(windows.about);
135 else {
136 windows.about =
137 GetDialogWindow(GetNewDialog(wAbout, NULL, (WindowPtr)-1));
138 wi = smalloc(sizeof(*wi));
139 memset(wi, 0, sizeof(*wi));
140 wi->wtype = wAbout;
141 wi->update = &mac_updateabout;
142 wi->click = &mac_clickabout;
143 wi->activate = &mac_activateabout;
144 SetWRefCon(windows.about, (long)wi);
145 vers = (VersRecHndl)Get1Resource('vers', 1);
146 if (vers != NULL && *vers != NULL) {
147 longvers = (*vers)->shortVersion + (*vers)->shortVersion[0] + 1;
148 GetDialogItem(GetDialogFromWindow(windows.about), wiAboutVersion,
149 &itemtype, &item, &box);
150 assert(itemtype & kStaticTextDialogItem);
151 SetDialogItemText(item, longvers);
152 }
153 ShowWindow(windows.about);
154 }
155}
156
157static void mac_openlicence(void)
158{
159 WinInfo *wi;
160
161 if (windows.licence)
162 SelectWindow(windows.licence);
163 else {
164 windows.licence = GetNewWindow(wLicence, NULL, (WindowPtr)-1);
165 wi = smalloc(sizeof(*wi));
166 memset(wi, 0, sizeof(*wi));
167 wi->wtype = wLicence;
168 wi->update = &mac_updatelicence;
169 SetWRefCon(windows.licence, (long)wi);
170 ShowWindow(windows.licence);
171 }
172}
173