-
Notifications
You must be signed in to change notification settings - Fork 0
/
TreeView.cpp
1197 lines (1034 loc) · 29.8 KB
/
TreeView.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
#include "stdafx.h"
#include "resource.h"
#include "res1.h"
#include "utils.h"
#include "apputils.h"
#include "FBEView.h"
#include "FBDoc.h"
#include "TreeView.h"
extern CElementDescMnr _EDMnr;
// redrawing the tree is _very_ ugly visually, so we first build a copy and compare them
struct TreeNode {
TreeNode *parent,*next,*last,*child;
CString text;
int img;
MSHTML::IHTMLElement *pos;
TreeNode() : parent(0), next(0), child(0), last(0), img(0), pos(0) { }
TreeNode(TreeNode *nn,const CString& s,int ii,MSHTML::IHTMLElement *pp) : parent(nn), next(0), last(0), child(0),
text(s), img(ii), pos(pp)
{
if (pos)
pos->AddRef();
}
~TreeNode() {
if (pos)
pos->Release();
TreeNode *q;
for (TreeNode *n=child;n;n=q) {
q=n->next;
delete n;
}
}
TreeNode *Append(const CString& s,int ii,MSHTML::IHTMLElement *p) {
TreeNode *n=new TreeNode(this,s,ii,p);
if (last) {
last->next=n;
last=n;
} else
last=child=n;
return n;
}
};
static bool SearchUnder(CTreeItem& ret,CTreeItem ii,MSHTML::IHTMLElement *p) {
CTreeItem jj(ii.GetChild());
while (!jj.IsNull()) {
MSHTML::IHTMLElement *n=(MSHTML::IHTMLElement *)jj.GetData();
if (n && n->contains(p)) {
ret=jj;
if (jj.HasChildren())
SearchUnder(ret,jj,p);
return true;
}
jj=jj.GetNextSibling();
}
return false;
}
CTreeItem CTreeView::LocatePosition(MSHTML::IHTMLElement *p) {
CTreeItem ret(TVI_ROOT,this);
if (GetCount()==0 || !p)
return ret; // no items at all
SearchUnder(ret,ret,p);
return ret;
}
void CTreeView::HighlightItemAtPos(MSHTML::IHTMLElement *p) {
CTreeItem ii(LocatePosition(p));
if (ii==m_last_lookup_item)
return;
m_last_lookup_item=ii;
ClearSelection();
if (ii!=TVI_ROOT) {
SelectItem(ii);
// EnsureVisible(ii);
} else
SelectItem(0);
}
static void MakeNode(TreeNode* parent, MSHTML::IHTMLDOMNodePtr elem)
{
if (elem->nodeType != 1)
return;
MSHTML::IHTMLElementPtr he(elem);
/*_bstr_t nn(he->tagName);
_bstr_t cn(he->className);
if (U::scmp(nn,L"DIV")==0) {
// at this point we are interested only in sections/subtitles/poems/stanzas
int img=0;
CString txt;
if (U::scmp(cn,L"poem")==0 || U::scmp(cn,L"stanza")==0) {
txt=FindTitle(elem);
img=3;
}
else if (U::scmp(cn,L"body")==0) {
txt=AU::GetAttrCS(he,L"fbname");
U::NormalizeInplace(txt);
if (txt.IsEmpty())
txt=FindTitle(elem);
img=0;
} else if (U::scmp(cn,L"section")==0) {
txt=FindTitle(elem);
img=0;
} else if (U::scmp(cn,L"epigraph")==0 || U::scmp(cn,L"annotation")==0 ||
U::scmp(cn,L"history")==0 || U::scmp(cn,L"cite")==0)
{
img=0;
}
// Modification by Pilgrim
else if (U::scmp(cn,L"table")==0 || U::scmp(cn,L"tr")==0 || U::scmp(cn,L"th")==0 || U::scmp(cn,L"td")==0) {
txt=FindTitle(elem);
img=3;
}
else if (U::scmp(cn,L"image")==0) {
txt=GetImageFileName(elem);
img=3;
}
else
{
elem=elem->firstChild;
while ((bool)elem)
{
MakeNode(parent,elem);
elem=elem->nextSibling;
}
return;
}
U::NormalizeInplace(txt);
if (txt.IsEmpty())
txt.Format(_T("<%s>"),(const TCHAR *)cn);
parent=parent->Append(txt,img,he);
} else if (U::scmp(nn,L"P")==0 && U::scmp(cn,L"subtitle")==0) {
CString txt((const TCHAR *)he->innerText);
U::NormalizeInplace(txt);
if (txt.IsEmpty())
txt=_T("<subtitle>");
parent=parent->Append(txt,6,he);
}*/
_bstr_t cn(he->className);
CElementDescriptor* ED = 0;
if(_EDMnr.GetElementDescriptor(he, &ED) && ED->ViewInTree())
{
CString txt = ED->GetTitle(he);
U::NormalizeInplace(txt);
if(txt.IsEmpty())
txt.Format(_T("<%s>"), (const TCHAR*)cn);
parent = parent->Append(txt, ED->GetDTImageID(), he);
}
elem = elem->firstChild;
while((bool)elem)
{
MakeNode(parent, elem);
elem = elem->nextSibling;
}
return;
}
static TreeNode *GetDocTree(MSHTML::IHTMLDocument2Ptr& view)
{
TreeNode *root=new TreeNode();
try {
MakeNode(root,view->body);
}
catch (_com_error&) {
}
if (!root->child) {
delete root;
return NULL;
}
return root;
}
static void CompareTreesAndSet(TreeNode *n,CTreeItem ii,bool& fDisableRedraw) {
bool fH1=ii==TVI_ROOT ? ii.m_pTreeView->GetCount()>0 : ii.HasChildren()!=0;
// walk them one by one and check
CTreeItem nc=fH1 ? ii.GetChild() : CTreeItem(NULL,ii.m_pTreeView);
TreeNode *ic=n->child;
CString text;
while (ic && nc) {
int img1,img2;
nc.GetImage(img1,img2);
nc.GetText(text);
if (text!=ic->text || img1!=ic->img) { // differ
// copy the item here
if (!fDisableRedraw) {
ii.m_pTreeView->SetRedraw(FALSE);
fDisableRedraw=true;
}
nc.SetImage(ic->img,ic->img);
nc.SetText(ic->text);
}
MSHTML::IHTMLElement *od=(MSHTML::IHTMLElement *)nc.GetData();
if (od)
od->Release();
if (ic->pos)
ic->pos->AddRef();
nc.SetData((LPARAM)ic->pos);
CompareTreesAndSet(ic,nc,fDisableRedraw);
ic=ic->next;
nc=nc.GetNextSibling();
}
CTreeItem next;
if ((nc || ic) && !fDisableRedraw) {
ii.m_pTreeView->SetRedraw(FALSE);
fDisableRedraw=true;
}
while (nc) { // remove extra children, staring with ic
next=nc.GetNextSibling();
nc.Delete();
nc=next;
}
while (ic) { // append children to ii
nc=ii.AddTail(ic->text,ic->img);
if (ic->pos)
ic->pos->AddRef();
nc.SetData((LPARAM)ic->pos);
if (ic->child)
CompareTreesAndSet(ic,nc,fDisableRedraw);
ic=ic->next;
}
}
void CTreeView::GetDocumentStructure(MSHTML::IHTMLDocument2Ptr& view) {
m_last_lookup_item=0;
TreeNode *root=GetDocTree(view);
if (!root) {
SetRedraw(FALSE);
DeleteAllItems();
SetRedraw(TRUE);
return;
}
bool fDisableRedraw=false;
CompareTreesAndSet(root,CTreeItem(TVI_ROOT,this),fDisableRedraw);
if (fDisableRedraw)
SetRedraw(TRUE);
delete root;
}
void CTreeView::UpdateAll()
{
::SendMessage(m_main_window, WM_COMMAND, MAKELONG(0,IDN_TREE_UPDATE_ME), (LPARAM)m_hWnd);
}
void CTreeView::UpdateDocumentStructure(MSHTML::IHTMLDocument2Ptr& v,MSHTML::IHTMLDOMNodePtr node) {
MSHTML::IHTMLElementPtr ce(node);
/*CTreeItem selected_item = GetFirstSelectedItem();
MSHTML::IHTMLElementPtr selected_elem;
if(!selected_item.IsNull() && selected_item.GetData())
{
selected_elem = (MSHTML::IHTMLElement*)selected_item.GetData();
}*/
CTreeItem ii(LocatePosition(ce));
if (ii==TVI_ROOT) { // huh?
GetDocumentStructure(v);
return;
}
MSHTML::IHTMLElementPtr jj((MSHTML::IHTMLElement *)ii.GetData());
// shortcut for the most common situation
// all changes confined to a P, which is not in the title
if (U::scmp(jj->tagName,L"DIV")==0 && U::scmp(node->nodeName,L"P")==0) {
MSHTML::IHTMLElementPtr tn(U::FindTitleNode(jj));
if (!(bool)tn || (tn!=ce && tn->contains(ce)!=VARIANT_TRUE))
return;
// ???? ???????? ????????? ?????? ? ?????????, ?? ?????? ?????? ????? ?????????
if ((bool)tn && (tn==ce || tn->contains(ce)==VARIANT_TRUE))
{
CString txt = (wchar_t*)tn->innerText;
U::NormalizeInplace(txt);
ii.SetText(txt);
return;
}
}
TreeNode *nn=new TreeNode;
try
{
MakeNode(nn,jj);
}
catch(_com_error&){}
bool fDisableRedraw=false;
// check the item itself
CString text;
int img1,img2;
ii.GetImage(img1,img2);
ii.GetText(text);
if (text!=nn->child->text || img1!=nn->child->img) { // differ
// copy the item here
SetRedraw(FALSE);
fDisableRedraw=true;
ii.SetImage(nn->child->img,nn->child->img);
ii.SetText(nn->child->text);
}
CompareTreesAndSet(nn->child,ii,fDisableRedraw);
if (fDisableRedraw)
SetRedraw(TRUE);
/* if((bool)selected_elem)
{
SelectElement(selected_elem);
}*/
delete nn;
}
BOOL CTreeView::PreTranslateMessage(MSG* pMsg)
{
pMsg;
return FALSE;
}
LRESULT CTreeView::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
// "CTreeViewCtrl::OnCreate()"
LRESULT lRet = DefWindowProc(uMsg, wParam, lParam);
// "OnInitialUpdate"
m_ImageList.CreateFromImage(IDB_STRUCTURE,16,32,RGB(255,0,255),IMAGE_BITMAP);
SetImageList(m_ImageList,TVSIL_NORMAL);
SetScrollTime(1);
FillEDMnr();
bHandled = TRUE;
return lRet;
}
LRESULT CTreeView::OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
SetImageList(NULL,TVSIL_NORMAL);
m_ImageList.Destroy();
/* delete m_bodyED;
delete m_sectionED;
delete m_imageED;
delete m_poemED;*/
// Say that we didn't handle it so that the treeview and anyone else
// interested gets to handle the message
bHandled = FALSE;
return 0;
}
LRESULT CTreeView::OnClick(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
bHandled = SetMultiSelection(wParam, lParam);
// check if we are going to hit an item
/*UINT flags=0;
CTreeItem ii(HitTest(CPoint(LOWORD(lParam),HIWORD(lParam)),&flags));
if (flags&TVHT_ONITEM && !ii.IsNull()) { // try to select and expand it
CTreeItem sel(GetSelectedItem());
if (sel!=ii)
ii.Select();
if (!(ii.GetState(TVIS_EXPANDED)&TVIS_EXPANDED))
ii.Expand();
SetFocus();
} //else*/
//bHandled=FALSE;
return 0;
}
LRESULT CTreeView::OnDblClick(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
// check if we double-clicked an already selected item item
UINT flags=0;
CTreeItem ii(HitTest(CPoint(LOWORD(lParam),HIWORD(lParam)),&flags));
if (flags&TVHT_ONITEM && !ii.IsNull() && ii==GetSelectedItem())
::SendMessage(m_main_window,WM_COMMAND,MAKELONG(0,IDN_TREE_CLICK),(LPARAM)m_hWnd);
return 0;
}
static void RecursiveExpand(CTreeItem n,bool *fEnable) {
for (CTreeItem ch(n.GetChild());!ch.IsNull();ch=ch.GetNextSibling()) {
if (!ch.HasChildren())
continue;
if (!(ch.GetState(TVIS_EXPANDED)&TVIS_EXPANDED)) {
if (!*fEnable) {
*fEnable=true;
ch.GetTreeView()->SetRedraw(FALSE);
}
ch.Expand();
}
RecursiveExpand(ch,fEnable);
}
}
LRESULT CTreeView::OnChar(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
switch (wParam) {
case VK_RETURN: // swallow
break;
case '*': // expand the entire tree
if (GetCount()>0) {
bool fEnable=false;
RecursiveExpand(CTreeItem(TVI_ROOT,this),&fEnable);
if (fEnable)
SetRedraw(TRUE);
}
break;
default: // pass to control
bHandled=FALSE;
}
bHandled=FALSE;
return 0;
}
LRESULT CTreeView::OnKeyDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
if (wParam==VK_RETURN)
::PostMessage(m_main_window,WM_COMMAND,MAKELONG(0,IDN_TREE_RETURN),(LPARAM)m_hWnd);
if ( (wParam==VK_UP || wParam==VK_DOWN) && GetKeyState( VK_SHIFT )&0x8000)
{
// Initialize the reference item if this is the first shift selection
if( !m_hItemFirstSel )
{
m_hItemFirstSel = GetSelectedItem();
ClearSelection();
}
// Find which item is currently selected
HTREEITEM hItemPrevSel = GetSelectedItem();
HTREEITEM hItemNext;
if ( wParam==VK_UP )
hItemNext = GetPrevVisibleItem( hItemPrevSel );
else
hItemNext = GetNextVisibleItem( hItemPrevSel );
if ( hItemNext )
{
// Determine if we need to reselect previously selected item
BOOL bReselect =
!( GetItemState( hItemNext, TVIS_SELECTED ) & TVIS_SELECTED );
// Select the next item - this will also deselect the previous item
SelectItem( hItemNext );
// Reselect the previously selected item
if ( bReselect )
SetItemState( hItemPrevSel, TVIS_SELECTED, TVIS_SELECTED );
}
bHandled=TRUE;
return 0;
}
else if( wParam >= VK_SPACE )
{
m_hItemFirstSel = NULL;
ClearSelection();
}
bHandled=FALSE;
return 0;
}
LRESULT CTreeView::OnBegindrag(int idCtrl, LPNMHDR mhdr, BOOL& bHandled)
{
LPNMTREEVIEW pnmtv = (LPNMTREEVIEW) mhdr;
m_move_from.m_hTreeItem = pnmtv->itemNew.hItem;
m_himlDrag = TreeView_CreateDragImage(*this, pnmtv->itemNew.hItem);
ImageList_BeginDrag(this->m_himlDrag, 0, 0, 0);
ImageList_DragEnter(*this, pnmtv->ptDrag.x,pnmtv->ptDrag.y);
SetCapture();
m_drag = true;
bHandled = false;
return 0;
}
LRESULT CTreeView::OnLButtonUp(UINT, WPARAM, LPARAM, BOOL& bHandled)
{
if(m_drag)
{
EndDrag();
::SendMessage(m_main_window,WM_COMMAND,MAKELONG(0,IDN_TREE_MOVE_ELEMENT),(LPARAM)m_hWnd);
}
bHandled = false;
return 0;
}
LRESULT CTreeView::OnMouseMove(UINT, WPARAM, LPARAM lParam, BOOL& bHandled)
{
if(m_drag)
{
POINTS Pos = MAKEPOINTS(lParam);
POINT point;
point.x = Pos.x;
point.y = Pos.y;
ImageList_DragMove(Pos.x, Pos.y);
UINT flags;
CTreeItem hitem(this->HitTest(point, &flags), this);
if (!hitem.IsNull())
{
if(IsDropChangePosition(flags, hitem))
{
CImageList::DragShowNolock(FALSE);
if(IsParent(m_move_from, hitem))
{
SetItemImage(m_move_to, m_drop_item_nimage, m_drop_item_nimage);
GetItemImage(hitem, m_drop_item_nimage, m_drop_item_nimage);
m_insert_type = CTreeView::none;
SelectItem(hitem);
}
else if(flags & TVHT_ONITEMICON)
{
SetItemImage(m_move_to, m_drop_item_nimage, m_drop_item_nimage);
GetItemImage(hitem, m_drop_item_nimage, m_drop_item_nimage);
SetItemImage(hitem, m_drop_item_nimage + 1, m_drop_item_nimage + 1);
m_insert_type = CTreeView::sibling;
SelectItem(hitem);
}
else if(flags & TVHT_ONITEMLABEL)
{
SetItemImage(m_move_to, m_drop_item_nimage, m_drop_item_nimage);
GetItemImage(hitem, m_drop_item_nimage, m_drop_item_nimage);
SetItemImage(hitem, m_drop_item_nimage + 2, m_drop_item_nimage + 2);
m_insert_type = CTreeView::child;
SelectItem(hitem);
}
else
{
SetItemImage(m_move_to, m_drop_item_nimage, m_drop_item_nimage);
GetItemImage(hitem, m_drop_item_nimage, m_drop_item_nimage);
m_insert_type = CTreeView::none;
}
m_move_to.m_hTreeItem = hitem;
CImageList::DragShowNolock(TRUE);
}
else
{
//SetItemImage(m_move_to, m_drop_item_nimage, m_drop_item_nimage);
}
}
}
bHandled = false;
return 0;
}
LRESULT CTreeView::OnRClick(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/)
{
if(m_drag)
{
EndDrag();
}
UINT flags = 0;
CTreeItem htItem(HitTest(CPoint(LOWORD(lParam),HIWORD(lParam)),&flags), this);
if(!htItem.GetState(TVIS_SELECTED))
{
ClearSelection();
SelectItem(htItem);
}
SendMessage(WM_CONTEXTMENU, (WPARAM) m_hWnd, GetMessagePos());
return 0;
}
LRESULT CTreeView::OnContextMenu(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
{
CPoint ptMousePos = (CPoint)lParam;
// i f Shift-F10
if (ptMousePos.x == -1 && ptMousePos.y == -1)
{
ptMousePos = (CPoint)GetMessagePos();
}
ScreenToClient(&ptMousePos);
UINT uFlags;
CTreeItem htItem(HitTest( ptMousePos, &uFlags ), this);
if( htItem.IsNull() )
return 0;
//m_hActiveItem = htItem;
HMENU menu;
HMENU pPopup;
// the font popup is stored in a resource
menu = ::LoadMenu(_Module.GetResourceInstance(), MAKEINTRESOURCEW(IDR_DOCUMENT_TREE));
pPopup = ::GetSubMenu(menu, 0);
ClientToScreen(&ptMousePos);
BOOL res = ::TrackPopupMenu(pPopup, TPM_LEFTALIGN, ptMousePos.x, ptMousePos.y, 0, *this, 0);
int err = GetLastError();
return 1;
}
bool CTreeView::IsDropChangePosition(UINT flags, HTREEITEM hitem)
{
if(hitem != m_move_to.m_hTreeItem)
return true;
if(flags & TVHT_ONITEMICON)
if(m_insert_type != CTreeView::sibling)
return true;
else
return false;
if(flags & TVHT_ONITEMLABEL)
if (m_insert_type != CTreeView::child)
return true;
else
return false;
if (m_insert_type != CTreeView::none)
return true;
else
return false;
}
bool CTreeView::IsParent(CTreeItem parent, CTreeItem child)
{
if(parent == child)
return true;
while((child = child.GetParent()) && !child.IsNull())
{
if(parent == child)
return true;
}
return false;
}
bool CTreeView::IsSibling(CTreeItem item, CTreeItem sibling)
{
return (IsOneOfNextSibling(item, sibling) || IsOneOfPrevSibling(item, sibling));
}
bool CTreeView::IsOneOfNextSibling(CTreeItem item, CTreeItem sibling)
{
if(item == sibling)
return true;
while((sibling = sibling.GetNextSibling()) && !sibling.IsNull())
{
if(item == sibling)
return true;
}
return false;
}
bool CTreeView::IsOneOfPrevSibling(CTreeItem item, CTreeItem sibling)
{
if(item == sibling)
return true;
while((sibling = sibling.GetPrevSibling()), !sibling.IsNull())
{
if(item == sibling)
return true;
}
return false;
}
void CTreeView::EndDrag()
{
ImageList_DragLeave(*this);
ImageList_EndDrag();
ReleaseCapture();
SetItemImage(m_move_to, m_drop_item_nimage, m_drop_item_nimage);
SelectDropTarget(NULL);
m_drag = false;
}
LRESULT CTreeView::OnCut(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
// remove last selection
SetItemState(m_move_from, 0, TVIS_CUT);
HTREEITEM hitem = GetSelectedItem();
m_move_from = hitem;
SetItemState(hitem, TVIS_CUT, TVIS_CUT);
return 0;
}
LRESULT CTreeView::OnPaste(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
// remove selection
SetItemState(m_move_from, 0, TVIS_CUT);
m_move_to = GetSelectedItem();
m_insert_type = CTreeView::child;
::SendMessage(m_main_window,WM_COMMAND,MAKELONG(0,IDN_TREE_MOVE_ELEMENT),(LPARAM)m_hWnd);
return 0;
}
LRESULT CTreeView::OnView(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
::SendMessage(m_main_window,WM_COMMAND,MAKELONG(0,IDN_TREE_VIEW_ELEMENT),(LPARAM)m_hWnd);
return 0;
}
LRESULT CTreeView::OnViewSource(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
::SendMessage(m_main_window,WM_COMMAND,MAKELONG(0,IDN_TREE_VIEW_ELEMENT_SOURCE),(LPARAM)m_hWnd);
return 0;
}
LRESULT CTreeView::OnDelete(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
::SendMessage(m_main_window,WM_COMMAND,MAKELONG(0,IDN_TREE_DELETE_ELEMENT),(LPARAM)m_hWnd);
return 0;
}
LRESULT CTreeView::OnRight(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
//m_move_from = GetSelectedItem();
// ???? ????????? ??????? ?????? ?????? ????????
HTREEITEM from = GetSelectedItem();
HTREEITEM item = GetSelectedItem();// = TreeView_GetNextSibling(*this, m_move_from);
HTREEITEM prevItem = 0;
while(item = TreeView_GetNextSibling(*this, item))
{
prevItem = item;
}
item = prevItem;
m_move_to = GetSelectedItem();
m_insert_type = CTreeView::child;
while(item = TreeView_GetPrevSibling(*this, item))
{
if(prevItem == from)
break;
m_move_from = prevItem;
::SendMessage(m_main_window,WM_COMMAND,MAKELONG(0,IDN_TREE_MOVE_ELEMENT),(LPARAM)m_hWnd);
prevItem = item;
}
m_move_from = from;
m_move_to = TreeView_GetPrevSibling(*this, m_move_from);
if(m_move_to)
{
item = TreeView_GetPrevSibling(*this, m_move_from);
if(!item || ! m_move_from)
return 0;
if(TreeView_GetChild(*this, item))
{
item = TreeView_GetChild(*this, item);
while (item)
{
m_move_to = item;
item = TreeView_GetNextSibling(*this, item);
}
m_insert_type = CTreeView::sibling;
}
else
{
m_move_to = item;
m_insert_type = CTreeView::child;
}
}
else
{
}
::SendMessage(m_main_window,WM_COMMAND,MAKELONG(0,IDN_TREE_MOVE_ELEMENT),(LPARAM)m_hWnd);
return 0;
}
LRESULT CTreeView::OnRightOne(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
::SendMessage(m_main_window,WM_COMMAND,MAKELONG(0,IDN_TREE_MOVE_ELEMENT_ONE),(LPARAM)m_hWnd);
/*HTREEITEM item = GetFirstSelectedItem();
if(!item)
return 0;
do
{
MoveRightOne(item);
}while(item = GetNextSelectedItem(item));*/
return 0;
}
LRESULT CTreeView::OnRightSmart(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
::SendMessage(m_main_window,WM_COMMAND,MAKELONG(0,IDN_TREE_MOVE_ELEMENT_SMART),(LPARAM)m_hWnd);
return 0;
}
LRESULT CTreeView::OnLeftOne(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
::SendMessage(m_main_window,WM_COMMAND,MAKELONG(0,IDN_TREE_MOVE_LEFT_ONE),(LPARAM)m_hWnd);
return 0;
}
LRESULT CTreeView::OnLeftWithChildren(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
::SendMessage(m_main_window,WM_COMMAND,MAKELONG(0,IDN_TREE_MOVE_LEFT),(LPARAM)m_hWnd);
return 0;
}
LRESULT CTreeView::OnMerge(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
::SendMessage(m_main_window, WM_COMMAND, MAKELONG(0, IDN_TREE_MERGE), (LPARAM)m_hWnd);
return 0;
}
LRESULT CTreeView::OnLeft(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
::SendMessage(m_main_window,WM_COMMAND,MAKELONG(0,IDN_TREE_MOVE_LEFT),(LPARAM)m_hWnd);
/*m_move_from = GetSelectedItem();
m_move_to = TreeView_GetParent(*this, m_move_from);
m_insert_type = InsertType::sibling;
::SendMessage(m_main_window,WM_COMMAND,MAKELONG(0,IDN_TREE_MOVE_ELEMENT),(LPARAM)m_hWnd);*/
return 0;
}
bool CTreeView::SetMultiSelection(UINT nFlags, CPoint point)
{
// Set focus to control if key strokes are needed.
// Focus is not automatically given to control on lbuttondown
//m_dwDragStart = GetTickCount();
UINT flag;
HTREEITEM hItem = HitTest( point, &flag );
if(nFlags & MK_SHIFT)
{
// Shift key is down
// Initialize the reference item if this is the first shift selection
if( !m_hItemFirstSel )
m_hItemFirstSel = GetSelectedItem();
// Select new item
if( GetSelectedItem() == hItem )
SelectItem( NULL ); // to prevent edit
// OnLButtonDown(nFlags, point);
if( m_hItemFirstSel )
{
SelectItems( m_hItemFirstSel, hItem, !(BOOL)(nFlags & MK_CONTROL));
return true;
}
}
else if(nFlags & MK_CONTROL )
{
// Control key is down
if( hItem )
{
// Toggle selection state
UINT uNewSelState =
GetItemState(hItem, TVIS_SELECTED) & TVIS_SELECTED ?
0 : TVIS_SELECTED;
// Get old selected (focus) item and state
HTREEITEM hItemOld = GetSelectedItem();
UINT uOldSelState = hItemOld ?
GetItemState(hItemOld, TVIS_SELECTED) : 0;
// Select new item
if( GetSelectedItem() == hItem )
{
SelectItem( NULL ); // to prevent edit
}
// OnLButtonDown(nFlags, point);
// Set proper selection (highlight) state for new item
SelectItem(hItem);
SetItemState(hItem, uNewSelState, TVIS_SELECTED);
// Restore state of old selected item
if (hItemOld && hItemOld != hItem)
SetItemState(hItemOld, uOldSelState, TVIS_SELECTED);
m_hItemFirstSel = NULL;
return true;
}
}
else
{
// Normal - remove all selection and let default
// handler do the rest
ClearSelection();
//SelectItem(hItem);
if(flag & TVHT_ONITEMICON || flag & TVHT_ONITEMLABEL)
TreeView_SetItemState(*this, hItem, TVIS_SELECTED, TVIS_SELECTED);
m_hItemFirstSel = NULL;
}
// OnLButtonDown(nFlags, point);
return false;
}
void CTreeView::ClearSelection()
{
// This can be time consuming for very large trees
// and is called every time the user does a normal selection
// If performance is an issue, it may be better to maintain
// a list of selected items
for (CTreeItem hItem(GetRootItem(), this); hItem!=NULL; hItem=GetNextItem(hItem))
if ( GetItemState( hItem, TVIS_SELECTED ) & TVIS_SELECTED )
SetItemState( hItem, 0, TVIS_SELECTED );
}
// SelectItems - Selects items from hItemFrom to hItemTo. Does not
// - select child item if parent is collapsed. Removes
// - selection from all other items
// hItemFrom - item to start selecting from
// hItemTo - item to end selection at.
BOOL CTreeView::SelectItems(HTREEITEM hItemFrom, HTREEITEM hItemTo, bool clearPrevSelection)
{
HTREEITEM hItem = GetRootItem();
// Clear selection upto the first item
while ( hItem && hItem!=hItemFrom && hItem!=hItemTo )
{
hItem = GetNextVisibleItem( hItem );
if(clearPrevSelection)
{
SetItemState( hItem, 0, TVIS_SELECTED );
}
}
if ( !hItem )
return FALSE; // Item is not visible
SelectItem( hItemTo );
// Rearrange hItemFrom and hItemTo so that hItemFirst is at top
if( hItem == hItemTo )
{
hItemTo = hItemFrom;
hItemFrom = hItem;
}
// Go through remaining visible items
BOOL bSelect = TRUE;
while ( hItem )
{
// Select or remove selection depending on whether item
// is still within the range.
SetItemState( hItem, bSelect ? TVIS_SELECTED : 0, TVIS_SELECTED );
// Do we need to start removing items from selection
if( hItem == hItemTo )
bSelect = FALSE;
hItem = GetNextVisibleItem( hItem );
}
return TRUE;
}
CTreeItem CTreeView::GetFirstSelectedItem()
{
for ( CTreeItem hItem(GetRootItem(), this); !hItem.IsNull(); hItem = GetNextItem( hItem ) )
if ( GetItemState( hItem, TVIS_SELECTED ) & TVIS_SELECTED )
return hItem;
return NULL;
}
CTreeItem CTreeView::GetLastSelectedItem()
{
CTreeItem ret;
for ( CTreeItem hItem(GetRootItem(), this); !hItem.IsNull(); hItem = GetNextItem( hItem ) )
if ( GetItemState( hItem, TVIS_SELECTED ) & TVIS_SELECTED )
{
ret = hItem;
}
return ret;
}
CTreeItem CTreeView::GetNextSelectedItem( CTreeItem hItem )
{
for ( hItem = GetNextItem( hItem ); !hItem.IsNull(); hItem = GetNextItem( hItem ) )
if ( GetItemState( hItem, TVIS_SELECTED ) & TVIS_SELECTED )
return hItem;