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