]> git.saurik.com Git - wxWidgets.git/blob - samples/treectrl/treetest.cpp
Added wxRegion version of wxWindowDC::SetClippingRegion
[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 // 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
42 BEGIN_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)
51 END_EVENT_TABLE()
52
53 BEGIN_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_CHAR(MyTreeCtrl::OnKeyDown)
68 END_EVENT_TABLE()
69
70 IMPLEMENT_APP(MyApp)
71
72 // `Main program' equivalent, creating windows and returning main app frame
73 bool 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
87 MyFrame::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 // set our text control as the log target
145 wxLogTextCtrl *logWindow = new wxLogTextCtrl(textCtrl);
146 delete wxLog::SetActiveTarget(logWindow);
147 }
148
149 MyFrame::~MyFrame()
150 {
151 delete wxLog::SetActiveTarget(NULL);
152 }
153
154 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
155 {
156 Close(TRUE);
157 }
158
159 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
160 {
161 wxMessageDialog dialog(this,
162 "Tree test sample\n"
163 "Julian Smart (c) 1997",
164 "About tree test", wxOK);
165
166 dialog.ShowModal();
167 }
168
169 void MyFrame::OnDump(wxCommandEvent& WXUNUSED(event))
170 {
171 wxTreeItemId root = m_treeCtrl->GetSelection();
172 m_treeCtrl->GetItemsRecursively(root, -1);
173 }
174
175 void MyFrame::DoSetBold(bool bold)
176 {
177 // m_treeCtrl->SetItemBold(m_treeCtrl->GetSelection(), bold);
178 }
179
180 void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event))
181 {
182 wxTreeItemId item = m_treeCtrl->GetSelection();
183 m_treeCtrl->Delete(item);
184 }
185
186 void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event))
187 {
188 m_treeCtrl->DeleteAllItems();
189 }
190
191 void MyFrame::OnRecreate(wxCommandEvent& event)
192 {
193 OnDeleteAll(event);
194 m_treeCtrl->AddTestItemsToTree(3, 2);
195 }
196
197 // MyTreeCtrl implementation
198 MyTreeCtrl::MyTreeCtrl(wxWindow *parent, const wxWindowID id,
199 const wxPoint& pos, const wxSize& size,
200 long style)
201 : wxTreeCtrl(parent, id, pos, size, style)
202 {
203 // Make an image list containing small icons
204 m_imageListNormal = new wxImageList(16, 16, TRUE);
205
206 // should correspond to TreeCtrlIcon_xxx enum
207 m_imageListNormal->Add(wxICON(icon1));
208 m_imageListNormal->Add(wxICON(icon2));
209
210 SetImageList(m_imageListNormal);
211
212 // Add some items to the tree
213 AddTestItemsToTree(3, 2);
214 }
215
216 MyTreeCtrl::~MyTreeCtrl()
217 {
218 delete m_imageListNormal;
219 }
220
221 void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId& idParent,
222 size_t numChildren,
223 size_t depth,
224 size_t folder)
225 {
226 if ( depth > 0 )
227 {
228 wxString str;
229 for ( size_t n = 0; n < numChildren; n++ )
230 {
231 // at depth 1 elements won't have any more children
232 if (depth == 1)
233 str.Printf("%s child %d.%d", "File", folder, n + 1);
234 else
235 str.Printf("%s child %d","Folder", n + 1);
236
237 int image = depth == 1 ? TreeCtrlIcon_File : TreeCtrlIcon_Folder;
238 wxTreeItemId id = AppendItem(idParent, str, image, image,
239 new MyTreeItemData(str));
240 AddItemsRecursively(id, numChildren, depth - 1, n + 1);
241 }
242 }
243 //else: done!
244 }
245
246 void MyTreeCtrl::AddTestItemsToTree(size_t numChildren,
247 size_t depth)
248 {
249 wxTreeItemId rootId = AddRoot("Root",
250 TreeCtrlIcon_Folder, TreeCtrlIcon_Folder,
251 new MyTreeItemData("Root item"));
252
253 AddItemsRecursively(rootId, numChildren, depth, 0);
254 }
255
256 void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId& idParent, long cookie)
257 {
258 wxTreeItemId id;
259
260 if( cookie == -1 )
261 id = GetFirstChild(idParent, cookie);
262 else
263 id = GetNextChild(idParent, cookie);
264
265 if(id <= 0)
266 return;
267
268 wxString text=GetItemText(id);
269 wxLogMessage(text);
270
271 if (ItemHasChildren(id))
272 GetItemsRecursively(id,-1);
273
274 GetItemsRecursively(idParent, cookie);
275 }
276
277
278 // avoid repetition
279 #define TREE_EVENT_HANDLER(name) \
280 void MyTreeCtrl::name(wxTreeEvent& WXUNUSED(event)) \
281 { \
282 wxLogMessage(#name); \
283 }
284
285 TREE_EVENT_HANDLER(OnBeginDrag)
286 TREE_EVENT_HANDLER(OnBeginRDrag)
287 TREE_EVENT_HANDLER(OnBeginLabelEdit)
288 TREE_EVENT_HANDLER(OnEndLabelEdit)
289 TREE_EVENT_HANDLER(OnDeleteItem)
290 TREE_EVENT_HANDLER(OnGetInfo)
291 TREE_EVENT_HANDLER(OnSetInfo)
292 TREE_EVENT_HANDLER(OnItemExpanded)
293 TREE_EVENT_HANDLER(OnItemExpanding)
294 TREE_EVENT_HANDLER(OnItemCollapsed)
295 TREE_EVENT_HANDLER(OnSelChanged)
296 TREE_EVENT_HANDLER(OnSelChanging)
297
298 #undef TREE_EVENT_HANDLER
299
300 void MyTreeCtrl::OnItemCollapsing(wxTreeEvent& event)
301 {
302 wxLogMessage("OnItemCollapsing");
303
304 // for testing, prevent the user from collapsing the root item
305 wxTreeItemId itemId = event.GetItem();
306 if ( !GetParent(itemId).IsOk() )
307 {
308 wxMessageBox("You can't collapse the root item.");
309
310 event.Veto();
311 }
312 }
313
314 void MyTreeCtrl::OnKeyDown(wxTreeEvent& WXUNUSED(event))
315 {
316 // show some info about this item
317 wxTreeItemId itemId = GetSelection();
318 MyTreeItemData *item = (MyTreeItemData *)GetItemData(itemId);
319
320 if ( item != NULL )
321 {
322 item->ShowInfo(this);
323 }
324
325 wxLogMessage("OnKeyDown");
326 }
327
328 static inline const char *Bool2String(bool b)
329 {
330 return b ? "" : "not ";
331 }
332
333 void MyTreeItemData::ShowInfo(wxTreeCtrl *tree)
334 {
335 /*
336 wxLogMessage("Item '%s': %sselected, %sexpanded, %sbold,\n"
337 "%u children (%u immediately under this item).",
338 m_desc.c_str(),
339 Bool2String(tree->IsSelected(GetId())),
340 Bool2String(tree->IsExpanded(GetId())),
341 Bool2String(tree->IsBold(GetId())),
342 tree->GetChildrenCount(GetId()),
343 tree->GetChildrenCount(GetId(), FALSE));
344 */
345 }