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