-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui_main.cc
493 lines (417 loc) · 8.75 KB
/
ui_main.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*
* Copyright 2022 Trevor Gale
*
* This file is part of MacWords
*
* MacWords is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* MacWords is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with MacWords. If not,
* see <https://www.gnu.org/licenses/>.
*/
#include <MacWindows.h>
#include <Events.h>
#include <Dialogs.h>
#include <LowMem.h>
#include <ToolUtils.h>
#include <Sound.h>
#include <Devices.h>
#include <Menus.h>
#include <string.h>
#include <TextUtils.h>
#include <ctype.h>
#include <stdio.h>
#include "board.hh"
#include "windows.hh"
static const char programName[] = "MacWords";
static const short defaultMenubar = 128;
static const short appleMenu = 128;
static const short fileMenu = 129;
static const short editMenu = 130;
static const short gameMenu = 131;
static const short appleMenuAbout = 1;
static const short fileMenuQuit = 1;
static const short editMenuCopy = 1;
static const short editMenuClear = 2;
static const short gameMenuBegin = 1;
static const short gameMenuClose = 2;
#if __MWERKS__ == 0
QDGlobals qd;
#endif
void initialize();
void mainLoop();
void terminate();
void processMouseMenuEvent(long action);
void processKeyMenyEvent(char key);
void beginGame();
void newGame(WindowPtr window);
void clearEntry(WindowPtr window);
WindowPtr createAboutWindow();
void drawAboutWindow(WindowPtr window);
void closeAboutWinow(WindowPtr window);
void closeWindow(WindowPtr window);
static BOOL aboutWindowOpen;
void main()
{
initialize();
beginGame();
mainLoop();
terminate();
}
void initialize()
{
MaxApplZone();
InitGraf(&qd.thePort);
InitFonts();
InitWindows();
InitMenus();
InitDialogs(nil);
InitCursor();
aboutWindowOpen = FALSE;
FlushEvents(everyEvent, 0);
qd.randSeed = TickCount();
Handle menuBar = GetNewMBar(defaultMenubar);
SetMenuBar(menuBar);
AppendResMenu(GetMenuHandle(appleMenu), 'DRVR');
DrawMenuBar();
}
void mainLoop()
{
EventRecord event;
while (true)
{
if (WaitNextEvent(everyEvent, &event, 10L, nil))
{
if (event.what == mouseDown)
{
WindowPtr clickedWindow;
short clickedPort = FindWindow(event.where, &clickedWindow);
if (clickedPort == inMenuBar)
{
processMouseMenuEvent(MenuSelect(event.where));
}
else if (clickedPort == inDrag)
{
SelectWindow(clickedWindow);
DragWindow(clickedWindow, event.where, &qd.screenBits.bounds);
}
else if (clickedPort == inGoAway) {
if (TrackGoAway(clickedWindow, event.where)) {
if (clickedWindow)
{
closeWindow(clickedWindow);
}
}
}
else if (clickedPort == inContent)
{
if (clickedWindow != FrontWindow())
{
SelectWindow(clickedWindow);
}
else
{
if(clickedWindow)
{
Board* b = (Board*) GetWRefCon((WindowPtr) clickedWindow);
if (b == 0)
{
// Close the About Window on click
closeAboutWinow(clickedWindow);
}
// Process the click event
}
}
}
}
else if (event.what == updateEvt)
{
BeginUpdate((WindowPtr) event.message);
Board* b = (Board*) GetWRefCon((WindowPtr) event.message);
if (b == 0)
{
drawAboutWindow((WindowPtr) event.message);
}
else if(b->type == BoardWindow)
{
b->draw();
}
else if(b->type == ScoreWindow)
{
Score* s = (Score*)b;
s->draw();
}
EndUpdate((WindowPtr) event.message);
}
else if (event.what == keyDown)
{
if(((event.modifiers & cmdKey) != 0))
{
processKeyMenyEvent(tolower(LoWord(event.message)));
}
else
{
// Standard keyboard input
Board* b = (Board*) GetWRefCon((WindowPtr) FrontWindow());
if(b != NULL && b->type == BoardWindow)
{
char ch = LoWord(event.message);
ch = toupper(ch);
if (isalpha(ch) || ch == '\n' || ch == '\b')
{
b->process_key(ch);
b->draw();
}
}
}
}
}
}
}
void terminate()
{
ExitToShell();
}
void processMouseMenuEvent(long action)
{
if (action <= 0)
{
return;
}
short menu = HiWord(action);
short item = LoWord(action);
if (menu == appleMenu)
{
if (item == appleMenuAbout)
{
SysBeep(1);
WindowPtr aboutWindow = createAboutWindow();
drawAboutWindow(aboutWindow);
}
else
{
Str255 name;
GetMenuItemText(GetMenuHandle(menu), item, name);
OpenDeskAcc(name);
}
}
else if (menu == fileMenu)
{
HiliteMenu(fileMenu);
if (item == fileMenuQuit)
{
terminate();
}
}
else if (menu == editMenu)
{
HiliteMenu(editMenu);
if (item == editMenuCopy)
{
// Copy
SysBeep(1);
}
else if (item == editMenuClear)
{
// Clear
clearEntry(FrontWindow());
}
}
else if (menu == gameMenu)
{
HiliteMenu(gameMenu);
if (item == gameMenuBegin)
{
newGame(FrontWindow());
}
else if (item == gameMenuClose)
{
closeWindow(FrontWindow());
}
}
HiliteMenu(0);
}
void processKeyMenyEvent(char key)
{
if (key == 'q')
{
HiliteMenu(fileMenu);
terminate();
HiliteMenu(0);
}
else if (key == 'c')
{
// Do copy
}
else if (key == 'b')
{
HiliteMenu(gameMenu);
newGame(FrontWindow());
HiliteMenu(0);
}
else if (key == 'w')
{
WindowPtr window = FrontWindow();
HiliteMenu(gameMenu);
closeWindow(window);
HiliteMenu(0);
}
else if (key == 's') /* TODO: remove */
{
WindowPtr w = FrontWindow();
Board* board = (Board*)GetWRefCon(w);
if (board && board->type == BoardWindow)
{
board->createScoreWindow(TRUE);
}
}
}
void newGame(WindowPtr window)
{
if(!window)
{
return;
}
Board* board = (Board*)GetWRefCon(window);
if(board == 0)
{
return;
}
if(board->type != BoardWindow)
{
return;
}
board->newGame();
board->draw();
}
void clearEntry(WindowPtr window)
{
if(!window)
{
return;
}
Board* board = (Board*)GetWRefCon(window);
if(board == 0)
{
return;
}
if(board->type != BoardWindow)
{
return;
}
board->clear();
board->draw();
}
void beginGame()
{
WindowPtr window = GetNewCWindow(mainWindow, nil, (WindowPtr) -1);
Str255 title;
c2pstrcpy_cust(title, programName);
SetWTitle(window, title);
Board* board = new Board(window);
SetWRefCon(window, (long) board);
SelectWindow(window);
board->draw();
}
WindowPtr createAboutWindow()
{
if (aboutWindowOpen)
{
return NULL;
}
WindowPtr window = GetNewCWindow(aboutWindow, nil, (WindowPtr) -1);
SetWRefCon(window, (long) 0);
aboutWindowOpen = TRUE;
return window;
}
void drawAboutWindow(WindowPtr window)
{
if (!window || !aboutWindowOpen)
{
return;
}
SetPort(window);
RGBColor green;
green.red = 93 << 8;
green.green = 170 << 8;
green.blue = 107 << 8;
PixPatHandle greenPixPat = NewPixPat();
MakeRGBPat(greenPixPat, &green);
Rect r = window->portRect;
FillCRect(&r, greenPixPat);
RGBColor white;
white.red = 255 << 8;
white.green = 255 << 8;
white.blue = 255 << 8;
RGBForeColor(&white);
short fontSize = 18;
PenState oldState;
GetPenState(&oldState);
Style s = {0};
Str255 fontName;
short fontFamily = 0;
c2pstrcpy_cust(fontName, "geneva");
GetFNum(fontName, &fontFamily);
TextFont(fontFamily);
TextSize(fontSize);
PenState ps = {0};
ps.pnLoc.v = window->portRect.top + 20;
ps.pnLoc.h = window->portRect.left + 5;
ps.pnSize.v = 1;
ps.pnSize.h = 1;
SetPenState(&ps);
Str255 writeString;
c2pstrcpy_cust(writeString, programName);
DrawString(writeString);
c2pstrcpy_cust(writeString, " v1.0.1");
DrawString(writeString);
fontSize = 14;
TextSize(fontSize);
ps.pnLoc.v = ((window->portRect.bottom - window->portRect.top) / 2) + window->portRect.top;
ps.pnLoc.h = window->portRect.left + 5;
SetPenState(&ps);
c2pstrcpy_cust(writeString, "Copyright 2022 Trevor Gale");
DrawString(writeString);
c2pstrcpy_cust(writeString, "under the GNU GPL");
ps.pnLoc.v += 16;
ps.pnLoc.h = window->portRect.left + 5;
SetPenState(&ps);
DrawString(writeString);
DisposePixPat(greenPixPat);
}
void closeAboutWinow(WindowPtr window)
{
if (!window || !aboutWindowOpen)
{
return;
}
DisposeWindow(window);
aboutWindowOpen = FALSE;
}
void closeWindow(WindowPtr window)
{
long board = 0;
if (window)
{
board = GetWRefCon(window);
}
if (window && board && ((Board*)board)->type == BoardWindow)
{
terminate();
}
else if (window && board && ((Score*)board)->type == ScoreWindow)
{
Score* s = (Score*) board;
delete s;
DisposeWindow(window);
}
else if (window)
{
closeAboutWinow(window);
}
}