File: checktreectrl.cpp

package info (click to toggle)
codelite 12.0+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 95,112 kB
  • sloc: cpp: 424,040; ansic: 18,284; php: 9,569; lex: 4,186; yacc: 2,820; python: 2,294; sh: 312; makefile: 51; xml: 13
file content (226 lines) | stat: -rw-r--r-- 6,659 bytes parent folder | download | duplicates (4)
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
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright            : (C) 2008 by Eran Ifrah                            
// file name            : checktreectrl.cpp              
//                                                                          
// -------------------------------------------------------------------------
// A                                                                        
//              _____           _      _     _ _                            
//             /  __ \         | |    | |   (_) |                           
//             | /  \/ ___   __| | ___| |    _| |_ ___                      
//             | |    / _ \ / _  |/ _ \ |   | | __/ _ )                     
//             | \__/\ (_) | (_| |  __/ |___| | ||  __/                     
//              \____/\___/ \__,_|\___\_____/_|\__\___|                     
//                                                                          
//                                                  F i l e                 
//                                                                          
//    This program is free software; you can redistribute it and/or modify  
//    it under the terms of the GNU General Public License as published by  
//    the Free Software Foundation; either version 2 of the License, or     
//    (at your option) any later version.                                   
//                                                                          
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
 #include "checktreectrl.h"
#include <wx/dcmemory.h>
#include <wx/renderer.h>
#include <wx/imaglist.h>
#include <wx/settings.h>

// Images used by the checktreectrl

/* XPM */
static const char *Checkbox_off_xpm[] = {
/* columns rows colors chars-per-pixel */
"16 16 11 1",
"$ c #F7F7F7",
"% c #FFFFFF",
". c #E7E7DE",
"  c #185284",
"X c #DEDEDE",
"o c #E7E7DE",
"@ c #E7E7E7",
"O c #EFEFE7",
"+ c #EFEFEF",
"# c #F7F7EF",
"& c None",
/* pixels */
"&&&&&&&&&&&&&&&&",
"                ",
" ......XooOO+.. ",
" ....XXo@O++#.. ",
" ...XXo@O+++#.. ",
" ..Xoo@@++#$$.. ",
" ..Xo@O++#+$$.. ",
" .oo@@+++$$$$.. ",
" .o@O++#+$$%%.. ",
" .oO+++$$$$%%.. ",
" .O++#$#$$%%%.. ",
" .O++$$$$%%%%.. ",
" .+#$#$$%%%%%.. ",
" .............. ",
"                ",
"&&&&&&&&&&&&&&&&"
};


/* XPM */
static const char *Checkbox_on_xpm[] = {
/* columns rows colors chars-per-pixel */
"16 16 10 1",
"# c #F7F7F7",
"$ c #FFFFFF",
"@ c #21A521",
"  c #185284",
". c #E7E7DE",
"X c #E7E7E7",
"o c #EFEFE7",
"O c #EFEFEF",
"+ c #F7F7EF",
"& c None",
/* pixels */
"&&&&&&&&&&&&&&&&",
"                ",
" .......XooO+.. ",
" .....XXooO+O.. ",
" ....XXXOO@+#.. ",
" ...XXXoO@@##.. ",
" ...@XOO@@@##.. ",
" ..X@@O@@@##$.. ",
" .Xo@@@@@##$#.. ",
" .ooO@@@##$$$.. ",
" .oO+O@##$#$$.. ",
" .O+O###$$$$$.. ",
" .++###$$#$$$.. ",
" .............. ",
"                ",
"&&&&&&&&&&&&&&&&"
};


//-----------------------------------------------------------
// wxCheckTreeCtrl
//-----------------------------------------------------------

DEFINE_EVENT_TYPE(wxEVT_CKTR_ITEM_SELECTED)
DEFINE_EVENT_TYPE(wxEVT_CKTR_ITEM_UNSELECTED)

BEGIN_EVENT_TABLE(wxCheckTreeCtrl, wxTreeCtrl)
EVT_LEFT_DOWN(wxCheckTreeCtrl::OnLeftDown)
EVT_LEFT_DCLICK(wxCheckTreeCtrl::OnLeftDown)
END_EVENT_TABLE()

wxCheckTreeCtrl::wxCheckTreeCtrl(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style, const wxValidator& validator, const wxString& name)
: m_checkedBmp(16, 16)
, m_uncheckedBmp(16, 16)
{
	Create(parent, id, pos, size, style | wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT, validator, name);
	//create images
	m_checkedBmp = wxBitmap(Checkbox_on_xpm);
	m_uncheckedBmp = wxBitmap(Checkbox_off_xpm);

	//create an image list and assign it
	wxImageList *il = new wxImageList(16, 16, true);
	il->Add(m_checkedBmp);		//0
	il->Add(m_uncheckedBmp);	//1

	//will be owned by the control
	AssignImageList(il);
}

wxCheckTreeCtrl::~wxCheckTreeCtrl()
{
}

wxTreeItemId wxCheckTreeCtrl::AddRoot(const wxString& text, bool checked, wxTreeItemData* data)
{
	return wxTreeCtrl::AddRoot(text, checked ? 0 : 1, checked ? 0 : 1, data);
}

wxTreeItemId wxCheckTreeCtrl::AppendItem(const wxTreeItemId& parent, const wxString& text, bool checked, wxTreeItemData* data)
{
	return wxTreeCtrl::AppendItem(parent, text, checked ? 0 : 1, checked ? 0 : 1, data);
}

void wxCheckTreeCtrl::OnLeftDown(wxMouseEvent &event)
{
	int flags;
	wxTreeItemId item = wxTreeCtrl::HitTest(event.GetPosition(), flags);
	if(item.IsOk() && flags & wxTREE_HITTEST_ONITEMICON){
		if(IsChecked(item)){
			//fire unselect event
			wxCheckTreeCtrlEvent e(wxEVT_CKTR_ITEM_UNSELECTED, GetId());
			e.SetItem(item);
			e.SetEventObject(this);
			GetEventHandler()->ProcessEvent(e);
		
			//Veto?
			if(!e.IsAllowed()){
				return;
			}
			Check(item, false);

		}else{
			//fire select event
			wxCheckTreeCtrlEvent e(wxEVT_CKTR_ITEM_SELECTED, GetId());
			e.SetItem(item);
			e.SetEventObject(this);
			GetEventHandler()->ProcessEvent(e);
		
			//Veto?
			if(!e.IsAllowed()){
				return;
			}
			Check(item, true);
		}
		return;
	}
	event.Skip();
}

bool wxCheckTreeCtrl::IsChecked(const wxTreeItemId &item) const
{
	int imgId = GetItemImage(item);
	return imgId == 0;
}

void wxCheckTreeCtrl::Check(const wxTreeItemId &item, bool check)
{
	if(check){
		SetItemImage(item, 0);
		SetItemImage(item, 0, wxTreeItemIcon_Selected);
	}else{
		SetItemImage(item, 1);
		SetItemImage(item, 1, wxTreeItemIcon_Selected);
	}
}

void wxCheckTreeCtrl::GetItemChildrenRecursive(const wxTreeItemId &parent, std::list<wxTreeItemId> &children)
{
	//delete the item's children
	wxTreeItemIdValue cookie;
	children.push_back(parent);
	wxTreeItemId child = GetFirstChild(parent, cookie);
	while(child.IsOk())
	{
		if(ItemHasChildren(child)){
			GetItemChildrenRecursive(child, children);
		}else{
			children.push_back(child);
		}
		child = GetNextChild(parent, cookie);
	}
}

void wxCheckTreeCtrl::RecursiveCheck(const wxTreeItemId &item, bool check)
{
	std::list<wxTreeItemId> children;
	GetItemChildrenRecursive(item, children);

	std::list<wxTreeItemId>::iterator iter = children.begin();
	for(; iter != children.end(); iter++)
	{
		Check((*iter), check);
	}
}