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