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