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