]> git.saurik.com Git - wxWidgets.git/blame_incremental - samples/treectrl/treetest.cpp
corrected (?) wxStringList::Delete()
[wxWidgets.git] / samples / treectrl / treetest.cpp
... / ...
CommitLineData
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 "treetest.h"
41
42BEGIN_EVENT_TABLE(MyFrame, wxFrame)
43 EVT_MENU(TreeTest_Quit, MyFrame::OnQuit)
44 EVT_MENU(TreeTest_About, MyFrame::OnAbout)
45 EVT_MENU(TreeTest_Dump, MyFrame::OnDump)
46 EVT_MENU(TreeTest_Bold, MyFrame::OnSetBold)
47 EVT_MENU(TreeTest_UnBold, MyFrame::OnClearBold)
48 EVT_MENU(TreeTest_Delete, MyFrame::OnDelete)
49 EVT_MENU(TreeTest_DeleteAll, MyFrame::OnDeleteAll)
50 EVT_MENU(TreeTest_Recreate, MyFrame::OnRecreate)
51END_EVENT_TABLE()
52
53BEGIN_EVENT_TABLE(MyTreeCtrl, wxTreeCtrl)
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)
67 EVT_TREE_KEY_DOWN(TreeTest_Ctrl, MyTreeCtrl::OnTreeKeyDown)
68END_EVENT_TABLE()
69
70IMPLEMENT_APP(MyApp)
71
72// `Main program' equivalent, creating windows and returning main app frame
73bool MyApp::OnInit()
74{
75 // Create the main frame window
76 MyFrame *frame = new MyFrame("wxTreeCtrl Test", 50, 50, 450, 340);
77
78 // Show the frame
79 frame->Show(TRUE);
80 SetTopWindow(frame);
81
82 return TRUE;
83}
84
85
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));
97
98 // Make a menubar
99 wxMenu *file_menu = new wxMenu;
100
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");
105 file_menu->AppendSeparator();
106 file_menu->Append(TreeTest_Bold, "Make item &bold");
107 file_menu->Append(TreeTest_UnBold, "Make item &not bold");
108 file_menu->AppendSeparator();
109 file_menu->Append(TreeTest_About, "&About...");
110 file_menu->AppendSeparator();
111 file_menu->Append(TreeTest_Quit, "E&xit");
112
113 wxMenuBar *menu_bar = new wxMenuBar;
114 menu_bar->Append(file_menu, "&File");
115 SetMenuBar(menu_bar);
116
117 // Make a panel with a message
118 m_treeCtrl = new MyTreeCtrl(this, TreeTest_Ctrl,
119 wxDefaultPosition, wxDefaultSize,
120 wxTR_HAS_BUTTONS | wxSUNKEN_BORDER);
121 wxTextCtrl *textCtrl = new wxTextCtrl(this, -1, "",
122 wxDefaultPosition, wxDefaultSize,
123 wxTE_MULTILINE | wxSUNKEN_BORDER);
124
125 wxLayoutConstraints *c = new wxLayoutConstraints;
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);
131
132 c = new wxLayoutConstraints;
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
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
149 // set our text control as the log target
150 wxLogTextCtrl *logWindow = new wxLogTextCtrl(textCtrl);
151 delete wxLog::SetActiveTarget(logWindow);
152#endif
153}
154
155MyFrame::~MyFrame()
156{
157 delete wxLog::SetActiveTarget(NULL);
158}
159
160void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
161{
162 Close(TRUE);
163}
164
165void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
166{
167 wxMessageDialog dialog(this,
168 "Tree test sample\n"
169 "Julian Smart (c) 1997",
170 "About tree test", wxOK);
171
172 dialog.ShowModal();
173}
174
175void MyFrame::OnDump(wxCommandEvent& WXUNUSED(event))
176{
177 wxTreeItemId root = m_treeCtrl->GetSelection();
178 m_treeCtrl->GetItemsRecursively(root, -1);
179}
180
181void MyFrame::DoSetBold(bool bold)
182{
183 m_treeCtrl->SetItemBold(m_treeCtrl->GetSelection(), bold);
184}
185
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
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)
208{
209 // Make an image list containing small icons
210 m_imageListNormal = new wxImageList(16, 16, TRUE);
211
212 // should correspond to TreeCtrlIcon_xxx enum
213 m_imageListNormal->Add(wxICON(icon1));
214 m_imageListNormal->Add(wxICON(icon2));
215
216 SetImageList(m_imageListNormal);
217
218 // Add some items to the tree
219 AddTestItemsToTree(3, 2);
220}
221
222MyTreeCtrl::~MyTreeCtrl()
223{
224 delete m_imageListNormal;
225}
226
227void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId& idParent,
228 size_t numChildren,
229 size_t depth,
230 size_t folder)
231{
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
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
243 int image = depth == 1 ? TreeCtrlIcon_File : TreeCtrlIcon_Folder;
244 wxTreeItemId id = AppendItem(idParent, str, image, image,
245 new MyTreeItemData(str));
246 AddItemsRecursively(id, numChildren, depth - 1, n + 1);
247 }
248 }
249 //else: done!
250}
251
252void MyTreeCtrl::AddTestItemsToTree(size_t numChildren,
253 size_t depth)
254{
255 wxTreeItemId rootId = AddRoot("Root",
256 TreeCtrlIcon_Folder, TreeCtrlIcon_Folder,
257 new MyTreeItemData("Root item"));
258
259 AddItemsRecursively(rootId, numChildren, depth, 0);
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);
281}
282
283
284// avoid repetition
285#define TREE_EVENT_HANDLER(name) \
286 void MyTreeCtrl::name(wxTreeEvent& WXUNUSED(event)) \
287 { \
288 wxLogMessage(#name); \
289 }
290
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)
307{
308 wxLogMessage("OnItemCollapsing");
309
310 // for testing, prevent the user from collapsing the root item
311 wxTreeItemId itemId = event.GetItem();
312 if ( !GetParent(itemId).IsOk() )
313 {
314 wxMessageBox("You can't collapse the root item.");
315
316 event.Veto();
317 }
318}
319
320void MyTreeCtrl::OnTreeKeyDown(wxTreeEvent&WXUNUSED(event))
321{
322 // show some info about this item
323 wxTreeItemId itemId = GetSelection();
324 MyTreeItemData *item = (MyTreeItemData *)GetItemData(itemId);
325
326 if ( item != NULL )
327 {
328 item->ShowInfo(this);
329 }
330
331 wxLogMessage("OnTreeKeyDown");
332}
333
334static inline const char *Bool2String(bool b)
335{
336 return b ? "" : "not ";
337}
338
339void MyTreeItemData::ShowInfo(wxTreeCtrl *tree)
340{
341 wxLogMessage("Item '%s': %sselected, %sexpanded, %sbold,\n"
342 "%u children (%u immediately under this item).",
343 m_desc.c_str(),
344 Bool2String(tree->IsSelected(GetId())),
345 Bool2String(tree->IsExpanded(GetId())),
346 Bool2String(tree->IsBold(GetId())),
347 tree->GetChildrenCount(GetId()),
348 tree->GetChildrenCount(GetId(), FALSE));
349}