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