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