]> git.saurik.com Git - wxWidgets.git/blob - samples/treectrl/treetest.cpp
Fixed 5 bugs in wxImage and Blit
[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 END_EVENT_TABLE()
46
47 BEGIN_EVENT_TABLE(MyTreeCtrl, wxTreeCtrl)
48 EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginDrag)
49 EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginRDrag)
50 EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnBeginLabelEdit)
51 EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnEndLabelEdit)
52 EVT_TREE_DELETE_ITEM(TreeTest_Ctrl, MyTreeCtrl::OnDeleteItem)
53 EVT_TREE_GET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnGetInfo)
54 EVT_TREE_SET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnSetInfo)
55 EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanded)
56 EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanding)
57 EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsed)
58 EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsing)
59 EVT_TREE_SEL_CHANGED(TreeTest_Ctrl, MyTreeCtrl::OnSelChanged)
60 EVT_TREE_SEL_CHANGING(TreeTest_Ctrl, MyTreeCtrl::OnSelChanging)
61 EVT_TREE_KEY_DOWN(TreeTest_Ctrl, MyTreeCtrl::OnKeyDown)
62 END_EVENT_TABLE()
63
64 IMPLEMENT_APP(MyApp)
65
66 // `Main program' equivalent, creating windows and returning main app frame
67 bool MyApp::OnInit()
68 {
69 // Create the main frame window
70 MyFrame *frame = new MyFrame("wxTreeCtrl Test", 50, 50, 450, 340);
71
72 // Show the frame
73 frame->Show(TRUE);
74 SetTopWindow(frame);
75
76 return TRUE;
77 }
78
79
80 // My frame constructor
81 MyFrame::MyFrame(const wxString& title, int x, int y, int w, int h)
82 : wxFrame((wxFrame *)NULL, -1, title, wxPoint(x, y), wxSize(w, h))
83 {
84 // This reduces flicker effects - even better would be to define
85 // OnEraseBackground to do nothing. When the tree control's scrollbars are
86 // show or hidden, the frame is sent a background erase event.
87 SetBackgroundColour(wxColour(255, 255, 255));
88
89 // Give it an icon
90 SetIcon(wxICON(mondrian));
91
92 // Make a menubar
93 wxMenu *file_menu = new wxMenu;
94
95 file_menu->Append(TreeTest_About, "&About...");
96 file_menu->Append(TreeTest_Quit, "E&xit");
97 wxMenuBar *menu_bar = new wxMenuBar;
98 menu_bar->Append(file_menu, "&File");
99 SetMenuBar(menu_bar);
100
101 // Make a panel with a message
102 m_treeCtrl = new MyTreeCtrl(this, TreeTest_Ctrl,
103 wxDefaultPosition, wxDefaultSize,
104 wxTR_HAS_BUTTONS | wxSUNKEN_BORDER);
105 wxTextCtrl *textCtrl = new wxTextCtrl(this, -1, "",
106 wxDefaultPosition, wxDefaultSize,
107 wxTE_MULTILINE | wxSUNKEN_BORDER);
108
109 wxLayoutConstraints *c = new wxLayoutConstraints;
110 c->top.SameAs(this, wxTop);
111 c->left.SameAs(this, wxLeft);
112 c->right.SameAs(this, wxRight);
113 c->height.PercentOf(this, wxHeight, 66);
114 m_treeCtrl->SetConstraints(c);
115
116 c = new wxLayoutConstraints;
117 c->top.Below(m_treeCtrl);
118 c->left.SameAs(this, wxLeft);
119 c->right.SameAs(this, wxRight);
120 c->bottom.SameAs(this, wxBottom);
121 textCtrl->SetConstraints(c);
122 SetAutoLayout(TRUE);
123
124 // create a status bar with 3 panes
125 CreateStatusBar(3);
126 SetStatusText("", 0);
127
128 // set our text control as the log target
129 wxLogTextCtrl *logWindow = new wxLogTextCtrl(textCtrl);
130 delete wxLog::SetActiveTarget(logWindow);
131 }
132
133 MyFrame::~MyFrame()
134 {
135 delete wxLog::SetActiveTarget(NULL);
136 }
137
138 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
139 {
140 Close(TRUE);
141 }
142
143 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
144 {
145 wxMessageDialog dialog(this, "Tree test sample\nJulian Smart (c) 1997",
146 "About tree test", wxOK);
147
148 dialog.ShowModal();
149 }
150
151 // MyTreeCtrl implementation
152 MyTreeCtrl::MyTreeCtrl(wxWindow *parent, const wxWindowID id,
153 const wxPoint& pos, const wxSize& size,
154 long style)
155 : wxTreeCtrl(parent, id, pos, size, style)
156 {
157 // Make an image list containing small icons
158 m_imageListNormal = new wxImageList(16, 16, TRUE);
159
160 // should correspond to TreeCtrlIcon_xxx enum
161 m_imageListNormal->Add(wxICON(icon2));
162 m_imageListNormal->Add(wxICON(icon1));
163
164 SetImageList(m_imageListNormal);
165
166 // Add some items to the tree
167 AddTestItemsToTree(4, 3);
168 }
169
170 MyTreeCtrl::~MyTreeCtrl()
171 {
172 delete m_imageListNormal;
173 }
174
175 void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId& idParent,
176 size_t numChildren,
177 size_t depth)
178 {
179 if ( depth > 0 )
180 {
181 wxString str;
182 for ( size_t n = 0; n < numChildren; n++ )
183 {
184 // at depth 1 elements won't have any more children
185 str.Printf("%s child %d", depth == 1 ? "File" : "Folder", n + 1);
186 int image = depth == 1 ? TreeCtrlIcon_File : TreeCtrlIcon_Folder;
187 wxTreeItemId id = AppendItem(idParent, str, image, image,
188 new MyTreeItemData(str));
189 AddItemsRecursively(id, numChildren, depth - 1);
190 }
191 }
192 //else: done!
193 }
194
195 void MyTreeCtrl::AddTestItemsToTree(size_t numChildren,
196 size_t depth)
197 {
198 wxTreeItemId rootId = AddRoot("Root",
199 TreeCtrlIcon_Folder, TreeCtrlIcon_Folder,
200 new MyTreeItemData("Root item"));
201
202 AddItemsRecursively(rootId, numChildren, depth);
203 }
204
205 // avoid repetition
206 #define TREE_EVENT_HANDLER(name) \
207 void MyTreeCtrl::name(wxTreeEvent& WXUNUSED(event)) \
208 { \
209 wxLogMessage(#name); \
210 }
211
212 TREE_EVENT_HANDLER(OnBeginDrag)
213 TREE_EVENT_HANDLER(OnBeginRDrag)
214 TREE_EVENT_HANDLER(OnBeginLabelEdit)
215 TREE_EVENT_HANDLER(OnEndLabelEdit)
216 TREE_EVENT_HANDLER(OnDeleteItem)
217 TREE_EVENT_HANDLER(OnGetInfo)
218 TREE_EVENT_HANDLER(OnSetInfo)
219 TREE_EVENT_HANDLER(OnItemExpanded)
220 TREE_EVENT_HANDLER(OnItemExpanding)
221 TREE_EVENT_HANDLER(OnItemCollapsed)
222 TREE_EVENT_HANDLER(OnSelChanged)
223 TREE_EVENT_HANDLER(OnSelChanging)
224
225 #undef TREE_EVENT_HANDLER
226
227 void MyTreeCtrl::OnItemCollapsing(wxTreeEvent& event)
228 {
229 wxLogMessage("OnItemCollapsing");
230
231 // for testing, prevent the user from collapsing the root item
232 wxTreeItemId itemId = event.GetItem();
233 if ( !GetParent(itemId).IsOk() )
234 {
235 wxMessageBox("You can't collapse the root item.");
236
237 event.Veto();
238 }
239 }
240
241 void MyTreeCtrl::OnKeyDown(wxTreeEvent& event)
242 {
243 // show some info about this item
244 wxTreeItemId itemId = GetSelection();
245 MyTreeItemData *item = (MyTreeItemData *)GetItemData(itemId);
246
247 if ( item != NULL )
248 {
249 item->ShowInfo(this);
250 }
251
252 wxLogMessage("OnKeyDown");
253 }
254
255 static inline const char *Bool2String(bool b)
256 {
257 return b ? "" : "not ";
258 }
259
260 void MyTreeItemData::ShowInfo(wxTreeCtrl *tree)
261 {
262 wxLogMessage("Item '%s': %sselected, %sexpanded.",
263 m_desc.c_str(),
264 Bool2String(tree->IsSelected(GetId())),
265 Bool2String(tree->IsExpanded(GetId())));
266 }