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