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