Rebaked makefiles after htmllistbox and collapsable panel additions.
[wxWidgets.git] / samples / treectrl / treetest.cpp
CommitLineData
457814b5 1/////////////////////////////////////////////////////////////////////////////
ec312631 2// Name: treetest.cpp
457814b5
JS
3// Purpose: wxTreeCtrl sample
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
6aa89a22 8// Copyright: (c) Julian Smart
3a5a2f56 9// Licence: wxWindows license
457814b5
JS
10/////////////////////////////////////////////////////////////////////////////
11
457814b5
JS
12// For compilers that support precompilation, includes "wx/wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
b2c82287 16 #pragma hdrstop
457814b5
JS
17#endif
18
19#ifndef WX_PRECOMP
b2c82287
WS
20 #include "wx/wx.h"
21 #include "wx/log.h"
457814b5
JS
22#endif
23
75cd9288 24#include "wx/colordlg.h"
e4f3eb42 25#include "wx/numdlg.h"
3a5a2f56 26
91b8de8d 27#include "wx/image.h"
3a5a2f56 28#include "wx/imaglist.h"
457814b5 29#include "wx/treectrl.h"
b713f891 30#include "wx/math.h"
cf724bce 31
2c8e4738
VZ
32#ifdef __WIN32__
33 // this is not supported by native control
7cc8c988 34 #define NO_VARIABLE_HEIGHT
74b31181 35#endif
91b8de8d 36
ec312631 37#include "treetest.h"
9dfbf520 38
6f4aae60
CE
39#include "icon1.xpm"
40#include "icon2.xpm"
41#include "icon3.xpm"
42#include "icon4.xpm"
43#include "icon5.xpm"
dacaa6f1 44
b2c82287
WS
45#ifndef __WXMSW__
46 #include "../sample.xpm"
dacaa6f1 47#endif
6f4aae60 48
e1ee62bd 49// verify that the item is ok and insult the user if it is not
4693b20c
MB
50#define CHECK_ITEM( item ) if ( !item.IsOk() ) { \
51 wxMessageBox(wxT("Please select some item first!"), \
52 wxT("Tree sample error"), \
53 wxOK | wxICON_EXCLAMATION, \
54 this); \
55 return; \
5f939e78
VZ
56 }
57
d88a2485
VZ
58#define MENU_LINK(name) EVT_MENU(TreeTest_##name, MyFrame::On##name)
59
457814b5 60BEGIN_EVENT_TABLE(MyFrame, wxFrame)
a67c5664 61 EVT_IDLE(MyFrame::OnIdle)
7cc8c988
VZ
62 EVT_SIZE(MyFrame::OnSize)
63
618a5e38
RR
64 MENU_LINK(Quit)
65 MENU_LINK(About)
11ab1dfb 66
618a5e38
RR
67 MENU_LINK(TogButtons)
68 MENU_LINK(TogTwist)
69 MENU_LINK(TogLines)
70 MENU_LINK(TogEdit)
71 MENU_LINK(TogHideRoot)
72 MENU_LINK(TogRootLines)
73 MENU_LINK(TogBorder)
33984936 74 MENU_LINK(TogFullHighlight)
d88a2485
VZ
75 MENU_LINK(SetFgColour)
76 MENU_LINK(SetBgColour)
11ab1dfb
VZ
77 MENU_LINK(ResetStyle)
78
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)
dc631754 109 MENU_LINK(SelectRoot)
618a5e38
RR
110#undef MENU_LINK
111
457814b5
JS
112END_EVENT_TABLE()
113
484523cf
JS
114#if USE_GENERIC_TREECTRL
115BEGIN_EVENT_TABLE(MyTreeCtrl, wxGenericTreeCtrl)
116#else
457814b5 117BEGIN_EVENT_TABLE(MyTreeCtrl, wxTreeCtrl)
484523cf 118#endif
5f939e78
VZ
119 EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginDrag)
120 EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginRDrag)
5888ef1e 121 EVT_TREE_END_DRAG(TreeTest_Ctrl, MyTreeCtrl::OnEndDrag)
5f939e78
VZ
122 EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnBeginLabelEdit)
123 EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnEndLabelEdit)
124 EVT_TREE_DELETE_ITEM(TreeTest_Ctrl, MyTreeCtrl::OnDeleteItem)
74b31181 125#if 0 // there are so many of those that logging them causes flicker
5f939e78 126 EVT_TREE_GET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnGetInfo)
74b31181 127#endif
5f939e78
VZ
128 EVT_TREE_SET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnSetInfo)
129 EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanded)
130 EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanding)
131 EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsed)
132 EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsing)
e3d0aa77 133
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)
c545c230 138
5fd60e08
VZ
139 // so many differents ways to handle right mouse button clicks...
140 EVT_CONTEXT_MENU(MyTreeCtrl::OnContextMenu)
9489a535
KH
141 // EVT_TREE_ITEM_MENU is the preferred event for creating context menus
142 // on a tree control, because it includes the point of the click or item,
143 // meaning that no additional placement calculations are required.
5fd60e08
VZ
144 EVT_TREE_ITEM_MENU(TreeTest_Ctrl, MyTreeCtrl::OnItemMenu)
145 EVT_TREE_ITEM_RIGHT_CLICK(TreeTest_Ctrl, MyTreeCtrl::OnItemRClick)
146
c545c230
VZ
147 EVT_RIGHT_DOWN(MyTreeCtrl::OnRMouseDown)
148 EVT_RIGHT_UP(MyTreeCtrl::OnRMouseUp)
5f939e78 149 EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick)
457814b5
JS
150END_EVENT_TABLE()
151
152IMPLEMENT_APP(MyApp)
153
c89a6106 154bool MyApp::OnInit()
457814b5 155{
5f939e78 156 // Create the main frame window
4693b20c 157 MyFrame *frame = new MyFrame(wxT("wxTreeCtrl Test"), 50, 50, 450, 600);
457814b5 158
5f939e78 159 // Show the frame
9013f513 160 frame->Show(true);
5f939e78 161 SetTopWindow(frame);
457814b5 162
9013f513 163 return true;
3a5a2f56 164}
457814b5 165
457814b5 166
3a5a2f56
VZ
167// My frame constructor
168MyFrame::MyFrame(const wxString& title, int x, int y, int w, int h)
9013f513 169 : wxFrame((wxFrame *)NULL, wxID_ANY, title, wxPoint(x, y), wxSize(w, h)),
b29903d4
WS
170 m_treeCtrl(NULL)
171#if wxUSE_LOG
172 , m_textCtrl(NULL)
173#endif // wxUSE_LOG
3a5a2f56 174{
5f939e78
VZ
175 // This reduces flicker effects - even better would be to define
176 // OnEraseBackground to do nothing. When the tree control's scrollbars are
177 // show or hidden, the frame is sent a background erase event.
178 SetBackgroundColour(wxColour(255, 255, 255));
179
180 // Give it an icon
b2c82287 181 SetIcon(wxICON(sample));
5f939e78 182
801bb685 183#if wxUSE_MENUS
5f939e78
VZ
184 // Make a menubar
185 wxMenu *file_menu = new wxMenu,
618a5e38 186 *style_menu = new wxMenu,
5f939e78
VZ
187 *tree_menu = new wxMenu,
188 *item_menu = new wxMenu;
189
4693b20c 190 file_menu->Append(TreeTest_About, wxT("&About..."));
5f939e78 191 file_menu->AppendSeparator();
4693b20c 192 file_menu->Append(TreeTest_Quit, wxT("E&xit\tAlt-X"));
5f939e78 193
2153bf89
DS
194 style_menu->AppendCheckItem(TreeTest_TogButtons, wxT("Toggle &normal buttons"));
195 style_menu->AppendCheckItem(TreeTest_TogTwist, wxT("Toggle &twister buttons"));
196 style_menu->AppendCheckItem(TreeTest_ToggleButtons, wxT("Toggle image &buttons"));
618a5e38 197 style_menu->AppendSeparator();
2153bf89
DS
198 style_menu->AppendCheckItem(TreeTest_TogLines, wxT("Toggle &connecting lines"));
199 style_menu->AppendCheckItem(TreeTest_TogRootLines, wxT("Toggle &lines at root"));
200 style_menu->AppendCheckItem(TreeTest_TogHideRoot, wxT("Toggle &hidden root"));
201 style_menu->AppendCheckItem(TreeTest_TogBorder, wxT("Toggle &item border"));
202 style_menu->AppendCheckItem(TreeTest_TogFullHighlight, wxT("Toggle &full row highlight"));
203 style_menu->AppendCheckItem(TreeTest_TogEdit, wxT("Toggle &edit mode"));
7cc8c988 204#ifndef NO_MULTIPLE_SELECTION
2153bf89 205 style_menu->AppendCheckItem(TreeTest_ToggleSel, wxT("Toggle &selection mode"));
7cc8c988 206#endif // NO_MULTIPLE_SELECTION
2153bf89 207 style_menu->AppendCheckItem(TreeTest_ToggleImages, wxT("Toggle show ima&ges"));
4693b20c 208 style_menu->Append(TreeTest_SetImageSize, wxT("Set image si&ze..."));
d88a2485
VZ
209 style_menu->AppendSeparator();
210 style_menu->Append(TreeTest_SetFgColour, wxT("Set &foreground colour..."));
211 style_menu->Append(TreeTest_SetBgColour, wxT("Set &background colour..."));
11ab1dfb
VZ
212 style_menu->AppendSeparator();
213 style_menu->Append(TreeTest_ResetStyle, wxT("&Reset to default\tF10"));
618a5e38 214
3acd4349
VS
215 tree_menu->Append(TreeTest_Recreate, wxT("&Recreate the tree"));
216 tree_menu->Append(TreeTest_CollapseAndReset, wxT("C&ollapse and reset"));
5f939e78 217 tree_menu->AppendSeparator();
3acd4349
VS
218 tree_menu->Append(TreeTest_AddItem, wxT("Append a &new item"));
219 tree_menu->Append(TreeTest_InsertItem, wxT("&Insert a new item"));
220 tree_menu->Append(TreeTest_Delete, wxT("&Delete this item"));
221 tree_menu->Append(TreeTest_DeleteChildren, wxT("Delete &children"));
222 tree_menu->Append(TreeTest_DeleteAll, wxT("Delete &all items"));
dc631754 223 tree_menu->Append(TreeTest_SelectRoot, wxT("Select root item"));
5f939e78 224 tree_menu->AppendSeparator();
3acd4349
VS
225 tree_menu->Append(TreeTest_Count, wxT("Count children of current item"));
226 tree_menu->Append(TreeTest_CountRec, wxT("Recursively count children of current item"));
5fd11f09 227 tree_menu->AppendSeparator();
3acd4349
VS
228 tree_menu->Append(TreeTest_Sort, wxT("Sort children of current item"));
229 tree_menu->Append(TreeTest_SortRev, wxT("Sort in reversed order"));
5f939e78 230 tree_menu->AppendSeparator();
3acd4349 231 tree_menu->Append(TreeTest_EnsureVisible, wxT("Make the last item &visible"));
cf724bce 232 tree_menu->AppendSeparator();
3acd4349
VS
233 tree_menu->Append(TreeTest_IncIndent, wxT("Add 5 points to indentation\tAlt-I"));
234 tree_menu->Append(TreeTest_DecIndent, wxT("Reduce indentation by 5 points\tAlt-R"));
cf724bce 235 tree_menu->AppendSeparator();
3acd4349
VS
236 tree_menu->Append(TreeTest_IncSpacing, wxT("Add 5 points to spacing\tCtrl-I"));
237 tree_menu->Append(TreeTest_DecSpacing, wxT("Reduce spacing by 5 points\tCtrl-R"));
5f939e78 238
3acd4349
VS
239 item_menu->Append(TreeTest_Dump, wxT("&Dump item children"));
240 item_menu->Append(TreeTest_Rename, wxT("&Rename item..."));
91b8de8d 241
5f939e78 242 item_menu->AppendSeparator();
3acd4349
VS
243 item_menu->Append(TreeTest_SetBold, wxT("Make item &bold"));
244 item_menu->Append(TreeTest_ClearBold, wxT("Make item &not bold"));
9dfbf520 245 item_menu->AppendSeparator();
3acd4349 246 item_menu->Append(TreeTest_ToggleIcon, wxT("Toggle the item's &icon"));
9dfbf520
VZ
247
248#ifndef NO_MULTIPLE_SELECTION
249 item_menu->AppendSeparator();
3acd4349
VS
250 item_menu->Append(TreeTest_DumpSelected, wxT("Dump selected items\tAlt-D"));
251 item_menu->Append(TreeTest_Select, wxT("Select current item\tAlt-S"));
252 item_menu->Append(TreeTest_Unselect, wxT("Unselect everything\tAlt-U"));
7cc8c988 253#endif // NO_MULTIPLE_SELECTION
5f939e78
VZ
254
255 wxMenuBar *menu_bar = new wxMenuBar;
3acd4349
VS
256 menu_bar->Append(file_menu, wxT("&File"));
257 menu_bar->Append(style_menu, wxT("&Style"));
258 menu_bar->Append(tree_menu, wxT("&Tree"));
259 menu_bar->Append(item_menu, wxT("&Item"));
5f939e78 260 SetMenuBar(menu_bar);
801bb685 261#endif // wxUSE_MENUS
2c8e4738 262
998cee67
VZ
263 m_panel = new wxPanel(this);
264
b29903d4 265#if wxUSE_LOG
11ab1dfb 266 // create the controls
998cee67 267 m_textCtrl = new wxTextCtrl(m_panel, wxID_ANY, wxT(""),
5f939e78
VZ
268 wxDefaultPosition, wxDefaultSize,
269 wxTE_MULTILINE | wxSUNKEN_BORDER);
b29903d4 270#endif // wxUSE_LOG
5f939e78 271
11ab1dfb
VZ
272 CreateTreeWithDefStyle();
273
9013f513 274 menu_bar->Check(TreeTest_ToggleImages, true);
11ab1dfb 275
a67c5664
VZ
276#if wxUSE_STATUSBAR
277 // create a status bar
278 CreateStatusBar(2);
279#endif // wxUSE_STATUSBAR
3a5a2f56 280
b29903d4 281#if wxUSE_LOG
a367b9b3 282#ifdef __WXMOTIF__
5f939e78
VZ
283 // For some reason, we get a memcpy crash in wxLogStream::DoLogStream
284 // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc?
285 delete wxLog::SetActiveTarget(new wxLogStderr);
a367b9b3 286#else
5f939e78 287 // set our text control as the log target
7cc8c988 288 wxLogTextCtrl *logWindow = new wxLogTextCtrl(m_textCtrl);
5f939e78 289 delete wxLog::SetActiveTarget(logWindow);
a367b9b3 290#endif
b29903d4 291#endif // wxUSE_LOG
457814b5
JS
292}
293
c89a6106 294MyFrame::~MyFrame()
457814b5 295{
b29903d4 296#if wxUSE_LOG
5f939e78 297 delete wxLog::SetActiveTarget(NULL);
b29903d4 298#endif // wxUSE_LOG
457814b5
JS
299}
300
11ab1dfb
VZ
301void MyFrame::CreateTreeWithDefStyle()
302{
303 long style = wxTR_DEFAULT_STYLE |
304#ifndef NO_VARIABLE_HEIGHT
305 wxTR_HAS_VARIABLE_ROW_HEIGHT |
306#endif
307 wxTR_EDIT_LABELS;
308
309 CreateTree(style | wxSUNKEN_BORDER);
310
311 // as we don't know what wxTR_DEFAULT_STYLE could contain, test for
312 // everything
313 wxMenuBar *mbar = GetMenuBar();
314 mbar->Check(TreeTest_TogButtons, (style & wxTR_HAS_BUTTONS) != 0);
315 mbar->Check(TreeTest_TogButtons, (style & wxTR_TWIST_BUTTONS) != 0);
316 mbar->Check(TreeTest_TogLines, (style & wxTR_NO_LINES) == 0);
317 mbar->Check(TreeTest_TogRootLines, (style & wxTR_LINES_AT_ROOT) != 0);
318 mbar->Check(TreeTest_TogHideRoot, (style & wxTR_HIDE_ROOT) != 0);
319 mbar->Check(TreeTest_TogEdit, (style & wxTR_EDIT_LABELS) != 0);
320 mbar->Check(TreeTest_TogBorder, (style & wxTR_ROW_LINES) != 0);
321 mbar->Check(TreeTest_TogFullHighlight, (style & wxTR_FULL_ROW_HIGHLIGHT) != 0);
322}
323
324void MyFrame::CreateTree(long style)
325{
998cee67 326 m_treeCtrl = new MyTreeCtrl(m_panel, TreeTest_Ctrl,
11ab1dfb
VZ
327 wxDefaultPosition, wxDefaultSize,
328 style);
329 Resize();
330}
331
332void MyFrame::TogStyle(int id, long flag)
618a5e38 333{
11ab1dfb
VZ
334 long style = m_treeCtrl->GetWindowStyle() ^ flag;
335
336 // most treectrl styles can't be changed on the fly using the native
337 // control and the tree must be recreated
338#ifndef __WXMSW__
339 m_treeCtrl->SetWindowStyle(style);
340#else // MSW
341 delete m_treeCtrl;
342 CreateTree(style);
343#endif // !MSW/MSW
344
345 GetMenuBar()->Check(id, (style & flag) != 0);
618a5e38
RR
346}
347
a67c5664
VZ
348void MyFrame::OnIdle(wxIdleEvent& event)
349{
350#if wxUSE_STATUSBAR
e4a8a172
VZ
351 if ( m_treeCtrl )
352 {
353 wxTreeItemId idRoot = m_treeCtrl->GetRootItem();
354
355 SetStatusText(wxString::Format
356 (
357 _T("Root/last item is %svisible/%svisible"),
358 m_treeCtrl->IsVisible(idRoot) ? _T("") : _T("not "),
359 m_treeCtrl->IsVisible(m_treeCtrl->GetLastChild(idRoot))
360 ? _T("") : _T("not ")
361 ), 1);
362 }
a67c5664
VZ
363#endif // wxUSE_STATUSBAR
364
365 event.Skip();
366}
367
7cc8c988
VZ
368void MyFrame::OnSize(wxSizeEvent& event)
369{
925e9792 370 if ( m_treeCtrl
b29903d4 371#if wxUSE_LOG
925e9792 372 && m_textCtrl
b29903d4
WS
373#endif // wxUSE_LOG
374 )
7cc8c988 375 {
11ab1dfb 376 Resize();
7cc8c988
VZ
377 }
378
379 event.Skip();
380}
381
11ab1dfb 382void MyFrame::Resize()
7cc8c988 383{
11ab1dfb 384 wxSize size = GetClientSize();
b29903d4
WS
385 m_treeCtrl->SetSize(0, 0, size.x, size.y
386#if !wxUSE_LOG
387 );
388#else
389 *2/3);
7cc8c988 390 m_textCtrl->SetSize(0, 2*size.y/3, size.x, size.y/3);
b29903d4 391#endif
7cc8c988
VZ
392}
393
3a5a2f56 394void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
457814b5 395{
9013f513 396 Close(true);
457814b5
JS
397}
398
3a5a2f56 399void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
457814b5 400{
3acd4349
VS
401 wxMessageBox(wxT("Tree test sample\n")
402 wxT("(c) Julian Smart 1997, Vadim Zeitlin 1998"),
403 wxT("About tree test"),
5f939e78 404 wxOK | wxICON_INFORMATION, this);
e1ee62bd
VZ
405}
406
407void MyFrame::OnRename(wxCommandEvent& WXUNUSED(event))
408{
5f939e78 409 wxTreeItemId item = m_treeCtrl->GetSelection();
e1ee62bd 410
5f939e78 411 CHECK_ITEM( item );
e1ee62bd 412
5ea47806
VZ
413 // old code - now we edit in place
414#if 0
5f939e78 415 static wxString s_text;
3acd4349 416 s_text = wxGetTextFromUser(wxT("New name: "), wxT("Tree sample question"),
5f939e78 417 s_text, this);
b713f891 418 if ( !s_text.empty() )
5f939e78
VZ
419 {
420 m_treeCtrl->SetItemText(item, s_text);
421 }
5ea47806
VZ
422#endif // 0
423
424 // TODO demonstrate creating a custom edit control...
425 (void)m_treeCtrl->EditLabel(item);
e1ee62bd
VZ
426}
427
5fd11f09
RR
428void MyFrame::OnCount(wxCommandEvent& WXUNUSED(event))
429{
430 wxTreeItemId item = m_treeCtrl->GetSelection();
431
432 CHECK_ITEM( item );
433
9013f513 434 int i = m_treeCtrl->GetChildrenCount( item, false );
75c74ca0 435
247e5b16 436 wxLogMessage(wxT("%d children"), i);
5fd11f09
RR
437}
438
439void MyFrame::OnCountRec(wxCommandEvent& WXUNUSED(event))
440{
441 wxTreeItemId item = m_treeCtrl->GetSelection();
442
443 CHECK_ITEM( item );
444
445 int i = m_treeCtrl->GetChildrenCount( item );
75c74ca0 446
247e5b16 447 wxLogMessage(wxT("%d children"), i);
5fd11f09
RR
448}
449
e1ee62bd
VZ
450void MyFrame::DoSort(bool reverse)
451{
5f939e78 452 wxTreeItemId item = m_treeCtrl->GetSelection();
e1ee62bd 453
5f939e78 454 CHECK_ITEM( item );
457814b5 455
5f939e78 456 m_treeCtrl->DoSortChildren(item, reverse);
457814b5
JS
457}
458
4832f7c0
VZ
459void MyFrame::OnDump(wxCommandEvent& WXUNUSED(event))
460{
5f939e78 461 wxTreeItemId root = m_treeCtrl->GetSelection();
e1ee62bd 462
5f939e78 463 CHECK_ITEM( root );
e1ee62bd 464
2d75caaa 465 m_treeCtrl->GetItemsRecursively(root);
4832f7c0
VZ
466}
467
9dfbf520
VZ
468#ifndef NO_MULTIPLE_SELECTION
469
11ab1dfb 470void MyFrame::OnToggleSel(wxCommandEvent& event)
7cc8c988 471{
11ab1dfb 472 TogStyle(event.GetId(), wxTR_MULTIPLE);
7cc8c988
VZ
473}
474
91b8de8d
RR
475void MyFrame::OnDumpSelected(wxCommandEvent& WXUNUSED(event))
476{
9dfbf520 477 wxArrayTreeItemIds array;
91b8de8d 478
9dfbf520 479 size_t count = m_treeCtrl->GetSelections(array);
b143cf70 480 wxLogMessage(wxT("%u items selected"), unsigned(count));
91b8de8d 481
9dfbf520
VZ
482 for ( size_t n = 0; n < count; n++ )
483 {
4693b20c 484 wxLogMessage(wxT("\t%s"), m_treeCtrl->GetItemText(array.Item(n)).c_str());
9dfbf520
VZ
485 }
486}
487
256b8649 488void MyFrame::OnSelect(wxCommandEvent& WXUNUSED(event))
9dfbf520
VZ
489{
490 m_treeCtrl->SelectItem(m_treeCtrl->GetSelection());
491}
492
dc631754
VZ
493void MyFrame::OnSelectRoot(wxCommandEvent& WXUNUSED(event))
494{
495 m_treeCtrl->SelectItem(m_treeCtrl->GetRootItem());
496}
497
256b8649 498void MyFrame::OnUnselect(wxCommandEvent& WXUNUSED(event))
9dfbf520
VZ
499{
500 m_treeCtrl->UnselectAll();
91b8de8d
RR
501}
502
9dfbf520
VZ
503#endif // NO_MULTIPLE_SELECTION
504
add28c55
VZ
505void MyFrame::DoSetBold(bool bold)
506{
5f939e78 507 wxTreeItemId item = m_treeCtrl->GetSelection();
e1ee62bd 508
5f939e78 509 CHECK_ITEM( item );
e1ee62bd 510
5f939e78 511 m_treeCtrl->SetItemBold(item, bold);
add28c55
VZ
512}
513
ff5bf259
VZ
514void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event))
515{
5f939e78 516 wxTreeItemId item = m_treeCtrl->GetSelection();
e1ee62bd 517
5f939e78 518 CHECK_ITEM( item );
e1ee62bd 519
5f939e78 520 m_treeCtrl->Delete(item);
ff5bf259
VZ
521}
522
372edb9d
VZ
523void MyFrame::OnDeleteChildren(wxCommandEvent& WXUNUSED(event))
524{
5f939e78 525 wxTreeItemId item = m_treeCtrl->GetSelection();
e1ee62bd 526
5f939e78 527 CHECK_ITEM( item );
e1ee62bd 528
5f939e78 529 m_treeCtrl->DeleteChildren(item);
372edb9d
VZ
530}
531
ff5bf259
VZ
532void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event))
533{
5f939e78 534 m_treeCtrl->DeleteAllItems();
ff5bf259
VZ
535}
536
537void MyFrame::OnRecreate(wxCommandEvent& event)
538{
5f939e78 539 OnDeleteAll(event);
618a5e38 540 m_treeCtrl->AddTestItemsToTree(5, 2);
ff5bf259
VZ
541}
542
256b8649 543void MyFrame::OnSetImageSize(wxCommandEvent& WXUNUSED(event))
2c8e4738 544{
3acd4349
VS
545 int size = wxGetNumberFromUser(wxT("Enter the size for the images to use"),
546 wxT("Size: "),
547 wxT("TreeCtrl sample"),
618a5e38 548 m_treeCtrl->ImageSize());
2c8e4738
VZ
549 if ( size == -1 )
550 return;
551
618a5e38 552 m_treeCtrl->CreateImageList(size);
9013f513 553 wxGetApp().SetShowImages(true);
2c8e4738
VZ
554}
555
256b8649 556void MyFrame::OnToggleImages(wxCommandEvent& WXUNUSED(event))
2c8e4738 557{
f6bcfd97
BP
558 if ( wxGetApp().ShowImages() )
559 {
560 m_treeCtrl->CreateImageList(-1);
9013f513 561 wxGetApp().SetShowImages(false);
f6bcfd97
BP
562 }
563 else
564 {
618a5e38 565 m_treeCtrl->CreateImageList(0);
9013f513 566 wxGetApp().SetShowImages(true);
f6bcfd97 567 }
618a5e38 568}
2c8e4738 569
256b8649 570void MyFrame::OnToggleButtons(wxCommandEvent& WXUNUSED(event))
618a5e38 571{
a3e377a3 572#if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
618a5e38
RR
573 if ( wxGetApp().ShowButtons() )
574 {
575 m_treeCtrl->CreateButtonsImageList(-1);
9013f513 576 wxGetApp().SetShowButtons(false);
618a5e38
RR
577 }
578 else
579 {
580 m_treeCtrl->CreateButtonsImageList(15);
9013f513 581 wxGetApp().SetShowButtons(true);
618a5e38 582 }
a3e377a3 583#endif
2c8e4738
VZ
584}
585
256b8649 586void MyFrame::OnCollapseAndReset(wxCommandEvent& WXUNUSED(event))
dd3646fd 587{
5f939e78 588 m_treeCtrl->CollapseAndReset(m_treeCtrl->GetRootItem());
dd3646fd
VZ
589}
590
256b8649 591void MyFrame::OnEnsureVisible(wxCommandEvent& WXUNUSED(event))
f65635b5
VZ
592{
593 m_treeCtrl->DoEnsureVisible();
594}
595
75c74ca0
VZ
596void MyFrame::OnInsertItem(wxCommandEvent& WXUNUSED(event))
597{
2c8e4738 598 int image = wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_File : -1;
3acd4349 599 m_treeCtrl->InsertItem(m_treeCtrl->GetRootItem(), image, wxT("2nd item"));
75c74ca0
VZ
600}
601
5f939e78
VZ
602void MyFrame::OnAddItem(wxCommandEvent& WXUNUSED(event))
603{
604 static int s_num = 0;
605
606 wxString text;
4693b20c 607 text.Printf(wxT("Item #%d"), ++s_num);
5f939e78
VZ
608
609 m_treeCtrl->AppendItem(m_treeCtrl->GetRootItem(),
112c5086
RR
610 text /*,
611 MyTreeCtrl::TreeCtrlIcon_File */ );
5f939e78
VZ
612}
613
cf724bce
RR
614void MyFrame::OnIncIndent(wxCommandEvent& WXUNUSED(event))
615{
616 unsigned int indent = m_treeCtrl->GetIndent();
617 if (indent < 100)
618 m_treeCtrl->SetIndent( indent+5 );
619}
620
621void MyFrame::OnDecIndent(wxCommandEvent& WXUNUSED(event))
622{
623 unsigned int indent = m_treeCtrl->GetIndent();
624 if (indent > 10)
625 m_treeCtrl->SetIndent( indent-5 );
626}
627
628void MyFrame::OnIncSpacing(wxCommandEvent& WXUNUSED(event))
629{
630 unsigned int indent = m_treeCtrl->GetSpacing();
631 if (indent < 100)
632 m_treeCtrl->SetSpacing( indent+5 );
633}
634
635void MyFrame::OnDecSpacing(wxCommandEvent& WXUNUSED(event))
636{
637 unsigned int indent = m_treeCtrl->GetSpacing();
638 if (indent > 10)
639 m_treeCtrl->SetSpacing( indent-5 );
640}
641
9dfbf520
VZ
642void MyFrame::OnToggleIcon(wxCommandEvent& WXUNUSED(event))
643{
644 wxTreeItemId item = m_treeCtrl->GetSelection();
645
646 CHECK_ITEM( item );
647
648 m_treeCtrl->DoToggleIcon(item);
649}
650
d88a2485
VZ
651void MyFrame::OnSetFgColour(wxCommandEvent& WXUNUSED(event))
652{
653 wxColour col = wxGetColourFromUser(this, m_treeCtrl->GetForegroundColour());
654 if ( col.Ok() )
655 m_treeCtrl->SetForegroundColour(col);
656}
657
658void MyFrame::OnSetBgColour(wxCommandEvent& WXUNUSED(event))
659{
660 wxColour col = wxGetColourFromUser(this, m_treeCtrl->GetBackgroundColour());
661 if ( col.Ok() )
662 m_treeCtrl->SetBackgroundColour(col);
663}
664
3a5a2f56 665// MyTreeCtrl implementation
484523cf
JS
666#if USE_GENERIC_TREECTRL
667IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl, wxGenericTreeCtrl)
668#else
23fd5130 669IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl, wxTreeCtrl)
484523cf 670#endif
23fd5130 671
3a5a2f56
VZ
672MyTreeCtrl::MyTreeCtrl(wxWindow *parent, const wxWindowID id,
673 const wxPoint& pos, const wxSize& size,
674 long style)
675 : wxTreeCtrl(parent, id, pos, size, style)
457814b5 676{
9013f513 677 m_reverseSort = false;
91b8de8d 678
2c8e4738 679 CreateImageList();
91b8de8d 680
2c8e4738 681 // Add some items to the tree
618a5e38 682 AddTestItemsToTree(5, 2);
2c8e4738
VZ
683}
684
685void MyTreeCtrl::CreateImageList(int size)
686{
2c8e4738
VZ
687 if ( size == -1 )
688 {
618a5e38
RR
689 SetImageList(NULL);
690 return;
2c8e4738 691 }
618a5e38
RR
692 if ( size == 0 )
693 size = m_imageSize;
f6bcfd97 694 else
618a5e38
RR
695 m_imageSize = size;
696
697 // Make an image list containing small icons
9013f513 698 wxImageList *images = new wxImageList(size, size, true);
e1ee62bd 699
618a5e38 700 // should correspond to TreeCtrlIcon_xxx enum
618a5e38
RR
701 wxBusyCursor wait;
702 wxIcon icons[5];
6f4aae60
CE
703 icons[0] = wxIcon(icon1_xpm);
704 icons[1] = wxIcon(icon2_xpm);
705 icons[2] = wxIcon(icon3_xpm);
706 icons[3] = wxIcon(icon4_xpm);
707 icons[4] = wxIcon(icon5_xpm);
618a5e38
RR
708
709 int sizeOrig = icons[0].GetWidth();
710 for ( size_t i = 0; i < WXSIZEOF(icons); i++ )
711 {
712 if ( size == sizeOrig )
2c8e4738 713 {
618a5e38 714 images->Add(icons[i]);
2c8e4738 715 }
618a5e38
RR
716 else
717 {
9f24e0b5 718 images->Add(wxBitmap(wxBitmap(icons[i]).ConvertToImage().Rescale(size, size)));
618a5e38
RR
719 }
720 }
618a5e38
RR
721
722 AssignImageList(images);
723}
724
256b8649 725#if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
618a5e38
RR
726void MyTreeCtrl::CreateButtonsImageList(int size)
727{
728 if ( size == -1 )
729 {
730 SetButtonsImageList(NULL);
731 return;
732 }
733
734 // Make an image list containing small icons
9013f513 735 wxImageList *images = new wxImageList(size, size, true);
618a5e38
RR
736
737 // should correspond to TreeCtrlIcon_xxx enum
618a5e38
RR
738 wxBusyCursor wait;
739 wxIcon icons[4];
6f4aae60
CE
740 icons[0] = wxIcon(icon3_xpm); // closed
741 icons[1] = wxIcon(icon3_xpm); // closed, selected
742 icons[2] = wxIcon(icon5_xpm); // open
743 icons[3] = wxIcon(icon5_xpm); // open, selected
618a5e38
RR
744
745 for ( size_t i = 0; i < WXSIZEOF(icons); i++ )
746 {
747 int sizeOrig = icons[i].GetWidth();
748 if ( size == sizeOrig )
749 {
750 images->Add(icons[i]);
751 }
752 else
753 {
8dda7c0c 754 images->Add(wxBitmap(wxBitmap(icons[i]).ConvertToImage().Rescale(size, size)));
618a5e38 755 }
2c8e4738 756 }
457814b5 757
618a5e38 758 AssignButtonsImageList(images);
256b8649
JS
759#else
760void MyTreeCtrl::CreateButtonsImageList(int WXUNUSED(size))
761{
a3e377a3 762#endif
457814b5
JS
763}
764
e1ee62bd
VZ
765int MyTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
766 const wxTreeItemId& item2)
767{
5f939e78
VZ
768 if ( m_reverseSort )
769 {
770 // just exchange 1st and 2nd items
771 return wxTreeCtrl::OnCompareItems(item2, item1);
772 }
773 else
774 {
775 return wxTreeCtrl::OnCompareItems(item1, item2);
776 }
e1ee62bd
VZ
777}
778
3a5a2f56
VZ
779void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId& idParent,
780 size_t numChildren,
4832f7c0
VZ
781 size_t depth,
782 size_t folder)
457814b5 783{
f65635b5 784 if ( depth > 0 )
3a5a2f56 785 {
74b31181
VZ
786 bool hasChildren = depth > 1;
787
f65635b5
VZ
788 wxString str;
789 for ( size_t n = 0; n < numChildren; n++ )
790 {
791 // at depth 1 elements won't have any more children
74b31181 792 if ( hasChildren )
b143cf70 793 str.Printf(wxT("%s child %u"), wxT("Folder"), unsigned(n + 1));
74b31181 794 else
b143cf70 795 str.Printf(wxT("%s child %u.%u"), wxT("File"), unsigned(folder), unsigned(n + 1));
f65635b5 796
74b31181
VZ
797 // here we pass to AppendItem() normal and selected item images (we
798 // suppose that selected image follows the normal one in the enum)
2c8e4738
VZ
799 int image, imageSel;
800 if ( wxGetApp().ShowImages() )
801 {
802 image = depth == 1 ? TreeCtrlIcon_File : TreeCtrlIcon_Folder;
803 imageSel = image + 1;
804 }
805 else
806 {
807 image = imageSel = -1;
808 }
809 wxTreeItemId id = AppendItem(idParent, str, image, imageSel,
f65635b5
VZ
810 new MyTreeItemData(str));
811
74b31181 812 // and now we also set the expanded one (only for the folders)
2c8e4738 813 if ( hasChildren && wxGetApp().ShowImages() )
74b31181
VZ
814 {
815 SetItemImage(id, TreeCtrlIcon_FolderOpened,
816 wxTreeItemIcon_Expanded);
817 }
818
f65635b5 819 // remember the last child for OnEnsureVisible()
74b31181 820 if ( !hasChildren && n == numChildren - 1 )
f65635b5
VZ
821 {
822 m_lastItem = id;
823 }
824
825 AddItemsRecursively(id, numChildren, depth - 1, n + 1);
826 }
3a5a2f56 827 }
f65635b5 828 //else: done!
457814b5
JS
829}
830
3a5a2f56
VZ
831void MyTreeCtrl::AddTestItemsToTree(size_t numChildren,
832 size_t depth)
457814b5 833{
2c8e4738 834 int image = wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_Folder : -1;
3acd4349 835 wxTreeItemId rootId = AddRoot(wxT("Root"),
2c8e4738 836 image, image,
3acd4349 837 new MyTreeItemData(wxT("Root item")));
2c8e4738
VZ
838 if ( image != -1 )
839 {
840 SetItemImage(rootId, TreeCtrlIcon_FolderOpened, wxTreeItemIcon_Expanded);
841 }
457814b5 842
5f939e78 843 AddItemsRecursively(rootId, numChildren, depth, 0);
9ec64fa7
VZ
844
845 // set some colours/fonts for testing
846 SetItemFont(rootId, *wxITALIC_FONT);
847
2d75caaa 848 wxTreeItemIdValue cookie;
9ec64fa7
VZ
849 wxTreeItemId id = GetFirstChild(rootId, cookie);
850 SetItemTextColour(id, *wxBLUE);
851
852 id = GetNextChild(rootId, cookie);
853 id = GetNextChild(rootId, cookie);
854 SetItemTextColour(id, *wxRED);
855 SetItemBackgroundColour(id, *wxLIGHT_GREY);
4832f7c0
VZ
856}
857
2d75caaa
VZ
858void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId& idParent,
859 wxTreeItemIdValue cookie)
4832f7c0 860{
5f939e78 861 wxTreeItemId id;
4832f7c0 862
2d75caaa 863 if ( !cookie )
5f939e78
VZ
864 id = GetFirstChild(idParent, cookie);
865 else
866 id = GetNextChild(idParent, cookie);
4832f7c0 867
2d75caaa 868 if ( !id.IsOk() )
5f939e78 869 return;
4832f7c0 870
9dfbf520 871 wxString text = GetItemText(id);
5f939e78 872 wxLogMessage(text);
4832f7c0 873
5f939e78 874 if (ItemHasChildren(id))
2d75caaa 875 GetItemsRecursively(id);
4832f7c0 876
5f939e78 877 GetItemsRecursively(idParent, cookie);
457814b5
JS
878}
879
9dfbf520
VZ
880void MyTreeCtrl::DoToggleIcon(const wxTreeItemId& item)
881{
0c36b36e
VS
882 int image = (GetItemImage(item) == TreeCtrlIcon_Folder)
883 ? TreeCtrlIcon_File
884 : TreeCtrlIcon_Folder;
885 SetItemImage(item, image, wxTreeItemIcon_Normal);
886
887 image = (GetItemImage(item) == TreeCtrlIcon_FolderSelected)
888 ? TreeCtrlIcon_FileSelected
889 : TreeCtrlIcon_FolderSelected;
890 SetItemImage(item, image, wxTreeItemIcon_Selected);
9dfbf520
VZ
891}
892
5c2603c2
VZ
893void MyTreeCtrl::LogEvent(const wxChar *name, const wxTreeEvent& event)
894{
895 wxTreeItemId item = event.GetItem();
896 wxString text;
897 if ( item.IsOk() )
898 text << _T('"') << GetItemText(item).c_str() << _T('"');
899 else
900 text = _T("invalid item");
901 wxLogMessage(wxT("%s(%s)"), name, text.c_str());
902}
4832f7c0 903
3a5a2f56 904// avoid repetition
4693b20c
MB
905#define TREE_EVENT_HANDLER(name) \
906void MyTreeCtrl::name(wxTreeEvent& event) \
907{ \
5c2603c2 908 LogEvent(_T(#name), event); \
1cf1238d 909 SetLastItem(wxTreeItemId()); \
4693b20c 910 event.Skip(); \
5f939e78 911}
457814b5 912
3a5a2f56 913TREE_EVENT_HANDLER(OnBeginRDrag)
3a5a2f56
VZ
914TREE_EVENT_HANDLER(OnDeleteItem)
915TREE_EVENT_HANDLER(OnGetInfo)
916TREE_EVENT_HANDLER(OnSetInfo)
917TREE_EVENT_HANDLER(OnItemExpanded)
918TREE_EVENT_HANDLER(OnItemExpanding)
919TREE_EVENT_HANDLER(OnItemCollapsed)
920TREE_EVENT_HANDLER(OnSelChanged)
921TREE_EVENT_HANDLER(OnSelChanging)
922
923#undef TREE_EVENT_HANDLER
924
1944ad76
VZ
925void LogKeyEvent(const wxChar *name, const wxKeyEvent& event)
926{
927 wxString key;
0c6099b7 928 long keycode = event.GetKeyCode();
1944ad76 929 {
0c6099b7 930 switch ( keycode )
1944ad76 931 {
0c6099b7
RN
932 case WXK_BACK: key = wxT("BACK"); break;
933 case WXK_TAB: key = wxT("TAB"); break;
934 case WXK_RETURN: key = wxT("RETURN"); break;
935 case WXK_ESCAPE: key = wxT("ESCAPE"); break;
936 case WXK_SPACE: key = wxT("SPACE"); break;
937 case WXK_DELETE: key = wxT("DELETE"); break;
938 case WXK_START: key = wxT("START"); break;
939 case WXK_LBUTTON: key = wxT("LBUTTON"); break;
940 case WXK_RBUTTON: key = wxT("RBUTTON"); break;
941 case WXK_CANCEL: key = wxT("CANCEL"); break;
942 case WXK_MBUTTON: key = wxT("MBUTTON"); break;
943 case WXK_CLEAR: key = wxT("CLEAR"); break;
944 case WXK_SHIFT: key = wxT("SHIFT"); break;
945 case WXK_ALT: key = wxT("ALT"); break;
946 case WXK_CONTROL: key = wxT("CONTROL"); break;
947 case WXK_MENU: key = wxT("MENU"); break;
948 case WXK_PAUSE: key = wxT("PAUSE"); break;
949 case WXK_CAPITAL: key = wxT("CAPITAL"); break;
0c6099b7
RN
950 case WXK_END: key = wxT("END"); break;
951 case WXK_HOME: key = wxT("HOME"); break;
952 case WXK_LEFT: key = wxT("LEFT"); break;
953 case WXK_UP: key = wxT("UP"); break;
954 case WXK_RIGHT: key = wxT("RIGHT"); break;
955 case WXK_DOWN: key = wxT("DOWN"); break;
956 case WXK_SELECT: key = wxT("SELECT"); break;
957 case WXK_PRINT: key = wxT("PRINT"); break;
958 case WXK_EXECUTE: key = wxT("EXECUTE"); break;
959 case WXK_SNAPSHOT: key = wxT("SNAPSHOT"); break;
960 case WXK_INSERT: key = wxT("INSERT"); break;
961 case WXK_HELP: key = wxT("HELP"); break;
962 case WXK_NUMPAD0: key = wxT("NUMPAD0"); break;
963 case WXK_NUMPAD1: key = wxT("NUMPAD1"); break;
964 case WXK_NUMPAD2: key = wxT("NUMPAD2"); break;
965 case WXK_NUMPAD3: key = wxT("NUMPAD3"); break;
966 case WXK_NUMPAD4: key = wxT("NUMPAD4"); break;
967 case WXK_NUMPAD5: key = wxT("NUMPAD5"); break;
968 case WXK_NUMPAD6: key = wxT("NUMPAD6"); break;
969 case WXK_NUMPAD7: key = wxT("NUMPAD7"); break;
970 case WXK_NUMPAD8: key = wxT("NUMPAD8"); break;
971 case WXK_NUMPAD9: key = wxT("NUMPAD9"); break;
972 case WXK_MULTIPLY: key = wxT("MULTIPLY"); break;
973 case WXK_ADD: key = wxT("ADD"); break;
974 case WXK_SEPARATOR: key = wxT("SEPARATOR"); break;
975 case WXK_SUBTRACT: key = wxT("SUBTRACT"); break;
976 case WXK_DECIMAL: key = wxT("DECIMAL"); break;
977 case WXK_DIVIDE: key = wxT("DIVIDE"); break;
978 case WXK_F1: key = wxT("F1"); break;
979 case WXK_F2: key = wxT("F2"); break;
980 case WXK_F3: key = wxT("F3"); break;
981 case WXK_F4: key = wxT("F4"); break;
982 case WXK_F5: key = wxT("F5"); break;
983 case WXK_F6: key = wxT("F6"); break;
984 case WXK_F7: key = wxT("F7"); break;
985 case WXK_F8: key = wxT("F8"); break;
986 case WXK_F9: key = wxT("F9"); break;
987 case WXK_F10: key = wxT("F10"); break;
988 case WXK_F11: key = wxT("F11"); break;
989 case WXK_F12: key = wxT("F12"); break;
990 case WXK_F13: key = wxT("F13"); break;
991 case WXK_F14: key = wxT("F14"); break;
992 case WXK_F15: key = wxT("F15"); break;
993 case WXK_F16: key = wxT("F16"); break;
994 case WXK_F17: key = wxT("F17"); break;
995 case WXK_F18: key = wxT("F18"); break;
996 case WXK_F19: key = wxT("F19"); break;
997 case WXK_F20: key = wxT("F20"); break;
998 case WXK_F21: key = wxT("F21"); break;
999 case WXK_F22: key = wxT("F22"); break;
1000 case WXK_F23: key = wxT("F23"); break;
1001 case WXK_F24: key = wxT("F24"); break;
1002 case WXK_NUMLOCK: key = wxT("NUMLOCK"); break;
1003 case WXK_SCROLL: key = wxT("SCROLL"); break;
1004 case WXK_PAGEUP: key = wxT("PAGEUP"); break;
1005 case WXK_PAGEDOWN: key = wxT("PAGEDOWN"); break;
1006 case WXK_NUMPAD_SPACE: key = wxT("NUMPAD_SPACE"); break;
1007 case WXK_NUMPAD_TAB: key = wxT("NUMPAD_TAB"); break;
1008 case WXK_NUMPAD_ENTER: key = wxT("NUMPAD_ENTER"); break;
1009 case WXK_NUMPAD_F1: key = wxT("NUMPAD_F1"); break;
1010 case WXK_NUMPAD_F2: key = wxT("NUMPAD_F2"); break;
1011 case WXK_NUMPAD_F3: key = wxT("NUMPAD_F3"); break;
1012 case WXK_NUMPAD_F4: key = wxT("NUMPAD_F4"); break;
1013 case WXK_NUMPAD_HOME: key = wxT("NUMPAD_HOME"); break;
1014 case WXK_NUMPAD_LEFT: key = wxT("NUMPAD_LEFT"); break;
1015 case WXK_NUMPAD_UP: key = wxT("NUMPAD_UP"); break;
1016 case WXK_NUMPAD_RIGHT: key = wxT("NUMPAD_RIGHT"); break;
1017 case WXK_NUMPAD_DOWN: key = wxT("NUMPAD_DOWN"); break;
0c6099b7
RN
1018 case WXK_NUMPAD_PAGEUP: key = wxT("NUMPAD_PAGEUP"); break;
1019 case WXK_NUMPAD_PAGEDOWN: key = wxT("NUMPAD_PAGEDOWN"); break;
1020 case WXK_NUMPAD_END: key = wxT("NUMPAD_END"); break;
1021 case WXK_NUMPAD_BEGIN: key = wxT("NUMPAD_BEGIN"); break;
1022 case WXK_NUMPAD_INSERT: key = wxT("NUMPAD_INSERT"); break;
1023 case WXK_NUMPAD_DELETE: key = wxT("NUMPAD_DELETE"); break;
1024 case WXK_NUMPAD_EQUAL: key = wxT("NUMPAD_EQUAL"); break;
1025 case WXK_NUMPAD_MULTIPLY: key = wxT("NUMPAD_MULTIPLY"); break;
1026 case WXK_NUMPAD_ADD: key = wxT("NUMPAD_ADD"); break;
1027 case WXK_NUMPAD_SEPARATOR: key = wxT("NUMPAD_SEPARATOR"); break;
1028 case WXK_NUMPAD_SUBTRACT: key = wxT("NUMPAD_SUBTRACT"); break;
1029 case WXK_NUMPAD_DECIMAL: key = wxT("NUMPAD_DECIMAL"); break;
1030
1031 default:
1032 {
bee09d36 1033 if ( keycode < 128 && wxIsprint((int)keycode) )
0c6099b7
RN
1034 key.Printf(wxT("'%c'"), (char)keycode);
1035 else if ( keycode > 0 && keycode < 27 )
1036 key.Printf(_("Ctrl-%c"), wxT('A') + keycode - 1);
1037 else
1038 key.Printf(wxT("unknown (%ld)"), keycode);
1039 }
1944ad76
VZ
1040 }
1041 }
1042
3acd4349 1043 wxLogMessage( wxT("%s event: %s (flags = %c%c%c%c)"),
1944ad76
VZ
1044 name,
1045 key.c_str(),
3acd4349
VS
1046 event.ControlDown() ? wxT('C') : wxT('-'),
1047 event.AltDown() ? wxT('A') : wxT('-'),
1048 event.ShiftDown() ? wxT('S') : wxT('-'),
1049 event.MetaDown() ? wxT('M') : wxT('-'));
1944ad76
VZ
1050}
1051
1052void MyTreeCtrl::OnTreeKeyDown(wxTreeEvent& event)
1053{
3acd4349 1054 LogKeyEvent(wxT("Tree key down "), event.GetKeyEvent());
1944ad76
VZ
1055
1056 event.Skip();
1057}
1058
5888ef1e
VZ
1059void MyTreeCtrl::OnBeginDrag(wxTreeEvent& event)
1060{
1061 // need to explicitly allow drag
1062 if ( event.GetItem() != GetRootItem() )
1063 {
1064 m_draggedItem = event.GetItem();
1065
4693b20c 1066 wxLogMessage(wxT("OnBeginDrag: started dragging %s"),
5888ef1e
VZ
1067 GetItemText(m_draggedItem).c_str());
1068
1069 event.Allow();
1070 }
1071 else
1072 {
4693b20c 1073 wxLogMessage(wxT("OnBeginDrag: this item can't be dragged."));
5888ef1e
VZ
1074 }
1075}
1076
1077void MyTreeCtrl::OnEndDrag(wxTreeEvent& event)
1078{
1079 wxTreeItemId itemSrc = m_draggedItem,
1080 itemDst = event.GetItem();
e97320d4 1081 m_draggedItem = (wxTreeItemId)0l;
5888ef1e
VZ
1082
1083 // where to copy the item?
1084 if ( itemDst.IsOk() && !ItemHasChildren(itemDst) )
1085 {
1086 // copy to the parent then
99006e44 1087 itemDst = GetItemParent(itemDst);
5888ef1e
VZ
1088 }
1089
1090 if ( !itemDst.IsOk() )
1091 {
4693b20c 1092 wxLogMessage(wxT("OnEndDrag: can't drop here."));
5888ef1e
VZ
1093
1094 return;
1095 }
1096
1097 wxString text = GetItemText(itemSrc);
4693b20c 1098 wxLogMessage(wxT("OnEndDrag: '%s' copied to '%s'."),
5888ef1e
VZ
1099 text.c_str(), GetItemText(itemDst).c_str());
1100
1101 // just do append here - we could also insert it just before/after the item
1102 // on which it was dropped, but this requires slightly more work... we also
1103 // completely ignore the client data and icon of the old item but could
1104 // copy them as well.
1105 //
1106 // Finally, we only copy one item here but we might copy the entire tree if
1107 // we were dragging a folder.
2c8e4738
VZ
1108 int image = wxGetApp().ShowImages() ? TreeCtrlIcon_File : -1;
1109 AppendItem(itemDst, text, image);
5888ef1e
VZ
1110}
1111
5ea47806
VZ
1112void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent& event)
1113{
4693b20c 1114 wxLogMessage(wxT("OnBeginLabelEdit"));
5ea47806 1115
618a5e38 1116 // for testing, prevent this item's label editing
5ea47806
VZ
1117 wxTreeItemId itemId = event.GetItem();
1118 if ( IsTestItem(itemId) )
1119 {
4693b20c 1120 wxMessageBox(wxT("You can't edit this item."));
5ea47806
VZ
1121
1122 event.Veto();
1123 }
646a8a4d
VZ
1124 else if ( itemId == GetRootItem() )
1125 {
1126 // test that it is possible to change the text of the item being edited
1127 SetItemText(itemId, _T("Editing root item"));
1128 }
5ea47806
VZ
1129}
1130
1131void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent& event)
1132{
4693b20c 1133 wxLogMessage(wxT("OnEndLabelEdit"));
5ea47806
VZ
1134
1135 // don't allow anything except letters in the labels
1136 if ( !event.GetLabel().IsWord() )
1137 {
9602d7ae 1138 wxMessageBox(wxT("The new label should be a single word."));
5ea47806
VZ
1139
1140 event.Veto();
1141 }
1142}
1143
3a5a2f56 1144void MyTreeCtrl::OnItemCollapsing(wxTreeEvent& event)
457814b5 1145{
4693b20c 1146 wxLogMessage(wxT("OnItemCollapsing"));
f135ff73 1147
5f939e78
VZ
1148 // for testing, prevent the user from collapsing the first child folder
1149 wxTreeItemId itemId = event.GetItem();
5ea47806 1150 if ( IsTestItem(itemId) )
5f939e78 1151 {
4693b20c 1152 wxMessageBox(wxT("You can't collapse this item."));
457814b5 1153
5f939e78
VZ
1154 event.Veto();
1155 }
457814b5
JS
1156}
1157
477350ce 1158void MyTreeCtrl::OnItemActivated(wxTreeEvent& event)
457814b5 1159{
5f939e78 1160 // show some info about this item
477350ce 1161 wxTreeItemId itemId = event.GetItem();
5f939e78 1162 MyTreeItemData *item = (MyTreeItemData *)GetItemData(itemId);
457814b5 1163
5f939e78
VZ
1164 if ( item != NULL )
1165 {
1166 item->ShowInfo(this);
1167 }
457814b5 1168
4693b20c 1169 wxLogMessage(wxT("OnItemActivated"));
5f939e78
VZ
1170}
1171
ef46ef23
VZ
1172void MyTreeCtrl::OnItemMenu(wxTreeEvent& event)
1173{
1174 wxTreeItemId itemId = event.GetItem();
1175 MyTreeItemData *item = itemId.IsOk() ? (MyTreeItemData *)GetItemData(itemId)
1176 : NULL;
1177
1178 wxLogMessage(wxT("OnItemMenu for item \"%s\""), item ? item->GetDesc()
1179 : _T(""));
1180
1181 event.Skip();
1182}
1183
ae8c4b33 1184void MyTreeCtrl::OnContextMenu(wxContextMenuEvent& event)
e3d0aa77
VZ
1185{
1186 wxPoint pt = event.GetPosition();
5fd60e08 1187 wxTreeItemId item;
7df07b10 1188 wxLogMessage(wxT("OnContextMenu at screen coords (%i, %i)"), pt.x, pt.y);
ae8c4b33 1189
ef46ef23
VZ
1190 // check if event was generated by keyboard (MSW-specific?)
1191 if ( pt.x == -1 && pt.y == -1 ) //(this is how MSW indicates it)
ae8c4b33 1192 {
a978c7d2
VZ
1193 if ( !HasFlag(wxTR_MULTIPLE) )
1194 item = GetSelection();
1195
ef46ef23
VZ
1196 // attempt to guess where to show the menu
1197 if ( item.IsOk() )
925e9792 1198 {
ef46ef23 1199 // if an item was clicked, show menu to the right of it
ae8c4b33 1200 wxRect rect;
ef46ef23 1201 GetBoundingRect(item, rect, true /* only the label */);
ae8c4b33
VZ
1202 pt = wxPoint(rect.GetRight(), rect.GetTop());
1203 }
1204 else
1205 {
1206 pt = wxPoint(0, 0);
1207 }
1208 }
ef46ef23 1209 else // event was generated by mouse, use supplied coords
ae8c4b33 1210 {
ae8c4b33 1211 pt = ScreenToClient(pt);
a978c7d2 1212 item = HitTest(pt);
ae8c4b33 1213 }
925e9792 1214
ae8c4b33 1215 ShowMenu(item, pt);
e3d0aa77
VZ
1216}
1217
1218void MyTreeCtrl::ShowMenu(wxTreeItemId id, const wxPoint& pt)
1219{
1220 wxString title;
1221 if ( id.IsOk() )
1222 {
3acd4349 1223 title << wxT("Menu for ") << GetItemText(id);
e3d0aa77
VZ
1224 }
1225 else
1226 {
3acd4349 1227 title = wxT("Menu for no particular item");
e3d0aa77
VZ
1228 }
1229
801bb685 1230#if wxUSE_MENUS
e3d0aa77 1231 wxMenu menu(title);
3acd4349
VS
1232 menu.Append(TreeTest_About, wxT("&About..."));
1233 menu.Append(TreeTest_Dump, wxT("&Dump"));
e3d0aa77
VZ
1234
1235 PopupMenu(&menu, pt);
801bb685 1236#endif // wxUSE_MENUS
e3d0aa77
VZ
1237}
1238
5fd60e08
VZ
1239void MyTreeCtrl::OnItemRClick(wxTreeEvent& event)
1240{
1241 wxTreeItemId itemId = event.GetItem();
1242 MyTreeItemData *item = itemId.IsOk() ? (MyTreeItemData *)GetItemData(itemId)
1243 : NULL;
1244
1245 wxLogMessage(wxT("Item \"%s\" right clicked"), item ? item->GetDesc()
1246 : _T(""));
1247
1248 event.Skip();
1249}
1250
c545c230
VZ
1251void MyTreeCtrl::OnRMouseDown(wxMouseEvent& event)
1252{
1253 wxLogMessage(wxT("Right mouse button down"));
1254
1255 event.Skip();
1256}
1257
1258void MyTreeCtrl::OnRMouseUp(wxMouseEvent& event)
1259{
1260 wxLogMessage(wxT("Right mouse button up"));
1261
1262 event.Skip();
1263}
1264
5f939e78
VZ
1265void MyTreeCtrl::OnRMouseDClick(wxMouseEvent& event)
1266{
1267 wxTreeItemId id = HitTest(event.GetPosition());
1268 if ( !id )
4693b20c 1269 wxLogMessage(wxT("No item under mouse"));
5f939e78
VZ
1270 else
1271 {
1272 MyTreeItemData *item = (MyTreeItemData *)GetItemData(id);
1273 if ( item )
4693b20c 1274 wxLogMessage(wxT("Item '%s' under mouse"), item->GetDesc());
5f939e78 1275 }
c545c230
VZ
1276
1277 event.Skip();
457814b5
JS
1278}
1279
4693b20c 1280static inline const wxChar *Bool2String(bool b)
457814b5 1281{
4693b20c 1282 return b ? wxT("") : wxT("not ");
457814b5
JS
1283}
1284
9769e589 1285void MyTreeItemData::ShowInfo(wxTreeCtrl *tree)
457814b5 1286{
4693b20c
MB
1287 wxLogMessage(wxT("Item '%s': %sselected, %sexpanded, %sbold,\n")
1288 wxT("%u children (%u immediately under this item)."),
5f939e78
VZ
1289 m_desc.c_str(),
1290 Bool2String(tree->IsSelected(GetId())),
1291 Bool2String(tree->IsExpanded(GetId())),
1292 Bool2String(tree->IsBold(GetId())),
b143cf70
PC
1293 unsigned(tree->GetChildrenCount(GetId())),
1294 unsigned(tree->GetChildrenCount(GetId(), false)));
457814b5 1295}