]> git.saurik.com Git - wxWidgets.git/blame - samples/treectrl/treetest.cpp
corrected (?) wxStringList::Delete()
[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
42BEGIN_EVENT_TABLE(MyFrame, wxFrame)
5a43154b
VZ
43 EVT_MENU(TreeTest_Quit, MyFrame::OnQuit)
44 EVT_MENU(TreeTest_About, MyFrame::OnAbout)
4832f7c0 45 EVT_MENU(TreeTest_Dump, MyFrame::OnDump)
add28c55
VZ
46 EVT_MENU(TreeTest_Bold, MyFrame::OnSetBold)
47 EVT_MENU(TreeTest_UnBold, MyFrame::OnClearBold)
ff5bf259
VZ
48 EVT_MENU(TreeTest_Delete, MyFrame::OnDelete)
49 EVT_MENU(TreeTest_DeleteAll, MyFrame::OnDeleteAll)
50 EVT_MENU(TreeTest_Recreate, MyFrame::OnRecreate)
457814b5
JS
51END_EVENT_TABLE()
52
53BEGIN_EVENT_TABLE(MyTreeCtrl, wxTreeCtrl)
5a43154b
VZ
54 EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginDrag)
55 EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginRDrag)
56 EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnBeginLabelEdit)
57 EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnEndLabelEdit)
58 EVT_TREE_DELETE_ITEM(TreeTest_Ctrl, MyTreeCtrl::OnDeleteItem)
59 EVT_TREE_GET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnGetInfo)
60 EVT_TREE_SET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnSetInfo)
61 EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanded)
62 EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanding)
63 EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsed)
64 EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsing)
65 EVT_TREE_SEL_CHANGED(TreeTest_Ctrl, MyTreeCtrl::OnSelChanged)
66 EVT_TREE_SEL_CHANGING(TreeTest_Ctrl, MyTreeCtrl::OnSelChanging)
6daa0637 67 EVT_TREE_KEY_DOWN(TreeTest_Ctrl, MyTreeCtrl::OnTreeKeyDown)
457814b5
JS
68END_EVENT_TABLE()
69
70IMPLEMENT_APP(MyApp)
71
72// `Main program' equivalent, creating windows and returning main app frame
c89a6106 73bool MyApp::OnInit()
457814b5
JS
74{
75 // Create the main frame window
3a5a2f56 76 MyFrame *frame = new MyFrame("wxTreeCtrl Test", 50, 50, 450, 340);
457814b5 77
3a5a2f56
VZ
78 // Show the frame
79 frame->Show(TRUE);
80 SetTopWindow(frame);
457814b5 81
3a5a2f56
VZ
82 return TRUE;
83}
457814b5 84
457814b5 85
3a5a2f56
VZ
86// My frame constructor
87MyFrame::MyFrame(const wxString& title, int x, int y, int w, int h)
88 : wxFrame((wxFrame *)NULL, -1, title, wxPoint(x, y), wxSize(w, h))
89{
90 // This reduces flicker effects - even better would be to define
91 // OnEraseBackground to do nothing. When the tree control's scrollbars are
92 // show or hidden, the frame is sent a background erase event.
93 SetBackgroundColour(wxColour(255, 255, 255));
94
95 // Give it an icon
96 SetIcon(wxICON(mondrian));
457814b5
JS
97
98 // Make a menubar
99 wxMenu *file_menu = new wxMenu;
100
ff5bf259
VZ
101 file_menu->Append(TreeTest_Dump, "D&ump tree items");
102 file_menu->Append(TreeTest_Delete, "&Delete this item");
103 file_menu->Append(TreeTest_DeleteAll, "Delete &all items");
104 file_menu->Append(TreeTest_Recreate, "&Recreate the tree");
4832f7c0 105 file_menu->AppendSeparator();
add28c55
VZ
106 file_menu->Append(TreeTest_Bold, "Make item &bold");
107 file_menu->Append(TreeTest_UnBold, "Make item &not bold");
108 file_menu->AppendSeparator();
5a43154b 109 file_menu->Append(TreeTest_About, "&About...");
4832f7c0 110 file_menu->AppendSeparator();
5a43154b 111 file_menu->Append(TreeTest_Quit, "E&xit");
4832f7c0 112
457814b5
JS
113 wxMenuBar *menu_bar = new wxMenuBar;
114 menu_bar->Append(file_menu, "&File");
3a5a2f56 115 SetMenuBar(menu_bar);
457814b5
JS
116
117 // Make a panel with a message
5a43154b 118 m_treeCtrl = new MyTreeCtrl(this, TreeTest_Ctrl,
3a5a2f56
VZ
119 wxDefaultPosition, wxDefaultSize,
120 wxTR_HAS_BUTTONS | wxSUNKEN_BORDER);
121 wxTextCtrl *textCtrl = new wxTextCtrl(this, -1, "",
122 wxDefaultPosition, wxDefaultSize,
123 wxTE_MULTILINE | wxSUNKEN_BORDER);
457814b5
JS
124
125 wxLayoutConstraints *c = new wxLayoutConstraints;
3a5a2f56
VZ
126 c->top.SameAs(this, wxTop);
127 c->left.SameAs(this, wxLeft);
128 c->right.SameAs(this, wxRight);
129 c->height.PercentOf(this, wxHeight, 66);
130 m_treeCtrl->SetConstraints(c);
457814b5
JS
131
132 c = new wxLayoutConstraints;
3a5a2f56
VZ
133 c->top.Below(m_treeCtrl);
134 c->left.SameAs(this, wxLeft);
135 c->right.SameAs(this, wxRight);
136 c->bottom.SameAs(this, wxBottom);
137 textCtrl->SetConstraints(c);
138 SetAutoLayout(TRUE);
139
140 // create a status bar with 3 panes
141 CreateStatusBar(3);
142 SetStatusText("", 0);
143
a367b9b3
JS
144#ifdef __WXMOTIF__
145 // For some reason, we get a memcpy crash in wxLogStream::DoLogStream
146 // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc?
147 delete wxLog::SetActiveTarget(new wxLogStderr);
148#else
3a5a2f56
VZ
149 // set our text control as the log target
150 wxLogTextCtrl *logWindow = new wxLogTextCtrl(textCtrl);
151 delete wxLog::SetActiveTarget(logWindow);
a367b9b3 152#endif
457814b5
JS
153}
154
c89a6106 155MyFrame::~MyFrame()
457814b5 156{
3a5a2f56 157 delete wxLog::SetActiveTarget(NULL);
457814b5
JS
158}
159
3a5a2f56 160void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
457814b5
JS
161{
162 Close(TRUE);
163}
164
3a5a2f56 165void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
457814b5 166{
4832f7c0
VZ
167 wxMessageDialog dialog(this,
168 "Tree test sample\n"
169 "Julian Smart (c) 1997",
3a5a2f56 170 "About tree test", wxOK);
457814b5
JS
171
172 dialog.ShowModal();
173}
174
4832f7c0
VZ
175void MyFrame::OnDump(wxCommandEvent& WXUNUSED(event))
176{
ff5bf259 177 wxTreeItemId root = m_treeCtrl->GetSelection();
4832f7c0
VZ
178 m_treeCtrl->GetItemsRecursively(root, -1);
179}
180
add28c55
VZ
181void MyFrame::DoSetBold(bool bold)
182{
ef44a621 183 m_treeCtrl->SetItemBold(m_treeCtrl->GetSelection(), bold);
add28c55
VZ
184}
185
ff5bf259
VZ
186void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event))
187{
188 wxTreeItemId item = m_treeCtrl->GetSelection();
189 m_treeCtrl->Delete(item);
190}
191
192void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event))
193{
194 m_treeCtrl->DeleteAllItems();
195}
196
197void MyFrame::OnRecreate(wxCommandEvent& event)
198{
199 OnDeleteAll(event);
200 m_treeCtrl->AddTestItemsToTree(3, 2);
201}
202
3a5a2f56
VZ
203// MyTreeCtrl implementation
204MyTreeCtrl::MyTreeCtrl(wxWindow *parent, const wxWindowID id,
205 const wxPoint& pos, const wxSize& size,
206 long style)
207 : wxTreeCtrl(parent, id, pos, size, style)
457814b5 208{
3a5a2f56
VZ
209 // Make an image list containing small icons
210 m_imageListNormal = new wxImageList(16, 16, TRUE);
457814b5 211
3a5a2f56 212 // should correspond to TreeCtrlIcon_xxx enum
df875e59 213 m_imageListNormal->Add(wxICON(icon1));
3db7be80 214 m_imageListNormal->Add(wxICON(icon2));
457814b5 215
3a5a2f56 216 SetImageList(m_imageListNormal);
457814b5 217
3a5a2f56 218 // Add some items to the tree
4832f7c0 219 AddTestItemsToTree(3, 2);
457814b5
JS
220}
221
3a5a2f56 222MyTreeCtrl::~MyTreeCtrl()
457814b5 223{
3a5a2f56 224 delete m_imageListNormal;
457814b5
JS
225}
226
3a5a2f56
VZ
227void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId& idParent,
228 size_t numChildren,
4832f7c0
VZ
229 size_t depth,
230 size_t folder)
457814b5 231{
3a5a2f56
VZ
232 if ( depth > 0 )
233 {
234 wxString str;
235 for ( size_t n = 0; n < numChildren; n++ )
236 {
237 // at depth 1 elements won't have any more children
4832f7c0
VZ
238 if (depth == 1)
239 str.Printf("%s child %d.%d", "File", folder, n + 1);
240 else
241 str.Printf("%s child %d","Folder", n + 1);
242
3a5a2f56
VZ
243 int image = depth == 1 ? TreeCtrlIcon_File : TreeCtrlIcon_Folder;
244 wxTreeItemId id = AppendItem(idParent, str, image, image,
245 new MyTreeItemData(str));
add28c55 246 AddItemsRecursively(id, numChildren, depth - 1, n + 1);
3a5a2f56
VZ
247 }
248 }
249 //else: done!
457814b5
JS
250}
251
3a5a2f56
VZ
252void MyTreeCtrl::AddTestItemsToTree(size_t numChildren,
253 size_t depth)
457814b5 254{
3efb01df 255 wxTreeItemId rootId = AddRoot("Root",
3a5a2f56
VZ
256 TreeCtrlIcon_Folder, TreeCtrlIcon_Folder,
257 new MyTreeItemData("Root item"));
457814b5 258
add28c55 259 AddItemsRecursively(rootId, numChildren, depth, 0);
4832f7c0
VZ
260}
261
262void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId& idParent, long cookie)
263{
264 wxTreeItemId id;
265
266 if( cookie == -1 )
267 id = GetFirstChild(idParent, cookie);
268 else
269 id = GetNextChild(idParent, cookie);
270
271 if(id <= 0)
272 return;
273
274 wxString text=GetItemText(id);
275 wxLogMessage(text);
276
277 if (ItemHasChildren(id))
278 GetItemsRecursively(id,-1);
279
280 GetItemsRecursively(idParent, cookie);
457814b5
JS
281}
282
4832f7c0 283
3a5a2f56
VZ
284// avoid repetition
285#define TREE_EVENT_HANDLER(name) \
286 void MyTreeCtrl::name(wxTreeEvent& WXUNUSED(event)) \
287 { \
288 wxLogMessage(#name); \
289 }
457814b5 290
3a5a2f56
VZ
291TREE_EVENT_HANDLER(OnBeginDrag)
292TREE_EVENT_HANDLER(OnBeginRDrag)
293TREE_EVENT_HANDLER(OnBeginLabelEdit)
294TREE_EVENT_HANDLER(OnEndLabelEdit)
295TREE_EVENT_HANDLER(OnDeleteItem)
296TREE_EVENT_HANDLER(OnGetInfo)
297TREE_EVENT_HANDLER(OnSetInfo)
298TREE_EVENT_HANDLER(OnItemExpanded)
299TREE_EVENT_HANDLER(OnItemExpanding)
300TREE_EVENT_HANDLER(OnItemCollapsed)
301TREE_EVENT_HANDLER(OnSelChanged)
302TREE_EVENT_HANDLER(OnSelChanging)
303
304#undef TREE_EVENT_HANDLER
305
306void MyTreeCtrl::OnItemCollapsing(wxTreeEvent& event)
457814b5 307{
f135ff73
VZ
308 wxLogMessage("OnItemCollapsing");
309
3a5a2f56
VZ
310 // for testing, prevent the user from collapsing the root item
311 wxTreeItemId itemId = event.GetItem();
312 if ( !GetParent(itemId).IsOk() )
313 {
f135ff73 314 wxMessageBox("You can't collapse the root item.");
457814b5 315
3a5a2f56
VZ
316 event.Veto();
317 }
457814b5
JS
318}
319
6daa0637 320void MyTreeCtrl::OnTreeKeyDown(wxTreeEvent&WXUNUSED(event))
457814b5 321{
3a5a2f56
VZ
322 // show some info about this item
323 wxTreeItemId itemId = GetSelection();
324 MyTreeItemData *item = (MyTreeItemData *)GetItemData(itemId);
457814b5 325
3a5a2f56
VZ
326 if ( item != NULL )
327 {
328 item->ShowInfo(this);
329 }
457814b5 330
6daa0637 331 wxLogMessage("OnTreeKeyDown");
457814b5
JS
332}
333
3a5a2f56 334static inline const char *Bool2String(bool b)
457814b5 335{
3a5a2f56 336 return b ? "" : "not ";
457814b5
JS
337}
338
9769e589 339void MyTreeItemData::ShowInfo(wxTreeCtrl *tree)
457814b5 340{
add28c55 341 wxLogMessage("Item '%s': %sselected, %sexpanded, %sbold,\n"
4832f7c0 342 "%u children (%u immediately under this item).",
3a5a2f56 343 m_desc.c_str(),
f135ff73 344 Bool2String(tree->IsSelected(GetId())),
4832f7c0 345 Bool2String(tree->IsExpanded(GetId())),
add28c55 346 Bool2String(tree->IsBold(GetId())),
4832f7c0
VZ
347 tree->GetChildrenCount(GetId()),
348 tree->GetChildrenCount(GetId(), FALSE));
457814b5 349}