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