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