This repository has been archived by the owner on Feb 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
GridCtrl.cpp
executable file
·5362 lines (4475 loc) · 168 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
//
// Written by Chris Maunder
// mailto:[email protected]
//
// Copyright (c) 1998-1999.
//
// The code contained in this file is based on the original
// WorldCom Grid control written by Joe Willcoxson,
// mailto:[email protected]
// http://users.aol.com/chinajoe
// 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 framewaork 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. If the source code in this file is used in
// any commercial application then a statement along the lines of
// "Portions copyright (c) Chris Maunder, 1998" must be included in
// the startup banner, "About" box or printed documentation. An email
// letting me know that you are using it would be nice as well. That's
// not much to ask considering the amount of work that went into this.
//
// 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 20 Feb 1998 First release version.
// 1.01 24 Feb 1998 Memory leak fix (Jens Bohlmann)
// Fixec typo (my fault!) in CMemDC.h - Claus Arend-Schneider)
// Bug in GetSelectedCount (Lyn Newton)
// 1.02 4 Mar 1998 Scrolling a little neater (less dead area)
// Cell selection via OnTimer correctly updates Focus cell (Suggested by Lyn Newton)
// 1.03 17 Mar 1998 Clipboard functions added, Intellimouse support
// Using 32 bit scroll pos functions instead of 16 bit ("cronos")
// Added OLE drag and drop.
// 1.04 6 Apr 1998 Added Ctrl-A = Select All, fixed CGridDropTarget
// problem, minor bug in CopyTextFromGrid (assert on
// empty string). Cleaned up reponse to m_bEditable
// (OnDrop and Ctrl-X disabled)
// 1.05 10 May 1998 Memory leak fixed. (Yuheng Zhao)
// Changed OLE initialisation (Carlo Comino)
// Added separate fore + background cell colours (Suggested by John Crane)
// ExpandToFit etc cleaned up - now decreases and
// increases cell sizes to fit client area.
// Added notification messages for the grid's parent (Suggested by
// Added GVIS_READONLY state
// 1.06 20 May 1998 Added TAB key handling. (Daniela Rybarova)
// Intellimouse code correction for whole page scrolling (Paul Grant)
// Fixed 16 bit thumb track problems (now 32 bit) (Paul Grant)
// Fixed accelerator key problem in CInPlaceEdit (Matt Weagle)
// Fixed Stupid ClassWizard code parsing problem (Michael A. Barnhart)
// Double buffering now programmatically selectable
// Workaround for win95 drag and drop registration problem
// Corrected UNICODE implementation of clipboard stuff
// Dragging and dropping from a selection onto itself no
// no longer causes the cells to be emptied
// 1.07 28 Jul 1998 Added EnsureVisible. (Roelf Werkman)
// Fixed delete key problem on read-only cells. (Serge Weinstock)
// OnEndInPlaceEdit sends notification AFTER storing
// the modified text in the cell.
// Added CreateInPlaceEditControl to make it easier to
// change the way cells are edited. (suggested by Chris Clark)
// Added Set/GetGridColor.
// CopyTextToClipboard and PasteTextToGrid problem with
// blank cells fixed, and CopyTextToClipboard tweaked.
// SetModified called when cutting text or hitting DEL. (Jonathan Watters)
// Focus cell made visible when editing begins.
// Blank lines now treated correctly when pasting data.
// Removed ES_MULTILINE style from the default edit control.
// Added virtual CreateCell(row, col) function.
// Fonts now specified on a per-cell basis using Get/SetItemFont.
// 1.08 6 Aug 1998 Ctrl+arrows now allows cell navigation. Modified
// CreateInPlaceEditControl to accept ID of control.
// Added Titletips to grid cells. (Added EnableTitleTips / GetTitleTips)
// 1.09 12 Sep 1998 When printing, parent window title is printed in header - Gert Rijs
// GetNextItem search with GVNI_DROPHILITED now returns
// cells with GVIS_DROPHILITED set, instead of GVIS_FOCUSED (Franco Bez)
// (Also fixed minor bug in GetNextItem) (Franco Bez)
// Cell selection using Shift+arrows works - Franco Bez
// SetModified called after edits ONLY if contents changed (Franco Bez)
// Cell colours now dithered in 256 colour screens.
// Support for MSVC 4.2 (Graham Cheetham)
// 1.10 30 Nov 1998 Titletips now disappear on a scroll event. Compiler errors
// fixed. Grid lines drawing fixed (Graham Cheetham).
// Cell focus fix on Isert Row/Col (Jochen Kauffmann)
// Added DeleteNonFixedRows() (John Rackley)
// Message #define conflict fixed (Oskar Wieland)
// Titletips & cell insert/delete fix (Ramesh Dhar)
// Titletips repeat-creation bug fixed.
// GVN_SELCHANGED message now sends current cell ID
// Font resource leak in GetTextExtent fixed (Gavin Jerman)
// More TAB fixes (Andreas Ruh)
// 1.11 1 Dec 1998 GetNextItem bug fix (suggested by Francis Fu)
// InsertColumn (-1) fix (Roy Hopkins)
// Was too liberal with the "IsEditable"'s. oops. (Michel Hete)
// 1.11a 4 Jan 1999 Compiler errors in VC6 fixed.
// 1.12 10 Apr 1999 Cleanup to allow GRIDCONTROL_NO_CLIPBOARD define
// CE #defines added. (Thanks to Frank Uzzolino for a start on this)
// TitleTip display fixed for cells with images, plus it now uses cell font
// Added GetTextRect and IsCellFixed
// Focus change problem when resizing columns fixed (Sergey Nikiforenko)
// Grid line drawing problem in fixed cells fixed (Sergey Nikiforenko)
// CreateCell format persistance bug fixed (Sergey Nikiforenko)
// DeleteColumn now returns TRUE (oops) (R. Elmer)
// Enter, Tab and Esc key problem (finally) fixed - Darren Webb and Koay Kah Hoe
// OnSize infinite loop fixed - Steve Kowald
// GVN_SELCHANGING and GVN_SELCHANGED values changed to avoid conflicts (Hiroaki Watanabe)
// Added single row selection mode (Yao Cai)
// Fixed image drawing clip problem
// Reduced unnecessary redraws significantly
// GetNextItem additions and bug fix, and GVNI_AREA search option (Franco Bez)
// Added GVIS_MODIFIED style for cells, so individual cells can have their
// modification status queried. (Franco Bez)
// 1.12a 15 Apr 1999 Removed the SetModified/GetModified inlines (no compiler warning!)
// Renamed IDC_INPLACE_CONTROL to IDC_INPLACE_CONTROL and moved
// to the header
//
// TODO:
// - OnOutOfMemory function instead of exceptions
// - Decrease timer interval over time to speed up selection over time
//
// NOTE: Grid data is stored row-by-row, so all operations on large numbers
// of cells should be done row-by-row as well.
//
// KNOWN ISSUES TO BE ADDRESSED (Please don't send bug reports):
// * Killfocus comes to late when a command is selected by the Menu.
// When you are editing a cell and choose a Menuitem that searches for all the
// modified cells it is not found. When you chose the menu a second time it is
// found. I assume that the Menu command is executed before the cell receives the
// KillFocus event. Expect similar Problems with accelerators. (Franco Bez)
// * When you select a cell and move the mouse around (with the Left button down
// i.e continuing with your selection) - if the mouse is over the Fixed column
// or Row the drawing of the selected region is strange - in particular as you
// move up and down say the Left Fixed Column notice the behaviour of the Focus
// Cell - it is out of sync. (Vinay Desai)
//
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "MemDC.h"
#include "GridCtrl.h"
#include "InPlaceEdit.h"
// OLE stuff for clipboard operations
#include <afxadv.h> // For CSharedFile
#include <afxconv.h> // For LPTSTR -> LPSTR macros
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define HEADER_HEIGHT 2 // For printing
#define FOOTER_HEIGHT 2
#define LEFT_MARGIN 4
#define RIGHT_MARGIN 4
#define TOP_MARGIN 1
#define BOTTOM_MARGIN 1
#define GAP 1
#define SELECTED_CELL_FONT_WEIGHT 600 // weight of text for selected items
IMPLEMENT_DYNCREATE(CGridCtrl, CWnd)
void AFXAPI DDX_GridControl(CDataExchange* pDX, int nIDC, CGridCtrl& rControl)
{
if (rControl.GetSafeHwnd() == NULL) // not subclassed yet
{
ASSERT(!pDX->m_bSaveAndValidate);
HWND hWndCtrl = pDX->PrepareCtrl(nIDC);
if (!rControl.SubclassWindow(hWndCtrl))
{
ASSERT(FALSE); // possibly trying to subclass twice?
AfxThrowNotSupportedException();
}
#ifndef _AFX_NO_OCC_SUPPORT
else
{
// If the control has reparented itself (e.g., invisible control),
// make sure that the CWnd gets properly wired to its control site.
if (pDX->m_pDlgWnd->GetSafeHwnd() != ::GetParent(rControl.GetSafeHwnd()))
rControl.AttachControlSite(pDX->m_pDlgWnd);
}
#endif //!_AFX_NO_OCC_SUPPORT
}
}
// 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
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);
}
return nScrollLines;
}
/////////////////////////////////////////////////////////////////////////////
// CGridCtrl
CGridCtrl::CGridCtrl(int nRows, int nCols, int nFixedRows, int nFixedCols)
{
RegisterWindowClass();
// Initialize OLE libraries
m_bMustUninitOLE = FALSE;
#if !defined(GRIDCONTROL_NO_DRAGDROP) || !defined(GRIDCONTROL_NO_CLIPBOARD)
_AFX_THREAD_STATE* pState = AfxGetThreadState();
if (!pState->m_bNeedTerm)
{
SCODE sc = ::OleInitialize(NULL);
if (FAILED(sc))
AfxMessageBox(_T("OLE initialization failed. Make sure that the OLE libraries are the correct version"));
else
m_bMustUninitOLE = TRUE;
}
#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_crGridColour = RGB(192,192,192);
m_nRows = 0;
m_nCols = 0;
m_nFixedRows = 0;
m_nFixedCols = 0;
m_nDefCellHeight = 10; // These will get changed to something meaningful
m_nDefCellWidth = 30; // when the window is created or subclassed
m_nVScrollMax = 0; // Scroll position
m_nHScrollMax = 0;
m_nMargin = 0; // cell padding
m_nRowsPerWheelNotch = GetMouseScrollLines(); // Get the number of lines
// per mouse wheel notch to scroll
m_MouseMode = MOUSE_NOTHING;
m_nGridLines = GVL_BOTH;
m_bEditable = TRUE;
m_bListMode = FALSE;
m_bSingleRowSelection = FALSE;
m_bAllowDraw = TRUE; // allow draw updates
m_bEnableSelection = TRUE;
m_bAllowRowResize = TRUE;
m_bAllowColumnResize = TRUE;
m_bSortOnClick = TRUE; // Sort on header row click if in list mode
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_bAscending = TRUE; // sorting stuff
m_SortColumn = -1;
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;
m_bAllowDragAndDrop = FALSE; // for drag and drop
#ifndef _WIN32_WCE
// Initially use the system message font for the GridCtrl font
NONCLIENTMETRICS ncm;
ncm.cbSize = sizeof(NONCLIENTMETRICS);
VERIFY(SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, 0));
memcpy(&m_Logfont, &(ncm.lfMessageFont), sizeof(LOGFONT));
#else
LOGFONT lf;
GetObject(GetStockObject(SYSTEM_FONT), sizeof(LOGFONT), &lf);
memcpy(&m_Logfont, &lf, sizeof(LOGFONT));
#endif
// Set up the initial grid size
SetRowCount(nRows);
SetColumnCount(nCols);
SetFixedRowCount(nFixedRows);
SetFixedColumnCount(nFixedCols);
// Set the colours
SetTextColor(m_crWindowText);
SetTextBkColor(m_crWindowColour);
SetBkColor(m_crShadow);
SetFixedTextColor(m_crWindowText);
SetFixedBkColor(m_cr3DFace);
// set initial selection range (ie. none)
m_SelectedCellMap.RemoveAll();
m_PrevSelectedCellMap.RemoveAll();
}
CGridCtrl::~CGridCtrl()
{
DeleteAllItems();
DestroyWindow();
m_Font.DeleteObject();
#if !defined(GRIDCONTROL_NO_DRAGDROP) || !defined(GRIDCONTROL_NO_CLIPBOARD)
// Uninitialize OLE support
if (m_bMustUninitOLE)
::OleUninitialize();
#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;
wndcls.hCursor = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
wndcls.hbrBackground = (HBRUSH) (COLOR_3DFACE + 1);
wndcls.lpszMenuName = NULL;
wndcls.lpszClassName = GRIDCTRL_CLASSNAME;
if (!AfxRegisterClass(&wndcls)) {
AfxThrowResourceException();
return FALSE;
}
}
return TRUE;
}
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;
#ifndef GRIDCONTROL_NO_DRAGDROP
m_DropTarget.Register(this);
#endif
// Create titletips
#ifndef GRIDCONTROL_NO_TITLETIPS
if (m_bTitleTips)
m_TitleTip.Create(this);
#endif
// 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();
e->Delete();
return FALSE;
}
END_CATCH
for (int i = 0; i < m_nRows; i++) m_arRowHeights[i] = m_nDefCellHeight;
for (i = 0; i < m_nCols; i++) m_arColWidths[i] = m_nDefCellWidth;
ResetScrollBars();
return TRUE;
}
void CGridCtrl::PreSubclassWindow()
{
CWnd::PreSubclassWindow();
HFONT hFont = ::CreateFontIndirect(&m_Logfont);
OnSetFont((LPARAM)hFont, 0);
DeleteObject(hFont);
ResetScrollBars();
}
BOOL CGridCtrl::SubclassWindow(HWND hWnd)
{
if (!CWnd::SubclassWindow(hWnd))
return FALSE;
#ifndef GRIDCONTROL_NO_DRAGDROP
m_DropTarget.Register(this);
#endif
#ifndef GRIDCONTROL_NO_TITLETIPS
if (m_bTitleTips && !IsWindow(m_TitleTip.m_hWnd))
m_TitleTip.Create(this);
#endif
return TRUE;
}
LRESULT CGridCtrl::SendMessageToParent(int nRow, int nCol, int nMessage)
{
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;
}
BEGIN_MESSAGE_MAP(CGridCtrl, CWnd)
//{{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_ERASEBKGND()
ON_COMMAND(ID_EDIT_SELECT_ALL, OnEditSelectAll)
ON_UPDATE_COMMAND_UI(ID_EDIT_SELECT_ALL, OnUpdateEditSelectAll)
//}}AFX_MSG_MAP
#ifndef _WIN32_WCE_NO_CURSOR
ON_WM_SETCURSOR()
#endif
#ifndef _WIN32_WCE
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 !defined(_WIN32_WCE) && (_MFC_VER >= 0x0421)
ON_WM_MOUSEWHEEL()
#endif
#if (_WIN32_WCE >= 210)
ON_WM_SETTINGCHANGE()
#endif
ON_MESSAGE(WM_SETFONT, OnSetFont)
ON_MESSAGE(WM_GETFONT, OnGetFont)
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
{
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 FixedBack(GetFixedBkColor()),
TextBack(GetTextBkColor()),
Back(GetBkColor());
if (pDC->GetClipBox(ClipRect) == ERROR)
return;
GetVisibleNonFixedCellRange(VisRect);
// Draw Fixed columns background
int nFixedColumnWidth = GetFixedColumnWidth();
if (ClipRect.left < nFixedColumnWidth && ClipRect.top < VisRect.bottom)
pDC->FillRect(CRect(ClipRect.left, ClipRect.top,
nFixedColumnWidth, VisRect.bottom),
&FixedBack);
// Draw Fixed rows background
int nFixedRowHeight = GetFixedRowHeight();
if (ClipRect.top < nFixedRowHeight &&
ClipRect.right > nFixedColumnWidth && ClipRect.left < VisRect.right)
pDC->FillRect(CRect(nFixedColumnWidth-1, ClipRect.top,
VisRect.right, nFixedRowHeight),
&FixedBack);
// 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;
// Start re-entry blocking
bAlreadyInsideThisProcedure = TRUE;
// if (::IsWindow(GetSafeHwnd()) && GetFocus()->GetSafeHwnd() != GetSafeHwnd())
SetFocus(); // Auto-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 (GetTextColor() == m_crWindowText) // Still using system colours
SetTextColor(::GetSysColor(COLOR_WINDOWTEXT)); // set to new system colour
if (GetTextBkColor() == m_crWindowColour)
SetTextBkColor(::GetSysColor(COLOR_WINDOW));
if (GetBkColor() == m_crShadow)
SetBkColor(::GetSysColor(COLOR_3DSHADOW));
if (GetFixedTextColor() == m_crWindowText)
SetFixedTextColor(::GetSysColor(COLOR_WINDOWTEXT));
if (GetFixedBkColor() == m_cr3DFace)
SetFixedBkColor(::GetSysColor(COLOR_3DFACE));
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 (GetTextColor() == m_crWindowText) // Still using system colours
SetTextColor(::GetSysColor(COLOR_WINDOWTEXT)); // set to new system colour
if (GetTextBkColor() == m_crWindowColour)
SetTextBkColor(::GetSysColor(COLOR_WINDOW));
if (GetBkColor() == m_crShadow)
SetBkColor(::GetSysColor(COLOR_3DSHADOW));
if (GetFixedTextColor() == m_crWindowText)
SetFixedTextColor(::GetSysColor(COLOR_WINDOWTEXT));
if (GetFixedBkColor() == m_cr3DFace)
SetFixedBkColor(::GetSysColor(COLOR_3DFACE));
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 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;
if (IsCTRLpressed())
{
switch (nChar)
{
case 'A': OnEditSelectAll(); break;
#ifndef GRIDCONTROL_NO_CLIPBOARD
case 'X': OnEditCut(); break;
case 'C': OnEditCopy(); break;
case 'V': OnEditPaste(); break;
#endif
}
}
switch (nChar)
{
case VK_DELETE:
if (IsCellEditable(m_idCurrentCell.row, m_idCurrentCell.col))
{
SendMessageToParent(m_idCurrentCell.row, m_idCurrentCell.col, GVN_BEGINLABELEDIT);
SetItemText(m_idCurrentCell.row, m_idCurrentCell.col, _T(""));
SetModified(TRUE, m_idCurrentCell.row, m_idCurrentCell.col);
SendMessageToParent(m_idCurrentCell.row, m_idCurrentCell.col, GVN_ENDLABELEDIT);
RedrawCell(m_idCurrentCell);
}
break;
case VK_TAB:
if (IsSHIFTpressed())
{
if (next.col > m_nFixedCols)
next.col--;
else if (next.col == m_nFixedCols && next.row > m_nFixedRows)
{
next.row--;
next.col = GetColumnCount() - 1;
bChangeLine = TRUE;
}
else
CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
}
else
{
if (next.col < (GetColumnCount() - 1))
next.col++;
else if (next.col == (GetColumnCount() - 1) &&
next.row < (GetRowCount() - 1) )
{
next.row++;
next.col = m_nFixedCols;
bChangeLine = TRUE;
}
else
CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
}
break;
case VK_DOWN:
if (next.row < (GetRowCount() - 1))
next.row++;
break;
case VK_UP:
if (next.row > m_nFixedRows)
next.row--;
break;
case VK_RIGHT:
if (next.col < (GetColumnCount() - 1))
next.col++;
break;
case VK_LEFT:
if (next.col > m_nFixedCols)
next.col--;
break;
case VK_NEXT:
{
CCellID idOldTopLeft = GetTopleftNonFixedCell();
SendMessage(WM_VSCROLL, SB_PAGEDOWN, 0);
CCellID idNewTopLeft = GetTopleftNonFixedCell();
int increment = idNewTopLeft.row - idOldTopLeft.row;
if (increment) {
next.row += increment;
if (next.row > (GetRowCount() - 1))
next.row = GetRowCount() - 1;
}
else
next.row = GetRowCount() - 1;
break;
}
case VK_PRIOR:
{
CCellID idOldTopLeft = GetTopleftNonFixedCell();
SendMessage(WM_VSCROLL, SB_PAGEUP, 0);
CCellID idNewTopLeft = GetTopleftNonFixedCell();
int increment = idNewTopLeft.row - idOldTopLeft.row;
if (increment)
{
next.row += increment;
if (next.row < m_nFixedRows)
next.row = m_nFixedRows;
} else
next.row = m_nFixedRows;
break;
}
case VK_HOME:
SendMessage(WM_VSCROLL, SB_TOP, 0);
next.row = m_nFixedRows;
break;
case VK_END:
SendMessage(WM_VSCROLL, SB_BOTTOM, 0);
next.row = GetRowCount() - 1;
break;
default:
CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
}
if (next != m_idCurrentCell)
{
// While moving with the Cursorkeys the current ROW/CELL will get selected
// OR Selection will get expanded when SHIFT is pressed
// Cut n paste from OnLButtonDown - Franco Bez
// Added check for NULL mouse mode - Chris Maunder.
if (m_MouseMode == MOUSE_NOTHING)
{
m_PrevSelectedCellMap.RemoveAll();
m_MouseMode = m_bListMode? MOUSE_SELECT_ROW : MOUSE_SELECT_CELLS;
if (!IsSHIFTpressed() || nChar == VK_TAB)
m_SelectionStartCell = next;
OnSelecting(next);
m_MouseMode = MOUSE_NOTHING;
}
SetFocusCell(next);
if (!IsCellVisible(next))
{
EnsureVisible(next); // Make sure cell is visible
switch (nChar) {
case VK_RIGHT:
SendMessage(WM_HSCROLL, SB_LINERIGHT, 0);
break;
case VK_LEFT:
SendMessage(WM_HSCROLL, SB_LINELEFT, 0);
break;
case VK_DOWN:
SendMessage(WM_VSCROLL, SB_LINEDOWN, 0);
break;
case VK_UP:
SendMessage(WM_VSCROLL, SB_LINEUP, 0);
break;
case VK_TAB:
if (IsSHIFTpressed())
{
if (bChangeLine)
{
SendMessage(WM_VSCROLL, SB_LINEUP, 0);
SetScrollPos32(SB_HORZ, m_nHScrollMax);
break;
}
else
SendMessage(WM_HSCROLL, SB_LINELEFT, 0);
}
else
{
if (bChangeLine)
{
SendMessage(WM_VSCROLL, SB_LINEDOWN, 0);
SetScrollPos32(SB_HORZ, 0);
break;
}
else
SendMessage(WM_HSCROLL, SB_LINERIGHT, 0);
}
break;
}
Invalidate();
}
}
}
// Instant editing of cells when keys are pressed
void CGridCtrl::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if (!IsCTRLpressed() && m_MouseMode == MOUSE_NOTHING)
{
if (!m_bHandleTabKey || (m_bHandleTabKey && nChar != VK_TAB))
OnEditCell(m_idCurrentCell.row, m_idCurrentCell.col, nChar);
}
CWnd::OnChar(nChar, nRepCnt, nFlags);
}
// Callback from any CInPlaceEdits that ended. This just calls OnEndEditCell,
// refreshes the edited cell and moves onto next cell if the return character
// from the edit says we should.
void CGridCtrl::OnEndInPlaceEdit(NMHDR* pNMHDR, LRESULT* pResult)
{
GV_DISPINFO *pgvDispInfo = (GV_DISPINFO *)pNMHDR;
GV_ITEM *pgvItem = &pgvDispInfo->item;