]>
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 | |
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 | #include "wx/log.h" | |
27 | #endif | |
28 | ||
29 | #include "wx/colordlg.h" | |
30 | #include "wx/numdlg.h" | |
31 | ||
32 | #include "wx/image.h" | |
33 | #include "wx/imaglist.h" | |
34 | #include "wx/treectrl.h" | |
35 | #include "wx/math.h" | |
36 | ||
37 | #ifdef __WIN32__ | |
38 | // this is not supported by native control | |
39 | #define NO_VARIABLE_HEIGHT | |
40 | #endif | |
41 | ||
42 | #include "treetest.h" | |
43 | ||
44 | #include "icon1.xpm" | |
45 | #include "icon2.xpm" | |
46 | #include "icon3.xpm" | |
47 | #include "icon4.xpm" | |
48 | #include "icon5.xpm" | |
49 | ||
50 | #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) | |
51 | #include "mondrian.xpm" | |
52 | #endif | |
53 | ||
54 | ||
55 | // verify that the item is ok and insult the user if it is not | |
56 | #define CHECK_ITEM( item ) if ( !item.IsOk() ) { \ | |
57 | wxMessageBox(wxT("Please select some item first!"), \ | |
58 | wxT("Tree sample error"), \ | |
59 | wxOK | wxICON_EXCLAMATION, \ | |
60 | this); \ | |
61 | return; \ | |
62 | } | |
63 | ||
64 | #define MENU_LINK(name) EVT_MENU(TreeTest_##name, MyFrame::On##name) | |
65 | ||
66 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
67 | EVT_IDLE(MyFrame::OnIdle) | |
68 | EVT_SIZE(MyFrame::OnSize) | |
69 | ||
70 | MENU_LINK(Quit) | |
71 | MENU_LINK(About) | |
72 | ||
73 | MENU_LINK(TogButtons) | |
74 | MENU_LINK(TogTwist) | |
75 | MENU_LINK(TogLines) | |
76 | MENU_LINK(TogEdit) | |
77 | MENU_LINK(TogHideRoot) | |
78 | MENU_LINK(TogRootLines) | |
79 | MENU_LINK(TogBorder) | |
80 | MENU_LINK(TogFullHighlight) | |
81 | MENU_LINK(SetFgColour) | |
82 | MENU_LINK(SetBgColour) | |
83 | MENU_LINK(ResetStyle) | |
84 | ||
85 | MENU_LINK(Dump) | |
86 | #ifndef NO_MULTIPLE_SELECTION | |
87 | MENU_LINK(DumpSelected) | |
88 | MENU_LINK(Select) | |
89 | MENU_LINK(Unselect) | |
90 | MENU_LINK(ToggleSel) | |
91 | #endif // NO_MULTIPLE_SELECTION | |
92 | MENU_LINK(Rename) | |
93 | MENU_LINK(Count) | |
94 | MENU_LINK(CountRec) | |
95 | MENU_LINK(Sort) | |
96 | MENU_LINK(SortRev) | |
97 | MENU_LINK(SetBold) | |
98 | MENU_LINK(ClearBold) | |
99 | MENU_LINK(Delete) | |
100 | MENU_LINK(DeleteChildren) | |
101 | MENU_LINK(DeleteAll) | |
102 | MENU_LINK(Recreate) | |
103 | MENU_LINK(ToggleImages) | |
104 | MENU_LINK(ToggleButtons) | |
105 | MENU_LINK(SetImageSize) | |
106 | MENU_LINK(CollapseAndReset) | |
107 | MENU_LINK(EnsureVisible) | |
108 | MENU_LINK(AddItem) | |
109 | MENU_LINK(InsertItem) | |
110 | MENU_LINK(IncIndent) | |
111 | MENU_LINK(DecIndent) | |
112 | MENU_LINK(IncSpacing) | |
113 | MENU_LINK(DecSpacing) | |
114 | MENU_LINK(ToggleIcon) | |
115 | MENU_LINK(SelectRoot) | |
116 | #undef MENU_LINK | |
117 | ||
118 | END_EVENT_TABLE() | |
119 | ||
120 | #if USE_GENERIC_TREECTRL | |
121 | BEGIN_EVENT_TABLE(MyTreeCtrl, wxGenericTreeCtrl) | |
122 | #else | |
123 | BEGIN_EVENT_TABLE(MyTreeCtrl, wxTreeCtrl) | |
124 | #endif | |
125 | EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginDrag) | |
126 | EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginRDrag) | |
127 | EVT_TREE_END_DRAG(TreeTest_Ctrl, MyTreeCtrl::OnEndDrag) | |
128 | EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnBeginLabelEdit) | |
129 | EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnEndLabelEdit) | |
130 | EVT_TREE_DELETE_ITEM(TreeTest_Ctrl, MyTreeCtrl::OnDeleteItem) | |
131 | #if 0 // there are so many of those that logging them causes flicker | |
132 | EVT_TREE_GET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnGetInfo) | |
133 | #endif | |
134 | EVT_TREE_SET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnSetInfo) | |
135 | EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanded) | |
136 | EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanding) | |
137 | EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsed) | |
138 | EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsing) | |
139 | ||
140 | EVT_CONTEXT_MENU(MyTreeCtrl::OnContextMenu) | |
141 | EVT_TREE_SEL_CHANGED(TreeTest_Ctrl, MyTreeCtrl::OnSelChanged) | |
142 | EVT_TREE_SEL_CHANGING(TreeTest_Ctrl, MyTreeCtrl::OnSelChanging) | |
143 | EVT_TREE_KEY_DOWN(TreeTest_Ctrl, MyTreeCtrl::OnTreeKeyDown) | |
144 | EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl, MyTreeCtrl::OnItemActivated) | |
145 | EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick) | |
146 | END_EVENT_TABLE() | |
147 | ||
148 | IMPLEMENT_APP(MyApp) | |
149 | ||
150 | bool MyApp::OnInit() | |
151 | { | |
152 | // Create the main frame window | |
153 | MyFrame *frame = new MyFrame(wxT("wxTreeCtrl Test"), 50, 50, 450, 600); | |
154 | ||
155 | // Show the frame | |
156 | frame->Show(true); | |
157 | SetTopWindow(frame); | |
158 | ||
159 | return true; | |
160 | } | |
161 | ||
162 | ||
163 | // My frame constructor | |
164 | MyFrame::MyFrame(const wxString& title, int x, int y, int w, int h) | |
165 | : wxFrame((wxFrame *)NULL, wxID_ANY, title, wxPoint(x, y), wxSize(w, h)), | |
166 | m_treeCtrl(NULL) | |
167 | #if wxUSE_LOG | |
168 | , m_textCtrl(NULL) | |
169 | #endif // wxUSE_LOG | |
170 | { | |
171 | // This reduces flicker effects - even better would be to define | |
172 | // OnEraseBackground to do nothing. When the tree control's scrollbars are | |
173 | // show or hidden, the frame is sent a background erase event. | |
174 | SetBackgroundColour(wxColour(255, 255, 255)); | |
175 | ||
176 | // Give it an icon | |
177 | SetIcon(wxICON(mondrian)); | |
178 | ||
179 | #if wxUSE_MENUS | |
180 | // Make a menubar | |
181 | wxMenu *file_menu = new wxMenu, | |
182 | *style_menu = new wxMenu, | |
183 | *tree_menu = new wxMenu, | |
184 | *item_menu = new wxMenu; | |
185 | ||
186 | file_menu->Append(TreeTest_About, wxT("&About...")); | |
187 | file_menu->AppendSeparator(); | |
188 | file_menu->Append(TreeTest_Quit, wxT("E&xit\tAlt-X")); | |
189 | ||
190 | style_menu->AppendCheckItem(TreeTest_TogButtons, wxT("Toggle &normal buttons")); | |
191 | style_menu->AppendCheckItem(TreeTest_TogTwist, wxT("Toggle &twister buttons")); | |
192 | style_menu->AppendCheckItem(TreeTest_ToggleButtons, wxT("Toggle image &buttons")); | |
193 | style_menu->AppendSeparator(); | |
194 | style_menu->AppendCheckItem(TreeTest_TogLines, wxT("Toggle &connecting lines")); | |
195 | style_menu->AppendCheckItem(TreeTest_TogRootLines, wxT("Toggle &lines at root")); | |
196 | style_menu->AppendCheckItem(TreeTest_TogHideRoot, wxT("Toggle &hidden root")); | |
197 | style_menu->AppendCheckItem(TreeTest_TogBorder, wxT("Toggle &item border")); | |
198 | style_menu->AppendCheckItem(TreeTest_TogFullHighlight, wxT("Toggle &full row highlight")); | |
199 | style_menu->AppendCheckItem(TreeTest_TogEdit, wxT("Toggle &edit mode")); | |
200 | #ifndef NO_MULTIPLE_SELECTION | |
201 | style_menu->AppendCheckItem(TreeTest_ToggleSel, wxT("Toggle &selection mode")); | |
202 | #endif // NO_MULTIPLE_SELECTION | |
203 | style_menu->AppendCheckItem(TreeTest_ToggleImages, wxT("Toggle show ima&ges")); | |
204 | style_menu->Append(TreeTest_SetImageSize, wxT("Set image si&ze...")); | |
205 | style_menu->AppendSeparator(); | |
206 | style_menu->Append(TreeTest_SetFgColour, wxT("Set &foreground colour...")); | |
207 | style_menu->Append(TreeTest_SetBgColour, wxT("Set &background colour...")); | |
208 | style_menu->AppendSeparator(); | |
209 | style_menu->Append(TreeTest_ResetStyle, wxT("&Reset to default\tF10")); | |
210 | ||
211 | tree_menu->Append(TreeTest_Recreate, wxT("&Recreate the tree")); | |
212 | tree_menu->Append(TreeTest_CollapseAndReset, wxT("C&ollapse and reset")); | |
213 | tree_menu->AppendSeparator(); | |
214 | tree_menu->Append(TreeTest_AddItem, wxT("Append a &new item")); | |
215 | tree_menu->Append(TreeTest_InsertItem, wxT("&Insert a new item")); | |
216 | tree_menu->Append(TreeTest_Delete, wxT("&Delete this item")); | |
217 | tree_menu->Append(TreeTest_DeleteChildren, wxT("Delete &children")); | |
218 | tree_menu->Append(TreeTest_DeleteAll, wxT("Delete &all items")); | |
219 | tree_menu->Append(TreeTest_SelectRoot, wxT("Select root item")); | |
220 | tree_menu->AppendSeparator(); | |
221 | tree_menu->Append(TreeTest_Count, wxT("Count children of current item")); | |
222 | tree_menu->Append(TreeTest_CountRec, wxT("Recursively count children of current item")); | |
223 | tree_menu->AppendSeparator(); | |
224 | tree_menu->Append(TreeTest_Sort, wxT("Sort children of current item")); | |
225 | tree_menu->Append(TreeTest_SortRev, wxT("Sort in reversed order")); | |
226 | tree_menu->AppendSeparator(); | |
227 | tree_menu->Append(TreeTest_EnsureVisible, wxT("Make the last item &visible")); | |
228 | tree_menu->AppendSeparator(); | |
229 | tree_menu->Append(TreeTest_IncIndent, wxT("Add 5 points to indentation\tAlt-I")); | |
230 | tree_menu->Append(TreeTest_DecIndent, wxT("Reduce indentation by 5 points\tAlt-R")); | |
231 | tree_menu->AppendSeparator(); | |
232 | tree_menu->Append(TreeTest_IncSpacing, wxT("Add 5 points to spacing\tCtrl-I")); | |
233 | tree_menu->Append(TreeTest_DecSpacing, wxT("Reduce spacing by 5 points\tCtrl-R")); | |
234 | ||
235 | item_menu->Append(TreeTest_Dump, wxT("&Dump item children")); | |
236 | item_menu->Append(TreeTest_Rename, wxT("&Rename item...")); | |
237 | ||
238 | item_menu->AppendSeparator(); | |
239 | item_menu->Append(TreeTest_SetBold, wxT("Make item &bold")); | |
240 | item_menu->Append(TreeTest_ClearBold, wxT("Make item ¬ bold")); | |
241 | item_menu->AppendSeparator(); | |
242 | item_menu->Append(TreeTest_ToggleIcon, wxT("Toggle the item's &icon")); | |
243 | ||
244 | #ifndef NO_MULTIPLE_SELECTION | |
245 | item_menu->AppendSeparator(); | |
246 | item_menu->Append(TreeTest_DumpSelected, wxT("Dump selected items\tAlt-D")); | |
247 | item_menu->Append(TreeTest_Select, wxT("Select current item\tAlt-S")); | |
248 | item_menu->Append(TreeTest_Unselect, wxT("Unselect everything\tAlt-U")); | |
249 | #endif // NO_MULTIPLE_SELECTION | |
250 | ||
251 | wxMenuBar *menu_bar = new wxMenuBar; | |
252 | menu_bar->Append(file_menu, wxT("&File")); | |
253 | menu_bar->Append(style_menu, wxT("&Style")); | |
254 | menu_bar->Append(tree_menu, wxT("&Tree")); | |
255 | menu_bar->Append(item_menu, wxT("&Item")); | |
256 | SetMenuBar(menu_bar); | |
257 | #endif // wxUSE_MENUS | |
258 | ||
259 | #if wxUSE_LOG | |
260 | // create the controls | |
261 | m_textCtrl = new wxTextCtrl(this, wxID_ANY, wxT(""), | |
262 | wxDefaultPosition, wxDefaultSize, | |
263 | wxTE_MULTILINE | wxSUNKEN_BORDER); | |
264 | #endif // wxUSE_LOG | |
265 | ||
266 | CreateTreeWithDefStyle(); | |
267 | ||
268 | menu_bar->Check(TreeTest_ToggleImages, true); | |
269 | ||
270 | #if wxUSE_STATUSBAR | |
271 | // create a status bar | |
272 | CreateStatusBar(2); | |
273 | #endif // wxUSE_STATUSBAR | |
274 | ||
275 | #if wxUSE_LOG | |
276 | #ifdef __WXMOTIF__ | |
277 | // For some reason, we get a memcpy crash in wxLogStream::DoLogStream | |
278 | // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc? | |
279 | delete wxLog::SetActiveTarget(new wxLogStderr); | |
280 | #else | |
281 | // set our text control as the log target | |
282 | wxLogTextCtrl *logWindow = new wxLogTextCtrl(m_textCtrl); | |
283 | delete wxLog::SetActiveTarget(logWindow); | |
284 | #endif | |
285 | #endif // wxUSE_LOG | |
286 | } | |
287 | ||
288 | MyFrame::~MyFrame() | |
289 | { | |
290 | #if wxUSE_LOG | |
291 | delete wxLog::SetActiveTarget(NULL); | |
292 | #endif // wxUSE_LOG | |
293 | } | |
294 | ||
295 | void MyFrame::CreateTreeWithDefStyle() | |
296 | { | |
297 | long style = wxTR_DEFAULT_STYLE | | |
298 | #ifndef NO_VARIABLE_HEIGHT | |
299 | wxTR_HAS_VARIABLE_ROW_HEIGHT | | |
300 | #endif | |
301 | wxTR_EDIT_LABELS; | |
302 | ||
303 | CreateTree(style | wxSUNKEN_BORDER); | |
304 | ||
305 | // as we don't know what wxTR_DEFAULT_STYLE could contain, test for | |
306 | // everything | |
307 | wxMenuBar *mbar = GetMenuBar(); | |
308 | mbar->Check(TreeTest_TogButtons, (style & wxTR_HAS_BUTTONS) != 0); | |
309 | mbar->Check(TreeTest_TogButtons, (style & wxTR_TWIST_BUTTONS) != 0); | |
310 | mbar->Check(TreeTest_TogLines, (style & wxTR_NO_LINES) == 0); | |
311 | mbar->Check(TreeTest_TogRootLines, (style & wxTR_LINES_AT_ROOT) != 0); | |
312 | mbar->Check(TreeTest_TogHideRoot, (style & wxTR_HIDE_ROOT) != 0); | |
313 | mbar->Check(TreeTest_TogEdit, (style & wxTR_EDIT_LABELS) != 0); | |
314 | mbar->Check(TreeTest_TogBorder, (style & wxTR_ROW_LINES) != 0); | |
315 | mbar->Check(TreeTest_TogFullHighlight, (style & wxTR_FULL_ROW_HIGHLIGHT) != 0); | |
316 | } | |
317 | ||
318 | void MyFrame::CreateTree(long style) | |
319 | { | |
320 | m_treeCtrl = new MyTreeCtrl(this, TreeTest_Ctrl, | |
321 | wxDefaultPosition, wxDefaultSize, | |
322 | style); | |
323 | Resize(); | |
324 | } | |
325 | ||
326 | void MyFrame::TogStyle(int id, long flag) | |
327 | { | |
328 | long style = m_treeCtrl->GetWindowStyle() ^ flag; | |
329 | ||
330 | // most treectrl styles can't be changed on the fly using the native | |
331 | // control and the tree must be recreated | |
332 | #ifndef __WXMSW__ | |
333 | m_treeCtrl->SetWindowStyle(style); | |
334 | #else // MSW | |
335 | delete m_treeCtrl; | |
336 | CreateTree(style); | |
337 | #endif // !MSW/MSW | |
338 | ||
339 | GetMenuBar()->Check(id, (style & flag) != 0); | |
340 | } | |
341 | ||
342 | void MyFrame::OnIdle(wxIdleEvent& event) | |
343 | { | |
344 | #if wxUSE_STATUSBAR | |
345 | if ( m_treeCtrl ) | |
346 | { | |
347 | wxTreeItemId idRoot = m_treeCtrl->GetRootItem(); | |
348 | ||
349 | SetStatusText(wxString::Format | |
350 | ( | |
351 | _T("Root/last item is %svisible/%svisible"), | |
352 | m_treeCtrl->IsVisible(idRoot) ? _T("") : _T("not "), | |
353 | m_treeCtrl->IsVisible(m_treeCtrl->GetLastChild(idRoot)) | |
354 | ? _T("") : _T("not ") | |
355 | ), 1); | |
356 | } | |
357 | #endif // wxUSE_STATUSBAR | |
358 | ||
359 | event.Skip(); | |
360 | } | |
361 | ||
362 | void MyFrame::OnSize(wxSizeEvent& event) | |
363 | { | |
364 | if ( m_treeCtrl | |
365 | #if wxUSE_LOG | |
366 | && m_textCtrl | |
367 | #endif // wxUSE_LOG | |
368 | ) | |
369 | { | |
370 | Resize(); | |
371 | } | |
372 | ||
373 | event.Skip(); | |
374 | } | |
375 | ||
376 | void MyFrame::Resize() | |
377 | { | |
378 | wxSize size = GetClientSize(); | |
379 | m_treeCtrl->SetSize(0, 0, size.x, size.y | |
380 | #if !wxUSE_LOG | |
381 | ); | |
382 | #else | |
383 | *2/3); | |
384 | m_textCtrl->SetSize(0, 2*size.y/3, size.x, size.y/3); | |
385 | #endif | |
386 | } | |
387 | ||
388 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
389 | { | |
390 | Close(true); | |
391 | } | |
392 | ||
393 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
394 | { | |
395 | wxMessageBox(wxT("Tree test sample\n") | |
396 | wxT("(c) Julian Smart 1997, Vadim Zeitlin 1998"), | |
397 | wxT("About tree test"), | |
398 | wxOK | wxICON_INFORMATION, this); | |
399 | } | |
400 | ||
401 | void MyFrame::OnRename(wxCommandEvent& WXUNUSED(event)) | |
402 | { | |
403 | wxTreeItemId item = m_treeCtrl->GetSelection(); | |
404 | ||
405 | CHECK_ITEM( item ); | |
406 | ||
407 | // old code - now we edit in place | |
408 | #if 0 | |
409 | static wxString s_text; | |
410 | s_text = wxGetTextFromUser(wxT("New name: "), wxT("Tree sample question"), | |
411 | s_text, this); | |
412 | if ( !s_text.empty() ) | |
413 | { | |
414 | m_treeCtrl->SetItemText(item, s_text); | |
415 | } | |
416 | #endif // 0 | |
417 | ||
418 | // TODO demonstrate creating a custom edit control... | |
419 | (void)m_treeCtrl->EditLabel(item); | |
420 | } | |
421 | ||
422 | void MyFrame::OnCount(wxCommandEvent& WXUNUSED(event)) | |
423 | { | |
424 | wxTreeItemId item = m_treeCtrl->GetSelection(); | |
425 | ||
426 | CHECK_ITEM( item ); | |
427 | ||
428 | int i = m_treeCtrl->GetChildrenCount( item, false ); | |
429 | ||
430 | wxLogMessage(wxT("%d children"), i); | |
431 | } | |
432 | ||
433 | void MyFrame::OnCountRec(wxCommandEvent& WXUNUSED(event)) | |
434 | { | |
435 | wxTreeItemId item = m_treeCtrl->GetSelection(); | |
436 | ||
437 | CHECK_ITEM( item ); | |
438 | ||
439 | int i = m_treeCtrl->GetChildrenCount( item ); | |
440 | ||
441 | wxLogMessage(wxT("%d children"), i); | |
442 | } | |
443 | ||
444 | void MyFrame::DoSort(bool reverse) | |
445 | { | |
446 | wxTreeItemId item = m_treeCtrl->GetSelection(); | |
447 | ||
448 | CHECK_ITEM( item ); | |
449 | ||
450 | m_treeCtrl->DoSortChildren(item, reverse); | |
451 | } | |
452 | ||
453 | void MyFrame::OnDump(wxCommandEvent& WXUNUSED(event)) | |
454 | { | |
455 | wxTreeItemId root = m_treeCtrl->GetSelection(); | |
456 | ||
457 | CHECK_ITEM( root ); | |
458 | ||
459 | m_treeCtrl->GetItemsRecursively(root); | |
460 | } | |
461 | ||
462 | #ifndef NO_MULTIPLE_SELECTION | |
463 | ||
464 | void MyFrame::OnToggleSel(wxCommandEvent& event) | |
465 | { | |
466 | TogStyle(event.GetId(), wxTR_MULTIPLE); | |
467 | } | |
468 | ||
469 | void MyFrame::OnDumpSelected(wxCommandEvent& WXUNUSED(event)) | |
470 | { | |
471 | wxArrayTreeItemIds array; | |
472 | ||
473 | size_t count = m_treeCtrl->GetSelections(array); | |
474 | wxLogMessage(wxT("%u items selected"), count); | |
475 | ||
476 | for ( size_t n = 0; n < count; n++ ) | |
477 | { | |
478 | wxLogMessage(wxT("\t%s"), m_treeCtrl->GetItemText(array.Item(n)).c_str()); | |
479 | } | |
480 | } | |
481 | ||
482 | void MyFrame::OnSelect(wxCommandEvent& WXUNUSED(event)) | |
483 | { | |
484 | m_treeCtrl->SelectItem(m_treeCtrl->GetSelection()); | |
485 | } | |
486 | ||
487 | void MyFrame::OnSelectRoot(wxCommandEvent& WXUNUSED(event)) | |
488 | { | |
489 | m_treeCtrl->SelectItem(m_treeCtrl->GetRootItem()); | |
490 | } | |
491 | ||
492 | void MyFrame::OnUnselect(wxCommandEvent& WXUNUSED(event)) | |
493 | { | |
494 | m_treeCtrl->UnselectAll(); | |
495 | } | |
496 | ||
497 | #endif // NO_MULTIPLE_SELECTION | |
498 | ||
499 | void MyFrame::DoSetBold(bool bold) | |
500 | { | |
501 | wxTreeItemId item = m_treeCtrl->GetSelection(); | |
502 | ||
503 | CHECK_ITEM( item ); | |
504 | ||
505 | m_treeCtrl->SetItemBold(item, bold); | |
506 | } | |
507 | ||
508 | void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event)) | |
509 | { | |
510 | wxTreeItemId item = m_treeCtrl->GetSelection(); | |
511 | ||
512 | CHECK_ITEM( item ); | |
513 | ||
514 | m_treeCtrl->Delete(item); | |
515 | } | |
516 | ||
517 | void MyFrame::OnDeleteChildren(wxCommandEvent& WXUNUSED(event)) | |
518 | { | |
519 | wxTreeItemId item = m_treeCtrl->GetSelection(); | |
520 | ||
521 | CHECK_ITEM( item ); | |
522 | ||
523 | m_treeCtrl->DeleteChildren(item); | |
524 | } | |
525 | ||
526 | void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event)) | |
527 | { | |
528 | m_treeCtrl->DeleteAllItems(); | |
529 | } | |
530 | ||
531 | void MyFrame::OnRecreate(wxCommandEvent& event) | |
532 | { | |
533 | OnDeleteAll(event); | |
534 | m_treeCtrl->AddTestItemsToTree(5, 2); | |
535 | } | |
536 | ||
537 | void MyFrame::OnSetImageSize(wxCommandEvent& WXUNUSED(event)) | |
538 | { | |
539 | int size = wxGetNumberFromUser(wxT("Enter the size for the images to use"), | |
540 | wxT("Size: "), | |
541 | wxT("TreeCtrl sample"), | |
542 | m_treeCtrl->ImageSize()); | |
543 | if ( size == -1 ) | |
544 | return; | |
545 | ||
546 | m_treeCtrl->CreateImageList(size); | |
547 | wxGetApp().SetShowImages(true); | |
548 | } | |
549 | ||
550 | void MyFrame::OnToggleImages(wxCommandEvent& WXUNUSED(event)) | |
551 | { | |
552 | if ( wxGetApp().ShowImages() ) | |
553 | { | |
554 | m_treeCtrl->CreateImageList(-1); | |
555 | wxGetApp().SetShowImages(false); | |
556 | } | |
557 | else | |
558 | { | |
559 | m_treeCtrl->CreateImageList(0); | |
560 | wxGetApp().SetShowImages(true); | |
561 | } | |
562 | } | |
563 | ||
564 | void MyFrame::OnToggleButtons(wxCommandEvent& WXUNUSED(event)) | |
565 | { | |
566 | #if USE_GENERIC_TREECTRL || !defined(__WXMSW__) | |
567 | if ( wxGetApp().ShowButtons() ) | |
568 | { | |
569 | m_treeCtrl->CreateButtonsImageList(-1); | |
570 | wxGetApp().SetShowButtons(false); | |
571 | } | |
572 | else | |
573 | { | |
574 | m_treeCtrl->CreateButtonsImageList(15); | |
575 | wxGetApp().SetShowButtons(true); | |
576 | } | |
577 | #endif | |
578 | } | |
579 | ||
580 | void MyFrame::OnCollapseAndReset(wxCommandEvent& WXUNUSED(event)) | |
581 | { | |
582 | m_treeCtrl->CollapseAndReset(m_treeCtrl->GetRootItem()); | |
583 | } | |
584 | ||
585 | void MyFrame::OnEnsureVisible(wxCommandEvent& WXUNUSED(event)) | |
586 | { | |
587 | m_treeCtrl->DoEnsureVisible(); | |
588 | } | |
589 | ||
590 | void MyFrame::OnInsertItem(wxCommandEvent& WXUNUSED(event)) | |
591 | { | |
592 | int image = wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_File : -1; | |
593 | m_treeCtrl->InsertItem(m_treeCtrl->GetRootItem(), image, wxT("2nd item")); | |
594 | } | |
595 | ||
596 | void MyFrame::OnAddItem(wxCommandEvent& WXUNUSED(event)) | |
597 | { | |
598 | static int s_num = 0; | |
599 | ||
600 | wxString text; | |
601 | text.Printf(wxT("Item #%d"), ++s_num); | |
602 | ||
603 | m_treeCtrl->AppendItem(m_treeCtrl->GetRootItem(), | |
604 | text /*, | |
605 | MyTreeCtrl::TreeCtrlIcon_File */ ); | |
606 | } | |
607 | ||
608 | void MyFrame::OnIncIndent(wxCommandEvent& WXUNUSED(event)) | |
609 | { | |
610 | unsigned int indent = m_treeCtrl->GetIndent(); | |
611 | if (indent < 100) | |
612 | m_treeCtrl->SetIndent( indent+5 ); | |
613 | } | |
614 | ||
615 | void MyFrame::OnDecIndent(wxCommandEvent& WXUNUSED(event)) | |
616 | { | |
617 | unsigned int indent = m_treeCtrl->GetIndent(); | |
618 | if (indent > 10) | |
619 | m_treeCtrl->SetIndent( indent-5 ); | |
620 | } | |
621 | ||
622 | void MyFrame::OnIncSpacing(wxCommandEvent& WXUNUSED(event)) | |
623 | { | |
624 | unsigned int indent = m_treeCtrl->GetSpacing(); | |
625 | if (indent < 100) | |
626 | m_treeCtrl->SetSpacing( indent+5 ); | |
627 | } | |
628 | ||
629 | void MyFrame::OnDecSpacing(wxCommandEvent& WXUNUSED(event)) | |
630 | { | |
631 | unsigned int indent = m_treeCtrl->GetSpacing(); | |
632 | if (indent > 10) | |
633 | m_treeCtrl->SetSpacing( indent-5 ); | |
634 | } | |
635 | ||
636 | void MyFrame::OnToggleIcon(wxCommandEvent& WXUNUSED(event)) | |
637 | { | |
638 | wxTreeItemId item = m_treeCtrl->GetSelection(); | |
639 | ||
640 | CHECK_ITEM( item ); | |
641 | ||
642 | m_treeCtrl->DoToggleIcon(item); | |
643 | } | |
644 | ||
645 | void MyFrame::OnSetFgColour(wxCommandEvent& WXUNUSED(event)) | |
646 | { | |
647 | wxColour col = wxGetColourFromUser(this, m_treeCtrl->GetForegroundColour()); | |
648 | if ( col.Ok() ) | |
649 | m_treeCtrl->SetForegroundColour(col); | |
650 | } | |
651 | ||
652 | void MyFrame::OnSetBgColour(wxCommandEvent& WXUNUSED(event)) | |
653 | { | |
654 | wxColour col = wxGetColourFromUser(this, m_treeCtrl->GetBackgroundColour()); | |
655 | if ( col.Ok() ) | |
656 | m_treeCtrl->SetBackgroundColour(col); | |
657 | } | |
658 | ||
659 | // MyTreeCtrl implementation | |
660 | #if USE_GENERIC_TREECTRL | |
661 | IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl, wxGenericTreeCtrl) | |
662 | #else | |
663 | IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl, wxTreeCtrl) | |
664 | #endif | |
665 | ||
666 | MyTreeCtrl::MyTreeCtrl(wxWindow *parent, const wxWindowID id, | |
667 | const wxPoint& pos, const wxSize& size, | |
668 | long style) | |
669 | : wxTreeCtrl(parent, id, pos, size, style) | |
670 | { | |
671 | m_reverseSort = false; | |
672 | ||
673 | CreateImageList(); | |
674 | ||
675 | // Add some items to the tree | |
676 | AddTestItemsToTree(5, 2); | |
677 | } | |
678 | ||
679 | void MyTreeCtrl::CreateImageList(int size) | |
680 | { | |
681 | if ( size == -1 ) | |
682 | { | |
683 | SetImageList(NULL); | |
684 | return; | |
685 | } | |
686 | if ( size == 0 ) | |
687 | size = m_imageSize; | |
688 | else | |
689 | m_imageSize = size; | |
690 | ||
691 | // Make an image list containing small icons | |
692 | wxImageList *images = new wxImageList(size, size, true); | |
693 | ||
694 | // should correspond to TreeCtrlIcon_xxx enum | |
695 | wxBusyCursor wait; | |
696 | wxIcon icons[5]; | |
697 | icons[0] = wxIcon(icon1_xpm); | |
698 | icons[1] = wxIcon(icon2_xpm); | |
699 | icons[2] = wxIcon(icon3_xpm); | |
700 | icons[3] = wxIcon(icon4_xpm); | |
701 | icons[4] = wxIcon(icon5_xpm); | |
702 | ||
703 | int sizeOrig = icons[0].GetWidth(); | |
704 | for ( size_t i = 0; i < WXSIZEOF(icons); i++ ) | |
705 | { | |
706 | if ( size == sizeOrig ) | |
707 | { | |
708 | images->Add(icons[i]); | |
709 | } | |
710 | else | |
711 | { | |
712 | images->Add(wxBitmap(wxBitmap(icons[i]).ConvertToImage().Rescale(size, size))); | |
713 | } | |
714 | } | |
715 | ||
716 | AssignImageList(images); | |
717 | } | |
718 | ||
719 | #if USE_GENERIC_TREECTRL || !defined(__WXMSW__) | |
720 | void MyTreeCtrl::CreateButtonsImageList(int size) | |
721 | { | |
722 | if ( size == -1 ) | |
723 | { | |
724 | SetButtonsImageList(NULL); | |
725 | return; | |
726 | } | |
727 | ||
728 | // Make an image list containing small icons | |
729 | wxImageList *images = new wxImageList(size, size, true); | |
730 | ||
731 | // should correspond to TreeCtrlIcon_xxx enum | |
732 | wxBusyCursor wait; | |
733 | wxIcon icons[4]; | |
734 | icons[0] = wxIcon(icon3_xpm); // closed | |
735 | icons[1] = wxIcon(icon3_xpm); // closed, selected | |
736 | icons[2] = wxIcon(icon5_xpm); // open | |
737 | icons[3] = wxIcon(icon5_xpm); // open, selected | |
738 | ||
739 | for ( size_t i = 0; i < WXSIZEOF(icons); i++ ) | |
740 | { | |
741 | int sizeOrig = icons[i].GetWidth(); | |
742 | if ( size == sizeOrig ) | |
743 | { | |
744 | images->Add(icons[i]); | |
745 | } | |
746 | else | |
747 | { | |
748 | images->Add(wxBitmap(wxBitmap(icons[i]).ConvertToImage().Rescale(size, size))); | |
749 | } | |
750 | } | |
751 | ||
752 | AssignButtonsImageList(images); | |
753 | #else | |
754 | void MyTreeCtrl::CreateButtonsImageList(int WXUNUSED(size)) | |
755 | { | |
756 | #endif | |
757 | } | |
758 | ||
759 | int MyTreeCtrl::OnCompareItems(const wxTreeItemId& item1, | |
760 | const wxTreeItemId& item2) | |
761 | { | |
762 | if ( m_reverseSort ) | |
763 | { | |
764 | // just exchange 1st and 2nd items | |
765 | return wxTreeCtrl::OnCompareItems(item2, item1); | |
766 | } | |
767 | else | |
768 | { | |
769 | return wxTreeCtrl::OnCompareItems(item1, item2); | |
770 | } | |
771 | } | |
772 | ||
773 | void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId& idParent, | |
774 | size_t numChildren, | |
775 | size_t depth, | |
776 | size_t folder) | |
777 | { | |
778 | if ( depth > 0 ) | |
779 | { | |
780 | bool hasChildren = depth > 1; | |
781 | ||
782 | wxString str; | |
783 | for ( size_t n = 0; n < numChildren; n++ ) | |
784 | { | |
785 | // at depth 1 elements won't have any more children | |
786 | if ( hasChildren ) | |
787 | str.Printf(wxT("%s child %d"), wxT("Folder"), n + 1); | |
788 | else | |
789 | str.Printf(wxT("%s child %d.%d"), wxT("File"), folder, n + 1); | |
790 | ||
791 | // here we pass to AppendItem() normal and selected item images (we | |
792 | // suppose that selected image follows the normal one in the enum) | |
793 | int image, imageSel; | |
794 | if ( wxGetApp().ShowImages() ) | |
795 | { | |
796 | image = depth == 1 ? TreeCtrlIcon_File : TreeCtrlIcon_Folder; | |
797 | imageSel = image + 1; | |
798 | } | |
799 | else | |
800 | { | |
801 | image = imageSel = -1; | |
802 | } | |
803 | wxTreeItemId id = AppendItem(idParent, str, image, imageSel, | |
804 | new MyTreeItemData(str)); | |
805 | ||
806 | // and now we also set the expanded one (only for the folders) | |
807 | if ( hasChildren && wxGetApp().ShowImages() ) | |
808 | { | |
809 | SetItemImage(id, TreeCtrlIcon_FolderOpened, | |
810 | wxTreeItemIcon_Expanded); | |
811 | } | |
812 | ||
813 | // remember the last child for OnEnsureVisible() | |
814 | if ( !hasChildren && n == numChildren - 1 ) | |
815 | { | |
816 | m_lastItem = id; | |
817 | } | |
818 | ||
819 | AddItemsRecursively(id, numChildren, depth - 1, n + 1); | |
820 | } | |
821 | } | |
822 | //else: done! | |
823 | } | |
824 | ||
825 | void MyTreeCtrl::AddTestItemsToTree(size_t numChildren, | |
826 | size_t depth) | |
827 | { | |
828 | int image = wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_Folder : -1; | |
829 | wxTreeItemId rootId = AddRoot(wxT("Root"), | |
830 | image, image, | |
831 | new MyTreeItemData(wxT("Root item"))); | |
832 | if ( image != -1 ) | |
833 | { | |
834 | SetItemImage(rootId, TreeCtrlIcon_FolderOpened, wxTreeItemIcon_Expanded); | |
835 | } | |
836 | ||
837 | AddItemsRecursively(rootId, numChildren, depth, 0); | |
838 | ||
839 | // set some colours/fonts for testing | |
840 | SetItemFont(rootId, *wxITALIC_FONT); | |
841 | ||
842 | wxTreeItemIdValue cookie; | |
843 | wxTreeItemId id = GetFirstChild(rootId, cookie); | |
844 | SetItemTextColour(id, *wxBLUE); | |
845 | ||
846 | id = GetNextChild(rootId, cookie); | |
847 | id = GetNextChild(rootId, cookie); | |
848 | SetItemTextColour(id, *wxRED); | |
849 | SetItemBackgroundColour(id, *wxLIGHT_GREY); | |
850 | } | |
851 | ||
852 | void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId& idParent, | |
853 | wxTreeItemIdValue cookie) | |
854 | { | |
855 | wxTreeItemId id; | |
856 | ||
857 | if ( !cookie ) | |
858 | id = GetFirstChild(idParent, cookie); | |
859 | else | |
860 | id = GetNextChild(idParent, cookie); | |
861 | ||
862 | if ( !id.IsOk() ) | |
863 | return; | |
864 | ||
865 | wxString text = GetItemText(id); | |
866 | wxLogMessage(text); | |
867 | ||
868 | if (ItemHasChildren(id)) | |
869 | GetItemsRecursively(id); | |
870 | ||
871 | GetItemsRecursively(idParent, cookie); | |
872 | } | |
873 | ||
874 | void MyTreeCtrl::DoToggleIcon(const wxTreeItemId& item) | |
875 | { | |
876 | int image = GetItemImage(item) == TreeCtrlIcon_Folder ? TreeCtrlIcon_File | |
877 | : TreeCtrlIcon_Folder; | |
878 | ||
879 | SetItemImage(item, image); | |
880 | } | |
881 | ||
882 | ||
883 | // avoid repetition | |
884 | #define TREE_EVENT_HANDLER(name) \ | |
885 | void MyTreeCtrl::name(wxTreeEvent& event) \ | |
886 | { \ | |
887 | wxLogMessage(wxT(#name)); \ | |
888 | SetLastItem(wxTreeItemId()); \ | |
889 | event.Skip(); \ | |
890 | } | |
891 | ||
892 | TREE_EVENT_HANDLER(OnBeginRDrag) | |
893 | TREE_EVENT_HANDLER(OnDeleteItem) | |
894 | TREE_EVENT_HANDLER(OnGetInfo) | |
895 | TREE_EVENT_HANDLER(OnSetInfo) | |
896 | TREE_EVENT_HANDLER(OnItemExpanded) | |
897 | TREE_EVENT_HANDLER(OnItemExpanding) | |
898 | TREE_EVENT_HANDLER(OnItemCollapsed) | |
899 | TREE_EVENT_HANDLER(OnSelChanged) | |
900 | TREE_EVENT_HANDLER(OnSelChanging) | |
901 | ||
902 | #undef TREE_EVENT_HANDLER | |
903 | ||
904 | void LogKeyEvent(const wxChar *name, const wxKeyEvent& event) | |
905 | { | |
906 | wxString key; | |
907 | long keycode = event.GetKeyCode(); | |
908 | { | |
909 | switch ( keycode ) | |
910 | { | |
911 | case WXK_BACK: key = wxT("BACK"); break; | |
912 | case WXK_TAB: key = wxT("TAB"); break; | |
913 | case WXK_RETURN: key = wxT("RETURN"); break; | |
914 | case WXK_ESCAPE: key = wxT("ESCAPE"); break; | |
915 | case WXK_SPACE: key = wxT("SPACE"); break; | |
916 | case WXK_DELETE: key = wxT("DELETE"); break; | |
917 | case WXK_START: key = wxT("START"); break; | |
918 | case WXK_LBUTTON: key = wxT("LBUTTON"); break; | |
919 | case WXK_RBUTTON: key = wxT("RBUTTON"); break; | |
920 | case WXK_CANCEL: key = wxT("CANCEL"); break; | |
921 | case WXK_MBUTTON: key = wxT("MBUTTON"); break; | |
922 | case WXK_CLEAR: key = wxT("CLEAR"); break; | |
923 | case WXK_SHIFT: key = wxT("SHIFT"); break; | |
924 | case WXK_ALT: key = wxT("ALT"); break; | |
925 | case WXK_CONTROL: key = wxT("CONTROL"); break; | |
926 | case WXK_MENU: key = wxT("MENU"); break; | |
927 | case WXK_PAUSE: key = wxT("PAUSE"); break; | |
928 | case WXK_CAPITAL: key = wxT("CAPITAL"); break; | |
929 | case WXK_PRIOR: key = wxT("PRIOR"); break; | |
930 | case WXK_NEXT: key = wxT("NEXT"); break; | |
931 | case WXK_END: key = wxT("END"); break; | |
932 | case WXK_HOME: key = wxT("HOME"); break; | |
933 | case WXK_LEFT: key = wxT("LEFT"); break; | |
934 | case WXK_UP: key = wxT("UP"); break; | |
935 | case WXK_RIGHT: key = wxT("RIGHT"); break; | |
936 | case WXK_DOWN: key = wxT("DOWN"); break; | |
937 | case WXK_SELECT: key = wxT("SELECT"); break; | |
938 | case WXK_PRINT: key = wxT("PRINT"); break; | |
939 | case WXK_EXECUTE: key = wxT("EXECUTE"); break; | |
940 | case WXK_SNAPSHOT: key = wxT("SNAPSHOT"); break; | |
941 | case WXK_INSERT: key = wxT("INSERT"); break; | |
942 | case WXK_HELP: key = wxT("HELP"); break; | |
943 | case WXK_NUMPAD0: key = wxT("NUMPAD0"); break; | |
944 | case WXK_NUMPAD1: key = wxT("NUMPAD1"); break; | |
945 | case WXK_NUMPAD2: key = wxT("NUMPAD2"); break; | |
946 | case WXK_NUMPAD3: key = wxT("NUMPAD3"); break; | |
947 | case WXK_NUMPAD4: key = wxT("NUMPAD4"); break; | |
948 | case WXK_NUMPAD5: key = wxT("NUMPAD5"); break; | |
949 | case WXK_NUMPAD6: key = wxT("NUMPAD6"); break; | |
950 | case WXK_NUMPAD7: key = wxT("NUMPAD7"); break; | |
951 | case WXK_NUMPAD8: key = wxT("NUMPAD8"); break; | |
952 | case WXK_NUMPAD9: key = wxT("NUMPAD9"); break; | |
953 | case WXK_MULTIPLY: key = wxT("MULTIPLY"); break; | |
954 | case WXK_ADD: key = wxT("ADD"); break; | |
955 | case WXK_SEPARATOR: key = wxT("SEPARATOR"); break; | |
956 | case WXK_SUBTRACT: key = wxT("SUBTRACT"); break; | |
957 | case WXK_DECIMAL: key = wxT("DECIMAL"); break; | |
958 | case WXK_DIVIDE: key = wxT("DIVIDE"); break; | |
959 | case WXK_F1: key = wxT("F1"); break; | |
960 | case WXK_F2: key = wxT("F2"); break; | |
961 | case WXK_F3: key = wxT("F3"); break; | |
962 | case WXK_F4: key = wxT("F4"); break; | |
963 | case WXK_F5: key = wxT("F5"); break; | |
964 | case WXK_F6: key = wxT("F6"); break; | |
965 | case WXK_F7: key = wxT("F7"); break; | |
966 | case WXK_F8: key = wxT("F8"); break; | |
967 | case WXK_F9: key = wxT("F9"); break; | |
968 | case WXK_F10: key = wxT("F10"); break; | |
969 | case WXK_F11: key = wxT("F11"); break; | |
970 | case WXK_F12: key = wxT("F12"); break; | |
971 | case WXK_F13: key = wxT("F13"); break; | |
972 | case WXK_F14: key = wxT("F14"); break; | |
973 | case WXK_F15: key = wxT("F15"); break; | |
974 | case WXK_F16: key = wxT("F16"); break; | |
975 | case WXK_F17: key = wxT("F17"); break; | |
976 | case WXK_F18: key = wxT("F18"); break; | |
977 | case WXK_F19: key = wxT("F19"); break; | |
978 | case WXK_F20: key = wxT("F20"); break; | |
979 | case WXK_F21: key = wxT("F21"); break; | |
980 | case WXK_F22: key = wxT("F22"); break; | |
981 | case WXK_F23: key = wxT("F23"); break; | |
982 | case WXK_F24: key = wxT("F24"); break; | |
983 | case WXK_NUMLOCK: key = wxT("NUMLOCK"); break; | |
984 | case WXK_SCROLL: key = wxT("SCROLL"); break; | |
985 | case WXK_PAGEUP: key = wxT("PAGEUP"); break; | |
986 | case WXK_PAGEDOWN: key = wxT("PAGEDOWN"); break; | |
987 | case WXK_NUMPAD_SPACE: key = wxT("NUMPAD_SPACE"); break; | |
988 | case WXK_NUMPAD_TAB: key = wxT("NUMPAD_TAB"); break; | |
989 | case WXK_NUMPAD_ENTER: key = wxT("NUMPAD_ENTER"); break; | |
990 | case WXK_NUMPAD_F1: key = wxT("NUMPAD_F1"); break; | |
991 | case WXK_NUMPAD_F2: key = wxT("NUMPAD_F2"); break; | |
992 | case WXK_NUMPAD_F3: key = wxT("NUMPAD_F3"); break; | |
993 | case WXK_NUMPAD_F4: key = wxT("NUMPAD_F4"); break; | |
994 | case WXK_NUMPAD_HOME: key = wxT("NUMPAD_HOME"); break; | |
995 | case WXK_NUMPAD_LEFT: key = wxT("NUMPAD_LEFT"); break; | |
996 | case WXK_NUMPAD_UP: key = wxT("NUMPAD_UP"); break; | |
997 | case WXK_NUMPAD_RIGHT: key = wxT("NUMPAD_RIGHT"); break; | |
998 | case WXK_NUMPAD_DOWN: key = wxT("NUMPAD_DOWN"); break; | |
999 | case WXK_NUMPAD_PRIOR: key = wxT("NUMPAD_PRIOR"); break; | |
1000 | case WXK_NUMPAD_PAGEUP: key = wxT("NUMPAD_PAGEUP"); break; | |
1001 | case WXK_NUMPAD_PAGEDOWN: key = wxT("NUMPAD_PAGEDOWN"); break; | |
1002 | case WXK_NUMPAD_END: key = wxT("NUMPAD_END"); break; | |
1003 | case WXK_NUMPAD_BEGIN: key = wxT("NUMPAD_BEGIN"); break; | |
1004 | case WXK_NUMPAD_INSERT: key = wxT("NUMPAD_INSERT"); break; | |
1005 | case WXK_NUMPAD_DELETE: key = wxT("NUMPAD_DELETE"); break; | |
1006 | case WXK_NUMPAD_EQUAL: key = wxT("NUMPAD_EQUAL"); break; | |
1007 | case WXK_NUMPAD_MULTIPLY: key = wxT("NUMPAD_MULTIPLY"); break; | |
1008 | case WXK_NUMPAD_ADD: key = wxT("NUMPAD_ADD"); break; | |
1009 | case WXK_NUMPAD_SEPARATOR: key = wxT("NUMPAD_SEPARATOR"); break; | |
1010 | case WXK_NUMPAD_SUBTRACT: key = wxT("NUMPAD_SUBTRACT"); break; | |
1011 | case WXK_NUMPAD_DECIMAL: key = wxT("NUMPAD_DECIMAL"); break; | |
1012 | ||
1013 | default: | |
1014 | { | |
1015 | if ( wxIsprint((int)keycode) ) | |
1016 | key.Printf(wxT("'%c'"), (char)keycode); | |
1017 | else if ( keycode > 0 && keycode < 27 ) | |
1018 | key.Printf(_("Ctrl-%c"), wxT('A') + keycode - 1); | |
1019 | else | |
1020 | key.Printf(wxT("unknown (%ld)"), keycode); | |
1021 | } | |
1022 | } | |
1023 | } | |
1024 | ||
1025 | wxLogMessage( wxT("%s event: %s (flags = %c%c%c%c)"), | |
1026 | name, | |
1027 | key.c_str(), | |
1028 | event.ControlDown() ? wxT('C') : wxT('-'), | |
1029 | event.AltDown() ? wxT('A') : wxT('-'), | |
1030 | event.ShiftDown() ? wxT('S') : wxT('-'), | |
1031 | event.MetaDown() ? wxT('M') : wxT('-')); | |
1032 | } | |
1033 | ||
1034 | void MyTreeCtrl::OnTreeKeyDown(wxTreeEvent& event) | |
1035 | { | |
1036 | LogKeyEvent(wxT("Tree key down "), event.GetKeyEvent()); | |
1037 | ||
1038 | event.Skip(); | |
1039 | } | |
1040 | ||
1041 | void MyTreeCtrl::OnBeginDrag(wxTreeEvent& event) | |
1042 | { | |
1043 | // need to explicitly allow drag | |
1044 | if ( event.GetItem() != GetRootItem() ) | |
1045 | { | |
1046 | m_draggedItem = event.GetItem(); | |
1047 | ||
1048 | wxLogMessage(wxT("OnBeginDrag: started dragging %s"), | |
1049 | GetItemText(m_draggedItem).c_str()); | |
1050 | ||
1051 | event.Allow(); | |
1052 | } | |
1053 | else | |
1054 | { | |
1055 | wxLogMessage(wxT("OnBeginDrag: this item can't be dragged.")); | |
1056 | } | |
1057 | } | |
1058 | ||
1059 | void MyTreeCtrl::OnEndDrag(wxTreeEvent& event) | |
1060 | { | |
1061 | wxTreeItemId itemSrc = m_draggedItem, | |
1062 | itemDst = event.GetItem(); | |
1063 | m_draggedItem = (wxTreeItemId)0l; | |
1064 | ||
1065 | // where to copy the item? | |
1066 | if ( itemDst.IsOk() && !ItemHasChildren(itemDst) ) | |
1067 | { | |
1068 | // copy to the parent then | |
1069 | itemDst = GetItemParent(itemDst); | |
1070 | } | |
1071 | ||
1072 | if ( !itemDst.IsOk() ) | |
1073 | { | |
1074 | wxLogMessage(wxT("OnEndDrag: can't drop here.")); | |
1075 | ||
1076 | return; | |
1077 | } | |
1078 | ||
1079 | wxString text = GetItemText(itemSrc); | |
1080 | wxLogMessage(wxT("OnEndDrag: '%s' copied to '%s'."), | |
1081 | text.c_str(), GetItemText(itemDst).c_str()); | |
1082 | ||
1083 | // just do append here - we could also insert it just before/after the item | |
1084 | // on which it was dropped, but this requires slightly more work... we also | |
1085 | // completely ignore the client data and icon of the old item but could | |
1086 | // copy them as well. | |
1087 | // | |
1088 | // Finally, we only copy one item here but we might copy the entire tree if | |
1089 | // we were dragging a folder. | |
1090 | int image = wxGetApp().ShowImages() ? TreeCtrlIcon_File : -1; | |
1091 | AppendItem(itemDst, text, image); | |
1092 | } | |
1093 | ||
1094 | void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent& event) | |
1095 | { | |
1096 | wxLogMessage(wxT("OnBeginLabelEdit")); | |
1097 | ||
1098 | // for testing, prevent this item's label editing | |
1099 | wxTreeItemId itemId = event.GetItem(); | |
1100 | if ( IsTestItem(itemId) ) | |
1101 | { | |
1102 | wxMessageBox(wxT("You can't edit this item.")); | |
1103 | ||
1104 | event.Veto(); | |
1105 | } | |
1106 | } | |
1107 | ||
1108 | void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent& event) | |
1109 | { | |
1110 | wxLogMessage(wxT("OnEndLabelEdit")); | |
1111 | ||
1112 | // don't allow anything except letters in the labels | |
1113 | if ( !event.GetLabel().IsWord() ) | |
1114 | { | |
1115 | wxMessageBox(wxT("The new label should be a single word.")); | |
1116 | ||
1117 | event.Veto(); | |
1118 | } | |
1119 | } | |
1120 | ||
1121 | void MyTreeCtrl::OnItemCollapsing(wxTreeEvent& event) | |
1122 | { | |
1123 | wxLogMessage(wxT("OnItemCollapsing")); | |
1124 | ||
1125 | // for testing, prevent the user from collapsing the first child folder | |
1126 | wxTreeItemId itemId = event.GetItem(); | |
1127 | if ( IsTestItem(itemId) ) | |
1128 | { | |
1129 | wxMessageBox(wxT("You can't collapse this item.")); | |
1130 | ||
1131 | event.Veto(); | |
1132 | } | |
1133 | } | |
1134 | ||
1135 | void MyTreeCtrl::OnItemActivated(wxTreeEvent& event) | |
1136 | { | |
1137 | // show some info about this item | |
1138 | wxTreeItemId itemId = event.GetItem(); | |
1139 | MyTreeItemData *item = (MyTreeItemData *)GetItemData(itemId); | |
1140 | ||
1141 | if ( item != NULL ) | |
1142 | { | |
1143 | item->ShowInfo(this); | |
1144 | } | |
1145 | ||
1146 | wxLogMessage(wxT("OnItemActivated")); | |
1147 | } | |
1148 | ||
1149 | void MyTreeCtrl::OnContextMenu(wxContextMenuEvent& event) | |
1150 | { | |
1151 | wxPoint pt = event.GetPosition(); | |
1152 | wxTreeItemId item = GetSelection(); | |
1153 | wxLogMessage(wxT("OnContextMenu at screen coords (%i, %i)"), pt.x, pt.y); | |
1154 | ||
1155 | //check if event was generated by keyboard (MSW-specific?) | |
1156 | if (pt.x==-1 && pt.y==-1) //(this is how MSW indicates it) | |
1157 | { | |
1158 | //attempt to guess where to show the menu | |
1159 | if (item.IsOk()) | |
1160 | { | |
1161 | //if an item was clicked, show menu to the right of it | |
1162 | wxRect rect; | |
1163 | GetBoundingRect(item, rect, true); //true = only the label | |
1164 | pt = wxPoint(rect.GetRight(), rect.GetTop()); | |
1165 | } | |
1166 | else | |
1167 | { | |
1168 | pt = wxPoint(0, 0); | |
1169 | } | |
1170 | } | |
1171 | else | |
1172 | { | |
1173 | //event was generated by mouse, use supplied coords | |
1174 | pt = ScreenToClient(pt); | |
1175 | } | |
1176 | ||
1177 | ShowMenu(item, pt); | |
1178 | } | |
1179 | ||
1180 | void MyTreeCtrl::ShowMenu(wxTreeItemId id, const wxPoint& pt) | |
1181 | { | |
1182 | wxString title; | |
1183 | if ( id.IsOk() ) | |
1184 | { | |
1185 | title << wxT("Menu for ") << GetItemText(id); | |
1186 | } | |
1187 | else | |
1188 | { | |
1189 | title = wxT("Menu for no particular item"); | |
1190 | } | |
1191 | ||
1192 | #if wxUSE_MENUS | |
1193 | wxMenu menu(title); | |
1194 | menu.Append(TreeTest_About, wxT("&About...")); | |
1195 | menu.Append(TreeTest_Dump, wxT("&Dump")); | |
1196 | ||
1197 | PopupMenu(&menu, pt); | |
1198 | #endif // wxUSE_MENUS | |
1199 | } | |
1200 | ||
1201 | void MyTreeCtrl::OnRMouseDClick(wxMouseEvent& event) | |
1202 | { | |
1203 | wxTreeItemId id = HitTest(event.GetPosition()); | |
1204 | if ( !id ) | |
1205 | wxLogMessage(wxT("No item under mouse")); | |
1206 | else | |
1207 | { | |
1208 | MyTreeItemData *item = (MyTreeItemData *)GetItemData(id); | |
1209 | if ( item ) | |
1210 | wxLogMessage(wxT("Item '%s' under mouse"), item->GetDesc()); | |
1211 | } | |
1212 | } | |
1213 | ||
1214 | static inline const wxChar *Bool2String(bool b) | |
1215 | { | |
1216 | return b ? wxT("") : wxT("not "); | |
1217 | } | |
1218 | ||
1219 | void MyTreeItemData::ShowInfo(wxTreeCtrl *tree) | |
1220 | { | |
1221 | wxLogMessage(wxT("Item '%s': %sselected, %sexpanded, %sbold,\n") | |
1222 | wxT("%u children (%u immediately under this item)."), | |
1223 | m_desc.c_str(), | |
1224 | Bool2String(tree->IsSelected(GetId())), | |
1225 | Bool2String(tree->IsExpanded(GetId())), | |
1226 | Bool2String(tree->IsBold(GetId())), | |
1227 | tree->GetChildrenCount(GetId()), | |
1228 | tree->GetChildrenCount(GetId(), false)); | |
1229 | } |