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