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