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