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