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