-
Notifications
You must be signed in to change notification settings - Fork 28
/
GridCtrl.cpp
7621 lines (6412 loc) · 228 KB
/
GridCtrl.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
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
// GridCtrl.cpp : implementation file
//
// MFC Grid Control v2.26
//
// Written by Chris Maunder <[email protected]>
// Copyright (c) 1998-2005. All Rights Reserved.
//
// The code contained in this file was based on the original
// WorldCom Grid control written by Joe Willcoxson,
// mailto:[email protected]
// http://users.aol.com/chinajoe
// (These addresses may be out of date) The code has gone through
// so many modifications that I'm not sure if there is even a single
// original line of code. In any case Joe's code was a great
// framework on which to build.
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed unmodified by any means PROVIDING it is
// not sold for profit without the authors written consent, and
// providing that this notice and the authors name and all copyright
// notices remains intact.
//
// An email letting me know how you are using it would be nice as well.
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability for any damage/loss of business that
// this product may cause.
//
// Expect bugs!
//
// Please use and enjoy, and let me know of any bugs/mods/improvements
// that you have found/implemented and I will fix/incorporate them into
// this file.
//
// History:
// --------
// This control is constantly evolving, sometimes due to new features that I
// feel are necessary, and sometimes due to existing bugs. Where possible I
// have credited the changes to those who contributed code corrections or
// enhancements (names in brackets) or code suggestions (suggested by...)
//
// 1.0 - 1.13 20 Feb 1998 - 6 May 1999
// First release version. Progressed from being a basic
// grid based on the original WorldCom Grid control
// written by Joe Willcoxson (mailto:[email protected],
// http://users.aol.com/chinajoe) to something a little
// more feature rich. Rewritten so many times I doubt
// there is a single line of Joe's code left. Many, many,
// MANY people sent in bug reports and fixes. Thank you
// all.
//
// 2.0 1 Feb 2000
// Rewritten to make the grid more object oriented, in
// that the CGridCell class now takes care of cell-specific
// tasks. This makes the code more robust, but more
// importantly it allows the simple insertion of other
// types of cells.
//
// 2.10 11 Mar 2000 - Ken Bertelson and Chris Maunder
// - Additions for virtual CGridCell support of embedded tree
// & cell buttons implementation
// - Optional WYSIWYG printing
// - Awareness of hidden (0 width/height) rows and columns for
// key movements, cut, copy, paste, and autosizing
// - CGridCell can make title tips display any text rather than
// cell text only
// - Minor vis bug fixes
// - CGridCtrl now works with CGridCellBase instead of CGridCell
// This is a taste of things to come.
//
// 2.20 30 Jul 2000 - Chris Maunder
// - Font storage optimised (suggested by Martin Richter)
// - AutoSizeColumn works on either column header, data or both
// - EnsureVisible. The saga continues... (Ken)
// - Rewrote exception handling
// - Added TrackFocusCell and FrameFocusCell properties, as well as
// ExpandLastColumn (suggested by Bruce E. Stemplewski).
// - InsertColumn now allows you to insert columns at the end of the
// column range (David Weibel)
// - Shift-cell-selection more intuitive
// - API change: Set/GetGridColor now Set/GetGridLineColor
// - API change: Set/GetBkColor now Set/GetGridBkColor
// - API change: Set/GetTextColor, Set/GetTextBkColor depricated
// - API change: Set/GetFixedTextColor, Set/GetFixedBkColor depricated
// - Stupid DDX_GridControl workaround removed.
// - Added "virtual mode" via Set/GetVirtualMode
// - Added SetCallbackFunc to allow callback functions in virtual mode
// - Added Set/GetAutoSizeStyle
// - AutoSize() bug fixed
// - added GVIS_FIXEDROW, GVIS_FIXEDCOL states
// - added Get/SetFixed[Row|Column]Selection
// - cell "Get" methods now const'd. Sorry folks...
// - GetMouseScrollLines now uses win98/W2K friendly code
// - WS_EX_CLIENTEDGE style now implicit
//
// [ Only the latest version and major version changes will be shown ]
////
// 2.25 13 Mar 2004 - Chris Maunder
// - Minor changes so it will compile in VS.NET (inc. Whidbey)
// - Fixed minor bug in EnsureVisible - Junlin Xu
// - Changed AfxGetInstanceHandle for AfxGetResourceHandle in RegisterWindowClass
// - Various changes thanks to Yogurt
//
// 2.26 13 Dec 2005 - Pierre Couderc
// - Added sort in Virtual mode
// - Change row/column order programatically or via drag and drop
// - Added save/restore layer (for undoing row/column order changes)
//
// TODO: 1) Implement sparse grids (super easy now)
// 2) Fix it so that as you drag select, the speed of selection increases
// with time.
// 3) Scrolling is still a little dodgy (too much grey area). I know there
// is a simple fix but it's been a low priority
//
// ISSUES: 1) WindowFromPoint seems to do weird things in W2K. Causing problems for
// the rigt-click-on-titletip code.
//
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "GridCtrl.h"
#include <algorithm>
namespace gridctrl {
#include "MemDC.h"
}
// OLE stuff for clipboard operations
#include <afxadv.h> // For CSharedFile
#include <afxconv.h> // For LPTSTR -> LPSTR macros
#pragma warning(disable:4996)
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// Spit out some messages as a sanity check for programmers
#ifdef GRIDCONTROL_NO_TITLETIPS
#pragma message(" -- CGridCtrl: No titletips for cells with large data")
#endif
#ifdef GRIDCONTROL_NO_DRAGDROP
#pragma message(" -- CGridCtrl: No OLE drag and drop")
#endif
#ifdef GRIDCONTROL_NO_CLIPBOARD
#pragma message(" -- CGridCtrl: No clipboard support")
#endif
#ifdef GRIDCONTROL_NO_PRINTING
#pragma message(" -- CGridCtrl: No printing support")
#endif
IMPLEMENT_DYNCREATE(CGridCtrl, CWnd)
// Get the number of lines to scroll with each mouse wheel notch
// Why doesn't windows give us this function???
UINT GetMouseScrollLines()
{
int nScrollLines = 3; // reasonable default
#ifndef _WIN32_WCE
// Do things the hard way in win95
OSVERSIONINFO VersionInfo;
VersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if (!GetVersionEx(&VersionInfo) ||
(VersionInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS && VersionInfo.dwMinorVersion == 0))
{
HKEY hKey;
if (RegOpenKeyEx(HKEY_CURRENT_USER, _T("Control Panel\\Desktop"),
0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
{
TCHAR szData[128];
DWORD dwKeyDataType;
DWORD dwDataBufSize = sizeof(szData);
if (RegQueryValueEx(hKey, _T("WheelScrollLines"), NULL, &dwKeyDataType,
(LPBYTE) &szData, &dwDataBufSize) == ERROR_SUCCESS)
{
nScrollLines = _tcstoul(szData, NULL, 10);
}
RegCloseKey(hKey);
}
}
// win98 or greater
else
SystemParametersInfo (SPI_GETWHEELSCROLLLINES, 0, &nScrollLines, 0);
#endif
return nScrollLines;
}
/////////////////////////////////////////////////////////////////////////////
// CGridCtrl
CGridCtrl::CGridCtrl(int nRows, int nCols, int nFixedRows, int nFixedCols)
{
RegisterWindowClass();
#if !defined(GRIDCONTROL_NO_DRAGDROP) || !defined(GRIDCONTROL_NO_CLIPBOARD)
_AFX_THREAD_STATE* pState = AfxGetThreadState();
if (!pState->m_bNeedTerm && !AfxOleInit())
AfxMessageBox(_T("OLE initialization failed. Make sure that the OLE libraries are the correct version"));
#endif
// Store the system colours in case they change. The gridctrl uses
// these colours, and in OnSysColorChange we can check to see if
// the gridctrl colours have been changed from the system colours.
// If they have, then leave them, otherwise change them to reflect
// the new system colours.
m_crWindowText = ::GetSysColor(COLOR_WINDOWTEXT);
m_crWindowColour = ::GetSysColor(COLOR_WINDOW);
m_cr3DFace = ::GetSysColor(COLOR_3DFACE);
m_crShadow = ::GetSysColor(COLOR_3DSHADOW);
m_crGridLineColour = RGB(192,192,192);
m_nRows = 0;
m_nCols = 0;
m_nFixedRows = 0;
m_nFixedCols = 0;
m_InDestructor = false;
m_bVirtualMode = FALSE;
m_pfnCallback = NULL;
m_nVScrollMax = 0; // Scroll position
m_nHScrollMax = 0;
m_nRowsPerWheelNotch = GetMouseScrollLines(); // Get the number of lines
// per mouse wheel notch to scroll
m_nBarState = GVL_NONE;
m_MouseMode = MOUSE_NOTHING;
m_nGridLines = GVL_BOTH;
m_bEditable = TRUE;
m_bListMode = FALSE;
m_bSingleRowSelection = FALSE;
m_bSingleColSelection = FALSE;
m_bLMouseButtonDown = FALSE;
m_bRMouseButtonDown = FALSE;
m_bAllowDraw = TRUE; // allow draw updates
m_bEnableSelection = TRUE;
m_bFixedColumnSelection = TRUE;
m_bFixedRowSelection = TRUE;
m_bAllowRowResize = TRUE;
m_bAllowColumnResize = TRUE;
m_bSortOnClick = FALSE; // Sort on header row click
m_bHandleTabKey = TRUE;
#ifdef _WIN32_WCE
m_bDoubleBuffer = FALSE; // Use double buffering to avoid flicker?
#else
m_bDoubleBuffer = TRUE; // Use double buffering to avoid flicker?
#endif
m_bTitleTips = TRUE; // show cell title tips
m_bWysiwygPrinting = FALSE; // use size-to-width printing
m_bHiddenColUnhide = TRUE; // 0-width columns can be expanded via mouse
m_bHiddenRowUnhide = TRUE; // 0-Height rows can be expanded via mouse
m_bAllowColHide = TRUE; // Columns can be contracted to 0-width via mouse
m_bAllowRowHide = TRUE; // Rows can be contracted to 0-height via mouse
m_bAscending = TRUE; // sorting stuff
m_nSortColumn = -1;
m_pfnCompare = NULL;
m_pfnVirtualCompare = NULL;
m_nAutoSizeColumnStyle = GVS_BOTH; // Autosize grid using header and data info
m_nTimerID = 0; // For drag-selection
m_nTimerInterval = 25; // (in milliseconds)
m_nResizeCaptureRange = 3; // When resizing columns/row, the cursor has to be
// within +/-3 pixels of the dividing line for
// resizing to be possible
m_pImageList = NULL; // Images in the grid
m_bAllowDragAndDrop = FALSE; // for drag and drop - EFW - off by default
m_bTrackFocusCell = TRUE; // Track Focus cell?
m_bFrameFocus = TRUE; // Frame the selected cell?
m_AllowReorderColumn = false;
m_QuitFocusOnTab = false;
m_AllowSelectRowInFixedCol = false;
m_bDragRowMode = TRUE; // allow to drop a line over another one to change row order
m_pRtcDefault = RUNTIME_CLASS(CGridCell);
SetupDefaultCells();
SetGridBkColor(m_crShadow);
// Set up the initial grid size
SetRowCount(nRows);
SetColumnCount(nCols);
SetFixedRowCount(nFixedRows);
SetFixedColumnCount(nFixedCols);
SetTitleTipTextClr(CLR_DEFAULT); //FNA
SetTitleTipBackClr(CLR_DEFAULT);
// set initial selection range (ie. none)
m_SelectedCellMap.RemoveAll();
m_PrevSelectedCellMap.RemoveAll();
#if !defined(_WIN32_WCE_NO_PRINTING) && !defined(GRIDCONTROL_NO_PRINTING)
// EFW - Added to support shaded/unshaded printout and
// user-definable margins.
m_bShadedPrintOut = TRUE;
SetPrintMarginInfo(2, 2, 4, 4, 1, 1, 1);
#endif
}
CGridCtrl::~CGridCtrl()
{
m_InDestructor = true;
DeleteAllItems();
#ifndef GRIDCONTROL_NO_TITLETIPS
if (m_bTitleTips && ::IsWindow(m_TitleTip.GetSafeHwnd()))
m_TitleTip.DestroyWindow();
#endif
DestroyWindow();
#if !defined(GRIDCONTROL_NO_DRAGDROP) || !defined(GRIDCONTROL_NO_CLIPBOARD)
// BUG FIX - EFW
COleDataSource *pSource = COleDataSource::GetClipboardOwner();
if(pSource)
COleDataSource::FlushClipboard();
#endif
}
// Register the window class if it has not already been registered.
BOOL CGridCtrl::RegisterWindowClass()
{
WNDCLASS wndcls;
//HINSTANCE hInst = AfxGetInstanceHandle();
HINSTANCE hInst = AfxGetResourceHandle();
if (!(::GetClassInfo(hInst, GRIDCTRL_CLASSNAME, &wndcls)))
{
// otherwise we need to register a new class
wndcls.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
wndcls.lpfnWndProc = ::DefWindowProc;
wndcls.cbClsExtra = wndcls.cbWndExtra = 0;
wndcls.hInstance = hInst;
wndcls.hIcon = NULL;
#ifndef _WIN32_WCE_NO_CURSOR
wndcls.hCursor = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
#else
wndcls.hCursor = 0;
#endif
wndcls.hbrBackground = (HBRUSH) (COLOR_3DFACE + 1);
wndcls.lpszMenuName = NULL;
wndcls.lpszClassName = GRIDCTRL_CLASSNAME;
if (!AfxRegisterClass(&wndcls))
{
AfxThrowResourceException();
return FALSE;
}
}
return TRUE;
}
BOOL CGridCtrl::Initialise()
{
// Stop re-entry problems
static BOOL bInProcedure = FALSE;
if (bInProcedure)
return FALSE;
bInProcedure = TRUE;
#ifndef GRIDCONTROL_NO_TITLETIPS
m_TitleTip.SetParentWnd(this);
#endif
// This would be a good place to register the droptarget but
// unfortunately this causes problems if you are using the
// grid in a view.
// Moved from OnSize.
//#ifndef GRIDCONTROL_NO_DRAGDROP
// m_DropTarget.Register(this);
//#endif
if (::IsWindow(m_hWnd))
ModifyStyleEx(0, WS_EX_CLIENTEDGE);
// Kludge: Make sure the client edge shows
// This is so horrible it makes my eyes water.
CRect rect;
GetWindowRect(rect);
CWnd* pParent = GetParent();
if (pParent != NULL)
pParent->ScreenToClient(rect);
rect.InflateRect(1,1); MoveWindow(rect);
rect.DeflateRect(1,1); MoveWindow(rect);
bInProcedure = FALSE;
return TRUE;
}
// creates the control - use like any other window create control
BOOL CGridCtrl::Create(const RECT& rect, CWnd* pParentWnd, UINT nID, DWORD dwStyle)
{
ASSERT(pParentWnd->GetSafeHwnd());
if (!CWnd::Create(GRIDCTRL_CLASSNAME, NULL, dwStyle, rect, pParentWnd, nID))
return FALSE;
//Initialise(); - called in PreSubclassWnd
// The number of rows and columns will only be non-zero if the constructor
// was called with non-zero initialising parameters. If this window was created
// using a dialog template then the number of rows and columns will be 0 (which
// means that the code below will not be needed - which is lucky 'cause it ain't
// gonna get called in a dialog-template-type-situation.
TRY
{
m_arRowHeights.SetSize(m_nRows); // initialize row heights
m_arColWidths.SetSize(m_nCols); // initialize column widths
}
CATCH (CMemoryException, e)
{
e->ReportError();
return FALSE;
}
END_CATCH
int i;
for (i = 0; i < m_nRows; i++)
m_arRowHeights[i] = m_cellDefault.GetHeight();
for (i = 0; i < m_nCols; i++)
m_arColWidths[i] = m_cellDefault.GetWidth();
return TRUE;
}
void CGridCtrl::SetupDefaultCells()
{
m_cellDefault.SetGrid(this); // Normal editable cell
m_cellFixedColDef.SetGrid(this); // Cell for fixed columns
m_cellFixedRowDef.SetGrid(this); // Cell for fixed rows
m_cellFixedRowColDef.SetGrid(this); // Cell for area overlapped by fixed columns/rows
m_cellDefault.SetTextClr(m_crWindowText);
m_cellDefault.SetBackClr(m_crWindowColour);
m_cellFixedColDef.SetTextClr(m_crWindowText);
m_cellFixedColDef.SetBackClr(m_cr3DFace);
m_cellFixedRowDef.SetTextClr(m_crWindowText);
m_cellFixedRowDef.SetBackClr(m_cr3DFace);
m_cellFixedRowColDef.SetTextClr(m_crWindowText);
m_cellFixedRowColDef.SetBackClr(m_cr3DFace);
}
void CGridCtrl::PreSubclassWindow()
{
CWnd::PreSubclassWindow();
//HFONT hFont = ::CreateFontIndirect(m_cellDefault.GetFont());
//OnSetFont((LPARAM)hFont, 0);
//DeleteObject(hFont);
Initialise();
}
// Sends a message to the parent in the form of a WM_NOTIFY message with
// a NM_GRIDVIEW structure attached
LRESULT CGridCtrl::SendMessageToParent(int nRow, int nCol, int nMessage) const
{
if (!IsWindow(m_hWnd))
return 0;
NM_GRIDVIEW nmgv;
nmgv.iRow = nRow;
nmgv.iColumn = nCol;
nmgv.hdr.hwndFrom = m_hWnd;
nmgv.hdr.idFrom = GetDlgCtrlID();
nmgv.hdr.code = nMessage;
CWnd *pOwner = GetOwner();
if (pOwner && IsWindow(pOwner->m_hWnd))
return pOwner->SendMessage(WM_NOTIFY, nmgv.hdr.idFrom, (LPARAM)&nmgv);
else
return 0;
}
// Send a request to the parent to return information on a given cell
LRESULT CGridCtrl::SendDisplayRequestToParent(GV_DISPINFO* pDisplayInfo) const
{
if (!IsWindow(m_hWnd))
return 0;
// Fix up the message headers
pDisplayInfo->hdr.hwndFrom = m_hWnd;
pDisplayInfo->hdr.idFrom = GetDlgCtrlID();
pDisplayInfo->hdr.code = GVN_GETDISPINFO;
// Send the message
CWnd *pOwner = GetOwner();
if (pOwner && IsWindow(pOwner->m_hWnd))
return pOwner->SendMessage(WM_NOTIFY, pDisplayInfo->hdr.idFrom, (LPARAM)pDisplayInfo);
else
return 0;
}
// Send a hint to the parent about caching information
LRESULT CGridCtrl::SendCacheHintToParent(const CCellRange& range) const
{
if (!IsWindow(m_hWnd))
return 0;
GV_CACHEHINT CacheHint;
// Fix up the message headers
CacheHint.hdr.hwndFrom = m_hWnd;
CacheHint.hdr.idFrom = GetDlgCtrlID();
CacheHint.hdr.code = GVN_ODCACHEHINT;
CacheHint.range = range;
// Send the message
CWnd *pOwner = GetOwner();
if (pOwner && IsWindow(pOwner->m_hWnd))
return pOwner->SendMessage(WM_NOTIFY, CacheHint.hdr.idFrom, (LPARAM)&CacheHint);
else
return 0;
}
#define LAYER_SIGNATURE (0x5FD4E64)
int CGridCtrl::GetLayer(int** pLayer) // used to save and restore order of columns
{ // gives back the size of the area (do not forget to delete pLayer)
int Length = 2+GetColumnCount()*2;
int *Layer = new int[Length]; // the caller is supposed to delete it
Layer[0]= LAYER_SIGNATURE;
Layer[1]= GetColumnCount();
memcpy(&Layer[2], &m_arColOrder[0], GetColumnCount()*sizeof(int));
memcpy(&Layer[2+GetColumnCount()], &m_arColWidths[0], GetColumnCount()*sizeof(int));
*pLayer = Layer;
return Length;
}
void CGridCtrl::SetLayer(int* pLayer)
{ // coming from a previous GetLayer (ignored if not same number of column, or the same revision number)
if(pLayer[0] != LAYER_SIGNATURE) return;
if(pLayer[1] != GetColumnCount()) return;
/* TRACE(" %d == %d \n",m_arColOrder[0],pLayer[2]);
TRACE(" %d == %d \n",m_arColOrder[1],pLayer[3]);
TRACE(" %d == %d \n",m_arColOrder[2],pLayer[4]);
TRACE(" %d == %d \n",m_arColWidths[0],pLayer[2+3]);
TRACE(" %d == %d \n",m_arColWidths[1],pLayer[3+3]);
TRACE(" %d == %d \n",m_arColWidths[2],pLayer[4+3]);
TRACE(" %d == %d \n",GetColumnCount(),3);
ASSERT(m_arColOrder[0]==pLayer[2]);
ASSERT(m_arColOrder[1]==pLayer[3]);
ASSERT(m_arColOrder[2]==pLayer[4]);
ASSERT(m_arColWidths[0]==pLayer[2+3]);
ASSERT(m_arColWidths[1]==pLayer[3+3]);
ASSERT(m_arColWidths[2]==pLayer[4+3]);
ASSERT(GetColumnCount()==3);
*/ memcpy(&m_arColOrder[0],&pLayer[2], GetColumnCount()*sizeof(int));
memcpy(&m_arColWidths[0],&pLayer[2+GetColumnCount()], GetColumnCount()*sizeof(int));
}
BEGIN_MESSAGE_MAP(CGridCtrl, CWnd)
//EFW - Added ON_WM_RBUTTONUP
//{{AFX_MSG_MAP(CGridCtrl)
ON_WM_PAINT()
ON_WM_HSCROLL()
ON_WM_VSCROLL()
ON_WM_SIZE()
ON_WM_LBUTTONUP()
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_WM_TIMER()
ON_WM_GETDLGCODE()
ON_WM_KEYDOWN()
ON_WM_CHAR()
ON_WM_LBUTTONDBLCLK()
ON_WM_RBUTTONDBLCLK()
ON_WM_ERASEBKGND()
ON_UPDATE_COMMAND_UI(ID_EDIT_SELECT_ALL, OnUpdateEditSelectAll)
ON_COMMAND(ID_EDIT_SELECT_ALL, OnEditSelectAll)
ON_WM_SYSKEYDOWN()
//}}AFX_MSG_MAP
#ifndef _WIN32_WCE_NO_CURSOR
ON_WM_SETCURSOR()
#endif
#ifndef _WIN32_WCE
ON_WM_RBUTTONUP()
ON_WM_SYSCOLORCHANGE()
ON_WM_CAPTURECHANGED()
#endif
#ifndef GRIDCONTROL_NO_CLIPBOARD
ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateEditCopy)
ON_COMMAND(ID_EDIT_CUT, OnEditCut)
ON_UPDATE_COMMAND_UI(ID_EDIT_CUT, OnUpdateEditCut)
ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, OnUpdateEditPaste)
#endif
#if (_WIN32_WCE >= 210)
ON_WM_SETTINGCHANGE()
#endif
#if !defined(_WIN32_WCE) && (_MFC_VER >= 0x0421)
ON_WM_MOUSEWHEEL()
#endif
ON_MESSAGE(WM_SETFONT, OnSetFont)
ON_MESSAGE(WM_GETFONT, OnGetFont)
ON_MESSAGE(WM_IME_CHAR, OnImeChar)
ON_NOTIFY(GVN_ENDLABELEDIT, IDC_INPLACE_CONTROL, OnEndInPlaceEdit)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CGridCtrl message handlers
void CGridCtrl::OnPaint()
{
CPaintDC dc(this); // device context for painting
if (m_bDoubleBuffer) // Use a memory DC to remove flicker
{
gridctrl::CMemDC MemDC(&dc);
OnDraw(&MemDC);
}
else // Draw raw - this helps in debugging vis problems.
OnDraw(&dc);
}
BOOL CGridCtrl::OnEraseBkgnd(CDC* /*pDC*/)
{
return TRUE; // Don't erase the background.
}
// Custom background erasure. This gets called from within the OnDraw function,
// since we will (most likely) be using a memory DC to stop flicker. If we just
// erase the background normally through OnEraseBkgnd, and didn't fill the memDC's
// selected bitmap with colour, then all sorts of vis problems would occur
void CGridCtrl::EraseBkgnd(CDC* pDC)
{
CRect VisRect, ClipRect, rect;
CBrush FixedRowColBack(GetDefaultCell(TRUE, TRUE)->GetBackClr()),
FixedRowBack(GetDefaultCell(TRUE, FALSE)->GetBackClr()),
FixedColBack(GetDefaultCell(FALSE, TRUE)->GetBackClr()),
TextBack(GetDefaultCell(FALSE, FALSE)->GetBackClr());
CBrush Back(GetGridBkColor());
//CBrush Back(GetTextBkColor());
if (pDC->GetClipBox(ClipRect) == ERROR)
return;
GetVisibleNonFixedCellRange(VisRect);
int nFixedColumnWidth = GetFixedColumnWidth();
int nFixedRowHeight = GetFixedRowHeight();
// Draw Fixed row/column background
if (ClipRect.left < nFixedColumnWidth && ClipRect.top < nFixedRowHeight)
pDC->FillRect(CRect(ClipRect.left, ClipRect.top,
nFixedColumnWidth, nFixedRowHeight),
&FixedRowColBack);
// Draw Fixed columns background
if (ClipRect.left < nFixedColumnWidth && ClipRect.top < VisRect.bottom)
pDC->FillRect(CRect(ClipRect.left, ClipRect.top,
nFixedColumnWidth, VisRect.bottom),
&FixedColBack);
// Draw Fixed rows background
if (ClipRect.top < nFixedRowHeight &&
ClipRect.right > nFixedColumnWidth && ClipRect.left < VisRect.right)
pDC->FillRect(CRect(nFixedColumnWidth-1, ClipRect.top,
VisRect.right, nFixedRowHeight),
&FixedRowBack);
// Draw non-fixed cell background
if (rect.IntersectRect(VisRect, ClipRect))
{
CRect CellRect(__max(nFixedColumnWidth, rect.left),
__max(nFixedRowHeight, rect.top),
rect.right, rect.bottom);
pDC->FillRect(CellRect, &TextBack);
}
// Draw right hand side of window outside grid
if (VisRect.right < ClipRect.right)
pDC->FillRect(CRect(VisRect.right, ClipRect.top,
ClipRect.right, ClipRect.bottom),
&Back);
// Draw bottom of window below grid
if (VisRect.bottom < ClipRect.bottom && ClipRect.left < VisRect.right)
pDC->FillRect(CRect(ClipRect.left, VisRect.bottom,
VisRect.right, ClipRect.bottom),
&Back);
}
void CGridCtrl::OnSize(UINT nType, int cx, int cy)
{
static BOOL bAlreadyInsideThisProcedure = FALSE;
if (bAlreadyInsideThisProcedure)
return;
if (!::IsWindow(m_hWnd))
return;
// This is not the ideal place to register the droptarget
#ifndef GRIDCONTROL_NO_DRAGDROP
m_DropTarget.Register(this);
#endif
// Start re-entry blocking
bAlreadyInsideThisProcedure = TRUE;
EndEditing(); // destroy any InPlaceEdit's
CWnd::OnSize(nType, cx, cy);
ResetScrollBars();
// End re-entry blocking
bAlreadyInsideThisProcedure = FALSE;
}
UINT CGridCtrl::OnGetDlgCode()
{
UINT nCode = DLGC_WANTARROWS | DLGC_WANTCHARS; // DLGC_WANTALLKEYS; //
if (m_bHandleTabKey && !IsCTRLpressed())
nCode |= DLGC_WANTTAB;
return nCode;
}
#ifndef _WIN32_WCE
// If system colours change, then redo colours
void CGridCtrl::OnSysColorChange()
{
CWnd::OnSysColorChange();
if (GetDefaultCell(FALSE, FALSE)->GetTextClr() == m_crWindowText) // Still using system colours
GetDefaultCell(FALSE, FALSE)->SetTextClr(::GetSysColor(COLOR_WINDOWTEXT)); // set to new system colour
if (GetDefaultCell(FALSE, FALSE)->GetBackClr() == m_crWindowColour)
GetDefaultCell(FALSE, FALSE)->SetBackClr(::GetSysColor(COLOR_WINDOW));
if (GetDefaultCell(TRUE, FALSE)->GetTextClr() == m_crWindowText) // Still using system colours
GetDefaultCell(TRUE, FALSE)->SetTextClr(::GetSysColor(COLOR_WINDOWTEXT)); // set to new system colour
if (GetDefaultCell(TRUE, FALSE)->GetBackClr() == m_crWindowColour)
GetDefaultCell(TRUE, FALSE)->SetBackClr(::GetSysColor(COLOR_WINDOW));
if (GetDefaultCell(FALSE, TRUE)->GetTextClr() == m_crWindowText) // Still using system colours
GetDefaultCell(FALSE, TRUE)->SetTextClr(::GetSysColor(COLOR_WINDOWTEXT)); // set to new system colour
if (GetDefaultCell(FALSE, TRUE)->GetBackClr() == m_crWindowColour)
GetDefaultCell(FALSE, TRUE)->SetBackClr(::GetSysColor(COLOR_WINDOW));
if (GetDefaultCell(TRUE, TRUE)->GetTextClr() == m_crWindowText) // Still using system colours
GetDefaultCell(TRUE, TRUE)->SetTextClr(::GetSysColor(COLOR_WINDOWTEXT)); // set to new system colour
if (GetDefaultCell(TRUE, TRUE)->GetBackClr() == m_crWindowColour)
GetDefaultCell(TRUE, TRUE)->SetBackClr(::GetSysColor(COLOR_WINDOW));
if (GetGridBkColor() == m_crShadow)
SetGridBkColor(::GetSysColor(COLOR_3DSHADOW));
m_crWindowText = ::GetSysColor(COLOR_WINDOWTEXT);
m_crWindowColour = ::GetSysColor(COLOR_WINDOW);
m_cr3DFace = ::GetSysColor(COLOR_3DFACE);
m_crShadow = ::GetSysColor(COLOR_3DSHADOW);
}
#endif
#ifndef _WIN32_WCE_NO_CURSOR
// If we are drag-selecting cells, or drag and dropping, stop now
void CGridCtrl::OnCaptureChanged(CWnd *pWnd)
{
if (pWnd->GetSafeHwnd() == GetSafeHwnd())
return;
// kill timer if active
if (m_nTimerID != 0)
{
KillTimer(m_nTimerID);
m_nTimerID = 0;
}
#ifndef GRIDCONTROL_NO_DRAGDROP
// Kill drag and drop if active
if (m_MouseMode == MOUSE_DRAGGING)
m_MouseMode = MOUSE_NOTHING;
#endif
}
#endif
#if (_MFC_VER >= 0x0421) || (_WIN32_WCE >= 210)
// If system settings change, then redo colours
void CGridCtrl::OnSettingChange(UINT uFlags, LPCTSTR lpszSection)
{
CWnd::OnSettingChange(uFlags, lpszSection);
if (GetDefaultCell(FALSE, FALSE)->GetTextClr() == m_crWindowText) // Still using system colours
GetDefaultCell(FALSE, FALSE)->SetTextClr(::GetSysColor(COLOR_WINDOWTEXT)); // set to new system colour
if (GetDefaultCell(FALSE, FALSE)->GetBackClr() == m_crWindowColour)
GetDefaultCell(FALSE, FALSE)->SetBackClr(::GetSysColor(COLOR_WINDOW));
if (GetDefaultCell(TRUE, FALSE)->GetTextClr() == m_crWindowText) // Still using system colours
GetDefaultCell(TRUE, FALSE)->SetTextClr(::GetSysColor(COLOR_WINDOWTEXT)); // set to new system colour
if (GetDefaultCell(TRUE, FALSE)->GetBackClr() == m_crWindowColour)
GetDefaultCell(TRUE, FALSE)->SetBackClr(::GetSysColor(COLOR_WINDOW));
if (GetDefaultCell(FALSE, TRUE)->GetTextClr() == m_crWindowText) // Still using system colours
GetDefaultCell(FALSE, TRUE)->SetTextClr(::GetSysColor(COLOR_WINDOWTEXT)); // set to new system colour
if (GetDefaultCell(FALSE, TRUE)->GetBackClr() == m_crWindowColour)
GetDefaultCell(FALSE, TRUE)->SetBackClr(::GetSysColor(COLOR_WINDOW));
if (GetDefaultCell(TRUE, TRUE)->GetTextClr() == m_crWindowText) // Still using system colours
GetDefaultCell(TRUE, TRUE)->SetTextClr(::GetSysColor(COLOR_WINDOWTEXT)); // set to new system colour
if (GetDefaultCell(TRUE, TRUE)->GetBackClr() == m_crWindowColour)
GetDefaultCell(TRUE, TRUE)->SetBackClr(::GetSysColor(COLOR_WINDOW));
if (GetGridBkColor() == m_crShadow)
SetGridBkColor(::GetSysColor(COLOR_3DSHADOW));
m_crWindowText = ::GetSysColor(COLOR_WINDOWTEXT);
m_crWindowColour = ::GetSysColor(COLOR_WINDOW);
m_cr3DFace = ::GetSysColor(COLOR_3DFACE);
m_crShadow = ::GetSysColor(COLOR_3DSHADOW);
m_nRowsPerWheelNotch = GetMouseScrollLines(); // Get the number of lines
}
#endif
// For drag-selection. Scrolls hidden cells into view
// TODO: decrease timer interval over time to speed up selection over time
void CGridCtrl::OnTimer(UINT_PTR nIDEvent)
{
ASSERT(nIDEvent == WM_LBUTTONDOWN);
if (nIDEvent != WM_LBUTTONDOWN)
return;
CPoint pt, origPt;
#ifdef _WIN32_WCE
if (m_MouseMode == MOUSE_NOTHING)
return;
origPt = GetMessagePos();
#else
if (!GetCursorPos(&origPt))
return;
#endif
ScreenToClient(&origPt);
CRect rect;
GetClientRect(rect);
int nFixedRowHeight = GetFixedRowHeight();
int nFixedColWidth = GetFixedColumnWidth();
pt = origPt;
if (pt.y > rect.bottom)
{
//SendMessage(WM_VSCROLL, SB_LINEDOWN, 0);
SendMessage(WM_KEYDOWN, VK_DOWN, 0);
if (pt.x < rect.left)
pt.x = rect.left;
if (pt.x > rect.right)
pt.x = rect.right;
pt.y = rect.bottom;
OnSelecting(GetCellFromPt(pt));
}
else if (pt.y < nFixedRowHeight)
{
//SendMessage(WM_VSCROLL, SB_LINEUP, 0);
SendMessage(WM_KEYDOWN, VK_UP, 0);
if (pt.x < rect.left)
pt.x = rect.left;
if (pt.x > rect.right)
pt.x = rect.right;
pt.y = nFixedRowHeight + 1;
OnSelecting(GetCellFromPt(pt));
}
pt = origPt;
if (pt.x > rect.right)
{
// SendMessage(WM_HSCROLL, SB_LINERIGHT, 0);
SendMessage(WM_KEYDOWN, VK_RIGHT, 0);
if (pt.y < rect.top)
pt.y = rect.top;
if (pt.y > rect.bottom)
pt.y = rect.bottom;
pt.x = rect.right;
OnSelecting(GetCellFromPt(pt));
}
else if (pt.x < nFixedColWidth)
{
//SendMessage(WM_HSCROLL, SB_LINELEFT, 0);
SendMessage(WM_KEYDOWN, VK_LEFT, 0);
if (pt.y < rect.top)
pt.y = rect.top;
if (pt.y > rect.bottom)
pt.y = rect.bottom;
pt.x = nFixedColWidth + 1;
OnSelecting(GetCellFromPt(pt));
}
}
// move about with keyboard
void CGridCtrl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if (!IsValid(m_idCurrentCell))
{
CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
return;
}
CCellID next = m_idCurrentCell;
BOOL bChangeLine = FALSE;
BOOL bHorzScrollAction = FALSE;
BOOL bVertScrollAction = FALSE;
if (IsCTRLpressed())
{
switch (nChar)
{
case 'A':
OnEditSelectAll();
break;
case 'k': // This is ctrl+ on french keyboard, may need to be better processed for other locales
AutoSizeColumns();
Invalidate();
break;
#ifndef GRIDCONTROL_NO_CLIPBOARD
case 'X':
OnEditCut();
break;
case VK_INSERT:
case 'C':
OnEditCopy();
break;
case 'V':
OnEditPaste();
break;
#endif
}
}
#ifndef GRIDCONTROL_NO_CLIPBOARD
if (IsSHIFTpressed() &&(nChar == VK_INSERT))
OnEditPaste();
#endif
BOOL bFoundVisible;
int iOrig;
if (nChar == VK_DELETE)
{
CutSelectedText();
}
else if (nChar == VK_DOWN)
{
// don't let user go to a hidden row
bFoundVisible = FALSE;
iOrig = next.row;
next.row++;
while( next.row < GetRowCount())
{
if( GetRowHeight( next.row) > 0)
{
bFoundVisible = TRUE;
break;
}
next.row++;
}
if( !bFoundVisible)
next.row = iOrig;
}
else if (nChar == VK_UP)
{
// don't let user go to a hidden row
bFoundVisible = FALSE;
iOrig = next.row;
next.row--;
while( next.row >= m_nFixedRows)
{
if( GetRowHeight( next.row) > 0)
{
bFoundVisible = TRUE;
break;
}
next.row--;
}
if( !bFoundVisible)
next.row = iOrig;
}
else if (nChar == VK_RIGHT || (nChar == VK_TAB && !IsSHIFTpressed()) )
{
if( (nChar == VK_TAB) && m_QuitFocusOnTab )
{