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