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