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