]> git.saurik.com Git - wxWidgets.git/blame - samples/treectrl/treetest.cpp
Fix for VA 4.0
[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);
31405c01
VZ
209
210 m_treeCtrl->SetBackgroundColour(wxColour(204, 205, 79));
211
5f939e78
VZ
212 wxTextCtrl *textCtrl = new wxTextCtrl(this, -1, "",
213 wxDefaultPosition, wxDefaultSize,
214 wxTE_MULTILINE | wxSUNKEN_BORDER);
215
216 wxLayoutConstraints *c = new wxLayoutConstraints;
217 c->top.SameAs(this, wxTop);
218 c->left.SameAs(this, wxLeft);
219 c->right.SameAs(this, wxRight);
220 c->height.PercentOf(this, wxHeight, 66);
221 m_treeCtrl->SetConstraints(c);
222
223 c = new wxLayoutConstraints;
224 c->top.Below(m_treeCtrl);
225 c->left.SameAs(this, wxLeft);
226 c->right.SameAs(this, wxRight);
227 c->bottom.SameAs(this, wxBottom);
228 textCtrl->SetConstraints(c);
229 SetAutoLayout(TRUE);
230
231 // create a status bar with 3 panes
232 CreateStatusBar(3);
233 SetStatusText("", 0);
3a5a2f56 234
a367b9b3 235#ifdef __WXMOTIF__
5f939e78
VZ
236 // For some reason, we get a memcpy crash in wxLogStream::DoLogStream
237 // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc?
238 delete wxLog::SetActiveTarget(new wxLogStderr);
a367b9b3 239#else
5f939e78
VZ
240 // set our text control as the log target
241 wxLogTextCtrl *logWindow = new wxLogTextCtrl(textCtrl);
242 delete wxLog::SetActiveTarget(logWindow);
a367b9b3 243#endif
457814b5
JS
244}
245
c89a6106 246MyFrame::~MyFrame()
457814b5 247{
5f939e78 248 delete wxLog::SetActiveTarget(NULL);
457814b5
JS
249}
250
3a5a2f56 251void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
457814b5 252{
5f939e78 253 Close(TRUE);
457814b5
JS
254}
255
3a5a2f56 256void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
457814b5 257{
5f939e78 258 wxMessageBox("Tree test sample\n"
5ea47806 259 "(c) Julian Smart 1997, Vadim Zeitlin 1998",
5f939e78
VZ
260 "About tree test",
261 wxOK | wxICON_INFORMATION, this);
e1ee62bd
VZ
262}
263
264void MyFrame::OnRename(wxCommandEvent& WXUNUSED(event))
265{
5f939e78 266 wxTreeItemId item = m_treeCtrl->GetSelection();
e1ee62bd 267
5f939e78 268 CHECK_ITEM( item );
e1ee62bd 269
5ea47806
VZ
270 // old code - now we edit in place
271#if 0
5f939e78
VZ
272 static wxString s_text;
273 s_text = wxGetTextFromUser("New name: ", "Tree sample question",
274 s_text, this);
275 if ( !s_text.IsEmpty() )
276 {
277 m_treeCtrl->SetItemText(item, s_text);
278 }
5ea47806
VZ
279#endif // 0
280
281 // TODO demonstrate creating a custom edit control...
282 (void)m_treeCtrl->EditLabel(item);
e1ee62bd
VZ
283}
284
5fd11f09
RR
285void MyFrame::OnCount(wxCommandEvent& WXUNUSED(event))
286{
287 wxTreeItemId item = m_treeCtrl->GetSelection();
288
289 CHECK_ITEM( item );
290
291 int i = m_treeCtrl->GetChildrenCount( item, FALSE );
75c74ca0 292
247e5b16 293 wxLogMessage(wxT("%d children"), i);
5fd11f09
RR
294}
295
296void MyFrame::OnCountRec(wxCommandEvent& WXUNUSED(event))
297{
298 wxTreeItemId item = m_treeCtrl->GetSelection();
299
300 CHECK_ITEM( item );
301
302 int i = m_treeCtrl->GetChildrenCount( item );
75c74ca0 303
247e5b16 304 wxLogMessage(wxT("%d children"), i);
5fd11f09
RR
305}
306
e1ee62bd
VZ
307void MyFrame::DoSort(bool reverse)
308{
5f939e78 309 wxTreeItemId item = m_treeCtrl->GetSelection();
e1ee62bd 310
5f939e78 311 CHECK_ITEM( item );
457814b5 312
5f939e78 313 m_treeCtrl->DoSortChildren(item, reverse);
457814b5
JS
314}
315
4832f7c0
VZ
316void MyFrame::OnDump(wxCommandEvent& WXUNUSED(event))
317{
5f939e78 318 wxTreeItemId root = m_treeCtrl->GetSelection();
e1ee62bd 319
5f939e78 320 CHECK_ITEM( root );
e1ee62bd 321
5f939e78 322 m_treeCtrl->GetItemsRecursively(root, -1);
4832f7c0
VZ
323}
324
9dfbf520
VZ
325#ifndef NO_MULTIPLE_SELECTION
326
91b8de8d
RR
327void MyFrame::OnDumpSelected(wxCommandEvent& WXUNUSED(event))
328{
9dfbf520 329 wxArrayTreeItemIds array;
91b8de8d 330
9dfbf520 331 size_t count = m_treeCtrl->GetSelections(array);
247e5b16 332 wxLogMessage(wxT("%u items selected"), count);
91b8de8d 333
9dfbf520
VZ
334 for ( size_t n = 0; n < count; n++ )
335 {
336 wxLogMessage("\t%s", m_treeCtrl->GetItemText(array.Item(n)).c_str());
337 }
338}
339
340void MyFrame::OnSelect(wxCommandEvent& event)
341{
342 m_treeCtrl->SelectItem(m_treeCtrl->GetSelection());
343}
344
345void MyFrame::OnUnselect(wxCommandEvent& event)
346{
347 m_treeCtrl->UnselectAll();
91b8de8d
RR
348}
349
9dfbf520
VZ
350#endif // NO_MULTIPLE_SELECTION
351
add28c55
VZ
352void MyFrame::DoSetBold(bool bold)
353{
5f939e78 354 wxTreeItemId item = m_treeCtrl->GetSelection();
e1ee62bd 355
5f939e78 356 CHECK_ITEM( item );
e1ee62bd 357
5f939e78 358 m_treeCtrl->SetItemBold(item, bold);
add28c55
VZ
359}
360
ff5bf259
VZ
361void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event))
362{
5f939e78 363 wxTreeItemId item = m_treeCtrl->GetSelection();
e1ee62bd 364
5f939e78 365 CHECK_ITEM( item );
e1ee62bd 366
5f939e78 367 m_treeCtrl->Delete(item);
ff5bf259
VZ
368}
369
372edb9d
VZ
370void MyFrame::OnDeleteChildren(wxCommandEvent& WXUNUSED(event))
371{
5f939e78 372 wxTreeItemId item = m_treeCtrl->GetSelection();
e1ee62bd 373
5f939e78 374 CHECK_ITEM( item );
e1ee62bd 375
5f939e78 376 m_treeCtrl->DeleteChildren(item);
372edb9d
VZ
377}
378
ff5bf259
VZ
379void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event))
380{
5f939e78 381 m_treeCtrl->DeleteAllItems();
ff5bf259
VZ
382}
383
384void MyFrame::OnRecreate(wxCommandEvent& event)
385{
5f939e78
VZ
386 OnDeleteAll(event);
387 m_treeCtrl->AddTestItemsToTree(3, 2);
ff5bf259
VZ
388}
389
dd3646fd
VZ
390void MyFrame::OnCollapseAndReset(wxCommandEvent& event)
391{
5f939e78 392 m_treeCtrl->CollapseAndReset(m_treeCtrl->GetRootItem());
dd3646fd
VZ
393}
394
f65635b5
VZ
395void MyFrame::OnEnsureVisible(wxCommandEvent& event)
396{
397 m_treeCtrl->DoEnsureVisible();
398}
399
75c74ca0
VZ
400void MyFrame::OnInsertItem(wxCommandEvent& WXUNUSED(event))
401{
402 m_treeCtrl->InsertItem(m_treeCtrl->GetRootItem(), 1, "2nd item");
403}
404
5f939e78
VZ
405void MyFrame::OnAddItem(wxCommandEvent& WXUNUSED(event))
406{
407 static int s_num = 0;
408
409 wxString text;
410 text.Printf("Item #%d", ++s_num);
411
412 m_treeCtrl->AppendItem(m_treeCtrl->GetRootItem(),
112c5086
RR
413 text /*,
414 MyTreeCtrl::TreeCtrlIcon_File */ );
5f939e78
VZ
415}
416
cf724bce
RR
417void MyFrame::OnIncIndent(wxCommandEvent& WXUNUSED(event))
418{
419 unsigned int indent = m_treeCtrl->GetIndent();
420 if (indent < 100)
421 m_treeCtrl->SetIndent( indent+5 );
422}
423
424void MyFrame::OnDecIndent(wxCommandEvent& WXUNUSED(event))
425{
426 unsigned int indent = m_treeCtrl->GetIndent();
427 if (indent > 10)
428 m_treeCtrl->SetIndent( indent-5 );
429}
430
431void MyFrame::OnIncSpacing(wxCommandEvent& WXUNUSED(event))
432{
433 unsigned int indent = m_treeCtrl->GetSpacing();
434 if (indent < 100)
435 m_treeCtrl->SetSpacing( indent+5 );
436}
437
438void MyFrame::OnDecSpacing(wxCommandEvent& WXUNUSED(event))
439{
440 unsigned int indent = m_treeCtrl->GetSpacing();
441 if (indent > 10)
442 m_treeCtrl->SetSpacing( indent-5 );
443}
444
9dfbf520
VZ
445void MyFrame::OnToggleIcon(wxCommandEvent& WXUNUSED(event))
446{
447 wxTreeItemId item = m_treeCtrl->GetSelection();
448
449 CHECK_ITEM( item );
450
451 m_treeCtrl->DoToggleIcon(item);
452}
453
3a5a2f56 454// MyTreeCtrl implementation
23fd5130
VZ
455IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl, wxTreeCtrl)
456
3a5a2f56
VZ
457MyTreeCtrl::MyTreeCtrl(wxWindow *parent, const wxWindowID id,
458 const wxPoint& pos, const wxSize& size,
459 long style)
460 : wxTreeCtrl(parent, id, pos, size, style)
457814b5 461{
9ce29a28
SB
462#ifndef NO_VARIABLE_HEIGHT
463#if wxUSE_LIBJPEG
9dfbf520 464 wxImage::AddHandler(new wxJPEGHandler);
91b8de8d
RR
465 wxImage image;
466
467 image.LoadFile(wxString("horse.jpg"), wxBITMAP_TYPE_JPEG );
9ce29a28 468#endif
91b8de8d
RR
469#endif
470
5f939e78 471 m_reverseSort = FALSE;
e1ee62bd 472
5f939e78
VZ
473 // Make an image list containing small icons
474 m_imageListNormal = new wxImageList(16, 16, TRUE);
457814b5 475
5f939e78 476 // should correspond to TreeCtrlIcon_xxx enum
f60d0f94 477#if defined(__WXMSW__) && defined(__WIN16__)
5f939e78
VZ
478 // This is required in 16-bit Windows mode only because we can't load a specific (16x16)
479 // icon image, so it comes out stretched
9ce29a28 480# ifndef NO_VARIABLE_HEIGHT
91b8de8d
RR
481 m_imageListNormal->Add(image.ConvertToBitmap());
482# else
5f939e78 483 m_imageListNormal->Add(wxBitmap("bitmap1", wxBITMAP_TYPE_BMP_RESOURCE));
91b8de8d 484# endif
5f939e78 485 m_imageListNormal->Add(wxBitmap("bitmap2", wxBITMAP_TYPE_BMP_RESOURCE));
74b31181
VZ
486 m_imageListNormal->Add(wxBitmap("bitmap3", wxBITMAP_TYPE_BMP_RESOURCE));
487 m_imageListNormal->Add(wxBitmap("bitmap4", wxBITMAP_TYPE_BMP_RESOURCE));
488 m_imageListNormal->Add(wxBitmap("bitmap5", wxBITMAP_TYPE_BMP_RESOURCE));
f60d0f94 489#else
9ce29a28 490# ifndef NO_VARIABLE_HEIGHT
91b8de8d
RR
491 m_imageListNormal->Add(image.ConvertToBitmap());
492# else
5f939e78 493 m_imageListNormal->Add(wxICON(icon1));
91b8de8d 494# endif
5f939e78 495 m_imageListNormal->Add(wxICON(icon2));
74b31181
VZ
496 m_imageListNormal->Add(wxICON(icon3));
497 m_imageListNormal->Add(wxICON(icon4));
498 m_imageListNormal->Add(wxICON(icon5));
f60d0f94 499#endif
457814b5 500
5f939e78 501 SetImageList(m_imageListNormal);
457814b5 502
5f939e78
VZ
503 // Add some items to the tree
504 AddTestItemsToTree(3, 2);
457814b5
JS
505}
506
3a5a2f56 507MyTreeCtrl::~MyTreeCtrl()
457814b5 508{
5f939e78 509 delete m_imageListNormal;
457814b5
JS
510}
511
e1ee62bd
VZ
512int MyTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
513 const wxTreeItemId& item2)
514{
5f939e78
VZ
515 if ( m_reverseSort )
516 {
517 // just exchange 1st and 2nd items
518 return wxTreeCtrl::OnCompareItems(item2, item1);
519 }
520 else
521 {
522 return wxTreeCtrl::OnCompareItems(item1, item2);
523 }
e1ee62bd
VZ
524}
525
3a5a2f56
VZ
526void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId& idParent,
527 size_t numChildren,
4832f7c0
VZ
528 size_t depth,
529 size_t folder)
457814b5 530{
f65635b5 531 if ( depth > 0 )
3a5a2f56 532 {
74b31181
VZ
533 bool hasChildren = depth > 1;
534
f65635b5
VZ
535 wxString str;
536 for ( size_t n = 0; n < numChildren; n++ )
537 {
538 // at depth 1 elements won't have any more children
74b31181 539 if ( hasChildren )
f65635b5 540 str.Printf("%s child %d", "Folder", n + 1);
74b31181
VZ
541 else
542 str.Printf("%s child %d.%d", "File", folder, n + 1);
f65635b5 543
74b31181
VZ
544 // here we pass to AppendItem() normal and selected item images (we
545 // suppose that selected image follows the normal one in the enum)
9dfbf520 546 int image = depth == 1 ? TreeCtrlIcon_File : TreeCtrlIcon_Folder;
74b31181 547 wxTreeItemId id = AppendItem(idParent, str, image, image + 1,
f65635b5
VZ
548 new MyTreeItemData(str));
549
74b31181
VZ
550 // and now we also set the expanded one (only for the folders)
551 if ( hasChildren )
552 {
553 SetItemImage(id, TreeCtrlIcon_FolderOpened,
554 wxTreeItemIcon_Expanded);
555 }
556
f65635b5 557 // remember the last child for OnEnsureVisible()
74b31181 558 if ( !hasChildren && n == numChildren - 1 )
f65635b5
VZ
559 {
560 m_lastItem = id;
561 }
562
563 AddItemsRecursively(id, numChildren, depth - 1, n + 1);
564 }
3a5a2f56 565 }
f65635b5 566 //else: done!
457814b5
JS
567}
568
3a5a2f56
VZ
569void MyTreeCtrl::AddTestItemsToTree(size_t numChildren,
570 size_t depth)
457814b5 571{
5f939e78
VZ
572 wxTreeItemId rootId = AddRoot("Root",
573 TreeCtrlIcon_Folder, TreeCtrlIcon_Folder,
574 new MyTreeItemData("Root item"));
74b31181 575 SetItemImage(rootId, TreeCtrlIcon_FolderOpened, wxTreeItemIcon_Expanded);
457814b5 576
5f939e78 577 AddItemsRecursively(rootId, numChildren, depth, 0);
9ec64fa7
VZ
578
579 // set some colours/fonts for testing
580 SetItemFont(rootId, *wxITALIC_FONT);
581
582 long cookie;
583 wxTreeItemId id = GetFirstChild(rootId, cookie);
584 SetItemTextColour(id, *wxBLUE);
585
586 id = GetNextChild(rootId, cookie);
587 id = GetNextChild(rootId, cookie);
588 SetItemTextColour(id, *wxRED);
589 SetItemBackgroundColour(id, *wxLIGHT_GREY);
4832f7c0
VZ
590}
591
592void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId& idParent, long cookie)
593{
5f939e78 594 wxTreeItemId id;
4832f7c0 595
5f939e78
VZ
596 if( cookie == -1 )
597 id = GetFirstChild(idParent, cookie);
598 else
599 id = GetNextChild(idParent, cookie);
4832f7c0 600
5f939e78
VZ
601 if(id <= 0)
602 return;
4832f7c0 603
9dfbf520 604 wxString text = GetItemText(id);
5f939e78 605 wxLogMessage(text);
4832f7c0 606
5f939e78
VZ
607 if (ItemHasChildren(id))
608 GetItemsRecursively(id,-1);
4832f7c0 609
5f939e78 610 GetItemsRecursively(idParent, cookie);
457814b5
JS
611}
612
9dfbf520
VZ
613void MyTreeCtrl::DoToggleIcon(const wxTreeItemId& item)
614{
615 int image = GetItemImage(item) == TreeCtrlIcon_Folder ? TreeCtrlIcon_File
616 : TreeCtrlIcon_Folder;
617
618 SetItemImage(item, image);
619}
620
4832f7c0 621
3a5a2f56 622// avoid repetition
5f939e78
VZ
623#define TREE_EVENT_HANDLER(name) \
624void MyTreeCtrl::name(wxTreeEvent& WXUNUSED(event)) \
625{ \
626 wxLogMessage(#name); \
627}
457814b5 628
3a5a2f56
VZ
629TREE_EVENT_HANDLER(OnBeginDrag)
630TREE_EVENT_HANDLER(OnBeginRDrag)
3a5a2f56
VZ
631TREE_EVENT_HANDLER(OnDeleteItem)
632TREE_EVENT_HANDLER(OnGetInfo)
633TREE_EVENT_HANDLER(OnSetInfo)
634TREE_EVENT_HANDLER(OnItemExpanded)
635TREE_EVENT_HANDLER(OnItemExpanding)
636TREE_EVENT_HANDLER(OnItemCollapsed)
637TREE_EVENT_HANDLER(OnSelChanged)
638TREE_EVENT_HANDLER(OnSelChanging)
639
640#undef TREE_EVENT_HANDLER
641
5ea47806
VZ
642void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent& event)
643{
644 wxLogMessage("OnBeginLabelEdit");
645
646 // for testing, prevent this items label editing
647 wxTreeItemId itemId = event.GetItem();
648 if ( IsTestItem(itemId) )
649 {
650 wxMessageBox("You can't edit this item.");
651
652 event.Veto();
653 }
654}
655
656void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent& event)
657{
658 wxLogMessage("OnEndLabelEdit");
659
660 // don't allow anything except letters in the labels
661 if ( !event.GetLabel().IsWord() )
662 {
663 wxMessageBox("The label should contain only letters.");
664
665 event.Veto();
666 }
667}
668
3a5a2f56 669void MyTreeCtrl::OnItemCollapsing(wxTreeEvent& event)
457814b5 670{
5f939e78 671 wxLogMessage("OnItemCollapsing");
f135ff73 672
5f939e78
VZ
673 // for testing, prevent the user from collapsing the first child folder
674 wxTreeItemId itemId = event.GetItem();
5ea47806 675 if ( IsTestItem(itemId) )
5f939e78
VZ
676 {
677 wxMessageBox("You can't collapse this item.");
457814b5 678
5f939e78
VZ
679 event.Veto();
680 }
457814b5
JS
681}
682
6daa0637 683void MyTreeCtrl::OnTreeKeyDown(wxTreeEvent&WXUNUSED(event))
435fe83e 684{
5f939e78 685 wxLogMessage("OnTreeKeyDown");
435fe83e
RR
686}
687
688void MyTreeCtrl::OnItemActivated(wxTreeEvent&WXUNUSED(event))
457814b5 689{
5f939e78
VZ
690 // show some info about this item
691 wxTreeItemId itemId = GetSelection();
692 MyTreeItemData *item = (MyTreeItemData *)GetItemData(itemId);
457814b5 693
5f939e78
VZ
694 if ( item != NULL )
695 {
696 item->ShowInfo(this);
697 }
457814b5 698
5f939e78
VZ
699 wxLogMessage("OnItemActivated");
700}
701
702void MyTreeCtrl::OnRMouseDClick(wxMouseEvent& event)
703{
704 wxTreeItemId id = HitTest(event.GetPosition());
705 if ( !id )
706 wxLogMessage("No item under mouse");
707 else
708 {
709 MyTreeItemData *item = (MyTreeItemData *)GetItemData(id);
710 if ( item )
711 wxLogMessage("Item '%s' under mouse", item->GetDesc());
712 }
457814b5
JS
713}
714
3a5a2f56 715static inline const char *Bool2String(bool b)
457814b5 716{
5f939e78 717 return b ? "" : "not ";
457814b5
JS
718}
719
9769e589 720void MyTreeItemData::ShowInfo(wxTreeCtrl *tree)
457814b5 721{
5f939e78
VZ
722 wxLogMessage("Item '%s': %sselected, %sexpanded, %sbold,\n"
723 "%u children (%u immediately under this item).",
724 m_desc.c_str(),
725 Bool2String(tree->IsSelected(GetId())),
726 Bool2String(tree->IsExpanded(GetId())),
727 Bool2String(tree->IsBold(GetId())),
728 tree->GetChildrenCount(GetId()),
729 tree->GetChildrenCount(GetId(), FALSE));
457814b5 730}