]> git.saurik.com Git - wxWidgets.git/blob - samples/treectrl/treetest.cpp
*** empty log message ***
[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 #include "wx/treectrl.h"
29
30 #include "treetest.h"
31
32 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
33 EVT_MENU(TREE_QUIT, MyFrame::OnQuit)
34 EVT_MENU(TREE_ABOUT, MyFrame::OnAbout)
35 END_EVENT_TABLE()
36
37 BEGIN_EVENT_TABLE(MyTreeCtrl, wxTreeCtrl)
38 EVT_TREE_BEGIN_DRAG(TREE_CTRL, MyTreeCtrl::OnBeginDrag)
39 EVT_TREE_BEGIN_RDRAG(TREE_CTRL, MyTreeCtrl::OnBeginRDrag)
40 EVT_TREE_BEGIN_LABEL_EDIT(TREE_CTRL, MyTreeCtrl::OnBeginLabelEdit)
41 EVT_TREE_END_LABEL_EDIT(TREE_CTRL, MyTreeCtrl::OnEndLabelEdit)
42 EVT_TREE_DELETE_ITEM(TREE_CTRL, MyTreeCtrl::OnDeleteItem)
43 EVT_TREE_GET_INFO(TREE_CTRL, MyTreeCtrl::OnGetInfo)
44 EVT_TREE_SET_INFO(TREE_CTRL, MyTreeCtrl::OnSetInfo)
45 EVT_TREE_ITEM_EXPANDED(TREE_CTRL, MyTreeCtrl::OnItemExpanded)
46 EVT_TREE_ITEM_EXPANDING(TREE_CTRL, MyTreeCtrl::OnItemExpanding)
47 EVT_TREE_SEL_CHANGED(TREE_CTRL, MyTreeCtrl::OnSelChanged)
48 EVT_TREE_SEL_CHANGING(TREE_CTRL, MyTreeCtrl::OnSelChanging)
49 EVT_TREE_KEY_DOWN(TREE_CTRL, MyTreeCtrl::OnKeyDown)
50 END_EVENT_TABLE()
51
52 IMPLEMENT_APP(MyApp)
53
54 // `Main program' equivalent, creating windows and returning main app frame
55 bool MyApp::OnInit(void)
56 {
57 // Create the main frame window
58 MyFrame *frame = new MyFrame(NULL, "wxTreeCtrl Test", 50, 50, 450, 340);
59
60 // This reduces flicker effects - even better would be to define OnEraseBackground
61 // to do nothing. When the tree control's scrollbars are show or hidden, the
62 // frame is sent a background erase event.
63 frame->SetBackgroundColour(wxColour(255, 255, 255));
64
65 // Give it an icon
66 #ifdef __WINDOWS__
67 frame->SetIcon(wxIcon("mondrian"));
68 #endif
69 #ifdef __X__
70 frame->SetIcon(wxIcon("aiai.xbm"));
71 #endif
72
73 // Make an image list containing large icons
74 m_imageListNormal = new wxImageList(16, 16, TRUE);
75
76 wxIcon *icon = new wxIcon("icon1", wxBITMAP_TYPE_ICO_RESOURCE);
77 m_imageListNormal->Add(*icon);
78 delete icon;
79 icon = new wxIcon("icon2", wxBITMAP_TYPE_ICO_RESOURCE);
80 m_imageListNormal->Add(*icon);
81 delete icon;
82
83 // Make a menubar
84 wxMenu *file_menu = new wxMenu;
85
86 file_menu->Append(TREE_ABOUT, "&About");
87 file_menu->Append(TREE_QUIT, "E&xit");
88 wxMenuBar *menu_bar = new wxMenuBar;
89 menu_bar->Append(file_menu, "&File");
90 frame->SetMenuBar(menu_bar);
91
92 // Make a panel with a message
93 frame->m_treeCtrl = new MyTreeCtrl(frame, TREE_CTRL, wxPoint(0, 0), wxSize(400, 200),
94 wxTR_HAS_BUTTONS|wxSUNKEN_BORDER);
95 frame->m_logWindow = new wxTextCtrl(frame, -1, "", wxPoint(0, 0), wxSize(400, 200),
96 wxTE_MULTILINE|wxSUNKEN_BORDER);
97
98 wxLayoutConstraints *c = new wxLayoutConstraints;
99 c->top.SameAs (frame, wxTop);
100 c->left.SameAs (frame, wxLeft);
101 c->right.SameAs (frame, wxRight);
102 c->height.PercentOf (frame, wxHeight, 66);
103 frame->m_treeCtrl->SetConstraints(c);
104
105 c = new wxLayoutConstraints;
106 c->top.Below (frame->m_treeCtrl);
107 c->left.SameAs (frame, wxLeft);
108 c->right.SameAs (frame, wxRight);
109 c->bottom.SameAs (frame, wxBottom);
110 frame->m_logWindow->SetConstraints(c);
111 frame->SetAutoLayout(TRUE);
112
113 frame->m_treeCtrl->SetImageList(wxGetApp().m_imageListNormal, wxIMAGE_LIST_NORMAL);
114
115 long rootId = frame->m_treeCtrl->InsertItem(0, "Root", 0);
116
117 char buf[20];
118 int i;
119 wxString str;
120
121 for ( i = 0; i < 10; i++)
122 {
123 sprintf(buf, "Folder child %d", i);
124 str = buf;
125 long id = frame->m_treeCtrl->InsertItem(rootId, str, 0);
126 int j;
127 for ( j = 0; j < 5; j++)
128 {
129 sprintf(buf, "File child %d", j);
130 str = buf;
131 frame->m_treeCtrl->InsertItem(id, str, 1);
132 }
133 }
134 for ( i = 0; i < 10; i++)
135 {
136 sprintf(buf, "File child %d", i);
137 str = buf;
138 frame->m_treeCtrl->InsertItem(rootId, str, 1);
139 }
140
141 frame->CreateStatusBar(3);
142 frame->SetStatusText("", 0);
143
144 // Show the frame
145 frame->Show(TRUE);
146
147 SetTopWindow(frame);
148
149 return TRUE;
150 }
151
152 // My frame constructor
153 MyFrame::MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h):
154 wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h))
155 {
156 m_treeCtrl = NULL;
157 m_logWindow = NULL;
158 }
159
160 MyFrame::~MyFrame(void)
161 {
162 delete wxGetApp().m_imageListNormal;
163 }
164
165 void MyFrame::OnQuit(wxCommandEvent& event)
166 {
167 Close(TRUE);
168 }
169
170 void MyFrame::OnAbout(wxCommandEvent& event)
171 {
172 wxMessageDialog dialog(this, "Tree test sample\nJulian Smart (c) 1997",
173 "About tree test", wxOK|wxCANCEL);
174
175 dialog.ShowModal();
176 }
177
178 // MyTreeCtrl
179
180 void MyTreeCtrl::OnBeginDrag(wxTreeEvent& event)
181 {
182 if ( !wxGetApp().GetTopWindow() )
183 return;
184
185 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
186 if ( !text )
187 return;
188
189 #ifndef __GNUWIN32__
190 ostream str(text);
191
192 str << "OnBeginDrag\n";
193 str.flush();
194 #endif
195 }
196
197 void MyTreeCtrl::OnBeginRDrag(wxTreeEvent& event)
198 {
199 if ( !wxGetApp().GetTopWindow() )
200 return;
201
202 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
203 if ( !text )
204 return;
205
206 #ifndef __GNUWIN32__
207 ostream str(text);
208
209 str << "OnBeginRDrag\n";
210 str.flush();
211 #endif
212 }
213
214 void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent& event)
215 {
216 if ( !wxGetApp().GetTopWindow() )
217 return;
218
219 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
220 if ( !text )
221 return;
222
223 #ifndef __GNUWIN32__
224 ostream str(text);
225
226 str << "OnBeginLabelEdit\n";
227 str.flush();
228 #endif
229 }
230
231 void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent& event)
232 {
233 if ( !wxGetApp().GetTopWindow() )
234 return;
235
236 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
237 if ( !text )
238 return;
239
240 #ifndef __GNUWIN32__
241 ostream str(text);
242
243 str << "OnEndLabelEdit\n";
244 str.flush();
245 #endif
246 }
247
248 void MyTreeCtrl::OnDeleteItem(wxTreeEvent& event)
249 {
250 if ( !wxGetApp().GetTopWindow() )
251 return;
252
253 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
254 if ( !text )
255 return;
256
257 #ifndef __GNUWIN32__
258 ostream str(text);
259
260 str << "OnDeleteItem\n";
261 str.flush();
262 #endif
263 }
264
265 void MyTreeCtrl::OnGetInfo(wxTreeEvent& event)
266 {
267 if ( !wxGetApp().GetTopWindow() )
268 return;
269
270 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
271 if ( !text )
272 return;
273
274 #ifndef __GNUWIN32__
275 ostream str(text);
276
277 str << "OnGetInfo\n";
278 str.flush();
279 #endif
280 }
281
282 void MyTreeCtrl::OnSetInfo(wxTreeEvent& event)
283 {
284 if ( !wxGetApp().GetTopWindow() )
285 return;
286
287 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
288 if ( !text )
289 return;
290
291 #ifndef __GNUWIN32__
292 ostream str(text);
293
294 str << "OnSetInfo\n";
295 str.flush();
296 #endif
297 }
298
299 void MyTreeCtrl::OnItemExpanded(wxTreeEvent& event)
300 {
301 if ( !wxGetApp().GetTopWindow() )
302 return;
303
304 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
305 if ( !text )
306 return;
307
308 #ifndef __GNUWIN32__
309 ostream str(text);
310
311 str << "OnItemExpanded\n";
312 str.flush();
313 #endif
314 }
315
316 void MyTreeCtrl::OnItemExpanding(wxTreeEvent& event)
317 {
318 if ( !wxGetApp().GetTopWindow() )
319 return;
320
321 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
322 if ( !text )
323 return;
324
325 #ifndef __GNUWIN32__
326 ostream str(text);
327
328 str << "OnItemExpanding\n";
329 str.flush();
330 #endif
331 }
332
333 void MyTreeCtrl::OnSelChanged(wxTreeEvent& event)
334 {
335 if ( !wxGetApp().GetTopWindow() )
336 return;
337
338 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
339 if ( !text )
340 return;
341
342 #ifndef __GNUWIN32__
343 ostream str(text);
344
345 str << "OnSelChanged\n";
346 str.flush();
347 #endif
348 }
349
350 void MyTreeCtrl::OnSelChanging(wxTreeEvent& event)
351 {
352 if ( !wxGetApp().GetTopWindow() )
353 return;
354
355 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
356 if ( !text )
357 return;
358
359 #ifndef __GNUWIN32__
360 ostream str(text);
361
362 str << "OnSelChanging\n";
363 str.flush();
364 #endif
365 }
366
367 void MyTreeCtrl::OnKeyDown(wxTreeEvent& event)
368 {
369 if ( !wxGetApp().GetTopWindow() )
370 return;
371
372 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
373 if ( !text )
374 return;
375
376 #ifndef __GNUWIN32__
377 ostream str(text);
378
379 str << "OnKeyDown\n";
380 str.flush();
381 #endif
382 }
383