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