]> git.saurik.com Git - wxWidgets.git/blob - samples/treectrl/treetest.cpp
70c37f8dd40f121f3d20308d5255c8a1763c0f8f
[wxWidgets.git] / samples / treectrl / treetest.cpp
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
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #ifndef WX_PRECOMP
20 #include "wx/wx.h"
21 #include "wx/log.h"
22 #endif
23
24 #include "wx/colordlg.h"
25 #include "wx/numdlg.h"
26
27 #include "wx/artprov.h"
28 #include "wx/image.h"
29 #include "wx/imaglist.h"
30 #include "wx/treectrl.h"
31 #include "wx/math.h"
32 #include "wx/renderer.h"
33
34 #ifdef __WIN32__
35 // this is not supported by native control
36 #define NO_VARIABLE_HEIGHT
37 #endif
38
39 #include "treetest.h"
40
41 #include "icon1.xpm"
42 #include "icon2.xpm"
43 #include "icon3.xpm"
44 #include "icon4.xpm"
45 #include "icon5.xpm"
46
47 #include "state1.xpm"
48 #include "state2.xpm"
49 #include "state3.xpm"
50 #include "state4.xpm"
51 #include "state5.xpm"
52
53 #include "unchecked.xpm"
54 #include "checked.xpm"
55
56 #ifndef __WXMSW__
57 #include "../sample.xpm"
58 #endif
59
60 static const int NUM_CHILDREN_PER_LEVEL = 5;
61 static const int NUM_LEVELS = 2;
62
63 // verify that the item is ok and insult the user if it is not
64 #define CHECK_ITEM( item ) if ( !item.IsOk() ) { \
65 wxMessageBox(wxT("Please select some item first!"), \
66 wxT("Tree sample error"), \
67 wxOK | wxICON_EXCLAMATION, \
68 this); \
69 return; \
70 }
71
72 #define MENU_LINK(name) EVT_MENU(TreeTest_##name, MyFrame::On##name)
73
74 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
75 EVT_IDLE(MyFrame::OnIdle)
76 EVT_SIZE(MyFrame::OnSize)
77
78 MENU_LINK(Quit)
79 MENU_LINK(About)
80 MENU_LINK(ClearLog)
81
82 MENU_LINK(TogButtons)
83 MENU_LINK(TogTwist)
84 MENU_LINK(TogLines)
85 MENU_LINK(TogEdit)
86 MENU_LINK(TogHideRoot)
87 MENU_LINK(TogRootLines)
88 MENU_LINK(TogBorder)
89 MENU_LINK(TogFullHighlight)
90 MENU_LINK(SetFgColour)
91 MENU_LINK(SetBgColour)
92 MENU_LINK(ResetStyle)
93
94 MENU_LINK(Highlight)
95 MENU_LINK(Dump)
96 #ifndef NO_MULTIPLE_SELECTION
97 MENU_LINK(DumpSelected)
98 MENU_LINK(Select)
99 MENU_LINK(Unselect)
100 MENU_LINK(ToggleSel)
101 MENU_LINK(SelectChildren)
102 #endif // NO_MULTIPLE_SELECTION
103 MENU_LINK(Rename)
104 MENU_LINK(Count)
105 MENU_LINK(CountRec)
106 MENU_LINK(Sort)
107 MENU_LINK(SortRev)
108 MENU_LINK(SetBold)
109 MENU_LINK(ClearBold)
110 MENU_LINK(Delete)
111 MENU_LINK(DeleteChildren)
112 MENU_LINK(DeleteAll)
113 MENU_LINK(Recreate)
114 MENU_LINK(ToggleImages)
115 MENU_LINK(ToggleStates)
116 MENU_LINK(ToggleAlternateImages)
117 MENU_LINK(ToggleAlternateStates)
118 MENU_LINK(ToggleButtons)
119 MENU_LINK(SetImageSize)
120 MENU_LINK(CollapseAndReset)
121 MENU_LINK(EnsureVisible)
122 MENU_LINK(SetFocus)
123 MENU_LINK(AddItem)
124 MENU_LINK(InsertItem)
125 MENU_LINK(IncIndent)
126 MENU_LINK(DecIndent)
127 MENU_LINK(IncSpacing)
128 MENU_LINK(DecSpacing)
129 MENU_LINK(ToggleIcon)
130 MENU_LINK(ToggleState)
131 MENU_LINK(SelectRoot)
132 MENU_LINK(SetFocusedRoot)
133 MENU_LINK(ClearFocused)
134
135 MENU_LINK(ShowFirstVisible)
136 #ifdef wxHAS_LAST_VISIBLE
137 MENU_LINK(ShowLastVisible)
138 #endif // wxHAS_LAST_VISIBLE
139 MENU_LINK(ShowNextVisible)
140 MENU_LINK(ShowPrevVisible)
141 MENU_LINK(ShowParent)
142 MENU_LINK(ShowPrevSibling)
143 MENU_LINK(ShowNextSibling)
144 MENU_LINK(ScrollTo)
145 MENU_LINK(SelectLast)
146 #undef MENU_LINK
147
148 END_EVENT_TABLE()
149
150 #if USE_GENERIC_TREECTRL
151 BEGIN_EVENT_TABLE(MyTreeCtrl, wxGenericTreeCtrl)
152 #else
153 BEGIN_EVENT_TABLE(MyTreeCtrl, wxTreeCtrl)
154 #endif
155 EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginDrag)
156 EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginRDrag)
157 EVT_TREE_END_DRAG(TreeTest_Ctrl, MyTreeCtrl::OnEndDrag)
158 EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnBeginLabelEdit)
159 EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnEndLabelEdit)
160 EVT_TREE_DELETE_ITEM(TreeTest_Ctrl, MyTreeCtrl::OnDeleteItem)
161 #if 0 // there are so many of those that logging them causes flicker
162 EVT_TREE_GET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnGetInfo)
163 #endif
164 EVT_TREE_SET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnSetInfo)
165 EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanded)
166 EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanding)
167 EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsed)
168 EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsing)
169
170 EVT_TREE_SEL_CHANGED(TreeTest_Ctrl, MyTreeCtrl::OnSelChanged)
171 EVT_TREE_SEL_CHANGING(TreeTest_Ctrl, MyTreeCtrl::OnSelChanging)
172 EVT_TREE_KEY_DOWN(TreeTest_Ctrl, MyTreeCtrl::OnTreeKeyDown)
173 EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl, MyTreeCtrl::OnItemActivated)
174 EVT_TREE_STATE_IMAGE_CLICK(TreeTest_Ctrl, MyTreeCtrl::OnItemStateClick)
175
176 // so many different ways to handle right mouse button clicks...
177 EVT_CONTEXT_MENU(MyTreeCtrl::OnContextMenu)
178 // EVT_TREE_ITEM_MENU is the preferred event for creating context menus
179 // on a tree control, because it includes the point of the click or item,
180 // meaning that no additional placement calculations are required.
181 EVT_TREE_ITEM_MENU(TreeTest_Ctrl, MyTreeCtrl::OnItemMenu)
182 EVT_TREE_ITEM_RIGHT_CLICK(TreeTest_Ctrl, MyTreeCtrl::OnItemRClick)
183
184 EVT_RIGHT_DOWN(MyTreeCtrl::OnRMouseDown)
185 EVT_RIGHT_UP(MyTreeCtrl::OnRMouseUp)
186 EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick)
187 END_EVENT_TABLE()
188
189 IMPLEMENT_APP(MyApp)
190
191 bool MyApp::OnInit()
192 {
193 if ( !wxApp::OnInit() )
194 return false;
195
196 // Create the main frame window
197 MyFrame *frame = new MyFrame(wxT("wxTreeCtrl Test"), 50, 50, 450, 600);
198
199 // Show the frame
200 frame->Show(true);
201 SetTopWindow(frame);
202
203 return true;
204 }
205
206
207 // My frame constructor
208 MyFrame::MyFrame(const wxString& title, int x, int y, int w, int h)
209 : wxFrame((wxFrame *)NULL, wxID_ANY, title, wxPoint(x, y), wxSize(w, h)),
210 m_treeCtrl(NULL)
211 #if wxUSE_LOG
212 , m_textCtrl(NULL)
213 #endif // wxUSE_LOG
214 {
215 // This reduces flicker effects - even better would be to define
216 // OnEraseBackground to do nothing. When the tree control's scrollbars are
217 // show or hidden, the frame is sent a background erase event.
218 SetBackgroundColour(wxColour(255, 255, 255));
219
220 // Give it an icon
221 SetIcon(wxICON(sample));
222
223 #if wxUSE_MENUS
224 // Make a menubar
225 wxMenu *file_menu = new wxMenu,
226 *style_menu = new wxMenu,
227 *tree_menu = new wxMenu,
228 *item_menu = new wxMenu;
229
230 file_menu->Append(TreeTest_ClearLog, wxT("&Clear log\tCtrl-L"));
231 file_menu->AppendSeparator();
232 file_menu->Append(TreeTest_About, wxT("&About..."));
233 file_menu->AppendSeparator();
234 file_menu->Append(TreeTest_Quit, wxT("E&xit\tAlt-X"));
235
236 style_menu->AppendCheckItem(TreeTest_TogButtons, wxT("Toggle &normal buttons"));
237 style_menu->AppendCheckItem(TreeTest_TogTwist, wxT("Toggle &twister buttons"));
238 style_menu->AppendCheckItem(TreeTest_ToggleButtons, wxT("Toggle image &buttons"));
239 style_menu->AppendSeparator();
240 style_menu->AppendCheckItem(TreeTest_TogLines, wxT("Toggle &connecting lines"));
241 style_menu->AppendCheckItem(TreeTest_TogRootLines, wxT("Toggle &lines at root"));
242 style_menu->AppendCheckItem(TreeTest_TogHideRoot, wxT("Toggle &hidden root"));
243 style_menu->AppendCheckItem(TreeTest_TogBorder, wxT("Toggle &item border"));
244 style_menu->AppendCheckItem(TreeTest_TogFullHighlight, wxT("Toggle &full row highlight"));
245 style_menu->AppendCheckItem(TreeTest_TogEdit, wxT("Toggle &edit mode"));
246 #ifndef NO_MULTIPLE_SELECTION
247 style_menu->AppendCheckItem(TreeTest_ToggleSel, wxT("Toggle &selection mode\tCtrl-S"));
248 #endif // NO_MULTIPLE_SELECTION
249 style_menu->AppendCheckItem(TreeTest_ToggleImages, wxT("Toggle show ima&ges"));
250 style_menu->AppendCheckItem(TreeTest_ToggleStates, wxT("Toggle show st&ates"));
251 style_menu->AppendCheckItem(TreeTest_ToggleAlternateImages, wxT("Toggle alternate images"));
252 style_menu->AppendCheckItem(TreeTest_ToggleAlternateStates, wxT("Toggle alternate state images"));
253 style_menu->Append(TreeTest_SetImageSize, wxT("Set image si&ze..."));
254 style_menu->AppendSeparator();
255 style_menu->Append(TreeTest_SetFgColour, wxT("Set &foreground colour..."));
256 style_menu->Append(TreeTest_SetBgColour, wxT("Set &background colour..."));
257 style_menu->AppendSeparator();
258 style_menu->Append(TreeTest_ResetStyle, wxT("&Reset to default\tF10"));
259
260 tree_menu->Append(TreeTest_Recreate, wxT("&Recreate the tree"));
261 tree_menu->Append(TreeTest_CollapseAndReset, wxT("C&ollapse and reset"));
262 tree_menu->AppendSeparator();
263 tree_menu->Append(TreeTest_AddItem, wxT("Append a &new item"));
264 tree_menu->Append(TreeTest_InsertItem, wxT("&Insert a new item"));
265 tree_menu->Append(TreeTest_Delete, wxT("&Delete this item"));
266 tree_menu->Append(TreeTest_DeleteChildren, wxT("Delete &children"));
267 tree_menu->Append(TreeTest_DeleteAll, wxT("Delete &all items"));
268 tree_menu->Append(TreeTest_SelectRoot, wxT("Select root item"));
269 tree_menu->AppendSeparator();
270 tree_menu->Append(TreeTest_SetFocusedRoot, wxT("Set focus to root item"));
271 tree_menu->Append(TreeTest_ClearFocused, wxT("Reset focus"));
272
273 tree_menu->AppendSeparator();
274 tree_menu->Append(TreeTest_Count, wxT("Count children of current item"));
275 tree_menu->Append(TreeTest_CountRec, wxT("Recursively count children of current item"));
276 tree_menu->AppendSeparator();
277 tree_menu->Append(TreeTest_Sort, wxT("Sort children of current item"));
278 tree_menu->Append(TreeTest_SortRev, wxT("Sort in reversed order"));
279 tree_menu->AppendSeparator();
280 tree_menu->Append(TreeTest_EnsureVisible, wxT("Make the last item &visible"));
281 tree_menu->Append(TreeTest_SetFocus, wxT("Set &focus to the tree"));
282 tree_menu->AppendSeparator();
283 tree_menu->Append(TreeTest_IncIndent, wxT("Add 5 points to indentation\tAlt-I"));
284 tree_menu->Append(TreeTest_DecIndent, wxT("Reduce indentation by 5 points\tAlt-R"));
285 tree_menu->AppendSeparator();
286 tree_menu->Append(TreeTest_IncSpacing, wxT("Add 5 points to spacing\tCtrl-I"));
287 tree_menu->Append(TreeTest_DecSpacing, wxT("Reduce spacing by 5 points\tCtrl-R"));
288
289 item_menu->Append(TreeTest_Dump, wxT("&Dump item children"));
290 item_menu->Append(TreeTest_Rename, wxT("&Rename item..."));
291
292 item_menu->AppendSeparator();
293 item_menu->Append(TreeTest_SetBold, wxT("Make item &bold"));
294 item_menu->Append(TreeTest_ClearBold, wxT("Make item &not bold"));
295 item_menu->AppendSeparator();
296 item_menu->Append(TreeTest_ToggleIcon, wxT("Toggle the item's &icon"));
297 item_menu->Append(TreeTest_ToggleState, wxT("Toggle the item's &state"));
298 item_menu->AppendSeparator();
299 item_menu->Append(TreeTest_ShowFirstVisible, wxT("Show &first visible"));
300 #ifdef wxHAS_LAST_VISIBLE
301 item_menu->Append(TreeTest_ShowLastVisible, wxT("Show &last visible"));
302 #endif // wxHAS_LAST_VISIBLE
303 item_menu->Append(TreeTest_ShowNextVisible, wxT("Show &next visible"));
304 item_menu->Append(TreeTest_ShowPrevVisible, wxT("Show &previous visible"));
305 item_menu->AppendSeparator();
306 item_menu->Append(TreeTest_ShowParent, "Show pa&rent");
307 item_menu->Append(TreeTest_ShowPrevSibling, "Show &previous sibling");
308 item_menu->Append(TreeTest_ShowNextSibling, "Show &next sibling");
309 item_menu->AppendSeparator();
310 item_menu->Append(TreeTest_ScrollTo, "Scroll &to item",
311 "Scroll to the last by one top level child");
312 item_menu->Append(TreeTest_SelectLast, "Select &last item",
313 "Select the last item");
314
315 #ifndef NO_MULTIPLE_SELECTION
316 item_menu->AppendSeparator();
317 item_menu->Append(TreeTest_DumpSelected, wxT("Dump selected items\tAlt-D"));
318 item_menu->Append(TreeTest_Select, wxT("Select current item\tAlt-S"));
319 item_menu->Append(TreeTest_Unselect, wxT("Unselect everything\tAlt-U"));
320 item_menu->Append(TreeTest_SelectChildren, wxT("Select all children\tCtrl-A"));
321 #endif // NO_MULTIPLE_SELECTION
322
323 wxMenuBar *menu_bar = new wxMenuBar;
324 menu_bar->Append(file_menu, wxT("&File"));
325 menu_bar->Append(style_menu, wxT("&Style"));
326 menu_bar->Append(tree_menu, wxT("&Tree"));
327 menu_bar->Append(item_menu, wxT("&Item"));
328 SetMenuBar(menu_bar);
329 #endif // wxUSE_MENUS
330
331 m_panel = new wxPanel(this);
332
333 #if wxUSE_LOG
334 // create the controls
335 m_textCtrl = new wxTextCtrl(m_panel, wxID_ANY, wxT(""),
336 wxDefaultPosition, wxDefaultSize,
337 wxTE_MULTILINE | wxSUNKEN_BORDER);
338 #endif // wxUSE_LOG
339
340 CreateTreeWithDefStyle();
341
342 menu_bar->Check(TreeTest_ToggleImages, true);
343 menu_bar->Check(TreeTest_ToggleStates, true);
344 menu_bar->Check(TreeTest_ToggleAlternateImages, false);
345 menu_bar->Check(TreeTest_ToggleAlternateStates, false);
346
347 #if wxUSE_STATUSBAR
348 // create a status bar
349 CreateStatusBar(2);
350 #endif // wxUSE_STATUSBAR
351
352 #if wxUSE_LOG
353 #ifdef __WXMOTIF__
354 // For some reason, we get a memcpy crash in wxLogStream::DoLogStream
355 // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc?
356 delete wxLog::SetActiveTarget(new wxLogStderr);
357 #else
358 // set our text control as the log target
359 wxLogTextCtrl *logWindow = new wxLogTextCtrl(m_textCtrl);
360 delete wxLog::SetActiveTarget(logWindow);
361 #endif
362 #endif // wxUSE_LOG
363 }
364
365 MyFrame::~MyFrame()
366 {
367 #if wxUSE_LOG
368 delete wxLog::SetActiveTarget(NULL);
369 #endif // wxUSE_LOG
370 }
371
372 void MyFrame::CreateTreeWithDefStyle()
373 {
374 long style = wxTR_DEFAULT_STYLE |
375 #ifndef NO_VARIABLE_HEIGHT
376 wxTR_HAS_VARIABLE_ROW_HEIGHT |
377 #endif
378 wxTR_EDIT_LABELS;
379
380 CreateTree(style | wxSUNKEN_BORDER);
381
382 // as we don't know what wxTR_DEFAULT_STYLE could contain, test for
383 // everything
384 wxMenuBar *mbar = GetMenuBar();
385 mbar->Check(TreeTest_TogButtons, (style & wxTR_HAS_BUTTONS) != 0);
386 mbar->Check(TreeTest_TogButtons, (style & wxTR_TWIST_BUTTONS) != 0);
387 mbar->Check(TreeTest_TogLines, (style & wxTR_NO_LINES) == 0);
388 mbar->Check(TreeTest_TogRootLines, (style & wxTR_LINES_AT_ROOT) != 0);
389 mbar->Check(TreeTest_TogHideRoot, (style & wxTR_HIDE_ROOT) != 0);
390 mbar->Check(TreeTest_TogEdit, (style & wxTR_EDIT_LABELS) != 0);
391 mbar->Check(TreeTest_TogBorder, (style & wxTR_ROW_LINES) != 0);
392 mbar->Check(TreeTest_TogFullHighlight, (style & wxTR_FULL_ROW_HIGHLIGHT) != 0);
393 }
394
395 void MyFrame::CreateTree(long style)
396 {
397 m_treeCtrl = new MyTreeCtrl(m_panel, TreeTest_Ctrl,
398 wxDefaultPosition, wxDefaultSize,
399 style);
400
401 GetMenuBar()->Enable(TreeTest_SelectRoot, !(style & wxTR_HIDE_ROOT));
402
403 Resize();
404 }
405
406 void MyFrame::TogStyle(int id, long flag)
407 {
408 long style = m_treeCtrl->GetWindowStyle() ^ flag;
409
410 // most treectrl styles can't be changed on the fly using the native
411 // control and the tree must be recreated
412 #ifndef __WXMSW__
413 m_treeCtrl->SetWindowStyle(style);
414 #else // MSW
415 delete m_treeCtrl;
416 CreateTree(style);
417 #endif // !MSW/MSW
418
419 GetMenuBar()->Check(id, (style & flag) != 0);
420 }
421
422 void MyFrame::OnIdle(wxIdleEvent& event)
423 {
424 #if wxUSE_STATUSBAR
425 if ( m_treeCtrl )
426 {
427 wxTreeItemId idRoot = m_treeCtrl->GetRootItem();
428 wxString status;
429 if (idRoot.IsOk())
430 {
431 wxTreeItemId idLast = m_treeCtrl->GetLastChild(idRoot);
432 status = wxString::Format(
433 wxT("Root/last item is %svisible/%svisible"),
434 m_treeCtrl->IsVisible(idRoot) ? wxT("") : wxT("not "),
435 idLast.IsOk() && m_treeCtrl->IsVisible(idLast)
436 ? wxT("") : wxT("not "));
437 }
438 else
439 status = wxT("No root item");
440
441 SetStatusText(status, 1);
442 }
443 #endif // wxUSE_STATUSBAR
444
445 event.Skip();
446 }
447
448 void MyFrame::OnSize(wxSizeEvent& event)
449 {
450 if ( m_treeCtrl
451 #if wxUSE_LOG
452 && m_textCtrl
453 #endif // wxUSE_LOG
454 )
455 {
456 Resize();
457 }
458
459 event.Skip();
460 }
461
462 void MyFrame::Resize()
463 {
464 wxSize size = GetClientSize();
465 m_treeCtrl->SetSize(0, 0, size.x, size.y
466 #if wxUSE_LOG
467 *2/3);
468 m_textCtrl->SetSize(0, 2*size.y/3, size.x, size.y/3
469 #endif
470 );
471 }
472
473 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
474 {
475 Close(true);
476 }
477
478 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
479 {
480 wxMessageBox(wxT("Tree test sample\n")
481 wxT("(c) Julian Smart 1997, Vadim Zeitlin 1998"),
482 wxT("About tree test"),
483 wxOK | wxICON_INFORMATION, this);
484 }
485
486 void MyFrame::OnClearLog(wxCommandEvent& WXUNUSED(event))
487 {
488 m_textCtrl->Clear();
489 }
490
491 void MyFrame::OnRename(wxCommandEvent& WXUNUSED(event))
492 {
493 wxTreeItemId item = m_treeCtrl->GetFocusedItem();
494
495 CHECK_ITEM( item );
496
497 // old code - now we edit in place
498 #if 0
499 static wxString s_text;
500 s_text = wxGetTextFromUser(wxT("New name: "), wxT("Tree sample question"),
501 s_text, this);
502 if ( !s_text.empty() )
503 {
504 m_treeCtrl->SetItemText(item, s_text);
505 }
506 #endif // 0
507
508 // TODO demonstrate creating a custom edit control...
509 (void)m_treeCtrl->EditLabel(item);
510 }
511
512 void MyFrame::OnCount(wxCommandEvent& WXUNUSED(event))
513 {
514 wxTreeItemId item = m_treeCtrl->GetFocusedItem();
515
516 CHECK_ITEM( item );
517
518 int i = m_treeCtrl->GetChildrenCount( item, false );
519
520 wxLogMessage(wxT("%d children"), i);
521 }
522
523 void MyFrame::OnCountRec(wxCommandEvent& WXUNUSED(event))
524 {
525 wxTreeItemId item = m_treeCtrl->GetFocusedItem();
526
527 CHECK_ITEM( item );
528
529 int i = m_treeCtrl->GetChildrenCount( item );
530
531 wxLogMessage(wxT("%d children"), i);
532 }
533
534 void MyFrame::DoSort(bool reverse)
535 {
536 wxTreeItemId item = m_treeCtrl->GetFocusedItem();
537
538 CHECK_ITEM( item );
539
540 m_treeCtrl->DoSortChildren(item, reverse);
541 }
542
543 void MyFrame::OnHighlight(wxCommandEvent& WXUNUSED(event))
544 {
545 wxTreeItemId id = m_treeCtrl->GetFocusedItem();
546
547 CHECK_ITEM( id );
548
549 wxRect r;
550 if ( !m_treeCtrl->GetBoundingRect(id, r, true /* text, not full row */) )
551 {
552 wxLogMessage(wxT("Failed to get bounding item rect"));
553 return;
554 }
555
556 wxClientDC dc(m_treeCtrl);
557 dc.SetBrush(*wxRED);
558 dc.SetPen(*wxTRANSPARENT_PEN);
559 dc.DrawRectangle(r);
560 m_treeCtrl->Update();
561 }
562
563 void MyFrame::OnDump(wxCommandEvent& WXUNUSED(event))
564 {
565 wxTreeItemId root = m_treeCtrl->GetFocusedItem();
566
567 CHECK_ITEM( root );
568
569 m_treeCtrl->GetItemsRecursively(root);
570 }
571
572 #ifndef NO_MULTIPLE_SELECTION
573
574 void MyFrame::OnToggleSel(wxCommandEvent& event)
575 {
576 TogStyle(event.GetId(), wxTR_MULTIPLE);
577 }
578
579 void MyFrame::OnDumpSelected(wxCommandEvent& WXUNUSED(event))
580 {
581 wxArrayTreeItemIds array;
582
583 size_t count = m_treeCtrl->GetSelections(array);
584 wxLogMessage(wxT("%u items selected"), unsigned(count));
585
586 for ( size_t n = 0; n < count; n++ )
587 {
588 wxLogMessage(wxT("\t%s"), m_treeCtrl->GetItemText(array.Item(n)).c_str());
589 }
590 }
591
592 void MyFrame::OnSelect(wxCommandEvent& WXUNUSED(event))
593 {
594 m_treeCtrl->SelectItem(m_treeCtrl->GetFocusedItem());
595 }
596
597 void MyFrame::OnSelectRoot(wxCommandEvent& WXUNUSED(event))
598 {
599 if ( !m_treeCtrl->HasFlag(wxTR_HIDE_ROOT) )
600 m_treeCtrl->SelectItem(m_treeCtrl->GetRootItem());
601 }
602
603 void MyFrame::OnSetFocusedRoot(wxCommandEvent& WXUNUSED(event))
604 {
605 if ( !m_treeCtrl->HasFlag(wxTR_HIDE_ROOT) )
606 m_treeCtrl->SetFocusedItem(m_treeCtrl->GetRootItem());
607 }
608
609 void MyFrame::OnClearFocused(wxCommandEvent& WXUNUSED(event))
610 {
611 m_treeCtrl->ClearFocusedItem();
612 }
613
614 void MyFrame::OnUnselect(wxCommandEvent& WXUNUSED(event))
615 {
616 m_treeCtrl->UnselectAll();
617 }
618
619 void MyFrame::OnSelectChildren(wxCommandEvent& WXUNUSED(event))
620 {
621 wxTreeItemId item = m_treeCtrl->GetFocusedItem();
622 if ( !item.IsOk() )
623 item = m_treeCtrl->GetRootItem();
624
625 m_treeCtrl->SelectChildren(item);
626 }
627
628 #endif // NO_MULTIPLE_SELECTION
629
630 void MyFrame::DoSetBold(bool bold)
631 {
632 wxTreeItemId item = m_treeCtrl->GetFocusedItem();
633
634 CHECK_ITEM( item );
635
636 m_treeCtrl->SetItemBold(item, bold);
637 }
638
639 void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event))
640 {
641 wxTreeItemId item = m_treeCtrl->GetFocusedItem();
642
643 CHECK_ITEM( item );
644
645 m_treeCtrl->Delete(item);
646 }
647
648 void MyFrame::OnDeleteChildren(wxCommandEvent& WXUNUSED(event))
649 {
650 wxTreeItemId item = m_treeCtrl->GetFocusedItem();
651
652 CHECK_ITEM( item );
653
654 m_treeCtrl->DeleteChildren(item);
655 }
656
657 void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event))
658 {
659 m_treeCtrl->DeleteAllItems();
660 }
661
662 void MyFrame::OnRecreate(wxCommandEvent& event)
663 {
664 OnDeleteAll(event);
665 m_treeCtrl->AddTestItemsToTree(NUM_CHILDREN_PER_LEVEL, NUM_LEVELS);
666 }
667
668 void MyFrame::OnSetImageSize(wxCommandEvent& WXUNUSED(event))
669 {
670 int size = wxGetNumberFromUser(wxT("Enter the size for the images to use"),
671 wxT("Size: "),
672 wxT("TreeCtrl sample"),
673 m_treeCtrl->ImageSize());
674 if ( size == -1 )
675 return;
676
677 m_treeCtrl->CreateImageList(size);
678 wxGetApp().SetShowImages(true);
679 }
680
681 void MyFrame::OnToggleImages(wxCommandEvent& WXUNUSED(event))
682 {
683 if ( wxGetApp().ShowImages() )
684 {
685 m_treeCtrl->CreateImageList(-1);
686 wxGetApp().SetShowImages(false);
687 }
688 else
689 {
690 m_treeCtrl->CreateImageList(0);
691 wxGetApp().SetShowImages(true);
692 }
693 }
694
695 void MyFrame::OnToggleStates(wxCommandEvent& WXUNUSED(event))
696 {
697 if ( wxGetApp().ShowStates() )
698 {
699 m_treeCtrl->CreateStateImageList(true);
700 wxGetApp().SetShowStates(false);
701 }
702 else
703 {
704 m_treeCtrl->CreateStateImageList(false);
705 wxGetApp().SetShowStates(true);
706 }
707 }
708
709 void MyFrame::OnToggleAlternateImages(wxCommandEvent& WXUNUSED(event))
710 {
711 bool alternateImages = m_treeCtrl->AlternateImages();
712
713 m_treeCtrl->SetAlternateImages(!alternateImages);
714 m_treeCtrl->CreateImageList(0);
715 }
716
717 void MyFrame::OnToggleAlternateStates(wxCommandEvent& WXUNUSED(event))
718 {
719 bool alternateStates = m_treeCtrl->AlternateStates();
720
721 m_treeCtrl->SetAlternateStates(!alternateStates);
722 m_treeCtrl->CreateStateImageList();
723
724 // normal states < alternate states
725 // so we must reset broken states
726 if ( alternateStates )
727 m_treeCtrl->ResetBrokenStateImages();
728 }
729
730 void MyFrame::OnToggleButtons(wxCommandEvent& WXUNUSED(event))
731 {
732 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
733 if ( wxGetApp().ShowButtons() )
734 {
735 m_treeCtrl->CreateButtonsImageList(-1);
736 wxGetApp().SetShowButtons(false);
737 }
738 else
739 {
740 m_treeCtrl->CreateButtonsImageList(15);
741 wxGetApp().SetShowButtons(true);
742 }
743 #endif
744 }
745
746 void MyFrame::OnCollapseAndReset(wxCommandEvent& WXUNUSED(event))
747 {
748 m_treeCtrl->CollapseAndReset(m_treeCtrl->GetRootItem());
749 }
750
751 void MyFrame::OnEnsureVisible(wxCommandEvent& WXUNUSED(event))
752 {
753 m_treeCtrl->DoEnsureVisible();
754 }
755
756 void MyFrame::OnSetFocus(wxCommandEvent& WXUNUSED(event))
757 {
758 m_treeCtrl->SetFocus();
759 }
760
761 void MyFrame::OnInsertItem(wxCommandEvent& WXUNUSED(event))
762 {
763 int image = wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_File : -1;
764 m_treeCtrl->InsertItem(m_treeCtrl->GetRootItem(), image, wxT("2nd item"));
765 }
766
767 void MyFrame::OnAddItem(wxCommandEvent& WXUNUSED(event))
768 {
769 static int s_num = 0;
770
771 wxString text;
772 text.Printf(wxT("Item #%d"), ++s_num);
773
774 m_treeCtrl->AppendItem(m_treeCtrl->GetRootItem(),
775 text /*,
776 MyTreeCtrl::TreeCtrlIcon_File */ );
777 }
778
779 void MyFrame::OnIncIndent(wxCommandEvent& WXUNUSED(event))
780 {
781 unsigned int indent = m_treeCtrl->GetIndent();
782 if (indent < 100)
783 m_treeCtrl->SetIndent( indent+5 );
784 }
785
786 void MyFrame::OnDecIndent(wxCommandEvent& WXUNUSED(event))
787 {
788 unsigned int indent = m_treeCtrl->GetIndent();
789 if (indent > 10)
790 m_treeCtrl->SetIndent( indent-5 );
791 }
792
793 void MyFrame::OnIncSpacing(wxCommandEvent& WXUNUSED(event))
794 {
795 unsigned int indent = m_treeCtrl->GetSpacing();
796 if (indent < 100)
797 {
798 m_treeCtrl->SetSpacing( indent+5 );
799 m_treeCtrl->Refresh();
800 }
801 }
802
803 void MyFrame::OnDecSpacing(wxCommandEvent& WXUNUSED(event))
804 {
805 unsigned int indent = m_treeCtrl->GetSpacing();
806 if (indent > 10)
807 {
808 m_treeCtrl->SetSpacing( indent-5 );
809 m_treeCtrl->Refresh();
810 }
811 }
812
813 void MyFrame::OnToggleIcon(wxCommandEvent& WXUNUSED(event))
814 {
815 wxTreeItemId item = m_treeCtrl->GetFocusedItem();
816
817 CHECK_ITEM( item );
818
819 m_treeCtrl->DoToggleIcon(item);
820 }
821
822 void MyFrame::OnToggleState(wxCommandEvent& WXUNUSED(event))
823 {
824 wxTreeItemId item = m_treeCtrl->GetFocusedItem();
825
826 CHECK_ITEM( item );
827
828 m_treeCtrl->DoToggleState(item);
829 }
830
831 void MyFrame::DoShowFirstOrLast(TreeFunc0_t pfn, const wxString& label)
832 {
833 const wxTreeItemId item = (m_treeCtrl->*pfn)();
834
835 if ( !item.IsOk() )
836 {
837 wxLogMessage("There is no %s item", label);
838 }
839 else
840 {
841 wxLogMessage("The %s item is \"%s\"",
842 label, m_treeCtrl->GetItemText(item));
843 }
844 }
845
846 void MyFrame::DoShowRelativeItem(TreeFunc1_t pfn, const wxString& label)
847 {
848 wxTreeItemId item = m_treeCtrl->GetFocusedItem();
849
850 CHECK_ITEM( item );
851
852 if ((pfn == (TreeFunc1_t) &wxTreeCtrl::GetPrevVisible
853 || pfn == (TreeFunc1_t) &wxTreeCtrl::GetNextVisible)
854 && !m_treeCtrl->IsVisible(item))
855 {
856 wxLogMessage("The selected item must be visible.");
857 return;
858 }
859
860 wxTreeItemId new_item = (m_treeCtrl->*pfn)(item);
861
862 if ( !new_item.IsOk() )
863 {
864 wxLogMessage("There is no %s item", label);
865 }
866 else
867 {
868 wxLogMessage("The %s item is \"%s\"",
869 label, m_treeCtrl->GetItemText(new_item));
870 }
871 }
872
873 void MyFrame::OnScrollTo(wxCommandEvent& WXUNUSED(event))
874 {
875 // scroll to the last but one top level child
876 wxTreeItemId item = m_treeCtrl->GetPrevSibling(
877 m_treeCtrl->GetLastChild(
878 m_treeCtrl->GetRootItem()));
879 CHECK_ITEM( item );
880
881 m_treeCtrl->ScrollTo(item);
882 }
883
884 void MyFrame::OnSelectLast(wxCommandEvent& WXUNUSED(event))
885 {
886 // select the very last item of the tree
887 wxTreeItemId item = m_treeCtrl->GetRootItem();
888 for ( ;; )
889 {
890 wxTreeItemId itemChild = m_treeCtrl->GetLastChild(item);
891 if ( !itemChild.IsOk() )
892 break;
893
894 item = itemChild;
895 }
896
897 CHECK_ITEM( item );
898
899 m_treeCtrl->SelectItem(item);
900 }
901
902 void MyFrame::OnSetFgColour(wxCommandEvent& WXUNUSED(event))
903 {
904 wxColour col = wxGetColourFromUser(this, m_treeCtrl->GetForegroundColour());
905 if ( col.Ok() )
906 m_treeCtrl->SetForegroundColour(col);
907 }
908
909 void MyFrame::OnSetBgColour(wxCommandEvent& WXUNUSED(event))
910 {
911 wxColour col = wxGetColourFromUser(this, m_treeCtrl->GetBackgroundColour());
912 if ( col.Ok() )
913 m_treeCtrl->SetBackgroundColour(col);
914 }
915
916 // MyTreeCtrl implementation
917 #if USE_GENERIC_TREECTRL
918 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl, wxGenericTreeCtrl)
919 #else
920 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl, wxTreeCtrl)
921 #endif
922
923 MyTreeCtrl::MyTreeCtrl(wxWindow *parent, const wxWindowID id,
924 const wxPoint& pos, const wxSize& size,
925 long style)
926 : wxTreeCtrl(parent, id, pos, size, style),
927 m_alternateImages(false),
928 m_alternateStates(false)
929 {
930 m_reverseSort = false;
931
932 CreateImageList();
933 CreateStateImageList();
934
935 // Add some items to the tree
936 AddTestItemsToTree(NUM_CHILDREN_PER_LEVEL, NUM_LEVELS);
937 }
938
939 void MyTreeCtrl::CreateImageList(int size)
940 {
941 if ( size == -1 )
942 {
943 SetImageList(NULL);
944 return;
945 }
946 if ( size == 0 )
947 size = m_imageSize;
948 else
949 m_imageSize = size;
950
951 // Make an image list containing small icons
952 wxImageList *images = new wxImageList(size, size, true);
953
954 // should correspond to TreeCtrlIcon_xxx enum
955 wxBusyCursor wait;
956 wxIcon icons[5];
957
958 if (m_alternateImages)
959 {
960 icons[TreeCtrlIcon_File] = wxIcon(icon1_xpm);
961 icons[TreeCtrlIcon_FileSelected] = wxIcon(icon2_xpm);
962 icons[TreeCtrlIcon_Folder] = wxIcon(icon3_xpm);
963 icons[TreeCtrlIcon_FolderSelected] = wxIcon(icon4_xpm);
964 icons[TreeCtrlIcon_FolderOpened] = wxIcon(icon5_xpm);
965 }
966 else
967 {
968 wxSize iconSize(size, size);
969
970 icons[TreeCtrlIcon_File] =
971 icons[TreeCtrlIcon_FileSelected] = wxArtProvider::GetIcon(wxART_NORMAL_FILE, wxART_LIST, iconSize);
972 icons[TreeCtrlIcon_Folder] =
973 icons[TreeCtrlIcon_FolderSelected] =
974 icons[TreeCtrlIcon_FolderOpened] = wxArtProvider::GetIcon(wxART_FOLDER, wxART_LIST, iconSize);
975 }
976
977 for ( size_t i = 0; i < WXSIZEOF(icons); i++ )
978 {
979 int sizeOrig = icons[0].GetWidth();
980 if ( size == sizeOrig )
981 {
982 images->Add(icons[i]);
983 }
984 else
985 {
986 images->Add(wxBitmap(wxBitmap(icons[i]).ConvertToImage().Rescale(size, size)));
987 }
988 }
989
990 AssignImageList(images);
991 }
992
993 void MyTreeCtrl::CreateStateImageList(bool del)
994 {
995 if ( del )
996 {
997 SetStateImageList(NULL);
998 return;
999 }
1000
1001 wxImageList *states;
1002 wxBusyCursor wait;
1003
1004 if (m_alternateStates)
1005 {
1006 wxIcon icons[5];
1007 icons[0] = wxIcon(state1_xpm); // yellow
1008 icons[1] = wxIcon(state2_xpm); // green
1009 icons[2] = wxIcon(state3_xpm); // red
1010 icons[3] = wxIcon(state4_xpm); // blue
1011 icons[4] = wxIcon(state5_xpm); // black
1012
1013 int width = icons[0].GetWidth(),
1014 height = icons[0].GetHeight();
1015
1016 // Make an state image list containing small icons
1017 states = new wxImageList(width, height, true);
1018
1019 for ( size_t i = 0; i < WXSIZEOF(icons); i++ )
1020 states->Add(icons[i]);
1021 }
1022 else
1023 {
1024 wxIcon icons[2];
1025 icons[0] = wxIcon(unchecked_xpm);
1026 icons[1] = wxIcon(checked_xpm);
1027
1028 int width = icons[0].GetWidth(),
1029 height = icons[0].GetHeight();
1030
1031 // Make an state image list containing small icons
1032 states = new wxImageList(width, height, true);
1033
1034 for ( size_t i = 0; i < WXSIZEOF(icons); i++ )
1035 states->Add(icons[i]);
1036 }
1037
1038 AssignStateImageList(states);
1039 }
1040
1041 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
1042 void MyTreeCtrl::CreateButtonsImageList(int size)
1043 {
1044 if ( size == -1 )
1045 {
1046 SetButtonsImageList(NULL);
1047 return;
1048 }
1049
1050 // Make an image list containing small icons
1051 wxImageList *images = new wxImageList(size, size, true);
1052
1053 // should correspond to TreeCtrlIcon_xxx enum
1054 wxBusyCursor wait;
1055 wxIcon icons[4];
1056
1057 if (m_alternateImages)
1058 {
1059 icons[0] = wxIcon(icon3_xpm); // closed
1060 icons[1] = wxIcon(icon3_xpm); // closed, selected
1061 icons[2] = wxIcon(icon5_xpm); // open
1062 icons[3] = wxIcon(icon5_xpm); // open, selected
1063 }
1064 else
1065 {
1066 wxSize iconSize(size, size);
1067
1068 icons[0] = // closed
1069 icons[1] = wxArtProvider::GetIcon(wxART_FOLDER, wxART_LIST, iconSize); // closed, selected
1070 icons[2] = // open
1071 icons[3] = wxArtProvider::GetIcon(wxART_FOLDER_OPEN, wxART_LIST, iconSize);// open, selected
1072 }
1073
1074 for ( size_t i = 0; i < WXSIZEOF(icons); i++ )
1075 {
1076 int sizeOrig = icons[i].GetWidth();
1077 if ( size == sizeOrig )
1078 {
1079 images->Add(icons[i]);
1080 }
1081 else
1082 {
1083 images->Add(wxBitmap(wxBitmap(icons[i]).ConvertToImage().Rescale(size, size)));
1084 }
1085 }
1086
1087 AssignButtonsImageList(images);
1088 #else
1089 void MyTreeCtrl::CreateButtonsImageList(int WXUNUSED(size))
1090 {
1091 #endif
1092 }
1093
1094 int MyTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
1095 const wxTreeItemId& item2)
1096 {
1097 if ( m_reverseSort )
1098 {
1099 // just exchange 1st and 2nd items
1100 return wxTreeCtrl::OnCompareItems(item2, item1);
1101 }
1102 else
1103 {
1104 return wxTreeCtrl::OnCompareItems(item1, item2);
1105 }
1106 }
1107
1108 void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId& idParent,
1109 size_t numChildren,
1110 size_t depth,
1111 size_t folder)
1112 {
1113 if ( depth > 0 )
1114 {
1115 bool hasChildren = depth > 1;
1116
1117 wxString str;
1118 for ( size_t n = 0; n < numChildren; n++ )
1119 {
1120 // at depth 1 elements won't have any more children
1121 if ( hasChildren )
1122 str.Printf(wxT("%s child %u"), wxT("Folder"), unsigned(n + 1));
1123 else
1124 str.Printf(wxT("%s child %u.%u"), wxT("File"), unsigned(folder), unsigned(n + 1));
1125
1126 // here we pass to AppendItem() normal and selected item images (we
1127 // suppose that selected image follows the normal one in the enum)
1128 int image, imageSel;
1129 if ( wxGetApp().ShowImages() )
1130 {
1131 image = depth == 1 ? TreeCtrlIcon_File : TreeCtrlIcon_Folder;
1132 imageSel = image + 1;
1133 }
1134 else
1135 {
1136 image = imageSel = -1;
1137 }
1138 wxTreeItemId id = AppendItem(idParent, str, image, imageSel,
1139 new MyTreeItemData(str));
1140
1141 if ( wxGetApp().ShowStates() )
1142 SetItemState(id, 0);
1143
1144 // and now we also set the expanded one (only for the folders)
1145 if ( hasChildren && wxGetApp().ShowImages() )
1146 {
1147 SetItemImage(id, TreeCtrlIcon_FolderOpened,
1148 wxTreeItemIcon_Expanded);
1149 }
1150
1151 // remember the last child for OnEnsureVisible()
1152 if ( !hasChildren && n == numChildren - 1 )
1153 {
1154 m_lastItem = id;
1155 }
1156
1157 AddItemsRecursively(id, numChildren, depth - 1, n + 1);
1158 }
1159 }
1160 //else: done!
1161 }
1162
1163 void MyTreeCtrl::AddTestItemsToTree(size_t numChildren,
1164 size_t depth)
1165 {
1166 int image = wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_Folder : -1;
1167 wxTreeItemId rootId = AddRoot(wxT("Root"),
1168 image, image,
1169 new MyTreeItemData(wxT("Root item")));
1170 if ( !HasFlag(wxTR_HIDE_ROOT) && image != -1 )
1171 {
1172 SetItemImage(rootId, TreeCtrlIcon_FolderOpened, wxTreeItemIcon_Expanded);
1173 }
1174
1175 AddItemsRecursively(rootId, numChildren, depth, 0);
1176
1177 // set some colours/fonts for testing
1178 if ( !HasFlag(wxTR_HIDE_ROOT) )
1179 SetItemFont(rootId, *wxITALIC_FONT);
1180
1181 wxTreeItemIdValue cookie;
1182 wxTreeItemId id = GetFirstChild(rootId, cookie);
1183 SetItemTextColour(id, *wxBLUE);
1184
1185 id = GetNextChild(rootId, cookie);
1186 if ( id )
1187 id = GetNextChild(rootId, cookie);
1188 if ( id )
1189 {
1190 SetItemTextColour(id, *wxRED);
1191 SetItemBackgroundColour(id, *wxLIGHT_GREY);
1192 }
1193 }
1194
1195 void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId& idParent,
1196 wxTreeItemIdValue cookie)
1197 {
1198 wxTreeItemId id;
1199
1200 if ( !cookie )
1201 id = GetFirstChild(idParent, cookie);
1202 else
1203 id = GetNextChild(idParent, cookie);
1204
1205 if ( !id.IsOk() )
1206 return;
1207
1208 wxString text = GetItemText(id);
1209 wxLogMessage(text);
1210
1211 if (ItemHasChildren(id))
1212 GetItemsRecursively(id);
1213
1214 GetItemsRecursively(idParent, cookie);
1215 }
1216
1217 void MyTreeCtrl::DoToggleIcon(const wxTreeItemId& item)
1218 {
1219 int image = GetItemImage(item) == TreeCtrlIcon_Folder
1220 ? TreeCtrlIcon_File
1221 : TreeCtrlIcon_Folder;
1222 SetItemImage(item, image, wxTreeItemIcon_Normal);
1223
1224 image = GetItemImage(item, wxTreeItemIcon_Selected) == TreeCtrlIcon_FolderSelected
1225 ? TreeCtrlIcon_FileSelected
1226 : TreeCtrlIcon_FolderSelected;
1227 SetItemImage(item, image, wxTreeItemIcon_Selected);
1228 }
1229
1230 void MyTreeCtrl::DoToggleState(const wxTreeItemId& item)
1231 {
1232 if ( m_alternateStates )
1233 {
1234 // sets random state unlike current
1235 int state = GetItemState(item);
1236 int nState;
1237
1238 srand (time(NULL));
1239 do {
1240 nState = rand() % GetStateImageList()->GetImageCount();
1241 } while (nState == state);
1242
1243 SetItemState(item, nState);
1244 }
1245 else
1246 {
1247 // we have only 2 checkbox states, so next state will be reversed
1248 SetItemState(item, wxTREE_ITEMSTATE_NEXT);
1249 }
1250 }
1251
1252 void MyTreeCtrl::DoResetBrokenStateImages(const wxTreeItemId& idParent,
1253 wxTreeItemIdValue cookie, int state)
1254 {
1255 wxTreeItemId id;
1256
1257 if ( !cookie )
1258 id = GetFirstChild(idParent, cookie);
1259 else
1260 id = GetNextChild(idParent, cookie);
1261
1262 if ( !id.IsOk() )
1263 return;
1264
1265 int curState = GetItemState(id);
1266 if ( curState != wxTREE_ITEMSTATE_NONE && curState > state )
1267 SetItemState(id, state);
1268
1269 if (ItemHasChildren(id))
1270 DoResetBrokenStateImages(id, 0, state);
1271
1272 DoResetBrokenStateImages(idParent, cookie, state);
1273 }
1274
1275 void MyTreeCtrl::LogEvent(const wxChar *name, const wxTreeEvent& event)
1276 {
1277 wxTreeItemId item = event.GetItem();
1278 wxString text;
1279 if ( item.IsOk() )
1280 text << wxT('"') << GetItemText(item).c_str() << wxT('"');
1281 else
1282 text = wxT("invalid item");
1283 wxLogMessage(wxT("%s(%s)"), name, text.c_str());
1284 }
1285
1286 // avoid repetition
1287 #define TREE_EVENT_HANDLER(name) \
1288 void MyTreeCtrl::name(wxTreeEvent& event) \
1289 { \
1290 LogEvent(wxT(#name), event); \
1291 SetLastItem(wxTreeItemId()); \
1292 event.Skip(); \
1293 }
1294
1295 TREE_EVENT_HANDLER(OnBeginRDrag)
1296 TREE_EVENT_HANDLER(OnDeleteItem)
1297 TREE_EVENT_HANDLER(OnGetInfo)
1298 TREE_EVENT_HANDLER(OnSetInfo)
1299 TREE_EVENT_HANDLER(OnItemExpanded)
1300 TREE_EVENT_HANDLER(OnItemExpanding)
1301 TREE_EVENT_HANDLER(OnItemCollapsed)
1302 TREE_EVENT_HANDLER(OnSelChanged)
1303 TREE_EVENT_HANDLER(OnSelChanging)
1304
1305 #undef TREE_EVENT_HANDLER
1306
1307 void LogKeyEvent(const wxChar *name, const wxKeyEvent& event)
1308 {
1309 wxString key;
1310 long keycode = event.GetKeyCode();
1311 {
1312 switch ( keycode )
1313 {
1314 case WXK_BACK: key = wxT("BACK"); break;
1315 case WXK_TAB: key = wxT("TAB"); break;
1316 case WXK_RETURN: key = wxT("RETURN"); break;
1317 case WXK_ESCAPE: key = wxT("ESCAPE"); break;
1318 case WXK_SPACE: key = wxT("SPACE"); break;
1319 case WXK_DELETE: key = wxT("DELETE"); break;
1320 case WXK_START: key = wxT("START"); break;
1321 case WXK_LBUTTON: key = wxT("LBUTTON"); break;
1322 case WXK_RBUTTON: key = wxT("RBUTTON"); break;
1323 case WXK_CANCEL: key = wxT("CANCEL"); break;
1324 case WXK_MBUTTON: key = wxT("MBUTTON"); break;
1325 case WXK_CLEAR: key = wxT("CLEAR"); break;
1326 case WXK_SHIFT: key = wxT("SHIFT"); break;
1327 case WXK_ALT: key = wxT("ALT"); break;
1328 case WXK_CONTROL: key = wxT("CONTROL"); break;
1329 case WXK_MENU: key = wxT("MENU"); break;
1330 case WXK_PAUSE: key = wxT("PAUSE"); break;
1331 case WXK_CAPITAL: key = wxT("CAPITAL"); break;
1332 case WXK_END: key = wxT("END"); break;
1333 case WXK_HOME: key = wxT("HOME"); break;
1334 case WXK_LEFT: key = wxT("LEFT"); break;
1335 case WXK_UP: key = wxT("UP"); break;
1336 case WXK_RIGHT: key = wxT("RIGHT"); break;
1337 case WXK_DOWN: key = wxT("DOWN"); break;
1338 case WXK_SELECT: key = wxT("SELECT"); break;
1339 case WXK_PRINT: key = wxT("PRINT"); break;
1340 case WXK_EXECUTE: key = wxT("EXECUTE"); break;
1341 case WXK_SNAPSHOT: key = wxT("SNAPSHOT"); break;
1342 case WXK_INSERT: key = wxT("INSERT"); break;
1343 case WXK_HELP: key = wxT("HELP"); break;
1344 case WXK_NUMPAD0: key = wxT("NUMPAD0"); break;
1345 case WXK_NUMPAD1: key = wxT("NUMPAD1"); break;
1346 case WXK_NUMPAD2: key = wxT("NUMPAD2"); break;
1347 case WXK_NUMPAD3: key = wxT("NUMPAD3"); break;
1348 case WXK_NUMPAD4: key = wxT("NUMPAD4"); break;
1349 case WXK_NUMPAD5: key = wxT("NUMPAD5"); break;
1350 case WXK_NUMPAD6: key = wxT("NUMPAD6"); break;
1351 case WXK_NUMPAD7: key = wxT("NUMPAD7"); break;
1352 case WXK_NUMPAD8: key = wxT("NUMPAD8"); break;
1353 case WXK_NUMPAD9: key = wxT("NUMPAD9"); break;
1354 case WXK_MULTIPLY: key = wxT("MULTIPLY"); break;
1355 case WXK_ADD: key = wxT("ADD"); break;
1356 case WXK_SEPARATOR: key = wxT("SEPARATOR"); break;
1357 case WXK_SUBTRACT: key = wxT("SUBTRACT"); break;
1358 case WXK_DECIMAL: key = wxT("DECIMAL"); break;
1359 case WXK_DIVIDE: key = wxT("DIVIDE"); break;
1360 case WXK_F1: key = wxT("F1"); break;
1361 case WXK_F2: key = wxT("F2"); break;
1362 case WXK_F3: key = wxT("F3"); break;
1363 case WXK_F4: key = wxT("F4"); break;
1364 case WXK_F5: key = wxT("F5"); break;
1365 case WXK_F6: key = wxT("F6"); break;
1366 case WXK_F7: key = wxT("F7"); break;
1367 case WXK_F8: key = wxT("F8"); break;
1368 case WXK_F9: key = wxT("F9"); break;
1369 case WXK_F10: key = wxT("F10"); break;
1370 case WXK_F11: key = wxT("F11"); break;
1371 case WXK_F12: key = wxT("F12"); break;
1372 case WXK_F13: key = wxT("F13"); break;
1373 case WXK_F14: key = wxT("F14"); break;
1374 case WXK_F15: key = wxT("F15"); break;
1375 case WXK_F16: key = wxT("F16"); break;
1376 case WXK_F17: key = wxT("F17"); break;
1377 case WXK_F18: key = wxT("F18"); break;
1378 case WXK_F19: key = wxT("F19"); break;
1379 case WXK_F20: key = wxT("F20"); break;
1380 case WXK_F21: key = wxT("F21"); break;
1381 case WXK_F22: key = wxT("F22"); break;
1382 case WXK_F23: key = wxT("F23"); break;
1383 case WXK_F24: key = wxT("F24"); break;
1384 case WXK_NUMLOCK: key = wxT("NUMLOCK"); break;
1385 case WXK_SCROLL: key = wxT("SCROLL"); break;
1386 case WXK_PAGEUP: key = wxT("PAGEUP"); break;
1387 case WXK_PAGEDOWN: key = wxT("PAGEDOWN"); break;
1388 case WXK_NUMPAD_SPACE: key = wxT("NUMPAD_SPACE"); break;
1389 case WXK_NUMPAD_TAB: key = wxT("NUMPAD_TAB"); break;
1390 case WXK_NUMPAD_ENTER: key = wxT("NUMPAD_ENTER"); break;
1391 case WXK_NUMPAD_F1: key = wxT("NUMPAD_F1"); break;
1392 case WXK_NUMPAD_F2: key = wxT("NUMPAD_F2"); break;
1393 case WXK_NUMPAD_F3: key = wxT("NUMPAD_F3"); break;
1394 case WXK_NUMPAD_F4: key = wxT("NUMPAD_F4"); break;
1395 case WXK_NUMPAD_HOME: key = wxT("NUMPAD_HOME"); break;
1396 case WXK_NUMPAD_LEFT: key = wxT("NUMPAD_LEFT"); break;
1397 case WXK_NUMPAD_UP: key = wxT("NUMPAD_UP"); break;
1398 case WXK_NUMPAD_RIGHT: key = wxT("NUMPAD_RIGHT"); break;
1399 case WXK_NUMPAD_DOWN: key = wxT("NUMPAD_DOWN"); break;
1400 case WXK_NUMPAD_PAGEUP: key = wxT("NUMPAD_PAGEUP"); break;
1401 case WXK_NUMPAD_PAGEDOWN: key = wxT("NUMPAD_PAGEDOWN"); break;
1402 case WXK_NUMPAD_END: key = wxT("NUMPAD_END"); break;
1403 case WXK_NUMPAD_BEGIN: key = wxT("NUMPAD_BEGIN"); break;
1404 case WXK_NUMPAD_INSERT: key = wxT("NUMPAD_INSERT"); break;
1405 case WXK_NUMPAD_DELETE: key = wxT("NUMPAD_DELETE"); break;
1406 case WXK_NUMPAD_EQUAL: key = wxT("NUMPAD_EQUAL"); break;
1407 case WXK_NUMPAD_MULTIPLY: key = wxT("NUMPAD_MULTIPLY"); break;
1408 case WXK_NUMPAD_ADD: key = wxT("NUMPAD_ADD"); break;
1409 case WXK_NUMPAD_SEPARATOR: key = wxT("NUMPAD_SEPARATOR"); break;
1410 case WXK_NUMPAD_SUBTRACT: key = wxT("NUMPAD_SUBTRACT"); break;
1411 case WXK_NUMPAD_DECIMAL: key = wxT("NUMPAD_DECIMAL"); break;
1412
1413 default:
1414 {
1415 if ( keycode < 128 && wxIsprint((int)keycode) )
1416 key.Printf(wxT("'%c'"), (char)keycode);
1417 else if ( keycode > 0 && keycode < 27 )
1418 key.Printf(_("Ctrl-%c"), wxT('A') + keycode - 1);
1419 else
1420 key.Printf(wxT("unknown (%ld)"), keycode);
1421 }
1422 }
1423 }
1424
1425 wxLogMessage( wxT("%s event: %s (flags = %c%c%c%c)"),
1426 name,
1427 key.c_str(),
1428 event.ControlDown() ? wxT('C') : wxT('-'),
1429 event.AltDown() ? wxT('A') : wxT('-'),
1430 event.ShiftDown() ? wxT('S') : wxT('-'),
1431 event.MetaDown() ? wxT('M') : wxT('-'));
1432 }
1433
1434 void MyTreeCtrl::OnTreeKeyDown(wxTreeEvent& event)
1435 {
1436 LogKeyEvent(wxT("Tree key down "), event.GetKeyEvent());
1437
1438 event.Skip();
1439 }
1440
1441 void MyTreeCtrl::OnBeginDrag(wxTreeEvent& event)
1442 {
1443 // need to explicitly allow drag
1444 if ( event.GetItem() != GetRootItem() )
1445 {
1446 m_draggedItem = event.GetItem();
1447
1448 wxPoint clientpt = event.GetPoint();
1449 wxPoint screenpt = ClientToScreen(clientpt);
1450
1451 wxLogMessage(wxT("OnBeginDrag: started dragging %s at screen coords (%i,%i)"),
1452 GetItemText(m_draggedItem).c_str(),
1453 screenpt.x, screenpt.y);
1454
1455 event.Allow();
1456 }
1457 else
1458 {
1459 wxLogMessage(wxT("OnBeginDrag: this item can't be dragged."));
1460 }
1461 }
1462
1463 void MyTreeCtrl::OnEndDrag(wxTreeEvent& event)
1464 {
1465 wxTreeItemId itemSrc = m_draggedItem,
1466 itemDst = event.GetItem();
1467 m_draggedItem = (wxTreeItemId)0l;
1468
1469 // where to copy the item?
1470 if ( itemDst.IsOk() && !ItemHasChildren(itemDst) )
1471 {
1472 // copy to the parent then
1473 itemDst = GetItemParent(itemDst);
1474 }
1475
1476 if ( !itemDst.IsOk() )
1477 {
1478 wxLogMessage(wxT("OnEndDrag: can't drop here."));
1479
1480 return;
1481 }
1482
1483 wxString text = GetItemText(itemSrc);
1484 wxLogMessage(wxT("OnEndDrag: '%s' copied to '%s'."),
1485 text.c_str(), GetItemText(itemDst).c_str());
1486
1487 // just do append here - we could also insert it just before/after the item
1488 // on which it was dropped, but this requires slightly more work... we also
1489 // completely ignore the client data and icon of the old item but could
1490 // copy them as well.
1491 //
1492 // Finally, we only copy one item here but we might copy the entire tree if
1493 // we were dragging a folder.
1494 int image = wxGetApp().ShowImages() ? TreeCtrlIcon_File : -1;
1495 wxTreeItemId id = AppendItem(itemDst, text, image);
1496
1497 if ( wxGetApp().ShowStates() )
1498 SetItemState(id, GetItemState(itemSrc));
1499 }
1500
1501 void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent& event)
1502 {
1503 wxLogMessage(wxT("OnBeginLabelEdit"));
1504
1505 // for testing, prevent this item's label editing
1506 wxTreeItemId itemId = event.GetItem();
1507 if ( IsTestItem(itemId) )
1508 {
1509 wxMessageBox(wxT("You can't edit this item."));
1510
1511 event.Veto();
1512 }
1513 else if ( itemId == GetRootItem() )
1514 {
1515 // test that it is possible to change the text of the item being edited
1516 SetItemText(itemId, wxT("Editing root item"));
1517 }
1518 }
1519
1520 void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent& event)
1521 {
1522 wxLogMessage(wxT("OnEndLabelEdit"));
1523
1524 // don't allow anything except letters in the labels
1525 if ( !event.GetLabel().IsWord() )
1526 {
1527 wxMessageBox(wxT("The new label should be a single word."));
1528
1529 event.Veto();
1530 }
1531 }
1532
1533 void MyTreeCtrl::OnItemCollapsing(wxTreeEvent& event)
1534 {
1535 wxLogMessage(wxT("OnItemCollapsing"));
1536
1537 // for testing, prevent the user from collapsing the first child folder
1538 wxTreeItemId itemId = event.GetItem();
1539 if ( IsTestItem(itemId) )
1540 {
1541 wxMessageBox(wxT("You can't collapse this item."));
1542
1543 event.Veto();
1544 }
1545 }
1546
1547 void MyTreeCtrl::OnItemActivated(wxTreeEvent& event)
1548 {
1549 // show some info about this item
1550 wxTreeItemId itemId = event.GetItem();
1551 MyTreeItemData *item = (MyTreeItemData *)GetItemData(itemId);
1552
1553 if ( item != NULL )
1554 {
1555 item->ShowInfo(this);
1556 }
1557
1558 wxLogMessage(wxT("OnItemActivated"));
1559 }
1560
1561 void MyTreeCtrl::OnItemStateClick(wxTreeEvent& event)
1562 {
1563 // toggle item state
1564 wxTreeItemId itemId = event.GetItem();
1565 DoToggleState(itemId);
1566
1567 wxLogMessage(wxT("Item \"%s\" state changed to %d"),
1568 GetItemText(itemId), GetItemState(itemId));
1569 }
1570
1571 void MyTreeCtrl::OnItemMenu(wxTreeEvent& event)
1572 {
1573 wxTreeItemId itemId = event.GetItem();
1574 MyTreeItemData *item = itemId.IsOk() ? (MyTreeItemData *)GetItemData(itemId)
1575 : NULL;
1576 wxPoint clientpt = event.GetPoint();
1577 wxPoint screenpt = ClientToScreen(clientpt);
1578
1579 wxLogMessage(wxT("OnItemMenu for item \"%s\" at screen coords (%i, %i)"),
1580 item ? item->GetDesc() : wxT(""), screenpt.x, screenpt.y);
1581
1582 ShowMenu(itemId, clientpt);
1583 event.Skip();
1584 }
1585
1586 void MyTreeCtrl::OnContextMenu(wxContextMenuEvent& event)
1587 {
1588 wxPoint pt = event.GetPosition();
1589
1590 wxLogMessage(wxT("OnContextMenu at screen coords (%i, %i)"), pt.x, pt.y);
1591
1592 event.Skip();
1593 }
1594
1595 void MyTreeCtrl::ShowMenu(wxTreeItemId id, const wxPoint& pt)
1596 {
1597 wxString title;
1598 if ( id.IsOk() )
1599 {
1600 title << wxT("Menu for ") << GetItemText(id);
1601 }
1602 else
1603 {
1604 title = wxT("Menu for no particular item");
1605 }
1606
1607 #if wxUSE_MENUS
1608 wxMenu menu(title);
1609 menu.Append(TreeTest_About, wxT("&About..."));
1610 menu.AppendSeparator();
1611 menu.Append(TreeTest_Highlight, wxT("&Highlight item"));
1612 menu.Append(TreeTest_Dump, wxT("&Dump"));
1613
1614 PopupMenu(&menu, pt);
1615 #endif // wxUSE_MENUS
1616 }
1617
1618 void MyTreeCtrl::OnItemRClick(wxTreeEvent& event)
1619 {
1620 wxTreeItemId itemId = event.GetItem();
1621 MyTreeItemData *item = itemId.IsOk() ? (MyTreeItemData *)GetItemData(itemId)
1622 : NULL;
1623
1624 wxLogMessage(wxT("Item \"%s\" right clicked"), item ? item->GetDesc()
1625 : wxT(""));
1626
1627 event.Skip();
1628 }
1629
1630 void MyTreeCtrl::OnRMouseDown(wxMouseEvent& event)
1631 {
1632 wxLogMessage(wxT("Right mouse button down"));
1633
1634 event.Skip();
1635 }
1636
1637 void MyTreeCtrl::OnRMouseUp(wxMouseEvent& event)
1638 {
1639 wxLogMessage(wxT("Right mouse button up"));
1640
1641 event.Skip();
1642 }
1643
1644 void MyTreeCtrl::OnRMouseDClick(wxMouseEvent& event)
1645 {
1646 wxTreeItemId id = HitTest(event.GetPosition());
1647 if ( !id )
1648 {
1649 wxLogMessage(wxT("No item under mouse"));
1650 }
1651 else
1652 {
1653 MyTreeItemData *item = (MyTreeItemData *)GetItemData(id);
1654 if ( item )
1655 {
1656 wxLogMessage(wxT("Item '%s' under mouse"), item->GetDesc());
1657 }
1658 }
1659
1660 event.Skip();
1661 }
1662
1663 static inline const wxChar *Bool2String(bool b)
1664 {
1665 return b ? wxT("") : wxT("not ");
1666 }
1667
1668 void MyTreeItemData::ShowInfo(wxTreeCtrl *tree)
1669 {
1670 wxLogMessage(wxT("Item '%s': %sselected, %sexpanded, %sbold,\n")
1671 wxT("%u children (%u immediately under this item)."),
1672 m_desc.c_str(),
1673 Bool2String(tree->IsSelected(GetId())),
1674 Bool2String(tree->IsExpanded(GetId())),
1675 Bool2String(tree->IsBold(GetId())),
1676 unsigned(tree->GetChildrenCount(GetId())),
1677 unsigned(tree->GetChildrenCount(GetId(), false)));
1678 }