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