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