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