]> git.saurik.com Git - wxWidgets.git/blame - samples/treectrl/treetest.cpp
1. corrected problem with label updating in wxProgressDialog
[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
74b31181 36#ifdef __WXMSW__
7cc8c988
VZ
37 // this is not supported at all under MSW
38 #define NO_VARIABLE_HEIGHT
39
89124b1a 40 #define NO_MULTIPLE_SELECTION
7cc8c988
VZ
41
42 // this is supported (so the next line may be uncommented) but not very
43 // well :-(
44 #undef NO_MULTIPLE_SELECTION
74b31181 45#endif
91b8de8d 46
9dfbf520
VZ
47#include "treetest.h"
48
91b8de8d
RR
49// under Windows the icons are in the .rc file
50#ifndef __WXMSW__
91b8de8d 51 #include "icon1.xpm"
91b8de8d 52 #include "icon2.xpm"
74b31181
VZ
53 #include "icon3.xpm"
54 #include "icon4.xpm"
8dc99046 55 #include "icon5.xpm"
91b8de8d
RR
56 #include "mondrian.xpm"
57#endif
58
e1ee62bd
VZ
59// verify that the item is ok and insult the user if it is not
60#define CHECK_ITEM( item ) if ( !item.IsOk() ) { \
61 wxMessageBox("Please select some item first!", \
62 "Tree sample error", \
63 wxOK | wxICON_EXCLAMATION, \
64 this); \
65 return; \
5f939e78
VZ
66 }
67
457814b5 68BEGIN_EVENT_TABLE(MyFrame, wxFrame)
7cc8c988
VZ
69 EVT_SIZE(MyFrame::OnSize)
70
5f939e78
VZ
71 EVT_MENU(TreeTest_Quit, MyFrame::OnQuit)
72 EVT_MENU(TreeTest_About, MyFrame::OnAbout)
73 EVT_MENU(TreeTest_Dump, MyFrame::OnDump)
9dfbf520
VZ
74#ifndef NO_MULTIPLE_SELECTION
75 EVT_MENU(TreeTest_DumpSelected, MyFrame::OnDumpSelected)
76 EVT_MENU(TreeTest_Select, MyFrame::OnSelect)
77 EVT_MENU(TreeTest_Unselect, MyFrame::OnUnselect)
7cc8c988 78 EVT_MENU(TreeTest_ToggleSel, MyFrame::OnToggleSel)
9dfbf520 79#endif // NO_MULTIPLE_SELECTION
5f939e78 80 EVT_MENU(TreeTest_Rename, MyFrame::OnRename)
5fd11f09
RR
81 EVT_MENU(TreeTest_Count, MyFrame::OnCount)
82 EVT_MENU(TreeTest_CountRec, MyFrame::OnCountRec)
5f939e78
VZ
83 EVT_MENU(TreeTest_Sort, MyFrame::OnSort)
84 EVT_MENU(TreeTest_SortRev, MyFrame::OnSortRev)
85 EVT_MENU(TreeTest_Bold, MyFrame::OnSetBold)
86 EVT_MENU(TreeTest_UnBold, MyFrame::OnClearBold)
87 EVT_MENU(TreeTest_Delete, MyFrame::OnDelete)
88 EVT_MENU(TreeTest_DeleteChildren, MyFrame::OnDeleteChildren)
89 EVT_MENU(TreeTest_DeleteAll, MyFrame::OnDeleteAll)
90 EVT_MENU(TreeTest_Recreate, MyFrame::OnRecreate)
91 EVT_MENU(TreeTest_CollapseAndReset, MyFrame::OnCollapseAndReset)
92 EVT_MENU(TreeTest_EnsureVisible, MyFrame::OnEnsureVisible)
93 EVT_MENU(TreeTest_AddItem, MyFrame::OnAddItem)
75c74ca0 94 EVT_MENU(TreeTest_InsertItem, MyFrame::OnInsertItem)
cf724bce
RR
95 EVT_MENU(TreeTest_IncIndent, MyFrame::OnIncIndent)
96 EVT_MENU(TreeTest_DecIndent, MyFrame::OnDecIndent)
97 EVT_MENU(TreeTest_IncSpacing, MyFrame::OnIncSpacing)
98 EVT_MENU(TreeTest_DecSpacing, MyFrame::OnDecSpacing)
9dfbf520 99 EVT_MENU(TreeTest_ToggleIcon, MyFrame::OnToggleIcon)
457814b5
JS
100END_EVENT_TABLE()
101
102BEGIN_EVENT_TABLE(MyTreeCtrl, wxTreeCtrl)
5f939e78
VZ
103 EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginDrag)
104 EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginRDrag)
5888ef1e 105 EVT_TREE_END_DRAG(TreeTest_Ctrl, MyTreeCtrl::OnEndDrag)
5f939e78
VZ
106 EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnBeginLabelEdit)
107 EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnEndLabelEdit)
108 EVT_TREE_DELETE_ITEM(TreeTest_Ctrl, MyTreeCtrl::OnDeleteItem)
74b31181 109#if 0 // there are so many of those that logging them causes flicker
5f939e78 110 EVT_TREE_GET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnGetInfo)
74b31181 111#endif
5f939e78
VZ
112 EVT_TREE_SET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnSetInfo)
113 EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanded)
114 EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanding)
115 EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsed)
116 EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsing)
117 EVT_TREE_SEL_CHANGED(TreeTest_Ctrl, MyTreeCtrl::OnSelChanged)
118 EVT_TREE_SEL_CHANGING(TreeTest_Ctrl, MyTreeCtrl::OnSelChanging)
119 EVT_TREE_KEY_DOWN(TreeTest_Ctrl, MyTreeCtrl::OnTreeKeyDown)
120 EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl, MyTreeCtrl::OnItemActivated)
121 EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick)
457814b5
JS
122END_EVENT_TABLE()
123
124IMPLEMENT_APP(MyApp)
125
c89a6106 126bool MyApp::OnInit()
457814b5 127{
5f939e78
VZ
128 // Create the main frame window
129 MyFrame *frame = new MyFrame("wxTreeCtrl Test", 50, 50, 450, 340);
457814b5 130
5f939e78
VZ
131 // Show the frame
132 frame->Show(TRUE);
133 SetTopWindow(frame);
457814b5 134
5f939e78 135 return TRUE;
3a5a2f56 136}
457814b5 137
457814b5 138
3a5a2f56
VZ
139// My frame constructor
140MyFrame::MyFrame(const wxString& title, int x, int y, int w, int h)
7cc8c988
VZ
141 : wxFrame((wxFrame *)NULL, -1, title, wxPoint(x, y), wxSize(w, h)),
142 m_treeCtrl(NULL), m_textCtrl(NULL)
3a5a2f56 143{
5f939e78
VZ
144 // This reduces flicker effects - even better would be to define
145 // OnEraseBackground to do nothing. When the tree control's scrollbars are
146 // show or hidden, the frame is sent a background erase event.
147 SetBackgroundColour(wxColour(255, 255, 255));
148
149 // Give it an icon
150 SetIcon(wxICON(mondrian));
151
152 // Make a menubar
153 wxMenu *file_menu = new wxMenu,
154 *tree_menu = new wxMenu,
155 *item_menu = new wxMenu;
156
157 file_menu->Append(TreeTest_About, "&About...");
158 file_menu->AppendSeparator();
3dbeaa52 159 file_menu->Append(TreeTest_Quit, "E&xit\tAlt-X");
5f939e78 160
7cc8c988
VZ
161#ifndef NO_MULTIPLE_SELECTION
162 tree_menu->Append(TreeTest_ToggleSel, "&Toggle selection mode");
163#endif // NO_MULTIPLE_SELECTION
5f939e78
VZ
164 tree_menu->Append(TreeTest_Recreate, "&Recreate the tree");
165 tree_menu->Append(TreeTest_CollapseAndReset, "C&ollapse and reset");
166 tree_menu->AppendSeparator();
167 tree_menu->Append(TreeTest_AddItem, "Append a &new item");
75c74ca0 168 tree_menu->Append(TreeTest_InsertItem, "&Insert a new item");
5f939e78
VZ
169 tree_menu->Append(TreeTest_Delete, "&Delete this item");
170 tree_menu->Append(TreeTest_DeleteChildren, "Delete &children");
171 tree_menu->Append(TreeTest_DeleteAll, "Delete &all items");
172 tree_menu->AppendSeparator();
5fd11f09
RR
173 tree_menu->Append(TreeTest_Count, "Count children of current item");
174 tree_menu->Append(TreeTest_CountRec, "Recursively count children of current item");
175 tree_menu->AppendSeparator();
5f939e78
VZ
176 tree_menu->Append(TreeTest_Sort, "Sort children of current item");
177 tree_menu->Append(TreeTest_SortRev, "Sort in reversed order");
178 tree_menu->AppendSeparator();
179 tree_menu->Append(TreeTest_EnsureVisible, "Make the last item &visible");
cf724bce
RR
180 tree_menu->AppendSeparator();
181 tree_menu->Append(TreeTest_IncIndent, "Add 5 points to indentation\tAlt-I");
182 tree_menu->Append(TreeTest_DecIndent, "Reduce indentation by 5 points\tAlt-R");
183 tree_menu->AppendSeparator();
184 tree_menu->Append(TreeTest_IncSpacing, "Add 5 points to spacing\tCtrl-I");
185 tree_menu->Append(TreeTest_DecSpacing, "Reduce spacing by 5 points\tCtrl-R");
5f939e78 186
91b8de8d 187 item_menu->Append(TreeTest_Dump, "&Dump item children");
5f939e78 188 item_menu->Append(TreeTest_Rename, "&Rename item...");
91b8de8d 189
5f939e78
VZ
190 item_menu->AppendSeparator();
191 item_menu->Append(TreeTest_Bold, "Make item &bold");
192 item_menu->Append(TreeTest_UnBold, "Make item &not bold");
9dfbf520
VZ
193 item_menu->AppendSeparator();
194 item_menu->Append(TreeTest_ToggleIcon, "Toggle the items &icon");
195
196#ifndef NO_MULTIPLE_SELECTION
197 item_menu->AppendSeparator();
198 item_menu->Append(TreeTest_DumpSelected, "Dump selected items\tAlt-D");
199 item_menu->Append(TreeTest_Select, "Select current item\tAlt-S");
200 item_menu->Append(TreeTest_Unselect, "Unselect everything\tAlt-U");
7cc8c988 201#endif // NO_MULTIPLE_SELECTION
5f939e78
VZ
202
203 wxMenuBar *menu_bar = new wxMenuBar;
204 menu_bar->Append(file_menu, "&File");
205 menu_bar->Append(tree_menu, "&Tree");
206 menu_bar->Append(item_menu, "&Item");
207 SetMenuBar(menu_bar);
208
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
dd3646fd
VZ
415void MyFrame::OnCollapseAndReset(wxCommandEvent& event)
416{
5f939e78 417 m_treeCtrl->CollapseAndReset(m_treeCtrl->GetRootItem());
dd3646fd
VZ
418}
419
f65635b5
VZ
420void MyFrame::OnEnsureVisible(wxCommandEvent& event)
421{
422 m_treeCtrl->DoEnsureVisible();
423}
424
75c74ca0
VZ
425void MyFrame::OnInsertItem(wxCommandEvent& WXUNUSED(event))
426{
427 m_treeCtrl->InsertItem(m_treeCtrl->GetRootItem(), 1, "2nd item");
428}
429
5f939e78
VZ
430void MyFrame::OnAddItem(wxCommandEvent& WXUNUSED(event))
431{
432 static int s_num = 0;
433
434 wxString text;
435 text.Printf("Item #%d", ++s_num);
436
437 m_treeCtrl->AppendItem(m_treeCtrl->GetRootItem(),
112c5086
RR
438 text /*,
439 MyTreeCtrl::TreeCtrlIcon_File */ );
5f939e78
VZ
440}
441
cf724bce
RR
442void MyFrame::OnIncIndent(wxCommandEvent& WXUNUSED(event))
443{
444 unsigned int indent = m_treeCtrl->GetIndent();
445 if (indent < 100)
446 m_treeCtrl->SetIndent( indent+5 );
447}
448
449void MyFrame::OnDecIndent(wxCommandEvent& WXUNUSED(event))
450{
451 unsigned int indent = m_treeCtrl->GetIndent();
452 if (indent > 10)
453 m_treeCtrl->SetIndent( indent-5 );
454}
455
456void MyFrame::OnIncSpacing(wxCommandEvent& WXUNUSED(event))
457{
458 unsigned int indent = m_treeCtrl->GetSpacing();
459 if (indent < 100)
460 m_treeCtrl->SetSpacing( indent+5 );
461}
462
463void MyFrame::OnDecSpacing(wxCommandEvent& WXUNUSED(event))
464{
465 unsigned int indent = m_treeCtrl->GetSpacing();
466 if (indent > 10)
467 m_treeCtrl->SetSpacing( indent-5 );
468}
469
9dfbf520
VZ
470void MyFrame::OnToggleIcon(wxCommandEvent& WXUNUSED(event))
471{
472 wxTreeItemId item = m_treeCtrl->GetSelection();
473
474 CHECK_ITEM( item );
475
476 m_treeCtrl->DoToggleIcon(item);
477}
478
3a5a2f56 479// MyTreeCtrl implementation
23fd5130
VZ
480IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl, wxTreeCtrl)
481
3a5a2f56
VZ
482MyTreeCtrl::MyTreeCtrl(wxWindow *parent, const wxWindowID id,
483 const wxPoint& pos, const wxSize& size,
484 long style)
485 : wxTreeCtrl(parent, id, pos, size, style)
457814b5 486{
9ce29a28
SB
487#ifndef NO_VARIABLE_HEIGHT
488#if wxUSE_LIBJPEG
9dfbf520 489 wxImage::AddHandler(new wxJPEGHandler);
91b8de8d
RR
490 wxImage image;
491
492 image.LoadFile(wxString("horse.jpg"), wxBITMAP_TYPE_JPEG );
9ce29a28 493#endif
91b8de8d
RR
494#endif
495
5f939e78 496 m_reverseSort = FALSE;
e1ee62bd 497
5f939e78
VZ
498 // Make an image list containing small icons
499 m_imageListNormal = new wxImageList(16, 16, TRUE);
457814b5 500
5f939e78 501 // should correspond to TreeCtrlIcon_xxx enum
f60d0f94 502#if defined(__WXMSW__) && defined(__WIN16__)
5f939e78
VZ
503 // This is required in 16-bit Windows mode only because we can't load a specific (16x16)
504 // icon image, so it comes out stretched
9ce29a28 505# ifndef NO_VARIABLE_HEIGHT
91b8de8d
RR
506 m_imageListNormal->Add(image.ConvertToBitmap());
507# else
5f939e78 508 m_imageListNormal->Add(wxBitmap("bitmap1", wxBITMAP_TYPE_BMP_RESOURCE));
91b8de8d 509# endif
5f939e78 510 m_imageListNormal->Add(wxBitmap("bitmap2", wxBITMAP_TYPE_BMP_RESOURCE));
74b31181
VZ
511 m_imageListNormal->Add(wxBitmap("bitmap3", wxBITMAP_TYPE_BMP_RESOURCE));
512 m_imageListNormal->Add(wxBitmap("bitmap4", wxBITMAP_TYPE_BMP_RESOURCE));
513 m_imageListNormal->Add(wxBitmap("bitmap5", wxBITMAP_TYPE_BMP_RESOURCE));
f60d0f94 514#else
9ce29a28 515# ifndef NO_VARIABLE_HEIGHT
91b8de8d
RR
516 m_imageListNormal->Add(image.ConvertToBitmap());
517# else
5f939e78 518 m_imageListNormal->Add(wxICON(icon1));
91b8de8d 519# endif
5f939e78 520 m_imageListNormal->Add(wxICON(icon2));
74b31181
VZ
521 m_imageListNormal->Add(wxICON(icon3));
522 m_imageListNormal->Add(wxICON(icon4));
523 m_imageListNormal->Add(wxICON(icon5));
f60d0f94 524#endif
457814b5 525
5f939e78 526 SetImageList(m_imageListNormal);
457814b5 527
5f939e78
VZ
528 // Add some items to the tree
529 AddTestItemsToTree(3, 2);
457814b5
JS
530}
531
3a5a2f56 532MyTreeCtrl::~MyTreeCtrl()
457814b5 533{
5f939e78 534 delete m_imageListNormal;
457814b5
JS
535}
536
e1ee62bd
VZ
537int MyTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
538 const wxTreeItemId& item2)
539{
5f939e78
VZ
540 if ( m_reverseSort )
541 {
542 // just exchange 1st and 2nd items
543 return wxTreeCtrl::OnCompareItems(item2, item1);
544 }
545 else
546 {
547 return wxTreeCtrl::OnCompareItems(item1, item2);
548 }
e1ee62bd
VZ
549}
550
3a5a2f56
VZ
551void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId& idParent,
552 size_t numChildren,
4832f7c0
VZ
553 size_t depth,
554 size_t folder)
457814b5 555{
f65635b5 556 if ( depth > 0 )
3a5a2f56 557 {
74b31181
VZ
558 bool hasChildren = depth > 1;
559
f65635b5
VZ
560 wxString str;
561 for ( size_t n = 0; n < numChildren; n++ )
562 {
563 // at depth 1 elements won't have any more children
74b31181 564 if ( hasChildren )
f65635b5 565 str.Printf("%s child %d", "Folder", n + 1);
74b31181
VZ
566 else
567 str.Printf("%s child %d.%d", "File", folder, n + 1);
f65635b5 568
74b31181
VZ
569 // here we pass to AppendItem() normal and selected item images (we
570 // suppose that selected image follows the normal one in the enum)
9dfbf520 571 int image = depth == 1 ? TreeCtrlIcon_File : TreeCtrlIcon_Folder;
74b31181 572 wxTreeItemId id = AppendItem(idParent, str, image, image + 1,
f65635b5
VZ
573 new MyTreeItemData(str));
574
74b31181
VZ
575 // and now we also set the expanded one (only for the folders)
576 if ( hasChildren )
577 {
578 SetItemImage(id, TreeCtrlIcon_FolderOpened,
579 wxTreeItemIcon_Expanded);
580 }
581
f65635b5 582 // remember the last child for OnEnsureVisible()
74b31181 583 if ( !hasChildren && n == numChildren - 1 )
f65635b5
VZ
584 {
585 m_lastItem = id;
586 }
587
588 AddItemsRecursively(id, numChildren, depth - 1, n + 1);
589 }
3a5a2f56 590 }
f65635b5 591 //else: done!
457814b5
JS
592}
593
3a5a2f56
VZ
594void MyTreeCtrl::AddTestItemsToTree(size_t numChildren,
595 size_t depth)
457814b5 596{
5f939e78
VZ
597 wxTreeItemId rootId = AddRoot("Root",
598 TreeCtrlIcon_Folder, TreeCtrlIcon_Folder,
599 new MyTreeItemData("Root item"));
74b31181 600 SetItemImage(rootId, TreeCtrlIcon_FolderOpened, wxTreeItemIcon_Expanded);
457814b5 601
5f939e78 602 AddItemsRecursively(rootId, numChildren, depth, 0);
9ec64fa7
VZ
603
604 // set some colours/fonts for testing
605 SetItemFont(rootId, *wxITALIC_FONT);
606
607 long cookie;
608 wxTreeItemId id = GetFirstChild(rootId, cookie);
609 SetItemTextColour(id, *wxBLUE);
610
611 id = GetNextChild(rootId, cookie);
612 id = GetNextChild(rootId, cookie);
613 SetItemTextColour(id, *wxRED);
614 SetItemBackgroundColour(id, *wxLIGHT_GREY);
4832f7c0
VZ
615}
616
617void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId& idParent, long cookie)
618{
5f939e78 619 wxTreeItemId id;
4832f7c0 620
5f939e78
VZ
621 if( cookie == -1 )
622 id = GetFirstChild(idParent, cookie);
623 else
624 id = GetNextChild(idParent, cookie);
4832f7c0 625
5f939e78
VZ
626 if(id <= 0)
627 return;
4832f7c0 628
9dfbf520 629 wxString text = GetItemText(id);
5f939e78 630 wxLogMessage(text);
4832f7c0 631
5f939e78
VZ
632 if (ItemHasChildren(id))
633 GetItemsRecursively(id,-1);
4832f7c0 634
5f939e78 635 GetItemsRecursively(idParent, cookie);
457814b5
JS
636}
637
9dfbf520
VZ
638void MyTreeCtrl::DoToggleIcon(const wxTreeItemId& item)
639{
640 int image = GetItemImage(item) == TreeCtrlIcon_Folder ? TreeCtrlIcon_File
641 : TreeCtrlIcon_Folder;
642
643 SetItemImage(item, image);
644}
645
4832f7c0 646
3a5a2f56 647// avoid repetition
5f939e78 648#define TREE_EVENT_HANDLER(name) \
3dbeaa52 649void MyTreeCtrl::name(wxTreeEvent& event) \
5f939e78
VZ
650{ \
651 wxLogMessage(#name); \
3dbeaa52 652 event.Skip(); \
5f939e78 653}
457814b5 654
3a5a2f56 655TREE_EVENT_HANDLER(OnBeginRDrag)
3a5a2f56
VZ
656TREE_EVENT_HANDLER(OnDeleteItem)
657TREE_EVENT_HANDLER(OnGetInfo)
658TREE_EVENT_HANDLER(OnSetInfo)
659TREE_EVENT_HANDLER(OnItemExpanded)
660TREE_EVENT_HANDLER(OnItemExpanding)
661TREE_EVENT_HANDLER(OnItemCollapsed)
662TREE_EVENT_HANDLER(OnSelChanged)
663TREE_EVENT_HANDLER(OnSelChanging)
3dbeaa52 664TREE_EVENT_HANDLER(OnTreeKeyDown)
3a5a2f56
VZ
665
666#undef TREE_EVENT_HANDLER
667
5888ef1e
VZ
668void MyTreeCtrl::OnBeginDrag(wxTreeEvent& event)
669{
670 // need to explicitly allow drag
671 if ( event.GetItem() != GetRootItem() )
672 {
673 m_draggedItem = event.GetItem();
674
675 wxLogMessage("OnBeginDrag: started dragging %s",
676 GetItemText(m_draggedItem).c_str());
677
678 event.Allow();
679 }
680 else
681 {
682 wxLogMessage("OnBeginDrag: this item can't be dragged.");
683 }
684}
685
686void MyTreeCtrl::OnEndDrag(wxTreeEvent& event)
687{
688 wxTreeItemId itemSrc = m_draggedItem,
689 itemDst = event.GetItem();
e97320d4 690 m_draggedItem = (wxTreeItemId)0l;
5888ef1e
VZ
691
692 // where to copy the item?
693 if ( itemDst.IsOk() && !ItemHasChildren(itemDst) )
694 {
695 // copy to the parent then
696 itemDst = GetParent(itemDst);
697 }
698
699 if ( !itemDst.IsOk() )
700 {
701 wxLogMessage("OnEndDrag: can't drop here.");
702
703 return;
704 }
705
706 wxString text = GetItemText(itemSrc);
707 wxLogMessage("OnEndDrag: '%s' copied to '%s'.",
708 text.c_str(), GetItemText(itemDst).c_str());
709
710 // just do append here - we could also insert it just before/after the item
711 // on which it was dropped, but this requires slightly more work... we also
712 // completely ignore the client data and icon of the old item but could
713 // copy them as well.
714 //
715 // Finally, we only copy one item here but we might copy the entire tree if
716 // we were dragging a folder.
3dbeaa52 717 AppendItem(itemDst, text, TreeCtrlIcon_File);
5888ef1e
VZ
718}
719
5ea47806
VZ
720void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent& event)
721{
722 wxLogMessage("OnBeginLabelEdit");
723
724 // for testing, prevent this items label editing
725 wxTreeItemId itemId = event.GetItem();
726 if ( IsTestItem(itemId) )
727 {
728 wxMessageBox("You can't edit this item.");
729
730 event.Veto();
731 }
732}
733
734void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent& event)
735{
736 wxLogMessage("OnEndLabelEdit");
737
738 // don't allow anything except letters in the labels
739 if ( !event.GetLabel().IsWord() )
740 {
741 wxMessageBox("The label should contain only letters.");
742
743 event.Veto();
744 }
745}
746
3a5a2f56 747void MyTreeCtrl::OnItemCollapsing(wxTreeEvent& event)
457814b5 748{
5f939e78 749 wxLogMessage("OnItemCollapsing");
f135ff73 750
5f939e78
VZ
751 // for testing, prevent the user from collapsing the first child folder
752 wxTreeItemId itemId = event.GetItem();
5ea47806 753 if ( IsTestItem(itemId) )
5f939e78
VZ
754 {
755 wxMessageBox("You can't collapse this item.");
457814b5 756
5f939e78
VZ
757 event.Veto();
758 }
457814b5
JS
759}
760
435fe83e 761void MyTreeCtrl::OnItemActivated(wxTreeEvent&WXUNUSED(event))
457814b5 762{
5f939e78
VZ
763 // show some info about this item
764 wxTreeItemId itemId = GetSelection();
765 MyTreeItemData *item = (MyTreeItemData *)GetItemData(itemId);
457814b5 766
5f939e78
VZ
767 if ( item != NULL )
768 {
769 item->ShowInfo(this);
770 }
457814b5 771
5f939e78
VZ
772 wxLogMessage("OnItemActivated");
773}
774
775void MyTreeCtrl::OnRMouseDClick(wxMouseEvent& event)
776{
777 wxTreeItemId id = HitTest(event.GetPosition());
778 if ( !id )
779 wxLogMessage("No item under mouse");
780 else
781 {
782 MyTreeItemData *item = (MyTreeItemData *)GetItemData(id);
783 if ( item )
784 wxLogMessage("Item '%s' under mouse", item->GetDesc());
785 }
457814b5
JS
786}
787
3a5a2f56 788static inline const char *Bool2String(bool b)
457814b5 789{
5f939e78 790 return b ? "" : "not ";
457814b5
JS
791}
792
9769e589 793void MyTreeItemData::ShowInfo(wxTreeCtrl *tree)
457814b5 794{
5f939e78
VZ
795 wxLogMessage("Item '%s': %sselected, %sexpanded, %sbold,\n"
796 "%u children (%u immediately under this item).",
797 m_desc.c_str(),
798 Bool2String(tree->IsSelected(GetId())),
799 Bool2String(tree->IsExpanded(GetId())),
800 Bool2String(tree->IsBold(GetId())),
801 tree->GetChildrenCount(GetId()),
802 tree->GetChildrenCount(GetId(), FALSE));
457814b5 803}