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