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