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