1 /* mac/machelp.c -- support code for mac like help system
2
3 Copyright (c) 1989-1991 Curtis McCauley, James E. Wilson
4
5 This software may be copied and distributed for educational, research, and
6 not for profit purposes provided that this copyright and statement are
7 included in all such copies. */
8
9 #ifndef THINK_C
10 #include <Types.h>
11 #include <Resources.h>
12 #include <Events.h>
13 #include <Quickdraw.h>
14 #include <TextEdit.h>
15 #include <Controls.h>
16 #include <Dialogs.h>
17
18 #include <ScrnMgr.h>
19 #else
20 #include <stddef.h>
21 #include "ScrnMgr.h"
22 #endif
23
24 #include "config.h"
25 #include "constant.h"
26 #include "types.h"
27
28 #define textType 'TEXT'
29 #define textID 512
30
31 #define macHelpDlgID 512
32 #define dfltBorder 2
33 #define textBox 3
34 #define scrollBar 4
35
36 #define codeEnter 0x03
37 #define codeReturn 0x0D
38
39 #ifdef THINK_C
40 /* Cover up error in THINK C library. */
41 #define ok OK
42 #define cancel Cancel
43 #endif
44
45 static ControlHandle okButton;
46
47 static ControlHandle myScrollBar;
48 static short scrollMin, scrollMax;
49 static short pageLines;
50
51 static TEHandle myTEHandle;
52
ActOnScroll(theControl,partCode)53 static pascal void ActOnScroll(theControl, partCode)
54 ControlHandle theControl;
55 short partCode;
56
57 {
58 short delta;
59 short newVal, origVal;
60
61 if (theControl != myScrollBar) return;
62
63 switch (partCode) {
64 case inUpButton:
65 delta = -1;
66 break;
67 case inDownButton:
68 delta = 1;
69 break;
70 case inPageUp:
71 delta = -pageLines;
72 break;
73 case inPageDown:
74 delta = pageLines;
75 break;
76 default:
77 delta = 0;
78 break;
79 }
80
81 if (delta) {
82 origVal = GetCtlValue(myScrollBar);
83 newVal = origVal + delta;
84 if (newVal < scrollMin) newVal = scrollMin;
85 if (newVal > scrollMax) newVal = scrollMax;
86 if (newVal != origVal) {
87 delta = newVal - origVal;
88 SetCtlValue(myScrollBar, newVal);
89 TEScroll(0, -delta * (*myTEHandle)->lineHeight, myTEHandle);
90 }
91 }
92
93 return;
94 }
95
DoHScroll(where,part)96 static void DoHScroll(where, part)
97 Point where;
98 short part;
99
100 {
101 short origVal, newVal;
102
103 switch (part) {
104
105 case inUpButton:
106 case inDownButton:
107 case inPageUp:
108 case inPageDown:
109 TrackControl(myScrollBar, where, (ProcPtr) ActOnScroll);
110 break;
111
112 case inThumb:
113 origVal = GetCtlValue(myScrollBar);
114 TrackControl(myScrollBar, where, NULL);
115 newVal = GetCtlValue(myScrollBar);
116 if (newVal != origVal)
117 TEScroll(0, (origVal - newVal) * (*myTEHandle)->lineHeight, myTEHandle);
118 break;
119
120 }
121
122 return;
123 }
124
MacHelpFilter(theDialog,theEvent,itemHit)125 static pascal Boolean MacHelpFilter(theDialog, theEvent, itemHit)
126 DialogPtr theDialog;
127 EventRecord *theEvent;
128 short *itemHit;
129
130 {
131 GrafPtr oldPort;
132 Point localPt;
133 WindowPtr theWindow;
134 ControlHandle theControl;
135 Boolean handled;
136 char key;
137 short part;
138
139 handled = false;
140
141 switch (theEvent->what) {
142
143 case keyDown:
144 key = theEvent->message & charCodeMask;
145 if ( (key == codeEnter) || (key == codeReturn) ) {
146 handled = true;
147 HiliteControl(okButton, inButton);
148 *itemHit = ok;
149 }
150 break;
151
152 case mouseDown:
153 part = FindWindow(theEvent->where, &theWindow);
154 if ( (theWindow == (WindowPtr) theDialog) && (part == inContent) ) {
155 GetPort(&oldPort);
156 SetPort((GrafPtr) theWindow);
157 localPt = theEvent->where;
158 GlobalToLocal(&localPt);
159 part = FindControl(localPt, theWindow, &theControl);
160 if (theControl == myScrollBar) {
161 handled = true;
162 DoHScroll(localPt, part);
163 *itemHit = scrollBar;
164 }
165 SetPort(oldPort);
166 }
167 break;
168
169 }
170
171 return(handled);
172 }
173
DrawTextBox(theWindow,theItem)174 static pascal void DrawTextBox(theWindow, theItem)
175 WindowPtr theWindow;
176 short theItem;
177
178 {
179 short itsType;
180 Handle itsHandle;
181 Rect itsRect;
182
183 GetDItem((DialogPtr) theWindow, theItem, &itsType, &itsHandle, &itsRect);
184 FrameRect(&itsRect);
185
186 TEUpdate(&theWindow->portRect, myTEHandle);
187
188 return;
189 }
190
DoMacHelp()191 void DoMacHelp()
192
193 {
194 GrafPtr oldPort;
195 DialogPtr theDialog;
196 Handle itsHandle;
197 Handle textHandle;
198 Handle versionHandle;
199 short itemHit;
200 short itsType;
201 Rect itsRect;
202 Rect textRect;
203 int32 h, v;
204
205 theDialog = GetNewDialog(macHelpDlgID, nil, (WindowPtr) -1);
206
207 GetPort(&oldPort);
208 SetPort((GrafPtr) theDialog);
209
210 CenterScreenDLOG(macHelpDlgID, fixHalf, fixThird, &h, &v);
211 MoveWindow((WindowPtr) theDialog, (short) h, (short) v, false);
212
213 GetDItem(theDialog, ok, &itsType, (Handle *) &okButton, &itsRect);
214 InsetRect(&itsRect, -4, -4);
215
216 SetDItem(theDialog, dfltBorder, userItem, (Handle) DrawDefaultBorder,
217 &itsRect);
218
219 GetDItem(theDialog, scrollBar, &itsType, (Handle *) &myScrollBar, &itsRect);
220
221 GetDItem(theDialog, textBox, &itsType, &itsHandle, &textRect);
222 SetDItem(theDialog, textBox, userItem, (Handle) DrawTextBox, &textRect);
223 InsetRect(&textRect, 4, 4);
224
225 myTEHandle = TENew(&textRect, &textRect);
226
227 textHandle = GetResource(textType, textID);
228 DetachResource(textHandle);
229
230 (*myTEHandle)->hText = textHandle;
231 (*myTEHandle)->txFont = monaco;
232 (*myTEHandle)->txSize = 9;
233 TECalText(myTEHandle);
234
235 pageLines = (textRect.bottom - textRect.top) / (*myTEHandle)->lineHeight;
236
237 scrollMin = 0;
238 scrollMax = (*myTEHandle)->nLines - pageLines;
239 if (scrollMax < 0) scrollMax = 0;
240
241 SetCtlMin(myScrollBar, scrollMin);
242 SetCtlMax(myScrollBar, scrollMax);
243
244 /* Get the version string. */
245 versionHandle = GetResource(MORIA_FCREATOR, 0);
246 HLock(versionHandle);
247 ParamText(*versionHandle, NULL, NULL, NULL);
248
249 ShowWindow((WindowPtr) theDialog);
250
251 do {
252 ModalDialog(MacHelpFilter, &itemHit);
253 } while (itemHit != ok);
254
255 HUnlock(versionHandle);
256 TEDispose(myTEHandle);
257
258 SetPort(oldPort);
259
260 DisposDialog(theDialog);
261
262 return;
263 }
264