Skip to content

Commit 14f3d42

Browse files
committed
Treeview - copy and delete a whole group
1 parent 2d959a3 commit 14f3d42

File tree

3 files changed

+83
-36
lines changed

3 files changed

+83
-36
lines changed

dialogs/world_prefs/GenPropertyPage.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ class CGenPropertyPage : public CPropertyPage
113113
void SortItems (void); // sort the tree/list control
114114
int GetSelectedItemCount () const; // how many items are selected?
115115
int GetItemCount () const; // how many items are there in the list/tree?
116+
int GetSelectedGroupCount () const; // count of selected items in a group in the tree view
116117
void CheckParentHasChildren (HTREEITEM hdlParent); // if this group is empty, delete it
117118

118119

dialogs/world_prefs/genpropertypage.cpp

Lines changed: 79 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ static char BASED_CODE THIS_FILE[] = __FILE__;
1515
// CGenPropertyPage property page
1616
IMPLEMENT_DYNAMIC(CGenPropertyPage, CPropertyPage)
1717

18+
/////////////////////////////////////////////////////////////////////////////
19+
// Constructor
20+
1821
CGenPropertyPage::CGenPropertyPage(const UINT nID) :
1922
CPropertyPage(nID)
2023
{
@@ -31,7 +34,10 @@ CGenPropertyPage::CGenPropertyPage(const UINT nID) :
3134
m_iColumnCount = 0;
3235
m_bWantTreeControl = false;
3336

34-
}
37+
} // end of CGenPropertyPage::CGenPropertyPage
38+
39+
/////////////////////////////////////////////////////////////////////////////
40+
// Destructor
3541

3642
CGenPropertyPage::~CGenPropertyPage()
3743
{
@@ -40,7 +46,7 @@ CGenPropertyPage::~CGenPropertyPage()
4046
delete [] m_strColumnHeadings;
4147
delete [] m_iColJust;
4248

43-
}
49+
} // end of CGenPropertyPage::~CGenPropertyPage
4450

4551
void CGenPropertyPage::DoDataExchange(CDataExchange* pDX)
4652
{
@@ -62,6 +68,9 @@ END_MESSAGE_MAP()
6268
// CGenPropertyPage message handlers
6369

6470

71+
/////////////////////////////////////////////////////////////////////////////
72+
// OnAddItem
73+
6574
void CGenPropertyPage::SetUpPage (CString strObjectType,
6675
CObjectMap * ObjectMap,
6776
CListCtrl * ctlList,
@@ -81,8 +90,10 @@ void CGenPropertyPage::SetUpPage (CString strObjectType,
8190
m_iMask = iMask;
8291
m_bWantTreeControl = bWantTreeControl;
8392

84-
}
93+
} // end of CGenPropertyPage::SetUpPage
8594

95+
/////////////////////////////////////////////////////////////////////////////
96+
// GetSelectedItemCount
8697

8798
int CGenPropertyPage::GetSelectedItemCount () const
8899
{
@@ -115,6 +126,39 @@ int CGenPropertyPage::GetSelectedItemCount () const
115126

116127
} // end of CGenPropertyPage::GetSelectedItemCount
117128

129+
/////////////////////////////////////////////////////////////////////////////
130+
// GetSelectedGroupCount
131+
132+
int CGenPropertyPage::GetSelectedGroupCount () const
133+
{
134+
135+
int iCount = 0;
136+
137+
if (m_bWantTreeControl)
138+
{
139+
140+
for (HTREEITEM hGroup = m_cTreeCtrl.GetRootItem ();
141+
hGroup;
142+
hGroup = m_cTreeCtrl.GetNextSiblingItem (hGroup))
143+
{
144+
145+
if (m_cTreeCtrl.GetItemState (hGroup, TVIS_SELECTED) & TVIS_SELECTED)
146+
{
147+
for (HTREEITEM hItem = m_cTreeCtrl.GetChildItem (hGroup);
148+
hItem;
149+
hItem = m_cTreeCtrl.GetNextSiblingItem (hItem))
150+
iCount++;
151+
return iCount;
152+
} // end of group selected
153+
} // end for each group
154+
} // end of tree control
155+
156+
return 0;
157+
}
158+
159+
/////////////////////////////////////////////////////////////////////////////
160+
// GetItemCount
161+
118162
int CGenPropertyPage::GetItemCount () const
119163
{
120164

@@ -569,27 +613,29 @@ void CGenPropertyPage::OnDeleteItem()
569613
{
570614

571615
int iCount = GetSelectedItemCount ();
616+
int iGroupCount = GetSelectedGroupCount ();
617+
572618
int nItem,
573619
i,
574620
iIncluded = 0,
575621
iExecuting = 0;
576622

577623

578-
if (iCount == 0)
624+
if ((iCount + iGroupCount) == 0)
579625
return;
580626

581627
if (App.m_bTriggerRemoveCheck)
582628
{
583629
// mucking around to make it plural properly
584630
CString sName = m_strObjectType;
585-
if (iCount > 1)
631+
if ((iCount + iGroupCount) > 1)
586632
if (sName == "alias")
587633
sName += "es";
588634
else
589635
sName += "s";
590636

591637
if (::UMessageBox (TFormat ("Delete %i %s - are you sure?",
592-
iCount, sName), MB_YESNO | MB_ICONQUESTION | MB_DEFBUTTON2)
638+
iCount + iGroupCount, sName), MB_YESNO | MB_ICONQUESTION | MB_DEFBUTTON2)
593639
!= IDYES )
594640
return;
595641
} // end of wanting to confirm
@@ -603,31 +649,28 @@ int nItem,
603649
hGroup;
604650
hGroup = m_cTreeCtrl.GetNextSiblingItem (hGroup))
605651
{
652+
bool bGroupSelected = m_cTreeCtrl.GetItemState (hGroup, TVIS_SELECTED) & TVIS_SELECTED;
653+
654+
// remember which we deleted
655+
list<HTREEITEM> deletedItems;
606656

607657
// find selected items
608658
for (HTREEITEM hItem = m_cTreeCtrl.GetChildItem (hGroup);
609659
hItem;
610660
hItem = m_cTreeCtrl.GetNextSiblingItem (hItem))
611-
{
612-
UINT iState = m_cTreeCtrl.GetItemState (hItem, TVIS_SELECTED);
613-
614-
if (iState & TVIS_SELECTED)
615-
{
616-
CString * pstrObjectName = (CString *) m_cTreeCtrl.GetItemData (hItem);
617-
618-
if (!DeleteOneItem (pstrObjectName, iIncluded, iExecuting))
619-
{
620-
HTREEITEM hdlParent = m_cTreeCtrl.GetParentItem (hItem);
621-
m_cTreeCtrl.DeleteItem (hItem);
622-
CheckParentHasChildren (hdlParent);
623-
}
661+
if (bGroupSelected || m_cTreeCtrl.GetItemState (hItem, TVIS_SELECTED) & TVIS_SELECTED)
662+
if (!DeleteOneItem ((CString *) m_cTreeCtrl.GetItemData (hItem), iIncluded, iExecuting))
663+
deletedItems.push_back (hItem);
624664

625-
} // end if selected
665+
// loop doesn't work if we delete while going through it, so do it now
666+
for (list<HTREEITEM>::const_iterator it = deletedItems.begin ();
667+
it != deletedItems.end ();
668+
++it)
669+
m_cTreeCtrl.DeleteItem (*it);
626670

627-
} // end for each item
671+
CheckParentHasChildren (hGroup);
628672

629673
} // end for each group
630-
631674
} // end of tree control
632675
else
633676
// list control ................
@@ -1296,7 +1339,7 @@ BOOL CGenPropertyPage::OnInitDialog()
12961339
wndpl.rcNormalPosition.left = wndpl.rcNormalPosition.right - 100;
12971340

12981341

1299-
m_cUseTreeViewCtrl.Create(Translate ("Tree View"),
1342+
m_cUseTreeViewCtrl.Create(Translate ("Tree Vie&w"),
13001343
BS_CHECKBOX | BS_AUTOCHECKBOX | BS_NOTIFY | WS_VISIBLE | WS_CHILD,
13011344
wndpl.rcNormalPosition,
13021345
this,
@@ -1471,22 +1514,25 @@ try
14711514
hGroup;
14721515
hGroup = m_cTreeCtrl.GetNextSiblingItem (hGroup))
14731516
{
1517+
bool bGroupSelected = m_cTreeCtrl.GetItemState (hGroup, TVIS_SELECTED) & TVIS_SELECTED;
1518+
1519+
// remember which we deleted
1520+
list<HTREEITEM> deletedItems;
14741521

14751522
for (HTREEITEM hItem = m_cTreeCtrl.GetChildItem (hGroup);
14761523
hItem;
14771524
hItem = m_cTreeCtrl.GetNextSiblingItem (hItem))
1478-
{
1479-
UINT iState = m_cTreeCtrl.GetItemState (hItem, TVIS_SELECTED);
1480-
1481-
if (iState & TVIS_SELECTED)
1482-
{
1525+
if (bGroupSelected || m_cTreeCtrl.GetItemState (hItem, TVIS_SELECTED) & TVIS_SELECTED)
14831526
if (CopyOneItem ((CString *) m_cTreeCtrl.GetItemData (hItem), ar))
1484-
{
1485-
m_cTreeCtrl.DeleteItem (hItem);
1486-
}
1487-
} // end if selected
1527+
deletedItems.push_back (hItem); // not there any more
14881528

1489-
} // end for each item
1529+
// loop doesn't work if we delete while going through it, so do it now
1530+
for (list<HTREEITEM>::const_iterator it = deletedItems.begin ();
1531+
it != deletedItems.end ();
1532+
++it)
1533+
m_cTreeCtrl.DeleteItem (*it);
1534+
1535+
CheckParentHasChildren (hGroup);
14901536

14911537
} // end for each group
14921538

dialogs/world_prefs/prefspropertypages.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3292,7 +3292,7 @@ void CPrefsP7::OnUpdateHaveDefaults(CCmdUI* pCmdUI)
32923292
void CPrefsP7::OnUpdateNeedSelection(CCmdUI* pCmdUI)
32933293
{
32943294

3295-
pCmdUI->Enable (GetSelectedItemCount () != 0 &&
3295+
pCmdUI->Enable ((GetSelectedItemCount () != 0 || GetSelectedGroupCount () != 0) &&
32963296
(m_ctlUseDefaultAliases.GetCheck () == 0 ||
32973297
App.m_strDefaultAliasesFile.IsEmpty ()));
32983298
} // end of CPrefsP7::OnUpdateNeedSelection
@@ -4452,7 +4452,7 @@ void CPrefsP8::OnUpdateHaveDefaults(CCmdUI* pCmdUI)
44524452

44534453
void CPrefsP8::OnUpdateNeedSelection(CCmdUI* pCmdUI)
44544454
{
4455-
pCmdUI->Enable (GetSelectedItemCount () != 0 &&
4455+
pCmdUI->Enable ((GetSelectedItemCount () != 0 || GetSelectedGroupCount () != 0) &&
44564456
(m_ctlUseDefaultTriggers.GetCheck () == 0 ||
44574457
App.m_strDefaultTriggersFile.IsEmpty ()));
44584458
} // end of CGenPropertyPage::OnUpdateNeedSelection
@@ -6825,7 +6825,7 @@ void CPrefsP16::OnUpdateHaveDefaults(CCmdUI* pCmdUI)
68256825

68266826
void CPrefsP16::OnUpdateNeedSelection(CCmdUI* pCmdUI)
68276827
{
6828-
pCmdUI->Enable (GetSelectedItemCount () != 0 &&
6828+
pCmdUI->Enable ((GetSelectedItemCount () != 0 || GetSelectedGroupCount () != 0) &&
68296829
(m_ctlUseDefaultTimers.GetCheck () == 0 ||
68306830
App.m_strDefaultTimersFile.IsEmpty ()));
68316831
} // end of CPrefsP16::OnUpdateNeedSelection

0 commit comments

Comments
 (0)