]> git.saurik.com Git - wxWidgets.git/blame - samples/treectrl/treetest.cpp
make wxArtProvider pure virtual (I was not so on
[wxWidgets.git] / samples / treectrl / treetest.cpp
CommitLineData
457814b5 1/////////////////////////////////////////////////////////////////////////////
ec312631 2// Name: treetest.cpp
457814b5
JS
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__
ec312631
JS
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"
75cd9288 26 #include "wx/log.h"
457814b5
JS
27#endif
28
75cd9288 29#include "wx/colordlg.h"
3a5a2f56 30
91b8de8d 31#include "wx/image.h"
3a5a2f56 32#include "wx/imaglist.h"
457814b5
JS
33#include "wx/treectrl.h"
34
cf724bce
RR
35#include "math.h"
36
2c8e4738
VZ
37#ifdef __WIN32__
38 // this is not supported by native control
7cc8c988 39 #define NO_VARIABLE_HEIGHT
74b31181 40#endif
91b8de8d 41
ec312631 42#include "treetest.h"
9dfbf520 43
91b8de8d
RR
44// under Windows the icons are in the .rc file
45#ifndef __WXMSW__
2c8e4738
VZ
46 #include "icon1.xpm"
47 #include "icon2.xpm"
48 #include "icon3.xpm"
49 #include "icon4.xpm"
50 #include "icon5.xpm"
51 #include "mondrian.xpm"
91b8de8d
RR
52#endif
53
e1ee62bd 54// verify that the item is ok and insult the user if it is not
4693b20c
MB
55#define CHECK_ITEM( item ) if ( !item.IsOk() ) { \
56 wxMessageBox(wxT("Please select some item first!"), \
57 wxT("Tree sample error"), \
58 wxOK | wxICON_EXCLAMATION, \
59 this); \
60 return; \
5f939e78
VZ
61 }
62
d88a2485
VZ
63#define MENU_LINK(name) EVT_MENU(TreeTest_##name, MyFrame::On##name)
64
457814b5 65BEGIN_EVENT_TABLE(MyFrame, wxFrame)
7cc8c988
VZ
66 EVT_SIZE(MyFrame::OnSize)
67
618a5e38
RR
68 MENU_LINK(Quit)
69 MENU_LINK(About)
11ab1dfb 70
618a5e38
RR
71 MENU_LINK(TogButtons)
72 MENU_LINK(TogTwist)
73 MENU_LINK(TogLines)
74 MENU_LINK(TogEdit)
75 MENU_LINK(TogHideRoot)
76 MENU_LINK(TogRootLines)
77 MENU_LINK(TogBorder)
33984936 78 MENU_LINK(TogFullHighlight)
d88a2485
VZ
79 MENU_LINK(SetFgColour)
80 MENU_LINK(SetBgColour)
11ab1dfb
VZ
81 MENU_LINK(ResetStyle)
82
618a5e38 83 MENU_LINK(Dump)
9dfbf520 84#ifndef NO_MULTIPLE_SELECTION
618a5e38
RR
85 MENU_LINK(DumpSelected)
86 MENU_LINK(Select)
87 MENU_LINK(Unselect)
88 MENU_LINK(ToggleSel)
9dfbf520 89#endif // NO_MULTIPLE_SELECTION
618a5e38
RR
90 MENU_LINK(Rename)
91 MENU_LINK(Count)
92 MENU_LINK(CountRec)
93 MENU_LINK(Sort)
94 MENU_LINK(SortRev)
95 MENU_LINK(SetBold)
96 MENU_LINK(ClearBold)
97 MENU_LINK(Delete)
98 MENU_LINK(DeleteChildren)
99 MENU_LINK(DeleteAll)
100 MENU_LINK(Recreate)
101 MENU_LINK(ToggleImages)
102 MENU_LINK(ToggleButtons)
103 MENU_LINK(SetImageSize)
104 MENU_LINK(CollapseAndReset)
105 MENU_LINK(EnsureVisible)
106 MENU_LINK(AddItem)
107 MENU_LINK(InsertItem)
108 MENU_LINK(IncIndent)
109 MENU_LINK(DecIndent)
110 MENU_LINK(IncSpacing)
111 MENU_LINK(DecSpacing)
112 MENU_LINK(ToggleIcon)
113#undef MENU_LINK
114
457814b5
JS
115END_EVENT_TABLE()
116
484523cf
JS
117#if USE_GENERIC_TREECTRL
118BEGIN_EVENT_TABLE(MyTreeCtrl, wxGenericTreeCtrl)
119#else
457814b5 120BEGIN_EVENT_TABLE(MyTreeCtrl, wxTreeCtrl)
484523cf 121#endif
5f939e78
VZ
122 EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginDrag)
123 EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginRDrag)
5888ef1e 124 EVT_TREE_END_DRAG(TreeTest_Ctrl, MyTreeCtrl::OnEndDrag)
5f939e78
VZ
125 EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnBeginLabelEdit)
126 EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnEndLabelEdit)
127 EVT_TREE_DELETE_ITEM(TreeTest_Ctrl, MyTreeCtrl::OnDeleteItem)
74b31181 128#if 0 // there are so many of those that logging them causes flicker
5f939e78 129 EVT_TREE_GET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnGetInfo)
74b31181 130#endif
5f939e78
VZ
131 EVT_TREE_SET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnSetInfo)
132 EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanded)
133 EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanding)
134 EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsed)
135 EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsing)
e3d0aa77
VZ
136 EVT_TREE_ITEM_RIGHT_CLICK(TreeTest_Ctrl, MyTreeCtrl::OnItemRightClick)
137
138 EVT_RIGHT_UP(MyTreeCtrl::OnRMouseUp)
5f939e78
VZ
139 EVT_TREE_SEL_CHANGED(TreeTest_Ctrl, MyTreeCtrl::OnSelChanged)
140 EVT_TREE_SEL_CHANGING(TreeTest_Ctrl, MyTreeCtrl::OnSelChanging)
141 EVT_TREE_KEY_DOWN(TreeTest_Ctrl, MyTreeCtrl::OnTreeKeyDown)
142 EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl, MyTreeCtrl::OnItemActivated)
143 EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick)
457814b5
JS
144END_EVENT_TABLE()
145
146IMPLEMENT_APP(MyApp)
147
c89a6106 148bool MyApp::OnInit()
457814b5 149{
5f939e78 150 // Create the main frame window
4693b20c 151 MyFrame *frame = new MyFrame(wxT("wxTreeCtrl Test"), 50, 50, 450, 600);
457814b5 152
5f939e78
VZ
153 // Show the frame
154 frame->Show(TRUE);
155 SetTopWindow(frame);
457814b5 156
5f939e78 157 return TRUE;
3a5a2f56 158}
457814b5 159
457814b5 160
3a5a2f56
VZ
161// My frame constructor
162MyFrame::MyFrame(const wxString& title, int x, int y, int w, int h)
7cc8c988
VZ
163 : wxFrame((wxFrame *)NULL, -1, title, wxPoint(x, y), wxSize(w, h)),
164 m_treeCtrl(NULL), m_textCtrl(NULL)
3a5a2f56 165{
5f939e78
VZ
166 // This reduces flicker effects - even better would be to define
167 // OnEraseBackground to do nothing. When the tree control's scrollbars are
168 // show or hidden, the frame is sent a background erase event.
169 SetBackgroundColour(wxColour(255, 255, 255));
170
171 // Give it an icon
172 SetIcon(wxICON(mondrian));
173
801bb685 174#if wxUSE_MENUS
5f939e78
VZ
175 // Make a menubar
176 wxMenu *file_menu = new wxMenu,
618a5e38 177 *style_menu = new wxMenu,
5f939e78
VZ
178 *tree_menu = new wxMenu,
179 *item_menu = new wxMenu;
180
4693b20c 181 file_menu->Append(TreeTest_About, wxT("&About..."));
5f939e78 182 file_menu->AppendSeparator();
4693b20c 183 file_menu->Append(TreeTest_Quit, wxT("E&xit\tAlt-X"));
5f939e78 184
11ab1dfb
VZ
185 style_menu->Append(TreeTest_TogButtons, "Toggle &normal buttons", _T(""), TRUE);
186 style_menu->Append(TreeTest_TogTwist, "Toggle &twister buttons", _T(""), TRUE);
187 style_menu->Append(TreeTest_ToggleButtons, "Toggle image &buttons", _T(""), TRUE);
618a5e38 188 style_menu->AppendSeparator();
11ab1dfb
VZ
189 style_menu->Append(TreeTest_TogLines, "Toggle &connecting lines", _T(""), TRUE);
190 style_menu->Append(TreeTest_TogRootLines, "Toggle &lines at root", _T(""), TRUE);
191 style_menu->Append(TreeTest_TogHideRoot, "Toggle &hidden root", _T(""), TRUE);
192 style_menu->Append(TreeTest_TogBorder, "Toggle &item border", _T(""), TRUE);
193 style_menu->Append(TreeTest_TogFullHighlight, "Toggle &full row highlight", _T(""), TRUE);
194 style_menu->Append(TreeTest_TogEdit, "Toggle &edit mode", _T(""), TRUE);
7cc8c988 195#ifndef NO_MULTIPLE_SELECTION
11ab1dfb 196 style_menu->Append(TreeTest_ToggleSel, wxT("Toggle &selection mode"), _T(""), TRUE);
7cc8c988 197#endif // NO_MULTIPLE_SELECTION
11ab1dfb 198 style_menu->Append(TreeTest_ToggleImages, wxT("Toggle show ima&ges"), _T(""), TRUE);
4693b20c 199 style_menu->Append(TreeTest_SetImageSize, wxT("Set image si&ze..."));
d88a2485
VZ
200 style_menu->AppendSeparator();
201 style_menu->Append(TreeTest_SetFgColour, wxT("Set &foreground colour..."));
202 style_menu->Append(TreeTest_SetBgColour, wxT("Set &background colour..."));
11ab1dfb
VZ
203 style_menu->AppendSeparator();
204 style_menu->Append(TreeTest_ResetStyle, wxT("&Reset to default\tF10"));
618a5e38 205
5f939e78
VZ
206 tree_menu->Append(TreeTest_Recreate, "&Recreate the tree");
207 tree_menu->Append(TreeTest_CollapseAndReset, "C&ollapse and reset");
208 tree_menu->AppendSeparator();
209 tree_menu->Append(TreeTest_AddItem, "Append a &new item");
75c74ca0 210 tree_menu->Append(TreeTest_InsertItem, "&Insert a new item");
5f939e78
VZ
211 tree_menu->Append(TreeTest_Delete, "&Delete this item");
212 tree_menu->Append(TreeTest_DeleteChildren, "Delete &children");
213 tree_menu->Append(TreeTest_DeleteAll, "Delete &all items");
214 tree_menu->AppendSeparator();
5fd11f09
RR
215 tree_menu->Append(TreeTest_Count, "Count children of current item");
216 tree_menu->Append(TreeTest_CountRec, "Recursively count children of current item");
217 tree_menu->AppendSeparator();
5f939e78
VZ
218 tree_menu->Append(TreeTest_Sort, "Sort children of current item");
219 tree_menu->Append(TreeTest_SortRev, "Sort in reversed order");
220 tree_menu->AppendSeparator();
221 tree_menu->Append(TreeTest_EnsureVisible, "Make the last item &visible");
cf724bce
RR
222 tree_menu->AppendSeparator();
223 tree_menu->Append(TreeTest_IncIndent, "Add 5 points to indentation\tAlt-I");
224 tree_menu->Append(TreeTest_DecIndent, "Reduce indentation by 5 points\tAlt-R");
225 tree_menu->AppendSeparator();
226 tree_menu->Append(TreeTest_IncSpacing, "Add 5 points to spacing\tCtrl-I");
227 tree_menu->Append(TreeTest_DecSpacing, "Reduce spacing by 5 points\tCtrl-R");
5f939e78 228
91b8de8d 229 item_menu->Append(TreeTest_Dump, "&Dump item children");
5f939e78 230 item_menu->Append(TreeTest_Rename, "&Rename item...");
91b8de8d 231
5f939e78 232 item_menu->AppendSeparator();
618a5e38
RR
233 item_menu->Append(TreeTest_SetBold, "Make item &bold");
234 item_menu->Append(TreeTest_ClearBold, "Make item &not bold");
9dfbf520 235 item_menu->AppendSeparator();
618a5e38 236 item_menu->Append(TreeTest_ToggleIcon, "Toggle the item's &icon");
9dfbf520
VZ
237
238#ifndef NO_MULTIPLE_SELECTION
239 item_menu->AppendSeparator();
240 item_menu->Append(TreeTest_DumpSelected, "Dump selected items\tAlt-D");
241 item_menu->Append(TreeTest_Select, "Select current item\tAlt-S");
242 item_menu->Append(TreeTest_Unselect, "Unselect everything\tAlt-U");
7cc8c988 243#endif // NO_MULTIPLE_SELECTION
5f939e78
VZ
244
245 wxMenuBar *menu_bar = new wxMenuBar;
246 menu_bar->Append(file_menu, "&File");
618a5e38 247 menu_bar->Append(style_menu, "&Style");
5f939e78
VZ
248 menu_bar->Append(tree_menu, "&Tree");
249 menu_bar->Append(item_menu, "&Item");
250 SetMenuBar(menu_bar);
801bb685 251#endif // wxUSE_MENUS
2c8e4738 252
11ab1dfb 253 // create the controls
7cc8c988 254 m_textCtrl = new wxTextCtrl(this, -1, "",
5f939e78
VZ
255 wxDefaultPosition, wxDefaultSize,
256 wxTE_MULTILINE | wxSUNKEN_BORDER);
257
11ab1dfb
VZ
258 CreateTreeWithDefStyle();
259
260 menu_bar->Check(TreeTest_ToggleImages, TRUE);
261
5f939e78
VZ
262 // create a status bar with 3 panes
263 CreateStatusBar(3);
264 SetStatusText("", 0);
3a5a2f56 265
a367b9b3 266#ifdef __WXMOTIF__
5f939e78
VZ
267 // For some reason, we get a memcpy crash in wxLogStream::DoLogStream
268 // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc?
269 delete wxLog::SetActiveTarget(new wxLogStderr);
a367b9b3 270#else
5f939e78 271 // set our text control as the log target
7cc8c988 272 wxLogTextCtrl *logWindow = new wxLogTextCtrl(m_textCtrl);
5f939e78 273 delete wxLog::SetActiveTarget(logWindow);
a367b9b3 274#endif
457814b5
JS
275}
276
c89a6106 277MyFrame::~MyFrame()
457814b5 278{
5f939e78 279 delete wxLog::SetActiveTarget(NULL);
457814b5
JS
280}
281
11ab1dfb
VZ
282void MyFrame::CreateTreeWithDefStyle()
283{
284 long style = wxTR_DEFAULT_STYLE |
285#ifndef NO_VARIABLE_HEIGHT
286 wxTR_HAS_VARIABLE_ROW_HEIGHT |
287#endif
288 wxTR_EDIT_LABELS;
289
290 CreateTree(style | wxSUNKEN_BORDER);
291
292 // as we don't know what wxTR_DEFAULT_STYLE could contain, test for
293 // everything
294 wxMenuBar *mbar = GetMenuBar();
295 mbar->Check(TreeTest_TogButtons, (style & wxTR_HAS_BUTTONS) != 0);
296 mbar->Check(TreeTest_TogButtons, (style & wxTR_TWIST_BUTTONS) != 0);
297 mbar->Check(TreeTest_TogLines, (style & wxTR_NO_LINES) == 0);
298 mbar->Check(TreeTest_TogRootLines, (style & wxTR_LINES_AT_ROOT) != 0);
299 mbar->Check(TreeTest_TogHideRoot, (style & wxTR_HIDE_ROOT) != 0);
300 mbar->Check(TreeTest_TogEdit, (style & wxTR_EDIT_LABELS) != 0);
301 mbar->Check(TreeTest_TogBorder, (style & wxTR_ROW_LINES) != 0);
302 mbar->Check(TreeTest_TogFullHighlight, (style & wxTR_FULL_ROW_HIGHLIGHT) != 0);
303}
304
305void MyFrame::CreateTree(long style)
306{
307 m_treeCtrl = new MyTreeCtrl(this, TreeTest_Ctrl,
308 wxDefaultPosition, wxDefaultSize,
309 style);
310 Resize();
311}
312
313void MyFrame::TogStyle(int id, long flag)
618a5e38 314{
11ab1dfb
VZ
315 long style = m_treeCtrl->GetWindowStyle() ^ flag;
316
317 // most treectrl styles can't be changed on the fly using the native
318 // control and the tree must be recreated
319#ifndef __WXMSW__
320 m_treeCtrl->SetWindowStyle(style);
321#else // MSW
322 delete m_treeCtrl;
323 CreateTree(style);
324#endif // !MSW/MSW
325
326 GetMenuBar()->Check(id, (style & flag) != 0);
618a5e38
RR
327}
328
7cc8c988
VZ
329void MyFrame::OnSize(wxSizeEvent& event)
330{
331 if ( m_treeCtrl && m_textCtrl )
332 {
11ab1dfb 333 Resize();
7cc8c988
VZ
334 }
335
336 event.Skip();
337}
338
11ab1dfb 339void MyFrame::Resize()
7cc8c988 340{
11ab1dfb 341 wxSize size = GetClientSize();
7cc8c988
VZ
342 m_treeCtrl->SetSize(0, 0, size.x, 2*size.y/3);
343 m_textCtrl->SetSize(0, 2*size.y/3, size.x, size.y/3);
344}
345
3a5a2f56 346void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
457814b5 347{
5f939e78 348 Close(TRUE);
457814b5
JS
349}
350
3a5a2f56 351void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
457814b5 352{
5f939e78 353 wxMessageBox("Tree test sample\n"
5ea47806 354 "(c) Julian Smart 1997, Vadim Zeitlin 1998",
5f939e78
VZ
355 "About tree test",
356 wxOK | wxICON_INFORMATION, this);
e1ee62bd
VZ
357}
358
359void MyFrame::OnRename(wxCommandEvent& WXUNUSED(event))
360{
5f939e78 361 wxTreeItemId item = m_treeCtrl->GetSelection();
e1ee62bd 362
5f939e78 363 CHECK_ITEM( item );
e1ee62bd 364
5ea47806
VZ
365 // old code - now we edit in place
366#if 0
5f939e78
VZ
367 static wxString s_text;
368 s_text = wxGetTextFromUser("New name: ", "Tree sample question",
369 s_text, this);
370 if ( !s_text.IsEmpty() )
371 {
372 m_treeCtrl->SetItemText(item, s_text);
373 }
5ea47806
VZ
374#endif // 0
375
376 // TODO demonstrate creating a custom edit control...
377 (void)m_treeCtrl->EditLabel(item);
e1ee62bd
VZ
378}
379
5fd11f09
RR
380void MyFrame::OnCount(wxCommandEvent& WXUNUSED(event))
381{
382 wxTreeItemId item = m_treeCtrl->GetSelection();
383
384 CHECK_ITEM( item );
385
386 int i = m_treeCtrl->GetChildrenCount( item, FALSE );
75c74ca0 387
247e5b16 388 wxLogMessage(wxT("%d children"), i);
5fd11f09
RR
389}
390
391void MyFrame::OnCountRec(wxCommandEvent& WXUNUSED(event))
392{
393 wxTreeItemId item = m_treeCtrl->GetSelection();
394
395 CHECK_ITEM( item );
396
397 int i = m_treeCtrl->GetChildrenCount( item );
75c74ca0 398
247e5b16 399 wxLogMessage(wxT("%d children"), i);
5fd11f09
RR
400}
401
e1ee62bd
VZ
402void MyFrame::DoSort(bool reverse)
403{
5f939e78 404 wxTreeItemId item = m_treeCtrl->GetSelection();
e1ee62bd 405
5f939e78 406 CHECK_ITEM( item );
457814b5 407
5f939e78 408 m_treeCtrl->DoSortChildren(item, reverse);
457814b5
JS
409}
410
4832f7c0
VZ
411void MyFrame::OnDump(wxCommandEvent& WXUNUSED(event))
412{
5f939e78 413 wxTreeItemId root = m_treeCtrl->GetSelection();
e1ee62bd 414
5f939e78 415 CHECK_ITEM( root );
e1ee62bd 416
5f939e78 417 m_treeCtrl->GetItemsRecursively(root, -1);
4832f7c0
VZ
418}
419
9dfbf520
VZ
420#ifndef NO_MULTIPLE_SELECTION
421
11ab1dfb 422void MyFrame::OnToggleSel(wxCommandEvent& event)
7cc8c988 423{
11ab1dfb 424 TogStyle(event.GetId(), wxTR_MULTIPLE);
7cc8c988
VZ
425}
426
91b8de8d
RR
427void MyFrame::OnDumpSelected(wxCommandEvent& WXUNUSED(event))
428{
9dfbf520 429 wxArrayTreeItemIds array;
91b8de8d 430
9dfbf520 431 size_t count = m_treeCtrl->GetSelections(array);
247e5b16 432 wxLogMessage(wxT("%u items selected"), count);
91b8de8d 433
9dfbf520
VZ
434 for ( size_t n = 0; n < count; n++ )
435 {
4693b20c 436 wxLogMessage(wxT("\t%s"), m_treeCtrl->GetItemText(array.Item(n)).c_str());
9dfbf520
VZ
437 }
438}
439
440void MyFrame::OnSelect(wxCommandEvent& event)
441{
442 m_treeCtrl->SelectItem(m_treeCtrl->GetSelection());
443}
444
445void MyFrame::OnUnselect(wxCommandEvent& event)
446{
447 m_treeCtrl->UnselectAll();
91b8de8d
RR
448}
449
9dfbf520
VZ
450#endif // NO_MULTIPLE_SELECTION
451
add28c55
VZ
452void MyFrame::DoSetBold(bool bold)
453{
5f939e78 454 wxTreeItemId item = m_treeCtrl->GetSelection();
e1ee62bd 455
5f939e78 456 CHECK_ITEM( item );
e1ee62bd 457
5f939e78 458 m_treeCtrl->SetItemBold(item, bold);
add28c55
VZ
459}
460
ff5bf259
VZ
461void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event))
462{
5f939e78 463 wxTreeItemId item = m_treeCtrl->GetSelection();
e1ee62bd 464
5f939e78 465 CHECK_ITEM( item );
e1ee62bd 466
5f939e78 467 m_treeCtrl->Delete(item);
ff5bf259
VZ
468}
469
372edb9d
VZ
470void MyFrame::OnDeleteChildren(wxCommandEvent& WXUNUSED(event))
471{
5f939e78 472 wxTreeItemId item = m_treeCtrl->GetSelection();
e1ee62bd 473
5f939e78 474 CHECK_ITEM( item );
e1ee62bd 475
5f939e78 476 m_treeCtrl->DeleteChildren(item);
372edb9d
VZ
477}
478
ff5bf259
VZ
479void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event))
480{
5f939e78 481 m_treeCtrl->DeleteAllItems();
ff5bf259
VZ
482}
483
484void MyFrame::OnRecreate(wxCommandEvent& event)
485{
5f939e78 486 OnDeleteAll(event);
618a5e38 487 m_treeCtrl->AddTestItemsToTree(5, 2);
ff5bf259
VZ
488}
489
2c8e4738
VZ
490void MyFrame::OnSetImageSize(wxCommandEvent& event)
491{
618a5e38 492 int size = wxGetNumberFromUser("Enter the size for the images to use",
2c8e4738
VZ
493 "Size: ",
494 "TreeCtrl sample",
618a5e38 495 m_treeCtrl->ImageSize());
2c8e4738
VZ
496 if ( size == -1 )
497 return;
498
618a5e38
RR
499 m_treeCtrl->CreateImageList(size);
500 wxGetApp().SetShowImages(TRUE);
2c8e4738
VZ
501}
502
503void MyFrame::OnToggleImages(wxCommandEvent& event)
504{
f6bcfd97
BP
505 if ( wxGetApp().ShowImages() )
506 {
507 m_treeCtrl->CreateImageList(-1);
f6bcfd97
BP
508 wxGetApp().SetShowImages(FALSE);
509 }
510 else
511 {
618a5e38 512 m_treeCtrl->CreateImageList(0);
f6bcfd97
BP
513 wxGetApp().SetShowImages(TRUE);
514 }
618a5e38 515}
2c8e4738 516
618a5e38
RR
517void MyFrame::OnToggleButtons(wxCommandEvent& event)
518{
a3e377a3 519#if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
618a5e38
RR
520 if ( wxGetApp().ShowButtons() )
521 {
522 m_treeCtrl->CreateButtonsImageList(-1);
523 wxGetApp().SetShowButtons(FALSE);
524 }
525 else
526 {
527 m_treeCtrl->CreateButtonsImageList(15);
528 wxGetApp().SetShowButtons(TRUE);
529 }
a3e377a3 530#endif
2c8e4738
VZ
531}
532
dd3646fd
VZ
533void MyFrame::OnCollapseAndReset(wxCommandEvent& event)
534{
5f939e78 535 m_treeCtrl->CollapseAndReset(m_treeCtrl->GetRootItem());
dd3646fd
VZ
536}
537
f65635b5
VZ
538void MyFrame::OnEnsureVisible(wxCommandEvent& event)
539{
540 m_treeCtrl->DoEnsureVisible();
541}
542
75c74ca0
VZ
543void MyFrame::OnInsertItem(wxCommandEvent& WXUNUSED(event))
544{
2c8e4738
VZ
545 int image = wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_File : -1;
546 m_treeCtrl->InsertItem(m_treeCtrl->GetRootItem(), image, "2nd item");
75c74ca0
VZ
547}
548
5f939e78
VZ
549void MyFrame::OnAddItem(wxCommandEvent& WXUNUSED(event))
550{
551 static int s_num = 0;
552
553 wxString text;
4693b20c 554 text.Printf(wxT("Item #%d"), ++s_num);
5f939e78
VZ
555
556 m_treeCtrl->AppendItem(m_treeCtrl->GetRootItem(),
112c5086
RR
557 text /*,
558 MyTreeCtrl::TreeCtrlIcon_File */ );
5f939e78
VZ
559}
560
cf724bce
RR
561void MyFrame::OnIncIndent(wxCommandEvent& WXUNUSED(event))
562{
563 unsigned int indent = m_treeCtrl->GetIndent();
564 if (indent < 100)
565 m_treeCtrl->SetIndent( indent+5 );
566}
567
568void MyFrame::OnDecIndent(wxCommandEvent& WXUNUSED(event))
569{
570 unsigned int indent = m_treeCtrl->GetIndent();
571 if (indent > 10)
572 m_treeCtrl->SetIndent( indent-5 );
573}
574
575void MyFrame::OnIncSpacing(wxCommandEvent& WXUNUSED(event))
576{
577 unsigned int indent = m_treeCtrl->GetSpacing();
578 if (indent < 100)
579 m_treeCtrl->SetSpacing( indent+5 );
580}
581
582void MyFrame::OnDecSpacing(wxCommandEvent& WXUNUSED(event))
583{
584 unsigned int indent = m_treeCtrl->GetSpacing();
585 if (indent > 10)
586 m_treeCtrl->SetSpacing( indent-5 );
587}
588
9dfbf520
VZ
589void MyFrame::OnToggleIcon(wxCommandEvent& WXUNUSED(event))
590{
591 wxTreeItemId item = m_treeCtrl->GetSelection();
592
593 CHECK_ITEM( item );
594
595 m_treeCtrl->DoToggleIcon(item);
596}
597
d88a2485
VZ
598void MyFrame::OnSetFgColour(wxCommandEvent& WXUNUSED(event))
599{
600 wxColour col = wxGetColourFromUser(this, m_treeCtrl->GetForegroundColour());
601 if ( col.Ok() )
602 m_treeCtrl->SetForegroundColour(col);
603}
604
605void MyFrame::OnSetBgColour(wxCommandEvent& WXUNUSED(event))
606{
607 wxColour col = wxGetColourFromUser(this, m_treeCtrl->GetBackgroundColour());
608 if ( col.Ok() )
609 m_treeCtrl->SetBackgroundColour(col);
610}
611
3a5a2f56 612// MyTreeCtrl implementation
484523cf
JS
613#if USE_GENERIC_TREECTRL
614IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl, wxGenericTreeCtrl)
615#else
23fd5130 616IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl, wxTreeCtrl)
484523cf 617#endif
23fd5130 618
3a5a2f56
VZ
619MyTreeCtrl::MyTreeCtrl(wxWindow *parent, const wxWindowID id,
620 const wxPoint& pos, const wxSize& size,
621 long style)
622 : wxTreeCtrl(parent, id, pos, size, style)
457814b5 623{
2c8e4738 624 m_reverseSort = FALSE;
91b8de8d 625
2c8e4738 626 CreateImageList();
91b8de8d 627
2c8e4738 628 // Add some items to the tree
618a5e38 629 AddTestItemsToTree(5, 2);
2c8e4738
VZ
630}
631
632void MyTreeCtrl::CreateImageList(int size)
633{
2c8e4738
VZ
634 if ( size == -1 )
635 {
618a5e38
RR
636 SetImageList(NULL);
637 return;
2c8e4738 638 }
618a5e38
RR
639 if ( size == 0 )
640 size = m_imageSize;
f6bcfd97 641 else
618a5e38
RR
642 m_imageSize = size;
643
644 // Make an image list containing small icons
645 wxImageList *images = new wxImageList(size, size, TRUE);
e1ee62bd 646
618a5e38 647 // should correspond to TreeCtrlIcon_xxx enum
f60d0f94 648#if defined(__WXMSW__) && defined(__WIN16__)
618a5e38
RR
649 images->Add(wxBitmap("bitmap1", wxBITMAP_TYPE_BMP_RESOURCE));
650 images->Add(wxBitmap("bitmap2", wxBITMAP_TYPE_BMP_RESOURCE));
651 images->Add(wxBitmap("bitmap3", wxBITMAP_TYPE_BMP_RESOURCE));
652 images->Add(wxBitmap("bitmap4", wxBITMAP_TYPE_BMP_RESOURCE));
653 images->Add(wxBitmap("bitmap5", wxBITMAP_TYPE_BMP_RESOURCE));
f6bcfd97 654#else // !MSW
618a5e38
RR
655 wxBusyCursor wait;
656 wxIcon icons[5];
657 icons[0] = wxICON(icon1);
658 icons[1] = wxICON(icon2);
659 icons[2] = wxICON(icon3);
660 icons[3] = wxICON(icon4);
661 icons[4] = wxICON(icon5);
662
663 int sizeOrig = icons[0].GetWidth();
664 for ( size_t i = 0; i < WXSIZEOF(icons); i++ )
665 {
666 if ( size == sizeOrig )
2c8e4738 667 {
618a5e38 668 images->Add(icons[i]);
2c8e4738 669 }
618a5e38
RR
670 else
671 {
9f24e0b5 672 images->Add(wxBitmap(wxBitmap(icons[i]).ConvertToImage().Rescale(size, size)));
618a5e38
RR
673 }
674 }
f6bcfd97 675#endif // MSW/!MSW
618a5e38
RR
676
677 AssignImageList(images);
678}
679
680void MyTreeCtrl::CreateButtonsImageList(int size)
681{
a3e377a3 682#if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
618a5e38
RR
683 if ( size == -1 )
684 {
685 SetButtonsImageList(NULL);
686 return;
687 }
688
689 // Make an image list containing small icons
690 wxImageList *images = new wxImageList(size, size, TRUE);
691
692 // should correspond to TreeCtrlIcon_xxx enum
693#if defined(__WXMSW__) && defined(__WIN16__)
694 images->Add(wxBitmap("bitmap1", wxBITMAP_TYPE_BMP_RESOURCE));
695 images->Add(wxBitmap("bitmap2", wxBITMAP_TYPE_BMP_RESOURCE));
696 images->Add(wxBitmap("bitmap3", wxBITMAP_TYPE_BMP_RESOURCE));
697 images->Add(wxBitmap("bitmap4", wxBITMAP_TYPE_BMP_RESOURCE));
698 images->Add(wxBitmap("bitmap5", wxBITMAP_TYPE_BMP_RESOURCE));
699#else // !MSW
700 wxBusyCursor wait;
701 wxIcon icons[4];
702 icons[0] = wxICON(icon3); // closed
703 icons[1] = wxICON(icon3); // closed, selected
704 icons[2] = wxICON(icon5); // open
705 icons[3] = wxICON(icon5); // open, selected
706
707 for ( size_t i = 0; i < WXSIZEOF(icons); i++ )
708 {
709 int sizeOrig = icons[i].GetWidth();
710 if ( size == sizeOrig )
711 {
712 images->Add(icons[i]);
713 }
714 else
715 {
368d59f0 716 images->Add(wxBitmap(icons[i].ConvertToImage().Rescale(size, size)));
618a5e38 717 }
2c8e4738 718 }
618a5e38 719#endif // MSW/!MSW
457814b5 720
618a5e38 721 AssignButtonsImageList(images);
a3e377a3 722#endif
457814b5
JS
723}
724
3a5a2f56 725MyTreeCtrl::~MyTreeCtrl()
457814b5 726{
457814b5
JS
727}
728
e1ee62bd
VZ
729int MyTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
730 const wxTreeItemId& item2)
731{
5f939e78
VZ
732 if ( m_reverseSort )
733 {
734 // just exchange 1st and 2nd items
735 return wxTreeCtrl::OnCompareItems(item2, item1);
736 }
737 else
738 {
739 return wxTreeCtrl::OnCompareItems(item1, item2);
740 }
e1ee62bd
VZ
741}
742
3a5a2f56
VZ
743void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId& idParent,
744 size_t numChildren,
4832f7c0
VZ
745 size_t depth,
746 size_t folder)
457814b5 747{
f65635b5 748 if ( depth > 0 )
3a5a2f56 749 {
74b31181
VZ
750 bool hasChildren = depth > 1;
751
f65635b5
VZ
752 wxString str;
753 for ( size_t n = 0; n < numChildren; n++ )
754 {
755 // at depth 1 elements won't have any more children
74b31181 756 if ( hasChildren )
4693b20c 757 str.Printf(wxT("%s child %d"), wxT("Folder"), n + 1);
74b31181 758 else
4693b20c 759 str.Printf(wxT("%s child %d.%d"), wxT("File"), folder, n + 1);
f65635b5 760
74b31181
VZ
761 // here we pass to AppendItem() normal and selected item images (we
762 // suppose that selected image follows the normal one in the enum)
2c8e4738
VZ
763 int image, imageSel;
764 if ( wxGetApp().ShowImages() )
765 {
766 image = depth == 1 ? TreeCtrlIcon_File : TreeCtrlIcon_Folder;
767 imageSel = image + 1;
768 }
769 else
770 {
771 image = imageSel = -1;
772 }
773 wxTreeItemId id = AppendItem(idParent, str, image, imageSel,
f65635b5
VZ
774 new MyTreeItemData(str));
775
74b31181 776 // and now we also set the expanded one (only for the folders)
2c8e4738 777 if ( hasChildren && wxGetApp().ShowImages() )
74b31181
VZ
778 {
779 SetItemImage(id, TreeCtrlIcon_FolderOpened,
780 wxTreeItemIcon_Expanded);
781 }
782
f65635b5 783 // remember the last child for OnEnsureVisible()
74b31181 784 if ( !hasChildren && n == numChildren - 1 )
f65635b5
VZ
785 {
786 m_lastItem = id;
787 }
788
789 AddItemsRecursively(id, numChildren, depth - 1, n + 1);
790 }
3a5a2f56 791 }
f65635b5 792 //else: done!
457814b5
JS
793}
794
3a5a2f56
VZ
795void MyTreeCtrl::AddTestItemsToTree(size_t numChildren,
796 size_t depth)
457814b5 797{
2c8e4738 798 int image = wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_Folder : -1;
5f939e78 799 wxTreeItemId rootId = AddRoot("Root",
2c8e4738 800 image, image,
5f939e78 801 new MyTreeItemData("Root item"));
2c8e4738
VZ
802 if ( image != -1 )
803 {
804 SetItemImage(rootId, TreeCtrlIcon_FolderOpened, wxTreeItemIcon_Expanded);
805 }
457814b5 806
5f939e78 807 AddItemsRecursively(rootId, numChildren, depth, 0);
9ec64fa7
VZ
808
809 // set some colours/fonts for testing
810 SetItemFont(rootId, *wxITALIC_FONT);
811
812 long cookie;
813 wxTreeItemId id = GetFirstChild(rootId, cookie);
814 SetItemTextColour(id, *wxBLUE);
815
816 id = GetNextChild(rootId, cookie);
817 id = GetNextChild(rootId, cookie);
818 SetItemTextColour(id, *wxRED);
819 SetItemBackgroundColour(id, *wxLIGHT_GREY);
4832f7c0
VZ
820}
821
822void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId& idParent, long cookie)
823{
5f939e78 824 wxTreeItemId id;
4832f7c0 825
5f939e78
VZ
826 if( cookie == -1 )
827 id = GetFirstChild(idParent, cookie);
828 else
829 id = GetNextChild(idParent, cookie);
4832f7c0 830
5f939e78
VZ
831 if(id <= 0)
832 return;
4832f7c0 833
9dfbf520 834 wxString text = GetItemText(id);
5f939e78 835 wxLogMessage(text);
4832f7c0 836
5f939e78
VZ
837 if (ItemHasChildren(id))
838 GetItemsRecursively(id,-1);
4832f7c0 839
5f939e78 840 GetItemsRecursively(idParent, cookie);
457814b5
JS
841}
842
9dfbf520
VZ
843void MyTreeCtrl::DoToggleIcon(const wxTreeItemId& item)
844{
845 int image = GetItemImage(item) == TreeCtrlIcon_Folder ? TreeCtrlIcon_File
846 : TreeCtrlIcon_Folder;
847
848 SetItemImage(item, image);
849}
850
4832f7c0 851
3a5a2f56 852// avoid repetition
4693b20c
MB
853#define TREE_EVENT_HANDLER(name) \
854void MyTreeCtrl::name(wxTreeEvent& event) \
855{ \
856 wxLogMessage(wxT(#name)); \
857 event.Skip(); \
5f939e78 858}
457814b5 859
3a5a2f56 860TREE_EVENT_HANDLER(OnBeginRDrag)
3a5a2f56
VZ
861TREE_EVENT_HANDLER(OnDeleteItem)
862TREE_EVENT_HANDLER(OnGetInfo)
863TREE_EVENT_HANDLER(OnSetInfo)
864TREE_EVENT_HANDLER(OnItemExpanded)
865TREE_EVENT_HANDLER(OnItemExpanding)
866TREE_EVENT_HANDLER(OnItemCollapsed)
867TREE_EVENT_HANDLER(OnSelChanged)
868TREE_EVENT_HANDLER(OnSelChanging)
3dbeaa52 869TREE_EVENT_HANDLER(OnTreeKeyDown)
3a5a2f56
VZ
870
871#undef TREE_EVENT_HANDLER
872
5888ef1e
VZ
873void MyTreeCtrl::OnBeginDrag(wxTreeEvent& event)
874{
875 // need to explicitly allow drag
876 if ( event.GetItem() != GetRootItem() )
877 {
878 m_draggedItem = event.GetItem();
879
4693b20c 880 wxLogMessage(wxT("OnBeginDrag: started dragging %s"),
5888ef1e
VZ
881 GetItemText(m_draggedItem).c_str());
882
883 event.Allow();
884 }
885 else
886 {
4693b20c 887 wxLogMessage(wxT("OnBeginDrag: this item can't be dragged."));
5888ef1e
VZ
888 }
889}
890
891void MyTreeCtrl::OnEndDrag(wxTreeEvent& event)
892{
893 wxTreeItemId itemSrc = m_draggedItem,
894 itemDst = event.GetItem();
e97320d4 895 m_draggedItem = (wxTreeItemId)0l;
5888ef1e
VZ
896
897 // where to copy the item?
898 if ( itemDst.IsOk() && !ItemHasChildren(itemDst) )
899 {
900 // copy to the parent then
901 itemDst = GetParent(itemDst);
902 }
903
904 if ( !itemDst.IsOk() )
905 {
4693b20c 906 wxLogMessage(wxT("OnEndDrag: can't drop here."));
5888ef1e
VZ
907
908 return;
909 }
910
911 wxString text = GetItemText(itemSrc);
4693b20c 912 wxLogMessage(wxT("OnEndDrag: '%s' copied to '%s'."),
5888ef1e
VZ
913 text.c_str(), GetItemText(itemDst).c_str());
914
915 // just do append here - we could also insert it just before/after the item
916 // on which it was dropped, but this requires slightly more work... we also
917 // completely ignore the client data and icon of the old item but could
918 // copy them as well.
919 //
920 // Finally, we only copy one item here but we might copy the entire tree if
921 // we were dragging a folder.
2c8e4738
VZ
922 int image = wxGetApp().ShowImages() ? TreeCtrlIcon_File : -1;
923 AppendItem(itemDst, text, image);
5888ef1e
VZ
924}
925
5ea47806
VZ
926void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent& event)
927{
4693b20c 928 wxLogMessage(wxT("OnBeginLabelEdit"));
5ea47806 929
618a5e38 930 // for testing, prevent this item's label editing
5ea47806
VZ
931 wxTreeItemId itemId = event.GetItem();
932 if ( IsTestItem(itemId) )
933 {
4693b20c 934 wxMessageBox(wxT("You can't edit this item."));
5ea47806
VZ
935
936 event.Veto();
937 }
938}
939
940void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent& event)
941{
4693b20c 942 wxLogMessage(wxT("OnEndLabelEdit"));
5ea47806
VZ
943
944 // don't allow anything except letters in the labels
945 if ( !event.GetLabel().IsWord() )
946 {
4693b20c 947 wxMessageBox(wxT("The label should contain only letters."));
5ea47806
VZ
948
949 event.Veto();
950 }
951}
952
3a5a2f56 953void MyTreeCtrl::OnItemCollapsing(wxTreeEvent& event)
457814b5 954{
4693b20c 955 wxLogMessage(wxT("OnItemCollapsing"));
f135ff73 956
5f939e78
VZ
957 // for testing, prevent the user from collapsing the first child folder
958 wxTreeItemId itemId = event.GetItem();
5ea47806 959 if ( IsTestItem(itemId) )
5f939e78 960 {
4693b20c 961 wxMessageBox(wxT("You can't collapse this item."));
457814b5 962
5f939e78
VZ
963 event.Veto();
964 }
457814b5
JS
965}
966
477350ce 967void MyTreeCtrl::OnItemActivated(wxTreeEvent& event)
457814b5 968{
5f939e78 969 // show some info about this item
477350ce 970 wxTreeItemId itemId = event.GetItem();
5f939e78 971 MyTreeItemData *item = (MyTreeItemData *)GetItemData(itemId);
457814b5 972
5f939e78
VZ
973 if ( item != NULL )
974 {
975 item->ShowInfo(this);
976 }
457814b5 977
4693b20c 978 wxLogMessage(wxT("OnItemActivated"));
5f939e78
VZ
979}
980
e3d0aa77
VZ
981void MyTreeCtrl::OnItemRightClick(wxTreeEvent& event)
982{
983 ShowMenu(event.GetItem(), event.GetPoint());
984}
985
986void MyTreeCtrl::OnRMouseUp(wxMouseEvent& event)
987{
988 wxPoint pt = event.GetPosition();
989 ShowMenu(HitTest(pt), pt);
990}
991
992void MyTreeCtrl::ShowMenu(wxTreeItemId id, const wxPoint& pt)
993{
994 wxString title;
995 if ( id.IsOk() )
996 {
997 title << _T("Menu for ") << GetItemText(id);
998 }
999 else
1000 {
1001 title = _T("Menu for no particular item");
1002 }
1003
801bb685 1004#if wxUSE_MENUS
e3d0aa77
VZ
1005 wxMenu menu(title);
1006 menu.Append(TreeTest_About, _T("&About..."));
1007 menu.Append(TreeTest_Dump, _T("&Dump"));
1008
1009 PopupMenu(&menu, pt);
801bb685 1010#endif // wxUSE_MENUS
e3d0aa77
VZ
1011}
1012
5f939e78
VZ
1013void MyTreeCtrl::OnRMouseDClick(wxMouseEvent& event)
1014{
1015 wxTreeItemId id = HitTest(event.GetPosition());
1016 if ( !id )
4693b20c 1017 wxLogMessage(wxT("No item under mouse"));
5f939e78
VZ
1018 else
1019 {
1020 MyTreeItemData *item = (MyTreeItemData *)GetItemData(id);
1021 if ( item )
4693b20c 1022 wxLogMessage(wxT("Item '%s' under mouse"), item->GetDesc());
5f939e78 1023 }
457814b5
JS
1024}
1025
4693b20c 1026static inline const wxChar *Bool2String(bool b)
457814b5 1027{
4693b20c 1028 return b ? wxT("") : wxT("not ");
457814b5
JS
1029}
1030
9769e589 1031void MyTreeItemData::ShowInfo(wxTreeCtrl *tree)
457814b5 1032{
4693b20c
MB
1033 wxLogMessage(wxT("Item '%s': %sselected, %sexpanded, %sbold,\n")
1034 wxT("%u children (%u immediately under this item)."),
5f939e78
VZ
1035 m_desc.c_str(),
1036 Bool2String(tree->IsSelected(GetId())),
1037 Bool2String(tree->IsExpanded(GetId())),
1038 Bool2String(tree->IsBold(GetId())),
1039 tree->GetChildrenCount(GetId()),
1040 tree->GetChildrenCount(GetId(), FALSE));
457814b5 1041}