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