-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtable.cpp
894 lines (788 loc) · 25 KB
/
table.cpp
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
/*
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
* ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
* PARTICULAR PURPOSE.
*
* Copyright (c) Microsoft Corporation. All Rights Reserved.
*/
/*
* standard table class.
*
* main interface functions.
*
* see table.h for interface description
*/
#include "precomp.h"
#include "table.h"
#include "tpriv.h"
/* global tools etc */
extern HANDLE hLibInst;
HANDLE hVertCurs;
HANDLE hNormCurs;
HPEN hpenDotted;
UINT gtab_msgcode;
/* function prototypes */
LRESULT gtab_wndproc(HWND, UINT, WPARAM, LPARAM);
void gtab_createtools(void);
void gtab_deltable(HWND hwnd, lpTable ptab);
lpTable gtab_buildtable(HWND hwnd, DWORD_PTR id);
void gtab_setsize(HWND hwnd, lpTable ptab);
void gtab_newsize(HWND hwnd, lpTable ptab);
void gtab_calcwidths(HWND hwnd, lpTable ptab);
BOOL gtab_alloclinedata(HWND hwnd, lpTable ptab);
void gtab_invallines(HWND hwnd, lpTable ptab, int start, int count);
void gtab_append(HWND hwnd, lpTable ptab, int rows, DWORD_PTR id);
/*
* initialise window class - called from DLL main init
*/
void
gtab_init(void)
{
WNDCLASS wc;
gtab_createtools();
gtab_msgcode = RegisterWindowMessage(TableMessage);
wc.style = CS_GLOBALCLASS | CS_DBLCLKS;
wc.lpfnWndProc = (WNDPROC)gtab_wndproc;
wc.cbClsExtra = 0;
wc.cbWndExtra = WLTOTAL;
wc.hInstance = (HINSTANCE)hLibInst;
wc.hIcon = NULL;
wc.hCursor = NULL;
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszClassName = TableClassName;
wc.lpszMenuName = NULL;
RegisterClass(&wc);
}
void
gtab_createtools(void)
{
hVertCurs = LoadCursor((HINSTANCE)hLibInst, "VertLine");
hNormCurs = LoadCursor(NULL, IDC_ARROW);
hpenDotted = CreatePen(PS_DOT, 1, RGB(0, 0, 0));
}
void
gtab_deltools(void)
{
DeleteObject(hpenDotted);
}
LRESULT
gtab_wndproc(
HWND hwnd,
UINT msg,
WPARAM wParam,
LPARAM lParam
)
{
CREATESTRUCT FAR * csp;
HWND hOwner;
lpTable ptab;
lpTableSelection pselect;
long oldtop;
long change;
switch (msg) {
case WM_CREATE:
/* create window. set the wnd extra bytes to
* contain the owner window and a null table.
* Owner window is either in lParam or the parent.
* Then wait for TM_NEWID.
*/
csp = (CREATESTRUCT FAR *) lParam;
if (csp->lpCreateParams == NULL) {
hOwner = GetParent(hwnd);
} else {
hOwner = (HWND) csp->lpCreateParams;
}
ptab = NULL;
SetWindowLongPtr(hwnd, WL_TABLE, (LONG_PTR) ptab);
SetWindowLongPtr(hwnd, WW_OWNER, (LONG_PTR) hOwner);
SetScrollRange(hwnd, SB_VERT, 0, 0, TRUE);
SetScrollRange(hwnd, SB_HORZ, 0, 0, TRUE);
break;
case TM_NEWID:
/* complete change of table.
* close old table, discard memory and
* build new table
*/
ptab = (lpTable) GetWindowLongPtr(hwnd, WL_TABLE);
if (ptab != NULL) {
gtab_sendtq(hwnd, TQ_CLOSE, ptab->hdr.id);
gtab_deltable(hwnd, ptab);
SetCursor((HCURSOR)hNormCurs);
SetWindowLongPtr(hwnd, WL_TABLE, 0);
}
if ( (ptab = gtab_buildtable(hwnd, (DWORD_PTR)lParam)) != NULL) {
SetWindowLongPtr(hwnd, WL_TABLE, (LONG_PTR) ptab);
gtab_setsize(hwnd, ptab);
} else {
SetScrollRange(hwnd, SB_VERT, 0, 0, TRUE);
SetScrollRange(hwnd, SB_HORZ, 0, 0, TRUE);
}
InvalidateRect(hwnd, NULL, TRUE);
break;
case TM_NEWLAYOUT:
/* change of layout but for same id. no TQ_CLOSE,
* but otherwise same as TM_NEWID
*/
ptab = (lpTable) GetWindowLongPtr(hwnd, WL_TABLE);
if (ptab != NULL) {
gtab_deltable(hwnd, ptab);
SetCursor((HCURSOR)hNormCurs);
SetWindowLongPtr(hwnd, WL_TABLE, 0);
}
if ( (ptab = gtab_buildtable(hwnd, (DWORD_PTR)lParam)) != NULL) {
SetWindowLongPtr(hwnd, WL_TABLE, (LONG_PTR) ptab);
gtab_setsize(hwnd, ptab);
} else {
SetScrollRange(hwnd, SB_VERT, 0, 0, TRUE);
SetScrollRange(hwnd, SB_HORZ, 0, 0, TRUE);
}
InvalidateRect(hwnd, NULL, TRUE);
break;
case TM_REFRESH:
/* data in table has changed. nrows may have
* changed. ncols and col types have not changed
*/
ptab = (lpTable) GetWindowLongPtr(hwnd, WL_TABLE);
if (ptab != NULL) {
gtab_newsize(hwnd, ptab);
gtab_sendtq(hwnd, TQ_SHOWWHITESPACE, (LPARAM) &ptab->show_whitespace);
}
InvalidateRect(hwnd, NULL, TRUE);
break;
case TM_SELECT:
ptab = (lpTable) GetWindowLongPtr(hwnd, WL_TABLE);
if (ptab != NULL) {
pselect = (lpTableSelection) lParam;
gtab_select(hwnd, ptab, pselect->startrow,
pselect->startcell,
pselect->nrows,
pselect->ncells,
TRUE);
gtab_showsel_middle(hwnd, ptab, pselect->dyRowsFromTop);
}
break;
case TM_GETSELECTION:
ptab = (lpTable) GetWindowLongPtr(hwnd, WL_TABLE);
if (ptab != NULL) {
pselect = (lpTableSelection) lParam;
*pselect = ptab->select;
}
break;
case TM_PRINT:
ptab = (lpTable) GetWindowLongPtr(hwnd, WL_TABLE);
if (ptab != NULL) {
return gtab_print(hwnd, ptab, (lpPrintContext) lParam);
}
return FALSE;
case TM_SETTABWIDTH:
ptab = (lpTable) GetWindowLongPtr(hwnd, WL_TABLE);
if (!ptab)
return 0;
ptab->tabchars = (int)lParam;
InvalidateRect(hwnd, NULL, FALSE);
break;
case TM_TOPROW:
/* return top row. if wParam is TRUE, set lParam
* as the new toprow
*/
ptab = (lpTable) GetWindowLongPtr(hwnd, WL_TABLE);
if (ptab == NULL) {
return(0);
}
oldtop = ptab->toprow;
if ((wParam) && (lParam < ptab->hdr.nrows)) {
change = (long)lParam - ptab->toprow;
change -= ptab->hdr.fixedrows;
gtab_dovscroll(hwnd, ptab, change);
}
return(oldtop);
case TM_ENDROW:
/* return the last visible row in the window */
ptab = (lpTable) GetWindowLongPtr(hwnd, WL_TABLE);
if (ptab == NULL) {
return(0);
}
return(ptab->nlines + ptab->toprow - 1);
case TM_APPEND:
/* new rows have been added to the end of the
* table, but the rest of the table has not
* been changed. Update without forcing redraw of
* everything.
* lParam contains the new total nr of rows
*/
ptab = (lpTable) GetWindowLongPtr(hwnd, WL_TABLE);
if (ptab != NULL) {
gtab_append(hwnd, ptab, (int) wParam, (DWORD_PTR)lParam);
return(TRUE);
}
break;
case WM_SIZE:
ptab = (lpTable) GetWindowLongPtr(hwnd, WL_TABLE);
if (ptab != NULL) {
gtab_setsize(hwnd, ptab);
}
break;
case WM_ERASEBKGND:
return TRUE;
case WM_DESTROY:
ptab = (lpTable) GetWindowLongPtr(hwnd, WL_TABLE);
if (ptab != NULL) {
gtab_sendtq(hwnd, TQ_CLOSE, ptab->hdr.id);
gtab_deltable(hwnd, ptab);
}
break;
case WM_SYSCOLORCHANGE:
InvalidateRect(hwnd, NULL, TRUE);
break;
case WM_PAINT:
gtab_paint(hwnd);
break;
case WM_HSCROLL:
ptab = (lpTable) GetWindowLongPtr(hwnd, WL_TABLE);
if (ptab != NULL) {
gtab_msg_hscroll(hwnd, ptab,
GET_SCROLL_OPCODE(wParam, lParam),
GET_SCROLL_POS(wParam, lParam));
}
break;
case WM_VSCROLL:
ptab = (lpTable) GetWindowLongPtr(hwnd, WL_TABLE);
if (ptab != NULL) {
gtab_msg_vscroll(hwnd, ptab,
GET_SCROLL_OPCODE(wParam, lParam),
GET_SCROLL_POS(wParam, lParam));
}
break;
case WM_MOUSEMOVE:
ptab = (lpTable) GetWindowLongPtr(hwnd, WL_TABLE);
if (ptab != NULL) {
gtab_move(hwnd, ptab, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam));
} else {
SetCursor((HCURSOR)hNormCurs);
}
break;
case WM_LBUTTONDOWN:
ptab = (lpTable) GetWindowLongPtr(hwnd, WL_TABLE);
if (ptab != NULL) {
gtab_press(hwnd, ptab, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam));
}
break;
case WM_RBUTTONDOWN:
ptab = (lpTable) GetWindowLongPtr(hwnd, WL_TABLE);
if (ptab != NULL) {
gtab_rightclick(hwnd, ptab, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam));
}
break;
case WM_LBUTTONUP:
ptab = (lpTable) GetWindowLongPtr(hwnd, WL_TABLE);
if (ptab != NULL) {
gtab_release(hwnd, ptab,
(int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam));
}
break;
case WM_LBUTTONDBLCLK:
ptab = (lpTable) GetWindowLongPtr(hwnd, WL_TABLE);
if (ptab != NULL) {
gtab_dblclick(hwnd, ptab,
(int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam));
}
break;
case WM_KEYDOWN:
/* handle key presses for cursor movement about
* the table, and return/space for selection.
* Any key we don't handle is passed to the owner window
* for him to handle.
* The table window should have the focus
*/
ptab = (lpTable) GetWindowLongPtr(hwnd, WL_TABLE);
if (ptab != NULL) {
if (gtab_key(hwnd, ptab, (int)wParam) != 0) {
hOwner = (HWND) GetWindowLongPtr(hwnd, WW_OWNER);
return(SendMessage(hOwner, WM_KEYDOWN, wParam, lParam));
} else {
return(0);
}
}
break;
#ifdef WM_MOUSEWHEEL
case WM_MOUSEWHEEL:
ptab = (lpTable)GetWindowLongPtr(hwnd, WL_TABLE);
if (ptab != NULL) {
if (gtab_mousewheel(hwnd,ptab, LOWORD(wParam), (short)HIWORD(wParam))) {
hOwner = (HWND)GetWindowLongPtr(hwnd, WW_OWNER);
return SendMessage(hOwner, WM_MOUSEWHEEL, wParam, lParam);
}
}
break;
#endif
default:
return(DefWindowProc(hwnd, msg, wParam, lParam));
}
return(TRUE);
}
/*
* send a table-query message to the owner window. returns message
* value.
*/
INT_PTR
gtab_sendtq(
HWND hwnd,
UINT cmd,
LPARAM lParam
)
{
HWND hOwner;
hOwner = (HWND) GetWindowLongPtr(hwnd, WW_OWNER);
return (SendMessage(hOwner, gtab_msgcode, cmd, lParam));
}
/*
* free the memory allocated for the array of lines (each containing
* an array of Cells, each containing an array of chars for the actual
* data). Called on any occasion that would change the number of visible lines
*/
void
gtab_freelinedata(
lpTable ptab
)
{
int i, j, ncols;
lpCellData cd;
ncols = ptab->hdr.ncols;
/* for each line */
for (i = 0; i < ptab->nlines; i++) {
/* for each cell */
for (j = 0; j < ncols; j++) {
/* free up the actual text space */
cd = &ptab->pdata[i].pdata[j];
HeapFree(GetProcessHeap(), NULL, cd->ptext);
HeapFree(GetProcessHeap(), NULL, cd->pwzText);
}
/* dealloc array of CellData */
HeapFree(GetProcessHeap(), NULL, ptab->pdata[i].pdata);
}
/* de-alloc array of linedatas */
HeapFree(GetProcessHeap(), NULL, ptab->pdata);
ptab->pdata = NULL;
}
/* allocate and init array of linedatas (include cell array
* and text for each cell)
*/
BOOL
gtab_alloclinedata(
HWND hwnd,
lpTable ptab
)
{
lpLineData pline;
lpCellData cd;
int i, j;
ptab->pdata = (lpLineData) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
sizeof(LineData) * ptab->nlines);
if (ptab->pdata == NULL) {
return(FALSE);
}
for (i = 0; i < ptab->nlines; i++) {
pline = &ptab->pdata[i];
pline->linepos.size = ptab->rowheight;
pline->pdata = (lpCellData) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
sizeof(CellData) * ptab->hdr.ncols);
if (pline->pdata == NULL) {
return(FALSE);
}
for (j = 0; j < ptab->hdr.ncols; j++) {
cd = &pline->pdata[j];
cd->props.valid = 0;
cd->flags = 0;
cd->nchars = ptab->pcolhdr[j].nchars;
if (cd->nchars > 0) {
cd->ptext = (char*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cd->nchars);
if (cd->ptext == NULL) {
return(FALSE);
}
cd->pwzText = 0;
}
}
}
return(TRUE);
}
/*
* free up all table data structures. Called for new layout or new data.
*/
void
gtab_deltable(
HWND hwnd,
lpTable ptab
)
{
int ncols;
if (ptab == NULL) {
return;
}
ncols = ptab->hdr.ncols;
if (ptab->pcolhdr != NULL) {
HeapFree(GetProcessHeap(), NULL, ptab->pcolhdr);
}
if (ptab->pcellpos != NULL) {
HeapFree(GetProcessHeap(), NULL, ptab->pcellpos);
}
if (ptab->pdata != NULL) {
gtab_freelinedata(ptab);
}
HeapFree(GetProcessHeap(), NULL, ptab);
}
/*
* build up a Table struct (excluding data allocation and
* anything to do with font or window size).
* return ptr to this or NULL if error
*/
lpTable
gtab_buildtable(
HWND hwnd,
DWORD_PTR id
)
{
lpTable ptab;
int ncols, i;
ColPropsList cplist;
ptab = (lpTable) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(Table));
if (ptab == NULL) {
return(NULL);
}
// get the tab width. most clients will not support this
if (gtab_sendtq(hwnd, TQ_TABS, (LPARAM) &ptab->tabchars) == FALSE) {
ptab->tabchars = TABWIDTH_DEFAULT;
}
// get the show whitespace value
if (gtab_sendtq(hwnd, TQ_SHOWWHITESPACE, (LPARAM) &ptab->show_whitespace) == FALSE) {
ptab->show_whitespace = FALSE;
}
/* get the row/column count from owner window */
ptab->hdr.id = id;
ptab->hdr.props.valid = 0;
ptab->hdr.sendscroll = FALSE;
if (gtab_sendtq(hwnd, TQ_GETSIZE, (LPARAM) &ptab->hdr) == FALSE) {
return(NULL);
}
ncols = ptab->hdr.ncols;
ptab->pcolhdr = (lpColProps) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ColProps) * ncols);
if (ptab->pcolhdr == NULL) {
/* should prob send TQ_CLOSE at this point */
return(NULL);
}
/* init col properties to default */
for (i=0; i < ncols; i++) {
ptab->pcolhdr[i].props.valid = 0;
ptab->pcolhdr[i].nchars = 0;
}
/* get the column props from owner */
cplist.plist = ptab->pcolhdr;
cplist.id = id;
cplist.startcol = 0;
cplist.ncols = ncols;
gtab_sendtq(hwnd, TQ_GETCOLPROPS, (LPARAM) &cplist);
/* init remaining fields */
ptab->pcellpos = (lpCellPos) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CellPos) * ncols);
if (ptab->pcellpos == NULL) {
return(NULL);
}
ptab->scrollscale = 1;
ptab->scroll_dx = 0;
ptab->toprow = 0;
ptab->pdata = NULL;
ptab->nlines = 0;
ptab->trackmode = TRACK_NONE;
/* we have to notify owner of the current selection
* whenever it is changed
*/
ptab->select.id = id;
gtab_select(hwnd, ptab, 0, 0, 0, 0, TRUE);
/* calc ave height/width, cell widths and min height.
* these change only when cell properties / col count changes -
* ie only on rebuild-header events
*/
gtab_calcwidths(hwnd, ptab);
return(ptab);
}
/* set sizes that are based on window size and scroll pos
* set:
* winwidth
* nlines
* cellpos start, clip start/end
* alloc linedata and init
*/
void
gtab_setsize(
HWND hwnd,
lpTable ptab
)
{
RECT rc;
int nlines;
long change;
SCROLLINFO si;
GetClientRect(hwnd, &rc);
ptab->winwidth = rc.right - rc.left;
nlines = (rc.bottom - rc.top) / ptab->rowheight;
/* nlines is the number of whole lines - add one extra
* for the partial line at the bottom
*/
nlines += 1;
/* alloc space for nlines of data - if nlines has changed */
if (nlines != ptab->nlines) {
gtab_freelinedata(ptab);
ptab->nlines = nlines;
if (!gtab_alloclinedata(hwnd, ptab)) {
ptab->nlines = 0;
return;
}
}
si.cbSize = sizeof(si);
si.fMask = SIF_PAGE|SIF_RANGE;
si.nMin = 0;
/* set scroll vertical range */
si.nMax = ptab->hdr.nrows;
si.nPage = ptab->nlines;
if (si.nMax < 0) {
si.nMax = 0;
change = -(ptab->toprow);
} else if (ptab->toprow > si.nMax) {
change = si.nMax - ptab->toprow;
} else {
change = 0;
}
/* the scroll range must be 16-bits for Win3
* scale until this is true
*/
ptab->scrollscale = 1;
while (si.nMax > 32766) {
ptab->scrollscale *= 16;
si.nMax /= 16;
si.nPage /= 16;
}
if (!si.nPage)
si.nPage = 1;
SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
gtab_dovscroll(hwnd, ptab, change);
/* set horz scroll range */
si.nMax = ptab->rowwidth;
si.nPage = ptab->winwidth;
if (si.nMax < 0) {
si.nMax = 0;
change = -(ptab->scroll_dx);
} else if (ptab->scroll_dx > si.nMax) {
change = si.nMax - ptab->scroll_dx;
} else {
change = 0;
}
/* horz scroll range will always be < 16 bits */
SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
gtab_dohscroll(hwnd, ptab, change);
}
/* set column widths/height and totals (based on column props)
* - no assumption of window size (see gtab_setsize)
* sets avewidth,rowheight,cellpos.size,rowwidth (total of cellpos.size)
*/
void
gtab_calcwidths(
HWND hwnd,
lpTable ptab
)
{
int i, cxtotal, cx, ave;
TEXTMETRIC tm = {0};
TEXTMETRIC tmcol = {0};
HDC hdc;
lpProps hdrprops, cellprops;
HFONT hfont;
hfont = NULL; /* eliminate spurious diagnostic, make code worse */
hdrprops = &ptab->hdr.props;
hdc = GetDC(hwnd);
if (hdc)
{
GetTextMetrics(hdc, &tm);
ptab->rowheight = tm.tmHeight + tm.tmExternalLeading;
if (hdrprops->valid & P_FONT) {
hfont = (HFONT)SelectObject(hdc, hdrprops->hFont);
}
GetTextMetrics(hdc, &tm);
if (hdrprops->valid & P_FONT) {
SelectObject(hdc, hfont);
}
ReleaseDC(hwnd, hdc);
}
else
{
// arbitrary, whatever...
ptab->rowheight = 14;
tm.tmHeight = 14;
tm.tmAveCharWidth = 5;
}
/* get width and height of average character */
ptab->avewidth = tm.tmAveCharWidth;
if (tm.tmHeight + tm.tmExternalLeading < ptab->rowheight - 2 ||
tm.tmHeight + tm.tmExternalLeading > ptab->rowheight) {
// make sure that the default FixedSys (and anything of similar size)
// doesn't vertically clip the System font used for line numbers,
// filenames, etc.
ptab->rowheight = tm.tmHeight;
if (tm.tmExternalLeading)
ptab->rowheight += tm.tmExternalLeading;
else
ptab->rowheight++;
}
if (hdrprops->valid & P_HEIGHT) {
ptab->rowheight = hdrprops->height;
}
/* set pixel width of each cell (and add up for row total)
* based on ave width * nr chars, unless P_WIDTH set
*/
cxtotal = 0;
for (i = 0; i < ptab->hdr.ncols; i++) {
cellprops = &ptab->pcolhdr[i].props;
if (cellprops->valid & P_WIDTH) {
cx = cellprops->width;
} else if (hdrprops->valid & P_WIDTH) {
cx = hdrprops->width;
} else {
if (cellprops->valid & P_FONT) {
hdc = GetDC(hwnd);
if (hdc)
{
hfont = (HFONT)SelectObject(hdc, cellprops->hFont);
GetTextMetrics(hdc, &tmcol);
SelectObject(hdc, hfont);
ReleaseDC(hwnd, hdc);
ave = tmcol.tmAveCharWidth;
}
else
ave = 5;
} else {
ave = ptab->avewidth;
}
/* ave width * nchars */
cx = ptab->pcolhdr[i].nchars + 1;
cx *= ave;
}
/* add 2 pixels for box lines */
cx += 2;
ptab->pcellpos[i].size = cx;
cxtotal += cx;
}
ptab->rowwidth = cxtotal;
}
/* called when row data + possible nrows changes.
* other changes are ignored
*/
void
gtab_newsize(
HWND hwnd,
lpTable ptab
)
{
TableHdr hdr;
/* get new row count */
hdr = ptab->hdr;
gtab_sendtq(hwnd, TQ_GETSIZE, (LPARAM) &hdr);
if (hdr.nrows != ptab->hdr.nrows) {
ptab->hdr.nrows = hdr.nrows;
gtab_setsize(hwnd, ptab);
}
gtab_invallines(hwnd, ptab, 0, ptab->nlines);
InvalidateRect(hwnd, NULL, FALSE);
}
void
gtab_invallines(
HWND hwnd,
lpTable ptab,
int start,
int count
)
{
int i, j;
for (i = start; i < start + count; i++) {
for (j = 0; j < ptab->hdr.ncols; j++) {
ptab->pdata[i].pdata[j].flags = 0;
}
}
}
/* new rows have been added to the table. adjust the scroll range and
* position, and redraw the rows if the end of the table is currently
* visible.
* rows = the new total row count.
*/
void
gtab_append(
HWND hwnd,
lpTable ptab,
int rows,
DWORD_PTR id
)
{
long oldrows;
int line, nupdates;
RECT rc;
SCROLLINFO si;
/* change to the new id */
ptab->hdr.id = id;
ptab->select.id = id;
/* update the header, but remember the old nr of rows
* so we know where to start updating
*/
oldrows = ptab->hdr.nrows;
/* check that the new nr of rows is not smaller.
*/
if (oldrows >= rows) {
return;
}
ptab->hdr.nrows = rows;
si.cbSize = sizeof(si);
si.fMask = SIF_PAGE|SIF_RANGE;
si.nMin = 0;
/* set the vertical scroll range */
si.nMax = rows;
si.nPage = ptab->nlines;
if (si.nMax < 0) {
si.nMax = 0;
}
/* force the scroll range into 16-bits for win 3.1 */
ptab->scrollscale = 1;
while (si.nMax > 32766) {
ptab->scrollscale *= 16;
si.nMax /= 16;
si.nPage /= 16;
}
if (!si.nPage)
si.nPage = 1;
/* now set the scroll bar range and position */
SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
if (si.nMax > 0) {
SetScrollPos(hwnd, SB_VERT,
(int) (ptab->toprow / ptab->scrollscale), TRUE);
}
/* calculate which screen lines need to be updated - find what
* screen line the start of the new section is at
*/
line = gtab_rowtoline(hwnd, ptab, oldrows);
if (line == -1) {
/* not visible -> no more to do */
return;
}
/* how many lines to update - rest of screen or nr of
* new lines if less than rest of screen
*/
nupdates = min((ptab->nlines - line), (int)(rows - oldrows));
/* invalidate the screen line buffers to indicate data
* needs to be refetch from parent window
*/
gtab_invallines(hwnd, ptab, line, nupdates);
/* calculate the region of the screen to be repainted -
* left and right are same as window. top and bottom
* need to be calculated from screen line height
*/
GetClientRect(hwnd, &rc);
rc.top += line * ptab->rowheight;
rc.bottom = rc.top + (nupdates * ptab->rowheight);
/* force a repaint of the updated region */
InvalidateRect(hwnd, &rc, FALSE);
}