]> git.saurik.com Git - wxWidgets.git/blame - samples/treectrl/treetest.cpp
Added wxMiniFrame
[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)
4832f7c0 67 EVT_CHAR(MyTreeCtrl::OnKeyDown)
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
144 // set our text control as the log target
145 wxLogTextCtrl *logWindow = new wxLogTextCtrl(textCtrl);
146 delete wxLog::SetActiveTarget(logWindow);
457814b5
JS
147}
148
c89a6106 149MyFrame::~MyFrame()
457814b5 150{
3a5a2f56 151 delete wxLog::SetActiveTarget(NULL);
457814b5
JS
152}
153
3a5a2f56 154void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
457814b5
JS
155{
156 Close(TRUE);
157}
158
3a5a2f56 159void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
457814b5 160{
4832f7c0
VZ
161 wxMessageDialog dialog(this,
162 "Tree test sample\n"
163 "Julian Smart (c) 1997",
3a5a2f56 164 "About tree test", wxOK);
457814b5
JS
165
166 dialog.ShowModal();
167}
168
4832f7c0
VZ
169void MyFrame::OnDump(wxCommandEvent& WXUNUSED(event))
170{
ff5bf259 171 wxTreeItemId root = m_treeCtrl->GetSelection();
4832f7c0
VZ
172 m_treeCtrl->GetItemsRecursively(root, -1);
173}
174
add28c55
VZ
175void MyFrame::DoSetBold(bool bold)
176{
3db7be80 177// m_treeCtrl->SetItemBold(m_treeCtrl->GetSelection(), bold);
add28c55
VZ
178}
179
ff5bf259
VZ
180void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event))
181{
182 wxTreeItemId item = m_treeCtrl->GetSelection();
183 m_treeCtrl->Delete(item);
184}
185
186void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event))
187{
188 m_treeCtrl->DeleteAllItems();
189}
190
191void MyFrame::OnRecreate(wxCommandEvent& event)
192{
193 OnDeleteAll(event);
194 m_treeCtrl->AddTestItemsToTree(3, 2);
195}
196
3a5a2f56
VZ
197// MyTreeCtrl implementation
198MyTreeCtrl::MyTreeCtrl(wxWindow *parent, const wxWindowID id,
199 const wxPoint& pos, const wxSize& size,
200 long style)
201 : wxTreeCtrl(parent, id, pos, size, style)
457814b5 202{
3a5a2f56
VZ
203 // Make an image list containing small icons
204 m_imageListNormal = new wxImageList(16, 16, TRUE);
457814b5 205
3a5a2f56 206 // should correspond to TreeCtrlIcon_xxx enum
df875e59 207 m_imageListNormal->Add(wxICON(icon1));
3db7be80 208 m_imageListNormal->Add(wxICON(icon2));
457814b5 209
3a5a2f56 210 SetImageList(m_imageListNormal);
457814b5 211
3a5a2f56 212 // Add some items to the tree
4832f7c0 213 AddTestItemsToTree(3, 2);
457814b5
JS
214}
215
3a5a2f56 216MyTreeCtrl::~MyTreeCtrl()
457814b5 217{
3a5a2f56 218 delete m_imageListNormal;
457814b5
JS
219}
220
3a5a2f56
VZ
221void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId& idParent,
222 size_t numChildren,
4832f7c0
VZ
223 size_t depth,
224 size_t folder)
457814b5 225{
3a5a2f56
VZ
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
4832f7c0
VZ
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
3a5a2f56
VZ
237 int image = depth == 1 ? TreeCtrlIcon_File : TreeCtrlIcon_Folder;
238 wxTreeItemId id = AppendItem(idParent, str, image, image,
239 new MyTreeItemData(str));
add28c55 240 AddItemsRecursively(id, numChildren, depth - 1, n + 1);
3a5a2f56
VZ
241 }
242 }
243 //else: done!
457814b5
JS
244}
245
3a5a2f56
VZ
246void MyTreeCtrl::AddTestItemsToTree(size_t numChildren,
247 size_t depth)
457814b5 248{
3efb01df 249 wxTreeItemId rootId = AddRoot("Root",
3a5a2f56
VZ
250 TreeCtrlIcon_Folder, TreeCtrlIcon_Folder,
251 new MyTreeItemData("Root item"));
457814b5 252
add28c55 253 AddItemsRecursively(rootId, numChildren, depth, 0);
4832f7c0
VZ
254}
255
256void 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);
457814b5
JS
275}
276
4832f7c0 277
3a5a2f56
VZ
278// avoid repetition
279#define TREE_EVENT_HANDLER(name) \
280 void MyTreeCtrl::name(wxTreeEvent& WXUNUSED(event)) \
281 { \
282 wxLogMessage(#name); \
283 }
457814b5 284
3a5a2f56
VZ
285TREE_EVENT_HANDLER(OnBeginDrag)
286TREE_EVENT_HANDLER(OnBeginRDrag)
287TREE_EVENT_HANDLER(OnBeginLabelEdit)
288TREE_EVENT_HANDLER(OnEndLabelEdit)
289TREE_EVENT_HANDLER(OnDeleteItem)
290TREE_EVENT_HANDLER(OnGetInfo)
291TREE_EVENT_HANDLER(OnSetInfo)
292TREE_EVENT_HANDLER(OnItemExpanded)
293TREE_EVENT_HANDLER(OnItemExpanding)
294TREE_EVENT_HANDLER(OnItemCollapsed)
295TREE_EVENT_HANDLER(OnSelChanged)
296TREE_EVENT_HANDLER(OnSelChanging)
297
298#undef TREE_EVENT_HANDLER
299
300void MyTreeCtrl::OnItemCollapsing(wxTreeEvent& event)
457814b5 301{
f135ff73
VZ
302 wxLogMessage("OnItemCollapsing");
303
3a5a2f56
VZ
304 // for testing, prevent the user from collapsing the root item
305 wxTreeItemId itemId = event.GetItem();
306 if ( !GetParent(itemId).IsOk() )
307 {
f135ff73 308 wxMessageBox("You can't collapse the root item.");
457814b5 309
3a5a2f56
VZ
310 event.Veto();
311 }
457814b5
JS
312}
313
d18ed59a 314void MyTreeCtrl::OnKeyDown(wxTreeEvent& WXUNUSED(event))
457814b5 315{
3a5a2f56
VZ
316 // show some info about this item
317 wxTreeItemId itemId = GetSelection();
318 MyTreeItemData *item = (MyTreeItemData *)GetItemData(itemId);
457814b5 319
3a5a2f56
VZ
320 if ( item != NULL )
321 {
322 item->ShowInfo(this);
323 }
457814b5 324
3a5a2f56 325 wxLogMessage("OnKeyDown");
457814b5
JS
326}
327
3a5a2f56 328static inline const char *Bool2String(bool b)
457814b5 329{
3a5a2f56 330 return b ? "" : "not ";
457814b5
JS
331}
332
3a5a2f56 333void MyTreeItemData::ShowInfo(wxTreeCtrl *tree)
457814b5 334{
3db7be80 335/*
add28c55 336 wxLogMessage("Item '%s': %sselected, %sexpanded, %sbold,\n"
4832f7c0 337 "%u children (%u immediately under this item).",
3a5a2f56 338 m_desc.c_str(),
f135ff73 339 Bool2String(tree->IsSelected(GetId())),
4832f7c0 340 Bool2String(tree->IsExpanded(GetId())),
add28c55 341 Bool2String(tree->IsBold(GetId())),
4832f7c0
VZ
342 tree->GetChildrenCount(GetId()),
343 tree->GetChildrenCount(GetId(), FALSE));
3db7be80 344*/
457814b5 345}