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