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