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