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