]> git.saurik.com Git - wxWidgets.git/blame - samples/treectrl/treectrl.cpp
Define SF_UNICODE if undefined
[wxWidgets.git] / samples / treectrl / treectrl.cpp
CommitLineData
457814b5 1/////////////////////////////////////////////////////////////////////////////
5301d933 2// Name: treectrl.cpp
457814b5
JS
3// Purpose: wxTreeCtrl sample
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart and Markus Holzem
3a5a2f56 9// Licence: wxWindows license
457814b5
JS
10/////////////////////////////////////////////////////////////////////////////
11
f6bcfd97
BP
12// important: the #pragma argument must be different from treectrl.cpp,
13// otherwise gcc gets confused (as there is also treectrl.cpp in the library
14// which has identical #pragma) and the sample crashes on startup!
457814b5 15#ifdef __GNUG__
f6bcfd97
BP
16 #pragma interface "treetest.cpp"
17 #pragma implementation "treetest.cpp"
457814b5
JS
18#endif
19
20// For compilers that support precompilation, includes "wx/wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
3a5a2f56 24 #pragma hdrstop
457814b5
JS
25#endif
26
27#ifndef WX_PRECOMP
3a5a2f56 28 #include "wx/wx.h"
457814b5
JS
29#endif
30
3a5a2f56
VZ
31#include "wx/log.h"
32
91b8de8d 33#include "wx/image.h"
3a5a2f56 34#include "wx/imaglist.h"
457814b5
JS
35#include "wx/treectrl.h"
36
cf724bce
RR
37#include "math.h"
38
2c8e4738
VZ
39#ifdef __WIN32__
40 // this is not supported by native control
7cc8c988 41 #define NO_VARIABLE_HEIGHT
74b31181 42#endif
91b8de8d 43
5301d933 44#include "treectrl.h"
9dfbf520 45
91b8de8d
RR
46// under Windows the icons are in the .rc file
47#ifndef __WXMSW__
2c8e4738
VZ
48 #include "icon1.xpm"
49 #include "icon2.xpm"
50 #include "icon3.xpm"
51 #include "icon4.xpm"
52 #include "icon5.xpm"
53 #include "mondrian.xpm"
91b8de8d
RR
54#endif
55
e1ee62bd 56// verify that the item is ok and insult the user if it is not
4693b20c
MB
57#define CHECK_ITEM( item ) if ( !item.IsOk() ) { \
58 wxMessageBox(wxT("Please select some item first!"), \
59 wxT("Tree sample error"), \
60 wxOK | wxICON_EXCLAMATION, \
61 this); \
62 return; \
5f939e78
VZ
63 }
64
457814b5 65BEGIN_EVENT_TABLE(MyFrame, wxFrame)
7cc8c988
VZ
66 EVT_SIZE(MyFrame::OnSize)
67
618a5e38
RR
68#define MENU_LINK(name) EVT_MENU(TreeTest_##name, MyFrame::On##name)
69 MENU_LINK(Quit)
70 MENU_LINK(About)
71 MENU_LINK(TogButtons)
72 MENU_LINK(TogTwist)
73 MENU_LINK(TogLines)
74 MENU_LINK(TogEdit)
75 MENU_LINK(TogHideRoot)
76 MENU_LINK(TogRootLines)
77 MENU_LINK(TogBorder)
78 MENU_LINK(Dump)
9dfbf520 79#ifndef NO_MULTIPLE_SELECTION
618a5e38
RR
80 MENU_LINK(DumpSelected)
81 MENU_LINK(Select)
82 MENU_LINK(Unselect)
83 MENU_LINK(ToggleSel)
9dfbf520 84#endif // NO_MULTIPLE_SELECTION
618a5e38
RR
85 MENU_LINK(Rename)
86 MENU_LINK(Count)
87 MENU_LINK(CountRec)
88 MENU_LINK(Sort)
89 MENU_LINK(SortRev)
90 MENU_LINK(SetBold)
91 MENU_LINK(ClearBold)
92 MENU_LINK(Delete)
93 MENU_LINK(DeleteChildren)
94 MENU_LINK(DeleteAll)
95 MENU_LINK(Recreate)
96 MENU_LINK(ToggleImages)
97 MENU_LINK(ToggleButtons)
98 MENU_LINK(SetImageSize)
99 MENU_LINK(CollapseAndReset)
100 MENU_LINK(EnsureVisible)
101 MENU_LINK(AddItem)
102 MENU_LINK(InsertItem)
103 MENU_LINK(IncIndent)
104 MENU_LINK(DecIndent)
105 MENU_LINK(IncSpacing)
106 MENU_LINK(DecSpacing)
107 MENU_LINK(ToggleIcon)
108#undef MENU_LINK
109
457814b5
JS
110END_EVENT_TABLE()
111
484523cf
JS
112#if USE_GENERIC_TREECTRL
113BEGIN_EVENT_TABLE(MyTreeCtrl, wxGenericTreeCtrl)
114#else
457814b5 115BEGIN_EVENT_TABLE(MyTreeCtrl, wxTreeCtrl)
484523cf 116#endif
5f939e78
VZ
117 EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginDrag)
118 EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginRDrag)
5888ef1e 119 EVT_TREE_END_DRAG(TreeTest_Ctrl, MyTreeCtrl::OnEndDrag)
5f939e78
VZ
120 EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnBeginLabelEdit)
121 EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnEndLabelEdit)
122 EVT_TREE_DELETE_ITEM(TreeTest_Ctrl, MyTreeCtrl::OnDeleteItem)
74b31181 123#if 0 // there are so many of those that logging them causes flicker
5f939e78 124 EVT_TREE_GET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnGetInfo)
74b31181 125#endif
5f939e78
VZ
126 EVT_TREE_SET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnSetInfo)
127 EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanded)
128 EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanding)
129 EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsed)
130 EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsing)
e3d0aa77
VZ
131 EVT_TREE_ITEM_RIGHT_CLICK(TreeTest_Ctrl, MyTreeCtrl::OnItemRightClick)
132
133 EVT_RIGHT_UP(MyTreeCtrl::OnRMouseUp)
5f939e78
VZ
134 EVT_TREE_SEL_CHANGED(TreeTest_Ctrl, MyTreeCtrl::OnSelChanged)
135 EVT_TREE_SEL_CHANGING(TreeTest_Ctrl, MyTreeCtrl::OnSelChanging)
136 EVT_TREE_KEY_DOWN(TreeTest_Ctrl, MyTreeCtrl::OnTreeKeyDown)
137 EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl, MyTreeCtrl::OnItemActivated)
138 EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick)
457814b5
JS
139END_EVENT_TABLE()
140
141IMPLEMENT_APP(MyApp)
142
c89a6106 143bool MyApp::OnInit()
457814b5 144{
5f939e78 145 // Create the main frame window
4693b20c 146 MyFrame *frame = new MyFrame(wxT("wxTreeCtrl Test"), 50, 50, 450, 600);
457814b5 147
5f939e78
VZ
148 // Show the frame
149 frame->Show(TRUE);
150 SetTopWindow(frame);
457814b5 151
5f939e78 152 return TRUE;
3a5a2f56 153}
457814b5 154
457814b5 155
3a5a2f56
VZ
156// My frame constructor
157MyFrame::MyFrame(const wxString& title, int x, int y, int w, int h)
7cc8c988
VZ
158 : wxFrame((wxFrame *)NULL, -1, title, wxPoint(x, y), wxSize(w, h)),
159 m_treeCtrl(NULL), m_textCtrl(NULL)
3a5a2f56 160{
5f939e78
VZ
161 // This reduces flicker effects - even better would be to define
162 // OnEraseBackground to do nothing. When the tree control's scrollbars are
163 // show or hidden, the frame is sent a background erase event.
164 SetBackgroundColour(wxColour(255, 255, 255));
165
166 // Give it an icon
167 SetIcon(wxICON(mondrian));
168
801bb685 169#if wxUSE_MENUS
5f939e78
VZ
170 // Make a menubar
171 wxMenu *file_menu = new wxMenu,
618a5e38 172 *style_menu = new wxMenu,
5f939e78
VZ
173 *tree_menu = new wxMenu,
174 *item_menu = new wxMenu;
175
4693b20c 176 file_menu->Append(TreeTest_About, wxT("&About..."));
5f939e78 177 file_menu->AppendSeparator();
4693b20c 178 file_menu->Append(TreeTest_Quit, wxT("E&xit\tAlt-X"));
5f939e78 179
618a5e38
RR
180 style_menu->Append(TreeTest_TogButtons, "Toggle &normal buttons");
181 style_menu->Append(TreeTest_TogTwist, "Toggle &twister buttons");
182 style_menu->Append(TreeTest_ToggleButtons, "Toggle image &buttons");
183 style_menu->AppendSeparator();
184 style_menu->Append(TreeTest_TogLines, "Toggle &connecting lines");
185 style_menu->Append(TreeTest_TogRootLines, "Toggle &lines at root");
186 style_menu->Append(TreeTest_TogHideRoot, "Toggle &hidden root");
187 style_menu->Append(TreeTest_TogBorder, "Toggle &item border");
188 style_menu->Append(TreeTest_TogEdit, "Toggle &edit mode");
7cc8c988 189#ifndef NO_MULTIPLE_SELECTION
4693b20c 190 style_menu->Append(TreeTest_ToggleSel, wxT("Toggle &selection mode"));
7cc8c988 191#endif // NO_MULTIPLE_SELECTION
4693b20c
MB
192 style_menu->Append(TreeTest_ToggleImages, wxT("Toggle show ima&ges"));
193 style_menu->Append(TreeTest_SetImageSize, wxT("Set image si&ze..."));
618a5e38 194
5f939e78
VZ
195 tree_menu->Append(TreeTest_Recreate, "&Recreate the tree");
196 tree_menu->Append(TreeTest_CollapseAndReset, "C&ollapse and reset");
197 tree_menu->AppendSeparator();
198 tree_menu->Append(TreeTest_AddItem, "Append a &new item");
75c74ca0 199 tree_menu->Append(TreeTest_InsertItem, "&Insert a new item");
5f939e78
VZ
200 tree_menu->Append(TreeTest_Delete, "&Delete this item");
201 tree_menu->Append(TreeTest_DeleteChildren, "Delete &children");
202 tree_menu->Append(TreeTest_DeleteAll, "Delete &all items");
203 tree_menu->AppendSeparator();
5fd11f09
RR
204 tree_menu->Append(TreeTest_Count, "Count children of current item");
205 tree_menu->Append(TreeTest_CountRec, "Recursively count children of current item");
206 tree_menu->AppendSeparator();
5f939e78
VZ
207 tree_menu->Append(TreeTest_Sort, "Sort children of current item");
208 tree_menu->Append(TreeTest_SortRev, "Sort in reversed order");
209 tree_menu->AppendSeparator();
210 tree_menu->Append(TreeTest_EnsureVisible, "Make the last item &visible");
cf724bce
RR
211 tree_menu->AppendSeparator();
212 tree_menu->Append(TreeTest_IncIndent, "Add 5 points to indentation\tAlt-I");
213 tree_menu->Append(TreeTest_DecIndent, "Reduce indentation by 5 points\tAlt-R");
214 tree_menu->AppendSeparator();
215 tree_menu->Append(TreeTest_IncSpacing, "Add 5 points to spacing\tCtrl-I");
216 tree_menu->Append(TreeTest_DecSpacing, "Reduce spacing by 5 points\tCtrl-R");
5f939e78 217
91b8de8d 218 item_menu->Append(TreeTest_Dump, "&Dump item children");
5f939e78 219 item_menu->Append(TreeTest_Rename, "&Rename item...");
91b8de8d 220
5f939e78 221 item_menu->AppendSeparator();
618a5e38
RR
222 item_menu->Append(TreeTest_SetBold, "Make item &bold");
223 item_menu->Append(TreeTest_ClearBold, "Make item &not bold");
9dfbf520 224 item_menu->AppendSeparator();
618a5e38 225 item_menu->Append(TreeTest_ToggleIcon, "Toggle the item's &icon");
9dfbf520
VZ
226
227#ifndef NO_MULTIPLE_SELECTION
228 item_menu->AppendSeparator();
229 item_menu->Append(TreeTest_DumpSelected, "Dump selected items\tAlt-D");
230 item_menu->Append(TreeTest_Select, "Select current item\tAlt-S");
231 item_menu->Append(TreeTest_Unselect, "Unselect everything\tAlt-U");
7cc8c988 232#endif // NO_MULTIPLE_SELECTION
5f939e78
VZ
233
234 wxMenuBar *menu_bar = new wxMenuBar;
235 menu_bar->Append(file_menu, "&File");
618a5e38 236 menu_bar->Append(style_menu, "&Style");
5f939e78
VZ
237 menu_bar->Append(tree_menu, "&Tree");
238 menu_bar->Append(item_menu, "&Item");
239 SetMenuBar(menu_bar);
240
618a5e38 241 //menu_bar->Check(TreeTest_ToggleImages, TRUE);
801bb685 242#endif // wxUSE_MENUS
2c8e4738 243
5f939e78
VZ
244 m_treeCtrl = new MyTreeCtrl(this, TreeTest_Ctrl,
245 wxDefaultPosition, wxDefaultSize,
17386f53 246 wxTR_DEFAULT_STYLE | wxTR_EDIT_LABELS |
89124b1a 247#ifndef NO_VARIABLE_HEIGHT
74b31181 248 wxTR_HAS_VARIABLE_ROW_HEIGHT |
89124b1a 249#endif
74b31181 250 wxSUNKEN_BORDER);
31405c01 251
c22886bd 252// m_treeCtrl->SetBackgroundColour( *wxLIGHT_GREY );
31405c01 253
7cc8c988 254 m_textCtrl = new wxTextCtrl(this, -1, "",
5f939e78
VZ
255 wxDefaultPosition, wxDefaultSize,
256 wxTE_MULTILINE | wxSUNKEN_BORDER);
257
5f939e78
VZ
258 // create a status bar with 3 panes
259 CreateStatusBar(3);
260 SetStatusText("", 0);
3a5a2f56 261
a367b9b3 262#ifdef __WXMOTIF__
5f939e78
VZ
263 // For some reason, we get a memcpy crash in wxLogStream::DoLogStream
264 // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc?
265 delete wxLog::SetActiveTarget(new wxLogStderr);
a367b9b3 266#else
5f939e78 267 // set our text control as the log target
7cc8c988 268 wxLogTextCtrl *logWindow = new wxLogTextCtrl(m_textCtrl);
5f939e78 269 delete wxLog::SetActiveTarget(logWindow);
a367b9b3 270#endif
457814b5
JS
271}
272
c89a6106 273MyFrame::~MyFrame()
457814b5 274{
5f939e78 275 delete wxLog::SetActiveTarget(NULL);
457814b5
JS
276}
277
618a5e38
RR
278void MyFrame::TogStyle(long flag)
279{
280 m_treeCtrl->SetWindowStyle(m_treeCtrl->GetWindowStyle() ^ flag);
281}
282
7cc8c988
VZ
283void MyFrame::OnSize(wxSizeEvent& event)
284{
285 if ( m_treeCtrl && m_textCtrl )
286 {
287 Resize(GetClientSize());
288 }
289
290 event.Skip();
291}
292
293void MyFrame::Resize(const wxSize& size)
294{
295 m_treeCtrl->SetSize(0, 0, size.x, 2*size.y/3);
296 m_textCtrl->SetSize(0, 2*size.y/3, size.x, size.y/3);
297}
298
3a5a2f56 299void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
457814b5 300{
5f939e78 301 Close(TRUE);
457814b5
JS
302}
303
3a5a2f56 304void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
457814b5 305{
5f939e78 306 wxMessageBox("Tree test sample\n"
5ea47806 307 "(c) Julian Smart 1997, Vadim Zeitlin 1998",
5f939e78
VZ
308 "About tree test",
309 wxOK | wxICON_INFORMATION, this);
e1ee62bd
VZ
310}
311
312void MyFrame::OnRename(wxCommandEvent& WXUNUSED(event))
313{
5f939e78 314 wxTreeItemId item = m_treeCtrl->GetSelection();
e1ee62bd 315
5f939e78 316 CHECK_ITEM( item );
e1ee62bd 317
5ea47806
VZ
318 // old code - now we edit in place
319#if 0
5f939e78
VZ
320 static wxString s_text;
321 s_text = wxGetTextFromUser("New name: ", "Tree sample question",
322 s_text, this);
323 if ( !s_text.IsEmpty() )
324 {
325 m_treeCtrl->SetItemText(item, s_text);
326 }
5ea47806
VZ
327#endif // 0
328
329 // TODO demonstrate creating a custom edit control...
330 (void)m_treeCtrl->EditLabel(item);
e1ee62bd
VZ
331}
332
5fd11f09
RR
333void MyFrame::OnCount(wxCommandEvent& WXUNUSED(event))
334{
335 wxTreeItemId item = m_treeCtrl->GetSelection();
336
337 CHECK_ITEM( item );
338
339 int i = m_treeCtrl->GetChildrenCount( item, FALSE );
75c74ca0 340
247e5b16 341 wxLogMessage(wxT("%d children"), i);
5fd11f09
RR
342}
343
344void MyFrame::OnCountRec(wxCommandEvent& WXUNUSED(event))
345{
346 wxTreeItemId item = m_treeCtrl->GetSelection();
347
348 CHECK_ITEM( item );
349
350 int i = m_treeCtrl->GetChildrenCount( item );
75c74ca0 351
247e5b16 352 wxLogMessage(wxT("%d children"), i);
5fd11f09
RR
353}
354
e1ee62bd
VZ
355void MyFrame::DoSort(bool reverse)
356{
5f939e78 357 wxTreeItemId item = m_treeCtrl->GetSelection();
e1ee62bd 358
5f939e78 359 CHECK_ITEM( item );
457814b5 360
5f939e78 361 m_treeCtrl->DoSortChildren(item, reverse);
457814b5
JS
362}
363
4832f7c0
VZ
364void MyFrame::OnDump(wxCommandEvent& WXUNUSED(event))
365{
5f939e78 366 wxTreeItemId root = m_treeCtrl->GetSelection();
e1ee62bd 367
5f939e78 368 CHECK_ITEM( root );
e1ee62bd 369
5f939e78 370 m_treeCtrl->GetItemsRecursively(root, -1);
4832f7c0
VZ
371}
372
9dfbf520
VZ
373#ifndef NO_MULTIPLE_SELECTION
374
7cc8c988
VZ
375void MyFrame::OnToggleSel(wxCommandEvent& WXUNUSED(event))
376{
618a5e38
RR
377 TogStyle(wxTR_MULTIPLE);
378#if 0
7cc8c988
VZ
379 long style = m_treeCtrl->GetWindowStyle();
380 if ( style & wxTR_MULTIPLE )
381 style &= ~wxTR_MULTIPLE;
382 else
383 style |= wxTR_MULTIPLE;
384
385 delete m_treeCtrl;
386
387 m_treeCtrl = new MyTreeCtrl(this, TreeTest_Ctrl,
388 wxDefaultPosition, wxDefaultSize,
389 style);
390 Resize(GetClientSize());
618a5e38 391#endif
7cc8c988
VZ
392}
393
91b8de8d
RR
394void MyFrame::OnDumpSelected(wxCommandEvent& WXUNUSED(event))
395{
9dfbf520 396 wxArrayTreeItemIds array;
91b8de8d 397
9dfbf520 398 size_t count = m_treeCtrl->GetSelections(array);
247e5b16 399 wxLogMessage(wxT("%u items selected"), count);
91b8de8d 400
9dfbf520
VZ
401 for ( size_t n = 0; n < count; n++ )
402 {
4693b20c 403 wxLogMessage(wxT("\t%s"), m_treeCtrl->GetItemText(array.Item(n)).c_str());
9dfbf520
VZ
404 }
405}
406
407void MyFrame::OnSelect(wxCommandEvent& event)
408{
409 m_treeCtrl->SelectItem(m_treeCtrl->GetSelection());
410}
411
412void MyFrame::OnUnselect(wxCommandEvent& event)
413{
414 m_treeCtrl->UnselectAll();
91b8de8d
RR
415}
416
9dfbf520
VZ
417#endif // NO_MULTIPLE_SELECTION
418
add28c55
VZ
419void MyFrame::DoSetBold(bool bold)
420{
5f939e78 421 wxTreeItemId item = m_treeCtrl->GetSelection();
e1ee62bd 422
5f939e78 423 CHECK_ITEM( item );
e1ee62bd 424
5f939e78 425 m_treeCtrl->SetItemBold(item, bold);
add28c55
VZ
426}
427
ff5bf259
VZ
428void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event))
429{
5f939e78 430 wxTreeItemId item = m_treeCtrl->GetSelection();
e1ee62bd 431
5f939e78 432 CHECK_ITEM( item );
e1ee62bd 433
5f939e78 434 m_treeCtrl->Delete(item);
ff5bf259
VZ
435}
436
372edb9d
VZ
437void MyFrame::OnDeleteChildren(wxCommandEvent& WXUNUSED(event))
438{
5f939e78 439 wxTreeItemId item = m_treeCtrl->GetSelection();
e1ee62bd 440
5f939e78 441 CHECK_ITEM( item );
e1ee62bd 442
5f939e78 443 m_treeCtrl->DeleteChildren(item);
372edb9d
VZ
444}
445
ff5bf259
VZ
446void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event))
447{
5f939e78 448 m_treeCtrl->DeleteAllItems();
ff5bf259
VZ
449}
450
451void MyFrame::OnRecreate(wxCommandEvent& event)
452{
5f939e78 453 OnDeleteAll(event);
618a5e38 454 m_treeCtrl->AddTestItemsToTree(5, 2);
ff5bf259
VZ
455}
456
2c8e4738
VZ
457void MyFrame::OnSetImageSize(wxCommandEvent& event)
458{
618a5e38 459 int size = wxGetNumberFromUser("Enter the size for the images to use",
2c8e4738
VZ
460 "Size: ",
461 "TreeCtrl sample",
618a5e38 462 m_treeCtrl->ImageSize());
2c8e4738
VZ
463 if ( size == -1 )
464 return;
465
618a5e38
RR
466 m_treeCtrl->CreateImageList(size);
467 wxGetApp().SetShowImages(TRUE);
2c8e4738
VZ
468}
469
470void MyFrame::OnToggleImages(wxCommandEvent& event)
471{
f6bcfd97
BP
472 if ( wxGetApp().ShowImages() )
473 {
474 m_treeCtrl->CreateImageList(-1);
f6bcfd97
BP
475 wxGetApp().SetShowImages(FALSE);
476 }
477 else
478 {
618a5e38 479 m_treeCtrl->CreateImageList(0);
f6bcfd97
BP
480 wxGetApp().SetShowImages(TRUE);
481 }
618a5e38 482}
2c8e4738 483
618a5e38
RR
484void MyFrame::OnToggleButtons(wxCommandEvent& event)
485{
a3e377a3 486#if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
618a5e38
RR
487 if ( wxGetApp().ShowButtons() )
488 {
489 m_treeCtrl->CreateButtonsImageList(-1);
490 wxGetApp().SetShowButtons(FALSE);
491 }
492 else
493 {
494 m_treeCtrl->CreateButtonsImageList(15);
495 wxGetApp().SetShowButtons(TRUE);
496 }
a3e377a3 497#endif
2c8e4738
VZ
498}
499
dd3646fd
VZ
500void MyFrame::OnCollapseAndReset(wxCommandEvent& event)
501{
5f939e78 502 m_treeCtrl->CollapseAndReset(m_treeCtrl->GetRootItem());
dd3646fd
VZ
503}
504
f65635b5
VZ
505void MyFrame::OnEnsureVisible(wxCommandEvent& event)
506{
507 m_treeCtrl->DoEnsureVisible();
508}
509
75c74ca0
VZ
510void MyFrame::OnInsertItem(wxCommandEvent& WXUNUSED(event))
511{
2c8e4738
VZ
512 int image = wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_File : -1;
513 m_treeCtrl->InsertItem(m_treeCtrl->GetRootItem(), image, "2nd item");
75c74ca0
VZ
514}
515
5f939e78
VZ
516void MyFrame::OnAddItem(wxCommandEvent& WXUNUSED(event))
517{
518 static int s_num = 0;
519
520 wxString text;
4693b20c 521 text.Printf(wxT("Item #%d"), ++s_num);
5f939e78
VZ
522
523 m_treeCtrl->AppendItem(m_treeCtrl->GetRootItem(),
112c5086
RR
524 text /*,
525 MyTreeCtrl::TreeCtrlIcon_File */ );
5f939e78
VZ
526}
527
cf724bce
RR
528void MyFrame::OnIncIndent(wxCommandEvent& WXUNUSED(event))
529{
530 unsigned int indent = m_treeCtrl->GetIndent();
531 if (indent < 100)
532 m_treeCtrl->SetIndent( indent+5 );
533}
534
535void MyFrame::OnDecIndent(wxCommandEvent& WXUNUSED(event))
536{
537 unsigned int indent = m_treeCtrl->GetIndent();
538 if (indent > 10)
539 m_treeCtrl->SetIndent( indent-5 );
540}
541
542void MyFrame::OnIncSpacing(wxCommandEvent& WXUNUSED(event))
543{
544 unsigned int indent = m_treeCtrl->GetSpacing();
545 if (indent < 100)
546 m_treeCtrl->SetSpacing( indent+5 );
547}
548
549void MyFrame::OnDecSpacing(wxCommandEvent& WXUNUSED(event))
550{
551 unsigned int indent = m_treeCtrl->GetSpacing();
552 if (indent > 10)
553 m_treeCtrl->SetSpacing( indent-5 );
554}
555
9dfbf520
VZ
556void MyFrame::OnToggleIcon(wxCommandEvent& WXUNUSED(event))
557{
558 wxTreeItemId item = m_treeCtrl->GetSelection();
559
560 CHECK_ITEM( item );
561
562 m_treeCtrl->DoToggleIcon(item);
563}
564
3a5a2f56 565// MyTreeCtrl implementation
484523cf
JS
566#if USE_GENERIC_TREECTRL
567IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl, wxGenericTreeCtrl)
568#else
23fd5130 569IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl, wxTreeCtrl)
484523cf 570#endif
23fd5130 571
3a5a2f56
VZ
572MyTreeCtrl::MyTreeCtrl(wxWindow *parent, const wxWindowID id,
573 const wxPoint& pos, const wxSize& size,
574 long style)
575 : wxTreeCtrl(parent, id, pos, size, style)
457814b5 576{
2c8e4738 577 m_reverseSort = FALSE;
91b8de8d 578
2c8e4738 579 CreateImageList();
91b8de8d 580
2c8e4738 581 // Add some items to the tree
618a5e38 582 AddTestItemsToTree(5, 2);
2c8e4738
VZ
583}
584
585void MyTreeCtrl::CreateImageList(int size)
586{
2c8e4738
VZ
587 if ( size == -1 )
588 {
618a5e38
RR
589 SetImageList(NULL);
590 return;
2c8e4738 591 }
618a5e38
RR
592 if ( size == 0 )
593 size = m_imageSize;
f6bcfd97 594 else
618a5e38
RR
595 m_imageSize = size;
596
597 // Make an image list containing small icons
598 wxImageList *images = new wxImageList(size, size, TRUE);
e1ee62bd 599
618a5e38 600 // should correspond to TreeCtrlIcon_xxx enum
f60d0f94 601#if defined(__WXMSW__) && defined(__WIN16__)
618a5e38
RR
602 images->Add(wxBitmap("bitmap1", wxBITMAP_TYPE_BMP_RESOURCE));
603 images->Add(wxBitmap("bitmap2", wxBITMAP_TYPE_BMP_RESOURCE));
604 images->Add(wxBitmap("bitmap3", wxBITMAP_TYPE_BMP_RESOURCE));
605 images->Add(wxBitmap("bitmap4", wxBITMAP_TYPE_BMP_RESOURCE));
606 images->Add(wxBitmap("bitmap5", wxBITMAP_TYPE_BMP_RESOURCE));
f6bcfd97 607#else // !MSW
618a5e38
RR
608 wxBusyCursor wait;
609 wxIcon icons[5];
610 icons[0] = wxICON(icon1);
611 icons[1] = wxICON(icon2);
612 icons[2] = wxICON(icon3);
613 icons[3] = wxICON(icon4);
614 icons[4] = wxICON(icon5);
615
616 int sizeOrig = icons[0].GetWidth();
617 for ( size_t i = 0; i < WXSIZEOF(icons); i++ )
618 {
619 if ( size == sizeOrig )
2c8e4738 620 {
618a5e38 621 images->Add(icons[i]);
2c8e4738 622 }
618a5e38
RR
623 else
624 {
625 images->Add(wxImage(icons[i]).Rescale(size, size).
626 ConvertToBitmap());
627 }
628 }
f6bcfd97 629#endif // MSW/!MSW
618a5e38
RR
630
631 AssignImageList(images);
632}
633
634void MyTreeCtrl::CreateButtonsImageList(int size)
635{
a3e377a3 636#if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
618a5e38
RR
637 if ( size == -1 )
638 {
639 SetButtonsImageList(NULL);
640 return;
641 }
642
643 // Make an image list containing small icons
644 wxImageList *images = new wxImageList(size, size, TRUE);
645
646 // should correspond to TreeCtrlIcon_xxx enum
647#if defined(__WXMSW__) && defined(__WIN16__)
648 images->Add(wxBitmap("bitmap1", wxBITMAP_TYPE_BMP_RESOURCE));
649 images->Add(wxBitmap("bitmap2", wxBITMAP_TYPE_BMP_RESOURCE));
650 images->Add(wxBitmap("bitmap3", wxBITMAP_TYPE_BMP_RESOURCE));
651 images->Add(wxBitmap("bitmap4", wxBITMAP_TYPE_BMP_RESOURCE));
652 images->Add(wxBitmap("bitmap5", wxBITMAP_TYPE_BMP_RESOURCE));
653#else // !MSW
654 wxBusyCursor wait;
655 wxIcon icons[4];
656 icons[0] = wxICON(icon3); // closed
657 icons[1] = wxICON(icon3); // closed, selected
658 icons[2] = wxICON(icon5); // open
659 icons[3] = wxICON(icon5); // open, selected
660
661 for ( size_t i = 0; i < WXSIZEOF(icons); i++ )
662 {
663 int sizeOrig = icons[i].GetWidth();
664 if ( size == sizeOrig )
665 {
666 images->Add(icons[i]);
667 }
668 else
669 {
670 images->Add(wxImage(icons[i]).Rescale(size, size).
671 ConvertToBitmap());
672 }
2c8e4738 673 }
618a5e38 674#endif // MSW/!MSW
457814b5 675
618a5e38 676 AssignButtonsImageList(images);
a3e377a3 677#endif
457814b5
JS
678}
679
3a5a2f56 680MyTreeCtrl::~MyTreeCtrl()
457814b5 681{
457814b5
JS
682}
683
e1ee62bd
VZ
684int MyTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
685 const wxTreeItemId& item2)
686{
5f939e78
VZ
687 if ( m_reverseSort )
688 {
689 // just exchange 1st and 2nd items
690 return wxTreeCtrl::OnCompareItems(item2, item1);
691 }
692 else
693 {
694 return wxTreeCtrl::OnCompareItems(item1, item2);
695 }
e1ee62bd
VZ
696}
697
3a5a2f56
VZ
698void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId& idParent,
699 size_t numChildren,
4832f7c0
VZ
700 size_t depth,
701 size_t folder)
457814b5 702{
f65635b5 703 if ( depth > 0 )
3a5a2f56 704 {
74b31181
VZ
705 bool hasChildren = depth > 1;
706
f65635b5
VZ
707 wxString str;
708 for ( size_t n = 0; n < numChildren; n++ )
709 {
710 // at depth 1 elements won't have any more children
74b31181 711 if ( hasChildren )
4693b20c 712 str.Printf(wxT("%s child %d"), wxT("Folder"), n + 1);
74b31181 713 else
4693b20c 714 str.Printf(wxT("%s child %d.%d"), wxT("File"), folder, n + 1);
f65635b5 715
74b31181
VZ
716 // here we pass to AppendItem() normal and selected item images (we
717 // suppose that selected image follows the normal one in the enum)
2c8e4738
VZ
718 int image, imageSel;
719 if ( wxGetApp().ShowImages() )
720 {
721 image = depth == 1 ? TreeCtrlIcon_File : TreeCtrlIcon_Folder;
722 imageSel = image + 1;
723 }
724 else
725 {
726 image = imageSel = -1;
727 }
728 wxTreeItemId id = AppendItem(idParent, str, image, imageSel,
f65635b5
VZ
729 new MyTreeItemData(str));
730
74b31181 731 // and now we also set the expanded one (only for the folders)
2c8e4738 732 if ( hasChildren && wxGetApp().ShowImages() )
74b31181
VZ
733 {
734 SetItemImage(id, TreeCtrlIcon_FolderOpened,
735 wxTreeItemIcon_Expanded);
736 }
737
f65635b5 738 // remember the last child for OnEnsureVisible()
74b31181 739 if ( !hasChildren && n == numChildren - 1 )
f65635b5
VZ
740 {
741 m_lastItem = id;
742 }
743
744 AddItemsRecursively(id, numChildren, depth - 1, n + 1);
745 }
3a5a2f56 746 }
f65635b5 747 //else: done!
457814b5
JS
748}
749
3a5a2f56
VZ
750void MyTreeCtrl::AddTestItemsToTree(size_t numChildren,
751 size_t depth)
457814b5 752{
2c8e4738 753 int image = wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_Folder : -1;
5f939e78 754 wxTreeItemId rootId = AddRoot("Root",
2c8e4738 755 image, image,
5f939e78 756 new MyTreeItemData("Root item"));
2c8e4738
VZ
757 if ( image != -1 )
758 {
759 SetItemImage(rootId, TreeCtrlIcon_FolderOpened, wxTreeItemIcon_Expanded);
760 }
457814b5 761
5f939e78 762 AddItemsRecursively(rootId, numChildren, depth, 0);
9ec64fa7
VZ
763
764 // set some colours/fonts for testing
765 SetItemFont(rootId, *wxITALIC_FONT);
766
767 long cookie;
768 wxTreeItemId id = GetFirstChild(rootId, cookie);
769 SetItemTextColour(id, *wxBLUE);
770
771 id = GetNextChild(rootId, cookie);
772 id = GetNextChild(rootId, cookie);
773 SetItemTextColour(id, *wxRED);
774 SetItemBackgroundColour(id, *wxLIGHT_GREY);
4832f7c0
VZ
775}
776
777void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId& idParent, long cookie)
778{
5f939e78 779 wxTreeItemId id;
4832f7c0 780
5f939e78
VZ
781 if( cookie == -1 )
782 id = GetFirstChild(idParent, cookie);
783 else
784 id = GetNextChild(idParent, cookie);
4832f7c0 785
5f939e78
VZ
786 if(id <= 0)
787 return;
4832f7c0 788
9dfbf520 789 wxString text = GetItemText(id);
5f939e78 790 wxLogMessage(text);
4832f7c0 791
5f939e78
VZ
792 if (ItemHasChildren(id))
793 GetItemsRecursively(id,-1);
4832f7c0 794
5f939e78 795 GetItemsRecursively(idParent, cookie);
457814b5
JS
796}
797
9dfbf520
VZ
798void MyTreeCtrl::DoToggleIcon(const wxTreeItemId& item)
799{
800 int image = GetItemImage(item) == TreeCtrlIcon_Folder ? TreeCtrlIcon_File
801 : TreeCtrlIcon_Folder;
802
803 SetItemImage(item, image);
804}
805
4832f7c0 806
3a5a2f56 807// avoid repetition
4693b20c
MB
808#define TREE_EVENT_HANDLER(name) \
809void MyTreeCtrl::name(wxTreeEvent& event) \
810{ \
811 wxLogMessage(wxT(#name)); \
812 event.Skip(); \
5f939e78 813}
457814b5 814
3a5a2f56 815TREE_EVENT_HANDLER(OnBeginRDrag)
3a5a2f56
VZ
816TREE_EVENT_HANDLER(OnDeleteItem)
817TREE_EVENT_HANDLER(OnGetInfo)
818TREE_EVENT_HANDLER(OnSetInfo)
819TREE_EVENT_HANDLER(OnItemExpanded)
820TREE_EVENT_HANDLER(OnItemExpanding)
821TREE_EVENT_HANDLER(OnItemCollapsed)
822TREE_EVENT_HANDLER(OnSelChanged)
823TREE_EVENT_HANDLER(OnSelChanging)
3dbeaa52 824TREE_EVENT_HANDLER(OnTreeKeyDown)
3a5a2f56
VZ
825
826#undef TREE_EVENT_HANDLER
827
5888ef1e
VZ
828void MyTreeCtrl::OnBeginDrag(wxTreeEvent& event)
829{
830 // need to explicitly allow drag
831 if ( event.GetItem() != GetRootItem() )
832 {
833 m_draggedItem = event.GetItem();
834
4693b20c 835 wxLogMessage(wxT("OnBeginDrag: started dragging %s"),
5888ef1e
VZ
836 GetItemText(m_draggedItem).c_str());
837
838 event.Allow();
839 }
840 else
841 {
4693b20c 842 wxLogMessage(wxT("OnBeginDrag: this item can't be dragged."));
5888ef1e
VZ
843 }
844}
845
846void MyTreeCtrl::OnEndDrag(wxTreeEvent& event)
847{
848 wxTreeItemId itemSrc = m_draggedItem,
849 itemDst = event.GetItem();
e97320d4 850 m_draggedItem = (wxTreeItemId)0l;
5888ef1e
VZ
851
852 // where to copy the item?
853 if ( itemDst.IsOk() && !ItemHasChildren(itemDst) )
854 {
855 // copy to the parent then
856 itemDst = GetParent(itemDst);
857 }
858
859 if ( !itemDst.IsOk() )
860 {
4693b20c 861 wxLogMessage(wxT("OnEndDrag: can't drop here."));
5888ef1e
VZ
862
863 return;
864 }
865
866 wxString text = GetItemText(itemSrc);
4693b20c 867 wxLogMessage(wxT("OnEndDrag: '%s' copied to '%s'."),
5888ef1e
VZ
868 text.c_str(), GetItemText(itemDst).c_str());
869
870 // just do append here - we could also insert it just before/after the item
871 // on which it was dropped, but this requires slightly more work... we also
872 // completely ignore the client data and icon of the old item but could
873 // copy them as well.
874 //
875 // Finally, we only copy one item here but we might copy the entire tree if
876 // we were dragging a folder.
2c8e4738
VZ
877 int image = wxGetApp().ShowImages() ? TreeCtrlIcon_File : -1;
878 AppendItem(itemDst, text, image);
5888ef1e
VZ
879}
880
5ea47806
VZ
881void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent& event)
882{
4693b20c 883 wxLogMessage(wxT("OnBeginLabelEdit"));
5ea47806 884
618a5e38 885 // for testing, prevent this item's label editing
5ea47806
VZ
886 wxTreeItemId itemId = event.GetItem();
887 if ( IsTestItem(itemId) )
888 {
4693b20c 889 wxMessageBox(wxT("You can't edit this item."));
5ea47806
VZ
890
891 event.Veto();
892 }
893}
894
895void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent& event)
896{
4693b20c 897 wxLogMessage(wxT("OnEndLabelEdit"));
5ea47806
VZ
898
899 // don't allow anything except letters in the labels
900 if ( !event.GetLabel().IsWord() )
901 {
4693b20c 902 wxMessageBox(wxT("The label should contain only letters."));
5ea47806
VZ
903
904 event.Veto();
905 }
906}
907
3a5a2f56 908void MyTreeCtrl::OnItemCollapsing(wxTreeEvent& event)
457814b5 909{
4693b20c 910 wxLogMessage(wxT("OnItemCollapsing"));
f135ff73 911
5f939e78
VZ
912 // for testing, prevent the user from collapsing the first child folder
913 wxTreeItemId itemId = event.GetItem();
5ea47806 914 if ( IsTestItem(itemId) )
5f939e78 915 {
4693b20c 916 wxMessageBox(wxT("You can't collapse this item."));
457814b5 917
5f939e78
VZ
918 event.Veto();
919 }
457814b5
JS
920}
921
477350ce 922void MyTreeCtrl::OnItemActivated(wxTreeEvent& event)
457814b5 923{
5f939e78 924 // show some info about this item
477350ce 925 wxTreeItemId itemId = event.GetItem();
5f939e78 926 MyTreeItemData *item = (MyTreeItemData *)GetItemData(itemId);
457814b5 927
5f939e78
VZ
928 if ( item != NULL )
929 {
930 item->ShowInfo(this);
931 }
457814b5 932
4693b20c 933 wxLogMessage(wxT("OnItemActivated"));
5f939e78
VZ
934}
935
e3d0aa77
VZ
936void MyTreeCtrl::OnItemRightClick(wxTreeEvent& event)
937{
938 ShowMenu(event.GetItem(), event.GetPoint());
939}
940
941void MyTreeCtrl::OnRMouseUp(wxMouseEvent& event)
942{
943 wxPoint pt = event.GetPosition();
944 ShowMenu(HitTest(pt), pt);
945}
946
947void MyTreeCtrl::ShowMenu(wxTreeItemId id, const wxPoint& pt)
948{
949 wxString title;
950 if ( id.IsOk() )
951 {
952 title << _T("Menu for ") << GetItemText(id);
953 }
954 else
955 {
956 title = _T("Menu for no particular item");
957 }
958
801bb685 959#if wxUSE_MENUS
e3d0aa77
VZ
960 wxMenu menu(title);
961 menu.Append(TreeTest_About, _T("&About..."));
962 menu.Append(TreeTest_Dump, _T("&Dump"));
963
964 PopupMenu(&menu, pt);
801bb685 965#endif // wxUSE_MENUS
e3d0aa77
VZ
966}
967
5f939e78
VZ
968void MyTreeCtrl::OnRMouseDClick(wxMouseEvent& event)
969{
970 wxTreeItemId id = HitTest(event.GetPosition());
971 if ( !id )
4693b20c 972 wxLogMessage(wxT("No item under mouse"));
5f939e78
VZ
973 else
974 {
975 MyTreeItemData *item = (MyTreeItemData *)GetItemData(id);
976 if ( item )
4693b20c 977 wxLogMessage(wxT("Item '%s' under mouse"), item->GetDesc());
5f939e78 978 }
457814b5
JS
979}
980
4693b20c 981static inline const wxChar *Bool2String(bool b)
457814b5 982{
4693b20c 983 return b ? wxT("") : wxT("not ");
457814b5
JS
984}
985
9769e589 986void MyTreeItemData::ShowInfo(wxTreeCtrl *tree)
457814b5 987{
4693b20c
MB
988 wxLogMessage(wxT("Item '%s': %sselected, %sexpanded, %sbold,\n")
989 wxT("%u children (%u immediately under this item)."),
5f939e78
VZ
990 m_desc.c_str(),
991 Bool2String(tree->IsSelected(GetId())),
992 Bool2String(tree->IsExpanded(GetId())),
993 Bool2String(tree->IsBold(GetId())),
994 tree->GetChildrenCount(GetId()),
995 tree->GetChildrenCount(GetId(), FALSE));
457814b5 996}