]>
Commit | Line | Data |
---|---|---|
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 __WXMSW__ | |
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 small icons | |
74 | m_imageListNormal = new wxImageList(16, 16, TRUE); | |
75 | ||
76 | #ifdef __WXMSW__ | |
77 | wxIcon icon1("icon1", wxBITMAP_TYPE_ICO_RESOURCE); | |
78 | m_imageListNormal->Add(icon1); | |
79 | wxIcon icon2("icon2", wxBITMAP_TYPE_ICO_RESOURCE); | |
80 | m_imageListNormal->Add(icon2); | |
81 | #else | |
82 | #endif | |
83 | ||
84 | // Make a menubar | |
85 | wxMenu *file_menu = new wxMenu; | |
86 | ||
87 | file_menu->Append(TREE_ABOUT, "&About"); | |
88 | file_menu->Append(TREE_QUIT, "E&xit"); | |
89 | wxMenuBar *menu_bar = new wxMenuBar; | |
90 | menu_bar->Append(file_menu, "&File"); | |
91 | frame->SetMenuBar(menu_bar); | |
92 | ||
93 | // Make a panel with a message | |
94 | frame->m_treeCtrl = new MyTreeCtrl(frame, TREE_CTRL, wxPoint(0, 0), wxSize(400, 200), | |
95 | wxTR_HAS_BUTTONS|wxSUNKEN_BORDER); | |
96 | frame->m_logWindow = new wxTextCtrl(frame, -1, "", wxPoint(0, 0), wxSize(400, 200), | |
97 | wxTE_MULTILINE|wxSUNKEN_BORDER); | |
98 | ||
99 | wxLayoutConstraints *c = new wxLayoutConstraints; | |
100 | c->top.SameAs (frame, wxTop); | |
101 | c->left.SameAs (frame, wxLeft); | |
102 | c->right.SameAs (frame, wxRight); | |
103 | c->height.PercentOf (frame, wxHeight, 66); | |
104 | frame->m_treeCtrl->SetConstraints(c); | |
105 | ||
106 | c = new wxLayoutConstraints; | |
107 | c->top.Below (frame->m_treeCtrl); | |
108 | c->left.SameAs (frame, wxLeft); | |
109 | c->right.SameAs (frame, wxRight); | |
110 | c->bottom.SameAs (frame, wxBottom); | |
111 | frame->m_logWindow->SetConstraints(c); | |
112 | frame->SetAutoLayout(TRUE); | |
113 | ||
114 | frame->m_treeCtrl->SetImageList(wxGetApp().m_imageListNormal, wxIMAGE_LIST_NORMAL); | |
115 | ||
116 | long rootId = frame->m_treeCtrl->InsertItem(0, "Root", 0); | |
117 | ||
118 | char buf[20]; | |
119 | int i; | |
120 | wxString str; | |
121 | ||
122 | for ( i = 0; i < 10; i++) | |
123 | { | |
124 | sprintf(buf, "Folder child %d", i); | |
125 | str = buf; | |
126 | long id = frame->m_treeCtrl->InsertItem(rootId, str, 0); | |
127 | int j; | |
128 | for ( j = 0; j < 5; j++) | |
129 | { | |
130 | sprintf(buf, "File child %d", j); | |
131 | str = buf; | |
132 | frame->m_treeCtrl->InsertItem(id, str, 1); | |
133 | } | |
134 | } | |
135 | for ( i = 0; i < 10; i++) | |
136 | { | |
137 | sprintf(buf, "File child %d", i); | |
138 | str = buf; | |
139 | frame->m_treeCtrl->InsertItem(rootId, str, 1); | |
140 | } | |
141 | ||
142 | frame->CreateStatusBar(3); | |
143 | frame->SetStatusText("", 0); | |
144 | ||
145 | // Show the frame | |
146 | frame->Show(TRUE); | |
147 | ||
148 | SetTopWindow(frame); | |
149 | ||
150 | return TRUE; | |
151 | } | |
152 | ||
153 | // My frame constructor | |
154 | MyFrame::MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h): | |
155 | wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h)) | |
156 | { | |
157 | m_treeCtrl = NULL; | |
158 | m_logWindow = NULL; | |
159 | } | |
160 | ||
161 | MyFrame::~MyFrame(void) | |
162 | { | |
163 | delete wxGetApp().m_imageListNormal; | |
164 | } | |
165 | ||
166 | void MyFrame::OnQuit(wxCommandEvent& event) | |
167 | { | |
168 | Close(TRUE); | |
169 | } | |
170 | ||
171 | void MyFrame::OnAbout(wxCommandEvent& event) | |
172 | { | |
173 | wxMessageDialog dialog(this, "Tree test sample\nJulian Smart (c) 1997", | |
174 | "About tree test", wxOK|wxCANCEL); | |
175 | ||
176 | dialog.ShowModal(); | |
177 | } | |
178 | ||
179 | // MyTreeCtrl | |
180 | ||
181 | void MyTreeCtrl::OnBeginDrag(wxTreeEvent& event) | |
182 | { | |
183 | if ( !wxGetApp().GetTopWindow() ) | |
184 | return; | |
185 | ||
186 | wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow; | |
187 | if ( !text ) | |
188 | return; | |
189 | ||
190 | #ifndef __GNUWIN32__ | |
191 | ostream str(text); | |
192 | ||
193 | str << "OnBeginDrag\n"; | |
194 | str.flush(); | |
195 | #endif | |
196 | } | |
197 | ||
198 | void MyTreeCtrl::OnBeginRDrag(wxTreeEvent& event) | |
199 | { | |
200 | if ( !wxGetApp().GetTopWindow() ) | |
201 | return; | |
202 | ||
203 | wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow; | |
204 | if ( !text ) | |
205 | return; | |
206 | ||
207 | #ifndef __GNUWIN32__ | |
208 | ostream str(text); | |
209 | ||
210 | str << "OnBeginRDrag\n"; | |
211 | str.flush(); | |
212 | #endif | |
213 | } | |
214 | ||
215 | void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent& event) | |
216 | { | |
217 | if ( !wxGetApp().GetTopWindow() ) | |
218 | return; | |
219 | ||
220 | wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow; | |
221 | if ( !text ) | |
222 | return; | |
223 | ||
224 | #ifndef __GNUWIN32__ | |
225 | ostream str(text); | |
226 | ||
227 | str << "OnBeginLabelEdit\n"; | |
228 | str.flush(); | |
229 | #endif | |
230 | } | |
231 | ||
232 | void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent& event) | |
233 | { | |
234 | if ( !wxGetApp().GetTopWindow() ) | |
235 | return; | |
236 | ||
237 | wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow; | |
238 | if ( !text ) | |
239 | return; | |
240 | ||
241 | #ifndef __GNUWIN32__ | |
242 | ostream str(text); | |
243 | ||
244 | str << "OnEndLabelEdit\n"; | |
245 | str.flush(); | |
246 | #endif | |
247 | } | |
248 | ||
249 | void MyTreeCtrl::OnDeleteItem(wxTreeEvent& event) | |
250 | { | |
251 | if ( !wxGetApp().GetTopWindow() ) | |
252 | return; | |
253 | ||
254 | wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow; | |
255 | if ( !text ) | |
256 | return; | |
257 | ||
258 | #ifndef __GNUWIN32__ | |
259 | ostream str(text); | |
260 | ||
261 | str << "OnDeleteItem\n"; | |
262 | str.flush(); | |
263 | #endif | |
264 | } | |
265 | ||
266 | void MyTreeCtrl::OnGetInfo(wxTreeEvent& event) | |
267 | { | |
268 | if ( !wxGetApp().GetTopWindow() ) | |
269 | return; | |
270 | ||
271 | wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow; | |
272 | if ( !text ) | |
273 | return; | |
274 | ||
275 | #ifndef __GNUWIN32__ | |
276 | ostream str(text); | |
277 | ||
278 | str << "OnGetInfo\n"; | |
279 | str.flush(); | |
280 | #endif | |
281 | } | |
282 | ||
283 | void MyTreeCtrl::OnSetInfo(wxTreeEvent& event) | |
284 | { | |
285 | if ( !wxGetApp().GetTopWindow() ) | |
286 | return; | |
287 | ||
288 | wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow; | |
289 | if ( !text ) | |
290 | return; | |
291 | ||
292 | #ifndef __GNUWIN32__ | |
293 | ostream str(text); | |
294 | ||
295 | str << "OnSetInfo\n"; | |
296 | str.flush(); | |
297 | #endif | |
298 | } | |
299 | ||
300 | void MyTreeCtrl::OnItemExpanded(wxTreeEvent& event) | |
301 | { | |
302 | if ( !wxGetApp().GetTopWindow() ) | |
303 | return; | |
304 | ||
305 | wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow; | |
306 | if ( !text ) | |
307 | return; | |
308 | ||
309 | #ifndef __GNUWIN32__ | |
310 | ostream str(text); | |
311 | ||
312 | str << "OnItemExpanded\n"; | |
313 | str.flush(); | |
314 | #endif | |
315 | } | |
316 | ||
317 | void MyTreeCtrl::OnItemExpanding(wxTreeEvent& event) | |
318 | { | |
319 | if ( !wxGetApp().GetTopWindow() ) | |
320 | return; | |
321 | ||
322 | wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow; | |
323 | if ( !text ) | |
324 | return; | |
325 | ||
326 | #ifndef __GNUWIN32__ | |
327 | ostream str(text); | |
328 | ||
329 | str << "OnItemExpanding\n"; | |
330 | str.flush(); | |
331 | #endif | |
332 | } | |
333 | ||
334 | void MyTreeCtrl::OnSelChanged(wxTreeEvent& event) | |
335 | { | |
336 | if ( !wxGetApp().GetTopWindow() ) | |
337 | return; | |
338 | ||
339 | wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow; | |
340 | if ( !text ) | |
341 | return; | |
342 | ||
343 | #ifndef __GNUWIN32__ | |
344 | ostream str(text); | |
345 | ||
346 | str << "OnSelChanged\n"; | |
347 | str.flush(); | |
348 | #endif | |
349 | } | |
350 | ||
351 | void MyTreeCtrl::OnSelChanging(wxTreeEvent& event) | |
352 | { | |
353 | if ( !wxGetApp().GetTopWindow() ) | |
354 | return; | |
355 | ||
356 | wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow; | |
357 | if ( !text ) | |
358 | return; | |
359 | ||
360 | #ifndef __GNUWIN32__ | |
361 | ostream str(text); | |
362 | ||
363 | str << "OnSelChanging\n"; | |
364 | str.flush(); | |
365 | #endif | |
366 | } | |
367 | ||
368 | void MyTreeCtrl::OnKeyDown(wxTreeEvent& event) | |
369 | { | |
370 | if ( !wxGetApp().GetTopWindow() ) | |
371 | return; | |
372 | ||
373 | wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow; | |
374 | if ( !text ) | |
375 | return; | |
376 | ||
377 | #ifndef __GNUWIN32__ | |
378 | ostream str(text); | |
379 | ||
380 | str << "OnKeyDown\n"; | |
381 | str.flush(); | |
382 | #endif | |
383 | } | |
384 |