]> git.saurik.com Git - wxWidgets.git/blob - samples/treectrl/treetest.cpp
New way to grey out disabled toolbar icons.
[wxWidgets.git] / samples / treectrl / treetest.cpp
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
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #pragma interface
15 #endif
16
17 // For compilers that support precompilation, includes "wx/wx.h".
18 #include "wx/wxprec.h"
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #ifndef WX_PRECOMP
25 #include "wx/wx.h"
26 #endif
27
28 // under Windows the icons are in the .rc file
29 #ifndef __WXMSW__
30 #include "icon1.xpm"
31 #include "icon2.xpm"
32 #include "mondrian.xpm"
33 #endif
34
35 #include "wx/log.h"
36
37 #include "wx/imaglist.h"
38 #include "wx/treectrl.h"
39
40 #include "math.h"
41
42 #include "treetest.h"
43
44 // verify that the item is ok and insult the user if it is not
45 #define CHECK_ITEM( item ) if ( !item.IsOk() ) { \
46 wxMessageBox("Please select some item first!", \
47 "Tree sample error", \
48 wxOK | wxICON_EXCLAMATION, \
49 this); \
50 return; \
51 }
52
53 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
54 EVT_MENU(TreeTest_Quit, MyFrame::OnQuit)
55 EVT_MENU(TreeTest_About, MyFrame::OnAbout)
56 EVT_MENU(TreeTest_Dump, MyFrame::OnDump)
57 EVT_MENU(TreeTest_Rename, MyFrame::OnRename)
58 EVT_MENU(TreeTest_Sort, MyFrame::OnSort)
59 EVT_MENU(TreeTest_SortRev, MyFrame::OnSortRev)
60 EVT_MENU(TreeTest_Bold, MyFrame::OnSetBold)
61 EVT_MENU(TreeTest_UnBold, MyFrame::OnClearBold)
62 EVT_MENU(TreeTest_Delete, MyFrame::OnDelete)
63 EVT_MENU(TreeTest_DeleteChildren, MyFrame::OnDeleteChildren)
64 EVT_MENU(TreeTest_DeleteAll, MyFrame::OnDeleteAll)
65 EVT_MENU(TreeTest_Recreate, MyFrame::OnRecreate)
66 EVT_MENU(TreeTest_CollapseAndReset, MyFrame::OnCollapseAndReset)
67 EVT_MENU(TreeTest_EnsureVisible, MyFrame::OnEnsureVisible)
68 EVT_MENU(TreeTest_AddItem, MyFrame::OnAddItem)
69 EVT_MENU(TreeTest_IncIndent, MyFrame::OnIncIndent)
70 EVT_MENU(TreeTest_DecIndent, MyFrame::OnDecIndent)
71 EVT_MENU(TreeTest_IncSpacing, MyFrame::OnIncSpacing)
72 EVT_MENU(TreeTest_DecSpacing, MyFrame::OnDecSpacing)
73 END_EVENT_TABLE()
74
75 BEGIN_EVENT_TABLE(MyTreeCtrl, wxTreeCtrl)
76 EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginDrag)
77 EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginRDrag)
78 EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnBeginLabelEdit)
79 EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnEndLabelEdit)
80 EVT_TREE_DELETE_ITEM(TreeTest_Ctrl, MyTreeCtrl::OnDeleteItem)
81 EVT_TREE_GET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnGetInfo)
82 EVT_TREE_SET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnSetInfo)
83 EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanded)
84 EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanding)
85 EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsed)
86 EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsing)
87 EVT_TREE_SEL_CHANGED(TreeTest_Ctrl, MyTreeCtrl::OnSelChanged)
88 EVT_TREE_SEL_CHANGING(TreeTest_Ctrl, MyTreeCtrl::OnSelChanging)
89 EVT_TREE_KEY_DOWN(TreeTest_Ctrl, MyTreeCtrl::OnTreeKeyDown)
90 EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl, MyTreeCtrl::OnItemActivated)
91 EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick)
92 END_EVENT_TABLE()
93
94 IMPLEMENT_APP(MyApp)
95
96 bool MyApp::OnInit()
97 {
98 // Create the main frame window
99 MyFrame *frame = new MyFrame("wxTreeCtrl Test", 50, 50, 450, 340);
100
101 // Show the frame
102 frame->Show(TRUE);
103 SetTopWindow(frame);
104
105 return TRUE;
106 }
107
108
109 // My frame constructor
110 MyFrame::MyFrame(const wxString& title, int x, int y, int w, int h)
111 : wxFrame((wxFrame *)NULL, -1, title, wxPoint(x, y), wxSize(w, h))
112 {
113 // This reduces flicker effects - even better would be to define
114 // OnEraseBackground to do nothing. When the tree control's scrollbars are
115 // show or hidden, the frame is sent a background erase event.
116 SetBackgroundColour(wxColour(255, 255, 255));
117
118 // Give it an icon
119 SetIcon(wxICON(mondrian));
120
121 // Make a menubar
122 wxMenu *file_menu = new wxMenu,
123 *tree_menu = new wxMenu,
124 *item_menu = new wxMenu;
125
126 file_menu->Append(TreeTest_About, "&About...");
127 file_menu->AppendSeparator();
128 file_menu->Append(TreeTest_Quit, "E&xit");
129
130 tree_menu->Append(TreeTest_Recreate, "&Recreate the tree");
131 tree_menu->Append(TreeTest_CollapseAndReset, "C&ollapse and reset");
132 tree_menu->AppendSeparator();
133 tree_menu->Append(TreeTest_AddItem, "Append a &new item");
134 tree_menu->Append(TreeTest_Delete, "&Delete this item");
135 tree_menu->Append(TreeTest_DeleteChildren, "Delete &children");
136 tree_menu->Append(TreeTest_DeleteAll, "Delete &all items");
137 tree_menu->AppendSeparator();
138 tree_menu->Append(TreeTest_Sort, "Sort children of current item");
139 tree_menu->Append(TreeTest_SortRev, "Sort in reversed order");
140 tree_menu->AppendSeparator();
141 tree_menu->Append(TreeTest_EnsureVisible, "Make the last item &visible");
142 tree_menu->AppendSeparator();
143 tree_menu->Append(TreeTest_IncIndent, "Add 5 points to indentation\tAlt-I");
144 tree_menu->Append(TreeTest_DecIndent, "Reduce indentation by 5 points\tAlt-R");
145 tree_menu->AppendSeparator();
146 tree_menu->Append(TreeTest_IncSpacing, "Add 5 points to spacing\tCtrl-I");
147 tree_menu->Append(TreeTest_DecSpacing, "Reduce spacing by 5 points\tCtrl-R");
148
149 item_menu->Append(TreeTest_Dump, "&Dump item children");
150 item_menu->AppendSeparator();
151 item_menu->Append(TreeTest_Rename, "&Rename item...");
152 item_menu->AppendSeparator();
153 item_menu->Append(TreeTest_Bold, "Make item &bold");
154 item_menu->Append(TreeTest_UnBold, "Make item &not bold");
155
156 wxMenuBar *menu_bar = new wxMenuBar;
157 menu_bar->Append(file_menu, "&File");
158 menu_bar->Append(tree_menu, "&Tree");
159 menu_bar->Append(item_menu, "&Item");
160 SetMenuBar(menu_bar);
161
162 m_treeCtrl = new MyTreeCtrl(this, TreeTest_Ctrl,
163 wxDefaultPosition, wxDefaultSize,
164 wxTR_HAS_BUTTONS | wxSUNKEN_BORDER);
165 wxTextCtrl *textCtrl = new wxTextCtrl(this, -1, "",
166 wxDefaultPosition, wxDefaultSize,
167 wxTE_MULTILINE | wxSUNKEN_BORDER);
168
169 wxLayoutConstraints *c = new wxLayoutConstraints;
170 c->top.SameAs(this, wxTop);
171 c->left.SameAs(this, wxLeft);
172 c->right.SameAs(this, wxRight);
173 c->height.PercentOf(this, wxHeight, 66);
174 m_treeCtrl->SetConstraints(c);
175
176 c = new wxLayoutConstraints;
177 c->top.Below(m_treeCtrl);
178 c->left.SameAs(this, wxLeft);
179 c->right.SameAs(this, wxRight);
180 c->bottom.SameAs(this, wxBottom);
181 textCtrl->SetConstraints(c);
182 SetAutoLayout(TRUE);
183
184 // create a status bar with 3 panes
185 CreateStatusBar(3);
186 SetStatusText("", 0);
187
188 #ifdef __WXMOTIF__
189 // For some reason, we get a memcpy crash in wxLogStream::DoLogStream
190 // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc?
191 delete wxLog::SetActiveTarget(new wxLogStderr);
192 #else
193 // set our text control as the log target
194 wxLogTextCtrl *logWindow = new wxLogTextCtrl(textCtrl);
195 delete wxLog::SetActiveTarget(logWindow);
196 #endif
197 }
198
199 MyFrame::~MyFrame()
200 {
201 delete wxLog::SetActiveTarget(NULL);
202 }
203
204 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
205 {
206 Close(TRUE);
207 }
208
209 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
210 {
211 wxMessageBox("Tree test sample\n"
212 "Julian Smart (c) 1997,\n"
213 "Vadim Zeitlin (c) 1998",
214 "About tree test",
215 wxOK | wxICON_INFORMATION, this);
216 }
217
218 void MyFrame::OnRename(wxCommandEvent& WXUNUSED(event))
219 {
220 wxTreeItemId item = m_treeCtrl->GetSelection();
221
222 CHECK_ITEM( item );
223
224 static wxString s_text;
225 s_text = wxGetTextFromUser("New name: ", "Tree sample question",
226 s_text, this);
227 if ( !s_text.IsEmpty() )
228 {
229 m_treeCtrl->SetItemText(item, s_text);
230 }
231 }
232
233 void MyFrame::DoSort(bool reverse)
234 {
235 wxTreeItemId item = m_treeCtrl->GetSelection();
236
237 CHECK_ITEM( item );
238
239 m_treeCtrl->DoSortChildren(item, reverse);
240 }
241
242 void MyFrame::OnDump(wxCommandEvent& WXUNUSED(event))
243 {
244 wxTreeItemId root = m_treeCtrl->GetSelection();
245
246 CHECK_ITEM( root );
247
248 m_treeCtrl->GetItemsRecursively(root, -1);
249 }
250
251 void MyFrame::DoSetBold(bool bold)
252 {
253 wxTreeItemId item = m_treeCtrl->GetSelection();
254
255 CHECK_ITEM( item );
256
257 m_treeCtrl->SetItemBold(item, bold);
258 }
259
260 void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event))
261 {
262 wxTreeItemId item = m_treeCtrl->GetSelection();
263
264 CHECK_ITEM( item );
265
266 m_treeCtrl->Delete(item);
267 }
268
269 void MyFrame::OnDeleteChildren(wxCommandEvent& WXUNUSED(event))
270 {
271 wxTreeItemId item = m_treeCtrl->GetSelection();
272
273 CHECK_ITEM( item );
274
275 m_treeCtrl->DeleteChildren(item);
276 }
277
278 void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event))
279 {
280 m_treeCtrl->DeleteAllItems();
281 }
282
283 void MyFrame::OnRecreate(wxCommandEvent& event)
284 {
285 OnDeleteAll(event);
286 m_treeCtrl->AddTestItemsToTree(3, 2);
287 }
288
289 void MyFrame::OnCollapseAndReset(wxCommandEvent& event)
290 {
291 m_treeCtrl->CollapseAndReset(m_treeCtrl->GetRootItem());
292 }
293
294 void MyFrame::OnEnsureVisible(wxCommandEvent& event)
295 {
296 m_treeCtrl->DoEnsureVisible();
297 }
298
299 void MyFrame::OnAddItem(wxCommandEvent& WXUNUSED(event))
300 {
301 static int s_num = 0;
302
303 wxString text;
304 text.Printf("Item #%d", ++s_num);
305
306 m_treeCtrl->AppendItem(m_treeCtrl->GetRootItem(),
307 text,
308 MyTreeCtrl::TreeCtrlIcon_File);
309 }
310
311 void MyFrame::OnIncIndent(wxCommandEvent& WXUNUSED(event))
312 {
313 unsigned int indent = m_treeCtrl->GetIndent();
314 if (indent < 100)
315 m_treeCtrl->SetIndent( indent+5 );
316 }
317
318 void MyFrame::OnDecIndent(wxCommandEvent& WXUNUSED(event))
319 {
320 unsigned int indent = m_treeCtrl->GetIndent();
321 if (indent > 10)
322 m_treeCtrl->SetIndent( indent-5 );
323 }
324
325 void MyFrame::OnIncSpacing(wxCommandEvent& WXUNUSED(event))
326 {
327 unsigned int indent = m_treeCtrl->GetSpacing();
328 if (indent < 100)
329 m_treeCtrl->SetSpacing( indent+5 );
330 }
331
332 void MyFrame::OnDecSpacing(wxCommandEvent& WXUNUSED(event))
333 {
334 unsigned int indent = m_treeCtrl->GetSpacing();
335 if (indent > 10)
336 m_treeCtrl->SetSpacing( indent-5 );
337 }
338
339 // MyTreeCtrl implementation
340 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl, wxTreeCtrl)
341
342 MyTreeCtrl::MyTreeCtrl(wxWindow *parent, const wxWindowID id,
343 const wxPoint& pos, const wxSize& size,
344 long style)
345 : wxTreeCtrl(parent, id, pos, size, style)
346 {
347 m_reverseSort = FALSE;
348
349 // Make an image list containing small icons
350 m_imageListNormal = new wxImageList(16, 16, TRUE);
351
352 // should correspond to TreeCtrlIcon_xxx enum
353 #if defined(__WXMSW__) && defined(__WIN16__)
354 // This is required in 16-bit Windows mode only because we can't load a specific (16x16)
355 // icon image, so it comes out stretched
356 m_imageListNormal->Add(wxBitmap("bitmap1", wxBITMAP_TYPE_BMP_RESOURCE));
357 m_imageListNormal->Add(wxBitmap("bitmap2", wxBITMAP_TYPE_BMP_RESOURCE));
358 #else
359 m_imageListNormal->Add(wxICON(icon1));
360 m_imageListNormal->Add(wxICON(icon2));
361 #endif
362
363 SetImageList(m_imageListNormal);
364
365 // Add some items to the tree
366 AddTestItemsToTree(3, 2);
367 }
368
369 MyTreeCtrl::~MyTreeCtrl()
370 {
371 delete m_imageListNormal;
372 }
373
374 int MyTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
375 const wxTreeItemId& item2)
376 {
377 if ( m_reverseSort )
378 {
379 // just exchange 1st and 2nd items
380 return wxTreeCtrl::OnCompareItems(item2, item1);
381 }
382 else
383 {
384 return wxTreeCtrl::OnCompareItems(item1, item2);
385 }
386 }
387
388 void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId& idParent,
389 size_t numChildren,
390 size_t depth,
391 size_t folder)
392 {
393 if ( depth > 0 )
394 {
395 wxString str;
396 for ( size_t n = 0; n < numChildren; n++ )
397 {
398 // at depth 1 elements won't have any more children
399 if (depth == 1)
400 str.Printf("%s child %d.%d", "File", folder, n + 1);
401 else
402 str.Printf("%s child %d", "Folder", n + 1);
403
404 int image = depth == 1 ? TreeCtrlIcon_File : TreeCtrlIcon_Folder;
405 wxTreeItemId id = AppendItem(idParent, str, image, image,
406 new MyTreeItemData(str));
407
408 // remember the last child for OnEnsureVisible()
409 if ( depth == 1 && n == numChildren - 1 )
410 {
411 m_lastItem = id;
412 }
413
414 AddItemsRecursively(id, numChildren, depth - 1, n + 1);
415 }
416 }
417 //else: done!
418 }
419
420 void MyTreeCtrl::AddTestItemsToTree(size_t numChildren,
421 size_t depth)
422 {
423 wxTreeItemId rootId = AddRoot("Root",
424 TreeCtrlIcon_Folder, TreeCtrlIcon_Folder,
425 new MyTreeItemData("Root item"));
426
427 AddItemsRecursively(rootId, numChildren, depth, 0);
428 }
429
430 void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId& idParent, long cookie)
431 {
432 wxTreeItemId id;
433
434 if( cookie == -1 )
435 id = GetFirstChild(idParent, cookie);
436 else
437 id = GetNextChild(idParent, cookie);
438
439 if(id <= 0)
440 return;
441
442 wxString text=GetItemText(id);
443 wxLogMessage(text);
444
445 if (ItemHasChildren(id))
446 GetItemsRecursively(id,-1);
447
448 GetItemsRecursively(idParent, cookie);
449 }
450
451
452 // avoid repetition
453 #define TREE_EVENT_HANDLER(name) \
454 void MyTreeCtrl::name(wxTreeEvent& WXUNUSED(event)) \
455 { \
456 wxLogMessage(#name); \
457 }
458
459 TREE_EVENT_HANDLER(OnBeginDrag)
460 TREE_EVENT_HANDLER(OnBeginRDrag)
461 TREE_EVENT_HANDLER(OnBeginLabelEdit)
462 TREE_EVENT_HANDLER(OnEndLabelEdit)
463 TREE_EVENT_HANDLER(OnDeleteItem)
464 TREE_EVENT_HANDLER(OnGetInfo)
465 TREE_EVENT_HANDLER(OnSetInfo)
466 TREE_EVENT_HANDLER(OnItemExpanded)
467 TREE_EVENT_HANDLER(OnItemExpanding)
468 TREE_EVENT_HANDLER(OnItemCollapsed)
469 TREE_EVENT_HANDLER(OnSelChanged)
470 TREE_EVENT_HANDLER(OnSelChanging)
471
472 #undef TREE_EVENT_HANDLER
473
474 void MyTreeCtrl::OnItemCollapsing(wxTreeEvent& event)
475 {
476 wxLogMessage("OnItemCollapsing");
477
478 // for testing, prevent the user from collapsing the first child folder
479 wxTreeItemId itemId = event.GetItem();
480 if ( GetParent(itemId) == GetRootItem() && !GetPrevSibling(itemId) )
481 {
482 wxMessageBox("You can't collapse this item.");
483
484 event.Veto();
485 }
486 }
487
488 void MyTreeCtrl::OnTreeKeyDown(wxTreeEvent&WXUNUSED(event))
489 {
490 wxLogMessage("OnTreeKeyDown");
491 }
492
493 void MyTreeCtrl::OnItemActivated(wxTreeEvent&WXUNUSED(event))
494 {
495 // show some info about this item
496 wxTreeItemId itemId = GetSelection();
497 MyTreeItemData *item = (MyTreeItemData *)GetItemData(itemId);
498
499 if ( item != NULL )
500 {
501 item->ShowInfo(this);
502 }
503
504 wxLogMessage("OnItemActivated");
505 }
506
507 void MyTreeCtrl::OnRMouseDClick(wxMouseEvent& event)
508 {
509 wxTreeItemId id = HitTest(event.GetPosition());
510 if ( !id )
511 wxLogMessage("No item under mouse");
512 else
513 {
514 MyTreeItemData *item = (MyTreeItemData *)GetItemData(id);
515 if ( item )
516 wxLogMessage("Item '%s' under mouse", item->GetDesc());
517 }
518 }
519
520 static inline const char *Bool2String(bool b)
521 {
522 return b ? "" : "not ";
523 }
524
525 void MyTreeItemData::ShowInfo(wxTreeCtrl *tree)
526 {
527 wxLogMessage("Item '%s': %sselected, %sexpanded, %sbold,\n"
528 "%u children (%u immediately under this item).",
529 m_desc.c_str(),
530 Bool2String(tree->IsSelected(GetId())),
531 Bool2String(tree->IsExpanded(GetId())),
532 Bool2String(tree->IsBold(GetId())),
533 tree->GetChildrenCount(GetId()),
534 tree->GetChildrenCount(GetId(), FALSE));
535 }