]>
Commit | Line | Data |
---|---|---|
457814b5 JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: treetest.cpp | |
3 | // Purpose: wxTreeCtrl sample | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
3a5a2f56 | 9 | // Licence: wxWindows license |
457814b5 JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifdef __GNUG__ | |
3a5a2f56 VZ |
13 | #pragma implementation |
14 | #pragma interface | |
457814b5 JS |
15 | #endif |
16 | ||
17 | // For compilers that support precompilation, includes "wx/wx.h". | |
18 | #include "wx/wxprec.h" | |
19 | ||
20 | #ifdef __BORLANDC__ | |
3a5a2f56 | 21 | #pragma hdrstop |
457814b5 JS |
22 | #endif |
23 | ||
24 | #ifndef WX_PRECOMP | |
3a5a2f56 | 25 | #include "wx/wx.h" |
457814b5 JS |
26 | #endif |
27 | ||
3a5a2f56 VZ |
28 | #include "wx/log.h" |
29 | ||
91b8de8d | 30 | #include "wx/image.h" |
3a5a2f56 | 31 | #include "wx/imaglist.h" |
457814b5 JS |
32 | #include "wx/treectrl.h" |
33 | ||
cf724bce RR |
34 | #include "math.h" |
35 | ||
74b31181 | 36 | #ifdef __WXMSW__ |
8dc99046 VZ |
37 | // comment out this line to test multiple selection even under MSW (where |
38 | // it looks ugly - but works) | |
89124b1a | 39 | #define NO_MULTIPLE_SELECTION |
74b31181 | 40 | #endif |
91b8de8d | 41 | |
8dc99046 VZ |
42 | #define NO_VARIABLE_HEIGHT |
43 | ||
9dfbf520 VZ |
44 | #include "treetest.h" |
45 | ||
91b8de8d RR |
46 | // under Windows the icons are in the .rc file |
47 | #ifndef __WXMSW__ | |
91b8de8d | 48 | #include "icon1.xpm" |
91b8de8d | 49 | #include "icon2.xpm" |
74b31181 VZ |
50 | #include "icon3.xpm" |
51 | #include "icon4.xpm" | |
8dc99046 | 52 | #include "icon5.xpm" |
91b8de8d RR |
53 | #include "mondrian.xpm" |
54 | #endif | |
55 | ||
e1ee62bd VZ |
56 | // verify that the item is ok and insult the user if it is not |
57 | #define CHECK_ITEM( item ) if ( !item.IsOk() ) { \ | |
58 | wxMessageBox("Please select some item first!", \ | |
59 | "Tree sample error", \ | |
60 | wxOK | wxICON_EXCLAMATION, \ | |
61 | this); \ | |
62 | return; \ | |
5f939e78 VZ |
63 | } |
64 | ||
457814b5 | 65 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) |
5f939e78 VZ |
66 | EVT_MENU(TreeTest_Quit, MyFrame::OnQuit) |
67 | EVT_MENU(TreeTest_About, MyFrame::OnAbout) | |
68 | EVT_MENU(TreeTest_Dump, MyFrame::OnDump) | |
9dfbf520 VZ |
69 | #ifndef NO_MULTIPLE_SELECTION |
70 | EVT_MENU(TreeTest_DumpSelected, MyFrame::OnDumpSelected) | |
71 | EVT_MENU(TreeTest_Select, MyFrame::OnSelect) | |
72 | EVT_MENU(TreeTest_Unselect, MyFrame::OnUnselect) | |
73 | #endif // NO_MULTIPLE_SELECTION | |
5f939e78 | 74 | EVT_MENU(TreeTest_Rename, MyFrame::OnRename) |
5fd11f09 RR |
75 | EVT_MENU(TreeTest_Count, MyFrame::OnCount) |
76 | EVT_MENU(TreeTest_CountRec, MyFrame::OnCountRec) | |
5f939e78 VZ |
77 | EVT_MENU(TreeTest_Sort, MyFrame::OnSort) |
78 | EVT_MENU(TreeTest_SortRev, MyFrame::OnSortRev) | |
79 | EVT_MENU(TreeTest_Bold, MyFrame::OnSetBold) | |
80 | EVT_MENU(TreeTest_UnBold, MyFrame::OnClearBold) | |
81 | EVT_MENU(TreeTest_Delete, MyFrame::OnDelete) | |
82 | EVT_MENU(TreeTest_DeleteChildren, MyFrame::OnDeleteChildren) | |
83 | EVT_MENU(TreeTest_DeleteAll, MyFrame::OnDeleteAll) | |
84 | EVT_MENU(TreeTest_Recreate, MyFrame::OnRecreate) | |
85 | EVT_MENU(TreeTest_CollapseAndReset, MyFrame::OnCollapseAndReset) | |
86 | EVT_MENU(TreeTest_EnsureVisible, MyFrame::OnEnsureVisible) | |
87 | EVT_MENU(TreeTest_AddItem, MyFrame::OnAddItem) | |
cf724bce RR |
88 | EVT_MENU(TreeTest_IncIndent, MyFrame::OnIncIndent) |
89 | EVT_MENU(TreeTest_DecIndent, MyFrame::OnDecIndent) | |
90 | EVT_MENU(TreeTest_IncSpacing, MyFrame::OnIncSpacing) | |
91 | EVT_MENU(TreeTest_DecSpacing, MyFrame::OnDecSpacing) | |
9dfbf520 | 92 | EVT_MENU(TreeTest_ToggleIcon, MyFrame::OnToggleIcon) |
457814b5 JS |
93 | END_EVENT_TABLE() |
94 | ||
95 | BEGIN_EVENT_TABLE(MyTreeCtrl, wxTreeCtrl) | |
5f939e78 VZ |
96 | EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginDrag) |
97 | EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginRDrag) | |
98 | EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnBeginLabelEdit) | |
99 | EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnEndLabelEdit) | |
100 | EVT_TREE_DELETE_ITEM(TreeTest_Ctrl, MyTreeCtrl::OnDeleteItem) | |
74b31181 | 101 | #if 0 // there are so many of those that logging them causes flicker |
5f939e78 | 102 | EVT_TREE_GET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnGetInfo) |
74b31181 | 103 | #endif |
5f939e78 VZ |
104 | EVT_TREE_SET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnSetInfo) |
105 | EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanded) | |
106 | EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanding) | |
107 | EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsed) | |
108 | EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsing) | |
109 | EVT_TREE_SEL_CHANGED(TreeTest_Ctrl, MyTreeCtrl::OnSelChanged) | |
110 | EVT_TREE_SEL_CHANGING(TreeTest_Ctrl, MyTreeCtrl::OnSelChanging) | |
111 | EVT_TREE_KEY_DOWN(TreeTest_Ctrl, MyTreeCtrl::OnTreeKeyDown) | |
112 | EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl, MyTreeCtrl::OnItemActivated) | |
113 | EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick) | |
457814b5 JS |
114 | END_EVENT_TABLE() |
115 | ||
116 | IMPLEMENT_APP(MyApp) | |
117 | ||
c89a6106 | 118 | bool MyApp::OnInit() |
457814b5 | 119 | { |
5f939e78 VZ |
120 | // Create the main frame window |
121 | MyFrame *frame = new MyFrame("wxTreeCtrl Test", 50, 50, 450, 340); | |
457814b5 | 122 | |
5f939e78 VZ |
123 | // Show the frame |
124 | frame->Show(TRUE); | |
125 | SetTopWindow(frame); | |
457814b5 | 126 | |
5f939e78 | 127 | return TRUE; |
3a5a2f56 | 128 | } |
457814b5 | 129 | |
457814b5 | 130 | |
3a5a2f56 VZ |
131 | // My frame constructor |
132 | MyFrame::MyFrame(const wxString& title, int x, int y, int w, int h) | |
133 | : wxFrame((wxFrame *)NULL, -1, title, wxPoint(x, y), wxSize(w, h)) | |
134 | { | |
5f939e78 VZ |
135 | // This reduces flicker effects - even better would be to define |
136 | // OnEraseBackground to do nothing. When the tree control's scrollbars are | |
137 | // show or hidden, the frame is sent a background erase event. | |
138 | SetBackgroundColour(wxColour(255, 255, 255)); | |
139 | ||
140 | // Give it an icon | |
141 | SetIcon(wxICON(mondrian)); | |
142 | ||
143 | // Make a menubar | |
144 | wxMenu *file_menu = new wxMenu, | |
145 | *tree_menu = new wxMenu, | |
146 | *item_menu = new wxMenu; | |
147 | ||
148 | file_menu->Append(TreeTest_About, "&About..."); | |
149 | file_menu->AppendSeparator(); | |
150 | file_menu->Append(TreeTest_Quit, "E&xit"); | |
151 | ||
152 | tree_menu->Append(TreeTest_Recreate, "&Recreate the tree"); | |
153 | tree_menu->Append(TreeTest_CollapseAndReset, "C&ollapse and reset"); | |
154 | tree_menu->AppendSeparator(); | |
155 | tree_menu->Append(TreeTest_AddItem, "Append a &new item"); | |
156 | tree_menu->Append(TreeTest_Delete, "&Delete this item"); | |
157 | tree_menu->Append(TreeTest_DeleteChildren, "Delete &children"); | |
158 | tree_menu->Append(TreeTest_DeleteAll, "Delete &all items"); | |
159 | tree_menu->AppendSeparator(); | |
5fd11f09 RR |
160 | tree_menu->Append(TreeTest_Count, "Count children of current item"); |
161 | tree_menu->Append(TreeTest_CountRec, "Recursively count children of current item"); | |
162 | tree_menu->AppendSeparator(); | |
5f939e78 VZ |
163 | tree_menu->Append(TreeTest_Sort, "Sort children of current item"); |
164 | tree_menu->Append(TreeTest_SortRev, "Sort in reversed order"); | |
165 | tree_menu->AppendSeparator(); | |
166 | tree_menu->Append(TreeTest_EnsureVisible, "Make the last item &visible"); | |
cf724bce RR |
167 | tree_menu->AppendSeparator(); |
168 | tree_menu->Append(TreeTest_IncIndent, "Add 5 points to indentation\tAlt-I"); | |
169 | tree_menu->Append(TreeTest_DecIndent, "Reduce indentation by 5 points\tAlt-R"); | |
170 | tree_menu->AppendSeparator(); | |
171 | tree_menu->Append(TreeTest_IncSpacing, "Add 5 points to spacing\tCtrl-I"); | |
172 | tree_menu->Append(TreeTest_DecSpacing, "Reduce spacing by 5 points\tCtrl-R"); | |
5f939e78 | 173 | |
91b8de8d | 174 | item_menu->Append(TreeTest_Dump, "&Dump item children"); |
5f939e78 | 175 | item_menu->Append(TreeTest_Rename, "&Rename item..."); |
91b8de8d | 176 | |
5f939e78 VZ |
177 | item_menu->AppendSeparator(); |
178 | item_menu->Append(TreeTest_Bold, "Make item &bold"); | |
179 | item_menu->Append(TreeTest_UnBold, "Make item ¬ bold"); | |
9dfbf520 VZ |
180 | item_menu->AppendSeparator(); |
181 | item_menu->Append(TreeTest_ToggleIcon, "Toggle the items &icon"); | |
182 | ||
183 | #ifndef NO_MULTIPLE_SELECTION | |
184 | item_menu->AppendSeparator(); | |
185 | item_menu->Append(TreeTest_DumpSelected, "Dump selected items\tAlt-D"); | |
186 | item_menu->Append(TreeTest_Select, "Select current item\tAlt-S"); | |
187 | item_menu->Append(TreeTest_Unselect, "Unselect everything\tAlt-U"); | |
188 | #endif | |
5f939e78 VZ |
189 | |
190 | wxMenuBar *menu_bar = new wxMenuBar; | |
191 | menu_bar->Append(file_menu, "&File"); | |
192 | menu_bar->Append(tree_menu, "&Tree"); | |
193 | menu_bar->Append(item_menu, "&Item"); | |
194 | SetMenuBar(menu_bar); | |
195 | ||
196 | m_treeCtrl = new MyTreeCtrl(this, TreeTest_Ctrl, | |
197 | wxDefaultPosition, wxDefaultSize, | |
5ea47806 VZ |
198 | wxTR_HAS_BUTTONS | |
199 | wxTR_EDIT_LABELS | | |
89124b1a | 200 | #ifndef NO_MULTIPLE_SELECTION |
74b31181 | 201 | wxTR_MULTIPLE | |
89124b1a RR |
202 | #endif |
203 | #ifndef NO_VARIABLE_HEIGHT | |
74b31181 | 204 | wxTR_HAS_VARIABLE_ROW_HEIGHT | |
89124b1a | 205 | #endif |
74b31181 | 206 | wxSUNKEN_BORDER); |
5f939e78 VZ |
207 | wxTextCtrl *textCtrl = new wxTextCtrl(this, -1, "", |
208 | wxDefaultPosition, wxDefaultSize, | |
209 | wxTE_MULTILINE | wxSUNKEN_BORDER); | |
210 | ||
211 | wxLayoutConstraints *c = new wxLayoutConstraints; | |
212 | c->top.SameAs(this, wxTop); | |
213 | c->left.SameAs(this, wxLeft); | |
214 | c->right.SameAs(this, wxRight); | |
215 | c->height.PercentOf(this, wxHeight, 66); | |
216 | m_treeCtrl->SetConstraints(c); | |
217 | ||
218 | c = new wxLayoutConstraints; | |
219 | c->top.Below(m_treeCtrl); | |
220 | c->left.SameAs(this, wxLeft); | |
221 | c->right.SameAs(this, wxRight); | |
222 | c->bottom.SameAs(this, wxBottom); | |
223 | textCtrl->SetConstraints(c); | |
224 | SetAutoLayout(TRUE); | |
225 | ||
226 | // create a status bar with 3 panes | |
227 | CreateStatusBar(3); | |
228 | SetStatusText("", 0); | |
3a5a2f56 | 229 | |
a367b9b3 | 230 | #ifdef __WXMOTIF__ |
5f939e78 VZ |
231 | // For some reason, we get a memcpy crash in wxLogStream::DoLogStream |
232 | // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc? | |
233 | delete wxLog::SetActiveTarget(new wxLogStderr); | |
a367b9b3 | 234 | #else |
5f939e78 VZ |
235 | // set our text control as the log target |
236 | wxLogTextCtrl *logWindow = new wxLogTextCtrl(textCtrl); | |
237 | delete wxLog::SetActiveTarget(logWindow); | |
a367b9b3 | 238 | #endif |
457814b5 JS |
239 | } |
240 | ||
c89a6106 | 241 | MyFrame::~MyFrame() |
457814b5 | 242 | { |
5f939e78 | 243 | delete wxLog::SetActiveTarget(NULL); |
457814b5 JS |
244 | } |
245 | ||
3a5a2f56 | 246 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
457814b5 | 247 | { |
5f939e78 | 248 | Close(TRUE); |
457814b5 JS |
249 | } |
250 | ||
3a5a2f56 | 251 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) |
457814b5 | 252 | { |
5f939e78 | 253 | wxMessageBox("Tree test sample\n" |
5ea47806 | 254 | "(c) Julian Smart 1997, Vadim Zeitlin 1998", |
5f939e78 VZ |
255 | "About tree test", |
256 | wxOK | wxICON_INFORMATION, this); | |
e1ee62bd VZ |
257 | } |
258 | ||
259 | void MyFrame::OnRename(wxCommandEvent& WXUNUSED(event)) | |
260 | { | |
5f939e78 | 261 | wxTreeItemId item = m_treeCtrl->GetSelection(); |
e1ee62bd | 262 | |
5f939e78 | 263 | CHECK_ITEM( item ); |
e1ee62bd | 264 | |
5ea47806 VZ |
265 | // old code - now we edit in place |
266 | #if 0 | |
5f939e78 VZ |
267 | static wxString s_text; |
268 | s_text = wxGetTextFromUser("New name: ", "Tree sample question", | |
269 | s_text, this); | |
270 | if ( !s_text.IsEmpty() ) | |
271 | { | |
272 | m_treeCtrl->SetItemText(item, s_text); | |
273 | } | |
5ea47806 VZ |
274 | #endif // 0 |
275 | ||
276 | // TODO demonstrate creating a custom edit control... | |
277 | (void)m_treeCtrl->EditLabel(item); | |
e1ee62bd VZ |
278 | } |
279 | ||
5fd11f09 RR |
280 | void MyFrame::OnCount(wxCommandEvent& WXUNUSED(event)) |
281 | { | |
282 | wxTreeItemId item = m_treeCtrl->GetSelection(); | |
283 | ||
284 | CHECK_ITEM( item ); | |
285 | ||
286 | int i = m_treeCtrl->GetChildrenCount( item, FALSE ); | |
287 | ||
247e5b16 | 288 | wxLogMessage(wxT("%d children"), i); |
5fd11f09 RR |
289 | } |
290 | ||
291 | void MyFrame::OnCountRec(wxCommandEvent& WXUNUSED(event)) | |
292 | { | |
293 | wxTreeItemId item = m_treeCtrl->GetSelection(); | |
294 | ||
295 | CHECK_ITEM( item ); | |
296 | ||
297 | int i = m_treeCtrl->GetChildrenCount( item ); | |
298 | ||
247e5b16 | 299 | wxLogMessage(wxT("%d children"), i); |
5fd11f09 RR |
300 | } |
301 | ||
e1ee62bd VZ |
302 | void MyFrame::DoSort(bool reverse) |
303 | { | |
5f939e78 | 304 | wxTreeItemId item = m_treeCtrl->GetSelection(); |
e1ee62bd | 305 | |
5f939e78 | 306 | CHECK_ITEM( item ); |
457814b5 | 307 | |
5f939e78 | 308 | m_treeCtrl->DoSortChildren(item, reverse); |
457814b5 JS |
309 | } |
310 | ||
4832f7c0 VZ |
311 | void MyFrame::OnDump(wxCommandEvent& WXUNUSED(event)) |
312 | { | |
5f939e78 | 313 | wxTreeItemId root = m_treeCtrl->GetSelection(); |
e1ee62bd | 314 | |
5f939e78 | 315 | CHECK_ITEM( root ); |
e1ee62bd | 316 | |
5f939e78 | 317 | m_treeCtrl->GetItemsRecursively(root, -1); |
4832f7c0 VZ |
318 | } |
319 | ||
9dfbf520 VZ |
320 | #ifndef NO_MULTIPLE_SELECTION |
321 | ||
91b8de8d RR |
322 | void MyFrame::OnDumpSelected(wxCommandEvent& WXUNUSED(event)) |
323 | { | |
9dfbf520 | 324 | wxArrayTreeItemIds array; |
91b8de8d | 325 | |
9dfbf520 | 326 | size_t count = m_treeCtrl->GetSelections(array); |
247e5b16 | 327 | wxLogMessage(wxT("%u items selected"), count); |
91b8de8d | 328 | |
9dfbf520 VZ |
329 | for ( size_t n = 0; n < count; n++ ) |
330 | { | |
331 | wxLogMessage("\t%s", m_treeCtrl->GetItemText(array.Item(n)).c_str()); | |
332 | } | |
333 | } | |
334 | ||
335 | void MyFrame::OnSelect(wxCommandEvent& event) | |
336 | { | |
337 | m_treeCtrl->SelectItem(m_treeCtrl->GetSelection()); | |
338 | } | |
339 | ||
340 | void MyFrame::OnUnselect(wxCommandEvent& event) | |
341 | { | |
342 | m_treeCtrl->UnselectAll(); | |
91b8de8d RR |
343 | } |
344 | ||
9dfbf520 VZ |
345 | #endif // NO_MULTIPLE_SELECTION |
346 | ||
add28c55 VZ |
347 | void MyFrame::DoSetBold(bool bold) |
348 | { | |
5f939e78 | 349 | wxTreeItemId item = m_treeCtrl->GetSelection(); |
e1ee62bd | 350 | |
5f939e78 | 351 | CHECK_ITEM( item ); |
e1ee62bd | 352 | |
5f939e78 | 353 | m_treeCtrl->SetItemBold(item, bold); |
add28c55 VZ |
354 | } |
355 | ||
ff5bf259 VZ |
356 | void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event)) |
357 | { | |
5f939e78 | 358 | wxTreeItemId item = m_treeCtrl->GetSelection(); |
e1ee62bd | 359 | |
5f939e78 | 360 | CHECK_ITEM( item ); |
e1ee62bd | 361 | |
5f939e78 | 362 | m_treeCtrl->Delete(item); |
ff5bf259 VZ |
363 | } |
364 | ||
372edb9d VZ |
365 | void MyFrame::OnDeleteChildren(wxCommandEvent& WXUNUSED(event)) |
366 | { | |
5f939e78 | 367 | wxTreeItemId item = m_treeCtrl->GetSelection(); |
e1ee62bd | 368 | |
5f939e78 | 369 | CHECK_ITEM( item ); |
e1ee62bd | 370 | |
5f939e78 | 371 | m_treeCtrl->DeleteChildren(item); |
372edb9d VZ |
372 | } |
373 | ||
ff5bf259 VZ |
374 | void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event)) |
375 | { | |
5f939e78 | 376 | m_treeCtrl->DeleteAllItems(); |
ff5bf259 VZ |
377 | } |
378 | ||
379 | void MyFrame::OnRecreate(wxCommandEvent& event) | |
380 | { | |
5f939e78 VZ |
381 | OnDeleteAll(event); |
382 | m_treeCtrl->AddTestItemsToTree(3, 2); | |
ff5bf259 VZ |
383 | } |
384 | ||
dd3646fd VZ |
385 | void MyFrame::OnCollapseAndReset(wxCommandEvent& event) |
386 | { | |
5f939e78 | 387 | m_treeCtrl->CollapseAndReset(m_treeCtrl->GetRootItem()); |
dd3646fd VZ |
388 | } |
389 | ||
f65635b5 VZ |
390 | void MyFrame::OnEnsureVisible(wxCommandEvent& event) |
391 | { | |
392 | m_treeCtrl->DoEnsureVisible(); | |
393 | } | |
394 | ||
5f939e78 VZ |
395 | void MyFrame::OnAddItem(wxCommandEvent& WXUNUSED(event)) |
396 | { | |
397 | static int s_num = 0; | |
398 | ||
399 | wxString text; | |
400 | text.Printf("Item #%d", ++s_num); | |
401 | ||
402 | m_treeCtrl->AppendItem(m_treeCtrl->GetRootItem(), | |
112c5086 RR |
403 | text /*, |
404 | MyTreeCtrl::TreeCtrlIcon_File */ ); | |
5f939e78 VZ |
405 | } |
406 | ||
cf724bce RR |
407 | void MyFrame::OnIncIndent(wxCommandEvent& WXUNUSED(event)) |
408 | { | |
409 | unsigned int indent = m_treeCtrl->GetIndent(); | |
410 | if (indent < 100) | |
411 | m_treeCtrl->SetIndent( indent+5 ); | |
412 | } | |
413 | ||
414 | void MyFrame::OnDecIndent(wxCommandEvent& WXUNUSED(event)) | |
415 | { | |
416 | unsigned int indent = m_treeCtrl->GetIndent(); | |
417 | if (indent > 10) | |
418 | m_treeCtrl->SetIndent( indent-5 ); | |
419 | } | |
420 | ||
421 | void MyFrame::OnIncSpacing(wxCommandEvent& WXUNUSED(event)) | |
422 | { | |
423 | unsigned int indent = m_treeCtrl->GetSpacing(); | |
424 | if (indent < 100) | |
425 | m_treeCtrl->SetSpacing( indent+5 ); | |
426 | } | |
427 | ||
428 | void MyFrame::OnDecSpacing(wxCommandEvent& WXUNUSED(event)) | |
429 | { | |
430 | unsigned int indent = m_treeCtrl->GetSpacing(); | |
431 | if (indent > 10) | |
432 | m_treeCtrl->SetSpacing( indent-5 ); | |
433 | } | |
434 | ||
9dfbf520 VZ |
435 | void MyFrame::OnToggleIcon(wxCommandEvent& WXUNUSED(event)) |
436 | { | |
437 | wxTreeItemId item = m_treeCtrl->GetSelection(); | |
438 | ||
439 | CHECK_ITEM( item ); | |
440 | ||
441 | m_treeCtrl->DoToggleIcon(item); | |
442 | } | |
443 | ||
3a5a2f56 | 444 | // MyTreeCtrl implementation |
23fd5130 VZ |
445 | IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl, wxTreeCtrl) |
446 | ||
3a5a2f56 VZ |
447 | MyTreeCtrl::MyTreeCtrl(wxWindow *parent, const wxWindowID id, |
448 | const wxPoint& pos, const wxSize& size, | |
449 | long style) | |
450 | : wxTreeCtrl(parent, id, pos, size, style) | |
457814b5 | 451 | { |
9ce29a28 SB |
452 | #ifndef NO_VARIABLE_HEIGHT |
453 | #if wxUSE_LIBJPEG | |
9dfbf520 | 454 | wxImage::AddHandler(new wxJPEGHandler); |
91b8de8d RR |
455 | wxImage image; |
456 | ||
457 | image.LoadFile(wxString("horse.jpg"), wxBITMAP_TYPE_JPEG ); | |
9ce29a28 | 458 | #endif |
91b8de8d RR |
459 | #endif |
460 | ||
5f939e78 | 461 | m_reverseSort = FALSE; |
e1ee62bd | 462 | |
5f939e78 VZ |
463 | // Make an image list containing small icons |
464 | m_imageListNormal = new wxImageList(16, 16, TRUE); | |
457814b5 | 465 | |
5f939e78 | 466 | // should correspond to TreeCtrlIcon_xxx enum |
f60d0f94 | 467 | #if defined(__WXMSW__) && defined(__WIN16__) |
5f939e78 VZ |
468 | // This is required in 16-bit Windows mode only because we can't load a specific (16x16) |
469 | // icon image, so it comes out stretched | |
9ce29a28 | 470 | # ifndef NO_VARIABLE_HEIGHT |
91b8de8d RR |
471 | m_imageListNormal->Add(image.ConvertToBitmap()); |
472 | # else | |
5f939e78 | 473 | m_imageListNormal->Add(wxBitmap("bitmap1", wxBITMAP_TYPE_BMP_RESOURCE)); |
91b8de8d | 474 | # endif |
5f939e78 | 475 | m_imageListNormal->Add(wxBitmap("bitmap2", wxBITMAP_TYPE_BMP_RESOURCE)); |
74b31181 VZ |
476 | m_imageListNormal->Add(wxBitmap("bitmap3", wxBITMAP_TYPE_BMP_RESOURCE)); |
477 | m_imageListNormal->Add(wxBitmap("bitmap4", wxBITMAP_TYPE_BMP_RESOURCE)); | |
478 | m_imageListNormal->Add(wxBitmap("bitmap5", wxBITMAP_TYPE_BMP_RESOURCE)); | |
f60d0f94 | 479 | #else |
9ce29a28 | 480 | # ifndef NO_VARIABLE_HEIGHT |
91b8de8d RR |
481 | m_imageListNormal->Add(image.ConvertToBitmap()); |
482 | # else | |
5f939e78 | 483 | m_imageListNormal->Add(wxICON(icon1)); |
91b8de8d | 484 | # endif |
5f939e78 | 485 | m_imageListNormal->Add(wxICON(icon2)); |
74b31181 VZ |
486 | m_imageListNormal->Add(wxICON(icon3)); |
487 | m_imageListNormal->Add(wxICON(icon4)); | |
488 | m_imageListNormal->Add(wxICON(icon5)); | |
f60d0f94 | 489 | #endif |
457814b5 | 490 | |
5f939e78 | 491 | SetImageList(m_imageListNormal); |
457814b5 | 492 | |
5f939e78 VZ |
493 | // Add some items to the tree |
494 | AddTestItemsToTree(3, 2); | |
457814b5 JS |
495 | } |
496 | ||
3a5a2f56 | 497 | MyTreeCtrl::~MyTreeCtrl() |
457814b5 | 498 | { |
5f939e78 | 499 | delete m_imageListNormal; |
457814b5 JS |
500 | } |
501 | ||
e1ee62bd VZ |
502 | int MyTreeCtrl::OnCompareItems(const wxTreeItemId& item1, |
503 | const wxTreeItemId& item2) | |
504 | { | |
5f939e78 VZ |
505 | if ( m_reverseSort ) |
506 | { | |
507 | // just exchange 1st and 2nd items | |
508 | return wxTreeCtrl::OnCompareItems(item2, item1); | |
509 | } | |
510 | else | |
511 | { | |
512 | return wxTreeCtrl::OnCompareItems(item1, item2); | |
513 | } | |
e1ee62bd VZ |
514 | } |
515 | ||
3a5a2f56 VZ |
516 | void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId& idParent, |
517 | size_t numChildren, | |
4832f7c0 VZ |
518 | size_t depth, |
519 | size_t folder) | |
457814b5 | 520 | { |
f65635b5 | 521 | if ( depth > 0 ) |
3a5a2f56 | 522 | { |
74b31181 VZ |
523 | bool hasChildren = depth > 1; |
524 | ||
f65635b5 VZ |
525 | wxString str; |
526 | for ( size_t n = 0; n < numChildren; n++ ) | |
527 | { | |
528 | // at depth 1 elements won't have any more children | |
74b31181 | 529 | if ( hasChildren ) |
f65635b5 | 530 | str.Printf("%s child %d", "Folder", n + 1); |
74b31181 VZ |
531 | else |
532 | str.Printf("%s child %d.%d", "File", folder, n + 1); | |
f65635b5 | 533 | |
74b31181 VZ |
534 | // here we pass to AppendItem() normal and selected item images (we |
535 | // suppose that selected image follows the normal one in the enum) | |
9dfbf520 | 536 | int image = depth == 1 ? TreeCtrlIcon_File : TreeCtrlIcon_Folder; |
74b31181 | 537 | wxTreeItemId id = AppendItem(idParent, str, image, image + 1, |
f65635b5 VZ |
538 | new MyTreeItemData(str)); |
539 | ||
74b31181 VZ |
540 | // and now we also set the expanded one (only for the folders) |
541 | if ( hasChildren ) | |
542 | { | |
543 | SetItemImage(id, TreeCtrlIcon_FolderOpened, | |
544 | wxTreeItemIcon_Expanded); | |
545 | } | |
546 | ||
f65635b5 | 547 | // remember the last child for OnEnsureVisible() |
74b31181 | 548 | if ( !hasChildren && n == numChildren - 1 ) |
f65635b5 VZ |
549 | { |
550 | m_lastItem = id; | |
551 | } | |
552 | ||
553 | AddItemsRecursively(id, numChildren, depth - 1, n + 1); | |
554 | } | |
3a5a2f56 | 555 | } |
f65635b5 | 556 | //else: done! |
457814b5 JS |
557 | } |
558 | ||
3a5a2f56 VZ |
559 | void MyTreeCtrl::AddTestItemsToTree(size_t numChildren, |
560 | size_t depth) | |
457814b5 | 561 | { |
5f939e78 VZ |
562 | wxTreeItemId rootId = AddRoot("Root", |
563 | TreeCtrlIcon_Folder, TreeCtrlIcon_Folder, | |
564 | new MyTreeItemData("Root item")); | |
74b31181 | 565 | SetItemImage(rootId, TreeCtrlIcon_FolderOpened, wxTreeItemIcon_Expanded); |
457814b5 | 566 | |
5f939e78 | 567 | AddItemsRecursively(rootId, numChildren, depth, 0); |
9ec64fa7 VZ |
568 | |
569 | // set some colours/fonts for testing | |
570 | SetItemFont(rootId, *wxITALIC_FONT); | |
571 | ||
572 | long cookie; | |
573 | wxTreeItemId id = GetFirstChild(rootId, cookie); | |
574 | SetItemTextColour(id, *wxBLUE); | |
575 | ||
576 | id = GetNextChild(rootId, cookie); | |
577 | id = GetNextChild(rootId, cookie); | |
578 | SetItemTextColour(id, *wxRED); | |
579 | SetItemBackgroundColour(id, *wxLIGHT_GREY); | |
4832f7c0 VZ |
580 | } |
581 | ||
582 | void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId& idParent, long cookie) | |
583 | { | |
5f939e78 | 584 | wxTreeItemId id; |
4832f7c0 | 585 | |
5f939e78 VZ |
586 | if( cookie == -1 ) |
587 | id = GetFirstChild(idParent, cookie); | |
588 | else | |
589 | id = GetNextChild(idParent, cookie); | |
4832f7c0 | 590 | |
5f939e78 VZ |
591 | if(id <= 0) |
592 | return; | |
4832f7c0 | 593 | |
9dfbf520 | 594 | wxString text = GetItemText(id); |
5f939e78 | 595 | wxLogMessage(text); |
4832f7c0 | 596 | |
5f939e78 VZ |
597 | if (ItemHasChildren(id)) |
598 | GetItemsRecursively(id,-1); | |
4832f7c0 | 599 | |
5f939e78 | 600 | GetItemsRecursively(idParent, cookie); |
457814b5 JS |
601 | } |
602 | ||
9dfbf520 VZ |
603 | void MyTreeCtrl::DoToggleIcon(const wxTreeItemId& item) |
604 | { | |
605 | int image = GetItemImage(item) == TreeCtrlIcon_Folder ? TreeCtrlIcon_File | |
606 | : TreeCtrlIcon_Folder; | |
607 | ||
608 | SetItemImage(item, image); | |
609 | } | |
610 | ||
4832f7c0 | 611 | |
3a5a2f56 | 612 | // avoid repetition |
5f939e78 VZ |
613 | #define TREE_EVENT_HANDLER(name) \ |
614 | void MyTreeCtrl::name(wxTreeEvent& WXUNUSED(event)) \ | |
615 | { \ | |
616 | wxLogMessage(#name); \ | |
617 | } | |
457814b5 | 618 | |
3a5a2f56 VZ |
619 | TREE_EVENT_HANDLER(OnBeginDrag) |
620 | TREE_EVENT_HANDLER(OnBeginRDrag) | |
3a5a2f56 VZ |
621 | TREE_EVENT_HANDLER(OnDeleteItem) |
622 | TREE_EVENT_HANDLER(OnGetInfo) | |
623 | TREE_EVENT_HANDLER(OnSetInfo) | |
624 | TREE_EVENT_HANDLER(OnItemExpanded) | |
625 | TREE_EVENT_HANDLER(OnItemExpanding) | |
626 | TREE_EVENT_HANDLER(OnItemCollapsed) | |
627 | TREE_EVENT_HANDLER(OnSelChanged) | |
628 | TREE_EVENT_HANDLER(OnSelChanging) | |
629 | ||
630 | #undef TREE_EVENT_HANDLER | |
631 | ||
5ea47806 VZ |
632 | void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent& event) |
633 | { | |
634 | wxLogMessage("OnBeginLabelEdit"); | |
635 | ||
636 | // for testing, prevent this items label editing | |
637 | wxTreeItemId itemId = event.GetItem(); | |
638 | if ( IsTestItem(itemId) ) | |
639 | { | |
640 | wxMessageBox("You can't edit this item."); | |
641 | ||
642 | event.Veto(); | |
643 | } | |
644 | } | |
645 | ||
646 | void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent& event) | |
647 | { | |
648 | wxLogMessage("OnEndLabelEdit"); | |
649 | ||
650 | // don't allow anything except letters in the labels | |
651 | if ( !event.GetLabel().IsWord() ) | |
652 | { | |
653 | wxMessageBox("The label should contain only letters."); | |
654 | ||
655 | event.Veto(); | |
656 | } | |
657 | } | |
658 | ||
3a5a2f56 | 659 | void MyTreeCtrl::OnItemCollapsing(wxTreeEvent& event) |
457814b5 | 660 | { |
5f939e78 | 661 | wxLogMessage("OnItemCollapsing"); |
f135ff73 | 662 | |
5f939e78 VZ |
663 | // for testing, prevent the user from collapsing the first child folder |
664 | wxTreeItemId itemId = event.GetItem(); | |
5ea47806 | 665 | if ( IsTestItem(itemId) ) |
5f939e78 VZ |
666 | { |
667 | wxMessageBox("You can't collapse this item."); | |
457814b5 | 668 | |
5f939e78 VZ |
669 | event.Veto(); |
670 | } | |
457814b5 JS |
671 | } |
672 | ||
6daa0637 | 673 | void MyTreeCtrl::OnTreeKeyDown(wxTreeEvent&WXUNUSED(event)) |
435fe83e | 674 | { |
5f939e78 | 675 | wxLogMessage("OnTreeKeyDown"); |
435fe83e RR |
676 | } |
677 | ||
678 | void MyTreeCtrl::OnItemActivated(wxTreeEvent&WXUNUSED(event)) | |
457814b5 | 679 | { |
5f939e78 VZ |
680 | // show some info about this item |
681 | wxTreeItemId itemId = GetSelection(); | |
682 | MyTreeItemData *item = (MyTreeItemData *)GetItemData(itemId); | |
457814b5 | 683 | |
5f939e78 VZ |
684 | if ( item != NULL ) |
685 | { | |
686 | item->ShowInfo(this); | |
687 | } | |
457814b5 | 688 | |
5f939e78 VZ |
689 | wxLogMessage("OnItemActivated"); |
690 | } | |
691 | ||
692 | void MyTreeCtrl::OnRMouseDClick(wxMouseEvent& event) | |
693 | { | |
694 | wxTreeItemId id = HitTest(event.GetPosition()); | |
695 | if ( !id ) | |
696 | wxLogMessage("No item under mouse"); | |
697 | else | |
698 | { | |
699 | MyTreeItemData *item = (MyTreeItemData *)GetItemData(id); | |
700 | if ( item ) | |
701 | wxLogMessage("Item '%s' under mouse", item->GetDesc()); | |
702 | } | |
457814b5 JS |
703 | } |
704 | ||
3a5a2f56 | 705 | static inline const char *Bool2String(bool b) |
457814b5 | 706 | { |
5f939e78 | 707 | return b ? "" : "not "; |
457814b5 JS |
708 | } |
709 | ||
9769e589 | 710 | void MyTreeItemData::ShowInfo(wxTreeCtrl *tree) |
457814b5 | 711 | { |
5f939e78 VZ |
712 | wxLogMessage("Item '%s': %sselected, %sexpanded, %sbold,\n" |
713 | "%u children (%u immediately under this item).", | |
714 | m_desc.c_str(), | |
715 | Bool2String(tree->IsSelected(GetId())), | |
716 | Bool2String(tree->IsExpanded(GetId())), | |
717 | Bool2String(tree->IsBold(GetId())), | |
718 | tree->GetChildrenCount(GetId()), | |
719 | tree->GetChildrenCount(GetId(), FALSE)); | |
457814b5 | 720 | } |