| 1 |
/***** |
| 2 |
* TWindow.cp |
| 3 |
* |
| 4 |
* Copyright © 1991 Symantec Corporation. All rights reserved. |
| 5 |
* |
| 6 |
* Generic window class to handle most window actions. |
| 7 |
* This class is intended to demonstrate the new |
| 8 |
* object-oriented features of THINK C 5.0. It is not |
| 9 |
* intended to be a general purpose window class for use |
| 10 |
* in commerical-quality applications. |
| 11 |
* |
| 12 |
* We store the object in the window's refCon and set |
| 13 |
* the windowKind to TWINDOWKIND so we can tell whether |
| 14 |
* there's an object in it. |
| 15 |
* |
| 16 |
*****/ |
| 17 |
|
| 18 |
#include <oops.h> |
| 19 |
#include "TWindow.h" |
| 20 |
|
| 21 |
// Prototypes for utility functions. |
| 22 |
|
| 23 |
static void EraseScrollBars (WindowPtr theWindow); |
| 24 |
static void InvalScrollBars (WindowPtr theWindow); |
| 25 |
static void ValidScrollBars (WindowPtr theWindow); |
| 26 |
|
| 27 |
short TWindow::windowCounter = 0; // the window counter that this class uses to |
| 28 |
// stack its windows |
| 29 |
|
| 30 |
|
| 31 |
/**** |
| 32 |
* TWindow constructor |
| 33 |
* |
| 34 |
* Create a new window and place it in an |
| 35 |
* attractive place on the screen. |
| 36 |
* |
| 37 |
****/ |
| 38 |
|
| 39 |
TWindow::TWindow(void) |
| 40 |
{ |
| 41 |
Rect myRect; |
| 42 |
short offset; |
| 43 |
Point originPt; |
| 44 |
|
| 45 |
theWindow = GetNewCWindow(TWinRSRC, NULL, (void *) -1L); |
| 46 |
drag = screenBits.bounds; |
| 47 |
SetRect(&smallest, 60, 60, 9999, 9999); |
| 48 |
|
| 49 |
((WindowPeek) theWindow)->windowKind = TWINDOWKIND; |
| 50 |
SetWRefCon(theWindow, (long) this); |
| 51 |
SetPort(theWindow); |
| 52 |
|
| 53 |
// Place this window in some attractive place |
| 54 |
// on the screen. TWindow uses the class variable |
| 55 |
// windowCounter to keep track of how many windows it |
| 56 |
// has stacked, but subclasses can access this variable |
| 57 |
// through the GetWindowNumber() method to create |
| 58 |
// numbered windows. |
| 59 |
|
| 60 |
originPt = topLeft(theWindow->portRect); |
| 61 |
LocalToGlobal(&originPt); |
| 62 |
offset = ((windowCounter) % 7) * 20; |
| 63 |
MoveWindow(theWindow, originPt.h+offset, originPt.v+offset, true); |
| 64 |
windowCounter++; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
/**** |
| 69 |
* TWindow destructor |
| 70 |
* |
| 71 |
* Dispose of the window. |
| 72 |
* |
| 73 |
****/ |
| 74 |
|
| 75 |
TWindow::~TWindow(void) |
| 76 |
{ |
| 77 |
DisposeWindow(theWindow); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
/**** |
| 82 |
* Close |
| 83 |
* |
| 84 |
* Close a window. |
| 85 |
* This method merely deletes the object. |
| 86 |
* |
| 87 |
****/ |
| 88 |
|
| 89 |
void TWindow::Close() |
| 90 |
{ |
| 91 |
delete this; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
/**** |
| 97 |
* GetWindowCounter |
| 98 |
* |
| 99 |
* Return the current value of the windowCounter |
| 100 |
* instance variable. |
| 101 |
* |
| 102 |
****/ |
| 103 |
|
| 104 |
short TWindow::GetWindowNumber (void) |
| 105 |
|
| 106 |
{ |
| 107 |
return windowCounter; |
| 108 |
} |
| 109 |
|
| 110 |
/**** |
| 111 |
* Draw |
| 112 |
* |
| 113 |
* Draw the contents of the window. |
| 114 |
* This method sets the foreground and background |
| 115 |
* color, erases the window, draws the scroll area |
| 116 |
* in white, and finally draws the grow icon. |
| 117 |
* |
| 118 |
****/ |
| 119 |
|
| 120 |
void TWindow::Draw() |
| 121 |
{ |
| 122 |
EraseRect(&theWindow->portRect); |
| 123 |
BackColor(whiteColor); |
| 124 |
EraseScrollBars(theWindow); |
| 125 |
DrawGrowIcon(theWindow); |
| 126 |
|
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
void TWindow::Hit(Point where) |
| 131 |
{ |
| 132 |
// Subclasses must do something here |
| 133 |
} |
| 134 |
|
| 135 |
/**** |
| 136 |
* Grow |
| 137 |
* |
| 138 |
* Grow the window. |
| 139 |
* If the window actually changed size, erase the |
| 140 |
* scroll bar areas. Return the new size of the window. |
| 141 |
* |
| 142 |
****/ |
| 143 |
|
| 144 |
long TWindow::Grow (Point where) |
| 145 |
{ |
| 146 |
long size; |
| 147 |
Rect smallest; |
| 148 |
|
| 149 |
smallest = this->smallest; // can't pass the address of |
| 150 |
// and instance variable to |
| 151 |
// a Toolbox routine that may |
| 152 |
// move memory. |
| 153 |
|
| 154 |
SetPort(theWindow); |
| 155 |
size = GrowWindow(theWindow, where, &smallest); |
| 156 |
|
| 157 |
if (size) { |
| 158 |
EraseScrollBars(theWindow); |
| 159 |
RefreshWindow(true); // force whoe window to be redrawn |
| 160 |
// the "true" param to SizeWindow |
| 161 |
// accumulates the new area into |
| 162 |
// the update region |
| 163 |
|
| 164 |
SizeWindow(theWindow, LoWord(size), HiWord(size), true); |
| 165 |
} |
| 166 |
|
| 167 |
return (size); |
| 168 |
} |
| 169 |
|
| 170 |
/**** |
| 171 |
* Zoom |
| 172 |
* |
| 173 |
* Zoom a window in reponse to a click in the zoom box. |
| 174 |
* |
| 175 |
****/ |
| 176 |
|
| 177 |
void TWindow::Zoom (short part) |
| 178 |
{ |
| 179 |
Rect myRect; |
| 180 |
|
| 181 |
SetPort(theWindow); |
| 182 |
EraseRect(&theWindow->portRect); |
| 183 |
ZoomWindow(theWindow, part, true); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
/**** |
| 188 |
* Update |
| 189 |
* |
| 190 |
* Update a window. |
| 191 |
* The usual thing to do here is to call the Draw() |
| 192 |
* method. |
| 193 |
* |
| 194 |
****/ |
| 195 |
|
| 196 |
void TWindow::Update(void) |
| 197 |
{ |
| 198 |
WindowPtr savePort; |
| 199 |
|
| 200 |
GetPort(&savePort); |
| 201 |
SetPort(theWindow); |
| 202 |
BeginUpdate(theWindow); |
| 203 |
Draw(); |
| 204 |
EndUpdate(theWindow); |
| 205 |
SetPort(savePort); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
/**** |
| 210 |
* Activate |
| 211 |
* |
| 212 |
* Respond to activate events. |
| 213 |
* All we really have to do is make sure that the |
| 214 |
* grow icon is drawn correctly. Subclasses that |
| 215 |
* want to do something special when deactivate should |
| 216 |
* override this method. |
| 217 |
* |
| 218 |
****/ |
| 219 |
|
| 220 |
void TWindow::Activate (short active) |
| 221 |
{ |
| 222 |
DrawGrowIcon(theWindow); // DrawGrowIcon knows how to draw the icon |
| 223 |
// for inactive & active windows |
| 224 |
|
| 225 |
if (active) |
| 226 |
SetPort(theWindow); |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
/**** |
| 232 |
* Draw |
| 233 |
* Select |
| 234 |
* Show |
| 235 |
* Hide |
| 236 |
* TrackClose |
| 237 |
* TrackZoom |
| 238 |
* IsVisible |
| 239 |
* GetWindowTitle |
| 240 |
* SetWindowTitle |
| 241 |
* |
| 242 |
* Various OOP interfaces to the Toolbox routines. |
| 243 |
* |
| 244 |
****/ |
| 245 |
|
| 246 |
|
| 247 |
void TWindow::Drag(Point where) |
| 248 |
{ |
| 249 |
DragWindow(theWindow, where, &drag); |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
void TWindow::Select(void) |
| 254 |
{ |
| 255 |
SelectWindow(theWindow); |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
void TWindow::Show(void) |
| 260 |
{ |
| 261 |
ShowWindow(theWindow); |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
void TWindow::Hide(void) |
| 266 |
{ |
| 267 |
HideWindow(theWindow); |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
void TWindow::TrackClose (Point where) |
| 272 |
{ |
| 273 |
if (TrackGoAway(theWindow, where)) |
| 274 |
Close(); |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
void TWindow::TrackZoom(Point where, short part) |
| 279 |
{ |
| 280 |
if (TrackBox(theWindow, where, part)) |
| 281 |
Zoom(part); |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
short TWindow::IsVisible() |
| 286 |
{ |
| 287 |
return ((WindowPeek) theWindow)->visible; |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
void TWindow::GetWindowTitle (Str255 theTitle) |
| 293 |
{ |
| 294 |
GetWTitle(theWindow, theTitle); |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
void TWindow::SetWindowTitle (Str255 theTitle) |
| 299 |
{ |
| 300 |
SetWTitle(theWindow, theTitle); |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
/**** |
| 305 |
* GetWindowRect |
| 306 |
* |
| 307 |
* Return the windows rectangle. |
| 308 |
* This method is provided so subclasses don't have |
| 309 |
* to go poking into the instance variables. |
| 310 |
* |
| 311 |
****/ |
| 312 |
|
| 313 |
void TWindow::GetWindowRect (Rect *theRect, Boolean entireWindow) |
| 314 |
{ |
| 315 |
*theRect = theWindow->portRect; |
| 316 |
if (!entireWindow) { |
| 317 |
theRect->top += 1; |
| 318 |
theRect->left += 1; |
| 319 |
theRect->right -= 16; |
| 320 |
theRect->bottom -= 16; |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
/**** |
| 326 |
* RefreshWindow |
| 327 |
* |
| 328 |
* Force a window to be redrawn on the next udpate event. |
| 329 |
* The Boolean parameter determines whether the scroll bars |
| 330 |
* should be redrawn or not. |
| 331 |
* |
| 332 |
****/ |
| 333 |
|
| 334 |
void TWindow::RefreshWindow (Boolean wholeWindow) |
| 335 |
{ |
| 336 |
InvalRect(&theWindow->portRect); |
| 337 |
if (!wholeWindow) |
| 338 |
ValidScrollBars(theWindow); |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
/**** |
| 344 |
* Zoom |
| 345 |
* |
| 346 |
****/ |
| 347 |
|
| 348 |
void TWindow::ZoomContent(short z) |
| 349 |
{ |
| 350 |
// Child will do |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
/*** |
| 355 |
* Utility routines to validate & inval the scroll bar rectangles. |
| 356 |
* |
| 357 |
***/ |
| 358 |
|
| 359 |
|
| 360 |
typedef enum { kInval, kValid, kErase } sbarAction; |
| 361 |
|
| 362 |
static void xScrollBars (WindowPtr theWindow, sbarAction action) |
| 363 |
{ |
| 364 |
Rect botRect; |
| 365 |
Rect rightRect; |
| 366 |
|
| 367 |
rightRect = botRect = theWindow->portRect; |
| 368 |
|
| 369 |
botRect.top = botRect.bottom-15; // The bottom scroll area |
| 370 |
|
| 371 |
rightRect.left = rightRect.right-15; // The right scroll area |
| 372 |
|
| 373 |
switch (action) { |
| 374 |
case kInval: InvalRect(&botRect); |
| 375 |
InvalRect(&rightRect); |
| 376 |
break; |
| 377 |
|
| 378 |
case kValid: ValidRect(&botRect); |
| 379 |
ValidRect(&rightRect); |
| 380 |
break; |
| 381 |
|
| 382 |
case kErase: EraseRect(&botRect); |
| 383 |
EraseRect(&rightRect); |
| 384 |
break; |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
static void InvalScrollBars (WindowPtr theWindow) |
| 389 |
{ |
| 390 |
xScrollBars(theWindow, kInval); |
| 391 |
} |
| 392 |
|
| 393 |
static void ValidScrollBars (WindowPtr theWindow) |
| 394 |
{ |
| 395 |
xScrollBars(theWindow, kValid); |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
static void EraseScrollBars (WindowPtr theWindow) |
| 400 |
{ |
| 401 |
xScrollBars(theWindow, kErase); |
| 402 |
} |