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