-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDataGridView.c
More file actions
2024 lines (1765 loc) · 63.4 KB
/
DataGridView.c
File metadata and controls
2024 lines (1765 loc) · 63.4 KB
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
/****************************************************************************\
*
* FILE: DataGridView.c
*
* PURPOSE: ListView based Datagrid with Editable subItems
*
* COMMENTS:
* This source 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.
*
* FUNCTIONS:
* EXPORTS:
* InitDataGridView() - Initialize and register the custom control
*
* ToDo: add export functions here
* LOCALS:
* Grid_GetInstanceData() - Get the Instance data associated with this instance.
* Grid_CreateInstanceData() - Allocate the Instance data associated with this instance.
* Grid_FreeInstanceData() - Free the instance data allocation of an instance of the Grid Control.
* Grid_GetColumnCount() - Under any condition get the number of columns in grid.
* ListView_GetFirstVisibleRow() - Get the row number of the first visible row.
* ListView_GetLastVisibleRow() - Get the row number of the last visible row.
* ListView_GetRowHeight() - Get the hight of the list view component rows.
* ListView_GetLastVisibleColWidth() - Get the width of the last visible col.
* ListView_GetFirstVisibleColWidth() - Get the width of the first visible col.
* SetColor() - Set the colors used to paint controls in OnCtlColor....
* Grid_OnCtlColorEdit() - Set the colors of the Editor's Edit Control
* CreateListView() - Creates the List View
* CreateCellEditor() - Creates the Cell Editor
* Edit_CenterTextVertically() - Draw vertically centered text in editor
* Grid_SetRowHeight() - Hack - Use image list to set row height
* Grid_OnCreate() - Handles WM_CREATE
* Grid_OnDestroy() - Handles WM_DESTROY
* Grid_OnGetDlgCode() - Handles WM_GETDLGCODE sent by a dialog procedure if control is
* used in a dialog
* Grid_OnSize() - Handles WM_SIZE
* Grid_OnSetFocus() - Handles WM_SETFOCUS
* Grid_OnSetCursor() - The cursor changes when the mouse leaves the header, this is the
* only easy indication that a column resize has been compleated.
* I use the event to crop the grid back to its data and to reposition
* the editor
* Grid_OnColumnResize() - Handles the HDN_ITEMCHANGING notification due to a resize attempt
* Grid_OnCustomDraw() - Define the look and feel of the Grid
* Grid_OnMouseClick() - Handles mouse clicks on the ListView
* Grid_OnNotify() - Handles notification messages for control's components
* Grid_OnCommand() - Handles notification codes of control's components
* ListView_Proc() - Window Procedure for the ListView
* Editor_Proc() - Window Procedure for the Editor
* Grid_WndProc() - Window Procedure for the Data View Grid
*
* Copyright 2007 David MacDermot.
*
* History:
* Aug '07 - Created
* July '09 - Added Unicode support
* Improved behavior of edit box during scrolling
* Improved redraw of grid lines
* Changed Get(Set)WindowLong to Get(Set)WindowLongPtr for 64bit compatability
*
\****************************************************************************/
//#define UNICODE
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>
#include "DataGridView.h"
#define ID_LISTVIEW 2000
#define ID_EDIT 2001
/****************************************************************************/
// DataGridView.c Macroe Definitions
#define NELEMS(a) (sizeof(a) / sizeof((a)[0]))
#define Refresh(hWnd) RedrawWindow(hWnd,NULL,NULL,RDW_NOERASE|RDW_INVALIDATE|RDW_ALLCHILDREN|RDW_UPDATENOW);
/****************************************************************************/
// DataGridView.c Structure
typedef struct _tagINSTANCEDATA {
HINSTANCE hInstance;
HWND hwndList;
HWND hwndEditor;
LVHITTESTINFO hti;
COLORREF Editor_TxtColr;
COLORREF Editor_BkColr;
COLORREF Cell_AltTxtColr;
COLORREF Cell_AltBkColr;
COLORREF Cell_ChangedTxtColr;
COLORREF Cell_ChangedBkColr;
COLORREF Cell_FaultyTxtColr;
COLORREF Cell_FaultyBkColr;
COLORREF Editor_ChangedTxtColr;
COLORREF Editor_ChangedBkColr;
COLORREF Editor_FaultyTxtColr;
COLORREF Editor_FaultyBkColr;
BOOL fPaintByRow;
BOOL fRowHeaders;
BOOL fResizableHeader;
BOOL fExtendLastCol;
BOOL fSuppressEraseBackground;
BOOL fsizeCol;
CHECKCHANGED pfnChangedCB;
} INSTANCEDATA, *LPINSTANCEDATA;
/****************************************************************************/
// DataGridView.c Internal Globals
LPINSTANCEDATA g_lpInst;
TCHAR g_szClassName[] = TEXT("DGridVwClass");
/****************************************************************************/
// DataGridView.c Function prototypes
static LRESULT CALLBACK Grid_WndProc(HWND, UINT, WPARAM, LPARAM);
static LRESULT CALLBACK Editor_Proc(HWND, UINT, WPARAM, LPARAM);
static LRESULT CALLBACK ListView_Proc(HWND, UINT, WPARAM, LPARAM);
/****************************************************************************
*
* FUNCTION: Grid_GetInstanceData
*
* PURPOSE: Get the Instance data associated with this instance.
*
* PARAMS: HWND hGrid - Handle to Current instance
* LPINSTANCEDATA *ppInstanceData - pointer to the address of an INSTANCEDATA struct
*
* RETURNS: BOOL - TRUE if successful
*
* History:
* August '07 - Created
*
\****************************************************************************/
static BOOL Grid_GetInstanceData(HWND hGrid, LPINSTANCEDATA * ppInstanceData)
{
*ppInstanceData = (LPINSTANCEDATA)GetProp(hGrid, TEXT("lpInsData"));
if (NULL != *ppInstanceData)
return TRUE;
return FALSE;
}
/****************************************************************************
*
* FUNCTION: Grid_CreateInstanceData
*
* PURPOSE: Allocate the Instance data associated with this instance.
*
* PARAMS: HWND hGrid - Handle to Current instance
* LPINSTANCEDATA pInstanceData - pointer to an INSTANCEDATA struct
*
* RETURNS: BOOL - TRUE if successful
*
* History:
* August '07 - Created
*
\****************************************************************************/
static BOOL Grid_CreateInstanceData(HWND hGrid, LPINSTANCEDATA pInstanceData)
{
LPINSTANCEDATA pInst = (LPINSTANCEDATA)malloc(sizeof(INSTANCEDATA));
memmove(pInst, pInstanceData, sizeof(INSTANCEDATA));
return SetProp(hGrid, TEXT("lpInsData"), pInst);
}
/****************************************************************************
*
* FUNCTION: Grid_FreeInstanceData
*
* PURPOSE: Free the instance data allocation of an instance of the Grid Control.
*
* PARAMS: HWND hGrid - Handle to Current instance
*
* RETURNS: BOOL - TRUE if successful
*
* History:
* August '07 - Created
*
\****************************************************************************/
static BOOL Grid_FreeInstanceData(HWND hGrid)
{
LPINSTANCEDATA pInst;
if (Grid_GetInstanceData(hGrid, &pInst))
{
free((LPINSTANCEDATA)pInst);
RemoveProp(hGrid, TEXT("lpInsData"));
return TRUE;
}
return FALSE;
}
/****************************************************************************
*
* FUNCTION: Grid_GetColumnCount
*
* PURPOSE: Under any condition get the number of columns in grid.
*
* PARAMS: HWND hwnd - Handle of Grid
*
* RETURNS: INT - number of columns
*
* History:
* August '07 - Created
*
\****************************************************************************/
static INT Grid_GetColumnCount(HWND hwnd)
{
// Method: hittest low right corner of row
BOOL fTempRow = FALSE;
RECT rc;
LVHITTESTINFO ht = { 0 };
if (0 == ListView_GetItemCount(hwnd)) // No rows yet
// Temporarily add a row in order to get the count
{
LV_ITEM Lv_i = { LVIF_TEXT, 0, 0, 0, 0, TEXT("") };
ListView_InsertItem(hwnd, &Lv_i);
fTempRow = TRUE;
}
ListView_GetItemRect(hwnd, ListView_GetItemCount(hwnd) - 1, &rc, LVIR_BOUNDS);
ht.pt.x = rc.right - 1;
ht.pt.y = rc.bottom - 1;
ListView_SubItemHitTest(hwnd, &ht);
if (fTempRow)
ListView_DeleteItem(hwnd, 0); //Delete temp row (if any)
return ht.iSubItem + 1;
}
/****************************************************************************
*
* FUNCTION: ListView_GetFirstVisibleRow
*
* PURPOSE: Get the row number of the first visible row.
*
* PARAMS: HWND hwnd - Handle of ListView
*
* PRECT lprc - pointer to rectangle (Grid Control)
*
* RETURNS: INT - number of last row
*
* History:
* Sept '07 - Created
*
\****************************************************************************/
static INT ListView_GetFirstVisibleRow(HWND hList)
{
RECT rc;
LVHITTESTINFO hti = { 0 };
GetClientRect(g_lpInst->hwndList, &rc);
hti.pt.x = rc.left + 10;
hti.pt.y = rc.top + 10;
ListView_SubItemHitTest(hList, &hti);
return hti.iItem + 1;
}
/****************************************************************************
*
* FUNCTION: ListView_GetLastVisibleRow
*
* PURPOSE: Get the row number of the last visible row.
*
* PARAMS: HWND hwnd - Handle of ListView
*
* PRECT lprc - pointer to rectangle (Grid Control)
*
* RETURNS: INT - number of last row
*
* History:
* Sept '07 - Created
*
\****************************************************************************/
static INT ListView_GetLastVisibleRow(HWND hList)
{
RECT rc;
LVHITTESTINFO hti = { 0 };
GetClientRect(g_lpInst->hwndList, &rc);
hti.pt.x = rc.left + 10;
hti.pt.y = rc.bottom - 10;
ListView_SubItemHitTest(hList, &hti);
return hti.iItem;
}
/****************************************************************************
*
* FUNCTION: ListView_GetRowHeight
*
* PURPOSE: Get the hight of the list view component rows.
*
* PARAMS: HWND hwnd - Handle of ListView
*
* RETURNS: INT - height of first row
*
* History:
* Sept '07 - Created
*
\****************************************************************************/
static INT ListView_GetRowHeight(HWND hList)
{
RECT rc;
if (ListView_GetItemRect(hList, 0, &rc, LVIR_BOUNDS))
return rc.bottom - rc.top;
else
return 0;
}
/****************************************************************************
*
* FUNCTION: ListView_GetLastVisibleColWidth
*
* PURPOSE: Get the width of the last visible col.
*
* PARAMS: HWND hwnd - Handle of ListView
*
* PRECT lprc - pointer to rectangle (Grid Control)
*
* RETURNS: INT - width of last col
*
* History:
* Sept '07 - Created
*
\****************************************************************************/
static INT ListView_GetLastVisibleColWidth(HWND hList, PRECT lprc)
{
LVHITTESTINFO hti = { 0 };
hti.pt.x = lprc->right;
hti.pt.y = lprc->bottom;
MapWindowPoints(GetParent(hList), hList, &hti.pt, 1);
ListView_SubItemHitTest(hList, &hti);
if (hti.iSubItem < 0)
return 0;
else
return ListView_GetColumnWidth(hList, hti.iSubItem);
}
/****************************************************************************
*
* FUNCTION: ListView_GetFirstVisibleColWidth
*
* PURPOSE: Get the width of the first visible col.
*
* PARAMS: HWND hwnd - Handle of ListView
*
* PRECT lprc - pointer to rectangle (Grid Control)
*
* RETURNS: INT - width of first col
*
* History:
* Sept '07 - Created
*
\****************************************************************************/
static INT ListView_GetFirstVisibleColWidth(HWND hList, PRECT lprc)
{
LVHITTESTINFO hti = { 0 };
hti.pt.x = lprc->left + 10;
hti.pt.y = lprc->top;
MapWindowPoints(GetParent(hList), hList, &hti.pt, 1);
ListView_SubItemHitTest(hList, &hti);
if (hti.iSubItem < 0)
return 0;
else
return ListView_GetColumnWidth(hList, hti.iSubItem);
}
/****************************************************************************
*
* FUNCTION: SetColor
*
* PURPOSE: Set the colors used to paint controls in OnCtlColor....
*
* PARAMS: COLORREF TxtColr - Desired text color
* COLORREF BkColr - Desired back color
* HDC hdc - Handle of a device context
*
* RETURNS: HBRUSH - A reusable brush object
*
* History:
* August '07 - Created
*
\****************************************************************************/
HBRUSH SetColor(COLORREF TxtColr, COLORREF BkColr, HDC hdc)
{
static HBRUSH ReUsableBrush;
DeleteObject(ReUsableBrush);
ReUsableBrush = CreateSolidBrush(BkColr);
SetTextColor(hdc, TxtColr);
SetBkColor(hdc, BkColr);
return ReUsableBrush;
}
/****************************************************************************
*
* FUNCTION: Grid_OnCtlColorEdit
*
* PURPOSE: Set the colors of the Editor's Edit Control
*
* PARAMS: HWND hwnd - Handle of Grid
* HDC hdc - Handle of a device context
* HWND hwndChild - Handle of the Edit Control to custom paint
* INT type - CTLCOLOR_EDIT
*
* RETURNS: HBRUSH - A reusable brush object
*
* History:
* August '07 - Created
*
\****************************************************************************/
HBRUSH Grid_OnCtlColorEdit(HWND hwnd, HDC hdc, HWND hwndChild, INT type)
{
//menno: set color based on state
if (g_lpInst->hwndEditor == hwndChild)
{
int c = 0;
if (g_lpInst->pfnChangedCB != NULL)
{
c = g_lpInst->pfnChangedCB(g_lpInst->hti.iItem);
}
if (c == 1)
return SetColor(g_lpInst->Editor_TxtColr, g_lpInst->Editor_ChangedBkColr, hdc);
else if (c == 2)
return SetColor(g_lpInst->Editor_TxtColr, g_lpInst->Editor_FaultyBkColr, hdc);
else
return SetColor(g_lpInst->Editor_TxtColr, g_lpInst->Editor_BkColr, hdc);
}
return NULL;
}
/****************************************************************************
*
* FUNCTION: CreateListView
*
* PARAMS: HINSTANCE hInstance - handle of application instance
* HWND hwndParent - handle of list view parent
*
* RETURNS: HWND - the handle of the list view
*
* History:
* Aug '07 - Created
*
\****************************************************************************/
static HWND CreateListView(HINSTANCE hInstance, HWND hwndParent)
{
DWORD dwStyle, dwExStyle;
HWND hwnd;
// Create the ListView
dwStyle = WS_CHILD | WS_VISIBLE | LVS_REPORT | LVS_SINGLESEL | WS_CLIPCHILDREN;
dwExStyle = WS_EX_LEFT;
hwnd = CreateWindowEx(
dwExStyle, // ex style
WC_LISTVIEW, // class name - defined in commctrl.h
TEXT(""), // dummy text
dwStyle, // style
0, // x position
0, // y position
0, // width
0, // height
hwndParent, // parent
(HMENU)ID_LISTVIEW, // ID
hInstance, // instance
NULL); // no extra data
if (!hwnd)
return NULL;
SendMessage(hwnd, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), 0);
// Subclass ListView and save the OldProc
SetProp(hwnd, TEXT("Wprc"), (HANDLE)GetWindowLongPtr(hwnd, GWL_WNDPROC));
SubclassWindow(hwnd, ListView_Proc);
return hwnd;
}
/****************************************************************************
*
* FUNCTION: CreateCellEditor
*
* PARAMS: HINSTANCE hInstance - handle of application instance
* HWND hwndParent - handle of list view parent
*
* RETURNS: HWND - the handle of the CellEditor
*
* History:
* Aug '07 - Created
*
\****************************************************************************/
static HWND CreateCellEditor(HINSTANCE hInstance, HWND hwndParent)
{
DWORD dwStyle, dwExStyle;
HWND hwnd;
dwStyle = WS_CHILD | WS_BORDER | ES_LEFT | ES_MULTILINE | ES_WANTRETURN;
dwExStyle = WS_EX_TRANSPARENT | WS_EX_LEFT;
hwnd = CreateWindowEx(
dwExStyle, // ex style
WC_EDIT, // class name - defined in commctrl.h
TEXT(""), // dummy text
dwStyle, // style
0, // x position
0, // y position
0, // width
0, // height
hwndParent, // parent
(HMENU)ID_EDIT, // ID
hInstance, // instance
NULL); // no extra data
if (!hwnd)
return NULL;
SendMessage(hwnd, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), 0L);
// Subclass Editor and save the OldProc
SetProp(hwnd, TEXT("Wprc"), (HANDLE)GetWindowLongPtr(hwnd, GWL_WNDPROC));
SubclassWindow(hwnd, Editor_Proc);
return hwnd;
}
/****************************************************************************
*
* FUNCTION: Edit_CenterTextVertically
*
* PARAMS: HWND hwnd - handle of an edit control
*
* RETURNS: VOID
*
* History:
* Jul '09 - Created
*
\****************************************************************************/
VOID Edit_CenterTextVertically(HWND hwnd)
{
RECT rcTxt = { 0, 0, 0, 0 };
RECT rcEdt = { 0, 0, 0, 0 };
HDC hdc;
//calculate client area height needed for a font
hdc = GetDC(hwnd);
DrawText(hdc, TEXT("Ky"), 2, &rcTxt, DT_CALCRECT | DT_LEFT);
ReleaseDC(hwnd, hdc);
// Set top and left margins
GetClientRect(hwnd, &rcEdt);
rcEdt.left += 4;
rcEdt.top = ((rcEdt.bottom - (rcTxt.bottom - rcTxt.top)) / 2);
Edit_SetRect(hwnd, &rcEdt);
}
/****************************************************************************
*
* FUNCTION: Grid_SetRowHeight
*
* PURPOSE: Hack - Use image list to set row height.
*
* PARAMS: HWND hList - Handle to listview
* INT iRowHeight - Desired row height
*
* RETURNS: VOID
*
* History:
* Sep '07 - Created
*
\****************************************************************************/
VOID Grid_SetRowHeight(HWND hList, INT iRowHeight)
{
static HIMAGELIST hIconList;
ImageList_Destroy(hIconList);
hIconList = ImageList_Create(1, iRowHeight, ILC_COLOR, 0, 1);
ListView_SetImageList(hList, hIconList, LVSIL_SMALL);
}
/****************************************************************************
*
* FUNCTION: Grid_OnCreate
*
* PURPOSE: Handles WM_CREATE
*
* PARAMS: HWND hwnd - handle of Control
* LPCREATESTRUCT lpCreateStruct - structure with creation data
*
* RETURNS: BOOL - If an application processes this message,
* it should return TRUE to continue creation of the window.
*
* History:
* Aug '07 - Created
*
\****************************************************************************/
static BOOL Grid_OnCreate(HWND hwnd, LPCREATESTRUCT lpCreateStruct)
{
INSTANCEDATA inst;
// initialize hit test info and scroll info
memset(&inst.hti, -1, sizeof(LVHITTESTINFO));
// get the hInstance
inst.hInstance = lpCreateStruct->hInstance;
// create the ListView control
inst.hwndList = CreateListView(lpCreateStruct->hInstance, hwnd);
if (NULL == inst.hwndList)
return FALSE;
//ListView_SetExtendedListViewStyleEx(inst.hwndList, LVS_EX_DOUBLEBUFFER, LVS_EX_DOUBLEBUFFER);
// gridlines default
ListView_SetExtendedListViewStyleEx(inst.hwndList, LVS_EX_GRIDLINES | LVS_EX_DOUBLEBUFFER, LVS_EX_GRIDLINES | LVS_EX_DOUBLEBUFFER);
// default ListView Colors
inst.fPaintByRow = TRUE;
inst.Cell_AltTxtColr = ListView_GetTextColor(inst.hwndList);
inst.Cell_AltBkColr = ListView_GetBkColor(inst.hwndList); //White
// default ListView Colors for changed items
inst.Cell_ChangedBkColr = RGB(200, 255, 200);
inst.Cell_ChangedTxtColr = RGB(0, 55, 0);
inst.Cell_FaultyBkColr = RGB(255, 200, 200);
inst.Cell_FaultyTxtColr = RGB(55, 0, 0);
inst.pfnChangedCB = (CHECKCHANGED)lpCreateStruct->lpCreateParams;
// default ListView pseudoHeaders off
inst.fRowHeaders = FALSE;
inst.fExtendLastCol = FALSE;
inst.hwndEditor = CreateCellEditor(lpCreateStruct->hInstance, hwnd);
if (NULL == inst.hwndEditor)
return FALSE;
// default Cell Editor Colors
inst.Editor_BkColr = ListView_GetBkColor(inst.hwndList);
inst.Editor_TxtColr = ListView_GetTextColor(inst.hwndList);
// defualt editor colors for changed items
inst.Editor_ChangedBkColr = RGB(155, 200, 155);
inst.Editor_ChangedTxtColr = RGB(55, 0, 0);
inst.Editor_FaultyBkColr = RGB(200, 155, 155);
inst.Editor_FaultyTxtColr = RGB(55, 0, 0);
Grid_SetRowHeight(inst.hwndList, 20);
return Grid_CreateInstanceData(hwnd, &inst);
}
/****************************************************************************
*
* FUNCTION: Grid_OnDestroy
*
* PURPOSE: Handles WM_DESTROY
*
* PARAMS: HWND hwnd - handle of Control
*
* RETURNS: VOID
*
* History:
* Aug '07 - Created
*
\****************************************************************************/
static VOID Grid_OnDestroy(HWND hwnd)
{
DestroyWindow(g_lpInst->hwndList);
DestroyWindow(g_lpInst->hwndEditor);
Grid_FreeInstanceData(hwnd);
PostQuitMessage(0);
}
/****************************************************************************
*
* FUNCTION: Grid_OnGetDlgCode
*
* PURPOSE: Handles WM_GETDLGCODE sent by a dialog procedure if control is
* used in a dialog.
*
* PARAMS: HWND hwnd - handle of control
* LPMSG lpmsg - If lpmsg is not NULL, it is a far pointer to an MSG
* structure that contains a message that is being sent to
* the control. Keyboard messages Include WM_KEYDOWN,
* WM_SYSCHAR, and WM_CHAR.
*
* RETURNS: BOOL - TRUE if handled
*
* History:
* Aug '07 - Created
*
\****************************************************************************/
static UINT Grid_OnGetDlgCode(HWND hwnd, LPMSG lpmsg)
{
return DLGC_WANTALLKEYS;
}
/****************************************************************************
*
* FUNCTION: Grid_OnSize
*
* PURPOSE: Handles WM_SIZE
*
* PARAMS: HWND hwnd - handle of Control
* UINT state - Specifies the type of resizing requested
* INT cx - width of client area
* INT cy - height of client area
*
* RETURNS: VOID
*
* History:
* Aug '07 - Created
*
\****************************************************************************/
static VOID Grid_OnSize(HWND hwnd, UINT state, INT cx, INT cy)
{
RECT rc1, rc2;
int iWidth;
//menno
// Resize parent (So the listview will remain completely visible after resize)
SetWindowPos(hwnd, NULL, 0, 0, cx, cy, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
//Size ListView component to fill parent
SetWindowPos(g_lpInst->hwndList, NULL, 0, 0, cx, cy, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
if (g_lpInst->fExtendLastCol) //Pin last column to right side
{
int iCount = Grid_GetColumnCount(g_lpInst->hwndList);
if (iCount < 1)
return;
ListView_GetSubItemRect(g_lpInst->hwndList, 0, iCount - 1, LVIR_BOUNDS, &rc1);
GetClientRect(g_lpInst->hwndList, &rc2);
iWidth = rc2.right - rc2.left - rc1.left; // - GetSystemMetrics(SM_CXVSCROLL);
ListView_SetColumnWidth(g_lpInst->hwndList, iCount - 1, 20 > iWidth ? 20 : iWidth);
//menno
// On resize, the editor box should resize accordingly
// This assumes automatic resizing of last column, I guess...
ListView_GetSubItemRect(g_lpInst->hwndList, g_lpInst->hti.iItem, g_lpInst->hti.iSubItem, LVIR_LABEL, &rc1);
//Map the SubItem rectangle to the parent dialog so that
//we can accurately position the Edit.
MapWindowPoints(g_lpInst->hwndList, hwnd, (LPPOINT)& rc1.left, 2);
SetWindowPos(g_lpInst->hwndEditor, NULL, rc1.left, rc1.top, rc1.right - rc1.left, rc1.bottom - rc1.top, SWP_NOZORDER | SWP_DEFERERASE);
}
//GetClientRect(hwnd, &rc1);
//rc1.right = ListView_GetColumnWidth(g_lpInst->hwndList, 0);
//g_lpInst->fSuppressEraseBackground = TRUE; //Supress just once
//InvalidateRect(g_lpInst->hwndList, &rc1, FALSE);
}
/****************************************************************************
*
* FUNCTION: Grid_OnSetFocus
*
* PURPOSE: Handles WM_SETFOCUS
*
* PARAMS: HWND hwnd - handle of Control
* HWND hwndOldFocus - handle of Control that previously held focus
*
* RETURNS: VOID
*
* History:
* Jul '09 - Created
*
\****************************************************************************/
void Grid_OnSetFocus(HWND hwnd, HWND hwndOldFocus)
{
SetFocus(g_lpInst->hwndEditor);
//SetFocus(g_lpInst->hwndList);
//Edit_CenterTextVertically(g_lpInst->hwndEditor);
}
/****************************************************************************
*
* FUNCTION: Grid_OnSetCursor
*
* PURPOSE: The cursor changes when the mouse leaves the header, this is the
* only easy indication that a column resize has been compleated.
* I use the event to crop the grid back to its data and to reposition
* the editor.
*
* PARAMS: HWND hwnd - handle of control
* HWND hwndCursor - the handle of the cursor
* UINT codeHitTest - hit test code
* UINT msg - error message related to hit test
*
* RETURNS: BOOL - TRUE if handled
*
* History:
* Aug '07 - Created
*
\****************************************************************************/
BOOL Grid_OnSetCursor(HWND hwnd, HWND hwndCursor, UINT codeHitTest, UINT msg)
{
RECT rc1, rc2;
int iWidth;
//
// In Grid_OnColumnResize() we set g_fsizeCol when we clicked on the listview header;
// now we have a means of knowing that the uncaptured cursor has
// just slipped off the header.
//
if (g_lpInst->fsizeCol)
{
if (g_lpInst->fExtendLastCol) //Pin last column to right side
{
int iCount = Grid_GetColumnCount(g_lpInst->hwndList);
if (iCount >= 1)
{
ListView_GetSubItemRect(g_lpInst->hwndList, 0, iCount - 1, LVIR_BOUNDS, &rc1);
GetClientRect(g_lpInst->hwndList, &rc2);
iWidth = rc2.right - rc2.left - rc1.left; // - GetSystemMetrics(SM_CXVSCROLL);
ListView_SetColumnWidth(g_lpInst->hwndList, iCount - 1, 20 > iWidth ? 20 : iWidth);
ListView_GetSubItemRect(g_lpInst->hwndList, g_lpInst->hti.iItem, g_lpInst->hti.iSubItem, LVIR_LABEL, &rc1);
//Map the SubItem rectangle to the parent dialog so that
//we can accurately position the Edit.
MapWindowPoints(g_lpInst->hwndList, hwnd, (LPPOINT)& rc1.left, 2);
SetWindowPos(g_lpInst->hwndEditor, NULL, rc1.left, rc1.top, rc1.right - rc1.left, rc1.bottom - rc1.top, SWP_NOZORDER | SWP_DEFERERASE);
}
}
GetClientRect(hwnd, &rc1);
rc1.right = ListView_GetColumnWidth(g_lpInst->hwndList, 0);
g_lpInst->fSuppressEraseBackground = TRUE; //Supress just once
InvalidateRect(g_lpInst->hwndList, &rc1, FALSE);
//RePosition the editor
ListView_GetSubItemRect(g_lpInst->hwndList, g_lpInst->hti.iItem, g_lpInst->hti.iSubItem, LVIR_LABEL, &rc1);
MapWindowPoints(g_lpInst->hwndList, hwnd, (LPPOINT)& rc1.left, 2);
MoveWindow(g_lpInst->hwndEditor, rc1.left, rc1.top, rc1.right - rc1.left, rc1.bottom - rc1.top, TRUE);
// Show the editor
Edit_CenterTextVertically(g_lpInst->hwndEditor);
ShowWindow(g_lpInst->hwndEditor, TRUE);
SetFocus(g_lpInst->hwndEditor);
Edit_SetSel(g_lpInst->hwndEditor, 0, -1);
g_lpInst->fsizeCol = FALSE;
}
return FALSE; //Since this is not the standard use of this event don't handle it.
}
/****************************************************************************
*
* FUNCTION: Grid_OnColumnResize
*
* PURPOSE: Handles the HDN_ITEMCHANGING notification due to a resize attempt.
*
* PARAMS: HWND hwnd - handle of control
* LPNMHDR pnm - pointer to the notification message header
*
* RETURNS: BOOL - TRUE = non resizeable, FALSE = do resize
*
* History:
* Sep '07 - Created
*
\****************************************************************************/
BOOL Grid_OnColumnResize(HWND hwnd, LPNMHDR pnm)
{
RECT rc1;
if (!g_lpInst->fResizableHeader)
return TRUE;
ShowWindow(g_lpInst->hwndEditor, FALSE);
g_lpInst->fsizeCol = TRUE;
//GetClientRect(hwnd, &rc1);
//SendMessage(hwnd, WM_SIZE, SIZE_RESTORED, MAKELPARAM(rc1.right, rc1.bottom));
return FALSE;
}
/****************************************************************************
*
* FUNCTION: Grid_OnCustomDraw
*
* PURPOSE: Define the look and feel of the Grid.
*
* PARAMS: HWND hwnd - handle of control
* LPNMLVCUSTOMDRAW lplvcd - pointer to a custom draw message struct
*
* RETURNS: LRESULT - depends on custom draw notification
*
* History:
* Sep '07 - Created
*
\****************************************************************************/
LRESULT Grid_OnCustomDraw(HWND hwnd, LPNMLVCUSTOMDRAW lplvcd)
{
switch (lplvcd->nmcd.dwDrawStage)
{
//Before the paint cycle begins
//request notifications for individual listview items
case CDDS_PREPAINT:
return CDRF_NOTIFYITEMDRAW;
//Before an item is drawn
//request notifications for individual listview subitems
case CDDS_ITEMPREPAINT:
return CDRF_NOTIFYSUBITEMDRAW;
case CDDS_SUBITEM | CDDS_ITEMPREPAINT:
{
int c = 0;
if(g_lpInst->pfnChangedCB != NULL)
{
// Color Rows with changed values
c = g_lpInst->pfnChangedCB(lplvcd->nmcd.dwItemSpec);
}
if (c == 1)
{
lplvcd->clrText = g_lpInst->Cell_ChangedTxtColr;
lplvcd->clrTextBk = g_lpInst->Cell_ChangedBkColr;
}
else if (c == 2)
{
lplvcd->clrText = g_lpInst->Cell_FaultyTxtColr;
lplvcd->clrTextBk = g_lpInst->Cell_FaultyBkColr;
}
// Color alternating Rows
else if (g_lpInst->fPaintByRow && lplvcd->nmcd.dwItemSpec & 1)
{
lplvcd->clrText = g_lpInst->Cell_AltTxtColr;
lplvcd->clrTextBk = g_lpInst->Cell_AltBkColr;
}
else if (!g_lpInst->fPaintByRow && lplvcd->iSubItem & 1)
{
lplvcd->clrText = g_lpInst->Cell_AltTxtColr;
lplvcd->clrTextBk = g_lpInst->Cell_AltBkColr;
}
else // Color default
{
lplvcd->clrText = ListView_GetTextColor(hwnd);
lplvcd->clrTextBk = ListView_GetBkColor(hwnd);
}
// Color active cell
if (lplvcd->iSubItem == g_lpInst->hti.iSubItem && lplvcd->nmcd.dwItemSpec == g_lpInst->hti.iItem)
{
int c = 0;
if (g_lpInst->pfnChangedCB != NULL)
{
g_lpInst->pfnChangedCB(g_lpInst->hti.iItem);
}
// color active cell accordingly if value has been changed
if (c == 1)
{
lplvcd->clrText = g_lpInst->Editor_ChangedTxtColr;
lplvcd->clrTextBk = g_lpInst->Editor_ChangedBkColr;
}
else if (c == 2)
{
lplvcd->clrText = g_lpInst->Editor_FaultyTxtColr;
lplvcd->clrTextBk = g_lpInst->Editor_FaultyBkColr;
}
else