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