-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathztable.c
1621 lines (1540 loc) · 62.5 KB
/
ztable.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
#define CLION
#ifdef CLION
#pragma clang diagnostic push
#pragma ide diagnostic ignored "OCDFAInspection"
#pragma ide diagnostic ignored "hicpp-multiway-paths-covered"
#pragma ide diagnostic ignored "cppcoreguidelines-narrowing-conversions"
#pragma ide diagnostic ignored "bugprone-reserved-identifier"
#pragma ide diagnostic ignored "OCUnusedMacroInspection"
#pragma ide diagnostic ignored "OCUnusedGlobalDeclarationInspection"
#endif
#define UNICODE
#define _UNICODE
#include <stdio.h>
#include <stddef.h>
#include <tchar.h>
#include <windows.h>
#include <commctrl.h>
#include "ztable.h"
#define and &&
#define or ||
#define not !
LRESULT CALLBACK ZTableWindowProcedure(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK ZTableEditorSubclassProc(
HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
INT CALLBACK ZTableDefaultSortProc(HWND hwnd, ZTableRow *row1, ZTableRow *row2, SHORT ic, LPARAM lParam);
LRESULT ZTableNotifyParent(
ZTableData *self, UINT code, SHORT ir, SHORT ic, LONG x, LONG y, LPVOID data);
LRESULT ZTable_WM_COMMAND(ZTableData *self, WPARAM wParam, LPARAM lParam);
LRESULT ZTable_WM_DESTROY(ZTableData *self, WPARAM wParam, LPARAM lParam);
LRESULT ZTable_WM_HSCROLL(ZTableData *self, WPARAM wParam, LPARAM lParam);
LRESULT ZTable_WM_KEYDOWN(ZTableData *self, WPARAM wParam, LPARAM lParam);
LRESULT ZTable_WM_LBUTTONDBLCLK(ZTableData *self, WPARAM wParam, LPARAM lParam);
LRESULT ZTable_WM_LBUTTONDOWN(ZTableData *self, WPARAM wParam, LPARAM lParam);
LRESULT ZTable_WM_LBUTTONUP(ZTableData *self, WPARAM wParam, LPARAM lParam);
LRESULT ZTable_WM_MOUSELEAVE(ZTableData *self, WPARAM wParam, LPARAM lParam);
LRESULT ZTable_WM_MOUSEMOVE(ZTableData *self, WPARAM wParam, LPARAM lParam);
LRESULT ZTable_WM_MOUSEWHEEL(ZTableData *self, WPARAM wParam, LPARAM lParam);
LRESULT ZTable_WM_PAINT(ZTableData *self, WPARAM wParam, LPARAM lParam);
LRESULT ZTable_WM_RBUTTONDOWN(ZTableData *self, WPARAM wParam, LPARAM lParam);
LRESULT ZTable_WM_SIZE(ZTableData *self, WPARAM wParam, LPARAM lParam);
LRESULT ZTable_WM_VSCROLL(ZTableData *self, WPARAM wParam, LPARAM lParam);
HCURSOR ARROW = NULL;
HCURSOR COLRESIZE = NULL;
HANDLE processHeap = NULL;
BYTE selbitmap[] = {
1, 0, 0, 0, 0,
1, 1, 0, 0, 0,
1, 1, 1, 0, 0,
1, 1, 1, 1, 0,
1, 1, 1, 1, 1,
1, 1, 1, 1, 0,
1, 1, 1, 0, 0,
1, 1, 0, 0, 0,
1, 0, 0, 0, 0,
};
WORD selbitmap_size = MAKEWORD(5, 9);
BYTE newbitmap[] = {
0, 0, 0, 0, 1, 0, 0, 0, 0,
0, 1, 0, 0, 1, 0, 0, 1, 0,
0, 0, 1, 1, 1, 1, 1, 0, 0,
0, 0, 1, 1, 1, 1, 1, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 0, 1, 1, 1, 1, 1, 0, 0,
0, 0, 1, 1, 1, 1, 1, 0, 0,
0, 1, 0, 0, 1, 0, 0, 1, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0,
};
WORD newbitmap_size = MAKEWORD(9, 9);
extern BOOL SetupClass() {
if (processHeap != NULL) return TRUE; /* already called */
if ((processHeap = GetProcessHeap()) == NULL) return FALSE;
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_DBLCLKS | CS_GLOBALCLASS;
wc.lpfnWndProc = ZTableWindowProcedure;
wc.cbClsExtra = 0;
wc.cbWndExtra = sizeof(ZTableData *);
wc.hInstance = GetModuleHandle(NULL);
wc.hIcon = NULL;
wc.hCursor = NULL;
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = ZTABLE_CLASSNAME;
wc.hIconSm = NULL;
if (!RegisterClassEx(&wc)) return FALSE;
ARROW = LoadCursor(NULL, IDC_ARROW);
if (ARROW == NULL) return FALSE;
COLRESIZE = LoadCursor(NULL, IDC_SIZEWE);
if (COLRESIZE == NULL) return FALSE;
return TRUE;
}
LRESULT ZTableNotifyParent(
ZTableData *self, UINT code, SHORT ir, SHORT ic, LONG x, LONG y, LPVOID data) {
self->nm.hdr.hwndFrom = self->hwnd;
int myID = GetDlgCtrlID(self->hwnd);
self->nm.hdr.idFrom = myID;
self->nm.hdr.code = code;
self->nm.ir = ir;
self->nm.ic = ic;
self->nm.location.x = x;
self->nm.location.y = y;
self->nm.data = data;
return SendMessage(self->parentHwnd, WM_NOTIFY, (WPARAM) myID, (LPARAM) (&(self->nm)));
}
#define BREAKPOINT(n) MessageBox(self->hwnd, _T("Breakpoint " # n), _T("Debug"), MB_OK)
#define SELF ((ZTableData*) GetWindowLongPtr(hwnd, 0))
LRESULT CALLBACK ZTableWindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
ZTableData *self;
SHORT ir, ic;
LRESULT res;
switch (message) {
case WM_CREATE:
#define CHECK(h) if (!(h)) return -1
CHECK(self = (ZTableData *) HeapAlloc(processHeap, HEAP_ZERO_MEMORY, sizeof(ZTableData)));
self->hwnd = hwnd;
self->parentHwnd = GetParent(hwnd);
INT fontHeight = MulDiv(GetDeviceCaps(GetDC(hwnd), LOGPIXELSY), 9, 72);
self->lineSizeX = fontHeight * 2;
self->selectedRow = -1;
self->lastSortColumn = -1;
self->headerHeight = fontHeight * 2;
CHECK(self->headerFont = CreateFont(
-fontHeight, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _T("Segoe UI")));
self->textPadX = MulDiv(fontHeight, 2, 9);
CHECK(self->headerFill = GetSysColorBrush(COLOR_BTNFACE));
CHECK(self->headerFillHover = GetSysColorBrush(COLOR_3DLIGHT));
CHECK(self->headerFillPressed = GetSysColorBrush(COLOR_3DHIGHLIGHT));
self->pressedHeader = -1;
self->hoverHeader = -1;
/**self->columns = NULL;**/
/**self->rows = NULL;**/
/**self->nRows = 0;**/
/**self->nRowsFiltered = 0;**/
/**self->nCols = 0;**/
self->rowHeight = self->lineSizeY = MulDiv(fontHeight, 5, 3);
CHECK(self->rowFont = CreateFont(
-fontHeight, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _T("Segoe UI")));
CHECK(self->rowFill = GetSysColorBrush(COLOR_WINDOW));
CHECK(self->rowFillAlternate = GetSysColorBrush(COLOR_BTNFACE));
CHECK(self->rowFillSelected = GetSysColorBrush(COLOR_HIGHLIGHT));
// the void* cast is to silence the ptr/int comparison warning
CHECK(ZTableUpdateColors(self));
CHECK(self->emptyText = HeapAlloc(processHeap, HEAP_ZERO_MEMORY, 1));
self->emptyTextLength = 0;
CHECK(ZTableSetEmptyText(self, _T("No items to show"), 16));
self->sc_selbitmap = selbitmap;
self->sc_selbitmapsize = selbitmap_size;
self->sc_newbitmap = newbitmap;
self->sc_newbitmapsize = newbitmap_size;
CHECK(self->sc_fill = GetSysColorBrush(COLOR_BTNFACE));
self->sc_bmpfg = GetSysColor(COLOR_BTNTEXT);
/**self->autoMakeNewRow = FALSE;**/
SetWindowLongPtr(hwnd, 0, (LONG_PTR) self);
ZTableUpdateScrollInfo(self);
return 0;
#undef CHECK
case WM_COMMAND:
return ZTable_WM_COMMAND(SELF, wParam, lParam);
case WM_DESTROY:
return ZTable_WM_DESTROY(SELF, wParam, lParam);
case WM_GETDLGCODE:
return DLGC_WANTARROWS;
case WM_HSCROLL:
return ZTable_WM_HSCROLL(SELF, wParam, lParam);
case WM_KEYDOWN:
return ZTable_WM_KEYDOWN(SELF, wParam, lParam);
case WM_LBUTTONDBLCLK:
return ZTable_WM_LBUTTONDBLCLK(SELF, wParam, lParam);
case WM_LBUTTONDOWN:
return ZTable_WM_LBUTTONDOWN(SELF, wParam, lParam);
case WM_LBUTTONUP:
return ZTable_WM_LBUTTONUP(SELF, wParam, lParam);
case WM_MOUSELEAVE:
return ZTable_WM_MOUSELEAVE(SELF, wParam, lParam);
case WM_MOUSEMOVE:
return ZTable_WM_MOUSEMOVE(SELF, wParam, lParam);
case WM_MOUSEWHEEL:
return ZTable_WM_MOUSEWHEEL(SELF, wParam, lParam);
case WM_PAINT:
return ZTable_WM_PAINT(SELF, wParam, lParam);
case WM_RBUTTONDOWN:
return ZTable_WM_RBUTTONDOWN(SELF, wParam, lParam);
case WM_SIZE:
return ZTable_WM_SIZE(SELF, wParam, lParam);
case WM_SYSCOLORCHANGE:
ZTableUpdateColors(SELF);
InvalidateRect(hwnd, NULL, TRUE);
break;
case WM_VSCROLL:
return ZTable_WM_VSCROLL(SELF, wParam, lParam);
/******************************************************************************/
case ZTM_ADDNEWROW:
return ZTableAddNewRow(SELF);
case ZTM_BEGINEDITING:
return ZTableBeginEditing(SELF, LOWORD(lParam), HIWORD(lParam));
case ZTM_CANCELEDITING:
return ZTableCancelEditing(SELF);
case ZTM_DELETEROW:
return ZTableDeleteRow(SELF, (SHORT) lParam);
case ZTM_ENDEDITING:
return ZTableEndEditing(SELF);
case ZTM_GETCELLAT:
res = ZTableGetCellAt(SELF, LOWORD(lParam), HIWORD(lParam), &ir, &ic);
*((LONG *) wParam) = MAKEWPARAM(ir, ic);
return res;
case ZTM_GETCELLRECT:
return ZTableGetCellRect(SELF, (RECT *) wParam, LOWORD(lParam), HIWORD(lParam));
case ZTM_GETINEDITING:
return SELF->inEditing;
case ZTM_GETITEMPTR:
return ZTableGetItemPtr(SELF, LOWORD(lParam), HIWORD(lParam), (ZTableItem **) wParam);
case ZTM_GETNROWS:
return SELF->nRows;
case ZTM_GETSELECTEDROW:
return SELF->selectedRow;
case ZTM_INSERTROW:
return ZTableInsertRow(SELF, (SHORT) lParam, (ZTableRow *) wParam);
case ZTM_SELECT:
return ZTableSelect(SELF, (SHORT) lParam);
case ZTM_SETCOLUMNS:
return ZTableSetColumns(SELF, (ZTableColumn *) wParam, (SHORT) lParam);
case ZTM_SETCOLUMNWIDTH:
return ZTableSetColumnWidth(SELF, (SHORT) wParam, (USHORT) lParam);
case ZTM_SETEMPTYTEXT:
return ZTableSetEmptyText(SELF, (LPCWSTR) wParam, (SHORT) lParam);
case ZTM_SETITEMTEXT:
return ZTableSetItemText(SELF, LOWORD(lParam), HIWORD(lParam), (LPCTSTR) wParam, -1);
case ZTM_GETITEMPARAM:
return ZTableGetItemParam(SELF, LOWORD(wParam), HIWORD(wParam), (LPARAM *) lParam);
case ZTM_SETAUTONEW:
return ZTableSetAutoMakeNewRow(SELF, wParam);
case ZTM_SETITEMPARAM:
return ZTableSetItemParam(SELF, LOWORD(wParam), HIWORD(wParam), (LPARAM) lParam);
case ZTM_SETROWCOLORS:
return ZTableSetRowColors(SELF, (SHORT) lParam, (ZTableRow *) wParam);
case ZTM_SETROWFILTERED:
return ZTableSetRowFiltered(SELF, (SHORT) lParam, (BOOL) wParam);
case ZTM_SETROWHEIGHT:
return ZTableSetRowHeight(SELF, (USHORT) wParam);
case ZTM_SORT:
return ZTableSort(SELF, (ZTableSortInfo *) wParam);
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
#undef SELF
#define CHECK(v) if (!(v)) {printf("CHECK(" #v ") failed, line %i\n", __LINE__); return FALSE;}
//#define CHECK_E(v, err) if (!(v)) {printf("CHECK(" #v ") failed\n"); self->lastError=err; return FALSE;}
BOOL ZTableBeginEditing(ZTableData *self, SHORT ir, SHORT ic) {
CHECK(self->columns[ic].flags & ZTC_EDITABLE)
CHECK(ZTableSelect(self, ir));
CHECK(ZTableSeeColumn(self, ic));
RECT rect;
CHECK(ZTableGetCellRect(self, &rect, ir, ic));
if (self->columns[ic].flags & ZTC_CUSTOMEDITOR) {
if (self->columns[ic].flags & ZTC_CUSTOMEDITORROW) {
if (self->rows[ir].items[ic].lParam != 0) {
self->currentEditWin = (HWND) self->rows[ir].items[ic].lParam;
goto ZTableBeginEditing_have_editor;
}
}
goto ZTableBeginEditing_use_column_editor;
}
goto ZTableBeginEditing_use_default_editor;
ZTableBeginEditing_use_column_editor:
if (self->columns[ic].editWin == NULL)
goto ZTableBeginEditing_use_default_editor;
self->currentEditWin = self->columns[ic].editWin;
goto ZTableBeginEditing_have_editor;
ZTableBeginEditing_use_default_editor:
if (self->editWin == NULL) {
CHECK(self->editWin = CreateWindowEx(
WS_EX_CLIENTEDGE, WC_EDIT, NULL,
WS_CHILD | WS_TABSTOP | ES_AUTOHSCROLL,
0, 0, 0, 0, self->hwnd, (HMENU)ZTABLE_EDIT_ID, NULL, NULL));
SendMessage(self->editWin, WM_SETFONT, (WPARAM) self->rowFont, 0);
SendMessage(self->editWin, EM_SETMARGINS,
EC_LEFTMARGIN | EC_RIGHTMARGIN,
MAKELPARAM(self->textPadX - 3, self->textPadX - 3));
}
LONG style = GetWindowLong(self->editWin, GWL_STYLE);
style = (style & ~0x3) | (self->columns[ic].flags & 0x3);
SetWindowLong(self->editWin, GWL_STYLE, style);
CHECK(SetWindowText(self->editWin, self->rows[ir].items[ic].text));
CHECK(PostMessage(self->editWin, EM_SETMODIFY, FALSE, 0));
CHECK(PostMessage(self->editWin, EM_SETSEL, 0, -1));
self->currentEditWin = self->editWin;
ZTableBeginEditing_have_editor:
if (!GetProp(self->currentEditWin, _T("ZTableSubclassed"))) {
LONG oldWndProc;
CHECK(oldWndProc = SetWindowLongPtr(
self->currentEditWin, GWLP_WNDPROC, (LONG_PTR) ZTableEditorSubclassProc));
SetWindowLongPtr(self->currentEditWin, GWLP_USERDATA, oldWndProc);
CHECK(SetProp(self->currentEditWin, _T("ZTableSubclassed"), (HANDLE) 1));
}
CHECK(SetProp(self->currentEditWin, _T("ZTableModified"), (HANDLE) 0));
ZTableNotifyParent(self, ZTN_EDITSTART, ir, ic, 0, 0, (LPVOID) self->currentEditWin);
CHECK(MoveWindow(
self->currentEditWin,
rect.left,
rect.top,
rect.right - rect.left - 1,
rect.bottom - rect.top - 1,
TRUE));
ShowWindow(self->currentEditWin, SW_SHOW);
SetFocus(self->currentEditWin);
self->editRow = ir;
self->editCol = ic;
self->inEditing = TRUE;
return TRUE;
}
BOOL ZTableAddNewRow(ZTableData *self) {
ZTableRow r;
SIZE_T size = self->nCols * sizeof(ZTableItem);
CHECK(r.items = (ZTableItem *) HeapAlloc(processHeap, HEAP_ZERO_MEMORY, size));
// no need to initialize the items, as textLength is 0
r.textColor = CLR_DEFAULT;
r.hFill = NULL;
CHECK(ZTableInsertRow(self, self->nRows, &r));
CHECK(HeapFree(processHeap, 0, (LPVOID)r.items))
CHECK(ZTableUpdateFilteredRows(self));
ZTableNotifyParent(self, ZTN_AUTONEWROW, self->nRows-1, self->nCols, 0, 0, NULL);
return TRUE;
}
BOOL ZTableCancelEditing(ZTableData *self) {
if (self->inEditing) {
self->inEditing = FALSE;
ShowWindow(self->currentEditWin, SW_HIDE);
}
return TRUE;
}
BOOL ZTableDeleteRow(ZTableData *self, SHORT index) {
CHECK(index >= 0 && index < self->nRows); // also ensures (self->nRows > 0)
// Recommended: user program should check itself for this
CHECK(not (self->autoMakeNewRow and index == self->nRows-1));
SHORT nRows = self->nRows - 1;
/* Memory error protection */
self->nRows = index;
SIZE_T newSize = nRows * sizeof(ZTableRow);
SIZE_T oldSize = HeapSize(processHeap, 0, self->rows);
if (oldSize == 0xFFFFFFFF || (oldSize - newSize) > 0x100) {
ZTableRow *newArray;
CHECK(newArray = (ZTableRow *) HeapReAlloc(
processHeap,
HEAP_ZERO_MEMORY,
self->rows,
newSize));
self->rows = newArray;
}
for (SHORT i = index; i < nRows; i++) {
self->rows[i] = self->rows[i + 1];
}
if (self->selectedRow == -1 || nRows == 0) {
self->selectedRow = -1;
} else if (index < self->selectedRow) {
// guaranteed: self->selectedRow > 0
self->selectedRow = self->selectedRow - 1;
} else {
// keep the same or the last one
self->selectedRow = min(self->selectedRow, nRows - 1);
}
self->nRows = nRows;
CHECK(ZTableUpdateFilteredRows(self));
CHECK(ZTableUpdateScrollInfo(self));
RECT rect;
rect.left = 0;
rect.top = self->headerHeight;
rect.right = self->cWidth;
rect.bottom = self->cHeight;
CHECK(InvalidateRect(self->hwnd, &rect, TRUE));
return TRUE;
}
BOOL ZTableEndEditing(ZTableData *self) {
if (self->inEditing == FALSE) return TRUE;
self->inEditing = FALSE;
if (self->currentEditWin != self->editWin) {
if (GetProp(self->currentEditWin, _T("ZTableModified"))) {
if (ZTableNotifyParent(self, ZTN_EDITEND, self->editRow, self->editCol, 0, 0,
(LPVOID) self->currentEditWin)) {
SetFocus(self->currentEditWin);
self->inEditing = TRUE;
return TRUE;
} else if (self->autoMakeNewRow and self->editRow == self->nRows - 1) {
ZTableAddNewRow(self);
}
}
} else {
if (SendMessage(self->editWin, EM_GETMODIFY, 0, 0)) {
if (ZTableNotifyParent(self, ZTN_EDITEND, self->editRow, self->editCol, 0, 0, (LPVOID) self->editWin)) {
SetFocus(self->editWin);
CHECK(PostMessage(self->editWin, EM_SETSEL, 0, -1));
self->inEditing = TRUE;
return TRUE;
} else {
LPTSTR string = self->rows[self->editRow].items[self->editCol].text;
INT ln = GetWindowTextLength(self->editWin);
CHECK(string = HeapReAlloc(processHeap, HEAP_ZERO_MEMORY, string, (ln + 1) * sizeof(TCHAR)));
CHECK(GetWindowText(self->editWin, string, ln + 1) || (GetLastError() == 0));
self->rows[self->editRow].items[self->editCol].text = string;
self->rows[self->editRow].items[self->editCol].textLength = ln;
if (self->autoMakeNewRow and self->editRow == self->nRows - 1) {
ZTableAddNewRow(self);
}
RECT rect;
CHECK(ZTableGetCellRect(self, &rect, self->editRow, self->editCol));
CHECK(InvalidateRect(self->hwnd, &rect, FALSE));
}
}
}
ShowWindow(self->currentEditWin, SW_HIDE);
return TRUE;
}
BOOL ZTableGetCellAt(ZTableData *self, LONG x, LONG y, SHORT *ir, SHORT *ic) {
SHORT row, col;
if (y < 0) {
row = -2;
} else if (y <= self->headerHeight) {
row = -1;
} else {
LONG cy = self->headerHeight - self->scrollPosY;
row = -2;
for (SHORT row_ = 0; row_ < self->nRowsFiltered; row_++) {
cy += self->rowHeight;
if (cy >= y) {
row = self->rowsFiltered[row_]->index;
break;
}
}
}
col = -1;
if (x >= 0) {
LONG cx = -self->scrollPosX;
for (USHORT col_ = 0; col_ < self->nCols; col_++) {
cx += self->columns[col_].width;
if (cx >= x) {
col = col_;
break;
}
}
}
*ic = col;
*ir = row;
return TRUE;
}
BOOL ZTableGetCellRect(ZTableData *self, RECT *rect, SHORT ir, SHORT ic) {
if (ir < -1 || ir >= self->nRows || ic < -1 || ic >= self->nCols) return FALSE;
if (ir != -1 && !self->rows[ir].filtered) {
rect->left = rect->top = rect->right = rect->bottom = -1;
return TRUE;
}
rect->left = -self->scrollPosX;
if (ic == -1) { /* full row rect */
rect->right = rect->left + self->scrollRegionX;
} else {
for (SHORT col = 0; col < ic; col++) {
rect->left += self->columns[col].width;
}
rect->right = rect->left + self->columns[ic].width;
}
if (ir == -1) { /* header cell rect */
rect->top = 0;
rect->bottom = self->headerHeight;
} else {
rect->top = self->rows[ir].indexFiltered * self->rowHeight + self->headerHeight - self->scrollPosY;
rect->bottom = rect->top + self->rowHeight;
}
return TRUE;
}
BOOL ZTableGetItemParam(ZTableData *self, SHORT ir, SHORT ic, LPARAM *pLParam) {
CHECK(ir >= 0 && ir < self->nRows);
CHECK(ic >= 0 && ic < self->nCols);
*pLParam = self->rows[ir].items[ic].lParam;
return TRUE;
}
BOOL ZTableGetItemPtr(ZTableData *self, SHORT ir, SHORT ic, ZTableItem **pItem) {
CHECK(ir >= 0 && ir < self->nRows);
CHECK(ic >= 0 && ic < self->nCols);
*pItem = &(self->rows[ir].items[ic]);
return TRUE;
}
BOOL ZTableGetNextEditable(ZTableData *self, USHORT ir, USHORT ic, USHORT *nir, USHORT *nic) {
for (USHORT col = ic + 1; col < self->nCols; col++) {
if (self->columns[col].flags & ZTC_EDITABLE) {
*nir = ir;
*nic = col;
return TRUE;
}
}
for (ir = ir + 1; ir < self->nRows; ir++) {
if (!self->rows[ir].filtered) continue;
for (USHORT col = 0; col <= ic; col++) {
if (self->columns[col].flags & ZTC_EDITABLE) {
*nir = ir;
*nic = col;
return TRUE;
}
}
}
return FALSE;
}
BOOL ZTableGetNextVisibleRow(ZTableData *self, USHORT ir, USHORT *nir) {
for (ir = ir + 1; ir < self->nRows; ir++) {
if (!self->rows[ir].filtered) continue;
*nir = ir;
return TRUE;
}
return FALSE;
}
BOOL ZTableGetPrevEditable(ZTableData *self, USHORT ir, USHORT ic, USHORT *nir, USHORT *nic) {
for (SHORT col = ic - 1; col > -1; col--) {
if (self->columns[col].flags & ZTC_EDITABLE) {
*nir = ir;
*nic = col;
return TRUE;
}
}
for (ir = ir - 1; ir >= 0; ir--) {
if (!self->rows[ir].filtered) continue;
for (SHORT col = self->nCols - 1; col >= ic; col--) {
if (self->columns[col].flags & ZTC_EDITABLE) {
*nir = ir;
*nic = col;
return TRUE;
}
}
}
return FALSE;
}
BOOL ZTableGetPrevVisibleRow(ZTableData *self, USHORT ir, USHORT *nir) {
for (ir = ir - 1; ir >= 0; ir--) {
if (!self->rows[ir].filtered) continue;
*nir = ir;
return TRUE;
}
return FALSE;
}
BOOL ZTableInsertRow(ZTableData *self, SHORT index, ZTableRow *row) {
CHECK(self->columns != NULL);
CHECK(self->nRows < 0xffff);
if (index < 0) index += self->nRows;
index = max(0, min(index, self->nRows));
ZTableRow *newarray;
SHORT nRows = (self->nRows) + 1;
/* Memory error protection */
self->nRows = index;
SIZE_T newSize = nRows * sizeof(ZTableRow);
if (self->rows == NULL) {
CHECK(newarray = (ZTableRow *) HeapAlloc(processHeap, HEAP_ZERO_MEMORY, newSize));
self->rows = newarray;
} else {
SIZE_T oldSize = HeapSize(processHeap, 0, self->rows);
if (oldSize < newSize || oldSize == 0xFFFFFFFF || (oldSize - newSize) > 0x100) {
CHECK(newarray = (ZTableRow *) HeapReAlloc(
processHeap,
HEAP_ZERO_MEMORY,
self->rows,
newSize));
self->rows = newarray;
}
}
for (SHORT i = nRows - 1; i > index; i--) {
self->rows[i] = self->rows[i - 1];
}
ZTableItem *items;
CHECK(self->rows[index].items = items = (ZTableItem *) LocalAlloc(LPTR, sizeof(ZTableItem) * (self->nCols)));
for (USHORT ic = 0; ic < self->nCols; ic++) {
CHECK(items[ic].text = (LPTSTR) LocalAlloc(LPTR, sizeof(TCHAR) * (row->items[ic].textLength + 1)));
if (row->items[ic].textLength) {
lstrcpyn(items[ic].text, row->items[ic].text, row->items[ic].textLength + 1);
} else {
items[ic].text[0] = 0;
}
items[ic].textLength = row->items[ic].textLength;
items[ic].lParam = row->items[ic].lParam;
}
self->rows[index].hFill = row->hFill;
self->rows[index].textColor = row->textColor;
self->rows[index].filtered = TRUE;
self->nRows = nRows;
CHECK(ZTableUpdateFilteredRows(self));
CHECK(ZTableUpdateScrollInfo(self));
RECT rect;
rect.left = 0;
rect.top = self->headerHeight;
rect.right = self->cWidth;
rect.bottom = self->cHeight;
CHECK(InvalidateRect(self->hwnd, &rect, FALSE));
if (self->nRows == 1) {
// Erase the empty text
rect.bottom = rect.top + self->headerHeight;
CHECK(InvalidateRect(self->hwnd, &rect, TRUE));
}
return TRUE;
}
BOOL ZTableSeeColumn(ZTableData *self, SHORT ic) {
CHECK(ic >= 0 && ic < self->nCols);
LONG old = self->scrollPosX;
LONG left = 0;
for (SHORT col = 0; col < ic; col++) {
left += self->columns[col].width;
}
LONG right = left + self->columns[ic].width;
if (right > self->scrollPosX + self->pageSizeX) {
self->scrollPosX = left - self->pageSizeX + (self->columns[ic].width);
}
if (left < self->scrollPosX) {
self->scrollPosX = left;
}
if (self->scrollPosX != old) {
CHECK(SetScrollPos(self->hwnd, SB_HORZ, self->scrollPosX + 1, TRUE));
CHECK(InvalidateRect(self->hwnd, NULL, FALSE));
}
return TRUE;
}
BOOL ZTableSeeRow(ZTableData *self, SHORT ir) {
CHECK(self->rows[ir].filtered);
LONG old = self->scrollPosY;
LONG top = self->rows[ir].indexFiltered * self->rowHeight;
LONG bottom = top + self->rowHeight;
if (bottom > self->scrollPosY + self->pageSizeY) {
self->scrollPosY = top - self->pageSizeY + self->rowHeight;
}
if (top < self->scrollPosY) {
self->scrollPosY = top;
}
if (self->scrollPosY != old) {
CHECK(SetScrollPos(self->hwnd, SB_VERT, self->scrollPosY + 1, TRUE));
RECT rect;
rect.left = 0;
rect.top = self->headerHeight;
rect.right = self->cWidth;
rect.bottom = self->cHeight;
CHECK(InvalidateRect(self->hwnd, &rect, FALSE));
}
return TRUE;
}
BOOL ZTableSelect(ZTableData *self, SHORT ir) {
CHECK(self->rows[ir].filtered);
RECT rect;
if (ir != self->selectedRow) {
CHECK(ZTableGetCellRect(self, &rect, self->selectedRow, -1));
CHECK(InvalidateRect(self->hwnd, &rect, FALSE));
self->selectedRow = ir;
CHECK(ZTableGetCellRect(self, &rect, self->selectedRow, -1));
CHECK(InvalidateRect(self->hwnd, &rect, FALSE));
}
CHECK(ZTableSeeRow(self, ir));
return TRUE;
}
BOOL ZTableSetAutoMakeNewRow(ZTableData *self, BOOL make) {
self->autoMakeNewRow = make;
if (self->nCols != 0 and self->nRows != 0) {
RECT r;
CHECK(ZTableGetCellRect(self, &r, self->nRows-1, -1));
CHECK(InvalidateRect(self->hwnd, &r, FALSE));
}
return TRUE;
}
BOOL ZTableSetColumns(ZTableData *self, ZTableColumn *cols, SHORT nCols) {
ZTableColumn *newarray;
SIZE_T newSize = nCols * sizeof(ZTableColumn);
if (self->columns == NULL) {
CHECK(newarray = (ZTableColumn *) HeapAlloc(
processHeap,
HEAP_ZERO_MEMORY,
newSize));
self->columns = newarray;
} else {
CHECK(self->nRows == 0);
SIZE_T oldSize = HeapSize(processHeap, 0, self->columns);
if (oldSize == 0xFFFFFFFF || oldSize < newSize || (oldSize - newSize) > 0x100) {
CHECK(newarray = (ZTableColumn *) HeapReAlloc(
processHeap,
HEAP_ZERO_MEMORY,
self->columns,
newSize));
self->columns = newarray;
}
}
for (SHORT ic = 0; ic < nCols; ic++) {
/* Memory error protection */
self->nCols = ic;
ZTableColumn *col = &(cols[ic]);
if (self->columns[ic].headerText != NULL) {
CHECK(self->columns[ic].headerText = (LPTSTR) LocalReAlloc(
(HLOCAL) self->columns[ic].headerText,
sizeof(TCHAR) * (col->headerTextLength + 1),
0));
} else {
CHECK(self->columns[ic].headerText = (LPTSTR) LocalAlloc(
LPTR,
sizeof(TCHAR) * (col->headerTextLength + 1)));
}
CHECK(lstrcpyn(
self->columns[ic].headerText,
cols[ic].headerText,
col->headerTextLength + 1));
self->columns[ic].headerTextLength = col->headerTextLength;
self->columns[ic].width = col->width;
self->columns[ic].minWidth = col->minWidth;
self->columns[ic].maxWidth = col->maxWidth;
self->columns[ic].defaultWidth = col->defaultWidth;
self->columns[ic].flags = col->flags;
self->columns[ic].editWin = col->editWin;
if (ic != 0 and col->flags & _ZTC_SELECTOR) {
// Only the first column may be selector
self->columns[ic].flags &= ~_ZTC_SELECTOR;
}
}
self->nCols = nCols;
CHECK(InvalidateRect(self->hwnd, NULL, TRUE));
return TRUE;
}
BOOL ZTableSetColumnWidth(ZTableData *self, SHORT ic, USHORT width) {
CHECK(0 <= ic && ic < self->nCols);
if (width == (USHORT) -1) width = self->columns[ic].defaultWidth;
RECT rect;
CHECK(ZTableGetCellRect(self, &rect, -1, ic));
rect.bottom = self->cHeight;
rect.right = self->cWidth;
InvalidateRect(self->hwnd, &rect, FALSE);
if (width < self->columns[ic].width) {
CHECK(ZTableGetCellRect(self, &rect, -1, self->nCols - 1));
rect.left = rect.right;
rect.top = 0;
rect.right = self->cWidth;
rect.bottom = self->cHeight;
CHECK(InvalidateRect(self->hwnd, &rect, TRUE))
}
self->columns[ic].width = width;
ZTableUpdateScrollInfo(self);
return TRUE;
}
BOOL ZTableSetEmptyText(ZTableData *self, LPCTSTR text, SHORT textLength) {
LPTSTR buf = self->emptyText;
CHECK(buf = HeapReAlloc(processHeap, HEAP_ZERO_MEMORY, buf, (textLength + 1) * sizeof(TCHAR)));
CHECK(lstrcpyn(buf, text, textLength + 1));
self->emptyText = buf;
self->emptyTextLength = textLength;
return TRUE;
}
BOOL ZTableSetItemParam(ZTableData *self, SHORT ir, SHORT ic, LPARAM lParam) {
CHECK(ir >= 0 && ir < self->nRows);
CHECK(ic >= 0 && ic < self->nCols);
self->rows[ir].items[ic].lParam = lParam;
RECT rect;
CHECK(ZTableGetCellRect(self, &rect, ir, ic));
CHECK(InvalidateRect(self->hwnd, &rect, FALSE));
return TRUE;
}
BOOL ZTableSetItemText(ZTableData *self, SHORT ir, SHORT ic, LPCWSTR text, UINT textLength) {
CHECK(ir >= 0 && ir < self->nRows);
CHECK(ic >= 0 && ic < self->nCols);
if (textLength == -1) textLength = lstrlen(text);
ZTableItem *item = &(self->rows[ir].items[ic]);
LPTSTR string = item->text;
CHECK(string = HeapReAlloc(processHeap, HEAP_ZERO_MEMORY, string, (textLength + 1) * sizeof(TCHAR)));
if (textLength != 0) {
CHECK(lstrcpyn(string, text, textLength + 1));
}
item->text = string;
item->textLength = (SHORT) textLength;
RECT rect;
CHECK(ZTableGetCellRect(self, &rect, ir, ic));
CHECK(InvalidateRect(self->hwnd, &rect, FALSE));
return TRUE;
}
BOOL ZTableSetRowColors(ZTableData *self, SHORT ir, ZTableRow *row) {
CHECK((0 <= ir) && (ir < self->nRows));
self->rows[ir].hFill = row->hFill;
self->rows[ir].textColor = row->textColor;
RECT rect;
CHECK(ZTableGetCellRect(self, &rect, ir, -1));
CHECK(InvalidateRect(self->hwnd, &rect, FALSE));
return TRUE;
}
BOOL ZTableSetRowFiltered(ZTableData *self, SHORT ir, BOOL filtered) {
CHECK((0 <= ir) && (ir < self->nRows));
if (filtered != self->rows[ir].filtered) {
if (ir == self->selectedRow) {
self->selectedRow = -1;
}
self->rows[ir].filtered = filtered;
CHECK(ZTableUpdateFilteredRows(self));
CHECK(ZTableUpdateScrollInfo(self));
RECT rect = {0, self->headerHeight, self->cWidth, self->cHeight};
CHECK(InvalidateRect(self->hwnd, &rect, ((!filtered) || (self->nRowsFiltered == 1))));
}
return TRUE;
}
BOOL ZTableSetRowHeight(ZTableData *self, USHORT height) {
CHECK(0 < height && height < 0x100);
self->rowHeight = height * self->lineSizeY;
CHECK(ZTableUpdateScrollInfo(self));
CHECK(InvalidateRect(self->hwnd, NULL, FALSE));
return TRUE;
}
BOOL ZTableSort(ZTableData *self, ZTableSortInfo *info) {
CHECK(info->column < self->nCols && info->column >= -1);
if (info->column != -1) {
if (info->reversed == -1) {
info->reversed = self->columns[info->column].flags & ZTC_REVERSED;
}
if (info->column == self->lastSortColumn) {
info->reversed = !(info->reversed);
}
if (info->reversed) {
self->columns[info->column].flags |= ZTC_REVERSED;
} else {
self->columns[info->column].flags &= ~ZTC_REVERSED;
}
}
if (self->nRows > (self->autoMakeNewRow != -1 ? 2 : 1)) {
INT reverse_multiplier = (info->reversed ? -1 : 1);
USHORT stopat = self->nRows-(self->autoMakeNewRow != -1 ? 1 : 0);
for (USHORT next_to_insert = 1; next_to_insert < stopat; next_to_insert++) {
// We always have a sorted array below next_to_insert
// Each item that we come to is floated down to its proper place in the sorted array
// Extract the value so we can use its space
ZTableRow value_being_inserted = self->rows[next_to_insert];
// Start comparing with the value next below this
SHORT am_i_bigger = next_to_insert - 1;
// Move everything bigger than this up a space
// to make a hole for value_being_inserted
// If we got to the bottom end of the array,
// then value_being_inserted is the smallest one yet
BYTE selected_row_may_be_moved = TRUE;
while (am_i_bigger >= 0) {
BOOL cmp = info->sortproc(
self->hwnd, &value_being_inserted, &(self->rows[am_i_bigger]),
info->column, info->lParam);
if (cmp == -2) {
self->rows[am_i_bigger + 1] = value_being_inserted;
return FALSE;
}
// If i_am_not_bigger, then to my right is
// the proper place for value_being_inserted
if ((cmp * reverse_multiplier) >= 0) break;
// If i_am_bigger than value_being_inserted, shift me up one space
self->rows[am_i_bigger + 1] = self->rows[am_i_bigger];
if (self->selectedRow == am_i_bigger) {
self->selectedRow += 1;
// We need this catch to prevent selectedRow from being
// moved because of equaling next_to_insert
// when it was merely bumped up a space to there
if (self->selectedRow == next_to_insert) {
selected_row_may_be_moved = FALSE;
}
}
// Now compare with the next one below me
am_i_bigger -= 1;
}
// Insert the value in the space it belongs in the sorted sub-array
self->rows[am_i_bigger + 1] = value_being_inserted;
if (self->selectedRow == next_to_insert && selected_row_may_be_moved) {
self->selectedRow = am_i_bigger + 1;
}
}
}
self->lastSortColumn = info->column;
CHECK(ZTableUpdateFilteredRows(self));
if (self->selectedRow != -1) {
CHECK(ZTableSeeRow(self, self->selectedRow));
}
if (self->nRowsFiltered > 1) {
CHECK(InvalidateRect(self->hwnd, NULL, FALSE));
} else {
RECT rect;
CHECK(ZTableGetCellRect(self, &rect, -1, -1));
rect.top = rect.bottom;
rect.bottom = self->cHeight;
CHECK(InvalidateRect(self->hwnd, &rect, TRUE));
}
return TRUE;
}
BOOL ZTableUpdateColors(ZTableData *self) {
self->headerTextColor = GetSysColor(COLOR_BTNTEXT);
//DeleteObject(self->headerFillHover);
//CHECK(self->headerFillHover = CreateSolidBrush(0xf9ebd9));
//DeleteObject(self->headerFillPressed);
//CHECK(self->headerFillPressed = CreateSolidBrush(0xf4dcbc));
DeleteObject(self->headerOutline);
CHECK(self->headerOutline = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_BTNSHADOW)));
DeleteObject(self->headerArrowPen);
CHECK(self->headerArrowPen = CreatePen(PS_SOLID, 2, GetSysColor(COLOR_GRAYTEXT)));
self->rowTextColor = GetSysColor(COLOR_WINDOWTEXT);
self->rowTextColorSelected = GetSysColor(COLOR_HIGHLIGHTTEXT);
DeleteObject(self->rowOutline);
CHECK(self->rowOutline = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_ACTIVEBORDER)));
self->sc_bmpfg = GetSysColor(COLOR_BTNTEXT);
return TRUE;
}
BOOL ZTableUpdateFilteredRows(ZTableData *self) {
SHORT nRowsFiltered = 0;
for (USHORT ir = 0; ir < self->nRows; ir++) {
if (self->rows[ir].filtered) {
self->rows[ir].index = ir;
self->rows[ir].indexFiltered = nRowsFiltered;
nRowsFiltered++;
}
}
self->nRowsFiltered = min(self->nRowsFiltered, nRowsFiltered);
ZTableRow **newarray;
SIZE_T newSize = nRowsFiltered * sizeof(ZTableRow *);
if (self->rowsFiltered == NULL) {
CHECK(newarray = (ZTableRow **) HeapAlloc(processHeap, HEAP_ZERO_MEMORY, newSize));
self->rowsFiltered = newarray;
} else {
SIZE_T oldSize = HeapSize(processHeap, 0, self->rowsFiltered);
if (oldSize < newSize || oldSize == 0xFFFFFFFF || (oldSize - newSize) > 0x100) {
CHECK(newarray = (ZTableRow **) HeapReAlloc(
processHeap,
HEAP_ZERO_MEMORY,
self->rowsFiltered,
newSize));
self->rowsFiltered = newarray;
}
}
for (USHORT ir = 0; ir < self->nRows; ir++) {
if (self->rows[ir].filtered) {
self->rowsFiltered[self->rows[ir].indexFiltered] = &(self->rows[ir]);
}
}
self->nRowsFiltered = nRowsFiltered;
return TRUE;
}
BOOL ZTableUpdateScrollInfo(ZTableData *self) {
LONG xrgn = 0;
for (LONG ic = 0; ic < self->nCols; ic++) {
xrgn += self->columns[ic].width;
}
self->scrollRegionX = xrgn;
self->scrollRegionY = self->nRowsFiltered * self->rowHeight;
self->pageSizeX = max(self->cWidth, self->lineSizeX);
self->scrollPosX = max(0, min(self->scrollPosX, self->scrollRegionX - self->pageSizeX));
SCROLLINFO si;