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