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