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