-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdlgedit.c
1012 lines (950 loc) · 24.9 KB
/
dlgedit.c
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* A simple dialog editor */
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <commdlg.h>
#include <stdio.h>
#include <string.h>
#include "xmalloc.h"
#include "tmplparser.h"
#include "graphhit.h"
#include "ufsys.h"
#include "resource.h"
/* Microsoft Visual C++ memory leak detection. (This program has NO
memory leaks, but it is here just to be on the safe side.) */
#ifdef _DEBUG
#include <crtdbg.h>
#endif
/* MSVC >= 8.0 pragmas */
#if _MSC_VER >= 1400
#pragma warning (disable: 4996) /* Disable deprecate */
#pragma warning (disable: 4267) /* Disable size_t warnings */
#endif
/* Windows variables */
HINSTANCE g_hInstance;
HWND textHwnd; /* Container window for text editor */
HWND textBox; /* Actual text edit control */
HFONT teFont = NULL; /* Text edit window font */
LRESULT CALLBACK MainWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam,
LPARAM lParam);
LRESULT CALLBACK TextWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam,
LPARAM lParam);
INT_PTR CALLBACK AboutBoxProc(HWND hDlg, UINT uMsg, WPARAM wParam,
LPARAM lParam);
BOOL LoadDialogTemplate(char* filename, BOOL useNewTmpl);
BOOL SaveDialogTemplate(char* filename);
void SetDlgHFont(HDC hDC);
void UpdateTextWindow();
void KbdMoveProc(HWND hwnd, WPARAM wParam);
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst,
LPSTR lpCmdLine, int nShowCmd)
{
HWND hwnd; /* Window handle */
WNDCLASSEX wcex; /* Window class */
MSG msg; /* Message structure */
HACCEL hAccel; /* Accelerator table */
/* Register the main window class */
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = MainWindowProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)MAIN_FRAME);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCTSTR)MAIN_FRAME;
wcex.lpszClassName = "mainWindow";
wcex.hIconSm = NULL;
if (!RegisterClassEx(&wcex))
return 0;
/* Create a window class for the text editor window */
wcex.lpfnWndProc = TextWindowProc;
wcex.hbrBackground = NULL;
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "DETextWindow";
if (!RegisterClassEx(&wcex))
return 0;
/* Create a pseudo-window class for drawing the title bar of the dialog */
wcex.lpfnWndProc = DefWindowProc;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hbrBackground = NULL;
wcex.lpszClassName = "DEPseudoWindow";
if (!RegisterClassEx(&wcex))
return 0;
/* Load the accelerator table */
hAccel = LoadAccelerators(hInstance, (LPCTSTR)MAIN_FRAME);
/* Globally save the application instance */
g_hInstance = hInstance;
/* Create the main window */
hwnd = CreateWindowEx(0, "mainWindow", "Dialog Editor",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, /* window position does not matter */
CW_USEDEFAULT, CW_USEDEFAULT, /* window size does not matter */
NULL, 0, hInstance, NULL);
if (!hwnd)
{
MessageBox(NULL, "Error creating main application window.", NULL,
MB_OK | MB_ICONERROR);
return 0;
}
/* Show the text window on startup */
textHwnd = CreateWindowEx(0, "DETextWindow", "Text Window",
WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_CLIPCHILDREN,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, 0, hInstance, NULL);
if (!textHwnd)
{
MessageBox(NULL, "Error creating application text window.", NULL,
MB_OK | MB_ICONERROR);
return 0;
}
/* Show and draw (update) the window */
ShowWindow(hwnd, nShowCmd);
UpdateWindow(hwnd);
/* Message loop */
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(hwnd, hAccel, &msg))
TranslateMessage(&msg);
DispatchMessage(&msg);
}
#ifdef _DEBUG
_CrtDumpMemoryLeaks();
#endif
return (int)msg.wParam;
}
LRESULT CALLBACK MainWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam,
LPARAM lParam)
{
static HWND pseudoHwnd; /* Used to draw the dialog title bar */
static BOOL firstSize = TRUE;
switch (uMsg)
{
case WM_CREATE:
{
HDC hDC;
dlgFont = NULL;
/* Create a hidden pseudo-window (for drawing the caption) */
dlgHasCaption = TRUE;
pseudoHwnd = CreateWindowEx(0, "DEPseudoWindow", "Error",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, g_hInstance, NULL);
LoadDialogTemplate(curFilename, TRUE);
if (dlgHasCaption == TRUE)
SetWindowText(pseudoHwnd, dlgCaption);
hDC = GetDC(hwnd);
SetDlgHFont(hDC);
UpdateFont(hDC);
ReleaseDC(hwnd, hDC);
break;
}
case WM_CLOSE:
if (!AskForSave(hwnd))
return 0;
break;
case WM_DESTROY:
if (textHwnd != NULL)
DestroyWindow(textHwnd);
DestroyWindow(pseudoHwnd);
DeleteObject(dlgFont);
FreeDlgData();
PostQuitMessage(0);
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hDC;
RECT rt;
HBRUSH hBr;
hDC = BeginPaint(hwnd, &ps);
SelectObject(hDC, dlgFont);
{
POINT scPos;
scPos.x = DLG2SCR_X(dlgPos.x);
scPos.y = DLG2SCR_Y(dlgPos.y);
if (dlgHasCaption == TRUE)
scPos.y += GetSystemMetrics(SM_CYCAPTION);
SetViewportOrgEx(hDC, scPos.x, scPos.y, NULL);
}
/* Draw the caption (if visible) */
if (dlgHasCaption == TRUE)
{
rt.left = 0;
rt.top = 0 - GetSystemMetrics(SM_CYCAPTION);
rt.bottom = 0;
rt.right = DLG2SCR_X(dlgWidth);
DrawCaption(pseudoHwnd, hDC, &rt, DC_ACTIVE | DC_ICON | DC_TEXT);
/* To keep things simple, don't draw the system buttons */
/*rt.left = rt.right - GetSystemMetrics(SM_CXSIZE) -
GetSystemMetrics(SM_CXSIZEFRAME);
rt.top += GetSystemMetrics(SM_CYSIZEFRAME);
rt.right -= GetSystemMetrics(SM_CXSIZEFRAME);
rt.bottom = rt.top + GetSystemMetrics(SM_CYSIZE);
DrawFrameControl(hDC, &rt, DFC_CAPTION, DFCS_CAPTIONCLOSE);*/
}
/* Draw the dialog client area */
hBr = GetSysColorBrush(COLOR_3DFACE);
rt.left = 0;
rt.top = 0;
rt.right = DLG2SCR_X(dlgWidth);
rt.bottom = DLG2SCR_Y(dlgHeight);
FillRect(hDC, &rt, hBr);
if (activeCtrl == -1 && clickHit == FALSE)
{
/* Draw the selection around the dialog */
if (dlgHasCaption == TRUE)
rt.top -= GetSystemMetrics(SM_CYCAPTION);
DrawSelRect(hDC, &rt);
}
{
unsigned i;
for (i = 0; i < dlgControls.len; i++)
DrawDlgItemDispatch(hDC, i);
}
EndPaint(hwnd, &ps);
break;
}
case WM_SIZE:
if (firstSize == TRUE)
{
/* Size and position the main and text windows */
RECT rt;
int winWidth, winHeight;
firstSize = FALSE;
GetWindowRect(hwnd, &rt);
winWidth = rt.right - rt.left;
winHeight = rt.bottom - rt.top;
MoveWindow(hwnd, rt.left, rt.top, winWidth,
winHeight / 3 * 2, TRUE);
MoveWindow(textHwnd, rt.left, rt.top + winHeight / 3 * 2,
winWidth, winHeight / 3, TRUE);
}
break;
/*{
long winWidth, winHeight;
long xSmIcon, ySmIcon;
winWidth = LOWORD(lParam);
winHeight = HIWORD(lParam);
xSmIcon = GetSystemMetrics(SM_CXSMICON);
ySmIcon = GetSystemMetrics(SM_CYSMICON);
if (dlgPos.x >= (winWidth - xSmIcon))
{
dlgPos.x = winWidth - xSmIcon;
if (dlgPos.x <= 0)
dlgPos.x = 1;
}
if (dlgPos.y >= (winHeight - ySmIcon))
{
dlgPos.y = winHeight - ySmIcon;
if (dlgPos.y <= 0)
dlgPos.y = 1;
}
break;
}*/
case WM_COMMAND:
switch (LOWORD(wParam))
{
case M_NEW:
{
HDC hDC;
if (!AskForSave(hwnd))
break;
FreeDlgData();
LoadDialogTemplate(curFilename, TRUE);
if (dlgHasCaption == TRUE)
SetWindowText(pseudoHwnd, dlgCaption);
hDC = GetDC(hwnd);
SetDlgHFont(hDC);
UpdateFont(hDC);
ReleaseDC(hwnd, hDC);
InvalidateRect(hwnd, NULL, TRUE);
break;
}
case M_OPEN:
if (!AskForSave(hwnd))
break;
curFilename[0] = '\0';
if (GetFilenameDialog(curFilename, "Open", hwnd, 0))
{
FreeDlgData();
curLine = 0;
if (LoadDialogTemplate(curFilename, FALSE))
{
HDC hDC;
noFileLoaded = FALSE;
if (dlgHasCaption == TRUE)
SetWindowText(pseudoHwnd, dlgCaption);
hDC = GetDC(hwnd);
SetDlgHFont(hDC);
UpdateFont(hDC);
ReleaseDC(hwnd, hDC);
fileChanged = FALSE;
InvalidateRect(hwnd, NULL, TRUE);
}
else
{
if (curLine != 0)
{
char* errorMsg;
errorMsg = (char*)xmalloc(22 + 11 +
strlen(errorDesc) + 1);
wsprintf(errorMsg, "Parse error on line %i. %s",
curLine, errorDesc);
MessageBox(hwnd, errorMsg, NULL,
MB_OK | MB_ICONEXCLAMATION);
xfree(errorMsg);
}
else
{
MessageBox(hwnd, "Error loading dialog template.",
NULL, MB_OK | MB_ICONEXCLAMATION);
}
noFileLoaded = TRUE;
/* Load the default dialog template */
{
HDC hDC;
FreeDlgData();
LoadDialogTemplate(curFilename, TRUE);
if (dlgHasCaption == TRUE)
SetWindowText(pseudoHwnd, dlgCaption);
hDC = GetDC(hwnd);
SetDlgHFont(hDC);
UpdateFont(hDC);
ReleaseDC(hwnd, hDC);
InvalidateRect(hwnd, NULL, TRUE);
}
}
}
break;
case M_SAVE:
if (noFileLoaded == TRUE)
{
if (GetFilenameDialog(curFilename, "Save As", hwnd, 1))
SaveDialogTemplate(curFilename);
}
else
SaveDialogTemplate(curFilename);
break;
case M_SAVEAS:
if (GetFilenameDialog(curFilename, "Save As", hwnd, 1))
SaveDialogTemplate(curFilename);
break;
case M_EXIT:
if (!AskForSave(hwnd))
break;
DestroyWindow(hwnd);
break;
case M_PARSETEXT:
{
char* textBuf;
unsigned textLen;
fileChanged = TRUE;
textLen = GetWindowTextLength(textBox);
textBuf = (char*)malloc(textLen + 1);
GetWindowText(textBox, textBuf, textLen + 1);
textLen = SetUnixNlChars(textBuf, textLen);
/* Add extra space for parser */
textLen++;
textBuf = (char*)xrealloc(textBuf, textLen + 1);
textBuf[textLen-1] = '\0';
textBuf[textLen] = '\0';
if (activeCtrl == -1)
{
char* oldHead;
/* Save old header in case of a parse error */
oldHead = dlgHead;
dlgHead = NULL;
xfree(dlgFontFam);
dlgFontFam = NULL;
if (!ParseDlgHead(textBuf, textLen))
{
char* errorMsg;
errorMsg = (char*)xmalloc(22 + 11 + strlen(errorDesc) + 1);
wsprintf(errorMsg, "Parse error on line %i. %s",
curLine, errorDesc);
MessageBox(hwnd, errorMsg, NULL,
MB_OK | MB_ICONEXCLAMATION);
xfree(errorMsg);
/* Set "dlgHead" to the old header */
dlgHead = oldHead;
}
else
{
HDC hDC;
xfree(oldHead); /* The new header was sucessfully set */
/* Set the caption */
if (dlgHasCaption == TRUE)
SetWindowText(pseudoHwnd, dlgCaption);
/* Set the font automatically */
hDC = GetDC(hwnd);
SetDlgHFont(hDC);
UpdateFont(hDC);
ReleaseDC(hwnd, hDC);
}
}
else
{
DlgItem* pCtrl;
pCtrl = &dlgControls.d[activeCtrl];
curPos = 0;
/* Delete dynamic allocations in control before parsing */
xfree(pCtrl->id);
pCtrl->id = NULL;
xfree(pCtrl->style);
pCtrl->style = NULL;
xfree(pCtrl->exStyle);
pCtrl->exStyle = NULL;
if (!ParseControl(textBuf, textLen, activeCtrl))
{
char* errorMsg;
errorMsg = (char*)xmalloc(29 + strlen(errorDesc) + 1);
wsprintf(errorMsg, "Parse error in control line. %s",
errorDesc);
MessageBox(hwnd, errorMsg, NULL,
MB_OK | MB_ICONEXCLAMATION);
xfree(errorMsg);
/* Allocate empty strings to avoid crashes */
pCtrl->id = (char*)xmalloc(1);
pCtrl->id[0] = '\0';
pCtrl->style = (char*)xmalloc(1);
pCtrl->style[0] = '\0';
pCtrl->exStyle = (char*)xmalloc(1);
pCtrl->exStyle[0] = '\0';
}
}
xfree(textBuf);
InvalidateRect(hwnd, NULL, TRUE);
break;
}
case M_ADDCTRL:
{
char* textBuf;
unsigned textLen;
fileChanged = TRUE;
textLen = GetWindowTextLength(textBox);
textBuf = (char*)xmalloc(textLen + 1);
GetWindowText(textBox, textBuf, textLen + 1);
textLen = SetUnixNlChars(textBuf, textLen);
curPos = 0;
/* Add extra space for parser */
textLen++;
textBuf = (char*)xrealloc(textBuf, textLen + 1);
textBuf[textLen-1] = '\0';
textBuf[textLen] = '\0';
if (!ParseControl(textBuf, textLen, dlgControls.len))
{
char* errorMsg;
unsigned newCtrl;
errorMsg = (char*)xmalloc(29 + strlen(errorDesc) + 1);
wsprintf(errorMsg, "Parse error in control line. %s",
errorDesc);
MessageBox(hwnd, errorMsg, NULL, MB_OK | MB_ICONEXCLAMATION);
xfree(errorMsg);
/* Delete dynamic allocations, if any */
newCtrl = dlgControls.len;
xfree(dlgControls.d[newCtrl].id);
dlgControls.d[newCtrl].id = NULL;
xfree(dlgControls.d[newCtrl].style);
dlgControls.d[newCtrl].style = NULL;
xfree(dlgControls.d[newCtrl].exStyle);
dlgControls.d[newCtrl].exStyle = NULL;
}
else
{
activeCtrl = dlgControls.len;
EA_ADD(DlgItem, dlgControls);
}
xfree(textBuf);
InvalidateRect(hwnd, NULL, TRUE);
break;
}
case M_DELCTRL:
if (activeCtrl != -1)
{
DlgItem* pCtrl;
pCtrl = &dlgControls.d[activeCtrl];
/* Delete dynamic allocations */
xfree(pCtrl->id);
pCtrl->id = NULL;
xfree(pCtrl->style);
pCtrl->style = NULL;
xfree(pCtrl->exStyle);
pCtrl->exStyle = NULL;
EA_REMOVE(DlgItem, dlgControls, activeCtrl);
activeCtrl = -1;
InvalidateRect(hwnd, NULL, TRUE);
}
break;
case M_FONT:
{
CHOOSEFONT cf;
LOGFONT logFnt;
ZeroMemory(&cf, sizeof(CHOOSEFONT));
GetObject(dlgFont, sizeof(LOGFONT), &logFnt);
cf.lStructSize = sizeof(CHOOSEFONT);
cf.hwndOwner = hwnd;
cf.lpLogFont = &logFnt;
cf.Flags = CF_SCREENFONTS;
if (dlgFont != NULL)
cf.Flags |= CF_INITTOLOGFONTSTRUCT;
if (ChooseFont(&cf))
{
HDC hDC;
DeleteObject(dlgFont);
dlgFont = CreateFontIndirect(cf.lpLogFont);
hDC = GetDC(hwnd);
SelectObject(hDC, dlgFont);
UpdateFont(hDC);
ReleaseDC(hwnd, hDC);
InvalidateRect(hwnd, NULL, TRUE);
}
break;
}
case M_SHOW_T_WIN:
ShowWindow(textHwnd, SW_SHOWNORMAL);
SetActiveWindow(textHwnd);
break;
case M_T_WIN_FONT:
{
CHOOSEFONT cf;
LOGFONT logFnt;
ZeroMemory(&cf, sizeof(CHOOSEFONT));
GetObject(teFont, sizeof(LOGFONT), &logFnt);
cf.lStructSize = sizeof(CHOOSEFONT);
cf.hwndOwner = hwnd;
cf.lpLogFont = &logFnt;
cf.Flags = CF_SCREENFONTS;
if (dlgFont != NULL)
cf.Flags |= CF_INITTOLOGFONTSTRUCT;
if (ChooseFont(&cf))
{
DeleteObject(teFont);
teFont = CreateFontIndirect(cf.lpLogFont);
SendMessage(textBox, WM_SETFONT,
(WPARAM)teFont, (LPARAM)TRUE);
}
break;
}
case M_ABOUT:
DialogBox(g_hInstance, (LPCTSTR)ABOUTBOX, hwnd, AboutBoxProc);
break;
}
break;
case WM_KEYDOWN:
switch (wParam)
{
case VK_DELETE:
case VK_BACK:
if (activeCtrl != -1)
{
DlgItem* pCtrl;
pCtrl = &dlgControls.d[activeCtrl];
/* Delete dynamic allocations */
xfree(pCtrl->id);
pCtrl->id = NULL;
xfree(pCtrl->style);
pCtrl->style = NULL;
xfree(pCtrl->exStyle);
pCtrl->exStyle = NULL;
EA_REMOVE(DlgItem, dlgControls, activeCtrl);
activeCtrl = -1;
InvalidateRect(hwnd, NULL, TRUE);
}
break;
case VK_ESCAPE:
if (clickHit == TRUE)
{
CancelDrag();
clickHit = FALSE;
ClipCursor(NULL);
InvalidateRect(hwnd, NULL, TRUE);
}
break;
/* Keyboard interface for graphical editing */
case VK_TAB:
/* Invalidate the area at the old control dimensions */
InvalSelItem(hwnd);
if (GetKeyState(VK_SHIFT) & 0x8000)
{
if (activeCtrl == -1)
activeCtrl = dlgControls.len - 1;
else
activeCtrl--;
}
else
{
if (activeCtrl == dlgControls.len - 1)
activeCtrl = -1;
else
activeCtrl++;
}
InvalSelItem(hwnd);
UpdateTextWindow();
break;
}
KbdMoveProc(hwnd, wParam);
break;
case WM_LBUTTONDOWN:
ProcLBtnDown(hwnd, lParam);
break;
case WM_MOUSEMOVE:
ProcMouseMove(hwnd, lParam);
break;
case WM_LBUTTONUP:
EndDlgClick(hwnd);
clickHit = FALSE;
curDragHand = -1;
break;
case WM_ACTIVATE:
if (LOWORD(wParam) == WA_INACTIVE)
{
EndDlgClick(hwnd);
clickHit = FALSE;
curDragHand = -1;
}
break;
case WM_SETCURSOR:
{
/* Do not change the cursor type while dragging */
if (clickHit == FALSE)
DragHandleTest(hwnd);
switch (curDragHand)
{
case 0: /* top-left */
case 4: /* bottom-right */
SetCursor(LoadCursor(NULL, IDC_SIZENWSE));
return TRUE;
case 1: /* top */
case 5: /* bottom */
SetCursor(LoadCursor(NULL, IDC_SIZENS));
return TRUE;
case 2: /* top-right */
case 6: /* bottom-left */
SetCursor(LoadCursor(NULL, IDC_SIZENESW));
return TRUE;
case 3: /* right */
case 7: /* left */
SetCursor(LoadCursor(NULL, IDC_SIZEWE));
return TRUE;
}
break;
}
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
LRESULT CALLBACK TextWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam,
LPARAM lParam)
{
switch (uMsg)
{
case WM_CREATE:
{
CREATESTRUCT* cs;
HDC hDC;
cs = (CREATESTRUCT*)lParam;
textBox = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", NULL,
WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL |
ES_LEFT | ES_MULTILINE | ES_AUTOHSCROLL | ES_WANTRETURN,
0, 0, cs->cx, cs->cy, hwnd,
(HMENU)1, g_hInstance, NULL);
if (textBox == NULL)
return -1;
hDC = GetDC(hwnd);
teFont = CreateFont(-MulDiv(10, GetDeviceCaps(hDC, LOGPIXELSY), 72),
0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, ANSI_CHARSET,
OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
FIXED_PITCH | FF_DONTCARE, "Courier New");
ReleaseDC(hwnd, hDC);
SendMessage(textBox, WM_SETFONT, (WPARAM)teFont, (LPARAM)FALSE);
break;
}
case WM_CLOSE:
ShowWindow(hwnd, SW_HIDE);
return 0;
case WM_DESTROY:
DestroyWindow(textBox);
DeleteObject(teFont);
textHwnd = NULL;
break;
case WM_SIZE:
MoveWindow(textBox, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
break;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
INT_PTR CALLBACK AboutBoxProc(HWND hDlg, UINT uMsg, WPARAM wParam,
LPARAM lParam)
{
HWND hwndOwner;
RECT rc, rcDlg, rcOwner;
switch (uMsg)
{
case WM_INITDIALOG:
/* Center the dialog in the parent window */
/* Get the owner window and dialog box rectangles */
if ((hwndOwner = GetParent(hDlg)) == NULL)
{
hwndOwner = GetDesktopWindow();
}
GetWindowRect(hwndOwner, &rcOwner);
GetWindowRect(hDlg, &rcDlg);
CopyRect(&rc, &rcOwner);
/* Offset the owner and dialog box rectangles so that
right and bottom values represent the width and
height, and then offset the owner again to discard
space taken up by the dialog box. */
OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
OffsetRect(&rc, -rc.left, -rc.top);
OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
/* The new position is the sum of half the remaining
space and the owner's original position. */
SetWindowPos(hDlg,
HWND_TOP,
rcOwner.left + (rc.right / 2),
rcOwner.top + (rc.bottom / 2),
0, 0, /* ignores size arguments */
SWP_NOSIZE);
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
#define SKIP_WHITESPACE() \
while (curPos < dataSize && \
(buffer[curPos] == ' ' || buffer[curPos] == '\t')) \
curPos++;
#define CHECK_CHAR(chcode) (buffer[curPos] == chcode)
BOOL LoadDialogTemplate(char* filename, BOOL useNewTmpl)
{
char* buffer;
unsigned fileSize;
unsigned dataSize;
FILE* fp;
if (useNewTmpl == FALSE)
{
/* Read the file */
fp = fopen(filename, "rb");
if (fp == NULL)
return FALSE;
fseek(fp, 0, SEEK_END);
fileSize = ftell(fp);
fseek(fp, 0, SEEK_SET);
buffer = (char*)xmalloc(fileSize + 1);
if (buffer == NULL)
{
fclose(fp);
return FALSE;
}
fread(buffer, fileSize, 1, fp);
buffer[fileSize] = '\0';
fclose(fp);
}
else
{
/* Initialize edit variables */
strcpy(curFilename, "Untitled.dlg");
noFileLoaded = TRUE;
fileChanged = FALSE;
dlgHead = NULL;
dlgFontFam = NULL;
activeCtrl = -1;
/* Load a default template */
{
HRSRC hRes;
HGLOBAL hResData;
char* resMem;
hRes = FindResource(NULL, (LPCTSTR)NEW_TEMPLATE, RT_RCDATA);
hResData = LoadResource(NULL, hRes);
fileSize = SizeofResource(NULL, hRes);
resMem = (char*)LockResource(hResData);
/* Create a writeable copy of the memory */
buffer = (char*)xmalloc(fileSize + 1);
strncpy(buffer, resMem, fileSize);
buffer[fileSize] = '\0';
}
}
/* Since we read the whole file this way, we will have to properly
format newlines. */
dataSize = SetUnixNlChars(buffer, fileSize);
/* Parse the file */
EA_INIT(DlgItem, dlgControls, 16);
if (!ParseDlgHead(buffer, dataSize))
{
xfree(buffer);
return FALSE;
}
while (curPos < dataSize)
{
unsigned lastPos;
if (!ParseControl(buffer, dataSize, dlgControls.len))
{
/* Do fully safe cleanup */
EA_ADD(DlgItem, dlgControls);
xfree(buffer);
FreeDlgData();
return FALSE;
}
EA_ADD(DlgItem, dlgControls);
/* Check for "END" or '}' */
lastPos = curPos;
SKIP_WHITESPACE();
if (curPos >= fileSize)
{
/* Do fully safe cleanup */
EA_ADD(DlgItem, dlgControls);
xfree(buffer);
FreeDlgData();
return FALSE;
}
if ((CHECK_CHAR('}') || strncmp("END", &buffer[curPos], 3) == 0))
break;
else
curPos = lastPos;
}
xfree(buffer);
return TRUE;
}
#undef SKIP_WHITESPACE
#undef CHECK_CHAR
BOOL SaveDialogTemplate(char* filename)
{
FILE* fp;
/* Temporary variables */
unsigned i;
fp = fopen(filename, "wt");
if (fp == NULL)
return FALSE;
/* Write the header */
fputs(dlgHead, fp);
/* Write the controls */
for (i = 0; i < dlgControls.len; i++)
{
char* ctrlLine;
ctrlLine = FmtControlText(i);
fputs(ctrlLine, fp);
xfree(ctrlLine);
}
/* Write the end marker */
if (dlgHead[strlen(dlgHead)-2] == '{')
fputs("}\n", fp);
else
fputs("END\n", fp);
fclose(fp);
/* Update flags */
fileChanged = FALSE;
return TRUE;
}
void SetDlgHFont(HDC hDC)
{
DeleteObject(dlgFont);
dlgFont = CreateFont(
-MulDiv(dlgPointSize, GetDeviceCaps(hDC, LOGPIXELSY), 72),
0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, ANSI_CHARSET,
OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
DEFAULT_PITCH | FF_DONTCARE, dlgFontFam);
SelectObject(hDC, dlgFont);
}
void UpdateTextWindow()
{
if (activeCtrl == -1)
{
char* winBuff;
FmtDlgHeader();
winBuff = GenWinNlChars(dlgHead, strlen(dlgHead));
SetWindowText(textBox, winBuff);
xfree(winBuff);
}
else
{
char* newText;
unsigned textLen;
newText = FmtControlText(activeCtrl);
/* Make sure that the line endings are in CR+LF form */
textLen = strlen(newText);
newText = (char*)xrealloc(newText, textLen + 2);
newText[textLen-1] = '\r';
newText[textLen] = '\n';
textLen++;
newText[textLen] = '\0';
SetWindowText(textBox, newText);
xfree(newText);
}
}
void KbdMoveProc(HWND hwnd, WPARAM wParam)
{
DlgItem* pCtrl;
int stride;
BOOL noModify;
/* Invalidate the area at the old control dimensions */
InvalSelItem(hwnd);
if (activeCtrl != -1)
pCtrl = &dlgControls.d[activeCtrl];
if (GetKeyState(VK_CONTROL) & 0x8000)
stride = 1;
else
stride = 8;
noModify = FALSE;
if (activeCtrl == -1)
{
if (GetKeyState(VK_SHIFT) & 0x8000)
{
switch (wParam)
{
case VK_UP: dlgHeight -= stride; break;
case VK_DOWN: dlgHeight += stride; break;
case VK_LEFT: dlgWidth -= stride; break;
case VK_RIGHT: dlgWidth += stride; break;
default: noModify = TRUE; break;
}
}
else
{
switch (wParam)
{
case VK_UP: dlgPos.y -= stride; break;
case VK_DOWN: dlgPos.y += stride; break;
case VK_LEFT: dlgPos.x -= stride; break;
case VK_RIGHT: dlgPos.x += stride; break;
default: noModify = TRUE; break;
}
}
}
else
{
if (GetKeyState(VK_SHIFT) & 0x8000)
{
switch (wParam)
{
case VK_UP: pCtrl->cy -= stride; break;
case VK_DOWN: pCtrl->cy += stride; break;
case VK_LEFT: pCtrl->cx -= stride; break;
case VK_RIGHT: pCtrl->cx += stride; break;
default: noModify = TRUE; break;
}
}
else
{
switch (wParam)
{
case VK_UP: pCtrl->y -= stride; break;
case VK_DOWN: pCtrl->y += stride; break;
case VK_LEFT: pCtrl->x -= stride; break;