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