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