]>
Commit | Line | Data |
---|---|---|
457814b5 JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: treetest.cpp | |
3 | // Purpose: wxTreeCtrl sample | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
3a5a2f56 | 9 | // Licence: wxWindows license |
457814b5 JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifdef __GNUG__ | |
3a5a2f56 VZ |
13 | #pragma implementation |
14 | #pragma interface | |
457814b5 JS |
15 | #endif |
16 | ||
17 | // For compilers that support precompilation, includes "wx/wx.h". | |
18 | #include "wx/wxprec.h" | |
19 | ||
20 | #ifdef __BORLANDC__ | |
3a5a2f56 | 21 | #pragma hdrstop |
457814b5 JS |
22 | #endif |
23 | ||
24 | #ifndef WX_PRECOMP | |
3a5a2f56 | 25 | #include "wx/wx.h" |
457814b5 JS |
26 | #endif |
27 | ||
3a5a2f56 VZ |
28 | #include "wx/log.h" |
29 | ||
91b8de8d | 30 | #include "wx/image.h" |
3a5a2f56 | 31 | #include "wx/imaglist.h" |
457814b5 JS |
32 | #include "wx/treectrl.h" |
33 | ||
cf724bce RR |
34 | #include "math.h" |
35 | ||
89124b1a RR |
36 | //#ifdef __WXMSW__ |
37 | #define NO_MULTIPLE_SELECTION | |
9dfbf520 | 38 | #define NO_VARIABLE_HEIGHT |
89124b1a | 39 | //#endif |
91b8de8d | 40 | |
9dfbf520 VZ |
41 | #include "treetest.h" |
42 | ||
91b8de8d RR |
43 | // under Windows the icons are in the .rc file |
44 | #ifndef __WXMSW__ | |
9ce29a28 | 45 | #ifdef NO_VARIABLE_HEIGHT |
91b8de8d | 46 | #include "icon1.xpm" |
9ce29a28 | 47 | #endif |
91b8de8d RR |
48 | #include "icon2.xpm" |
49 | #include "mondrian.xpm" | |
50 | #endif | |
51 | ||
e1ee62bd VZ |
52 | // verify that the item is ok and insult the user if it is not |
53 | #define CHECK_ITEM( item ) if ( !item.IsOk() ) { \ | |
54 | wxMessageBox("Please select some item first!", \ | |
55 | "Tree sample error", \ | |
56 | wxOK | wxICON_EXCLAMATION, \ | |
57 | this); \ | |
58 | return; \ | |
5f939e78 VZ |
59 | } |
60 | ||
457814b5 | 61 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) |
5f939e78 VZ |
62 | EVT_MENU(TreeTest_Quit, MyFrame::OnQuit) |
63 | EVT_MENU(TreeTest_About, MyFrame::OnAbout) | |
64 | EVT_MENU(TreeTest_Dump, MyFrame::OnDump) | |
9dfbf520 VZ |
65 | #ifndef NO_MULTIPLE_SELECTION |
66 | EVT_MENU(TreeTest_DumpSelected, MyFrame::OnDumpSelected) | |
67 | EVT_MENU(TreeTest_Select, MyFrame::OnSelect) | |
68 | EVT_MENU(TreeTest_Unselect, MyFrame::OnUnselect) | |
69 | #endif // NO_MULTIPLE_SELECTION | |
5f939e78 VZ |
70 | EVT_MENU(TreeTest_Rename, MyFrame::OnRename) |
71 | EVT_MENU(TreeTest_Sort, MyFrame::OnSort) | |
72 | EVT_MENU(TreeTest_SortRev, MyFrame::OnSortRev) | |
73 | EVT_MENU(TreeTest_Bold, MyFrame::OnSetBold) | |
74 | EVT_MENU(TreeTest_UnBold, MyFrame::OnClearBold) | |
75 | EVT_MENU(TreeTest_Delete, MyFrame::OnDelete) | |
76 | EVT_MENU(TreeTest_DeleteChildren, MyFrame::OnDeleteChildren) | |
77 | EVT_MENU(TreeTest_DeleteAll, MyFrame::OnDeleteAll) | |
78 | EVT_MENU(TreeTest_Recreate, MyFrame::OnRecreate) | |
79 | EVT_MENU(TreeTest_CollapseAndReset, MyFrame::OnCollapseAndReset) | |
80 | EVT_MENU(TreeTest_EnsureVisible, MyFrame::OnEnsureVisible) | |
81 | EVT_MENU(TreeTest_AddItem, MyFrame::OnAddItem) | |
cf724bce RR |
82 | EVT_MENU(TreeTest_IncIndent, MyFrame::OnIncIndent) |
83 | EVT_MENU(TreeTest_DecIndent, MyFrame::OnDecIndent) | |
84 | EVT_MENU(TreeTest_IncSpacing, MyFrame::OnIncSpacing) | |
85 | EVT_MENU(TreeTest_DecSpacing, MyFrame::OnDecSpacing) | |
9dfbf520 | 86 | EVT_MENU(TreeTest_ToggleIcon, MyFrame::OnToggleIcon) |
457814b5 JS |
87 | END_EVENT_TABLE() |
88 | ||
89 | BEGIN_EVENT_TABLE(MyTreeCtrl, wxTreeCtrl) | |
5f939e78 VZ |
90 | EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginDrag) |
91 | EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginRDrag) | |
92 | EVT_TREE_BEGIN_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnBeginLabelEdit) | |
93 | EVT_TREE_END_LABEL_EDIT(TreeTest_Ctrl, MyTreeCtrl::OnEndLabelEdit) | |
94 | EVT_TREE_DELETE_ITEM(TreeTest_Ctrl, MyTreeCtrl::OnDeleteItem) | |
95 | EVT_TREE_GET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnGetInfo) | |
96 | EVT_TREE_SET_INFO(TreeTest_Ctrl, MyTreeCtrl::OnSetInfo) | |
97 | EVT_TREE_ITEM_EXPANDED(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanded) | |
98 | EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanding) | |
99 | EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsed) | |
100 | EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsing) | |
101 | EVT_TREE_SEL_CHANGED(TreeTest_Ctrl, MyTreeCtrl::OnSelChanged) | |
102 | EVT_TREE_SEL_CHANGING(TreeTest_Ctrl, MyTreeCtrl::OnSelChanging) | |
103 | EVT_TREE_KEY_DOWN(TreeTest_Ctrl, MyTreeCtrl::OnTreeKeyDown) | |
104 | EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl, MyTreeCtrl::OnItemActivated) | |
105 | EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick) | |
457814b5 JS |
106 | END_EVENT_TABLE() |
107 | ||
108 | IMPLEMENT_APP(MyApp) | |
109 | ||
c89a6106 | 110 | bool MyApp::OnInit() |
457814b5 | 111 | { |
5f939e78 VZ |
112 | // Create the main frame window |
113 | MyFrame *frame = new MyFrame("wxTreeCtrl Test", 50, 50, 450, 340); | |
457814b5 | 114 | |
5f939e78 VZ |
115 | // Show the frame |
116 | frame->Show(TRUE); | |
117 | SetTopWindow(frame); | |
457814b5 | 118 | |
5f939e78 | 119 | return TRUE; |
3a5a2f56 | 120 | } |
457814b5 | 121 | |
457814b5 | 122 | |
3a5a2f56 VZ |
123 | // My frame constructor |
124 | MyFrame::MyFrame(const wxString& title, int x, int y, int w, int h) | |
125 | : wxFrame((wxFrame *)NULL, -1, title, wxPoint(x, y), wxSize(w, h)) | |
126 | { | |
5f939e78 VZ |
127 | // This reduces flicker effects - even better would be to define |
128 | // OnEraseBackground to do nothing. When the tree control's scrollbars are | |
129 | // show or hidden, the frame is sent a background erase event. | |
130 | SetBackgroundColour(wxColour(255, 255, 255)); | |
131 | ||
132 | // Give it an icon | |
133 | SetIcon(wxICON(mondrian)); | |
134 | ||
135 | // Make a menubar | |
136 | wxMenu *file_menu = new wxMenu, | |
137 | *tree_menu = new wxMenu, | |
138 | *item_menu = new wxMenu; | |
139 | ||
140 | file_menu->Append(TreeTest_About, "&About..."); | |
141 | file_menu->AppendSeparator(); | |
142 | file_menu->Append(TreeTest_Quit, "E&xit"); | |
143 | ||
144 | tree_menu->Append(TreeTest_Recreate, "&Recreate the tree"); | |
145 | tree_menu->Append(TreeTest_CollapseAndReset, "C&ollapse and reset"); | |
146 | tree_menu->AppendSeparator(); | |
147 | tree_menu->Append(TreeTest_AddItem, "Append a &new item"); | |
148 | tree_menu->Append(TreeTest_Delete, "&Delete this item"); | |
149 | tree_menu->Append(TreeTest_DeleteChildren, "Delete &children"); | |
150 | tree_menu->Append(TreeTest_DeleteAll, "Delete &all items"); | |
151 | tree_menu->AppendSeparator(); | |
152 | tree_menu->Append(TreeTest_Sort, "Sort children of current item"); | |
153 | tree_menu->Append(TreeTest_SortRev, "Sort in reversed order"); | |
154 | tree_menu->AppendSeparator(); | |
155 | tree_menu->Append(TreeTest_EnsureVisible, "Make the last item &visible"); | |
cf724bce RR |
156 | tree_menu->AppendSeparator(); |
157 | tree_menu->Append(TreeTest_IncIndent, "Add 5 points to indentation\tAlt-I"); | |
158 | tree_menu->Append(TreeTest_DecIndent, "Reduce indentation by 5 points\tAlt-R"); | |
159 | tree_menu->AppendSeparator(); | |
160 | tree_menu->Append(TreeTest_IncSpacing, "Add 5 points to spacing\tCtrl-I"); | |
161 | tree_menu->Append(TreeTest_DecSpacing, "Reduce spacing by 5 points\tCtrl-R"); | |
5f939e78 | 162 | |
91b8de8d | 163 | item_menu->Append(TreeTest_Dump, "&Dump item children"); |
5f939e78 | 164 | item_menu->Append(TreeTest_Rename, "&Rename item..."); |
91b8de8d | 165 | |
5f939e78 VZ |
166 | item_menu->AppendSeparator(); |
167 | item_menu->Append(TreeTest_Bold, "Make item &bold"); | |
168 | item_menu->Append(TreeTest_UnBold, "Make item ¬ bold"); | |
9dfbf520 VZ |
169 | item_menu->AppendSeparator(); |
170 | item_menu->Append(TreeTest_ToggleIcon, "Toggle the items &icon"); | |
171 | ||
172 | #ifndef NO_MULTIPLE_SELECTION | |
173 | item_menu->AppendSeparator(); | |
174 | item_menu->Append(TreeTest_DumpSelected, "Dump selected items\tAlt-D"); | |
175 | item_menu->Append(TreeTest_Select, "Select current item\tAlt-S"); | |
176 | item_menu->Append(TreeTest_Unselect, "Unselect everything\tAlt-U"); | |
177 | #endif | |
5f939e78 VZ |
178 | |
179 | wxMenuBar *menu_bar = new wxMenuBar; | |
180 | menu_bar->Append(file_menu, "&File"); | |
181 | menu_bar->Append(tree_menu, "&Tree"); | |
182 | menu_bar->Append(item_menu, "&Item"); | |
183 | SetMenuBar(menu_bar); | |
184 | ||
185 | m_treeCtrl = new MyTreeCtrl(this, TreeTest_Ctrl, | |
186 | wxDefaultPosition, wxDefaultSize, | |
5ea47806 VZ |
187 | wxTR_HAS_BUTTONS | |
188 | wxTR_EDIT_LABELS | | |
89124b1a RR |
189 | #ifndef NO_MULTIPLE_SELECTION |
190 | wxTR_MULTIPLE | | |
191 | #endif | |
192 | #ifndef NO_VARIABLE_HEIGHT | |
193 | wxTR_HAS_VARIABLE_ROW_HEIGHT | | |
194 | #endif | |
195 | wxSUNKEN_BORDER); | |
5f939e78 VZ |
196 | wxTextCtrl *textCtrl = new wxTextCtrl(this, -1, "", |
197 | wxDefaultPosition, wxDefaultSize, | |
198 | wxTE_MULTILINE | wxSUNKEN_BORDER); | |
199 | ||
200 | wxLayoutConstraints *c = new wxLayoutConstraints; | |
201 | c->top.SameAs(this, wxTop); | |
202 | c->left.SameAs(this, wxLeft); | |
203 | c->right.SameAs(this, wxRight); | |
204 | c->height.PercentOf(this, wxHeight, 66); | |
205 | m_treeCtrl->SetConstraints(c); | |
206 | ||
207 | c = new wxLayoutConstraints; | |
208 | c->top.Below(m_treeCtrl); | |
209 | c->left.SameAs(this, wxLeft); | |
210 | c->right.SameAs(this, wxRight); | |
211 | c->bottom.SameAs(this, wxBottom); | |
212 | textCtrl->SetConstraints(c); | |
213 | SetAutoLayout(TRUE); | |
214 | ||
215 | // create a status bar with 3 panes | |
216 | CreateStatusBar(3); | |
217 | SetStatusText("", 0); | |
3a5a2f56 | 218 | |
a367b9b3 | 219 | #ifdef __WXMOTIF__ |
5f939e78 VZ |
220 | // For some reason, we get a memcpy crash in wxLogStream::DoLogStream |
221 | // on gcc/wxMotif, if we use wxLogTextCtl. Maybe it's just gcc? | |
222 | delete wxLog::SetActiveTarget(new wxLogStderr); | |
a367b9b3 | 223 | #else |
5f939e78 VZ |
224 | // set our text control as the log target |
225 | wxLogTextCtrl *logWindow = new wxLogTextCtrl(textCtrl); | |
226 | delete wxLog::SetActiveTarget(logWindow); | |
a367b9b3 | 227 | #endif |
457814b5 JS |
228 | } |
229 | ||
c89a6106 | 230 | MyFrame::~MyFrame() |
457814b5 | 231 | { |
5f939e78 | 232 | delete wxLog::SetActiveTarget(NULL); |
457814b5 JS |
233 | } |
234 | ||
3a5a2f56 | 235 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
457814b5 | 236 | { |
5f939e78 | 237 | Close(TRUE); |
457814b5 JS |
238 | } |
239 | ||
3a5a2f56 | 240 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) |
457814b5 | 241 | { |
5f939e78 | 242 | wxMessageBox("Tree test sample\n" |
5ea47806 | 243 | "(c) Julian Smart 1997, Vadim Zeitlin 1998", |
5f939e78 VZ |
244 | "About tree test", |
245 | wxOK | wxICON_INFORMATION, this); | |
e1ee62bd VZ |
246 | } |
247 | ||
248 | void MyFrame::OnRename(wxCommandEvent& WXUNUSED(event)) | |
249 | { | |
5f939e78 | 250 | wxTreeItemId item = m_treeCtrl->GetSelection(); |
e1ee62bd | 251 | |
5f939e78 | 252 | CHECK_ITEM( item ); |
e1ee62bd | 253 | |
5ea47806 VZ |
254 | // old code - now we edit in place |
255 | #if 0 | |
5f939e78 VZ |
256 | static wxString s_text; |
257 | s_text = wxGetTextFromUser("New name: ", "Tree sample question", | |
258 | s_text, this); | |
259 | if ( !s_text.IsEmpty() ) | |
260 | { | |
261 | m_treeCtrl->SetItemText(item, s_text); | |
262 | } | |
5ea47806 VZ |
263 | #endif // 0 |
264 | ||
265 | // TODO demonstrate creating a custom edit control... | |
266 | (void)m_treeCtrl->EditLabel(item); | |
e1ee62bd VZ |
267 | } |
268 | ||
269 | void MyFrame::DoSort(bool reverse) | |
270 | { | |
5f939e78 | 271 | wxTreeItemId item = m_treeCtrl->GetSelection(); |
e1ee62bd | 272 | |
5f939e78 | 273 | CHECK_ITEM( item ); |
457814b5 | 274 | |
5f939e78 | 275 | m_treeCtrl->DoSortChildren(item, reverse); |
457814b5 JS |
276 | } |
277 | ||
4832f7c0 VZ |
278 | void MyFrame::OnDump(wxCommandEvent& WXUNUSED(event)) |
279 | { | |
5f939e78 | 280 | wxTreeItemId root = m_treeCtrl->GetSelection(); |
e1ee62bd | 281 | |
5f939e78 | 282 | CHECK_ITEM( root ); |
e1ee62bd | 283 | |
5f939e78 | 284 | m_treeCtrl->GetItemsRecursively(root, -1); |
4832f7c0 VZ |
285 | } |
286 | ||
9dfbf520 VZ |
287 | #ifndef NO_MULTIPLE_SELECTION |
288 | ||
91b8de8d RR |
289 | void MyFrame::OnDumpSelected(wxCommandEvent& WXUNUSED(event)) |
290 | { | |
9dfbf520 | 291 | wxArrayTreeItemIds array; |
91b8de8d | 292 | |
9dfbf520 VZ |
293 | size_t count = m_treeCtrl->GetSelections(array); |
294 | wxLogMessage(_T("%u items selected"), count); | |
91b8de8d | 295 | |
9dfbf520 VZ |
296 | for ( size_t n = 0; n < count; n++ ) |
297 | { | |
298 | wxLogMessage("\t%s", m_treeCtrl->GetItemText(array.Item(n)).c_str()); | |
299 | } | |
300 | } | |
301 | ||
302 | void MyFrame::OnSelect(wxCommandEvent& event) | |
303 | { | |
304 | m_treeCtrl->SelectItem(m_treeCtrl->GetSelection()); | |
305 | } | |
306 | ||
307 | void MyFrame::OnUnselect(wxCommandEvent& event) | |
308 | { | |
309 | m_treeCtrl->UnselectAll(); | |
91b8de8d RR |
310 | } |
311 | ||
9dfbf520 VZ |
312 | #endif // NO_MULTIPLE_SELECTION |
313 | ||
add28c55 VZ |
314 | void MyFrame::DoSetBold(bool bold) |
315 | { | |
5f939e78 | 316 | wxTreeItemId item = m_treeCtrl->GetSelection(); |
e1ee62bd | 317 | |
5f939e78 | 318 | CHECK_ITEM( item ); |
e1ee62bd | 319 | |
5f939e78 | 320 | m_treeCtrl->SetItemBold(item, bold); |
add28c55 VZ |
321 | } |
322 | ||
ff5bf259 VZ |
323 | void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event)) |
324 | { | |
5f939e78 | 325 | wxTreeItemId item = m_treeCtrl->GetSelection(); |
e1ee62bd | 326 | |
5f939e78 | 327 | CHECK_ITEM( item ); |
e1ee62bd | 328 | |
5f939e78 | 329 | m_treeCtrl->Delete(item); |
ff5bf259 VZ |
330 | } |
331 | ||
372edb9d VZ |
332 | void MyFrame::OnDeleteChildren(wxCommandEvent& WXUNUSED(event)) |
333 | { | |
5f939e78 | 334 | wxTreeItemId item = m_treeCtrl->GetSelection(); |
e1ee62bd | 335 | |
5f939e78 | 336 | CHECK_ITEM( item ); |
e1ee62bd | 337 | |
5f939e78 | 338 | m_treeCtrl->DeleteChildren(item); |
372edb9d VZ |
339 | } |
340 | ||
ff5bf259 VZ |
341 | void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event)) |
342 | { | |
5f939e78 | 343 | m_treeCtrl->DeleteAllItems(); |
ff5bf259 VZ |
344 | } |
345 | ||
346 | void MyFrame::OnRecreate(wxCommandEvent& event) | |
347 | { | |
5f939e78 VZ |
348 | OnDeleteAll(event); |
349 | m_treeCtrl->AddTestItemsToTree(3, 2); | |
ff5bf259 VZ |
350 | } |
351 | ||
dd3646fd VZ |
352 | void MyFrame::OnCollapseAndReset(wxCommandEvent& event) |
353 | { | |
5f939e78 | 354 | m_treeCtrl->CollapseAndReset(m_treeCtrl->GetRootItem()); |
dd3646fd VZ |
355 | } |
356 | ||
f65635b5 VZ |
357 | void MyFrame::OnEnsureVisible(wxCommandEvent& event) |
358 | { | |
359 | m_treeCtrl->DoEnsureVisible(); | |
360 | } | |
361 | ||
5f939e78 VZ |
362 | void MyFrame::OnAddItem(wxCommandEvent& WXUNUSED(event)) |
363 | { | |
364 | static int s_num = 0; | |
365 | ||
366 | wxString text; | |
367 | text.Printf("Item #%d", ++s_num); | |
368 | ||
369 | m_treeCtrl->AppendItem(m_treeCtrl->GetRootItem(), | |
112c5086 RR |
370 | text /*, |
371 | MyTreeCtrl::TreeCtrlIcon_File */ ); | |
5f939e78 VZ |
372 | } |
373 | ||
cf724bce RR |
374 | void MyFrame::OnIncIndent(wxCommandEvent& WXUNUSED(event)) |
375 | { | |
376 | unsigned int indent = m_treeCtrl->GetIndent(); | |
377 | if (indent < 100) | |
378 | m_treeCtrl->SetIndent( indent+5 ); | |
379 | } | |
380 | ||
381 | void MyFrame::OnDecIndent(wxCommandEvent& WXUNUSED(event)) | |
382 | { | |
383 | unsigned int indent = m_treeCtrl->GetIndent(); | |
384 | if (indent > 10) | |
385 | m_treeCtrl->SetIndent( indent-5 ); | |
386 | } | |
387 | ||
388 | void MyFrame::OnIncSpacing(wxCommandEvent& WXUNUSED(event)) | |
389 | { | |
390 | unsigned int indent = m_treeCtrl->GetSpacing(); | |
391 | if (indent < 100) | |
392 | m_treeCtrl->SetSpacing( indent+5 ); | |
393 | } | |
394 | ||
395 | void MyFrame::OnDecSpacing(wxCommandEvent& WXUNUSED(event)) | |
396 | { | |
397 | unsigned int indent = m_treeCtrl->GetSpacing(); | |
398 | if (indent > 10) | |
399 | m_treeCtrl->SetSpacing( indent-5 ); | |
400 | } | |
401 | ||
9dfbf520 VZ |
402 | void MyFrame::OnToggleIcon(wxCommandEvent& WXUNUSED(event)) |
403 | { | |
404 | wxTreeItemId item = m_treeCtrl->GetSelection(); | |
405 | ||
406 | CHECK_ITEM( item ); | |
407 | ||
408 | m_treeCtrl->DoToggleIcon(item); | |
409 | } | |
410 | ||
3a5a2f56 | 411 | // MyTreeCtrl implementation |
23fd5130 VZ |
412 | IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl, wxTreeCtrl) |
413 | ||
3a5a2f56 VZ |
414 | MyTreeCtrl::MyTreeCtrl(wxWindow *parent, const wxWindowID id, |
415 | const wxPoint& pos, const wxSize& size, | |
416 | long style) | |
417 | : wxTreeCtrl(parent, id, pos, size, style) | |
457814b5 | 418 | { |
9ce29a28 SB |
419 | #ifndef NO_VARIABLE_HEIGHT |
420 | #if wxUSE_LIBJPEG | |
9dfbf520 | 421 | wxImage::AddHandler(new wxJPEGHandler); |
91b8de8d RR |
422 | wxImage image; |
423 | ||
424 | image.LoadFile(wxString("horse.jpg"), wxBITMAP_TYPE_JPEG ); | |
9ce29a28 | 425 | #endif |
91b8de8d RR |
426 | #endif |
427 | ||
5f939e78 | 428 | m_reverseSort = FALSE; |
e1ee62bd | 429 | |
5f939e78 VZ |
430 | // Make an image list containing small icons |
431 | m_imageListNormal = new wxImageList(16, 16, TRUE); | |
457814b5 | 432 | |
5f939e78 | 433 | // should correspond to TreeCtrlIcon_xxx enum |
f60d0f94 | 434 | #if defined(__WXMSW__) && defined(__WIN16__) |
5f939e78 VZ |
435 | // This is required in 16-bit Windows mode only because we can't load a specific (16x16) |
436 | // icon image, so it comes out stretched | |
9ce29a28 | 437 | # ifndef NO_VARIABLE_HEIGHT |
91b8de8d RR |
438 | m_imageListNormal->Add(image.ConvertToBitmap()); |
439 | # else | |
5f939e78 | 440 | m_imageListNormal->Add(wxBitmap("bitmap1", wxBITMAP_TYPE_BMP_RESOURCE)); |
91b8de8d | 441 | # endif |
5f939e78 | 442 | m_imageListNormal->Add(wxBitmap("bitmap2", wxBITMAP_TYPE_BMP_RESOURCE)); |
f60d0f94 | 443 | #else |
9ce29a28 | 444 | # ifndef NO_VARIABLE_HEIGHT |
91b8de8d RR |
445 | m_imageListNormal->Add(image.ConvertToBitmap()); |
446 | # else | |
5f939e78 | 447 | m_imageListNormal->Add(wxICON(icon1)); |
91b8de8d | 448 | # endif |
5f939e78 | 449 | m_imageListNormal->Add(wxICON(icon2)); |
f60d0f94 | 450 | #endif |
457814b5 | 451 | |
5f939e78 | 452 | SetImageList(m_imageListNormal); |
457814b5 | 453 | |
5f939e78 VZ |
454 | // Add some items to the tree |
455 | AddTestItemsToTree(3, 2); | |
457814b5 JS |
456 | } |
457 | ||
3a5a2f56 | 458 | MyTreeCtrl::~MyTreeCtrl() |
457814b5 | 459 | { |
5f939e78 | 460 | delete m_imageListNormal; |
457814b5 JS |
461 | } |
462 | ||
e1ee62bd VZ |
463 | int MyTreeCtrl::OnCompareItems(const wxTreeItemId& item1, |
464 | const wxTreeItemId& item2) | |
465 | { | |
5f939e78 VZ |
466 | if ( m_reverseSort ) |
467 | { | |
468 | // just exchange 1st and 2nd items | |
469 | return wxTreeCtrl::OnCompareItems(item2, item1); | |
470 | } | |
471 | else | |
472 | { | |
473 | return wxTreeCtrl::OnCompareItems(item1, item2); | |
474 | } | |
e1ee62bd VZ |
475 | } |
476 | ||
3a5a2f56 VZ |
477 | void MyTreeCtrl::AddItemsRecursively(const wxTreeItemId& idParent, |
478 | size_t numChildren, | |
4832f7c0 VZ |
479 | size_t depth, |
480 | size_t folder) | |
457814b5 | 481 | { |
f65635b5 | 482 | if ( depth > 0 ) |
3a5a2f56 | 483 | { |
f65635b5 VZ |
484 | wxString str; |
485 | for ( size_t n = 0; n < numChildren; n++ ) | |
486 | { | |
487 | // at depth 1 elements won't have any more children | |
488 | if (depth == 1) | |
489 | str.Printf("%s child %d.%d", "File", folder, n + 1); | |
490 | else | |
491 | str.Printf("%s child %d", "Folder", n + 1); | |
492 | ||
9dfbf520 | 493 | int image = depth == 1 ? TreeCtrlIcon_File : TreeCtrlIcon_Folder; |
f65635b5 VZ |
494 | wxTreeItemId id = AppendItem(idParent, str, image, image, |
495 | new MyTreeItemData(str)); | |
496 | ||
497 | // remember the last child for OnEnsureVisible() | |
498 | if ( depth == 1 && n == numChildren - 1 ) | |
499 | { | |
500 | m_lastItem = id; | |
501 | } | |
502 | ||
503 | AddItemsRecursively(id, numChildren, depth - 1, n + 1); | |
504 | } | |
3a5a2f56 | 505 | } |
f65635b5 | 506 | //else: done! |
457814b5 JS |
507 | } |
508 | ||
3a5a2f56 VZ |
509 | void MyTreeCtrl::AddTestItemsToTree(size_t numChildren, |
510 | size_t depth) | |
457814b5 | 511 | { |
5f939e78 VZ |
512 | wxTreeItemId rootId = AddRoot("Root", |
513 | TreeCtrlIcon_Folder, TreeCtrlIcon_Folder, | |
514 | new MyTreeItemData("Root item")); | |
457814b5 | 515 | |
5f939e78 | 516 | AddItemsRecursively(rootId, numChildren, depth, 0); |
4832f7c0 VZ |
517 | } |
518 | ||
519 | void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId& idParent, long cookie) | |
520 | { | |
5f939e78 | 521 | wxTreeItemId id; |
4832f7c0 | 522 | |
5f939e78 VZ |
523 | if( cookie == -1 ) |
524 | id = GetFirstChild(idParent, cookie); | |
525 | else | |
526 | id = GetNextChild(idParent, cookie); | |
4832f7c0 | 527 | |
5f939e78 VZ |
528 | if(id <= 0) |
529 | return; | |
4832f7c0 | 530 | |
9dfbf520 | 531 | wxString text = GetItemText(id); |
5f939e78 | 532 | wxLogMessage(text); |
4832f7c0 | 533 | |
5f939e78 VZ |
534 | if (ItemHasChildren(id)) |
535 | GetItemsRecursively(id,-1); | |
4832f7c0 | 536 | |
5f939e78 | 537 | GetItemsRecursively(idParent, cookie); |
457814b5 JS |
538 | } |
539 | ||
9dfbf520 VZ |
540 | void MyTreeCtrl::DoToggleIcon(const wxTreeItemId& item) |
541 | { | |
542 | int image = GetItemImage(item) == TreeCtrlIcon_Folder ? TreeCtrlIcon_File | |
543 | : TreeCtrlIcon_Folder; | |
544 | ||
545 | SetItemImage(item, image); | |
546 | } | |
547 | ||
4832f7c0 | 548 | |
3a5a2f56 | 549 | // avoid repetition |
5f939e78 VZ |
550 | #define TREE_EVENT_HANDLER(name) \ |
551 | void MyTreeCtrl::name(wxTreeEvent& WXUNUSED(event)) \ | |
552 | { \ | |
553 | wxLogMessage(#name); \ | |
554 | } | |
457814b5 | 555 | |
3a5a2f56 VZ |
556 | TREE_EVENT_HANDLER(OnBeginDrag) |
557 | TREE_EVENT_HANDLER(OnBeginRDrag) | |
3a5a2f56 VZ |
558 | TREE_EVENT_HANDLER(OnDeleteItem) |
559 | TREE_EVENT_HANDLER(OnGetInfo) | |
560 | TREE_EVENT_HANDLER(OnSetInfo) | |
561 | TREE_EVENT_HANDLER(OnItemExpanded) | |
562 | TREE_EVENT_HANDLER(OnItemExpanding) | |
563 | TREE_EVENT_HANDLER(OnItemCollapsed) | |
564 | TREE_EVENT_HANDLER(OnSelChanged) | |
565 | TREE_EVENT_HANDLER(OnSelChanging) | |
566 | ||
567 | #undef TREE_EVENT_HANDLER | |
568 | ||
5ea47806 VZ |
569 | void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent& event) |
570 | { | |
571 | wxLogMessage("OnBeginLabelEdit"); | |
572 | ||
573 | // for testing, prevent this items label editing | |
574 | wxTreeItemId itemId = event.GetItem(); | |
575 | if ( IsTestItem(itemId) ) | |
576 | { | |
577 | wxMessageBox("You can't edit this item."); | |
578 | ||
579 | event.Veto(); | |
580 | } | |
581 | } | |
582 | ||
583 | void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent& event) | |
584 | { | |
585 | wxLogMessage("OnEndLabelEdit"); | |
586 | ||
587 | // don't allow anything except letters in the labels | |
588 | if ( !event.GetLabel().IsWord() ) | |
589 | { | |
590 | wxMessageBox("The label should contain only letters."); | |
591 | ||
592 | event.Veto(); | |
593 | } | |
594 | } | |
595 | ||
3a5a2f56 | 596 | void MyTreeCtrl::OnItemCollapsing(wxTreeEvent& event) |
457814b5 | 597 | { |
5f939e78 | 598 | wxLogMessage("OnItemCollapsing"); |
f135ff73 | 599 | |
5f939e78 VZ |
600 | // for testing, prevent the user from collapsing the first child folder |
601 | wxTreeItemId itemId = event.GetItem(); | |
5ea47806 | 602 | if ( IsTestItem(itemId) ) |
5f939e78 VZ |
603 | { |
604 | wxMessageBox("You can't collapse this item."); | |
457814b5 | 605 | |
5f939e78 VZ |
606 | event.Veto(); |
607 | } | |
457814b5 JS |
608 | } |
609 | ||
6daa0637 | 610 | void MyTreeCtrl::OnTreeKeyDown(wxTreeEvent&WXUNUSED(event)) |
435fe83e | 611 | { |
5f939e78 | 612 | wxLogMessage("OnTreeKeyDown"); |
435fe83e RR |
613 | } |
614 | ||
615 | void MyTreeCtrl::OnItemActivated(wxTreeEvent&WXUNUSED(event)) | |
457814b5 | 616 | { |
5f939e78 VZ |
617 | // show some info about this item |
618 | wxTreeItemId itemId = GetSelection(); | |
619 | MyTreeItemData *item = (MyTreeItemData *)GetItemData(itemId); | |
457814b5 | 620 | |
5f939e78 VZ |
621 | if ( item != NULL ) |
622 | { | |
623 | item->ShowInfo(this); | |
624 | } | |
457814b5 | 625 | |
5f939e78 VZ |
626 | wxLogMessage("OnItemActivated"); |
627 | } | |
628 | ||
629 | void MyTreeCtrl::OnRMouseDClick(wxMouseEvent& event) | |
630 | { | |
631 | wxTreeItemId id = HitTest(event.GetPosition()); | |
632 | if ( !id ) | |
633 | wxLogMessage("No item under mouse"); | |
634 | else | |
635 | { | |
636 | MyTreeItemData *item = (MyTreeItemData *)GetItemData(id); | |
637 | if ( item ) | |
638 | wxLogMessage("Item '%s' under mouse", item->GetDesc()); | |
639 | } | |
457814b5 JS |
640 | } |
641 | ||
3a5a2f56 | 642 | static inline const char *Bool2String(bool b) |
457814b5 | 643 | { |
5f939e78 | 644 | return b ? "" : "not "; |
457814b5 JS |
645 | } |
646 | ||
9769e589 | 647 | void MyTreeItemData::ShowInfo(wxTreeCtrl *tree) |
457814b5 | 648 | { |
5f939e78 VZ |
649 | wxLogMessage("Item '%s': %sselected, %sexpanded, %sbold,\n" |
650 | "%u children (%u immediately under this item).", | |
651 | m_desc.c_str(), | |
652 | Bool2String(tree->IsSelected(GetId())), | |
653 | Bool2String(tree->IsExpanded(GetId())), | |
654 | Bool2String(tree->IsBold(GetId())), | |
655 | tree->GetChildrenCount(GetId()), | |
656 | tree->GetChildrenCount(GetId(), FALSE)); | |
457814b5 | 657 | } |