-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsupergridctrl.cpp
2539 lines (2092 loc) · 59.4 KB
/
supergridctrl.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
// SuperGridCtrl.cpp : implementation file
//
#include "stdafx.h"
#include "SuperGrid.h"
#include "SuperGridCtrl.h"
#include "ListEditCtrl.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/*
SuperGrid, YALC (yet another listview control)
Written by me(Allan Nielsen), that is .. if the code works :)
mailto:allan@object-oriented.com
Copyright (c) 1999.
If you find bugs, please notify me and I'll contact the author who wrote the darn thing :)
You may use it, abuse it, redistribute it, in any way you desire, but
if you choose to use this control in any commercial or non commercial application then please
send me an email letting me know, makes me :) .
HISTORY
what's new since last update:
4 dec. 1998.
- MeasureItem now uses current font
- Combobox support in CItemInfo
see the ShowList in MySuperGrid.h/cpp, PreTranslateMessage functions
and the file ComboInListView.cpp/h
- function to indicate the precens of a combobox it draws a down arrow ..see DrawComboBox
the idea came from Robert Bouwens. :)
- in file TestTreeDlg.cpp: due to the MeasureItem message-reflection
the listctrl is now created in the CDialog::OnCreate function and later on resized
- LVIS_DROPHILITED support when dragging items
- Support for LVS_EX_CHECKBOXES
- Added some examples on how to print preview selected item(or at least to get some information from of the grid)
the sample is in the CMySuperGrid.h/cpp which is derived from CSuperGridCtrl it shows you
how to initalize the grid, sort items, search for items and subitems, select and delete thems
- added virtual GetIcon function to set your default icon handler for a listview index.
- added virtual GetCellRGB() to set the color for current selected cell
- renamed CTreeNode to CTreeItem, sorry ;-(
- the nested class CTreeItem has been stripped down to nothing but simple (struct)data members
only servering as a linked list of lists now :-)
- well.. moved all the importent stuff from CTreeItem to CSuperGridCtrl e.g
all operations on a CTreeItem now resides in the CSuperGridCtrl, much more OO-like ;-)
- added Quicksort function
- got rid of TheOpenCloseThing function.
- added virtual function OnUpdateListViewItem..called when ever an Item is about to be updated
- added virtual function OnControlLButtonDown...called when ever LButtondown in a cell
- added virtual function CreateDragImageEx...
11 jan 1999:
- added SetFocus in OnDblclk and in OnLButtonDown, big thank you to Dieter Gollwitzer
- fixed a minor hittest error in the HitTestOnSign(), again thanks to Dieter Gollwitzer
16 jan 1999:
- added virtual OnItemExpanding(....
- added virtual OnItemExpanded(...
- added virtual OnCollapsing(....
- added virtual OnItemCollapsed(...
- added virtual OnDeleteItem(....
- added virtual OnVKMultiply(...
- added virtual OnVkSubTract(..
- added virtual OnVKAdd(....
- added SetChildrenFlag();
1 feb 1999:
- CItemInfo now supports individual cell-color
- fixed some bugs in regards to listdata associated with in each cell
- added virtual BOOL OnVkReturn(........
- added virtual BOOL OnItemLButtonDown(.....
4 mai 1999 - moved to Switzerland to work as a systemdeveloper for 2-3 years :).
- new email :)
- added new class for encapsulating drawing + - buttons.
22.june 1999 - Added GetCount()
- removed support for VC++ 5.0.
- added GetTreeItem(.....)
- GetSelectedItem(.....)
- Howto disable Drag/drop in CMySuperGrid
- level 4 compiled
- Clean up some redudant code crap
30.june 99 - fixed minor error in DrawItem, a drawing error occurred when 1.st column was hidden,
it was previous fixed, but some how it was introduced again :).
9. juli 99 - CRAIG SCHMIDT "the beachboy":)
Craig did the implementation of the Multiple Root request
his email are "craig_a_schmidt@yahoo.com" if you have any Questions
in regards to multiple roots implementation, although I think I can help too :).
he added the following functions :
- CTreeItem* InsertRootItem(.....
- void DeleteRootItem(....
- BOOL IsRoot(...
- CTreeItem* GetRootItem(..
- int GetRootIndex(..
- int GetRootCount(...
- and a member variable, CPtrList m_RootItems which of course are a collection of ..ta da RootItems a.k.a CTreeItem.
see the file CMySuperGrid.cpp for usage of the InsertRootItem.
10.juli 99 added simple wrappers for the rootitems collection
- Added GetRootHeadPosition(...)
- Added GetRootTailPosition();
- Added CTreeItem* GetNextRoot(...
- Added CTreeItem* GetPrevRoot(...
12. juli 99 - fixed minor error in CreateDragImageEx, an error occurred when 1st column was hidden and a drag operation was initiated
13. juli 99 - Added void DeleteAll()...deletes all items in the grid, uhh
- Removed GetNextSiblingItem(...), GetPrevSiblingItem(...) due to support for multiple roots.
- Added CTreeItem *GetNext(...) and CTreeItem* GetPrev(..) due to support for multiple roots.
14. juli 99 - Fixed a memory leak in CMySuperGrid::HowToInsertItemsAfterTheGridHasBeenInitialized....
WHAT'S UP :
- Vacation
- ATL VERSION COMMING UP.
- Better documentation :)
- Peace on earth.
- service pack 4 for VC++ 6.0 :=|
- Windows 3000 :)
*/
/////////////////////////////////////////////////////////////////////////////
// CSuperGridCtrl
CSuperGridCtrl::CSuperGridCtrl()
{
m_cxImage = m_cyImage = m_bIsDragging = m_CurSubItem = 0;
m_nDragTarget=m_nDragItem = -1;
m_psTreeLine.CreatePen(PS_SOLID, 1, RGB(192,192,192));
m_psRectangle.CreatePen(PS_SOLID, 1, RGB(198,198,198));
m_psPlusMinus.CreatePen(PS_SOLID, 1, RGB(0,0,0));
m_brushErase.CreateSolidBrush(RGB(255,255,255));
m_himl = NULL;
}
CSuperGridCtrl::~CSuperGridCtrl()
{
m_psPlusMinus.DeleteObject();
m_psRectangle.DeleteObject();
m_psTreeLine.DeleteObject();
m_brushErase.DeleteObject();
while(m_RootItems.GetCount())
{
CTreeItem * root = (CTreeItem*)m_RootItems.RemoveHead();
if(root!=NULL && GetData(root) != NULL)
delete GetData(root);
delete root;
}
m_RootItems.RemoveAll();
}
BEGIN_MESSAGE_MAP(CSuperGridCtrl, CListCtrl)
//{{AFX_MSG_MAP(CSuperGridCtrl)
ON_NOTIFY_REFLECT(NM_DBLCLK, OnDblclk)
ON_NOTIFY_REFLECT(LVN_ENDLABELEDIT, OnEndlabeledit)
ON_WM_CREATE()
ON_WM_HSCROLL()
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_WM_TIMER()
ON_WM_VSCROLL()
ON_WM_LBUTTONUP()
ON_NOTIFY_REFLECT(LVN_KEYDOWN, OnKeydown)
ON_NOTIFY_REFLECT(LVN_BEGINDRAG, OnBegindrag)
ON_WM_MEASUREITEM_REFLECT()
ON_WM_DRAWITEM_REFLECT()
ON_WM_SYSCOLORCHANGE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSuperGridCtrl message handlers
BOOL CSuperGridCtrl::PreCreateWindow(CREATESTRUCT& cs)
{
cs.style |= LVS_REPORT | LVS_SINGLESEL | LVS_SHAREIMAGELISTS | LVS_OWNERDRAWFIXED | LVS_SHOWSELALWAYS;
return CListCtrl::PreCreateWindow(cs);
}
int CSuperGridCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CListCtrl::OnCreate(lpCreateStruct) == -1)
return -1;
return 0;
}
#define OFFSET_FIRST 2
#define OFFSET_OTHER 6
void CSuperGridCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
if (lpDrawItemStruct->CtlType != ODT_LISTVIEW)
return;
if(lpDrawItemStruct->itemAction == ODA_DRAWENTIRE)
{
if(m_himl==NULL)
{
m_himl = (HIMAGELIST)::SendMessage(m_hWnd, LVM_GETIMAGELIST, (WPARAM)(int)(LVSIL_SMALL), 0L);
if(m_himl==NULL)
return;
}
LV_ITEM lvi;
static _TCHAR szBuff[MAX_PATH];
LPCTSTR pszText;
int nItem = lpDrawItemStruct->itemID;
CRect rcItem(lpDrawItemStruct->rcItem);
lvi.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_STATE | LVIF_INDENT | LVIF_PARAM;
lvi.iItem = nItem;
lvi.iSubItem =0;
lvi.pszText = szBuff;
lvi.cchTextMax = sizeof(szBuff);
lvi.stateMask = 0xFFFF;
GetItem(&lvi);
CTreeItem *pSelItem = (CTreeItem*)lpDrawItemStruct->itemData;
CRect rcLabel;
GetItemRect(nItem, rcLabel, LVIR_LABEL);
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
ASSERT(pDC);
CRect rcClipBox;
pDC->GetClipBox(rcClipBox);
COLORREF crBackground, crText;
if (lpDrawItemStruct->itemState & ODS_SELECTED)
{
// Set the text background and foreground colors
crBackground = GetSysColor (COLOR_HIGHLIGHT);
crText = GetSysColor (COLOR_HIGHLIGHTTEXT);
}
else
{
// Set the text background and foreground colors to the standard window
// colors
crBackground = GetSysColor (COLOR_WINDOW);
crText = GetSysColor (COLOR_WINDOWTEXT);
}
//Get current Icon, you have overridden this I hope :-)
int iImage = GetIcon(pSelItem);
if(iImage!=-1)
{
if(lvi.iImage!=iImage)
{
LV_ITEM lvItem;
lvItem.mask = LVIF_IMAGE;
lvItem.iImage = iImage;
lvItem.iItem = nItem;
lvItem.iSubItem = 0;
SetItem(&lvItem);
lvi.iImage = iImage;
}
}
CRect rc;
GetItemRect(nItem, rc, LVIR_BOUNDS);
CRect rcIcon;
GetItemRect(nItem, rcIcon, LVIR_ICON);
//Draw Current image
int nOffset = (rcItem.Height() - m_cyImage)/2;
int nY = rcItem.bottom - m_cyImage - nOffset;
int nWidth = m_cxImage;//def icon size
//do not draw icon out side 1.st column.
int iTryIndent = GetIndent(pSelItem) * m_cxImage + m_cxImage;
if(rc.left + iTryIndent > GetColumnWidth(0)-2/*looks better -2*/)
nWidth = (rc.left + iTryIndent) - GetColumnWidth(0);
UINT uiFlags = ILD_TRANSPARENT;
if( GetItemState(nItem, LVIF_STATE) & LVIS_DROPHILITED)//if dragging show a SelectDropTarget alike effect :)
uiFlags |= ILD_BLEND50;
if((nWidth=m_cxImage-nWidth) >-1)//calc width of icon
{
ImageList_DrawEx(m_himl, lvi.iImage, pDC->m_hDC,
rc.left + (GetIndent(pSelItem) * m_cxImage),
nY,
nWidth,
m_cyImage,
CLR_DEFAULT,
CLR_DEFAULT,
uiFlags);
DrawTreeItem(pDC, pSelItem, nItem, rc);
}
//Draw selection bar (erase old selection too)
pDC->SetBkColor(crBackground);
CRect rcClip = lpDrawItemStruct->rcItem;
rcClip.left += GetIndent(pSelItem) * m_cxImage + m_cxImage + 2;
if(rcClip.left > GetColumnWidth(0))
rcClip.left = GetColumnWidth(0);
//fill background color
ExtTextOut(pDC->GetSafeHdc(), 0, 0, ETO_OPAQUE, rcClip, NULL, 0, NULL);
//draw color in first col if any
rcClip.right = rcLabel.right;
CItemInfo *pItemInfo = GetData(pSelItem);
COLORREF clf = pItemInfo->GetItemClr();
if(clf!=-1)
{
CBrush br(clf);
pDC->FillRect(rcClip, &br);
}
//draw selection rect in 1.st col if selected
if ((lpDrawItemStruct->itemState & ODS_SELECTED) && (m_CurSubItem==0))
{
CBrush br(GetCellRGB());
pDC->FillRect(rcClip,&br);
pDC->DrawFocusRect(rcClip);
}
//if checkbox style
UINT nStateImageMask = lvi.state & LVIS_STATEIMAGEMASK;
if (nStateImageMask)
{
int nImage = (nStateImageMask>>12) - 1;
CImageList *pImageList = GetImageList(LVSIL_STATE);
if (pImageList)
{
int cxIcon,cyIcon=0;
ImageList_GetIconSize(pImageList->m_hImageList, &cxIcon, &cyIcon);
if(rc.left + (GetIndent(pSelItem) * m_cxImage) + m_cxImage + cxIcon < GetColumnWidth(0))
pImageList->Draw(pDC, nImage,CPoint(rc.left + (GetIndent(pSelItem) * m_cxImage) + cxIcon, nY), ILD_TRANSPARENT);
}
}
//draw 1. item
GetItemRect(nItem, rcItem, LVIR_LABEL);
pszText = MakeShortString(pDC, szBuff, rcItem.right - rcItem.left, 2*OFFSET_FIRST);
rcLabel = rcItem;
rcLabel.left+=OFFSET_FIRST;
rcLabel.right-=OFFSET_FIRST;
pDC->SetBkColor (crBackground);
pDC->SetTextColor (crText);
pDC->DrawText(pszText,-1, rcLabel,DT_LEFT | DT_SINGLELINE | DT_NOPREFIX | DT_VCENTER | DT_EXTERNALLEADING);
//draw subitems..
LV_COLUMN lvc;
lvc.mask = LVCF_FMT | LVCF_WIDTH;
for(int nColumn=1; GetColumn(nColumn, &lvc); nColumn++)
{
rcItem.left=rcItem.right;
rcItem.right+=lvc.cx;
if (rcItem.left < rcClipBox.right && rcItem.right > rcClipBox.left && rcItem.right > rcItem.left)
{
//support for colors in each cell
COLORREF clf = pItemInfo->GetBkColor(nColumn-1);
if(clf!=-1)
{
CBrush br(clf);
pDC->FillRect(rcItem, &br);
}
int nRetLen = GetItemText(nItem, nColumn, szBuff, sizeof(szBuff));
if(nRetLen==0)
pszText=NULL;
else
pszText=MakeShortString(pDC,szBuff,rcItem.right-rcItem.left,2*OFFSET_OTHER);
UINT nJustify=DT_LEFT;
if(pszText==szBuff)
{
switch(lvc.fmt & LVCFMT_JUSTIFYMASK)
{
case LVCFMT_RIGHT:
nJustify=DT_RIGHT;
break;
case LVCFMT_CENTER:
nJustify=DT_CENTER;
break;
default:
break;
}
}
rcLabel=rcItem;
rcLabel.left+=OFFSET_OTHER;
rcLabel.right-=OFFSET_OTHER;
if (lpDrawItemStruct->itemState & ODS_SELECTED && !m_bIsDragging)
DrawFocusCell(pDC, lpDrawItemStruct->itemID, nColumn);
if(pszText!=NULL)
pDC->DrawText(pszText,-1,rcLabel, nJustify | DT_SINGLELINE | DT_NOPREFIX | DT_VCENTER | DT_EXTERNALLEADING);
}//if
//draw down arrow if combobox regardless of rcClipBox rgn
if (lpDrawItemStruct->itemState & ODS_SELECTED)
DrawComboBox(pDC, pSelItem, nItem, nColumn);
}//for
}//ODA_DRAWENTIRE
}
void CSuperGridCtrl::DrawComboBox(CDC* pDC, CTreeItem *pSelItem, int nItem, int nColumn)
{
CItemInfo* pInfo = GetData(pSelItem);
CItemInfo::CONTROLTYPE ctrlType;
if(pInfo->GetControlType(nColumn-1, ctrlType))
{
if(ctrlType == pInfo->CONTROLTYPE::combobox)
{
CRect rect;
GetSubItemRect(nItem, nColumn, LVIR_BOUNDS, rect);
rect.left=rect.right - GetSystemMetrics(SM_CYVSCROLL);
pDC->DrawFrameControl(rect, DFC_SCROLL, DFCS_SCROLLDOWN);
}
}
}
//this piece of code is borrowed from the wndproc.c file in the odlistvw.exe example from MSDN and was converted to mfc-style
void CSuperGridCtrl::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
if (lpMeasureItemStruct->CtlType != ODT_LISTVIEW)
return;
TEXTMETRIC tm;
CClientDC dc(this);
CFont* pFont = GetFont();
CFont* pOldFont = dc.SelectObject(pFont);
dc.GetTextMetrics(&tm);
int nItemHeight = tm.tmHeight + tm.tmExternalLeading;
lpMeasureItemStruct->itemHeight = nItemHeight + 4; //or should I go for max(nItemheight+4, m_cxImage+2);
dc.SelectObject(pOldFont);
}
//the basic rutine making the ... thing snatched it from some tedious code example some where in MSDN call odlistvw.exe
LPCTSTR CSuperGridCtrl::MakeShortString(CDC* pDC, LPCTSTR lpszLong, int nColumnLen, int nOffset)
{
static const _TCHAR szThreeDots[]=_T("...");
int nStringLen=lstrlen(lpszLong);
if(nStringLen==0 || pDC->GetTextExtent(lpszLong,nStringLen).cx + nOffset < nColumnLen)
return(lpszLong);
static _TCHAR szShort[MAX_PATH];
lstrcpy(szShort,lpszLong);
int nAddLen = pDC->GetTextExtent(szThreeDots,sizeof(szThreeDots)).cx;
for(int i=nStringLen-1; i > 0; i--)
{
szShort[i]=0;
if(pDC->GetTextExtent(szShort,i).cx + nOffset + nAddLen < nColumnLen)
break;
}
lstrcat(szShort,szThreeDots);
return(szShort);
}
void CSuperGridCtrl::OnKeydown(NMHDR* pNMHDR, LRESULT* pResult)
{
LV_KEYDOWN* pLVKeyDow = (LV_KEYDOWN*)pNMHDR;
switch(pLVKeyDow->wVKey)
{
case VK_SPACE:
{
if(GetExtendedStyle() & LVS_EX_CHECKBOXES)
{
int nIndex = GetSelectedItem();
if(nIndex !=-1)
{
CTreeItem* pItem = GetTreeItem(nIndex);
if(pItem!=NULL)
{
CItemInfo *pInfo = GetData(pItem);
pInfo->SetCheck(!pInfo->GetCheck());
}
}
}
}break;
case VK_DELETE:
{
int nItem = GetSelectedItem();
if(nItem!=-1)
{
CTreeItem* pSelItem = GetTreeItem(nItem);
if(pSelItem != NULL)
{
if(OnDeleteItem(pSelItem, nItem))
DeleteItemEx(pSelItem, nItem);
}
}
} break;
case VK_MULTIPLY:
{
int nIndex = GetSelectedItem();
if(nIndex != -1)
{
CWaitCursor wait;
SetRedraw(0);
CTreeItem *pParent = GetTreeItem(nIndex);
int nScroll=0;
if(OnVKMultiply(pParent, nIndex))
{
ExpandAll(pParent, nScroll);
}
SetRedraw(1);
RedrawItems(nIndex, nScroll);
EnsureVisible(nScroll, TRUE);
}
}break;
case VK_ADD:
{
int nIndex = GetSelectedItem();
if(nIndex!=-1)
{
CWaitCursor wait;
CTreeItem *pSelItem = GetTreeItem(nIndex);
int nScrollIndex = 0;
if(OnVKAdd(pSelItem, nIndex))
{
nScrollIndex = Expand(pSelItem, nIndex);
}
CRect rc;
GetItemRect(nIndex, rc, LVIR_BOUNDS);
InvalidateRect(rc);
UpdateWindow();
EnsureVisible(nScrollIndex, 1);
}
}break;
case VK_SUBTRACT:
{
int nIndex = GetSelectedItem();
if(nIndex!=-1)
{
CWaitCursor wait;
CTreeItem *pSelItem = GetTreeItem(nIndex);
if(OnVkSubTract(pSelItem, nIndex))
{
Collapse(pSelItem);
}
CRect rc;
GetItemRect(nIndex, rc, LVIR_BOUNDS);
InvalidateRect(rc);
UpdateWindow();
}
}break;
default :break;
}
*pResult = 0;
}
BOOL CSuperGridCtrl::HitTestOnSign(CPoint point, LVHITTESTINFO& ht)
{
ht.pt = point;
// Test which subitem was clicked.
SubItemHitTest(&ht);
if(ht.iItem!=-1)
{
//first hittest on checkbox
BOOL bHit = FALSE;
if(GetExtendedStyle() & LVS_EX_CHECKBOXES)
{
if (ht.flags == LVHT_ONITEM && (GetStyle() & LVS_OWNERDRAWFIXED))//isn't this allways ownerdrawfixed :-)
{
CRect rcIcon,rcLabel;
GetItemRect(ht.iItem, rcIcon, LVIR_ICON);//has to be between these two ....right :)
GetItemRect(ht.iItem, rcLabel, LVIR_LABEL);
// check if hit was on a state icon
if (point.x > rcIcon.left && point.x < rcLabel.left)
bHit = TRUE;
}
else if (ht.flags & LVHT_ONITEMSTATEICON)
bHit = TRUE;
}
CTreeItem* pItem = GetTreeItem(ht.iItem);
if(pItem!=NULL)
{
if(bHit)//if checkbox
{
//yes I know..have to maintain to sets of checkstates here...
//one for listview statemask and one for CTreeItem..but its located here so no harm done
SetCheck(ht.iItem,!GetCheck(ht.iItem));
CItemInfo *pInfo = GetData(pItem);
pInfo->SetCheck(!pInfo->GetCheck());
}
//if haschildren and clicked on + or - then expand/collapse
if(ItemHasChildren(pItem))
{
//hittest on the plus/sign "button"
CRect rcBounds;
GetItemRect(ht.iItem, rcBounds, LVIR_BOUNDS);
CRectangle rect(this, NULL, GetIndent(pItem), rcBounds);
BOOL bRedraw=0;//if OnItemExpanding or OnCollapsing returns false, dont redraw
if(rect.HitTest(point))
{
SetRedraw(0);
int nScrollIndex=0;
if(IsCollapsed(pItem))
{
if(OnItemExpanding(pItem, ht.iItem))
{
nScrollIndex = Expand(pItem, ht.iItem);
OnItemExpanded(pItem, ht.iItem);
bRedraw=1;
}
}
else {
if(OnCollapsing(pItem))
{
Collapse(pItem);
OnItemCollapsed(pItem);
bRedraw=1;
}
}
SetRedraw(1);
if(bRedraw)
{
CRect rc;
GetItemRect(ht.iItem, rc, LVIR_BOUNDS);
InvalidateRect(rc);
UpdateWindow();
EnsureVisible(nScrollIndex, 1);
return 0;
}
}
}//has kids
}//pItem!=NULL
}
return 1;
}
void CSuperGridCtrl::OnDblclk(NMHDR* pNMHDR, LRESULT* pResult)
{
if( GetFocus() != this)
SetFocus();
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
LVHITTESTINFO ht;
ht.pt = pNMListView->ptAction;
SubItemHitTest(&ht);
if(OnItemLButtonDown(ht))
{
BOOL bSelect=1;
bSelect = HitTestOnSign(pNMListView->ptAction, ht);
//normal selection
if(bSelect && ht.iItem !=-1)
{
int nIndex = GetSelectedItem();
if(nIndex!=-1)
{
CTreeItem *pSelItem = GetTreeItem(nIndex);
if (pSelItem != NULL)
{
BOOL bRedraw=0;
if(ItemHasChildren(pSelItem))
{
SetRedraw(0);
int nScrollIndex=0;
if(IsCollapsed(pSelItem))
{
if(OnItemExpanding(pSelItem, nIndex))
{
nScrollIndex = Expand(pSelItem, nIndex);
OnItemExpanded(pSelItem, nIndex);
bRedraw=1;
}
}
else
{
if(OnCollapsing(pSelItem))
{
Collapse(pSelItem);
OnItemCollapsed(pSelItem);
bRedraw=1;
}
}
SetRedraw(1);
if(bRedraw)
{
CRect rc;
GetItemRect(nIndex,rc,LVIR_BOUNDS);
InvalidateRect(rc);
UpdateWindow();
EnsureVisible(nScrollIndex,1);
}
}//ItemHasChildren
}//!=NULL
}
}
}
*pResult = 0;
}
void CSuperGridCtrl::OnBegindrag(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
if(pNMListView->iItem != -1)
{
m_nDragItem = pNMListView->iItem;
CImageList* pDragImage=NULL;
pDragImage = CreateDragImageEx(m_nDragItem);//override this if you want another dragimage or none at all.
if(pDragImage)
{
pDragImage->BeginDrag(0, CPoint(0,0));
pDragImage->DragEnter(this, pNMListView->ptAction);
SetCapture();
m_bIsDragging = TRUE;
}
delete pDragImage;
}
*pResult = 0;
}
//Create dragimage : Icon + the itemtext
CImageList *CSuperGridCtrl::CreateDragImageEx(int nItem)
{
CImageList *pList = new CImageList;
//get image index
LV_ITEM lvItem;
lvItem.mask = LVIF_IMAGE;
lvItem.iItem = nItem;
lvItem.iSubItem = 0;
GetItem(&lvItem);
CRect rc;
GetItemRect(nItem, &rc, LVIR_BOUNDS);
CString str;
str = GetItemText(nItem, 0);
CFont *pFont = GetFont();
rc.OffsetRect(-rc.left, -rc.top);
rc.right = GetColumnWidth(0);
pList->Create(rc.Width(), rc.Height(),ILC_COLOR24| ILC_MASK , 1, 1);
CDC *pDC = GetDC();
if(pDC)
{
CDC dc;
dc.CreateCompatibleDC(pDC);
CBitmap bmpMap;
bmpMap.CreateCompatibleBitmap(pDC, rc.Width(), rc.Height());
CBitmap *pOldBmp = dc.SelectObject(&bmpMap);
CFont *pOldFont = dc.SelectObject(pFont);
dc.FillSolidRect(rc, GetSysColor(COLOR_WINDOW));
CImageList *pImgList = GetImageList(LVSIL_SMALL);
if(pImgList)
pImgList->Draw(&dc, lvItem.iImage, CPoint(0,0), ILD_TRANSPARENT);
dc.TextOut(m_cxImage + 4, 0, str);
dc.SelectObject(pOldFont);
dc.SelectObject(pOldBmp);
//causes an error if the 1st column is hidden so must check the imagelist
if(pList->m_hImageList != NULL)
pList->Add(&bmpMap, RGB(255,255,255));
else {
delete pList;
pList=NULL;
}
ReleaseDC(pDC);
}
return pList;
}
void CSuperGridCtrl::OnMouseMove(UINT nFlags, CPoint point)
{
if(m_bIsDragging)
{
KillTimer(1);
if (CWnd::GetCapture() != this)
m_bIsDragging=0;
if(nFlags==MK_RBUTTON || nFlags==MK_MBUTTON)
m_bIsDragging=0;
if(GetKeyState(VK_ESCAPE) < 0)
m_bIsDragging=0;
if(!m_bIsDragging)//why not put this in a funtion :)
{
SetItemState (m_nDragTarget, 0, LVIS_DROPHILITED);
CImageList::DragLeave(this);
CImageList::EndDrag();
ReleaseCapture();
InvalidateRect(NULL);
UpdateWindow();
}
else
{
CPoint ptList(point);
MapWindowPoints(this, &ptList, 1);
CImageList::DragMove(ptList);
UINT uHitTest = LVHT_ONITEM;
m_nDragTarget = HitTest(ptList, &uHitTest);
// try turn off hilight for previous DROPHILITED state
int nPrev = GetNextItem(-1,LVNI_DROPHILITED);
if(nPrev != m_nDragTarget)//prevents flicker
SetItemState(nPrev, 0, LVIS_DROPHILITED);
CRect rect;
GetClientRect (rect);
int cy = rect.Height();
if(m_nDragTarget!=-1)
{
SetItemState(m_nDragTarget, LVIS_DROPHILITED, LVIS_DROPHILITED);
CTreeItem* pTarget = GetTreeItem(m_nDragTarget);
if ((point.y >= 0 && point.y <= m_cyImage) || (point.y >= cy - m_cyImage && point.y <= cy) ||
( pTarget!=NULL && ItemHasChildren(pTarget) && IsCollapsed(pTarget)))
{
SetTimer(1,300,NULL);
}
}
}
}
CListCtrl::OnMouseMove(nFlags, point);
}
void CSuperGridCtrl::OnTimer(UINT nIDEvent)
{
CListCtrl::OnTimer(nIDEvent);
if(nIDEvent==1)
{
if(CWnd::GetCapture()!=this)
{
SetItemState(m_nDragTarget, 0, LVIS_DROPHILITED);
m_bIsDragging=0;
CImageList::DragLeave(this);
CImageList::EndDrag();
ReleaseCapture();
InvalidateRect(NULL);
UpdateWindow();
KillTimer(1);
return;
}
SetTimer(1,300,NULL);//reset timer
DWORD dwPos = ::GetMessagePos();
CPoint ptList(LOWORD(dwPos),HIWORD(dwPos));
ScreenToClient(&ptList);
CRect rect;
GetClientRect(rect);
int cy = rect.Height();
//
// perform autoscroll if the cursor is near the top or bottom.
//
if (ptList.y >= 0 && ptList.y <= m_cyImage)
{
int n = GetTopIndex();
CImageList::DragShowNolock(0);
SendMessage(WM_VSCROLL, MAKEWPARAM(SB_LINEUP, 0), NULL);
CImageList::DragShowNolock(1);
if (GetTopIndex()== n)
KillTimer (1);
else {
CImageList::DragShowNolock(0);
CImageList::DragMove(ptList);
CImageList::DragShowNolock(1);
return;
}
}
else if (ptList.y >= cy - m_cyImage && ptList.y <= cy)
{
int n = GetTopIndex();
CImageList::DragShowNolock(0);
SendMessage(WM_VSCROLL, MAKEWPARAM(SB_LINEDOWN, 0), NULL);
CImageList::DragShowNolock(1);
if (GetTopIndex()== n)
KillTimer (1);
else {
CImageList::DragShowNolock(0);
CImageList::DragMove(ptList);
CImageList::DragShowNolock(1);
return;
}
}
//Hover test
CImageList::DragMove(ptList);
UINT uHitTest = LVHT_ONITEM;
m_nDragTarget = HitTest(ptList, &uHitTest);
if(m_nDragTarget!=-1)
{
//if the target has children
//expand them
CTreeItem* pTarget=GetTreeItem(m_nDragTarget);
if(pTarget != NULL && ItemHasChildren(pTarget) && IsCollapsed(pTarget) && (m_nDragItem!=-1))
{
CImageList::DragShowNolock(0);
CTreeItem* pSource = GetTreeItem(m_nDragItem);
SetRedraw(0);
int nScrollIndex=0;
if(ItemHasChildren(pTarget) && IsCollapsed(pTarget))
{
if(OnItemExpanding(pTarget, m_nDragTarget))
{
nScrollIndex = Expand(pTarget, m_nDragTarget);
OnItemExpanded(pTarget, m_nDragTarget);
}
}
m_nDragItem = NodeToIndex(pSource);
SetRedraw(1);
EnsureVisible(nScrollIndex, 1);
InvalidateRect(NULL);
UpdateWindow();
CImageList::DragShowNolock(1);
KillTimer(1);
return;
}
}
KillTimer(1);
}
}
void CSuperGridCtrl::OnLButtonUp(UINT nFlags, CPoint point)
{
if(m_bIsDragging == TRUE)
{
KillTimer(1);
CImageList::DragLeave(this);
CImageList::EndDrag();
ReleaseCapture();
m_bIsDragging = FALSE;
SetItemState(m_nDragTarget, 0, LVIS_DROPHILITED);
if((m_nDragTarget != -1) && (m_nDragTarget != m_nDragItem) && (m_nDragItem!=-1))//no drop on me self
{
CTreeItem* pSource = GetTreeItem(m_nDragItem);
CTreeItem* pTarget = GetTreeItem(m_nDragTarget);
if(IsRoot(pSource))