Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[91ef65]: MyTreeCtrl.cpp Maximize Restore History

Download this file

MyTreeCtrl.cpp    997 lines (871 with data), 29.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
/*
* Copyright (c) 2003-2007 Rony Shapiro <ronys@users.sourceforge.net>.
* All rights reserved. Use of the code is allowed under the
* Artistic License terms, as specified in the LICENSE file
* distributed with this code, or available from
* http://www.opensource.org/licenses/artistic-license.php
*/
/*
* Silly subclass of CTreeCtrl just to implement Drag&Drop.
*
* Based on MFC sample code from CMNCTRL1
*/
// Need a set to keep track of what nodes are expanded, to re-expand
// after minimize
#include <set>
using namespace std ;
#include "stdafx.h"
#include "MyTreeCtrl.h"
#include "DboxMain.h"
#include "corelib/ItemData.h"
#include "corelib/MyString.h"
#include "corelib/Util.h"
#include "corelib/Pwsprefs.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
typedef set<CItemData *> SetTreeItem_t;
typedef SetTreeItem_t *SetTreeItemP_t;
static const TCHAR GROUP_SEP = TCHAR('.');
CMyTreeCtrl::CMyTreeCtrl() : m_bDragging(false), m_pimagelist(NULL)
{
m_expandedItems = new SetTreeItem_t;
m_isRestoring = false;
}
CMyTreeCtrl::~CMyTreeCtrl()
{
delete m_pimagelist;
delete (SetTreeItem_t *)m_expandedItems;
}
BEGIN_MESSAGE_MAP(CMyTreeCtrl, CTreeCtrl)
//{{AFX_MSG_MAP(CMyTreeCtrl)
ON_NOTIFY_REFLECT(TVN_BEGINLABELEDIT, OnBeginLabelEdit)
ON_NOTIFY_REFLECT(TVN_ENDLABELEDIT, OnEndLabelEdit)
ON_NOTIFY_REFLECT(TVN_BEGINDRAG, OnBeginDrag)
ON_NOTIFY_REFLECT(TVN_ITEMEXPANDED, OnExpandCollapse)
ON_WM_MOUSEMOVE()
ON_WM_DESTROY()
ON_WM_LBUTTONUP()
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
void CMyTreeCtrl::OnDestroy()
{
CImageList *pimagelist;
pimagelist = GetImageList(TVSIL_NORMAL);
if (pimagelist != NULL) {
pimagelist->DeleteImageList();
delete pimagelist;
}
}
BOOL CMyTreeCtrl::PreTranslateMessage(MSG* pMsg)
{
// When an item is being edited make sure the edit control
// receives certain important key strokes
if (GetEditControl()) {
::TranslateMessage(pMsg);
::DispatchMessage(pMsg);
return TRUE; // DO NOT process further
}
//Hitting the Escape key, Cancelling drag & drop
if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE && m_bDragging) {
EndDragging(TRUE);
return TRUE;
}
//hitting the F2 key, being in-place editing of an item
else if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_F2) {
HTREEITEM hItem = GetSelectedItem();
if (hItem != NULL && !((DboxMain *)GetParent())->IsMcoreReadOnly())
EditLabel(hItem);
return TRUE;
}
//Let the parent class do its thing
return CTreeCtrl::PreTranslateMessage(pMsg);
}
void CMyTreeCtrl::SetNewStyle(long lStyleMask, BOOL bSetBits)
{
long lStyleOld;
lStyleOld = GetWindowLong(m_hWnd, GWL_STYLE);
lStyleOld &= ~lStyleMask;
if (bSetBits)
lStyleOld |= lStyleMask;
SetWindowLong(m_hWnd, GWL_STYLE, lStyleOld);
SetWindowPos(NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER);
}
void CMyTreeCtrl::UpdateLeafsGroup(HTREEITEM hItem, CString prefix)
{
// Starting with hItem, update the Group field of all of hItem's
// children. Called after a label has been edited.
if (IsLeafNode(hItem)) {
DWORD itemData = GetItemData(hItem);
ASSERT(itemData != NULL);
CItemData *ci = (CItemData *)itemData;
ci->SetGroup(CMyString(prefix));
} else { // update prefix with current group name and recurse
if (!prefix.IsEmpty())
prefix += GROUP_SEP;
prefix += GetItemText(hItem);
HTREEITEM child;
for(child = GetChildItem(hItem); child != NULL; child = GetNextSiblingItem(child)) {
UpdateLeafsGroup(child, prefix);
}
}
}
void CMyTreeCtrl::OnBeginLabelEdit(LPNMHDR pnmhdr, LRESULT *pLResult)
{
NMTVDISPINFO *ptvinfo = (NMTVDISPINFO *)pnmhdr;
*pLResult = TRUE; // TRUE cancels label editing
/*
Allowed formats:
1. title
If preference ShowUsernameInTree is set:
2. title [username]
If preferences ShowUsernameInTree and ShowPasswordInTree are set:
3. title [username] {password}
Neither Title, Username or Password may contain square or curly brackes to be
edited in place and visible.
*/
HTREEITEM ti = ptvinfo->item.hItem;
PWSprefs *prefs = PWSprefs::GetInstance();
if (IsLeafNode(ti)) {
DWORD itemData = GetItemData(ti);
ASSERT(itemData != NULL);
CItemData *ci = (CItemData *)itemData;
CMyString currentTitle, currentUser, currentPassword;
// We cannot allow in-place edit if these fields contain braces!
currentTitle = ci->GetTitle();
if (currentTitle.FindOneOf(_T("[]{}")) != -1)
return;
currentUser = ci->GetUser();
if (prefs->GetPref(PWSprefs::ShowUsernameInTree) &&
currentUser.FindOneOf(_T("[]{}")) != -1)
return;
currentPassword = ci->GetPassword();
if (prefs->GetPref(PWSprefs::ShowPasswordInTree) &&
currentPassword.FindOneOf(_T("[]{}")) != -1)
return;
}
// Allow in-place editing
*pLResult = FALSE;
return;
}
static bool splitLeafText(const TCHAR *lt, CString &newTitle,
CString &newUser, CString &newPassword)
{
bool bPasswordSet(false);
newTitle = _T("");
newUser = _T("");
newPassword = _T("");
CString cs_leafText(lt);
cs_leafText.Trim();
if (cs_leafText.IsEmpty())
return false;
// Check no duplicate braces
int OpenSquareBraceIndex = cs_leafText.Find(TCHAR('['));
if (OpenSquareBraceIndex >= 0)
if (cs_leafText.Find(TCHAR('['), OpenSquareBraceIndex + 1) != -1)
return false;
int CloseSquareBraceIndex = cs_leafText.Find(TCHAR(']'));
if (CloseSquareBraceIndex >= 0)
if (cs_leafText.Find(TCHAR(']'), CloseSquareBraceIndex + 1) != -1)
return false;
int OpenCurlyBraceIndex = cs_leafText.Find(TCHAR('{'));
if (OpenCurlyBraceIndex >= 0)
if (cs_leafText.Find(TCHAR('{'), OpenCurlyBraceIndex + 1) != -1)
return false;
int CloseCurlyBraceIndex = cs_leafText.Find(TCHAR('}'));
if (CloseCurlyBraceIndex >= 0)
if (cs_leafText.Find(TCHAR('}'), CloseCurlyBraceIndex + 1) != -1)
return false;
// Check we have both open and close brackets
if (OpenSquareBraceIndex >= 0 && CloseSquareBraceIndex == -1)
return false;
if (OpenSquareBraceIndex == -1 && CloseSquareBraceIndex >= 0)
return false;
if (OpenCurlyBraceIndex >= 0 && CloseCurlyBraceIndex == -1)
return false;
if (OpenCurlyBraceIndex == -1 && CloseCurlyBraceIndex >= 0)
return false;
// Check we are in the right order - open before close
if (OpenSquareBraceIndex >= 0 && CloseSquareBraceIndex < OpenSquareBraceIndex)
return false;
if (OpenCurlyBraceIndex >= 0 && CloseCurlyBraceIndex < OpenCurlyBraceIndex)
return false;
// Check we are in the right order - square before curly
if (OpenSquareBraceIndex >= 0 && OpenCurlyBraceIndex >= 0 &&
OpenCurlyBraceIndex < OpenSquareBraceIndex)
return false;
if (OpenSquareBraceIndex == -1 && OpenCurlyBraceIndex == -1) {
// title
newTitle = cs_leafText;
return true;
}
if (OpenSquareBraceIndex >= 0 && OpenCurlyBraceIndex == -1) {
// title [user]
newTitle = cs_leafText.Left(OpenSquareBraceIndex);
newTitle.Trim();
newUser = cs_leafText.Mid(OpenSquareBraceIndex + 1,
CloseSquareBraceIndex - OpenSquareBraceIndex - 1);
newUser.Trim();
goto final_check;
}
if (OpenSquareBraceIndex == -1 && OpenCurlyBraceIndex >= 0) {
// title {password}
newTitle = cs_leafText.Left(OpenCurlyBraceIndex);
newTitle.Trim();
newPassword = cs_leafText.Mid(OpenCurlyBraceIndex + 1,
CloseCurlyBraceIndex - OpenCurlyBraceIndex - 1);
newPassword.Trim();
bPasswordSet = true;
goto final_check;
}
if (OpenSquareBraceIndex >= 0 && OpenCurlyBraceIndex >= 0) {
// title [user] {password}
newTitle = cs_leafText.Left(OpenSquareBraceIndex);
newTitle.Trim();
newUser = cs_leafText.Mid(OpenSquareBraceIndex + 1,
CloseSquareBraceIndex - OpenSquareBraceIndex - 1);
newUser.Trim();
newPassword = cs_leafText.Mid(OpenCurlyBraceIndex + 1,
CloseCurlyBraceIndex - OpenCurlyBraceIndex - 1);
newPassword.Trim();
bPasswordSet = true;
goto final_check;
}
return false; // Should never get here!
final_check:
bool bRC(true);
if (newTitle.IsEmpty())
bRC = false;
if (bPasswordSet && newPassword.IsEmpty())
bRC = false;
return bRC;
}
void CMyTreeCtrl::OnEndLabelEdit(LPNMHDR pnmhdr, LRESULT *pLResult)
{
if (((DboxMain *)GetParent())->IsMcoreReadOnly())
return; // don't drag in read-only mode
// Initial verification performed in OnBeginLabelEdit - so some events may not get here!
// Only items visible will be changed - e.g. if password is not shown and the user
// puts in the new dispay text, it will be ignored.
/* Allowed formats:
1. title
If preference ShowUsernameInTree is set:
2. title [username]
If preferences ShowUsernameInTree and ShowPasswordInTree are set:
3. title [username] {password}
There can only be one of each:
open square brace
close square brace
open curly brace
close curly brace
If pos_xtb = position of x = open/close, t = square/curly brackes, then
pos_osb < pos_csb < pos_ocb < pos_ccb
Title and Password are mandatory fields within the PWS database and so, if specified,
these fields cannot be empty.
*/
NMTVDISPINFO *ptvinfo = (NMTVDISPINFO *)pnmhdr;
HTREEITEM ti = ptvinfo->item.hItem;
if (ptvinfo->item.pszText != NULL && // NULL if edit cancelled,
ptvinfo->item.pszText[0] != TCHAR('\0')) { // empty if text deleted - not allowed
ptvinfo->item.mask = TVIF_TEXT;
SetItem(&ptvinfo->item);
if (IsLeafNode(ptvinfo->item.hItem)) {
DWORD itemData = GetItemData(ti);
ASSERT(itemData != NULL);
CItemData *ci = (CItemData *)itemData;
CString group, newTitle, newUser, newPassword;
if (!splitLeafText(ptvinfo->item.pszText, newTitle, newUser, newPassword)) {
// errors in user's input - restore text and refresh display
goto bad_exit;
}
group = CString(ci->GetGroup());
if (((DboxMain *)m_parent)->Find(group, newTitle, newUser) != NULL) {
CMyString temp;
if (group.IsEmpty())
temp.Format(IDS_ENTRYEXISTS2, newTitle, newUser);
else
temp.Format(IDS_ENTRYEXISTS, group, newTitle, newUser);
AfxMessageBox(temp);
goto bad_exit;
}
if (newUser.IsEmpty())
newUser = CString(ci->GetUser());
if (newPassword.IsEmpty())
newPassword = CString(ci->GetPassword());
PWSprefs *prefs = PWSprefs::GetInstance();
CString treeDispString;
bool bShowUsernameInTree = prefs->GetPref(PWSprefs::ShowUsernameInTree);
bool bShowPasswordInTree = prefs->GetPref(PWSprefs::ShowPasswordInTree);
treeDispString = newTitle;
if (bShowUsernameInTree) {
treeDispString += _T(" [");
treeDispString += newUser;
treeDispString += _T("]");
if (bShowPasswordInTree) {
treeDispString += _T(" {");
treeDispString += newPassword;
treeDispString += _T("}");
}
}
// Update corresponding Tree mode text with the new display text (ie: in case
// the username was removed and had to be normalized back in). Note that
// we cannot do "SetItemText(ti, treeDispString)" here since Windows will
// automatically overwrite and update the item text with the contents from
// the "ptvinfo->item.pszText" buffer.
PWSUtil::strCopy(ptvinfo->item.pszText, ptvinfo->item.cchTextMax,
treeDispString, ptvinfo->item.cchTextMax);
ptvinfo->item.pszText[ptvinfo->item.cchTextMax - 1] = TCHAR('\0');
// update corresponding List mode text
DisplayInfo *di = (DisplayInfo *)ci->GetDisplayInfo();
ASSERT(di != NULL);
int lindex = di->list_index;
// update the password database record - but only those items visible!!!
ci->SetTitle(newTitle);
((DboxMain *)m_parent)->UpdateListItemTitle(lindex, newTitle);
if (bShowUsernameInTree) {
ci->SetUser(newUser);
((DboxMain *)m_parent)->UpdateListItemUser(lindex, newUser);
if (bShowPasswordInTree) {
ci->SetPassword(newPassword);
((DboxMain *)m_parent)->UpdateListItemPassword(lindex, newPassword);
}
}
} else {
// Update all leaf children with new path element
// prefix is path up to and NOT including renamed node
CString prefix;
HTREEITEM parent, current = ti;
do {
parent = GetParentItem(current);
if (parent == NULL) {
break;
}
current = parent;
if (!prefix.IsEmpty())
prefix = GROUP_SEP + prefix;
prefix = GetItemText(current) + prefix;
} while (1);
UpdateLeafsGroup(ti, prefix);
}
// Mark database as modified
((DboxMain *)m_parent)->SetChanged(DboxMain::Data);
// Sort it as appropriate
if (((DboxMain *)GetParent())->IsExplorerTree())
((DboxMain *)GetParent())->SortTree(ti);
else
SortChildren(GetParentItem(ti));
// OK
*pLResult = TRUE;
return;
}
bad_exit:
// Refresh display to show old text - if we don't no one else will
((DboxMain *)m_parent)->RefreshList();
// restore text
// (not that this is documented anywhere in MS's docs...)
*pLResult = FALSE;
}
void CMyTreeCtrl::OnMouseMove(UINT nFlags, CPoint point)
{
if (m_nHoverTimerID) {
KillTimer( m_nHoverTimerID );
m_nHoverTimerID = 0;
}
if (m_bDragging) {
UINT flags;
ASSERT(m_pimagelist != NULL);
m_pimagelist->DragMove(point);
m_nHoverTimerID = SetTimer(2, 750, NULL);
m_HoverPoint = point;
HTREEITEM hitem = HitTest(point, &flags);
if (hitem != NULL) {
m_pimagelist->DragLeave(this);
SelectDropTarget(hitem);
m_hitemDrop = hitem;
m_pimagelist->DragEnter(this, point);
}
m_pimagelist->DragShowNolock(TRUE);
}
CTreeCtrl::OnMouseMove(nFlags, point);
}
bool CMyTreeCtrl::IsChildNodeOf(HTREEITEM hitemChild, HTREEITEM hitemSuspectedParent)
{
do {
if (hitemChild == hitemSuspectedParent)
break;
} while ((hitemChild = GetParentItem(hitemChild)) != NULL);
return (hitemChild != NULL);
}
bool CMyTreeCtrl::IsLeafNode(HTREEITEM hItem)
{
// ItemHasChildren() won't work in the general case
BOOL status;
int i, dummy;
status = GetItemImage(hItem, i, dummy);
ASSERT(status);
return (i != NODE);
}
void CMyTreeCtrl::DeleteWithParents(HTREEITEM hItem)
{
// We don't want nodes that have no children to remain
HTREEITEM p;
do {
p = GetParentItem(hItem);
DeleteItem(hItem);
if (ItemHasChildren(p))
break;
hItem = p;
} while (p != TVI_ROOT && p != NULL);
}
void CMyTreeCtrl::DeleteFromSet(HTREEITEM hItem)
{
SetTreeItemP_t pSet = SetTreeItemP_t(m_expandedItems);
DWORD itemData = GetItemData(hItem);
ASSERT(itemData != NULL);
CItemData *ci = (CItemData *)itemData;
pSet->erase(ci);
}
// Return the full path leading up to a given item, but
// not including the name of the item itself.
CString CMyTreeCtrl::GetGroup(HTREEITEM hItem)
{
CString retval;
CString nodeText;
while (hItem != NULL) {
nodeText = GetItemText(hItem);
if (!retval.IsEmpty())
nodeText += GROUP_SEP;
retval = nodeText + retval;
hItem = GetParentItem(hItem);
}
return retval;
}
static CMyString GetPathElem(CMyString &path)
{
// Get first path element and chop it off, i.e., if
// path = "a.b.c.d"
// will return "a" and path will be "b.c.d"
// (assuming GROUP_SEP is '.')
CMyString retval;
int N = path.Find(GROUP_SEP);
if (N == -1) {
retval = path;
path = _T("");
} else {
const int Len = path.GetLength();
retval = CMyString(path.Left(N));
path = CMyString(path.Right(Len - N - 1));
}
return retval;
}
static bool ExistsInTree(CTreeCtrl &Tree, HTREEITEM node,
const CMyString &s, HTREEITEM &si)
{
// returns true iff s is a direct descendant of node
HTREEITEM ti = Tree.GetChildItem(node);
while (ti != NULL) {
const CMyString itemText = Tree.GetItemText(ti);
if (itemText == s) {
si = ti;
return true;
}
ti = Tree.GetNextItem(ti, TVGN_NEXT);
}
return false;
}
HTREEITEM CMyTreeCtrl::AddGroup(const CString &group)
{
// Add a group at the end of path
HTREEITEM ti = TVI_ROOT;
HTREEITEM si;
if (!group.IsEmpty()) {
CMyString path = group;
CMyString s;
do {
s = GetPathElem(path);
if (!ExistsInTree(*this, ti, s, si)) {
ti = InsertItem(s, ti, TVI_SORT);
SetItemImage(ti, CMyTreeCtrl::NODE, CMyTreeCtrl::NODE);
} else
ti = si;
} while (!path.IsEmpty());
}
return ti;
}
bool CMyTreeCtrl::TransferItem(HTREEITEM hitemDrag, HTREEITEM hitemDrop)
{
TV_INSERTSTRUCT tvstruct;
TCHAR sztBuffer[260]; // max visible
HTREEITEM hNewItem, hFirstChild;
DWORD itemData = GetItemData(hitemDrag);
// avoid an infinite recursion
tvstruct.item.hItem = hitemDrag;
tvstruct.item.cchTextMax = sizeof(sztBuffer)/sizeof(TCHAR) - 1;
tvstruct.item.pszText = sztBuffer;
tvstruct.item.mask = (TVIF_CHILDREN | TVIF_HANDLE | TVIF_IMAGE
| TVIF_SELECTEDIMAGE | TVIF_TEXT);
GetItem(&tvstruct.item); // get information of the dragged element
tvstruct.hParent = hitemDrop;
if (((DboxMain *)GetParent())->IsExplorerTree())
tvstruct.hInsertAfter = TVI_LAST;
else
tvstruct.hInsertAfter = TVI_SORT;
tvstruct.item.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_TEXT;
hNewItem = InsertItem(&tvstruct);
if (itemData != 0) { // Non-NULL itemData implies Leaf
CItemData *ci = (CItemData *)itemData;
// Update Group
CMyString path, elem;
HTREEITEM p, q = hNewItem;
do {
p = GetParentItem(q);
if (p != NULL) {
elem = CMyString(GetItemText(p));
if (!path.IsEmpty())
elem += GROUP_SEP;
path = elem + path;
q = p;
} else
break;
} while (1);
// Get information from current selected entry
CMyString ci_user = ci->GetUser();
CMyString ci_title0 = ci->GetTitle();
CMyString ci_title = ci_title0;
if (((DboxMain *)m_parent)->Find(path, ci_title0, ci_user) != NULL) {
// Find a unique "Title"
POSITION listpos = NULL;
int i = 0;
CString s_copy;
do {
i++;
s_copy.Format(IDS_DRAGNUMBER, i);
ci_title = ci_title0 + CMyString(s_copy);
listpos = ((DboxMain *)m_parent)->Find(path, ci_title, ci_user);
} while (listpos != NULL);
}
ci->SetGroup(path);
DisplayInfo *di = (DisplayInfo *)ci->GetDisplayInfo();
ASSERT(di != NULL);
if (ci_title.Compare(ci_title0) != 0) {
ci->SetTitle(ci_title);
CMyString treeDispString;
treeDispString = ci_title;
if (PWSprefs::GetInstance()->GetPref(PWSprefs::ShowUsernameInTree)) {
treeDispString += _T(" [");
treeDispString += ci_user;
treeDispString += _T("]");
if (PWSprefs::GetInstance()->GetPref(PWSprefs::ShowPasswordInTree)) {
CMyString ci_Password = ci->GetPassword();
treeDispString += _T(" {");
treeDispString += ci_Password;
treeDispString += _T("}");
}
}
// Update tree label
SetItemText(hNewItem, treeDispString);
// Update list field
((DboxMain *)GetParent())->UpdateListItemTitle(di->list_index, (CString)ci_title);
}
// Mark database as modified!
((DboxMain *)GetParent())->SetChanged(DboxMain::Data);
// Update DisplayInfo record associated with ItemData
di->tree_item = hNewItem;
}
SetItemData(hNewItem, itemData);
if (((DboxMain *)GetParent())->IsExplorerTree())
((DboxMain *)GetParent())->SortTree(hitemDrop);
else
SortChildren(hitemDrop);
while ((hFirstChild = GetChildItem(hitemDrag)) != NULL) {
TransferItem(hFirstChild, hNewItem); // recursively transfer all the items
DeleteItem(hFirstChild);
}
return true;
}
void CMyTreeCtrl::EndDragging(BOOL bCancel)
{
if (m_bDragging) {
ASSERT(m_pimagelist != NULL);
m_pimagelist->DragLeave(this);
m_pimagelist->EndDrag();
delete m_pimagelist;
m_pimagelist = NULL;
HTREEITEM parent = GetParentItem(m_hitemDrag);
if (IsLeafNode(m_hitemDrop))
m_hitemDrop = GetParentItem(m_hitemDrop);
if (!bCancel &&
m_hitemDrag != m_hitemDrop &&
!IsChildNodeOf(m_hitemDrop, m_hitemDrag) &&
parent != m_hitemDrop)
{
// drag operation completed successfully.
TransferItem(m_hitemDrag, m_hitemDrop);
DeleteItem(m_hitemDrag);
while (parent != NULL && !ItemHasChildren(parent)) {
HTREEITEM grandParent = GetParentItem(parent);
DeleteItem(parent);
parent = grandParent;
}
SelectItem(m_hitemDrop);
} else {
// drag failed or cancelled, revert to last selected
SelectItem(m_hitemDrag);
}
ReleaseCapture();
m_bDragging = FALSE;
SelectDropTarget(NULL);
}
KillTimer(m_nTimerID);
KillTimer(m_nHoverTimerID);
}
void CMyTreeCtrl::OnLButtonUp(UINT nFlags, CPoint point)
{
if (m_bDragging) {
// Dragging operation should be ended, but first check to see
// if the mouse is outside the window to decide if the
// drag operation should be aborted.
CRect clientRect;
GetClientRect(&clientRect);
if (clientRect.PtInRect(point))
EndDragging(FALSE);
else
EndDragging(TRUE);
}
CTreeCtrl::OnLButtonUp(nFlags, point);
}
void CMyTreeCtrl::OnBeginDrag(NMHDR* pNMHDR, LRESULT* pResult)
{
CPoint ptAction;
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
*pResult = 0;
if (((DboxMain *)GetParent())->IsMcoreReadOnly())
return; // don't drag in read-only mode
GetCursorPos(&ptAction);
ScreenToClient(&ptAction);
ASSERT(!m_bDragging);
m_bDragging = TRUE;
m_hitemDrag = pNMTreeView->itemNew.hItem;
m_hitemDrop = NULL;
SelectItem(m_hitemDrag);
ASSERT(m_pimagelist == NULL);
m_pimagelist = CreateDragImage(m_hitemDrag);
m_pimagelist->DragShowNolock(TRUE);
m_pimagelist->SetDragCursorImage(0, CPoint(0, 0));
m_pimagelist->BeginDrag(0, CPoint(0,0));
m_pimagelist->DragMove(ptAction);
m_pimagelist->DragEnter(this, ptAction);
SetCapture();
// Set up the timer
m_nTimerID = SetTimer(1, 75, NULL);
}
void CMyTreeCtrl::OnExpandCollapse(NMHDR *pNotifyStruct, LRESULT *)
{
// The hItem that is expanded isn't the one that will be restored,
// since the tree is rebuilt in DboxMain::RefreshList. Therefore, we
// need to store the corresponding elements. But groups have none, so
// we store the first (or any) child element, and upon restore, expand
// the parent. Ugh++.
if (!m_isRestoring) {
SetTreeItemP_t pSet = SetTreeItemP_t(m_expandedItems);
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNotifyStruct;
// now find a leaf son and add it's CitemData to set.
HTREEITEM child = GetChildItem(pNMTreeView->itemNew.hItem);
ASSERT(child != NULL); // can't expand something w/o children, right?
do {
if (IsLeafNode(child)) {
break; // stop at first leaf child found
}
child = GetNextSiblingItem(child);
} while (child != NULL);
if (child == NULL) {
// case where expanded node has only tree subnodes,
// nothing to get a CItemData from. This borderline
// case is hereby deemed more trouble than it's worth to
// handle correctly.
return;
}
DWORD itemData = GetItemData(child);
ASSERT(itemData != NULL);
CItemData *ci = (CItemData *)itemData;
if (pNMTreeView->action == TVE_EXPAND)
pSet->insert(ci);
else if (pNMTreeView->action == TVE_COLLAPSE) {
ASSERT(pSet->find(ci) != pSet->end());
pSet->erase(ci);
}
}
}
void CMyTreeCtrl::RestoreExpanded()
{
m_isRestoring = true;
SetTreeItemP_t pSet = SetTreeItemP_t(m_expandedItems);
SetTreeItem_t::iterator it;
for (it = pSet->begin(); it != pSet->end(); it++) {
CItemData *ci = *it;
DisplayInfo *di = (DisplayInfo *)ci->GetDisplayInfo();
HTREEITEM parent = GetParentItem(di->tree_item);
Expand(parent, TVE_EXPAND);
}
m_isRestoring = false;
}
void CMyTreeCtrl::ClearExpanded()
{
SetTreeItemP_t pSet = SetTreeItemP_t(m_expandedItems);
pSet->clear();
}
void CMyTreeCtrl::OnExpandAll()
{
// Updated to test for zero entries!
HTREEITEM hItem = this->GetRootItem();
if (hItem == NULL)
return;
SetRedraw(FALSE);
do {
this->Expand(hItem,TVE_EXPAND);
hItem = this->GetNextItem(hItem,TVGN_NEXTVISIBLE);
}
while (hItem);
this->EnsureVisible(this->GetSelectedItem());
SetRedraw();
}
void CMyTreeCtrl::OnCollapseAll()
{
// Courtesy of Zafir Anjum from www.codeguru.com
// Updated to test for zero entries!
HTREEITEM hItem = this->GetRootItem();
if (hItem == NULL)
return;
SetRedraw(FALSE);
do {
CollapseBranch(hItem);
}
while((hItem = this->GetNextSiblingItem(hItem)) != NULL);
SetRedraw();
}
void CMyTreeCtrl::CollapseBranch(HTREEITEM hItem)
{
// Courtesy of Zafir Anjumfrom www.codeguru.com
if(this->ItemHasChildren(hItem)) {
this->Expand(hItem, TVE_COLLAPSE);
hItem = this->GetChildItem(hItem);
do {
CollapseBranch(hItem);
}
while((hItem = this->GetNextSiblingItem(hItem)) != NULL);
}
}
void CMyTreeCtrl::OnTimer(UINT nIDEvent)
{
const int SCROLL_BORDER = 10;
const int SCROLL_SPEED_ZONE_WIDTH = 20;
if (nIDEvent == m_nHoverTimerID) {
KillTimer(m_nHoverTimerID);
m_nHoverTimerID = 0;
HTREEITEM trItem = 0;
UINT uFlag = 0;
trItem = HitTest(m_HoverPoint, &uFlag);
if (trItem) {
SelectItem(trItem);
Expand(trItem,TVE_EXPAND);
}
return;
}
if (nIDEvent != m_nTimerID) {
CTreeCtrl::OnTimer(nIDEvent);
return;
}
// Doesn't matter that we didn't initialize m_timerticks
m_timerticks++;
POINT pt;
GetCursorPos(&pt);
CRect rect;
GetClientRect(&rect);
ClientToScreen(&rect);
// NOTE: Screen coordinate is being used because the call
// to DragEnter had used the Desktop window.
POINT pttemp = POINT(pt);
ClientToScreen(&pttemp);
CImageList::DragMove(pttemp);
int iMaxV = GetScrollLimit(SB_VERT);
int iPosV = GetScrollPos(SB_VERT);
HTREEITEM hitem = GetFirstVisibleItem();
// The cursor must not only be SOMEWHERE above/beneath the tree control
// BUT RIGHT above or beneath it
// i.e. the x-coordinates must be those of the control (+/- SCROLL_BORDER)
if ( pt.x < rect.left - SCROLL_BORDER )
; // Too much to the left
else if ( pt.x > rect.right + SCROLL_BORDER )
; // Too much to the right
else if( (pt.y < rect.top + SCROLL_BORDER) && iPosV ) {
// We need to scroll up
// Scroll slowly if cursor near the treeview control
int slowscroll = 6 - (rect.top + SCROLL_BORDER - pt.y) / SCROLL_SPEED_ZONE_WIDTH;
if (0 == (m_timerticks % (slowscroll > 0 ? slowscroll : 1)))
{
CImageList::DragShowNolock(FALSE);
SendMessage(WM_VSCROLL, SB_LINEUP);
SelectDropTarget(hitem);
m_hitemDrop = hitem;
CImageList::DragShowNolock(TRUE);
}
}
else if( (pt.y > rect.bottom - SCROLL_BORDER) && (iPosV!=iMaxV) )
{
// We need to scroll down
// Scroll slowly if cursor near the treeview control
int slowscroll = 6 - (pt.y - rect.bottom + SCROLL_BORDER ) / SCROLL_SPEED_ZONE_WIDTH;
if (0 == (m_timerticks % (slowscroll > 0 ? slowscroll : 1)))
{
CImageList::DragShowNolock(FALSE);
SendMessage(WM_VSCROLL, SB_LINEDOWN);
int nCount = GetVisibleCount();
for (int i=0; i<nCount-1; ++i)
hitem = GetNextVisibleItem(hitem);
if(hitem)
SelectDropTarget(hitem);
m_hitemDrop = hitem;
CImageList::DragShowNolock(TRUE);
}
}
// The cursor must be in a small zone IN the treecontrol at the left/right
int iPosH = GetScrollPos(SB_HORZ);
int iMaxH = GetScrollLimit(SB_HORZ);
if (!rect.PtInRect(pt)) return; // not in TreeCtrl
else if ((pt.x < rect.left + SCROLL_BORDER) && (iPosH != 0)) {
// We need to scroll to the left
CImageList::DragShowNolock(FALSE);
SendMessage(WM_HSCROLL, SB_LINELEFT);
CImageList::DragShowNolock(TRUE);
}
else if ((pt.x > rect.right - SCROLL_BORDER) && (iPosH != iMaxH)) {
// We need to scroll to the right
CImageList::DragShowNolock(FALSE);
SendMessage(WM_HSCROLL, SB_LINERIGHT);
CImageList::DragShowNolock(TRUE);
}
}
HTREEITEM
CMyTreeCtrl::GetNextTreeItem(HTREEITEM hItem)
{
if (NULL == hItem)
return this->GetRootItem();
// First, try to go to this item's 1st child
HTREEITEM hReturn = this->GetChildItem(hItem);
// If no more child items...
while (hItem && !hReturn) {
// Get this item's next sibling
hReturn = this->GetNextSiblingItem(hItem);
// If hReturn is NULL, then there are no
// sibling items, and we're on a leaf node.
// Backtrack up the tree one level, and
// we'll look for a sibling on the next
// iteration (or we'll reach the root and
// quit).
hItem = this->GetParentItem(hItem);
}
return hReturn;
}