More RTL work.
[wxWidgets.git] / src / generic / treectlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/treectlg.cpp
3 // Purpose: generic tree control implementation
4 // Author: Robert Roebling
5 // Created: 01/02/97
6 // Modified: 22/10/98 - almost total rewrite, simpler interface (VZ)
7 // Id: $Id$
8 // Copyright: (c) 1998 Robert Roebling and Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // =============================================================================
13 // declarations
14 // =============================================================================
15
16 // -----------------------------------------------------------------------------
17 // headers
18 // -----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_TREECTRL
28
29 #include "wx/treectrl.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/dcclient.h"
33 #include "wx/timer.h"
34 #include "wx/settings.h"
35 #include "wx/listbox.h"
36 #include "wx/textctrl.h"
37 #endif
38
39 #include "wx/generic/treectlg.h"
40 #include "wx/imaglist.h"
41
42 #include "wx/renderer.h"
43
44 #ifdef __WXMAC__
45 #include "wx/mac/private.h"
46 #endif
47
48 // -----------------------------------------------------------------------------
49 // array types
50 // -----------------------------------------------------------------------------
51
52 class WXDLLEXPORT wxGenericTreeItem;
53
54 WX_DEFINE_EXPORTED_ARRAY_PTR(wxGenericTreeItem *, wxArrayGenericTreeItems);
55
56 // ----------------------------------------------------------------------------
57 // constants
58 // ----------------------------------------------------------------------------
59
60 static const int NO_IMAGE = -1;
61
62 static const int PIXELS_PER_UNIT = 10;
63
64 // the margin between the item image and the item text
65 static const int MARGIN_BETWEEN_IMAGE_AND_TEXT = 4;
66
67 // -----------------------------------------------------------------------------
68 // private classes
69 // -----------------------------------------------------------------------------
70
71 // timer used for enabling in-place edit
72 class WXDLLEXPORT wxTreeRenameTimer: public wxTimer
73 {
74 public:
75 // start editing the current item after half a second (if the mouse hasn't
76 // been clicked/moved)
77 enum { DELAY = 500 };
78
79 wxTreeRenameTimer( wxGenericTreeCtrl *owner );
80
81 virtual void Notify();
82
83 private:
84 wxGenericTreeCtrl *m_owner;
85
86 DECLARE_NO_COPY_CLASS(wxTreeRenameTimer)
87 };
88
89 // control used for in-place edit
90 class WXDLLEXPORT wxTreeTextCtrl: public wxTextCtrl
91 {
92 public:
93 wxTreeTextCtrl(wxGenericTreeCtrl *owner, wxGenericTreeItem *item);
94
95 void EndEdit(bool discardChanges = false)
96 {
97 if ( discardChanges )
98 {
99 StopEditing();
100 }
101 else
102 {
103 m_aboutToFinish = true;
104
105 // Notify the owner about the changes
106 AcceptChanges();
107
108 // Even if vetoed, close the control (consistent with MSW)
109 Finish();
110 }
111 }
112
113 void StopEditing()
114 {
115 Finish();
116 m_owner->OnRenameCancelled(m_itemEdited);
117 }
118 const wxGenericTreeItem* item() const { return m_itemEdited; }
119
120 protected:
121 void OnChar( wxKeyEvent &event );
122 void OnKeyUp( wxKeyEvent &event );
123 void OnKillFocus( wxFocusEvent &event );
124
125 bool AcceptChanges();
126 void Finish();
127
128 private:
129 wxGenericTreeCtrl *m_owner;
130 wxGenericTreeItem *m_itemEdited;
131 wxString m_startValue;
132 bool m_finished;
133 bool m_aboutToFinish;
134
135 DECLARE_EVENT_TABLE()
136 DECLARE_NO_COPY_CLASS(wxTreeTextCtrl)
137 };
138
139 // timer used to clear wxGenericTreeCtrl::m_findPrefix if no key was pressed
140 // for a sufficiently long time
141 class WXDLLEXPORT wxTreeFindTimer : public wxTimer
142 {
143 public:
144 // reset the current prefix after half a second of inactivity
145 enum { DELAY = 500 };
146
147 wxTreeFindTimer( wxGenericTreeCtrl *owner ) { m_owner = owner; }
148
149 virtual void Notify() { m_owner->m_findPrefix.clear(); }
150
151 private:
152 wxGenericTreeCtrl *m_owner;
153
154 DECLARE_NO_COPY_CLASS(wxTreeFindTimer)
155 };
156
157 // a tree item
158 class WXDLLEXPORT wxGenericTreeItem
159 {
160 public:
161 // ctors & dtor
162 wxGenericTreeItem() { m_data = NULL; }
163 wxGenericTreeItem( wxGenericTreeItem *parent,
164 const wxString& text,
165 int image,
166 int selImage,
167 wxTreeItemData *data );
168
169 ~wxGenericTreeItem();
170
171 // trivial accessors
172 wxArrayGenericTreeItems& GetChildren() { return m_children; }
173
174 const wxString& GetText() const { return m_text; }
175 int GetImage(wxTreeItemIcon which = wxTreeItemIcon_Normal) const
176 { return m_images[which]; }
177 wxTreeItemData *GetData() const { return m_data; }
178
179 // returns the current image for the item (depending on its
180 // selected/expanded/whatever state)
181 int GetCurrentImage() const;
182
183 void SetText( const wxString &text );
184 void SetImage(int image, wxTreeItemIcon which) { m_images[which] = image; }
185 void SetData(wxTreeItemData *data) { m_data = data; }
186
187 void SetHasPlus(bool has = true) { m_hasPlus = has; }
188
189 void SetBold(bool bold) { m_isBold = bold; }
190
191 int GetX() const { return m_x; }
192 int GetY() const { return m_y; }
193
194 void SetX(int x) { m_x = x; }
195 void SetY(int y) { m_y = y; }
196
197 int GetHeight() const { return m_height; }
198 int GetWidth() const { return m_width; }
199
200 void SetHeight(int h) { m_height = h; }
201 void SetWidth(int w) { m_width = w; }
202
203 wxGenericTreeItem *GetParent() const { return m_parent; }
204
205 // operations
206
207 // deletes all children notifying the treectrl about it
208 void DeleteChildren(wxGenericTreeCtrl *tree);
209
210 // get count of all children (and grand children if 'recursively')
211 size_t GetChildrenCount(bool recursively = true) const;
212
213 void Insert(wxGenericTreeItem *child, size_t index)
214 { m_children.Insert(child, index); }
215
216 void GetSize( int &x, int &y, const wxGenericTreeCtrl* );
217
218 // return the item at given position (or NULL if no item), onButton is
219 // true if the point belongs to the item's button, otherwise it lies
220 // on the item's label
221 wxGenericTreeItem *HitTest( const wxPoint& point,
222 const wxGenericTreeCtrl *,
223 int &flags,
224 int level );
225
226 void Expand() { m_isCollapsed = false; }
227 void Collapse() { m_isCollapsed = true; }
228
229 void SetHilight( bool set = true ) { m_hasHilight = set; }
230
231 // status inquiries
232 bool HasChildren() const { return !m_children.IsEmpty(); }
233 bool IsSelected() const { return m_hasHilight != 0; }
234 bool IsExpanded() const { return !m_isCollapsed; }
235 bool HasPlus() const { return m_hasPlus || HasChildren(); }
236 bool IsBold() const { return m_isBold != 0; }
237
238 // attributes
239 // get them - may be NULL
240 wxTreeItemAttr *GetAttributes() const { return m_attr; }
241 // get them ensuring that the pointer is not NULL
242 wxTreeItemAttr& Attr()
243 {
244 if ( !m_attr )
245 {
246 m_attr = new wxTreeItemAttr;
247 m_ownsAttr = true;
248 }
249 return *m_attr;
250 }
251 // set them
252 void SetAttributes(wxTreeItemAttr *attr)
253 {
254 if ( m_ownsAttr ) delete m_attr;
255 m_attr = attr;
256 m_ownsAttr = false;
257 }
258 // set them and delete when done
259 void AssignAttributes(wxTreeItemAttr *attr)
260 {
261 SetAttributes(attr);
262 m_ownsAttr = true;
263 }
264
265 private:
266 // since there can be very many of these, we save size by chosing
267 // the smallest representation for the elements and by ordering
268 // the members to avoid padding.
269 wxString m_text; // label to be rendered for item
270
271 wxTreeItemData *m_data; // user-provided data
272
273 wxArrayGenericTreeItems m_children; // list of children
274 wxGenericTreeItem *m_parent; // parent of this item
275
276 wxTreeItemAttr *m_attr; // attributes???
277
278 // tree ctrl images for the normal, selected, expanded and
279 // expanded+selected states
280 int m_images[wxTreeItemIcon_Max];
281
282 wxCoord m_x; // (virtual) offset from top
283 wxCoord m_y; // (virtual) offset from left
284 int m_width; // width of this item
285 int m_height; // height of this item
286
287 // use bitfields to save size
288 unsigned int m_isCollapsed :1;
289 unsigned int m_hasHilight :1; // same as focused
290 unsigned int m_hasPlus :1; // used for item which doesn't have
291 // children but has a [+] button
292 unsigned int m_isBold :1; // render the label in bold font
293 unsigned int m_ownsAttr :1; // delete attribute when done
294
295 DECLARE_NO_COPY_CLASS(wxGenericTreeItem)
296 };
297
298 // =============================================================================
299 // implementation
300 // =============================================================================
301
302 // ----------------------------------------------------------------------------
303 // private functions
304 // ----------------------------------------------------------------------------
305
306 // translate the key or mouse event flags to the type of selection we're
307 // dealing with
308 static void EventFlagsToSelType(long style,
309 bool shiftDown,
310 bool ctrlDown,
311 bool &is_multiple,
312 bool &extended_select,
313 bool &unselect_others)
314 {
315 is_multiple = (style & wxTR_MULTIPLE) != 0;
316 extended_select = shiftDown && is_multiple;
317 unselect_others = !(extended_select || (ctrlDown && is_multiple));
318 }
319
320 // check if the given item is under another one
321 static bool IsDescendantOf(const wxGenericTreeItem *parent, const wxGenericTreeItem *item)
322 {
323 while ( item )
324 {
325 if ( item == parent )
326 {
327 // item is a descendant of parent
328 return true;
329 }
330
331 item = item->GetParent();
332 }
333
334 return false;
335 }
336
337 // -----------------------------------------------------------------------------
338 // wxTreeRenameTimer (internal)
339 // -----------------------------------------------------------------------------
340
341 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl *owner )
342 {
343 m_owner = owner;
344 }
345
346 void wxTreeRenameTimer::Notify()
347 {
348 m_owner->OnRenameTimer();
349 }
350
351 //-----------------------------------------------------------------------------
352 // wxTreeTextCtrl (internal)
353 //-----------------------------------------------------------------------------
354
355 BEGIN_EVENT_TABLE(wxTreeTextCtrl,wxTextCtrl)
356 EVT_CHAR (wxTreeTextCtrl::OnChar)
357 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp)
358 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus)
359 END_EVENT_TABLE()
360
361 wxTreeTextCtrl::wxTreeTextCtrl(wxGenericTreeCtrl *owner,
362 wxGenericTreeItem *item)
363 : m_itemEdited(item), m_startValue(item->GetText())
364 {
365 m_owner = owner;
366 m_finished = false;
367 m_aboutToFinish = false;
368
369 int w = m_itemEdited->GetWidth(),
370 h = m_itemEdited->GetHeight();
371
372 int x, y;
373 m_owner->CalcScrolledPosition(item->GetX(), item->GetY(), &x, &y);
374
375 int image_h = 0,
376 image_w = 0;
377
378 int image = item->GetCurrentImage();
379 if ( image != NO_IMAGE )
380 {
381 if ( m_owner->m_imageListNormal )
382 {
383 m_owner->m_imageListNormal->GetSize( image, image_w, image_h );
384 image_w += MARGIN_BETWEEN_IMAGE_AND_TEXT;
385 }
386 else
387 {
388 wxFAIL_MSG(_T("you must create an image list to use images!"));
389 }
390 }
391
392 // FIXME: what are all these hardcoded 4, 8 and 11s really?
393 x += image_w;
394 w -= image_w + 4;
395 #ifdef __WXMAC__
396 wxSize bs = DoGetBestSize() ;
397 // edit control height
398 if ( h > bs.y - 8 )
399 {
400 int diff = h - ( bs.y - 8 ) ;
401 h -= diff ;
402 y += diff / 2 ;
403 }
404 #endif
405
406 (void)Create(m_owner, wxID_ANY, m_startValue,
407 wxPoint(x - 4, y - 4), wxSize(w + 11, h + 8));
408 }
409
410 bool wxTreeTextCtrl::AcceptChanges()
411 {
412 const wxString value = GetValue();
413
414 if ( value == m_startValue )
415 {
416 // nothing changed, always accept
417 // when an item remains unchanged, the owner
418 // needs to be notified that the user decided
419 // not to change the tree item label, and that
420 // the edit has been cancelled
421
422 m_owner->OnRenameCancelled(m_itemEdited);
423 return true;
424 }
425
426 if ( !m_owner->OnRenameAccept(m_itemEdited, value) )
427 {
428 // vetoed by the user
429 return false;
430 }
431
432 // accepted, do rename the item
433 m_owner->SetItemText(m_itemEdited, value);
434
435 return true;
436 }
437
438 void wxTreeTextCtrl::Finish()
439 {
440 if ( !m_finished )
441 {
442 m_owner->ResetTextControl();
443
444 wxPendingDelete.Append(this);
445
446 m_finished = true;
447
448 m_owner->SetFocus();
449 }
450 }
451
452 void wxTreeTextCtrl::OnChar( wxKeyEvent &event )
453 {
454 switch ( event.m_keyCode )
455 {
456 case WXK_RETURN:
457 EndEdit();
458 break;
459
460 case WXK_ESCAPE:
461 StopEditing();
462 break;
463
464 default:
465 event.Skip();
466 }
467 }
468
469 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent &event )
470 {
471 if ( !m_finished )
472 {
473 // auto-grow the textctrl:
474 wxSize parentSize = m_owner->GetSize();
475 wxPoint myPos = GetPosition();
476 wxSize mySize = GetSize();
477 int sx, sy;
478 GetTextExtent(GetValue() + _T("M"), &sx, &sy);
479 if (myPos.x + sx > parentSize.x)
480 sx = parentSize.x - myPos.x;
481 if (mySize.x > sx)
482 sx = mySize.x;
483 SetSize(sx, wxDefaultCoord);
484 }
485
486 event.Skip();
487 }
488
489 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent &event )
490 {
491 if ( !m_finished && !m_aboutToFinish )
492 {
493 // We must finish regardless of success, otherwise we'll get
494 // focus problems:
495 Finish();
496
497 if ( !AcceptChanges() )
498 m_owner->OnRenameCancelled( m_itemEdited );
499 }
500
501 // We must let the native text control handle focus, too, otherwise
502 // it could have problems with the cursor (e.g., in wxGTK).
503 event.Skip();
504 }
505
506 // -----------------------------------------------------------------------------
507 // wxGenericTreeItem
508 // -----------------------------------------------------------------------------
509
510 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem *parent,
511 const wxString& text,
512 int image, int selImage,
513 wxTreeItemData *data)
514 : m_text(text)
515 {
516 m_images[wxTreeItemIcon_Normal] = image;
517 m_images[wxTreeItemIcon_Selected] = selImage;
518 m_images[wxTreeItemIcon_Expanded] = NO_IMAGE;
519 m_images[wxTreeItemIcon_SelectedExpanded] = NO_IMAGE;
520
521 m_data = data;
522 m_x = m_y = 0;
523
524 m_isCollapsed = true;
525 m_hasHilight = false;
526 m_hasPlus = false;
527 m_isBold = false;
528
529 m_parent = parent;
530
531 m_attr = (wxTreeItemAttr *)NULL;
532 m_ownsAttr = false;
533
534 // We don't know the height here yet.
535 m_width = 0;
536 m_height = 0;
537 }
538
539 wxGenericTreeItem::~wxGenericTreeItem()
540 {
541 delete m_data;
542
543 if (m_ownsAttr) delete m_attr;
544
545 wxASSERT_MSG( m_children.IsEmpty(),
546 wxT("please call DeleteChildren() before deleting the item") );
547 }
548
549 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl *tree)
550 {
551 size_t count = m_children.Count();
552 for ( size_t n = 0; n < count; n++ )
553 {
554 wxGenericTreeItem *child = m_children[n];
555 tree->SendDeleteEvent(child);
556
557 child->DeleteChildren(tree);
558 if ( child == tree->m_select_me )
559 tree->m_select_me = NULL;
560 delete child;
561 }
562
563 m_children.Empty();
564 }
565
566 void wxGenericTreeItem::SetText( const wxString &text )
567 {
568 m_text = text;
569 }
570
571 size_t wxGenericTreeItem::GetChildrenCount(bool recursively) const
572 {
573 size_t count = m_children.Count();
574 if ( !recursively )
575 return count;
576
577 size_t total = count;
578 for (size_t n = 0; n < count; ++n)
579 {
580 total += m_children[n]->GetChildrenCount();
581 }
582
583 return total;
584 }
585
586 void wxGenericTreeItem::GetSize( int &x, int &y,
587 const wxGenericTreeCtrl *theButton )
588 {
589 int bottomY=m_y+theButton->GetLineHeight(this);
590 if ( y < bottomY ) y = bottomY;
591 int width = m_x + m_width;
592 if ( x < width ) x = width;
593
594 if (IsExpanded())
595 {
596 size_t count = m_children.Count();
597 for ( size_t n = 0; n < count; ++n )
598 {
599 m_children[n]->GetSize( x, y, theButton );
600 }
601 }
602 }
603
604 wxGenericTreeItem *wxGenericTreeItem::HitTest(const wxPoint& point,
605 const wxGenericTreeCtrl *theCtrl,
606 int &flags,
607 int level)
608 {
609 // for a hidden root node, don't evaluate it, but do evaluate children
610 if ( !(level == 0 && theCtrl->HasFlag(wxTR_HIDE_ROOT)) )
611 {
612 // evaluate the item
613 int h = theCtrl->GetLineHeight(this);
614 if ((point.y > m_y) && (point.y < m_y + h))
615 {
616 int y_mid = m_y + h/2;
617 if (point.y < y_mid )
618 flags |= wxTREE_HITTEST_ONITEMUPPERPART;
619 else
620 flags |= wxTREE_HITTEST_ONITEMLOWERPART;
621
622 int xCross = m_x - theCtrl->GetSpacing();
623 #ifdef __WXMAC__
624 // according to the drawing code the triangels are drawn
625 // at -4 , -4 from the position up to +10/+10 max
626 if ((point.x > xCross-4) && (point.x < xCross+10) &&
627 (point.y > y_mid-4) && (point.y < y_mid+10) &&
628 HasPlus() && theCtrl->HasButtons() )
629 #else
630 // 5 is the size of the plus sign
631 if ((point.x > xCross-6) && (point.x < xCross+6) &&
632 (point.y > y_mid-6) && (point.y < y_mid+6) &&
633 HasPlus() && theCtrl->HasButtons() )
634 #endif
635 {
636 flags |= wxTREE_HITTEST_ONITEMBUTTON;
637 return this;
638 }
639
640 if ((point.x >= m_x) && (point.x <= m_x+m_width))
641 {
642 int image_w = -1;
643 int image_h;
644
645 // assuming every image (normal and selected) has the same size!
646 if ( (GetImage() != NO_IMAGE) && theCtrl->m_imageListNormal )
647 theCtrl->m_imageListNormal->GetSize(GetImage(),
648 image_w, image_h);
649
650 if ((image_w != -1) && (point.x <= m_x + image_w + 1))
651 flags |= wxTREE_HITTEST_ONITEMICON;
652 else
653 flags |= wxTREE_HITTEST_ONITEMLABEL;
654
655 return this;
656 }
657
658 if (point.x < m_x)
659 flags |= wxTREE_HITTEST_ONITEMINDENT;
660 if (point.x > m_x+m_width)
661 flags |= wxTREE_HITTEST_ONITEMRIGHT;
662
663 return this;
664 }
665
666 // if children are expanded, fall through to evaluate them
667 if (m_isCollapsed) return (wxGenericTreeItem*) NULL;
668 }
669
670 // evaluate children
671 size_t count = m_children.Count();
672 for ( size_t n = 0; n < count; n++ )
673 {
674 wxGenericTreeItem *res = m_children[n]->HitTest( point,
675 theCtrl,
676 flags,
677 level + 1 );
678 if ( res != NULL )
679 return res;
680 }
681
682 return (wxGenericTreeItem*) NULL;
683 }
684
685 int wxGenericTreeItem::GetCurrentImage() const
686 {
687 int image = NO_IMAGE;
688 if ( IsExpanded() )
689 {
690 if ( IsSelected() )
691 {
692 image = GetImage(wxTreeItemIcon_SelectedExpanded);
693 }
694
695 if ( image == NO_IMAGE )
696 {
697 // we usually fall back to the normal item, but try just the
698 // expanded one (and not selected) first in this case
699 image = GetImage(wxTreeItemIcon_Expanded);
700 }
701 }
702 else // not expanded
703 {
704 if ( IsSelected() )
705 image = GetImage(wxTreeItemIcon_Selected);
706 }
707
708 // maybe it doesn't have the specific image we want,
709 // try the default one instead
710 if ( image == NO_IMAGE ) image = GetImage();
711
712 return image;
713 }
714
715 // -----------------------------------------------------------------------------
716 // wxGenericTreeCtrl implementation
717 // -----------------------------------------------------------------------------
718
719 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl, wxControl)
720
721 BEGIN_EVENT_TABLE(wxGenericTreeCtrl, wxTreeCtrlBase)
722 EVT_PAINT (wxGenericTreeCtrl::OnPaint)
723 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse)
724 EVT_CHAR (wxGenericTreeCtrl::OnChar)
725 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus)
726 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus)
727 EVT_TREE_ITEM_GETTOOLTIP(wxID_ANY, wxGenericTreeCtrl::OnGetToolTip)
728 END_EVENT_TABLE()
729
730 #if !defined(__WXMSW__) || defined(__WXUNIVERSAL__)
731 /*
732 * wxTreeCtrl has to be a real class or we have problems with
733 * the run-time information.
734 */
735
736 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxGenericTreeCtrl)
737 #endif
738
739 // -----------------------------------------------------------------------------
740 // construction/destruction
741 // -----------------------------------------------------------------------------
742
743 void wxGenericTreeCtrl::Init()
744 {
745 m_current =
746 m_key_current =
747 m_anchor =
748 m_select_me = (wxGenericTreeItem *) NULL;
749 m_hasFocus = false;
750 m_dirty = false;
751
752 m_lineHeight = 10;
753 m_indent = 15;
754 m_spacing = 18;
755
756 m_hilightBrush = new wxBrush
757 (
758 wxSystemSettings::GetColour
759 (
760 wxSYS_COLOUR_HIGHLIGHT
761 ),
762 wxSOLID
763 );
764
765 m_hilightUnfocusedBrush = new wxBrush
766 (
767 wxSystemSettings::GetColour
768 (
769 wxSYS_COLOUR_BTNSHADOW
770 ),
771 wxSOLID
772 );
773
774 m_imageListButtons = NULL;
775 m_ownsImageListButtons = false;
776
777 m_dragCount = 0;
778 m_isDragging = false;
779 m_dropTarget = m_oldSelection = NULL;
780 m_underMouse = NULL;
781 m_textCtrl = NULL;
782
783 m_renameTimer = NULL;
784 m_freezeCount = 0;
785
786 m_findTimer = NULL;
787
788 m_dropEffectAboveItem = false;
789
790 m_lastOnSame = false;
791
792 #ifdef __WXMAC_CARBON__
793 m_normalFont.MacCreateThemeFont( kThemeViewsFont ) ;
794 #else
795 m_normalFont = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT );
796 #endif
797 m_boldFont = wxFont(m_normalFont.GetPointSize(),
798 m_normalFont.GetFamily(),
799 m_normalFont.GetStyle(),
800 wxBOLD,
801 m_normalFont.GetUnderlined(),
802 m_normalFont.GetFaceName(),
803 m_normalFont.GetEncoding());
804 }
805
806 bool wxGenericTreeCtrl::Create(wxWindow *parent,
807 wxWindowID id,
808 const wxPoint& pos,
809 const wxSize& size,
810 long style,
811 const wxValidator& validator,
812 const wxString& name )
813 {
814 #ifdef __WXMAC__
815 int major,minor;
816 wxGetOsVersion( &major, &minor );
817
818 style &= ~wxTR_LINES_AT_ROOT;
819 style |= wxTR_NO_LINES;
820 if (major < 10)
821 style |= wxTR_ROW_LINES;
822 #endif // __WXMAC__
823
824 if ( !wxControl::Create( parent, id, pos, size,
825 style|wxHSCROLL|wxVSCROLL,
826 validator,
827 name ) )
828 return false;
829
830 // If the tree display has no buttons, but does have
831 // connecting lines, we can use a narrower layout.
832 // It may not be a good idea to force this...
833 if (!HasButtons() && !HasFlag(wxTR_NO_LINES))
834 {
835 m_indent= 10;
836 m_spacing = 10;
837 }
838
839 wxVisualAttributes attr = GetDefaultAttributes();
840 SetOwnForegroundColour( attr.colFg );
841 SetOwnBackgroundColour( attr.colBg );
842 if (!m_hasFont)
843 SetOwnFont(attr.font);
844
845 m_dottedPen = wxPen( wxT("grey"), 0, 0 );
846
847 SetBestSize(size);
848
849 return true;
850 }
851
852 wxGenericTreeCtrl::~wxGenericTreeCtrl()
853 {
854 delete m_hilightBrush;
855 delete m_hilightUnfocusedBrush;
856
857 DeleteAllItems();
858
859 delete m_renameTimer;
860 delete m_findTimer;
861
862 if (m_ownsImageListButtons)
863 delete m_imageListButtons;
864 }
865
866 // -----------------------------------------------------------------------------
867 // accessors
868 // -----------------------------------------------------------------------------
869
870 unsigned int wxGenericTreeCtrl::GetCount() const
871 {
872 if ( !m_anchor )
873 {
874 // the tree is empty
875 return 0;
876 }
877
878 unsigned int count = m_anchor->GetChildrenCount();
879 if ( !HasFlag(wxTR_HIDE_ROOT) )
880 {
881 // take the root itself into account
882 count++;
883 }
884
885 return count;
886 }
887
888 void wxGenericTreeCtrl::SetIndent(unsigned int indent)
889 {
890 m_indent = (unsigned short) indent;
891 m_dirty = true;
892 }
893
894 size_t
895 wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId& item,
896 bool recursively) const
897 {
898 wxCHECK_MSG( item.IsOk(), 0u, wxT("invalid tree item") );
899
900 return ((wxGenericTreeItem*) item.m_pItem)->GetChildrenCount(recursively);
901 }
902
903 void wxGenericTreeCtrl::SetWindowStyle(const long styles)
904 {
905 // Do not try to expand the root node if it hasn't been created yet
906 if (m_anchor && !HasFlag(wxTR_HIDE_ROOT) && (styles & wxTR_HIDE_ROOT))
907 {
908 // if we will hide the root, make sure children are visible
909 m_anchor->SetHasPlus();
910 m_anchor->Expand();
911 CalculatePositions();
912 }
913
914 // right now, just sets the styles. Eventually, we may
915 // want to update the inherited styles, but right now
916 // none of the parents has updatable styles
917 m_windowStyle = styles;
918 m_dirty = true;
919 }
920
921 // -----------------------------------------------------------------------------
922 // functions to work with tree items
923 // -----------------------------------------------------------------------------
924
925 wxString wxGenericTreeCtrl::GetItemText(const wxTreeItemId& item) const
926 {
927 wxCHECK_MSG( item.IsOk(), wxEmptyString, wxT("invalid tree item") );
928
929 return ((wxGenericTreeItem*) item.m_pItem)->GetText();
930 }
931
932 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId& item,
933 wxTreeItemIcon which) const
934 {
935 wxCHECK_MSG( item.IsOk(), -1, wxT("invalid tree item") );
936
937 return ((wxGenericTreeItem*) item.m_pItem)->GetImage(which);
938 }
939
940 wxTreeItemData *wxGenericTreeCtrl::GetItemData(const wxTreeItemId& item) const
941 {
942 wxCHECK_MSG( item.IsOk(), NULL, wxT("invalid tree item") );
943
944 return ((wxGenericTreeItem*) item.m_pItem)->GetData();
945 }
946
947 wxColour wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId& item) const
948 {
949 wxCHECK_MSG( item.IsOk(), wxNullColour, wxT("invalid tree item") );
950
951 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
952 return pItem->Attr().GetTextColour();
953 }
954
955 wxColour wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId& item) const
956 {
957 wxCHECK_MSG( item.IsOk(), wxNullColour, wxT("invalid tree item") );
958
959 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
960 return pItem->Attr().GetBackgroundColour();
961 }
962
963 wxFont wxGenericTreeCtrl::GetItemFont(const wxTreeItemId& item) const
964 {
965 wxCHECK_MSG( item.IsOk(), wxNullFont, wxT("invalid tree item") );
966
967 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
968 return pItem->Attr().GetFont();
969 }
970
971 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text)
972 {
973 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
974
975 wxClientDC dc(this);
976 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
977 pItem->SetText(text);
978 CalculateSize(pItem, dc);
979 RefreshLine(pItem);
980 }
981
982 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId& item,
983 int image,
984 wxTreeItemIcon which)
985 {
986 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
987
988 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
989 pItem->SetImage(image, which);
990
991 wxClientDC dc(this);
992 CalculateSize(pItem, dc);
993 RefreshLine(pItem);
994 }
995
996 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
997 {
998 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
999
1000 if (data)
1001 data->SetId( item );
1002
1003 ((wxGenericTreeItem*) item.m_pItem)->SetData(data);
1004 }
1005
1006 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
1007 {
1008 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1009
1010 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
1011 pItem->SetHasPlus(has);
1012 RefreshLine(pItem);
1013 }
1014
1015 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
1016 {
1017 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1018
1019 // avoid redrawing the tree if no real change
1020 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
1021 if ( pItem->IsBold() != bold )
1022 {
1023 pItem->SetBold(bold);
1024
1025 // recalculate the item size as bold and non bold fonts have different
1026 // widths
1027 wxClientDC dc(this);
1028 CalculateSize(pItem, dc);
1029
1030 RefreshLine(pItem);
1031 }
1032 }
1033
1034 void wxGenericTreeCtrl::SetItemDropHighlight(const wxTreeItemId& item,
1035 bool highlight)
1036 {
1037 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1038
1039 wxColour fg, bg;
1040
1041 if (highlight)
1042 {
1043 bg = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
1044 fg = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
1045 }
1046
1047 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
1048 pItem->Attr().SetTextColour(fg);
1049 pItem->Attr().SetBackgroundColour(bg);
1050 RefreshLine(pItem);
1051 }
1052
1053 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId& item,
1054 const wxColour& col)
1055 {
1056 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1057
1058 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
1059 pItem->Attr().SetTextColour(col);
1060 RefreshLine(pItem);
1061 }
1062
1063 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId& item,
1064 const wxColour& col)
1065 {
1066 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1067
1068 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
1069 pItem->Attr().SetBackgroundColour(col);
1070 RefreshLine(pItem);
1071 }
1072
1073 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId& item, const wxFont& font)
1074 {
1075 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1076
1077 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
1078 pItem->Attr().SetFont(font);
1079 RefreshLine(pItem);
1080 }
1081
1082 bool wxGenericTreeCtrl::SetFont( const wxFont &font )
1083 {
1084 wxTreeCtrlBase::SetFont(font);
1085
1086 m_normalFont = font ;
1087 m_boldFont = wxFont(m_normalFont.GetPointSize(),
1088 m_normalFont.GetFamily(),
1089 m_normalFont.GetStyle(),
1090 wxBOLD,
1091 m_normalFont.GetUnderlined(),
1092 m_normalFont.GetFaceName(),
1093 m_normalFont.GetEncoding());
1094
1095 return true;
1096 }
1097
1098
1099 // -----------------------------------------------------------------------------
1100 // item status inquiries
1101 // -----------------------------------------------------------------------------
1102
1103 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId& item) const
1104 {
1105 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
1106
1107 // An item is only visible if it's not a descendant of a collapsed item
1108 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
1109 wxGenericTreeItem* parent = pItem->GetParent();
1110 while (parent)
1111 {
1112 if (!parent->IsExpanded())
1113 return false;
1114 parent = parent->GetParent();
1115 }
1116
1117 int startX, startY;
1118 GetViewStart(& startX, & startY);
1119
1120 wxSize clientSize = GetClientSize();
1121
1122 wxRect rect;
1123 if (!GetBoundingRect(item, rect))
1124 return false;
1125 if (rect.GetWidth() == 0 || rect.GetHeight() == 0)
1126 return false;
1127 if (rect.GetBottom() < 0 || rect.GetTop() > clientSize.y)
1128 return false;
1129 if (rect.GetRight() < 0 || rect.GetLeft() > clientSize.x)
1130 return false;
1131
1132 return true;
1133 }
1134
1135 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
1136 {
1137 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
1138
1139 // consider that the item does have children if it has the "+" button: it
1140 // might not have them (if it had never been expanded yet) but then it
1141 // could have them as well and it's better to err on this side rather than
1142 // disabling some operations which are restricted to the items with
1143 // children for an item which does have them
1144 return ((wxGenericTreeItem*) item.m_pItem)->HasPlus();
1145 }
1146
1147 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId& item) const
1148 {
1149 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
1150
1151 return ((wxGenericTreeItem*) item.m_pItem)->IsExpanded();
1152 }
1153
1154 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId& item) const
1155 {
1156 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
1157
1158 return ((wxGenericTreeItem*) item.m_pItem)->IsSelected();
1159 }
1160
1161 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId& item) const
1162 {
1163 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
1164
1165 return ((wxGenericTreeItem*) item.m_pItem)->IsBold();
1166 }
1167
1168 // -----------------------------------------------------------------------------
1169 // navigation
1170 // -----------------------------------------------------------------------------
1171
1172 wxTreeItemId wxGenericTreeCtrl::GetItemParent(const wxTreeItemId& item) const
1173 {
1174 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1175
1176 return ((wxGenericTreeItem*) item.m_pItem)->GetParent();
1177 }
1178
1179 wxTreeItemId wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId& item,
1180 wxTreeItemIdValue& cookie) const
1181 {
1182 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1183
1184 cookie = 0;
1185 return GetNextChild(item, cookie);
1186 }
1187
1188 wxTreeItemId wxGenericTreeCtrl::GetNextChild(const wxTreeItemId& item,
1189 wxTreeItemIdValue& cookie) const
1190 {
1191 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1192
1193 wxArrayGenericTreeItems& children = ((wxGenericTreeItem*) item.m_pItem)->GetChildren();
1194
1195 // it's ok to cast cookie to size_t, we never have indices big enough to
1196 // overflow "void *"
1197 size_t *pIndex = (size_t *)&cookie;
1198 if ( *pIndex < children.Count() )
1199 {
1200 return children.Item((*pIndex)++);
1201 }
1202 else
1203 {
1204 // there are no more of them
1205 return wxTreeItemId();
1206 }
1207 }
1208
1209 #if WXWIN_COMPATIBILITY_2_4
1210
1211 wxTreeItemId wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId& item,
1212 long& cookie) const
1213 {
1214 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1215
1216 cookie = 0;
1217 return GetNextChild(item, cookie);
1218 }
1219
1220 wxTreeItemId wxGenericTreeCtrl::GetNextChild(const wxTreeItemId& item,
1221 long& cookie) const
1222 {
1223 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1224
1225 wxArrayGenericTreeItems& children = ((wxGenericTreeItem*) item.m_pItem)->GetChildren();
1226 if ( (size_t)cookie < children.Count() )
1227 {
1228 return children.Item((size_t)cookie++);
1229 }
1230 else
1231 {
1232 // there are no more of them
1233 return wxTreeItemId();
1234 }
1235 }
1236
1237 #endif // WXWIN_COMPATIBILITY_2_4
1238
1239 wxTreeItemId wxGenericTreeCtrl::GetLastChild(const wxTreeItemId& item) const
1240 {
1241 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1242
1243 wxArrayGenericTreeItems& children = ((wxGenericTreeItem*) item.m_pItem)->GetChildren();
1244 return (children.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children.Last()));
1245 }
1246
1247 wxTreeItemId wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
1248 {
1249 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1250
1251 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
1252 wxGenericTreeItem *parent = i->GetParent();
1253 if ( parent == NULL )
1254 {
1255 // root item doesn't have any siblings
1256 return wxTreeItemId();
1257 }
1258
1259 wxArrayGenericTreeItems& siblings = parent->GetChildren();
1260 int index = siblings.Index(i);
1261 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
1262
1263 size_t n = (size_t)(index + 1);
1264 return n == siblings.Count() ? wxTreeItemId() : wxTreeItemId(siblings[n]);
1265 }
1266
1267 wxTreeItemId wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
1268 {
1269 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1270
1271 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
1272 wxGenericTreeItem *parent = i->GetParent();
1273 if ( parent == NULL )
1274 {
1275 // root item doesn't have any siblings
1276 return wxTreeItemId();
1277 }
1278
1279 wxArrayGenericTreeItems& siblings = parent->GetChildren();
1280 int index = siblings.Index(i);
1281 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
1282
1283 return index == 0 ? wxTreeItemId()
1284 : wxTreeItemId(siblings[(size_t)(index - 1)]);
1285 }
1286
1287 // Only for internal use right now, but should probably be public
1288 wxTreeItemId wxGenericTreeCtrl::GetNext(const wxTreeItemId& item) const
1289 {
1290 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1291
1292 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
1293
1294 // First see if there are any children.
1295 wxArrayGenericTreeItems& children = i->GetChildren();
1296 if (children.GetCount() > 0)
1297 {
1298 return children.Item(0);
1299 }
1300 else
1301 {
1302 // Try a sibling of this or ancestor instead
1303 wxTreeItemId p = item;
1304 wxTreeItemId toFind;
1305 do
1306 {
1307 toFind = GetNextSibling(p);
1308 p = GetItemParent(p);
1309 } while (p.IsOk() && !toFind.IsOk());
1310 return toFind;
1311 }
1312 }
1313
1314 wxTreeItemId wxGenericTreeCtrl::GetFirstVisibleItem() const
1315 {
1316 wxTreeItemId id = GetRootItem();
1317 if (!id.IsOk())
1318 return id;
1319
1320 do
1321 {
1322 if (IsVisible(id))
1323 return id;
1324 id = GetNext(id);
1325 } while (id.IsOk());
1326
1327 return wxTreeItemId();
1328 }
1329
1330 wxTreeItemId wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
1331 {
1332 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1333
1334 wxTreeItemId id = item;
1335 if (id.IsOk())
1336 {
1337 while (id = GetNext(id), id.IsOk())
1338 {
1339 if (IsVisible(id))
1340 return id;
1341 }
1342 }
1343 return wxTreeItemId();
1344 }
1345
1346 wxTreeItemId wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
1347 {
1348 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1349
1350 wxFAIL_MSG(wxT("not implemented"));
1351
1352 return wxTreeItemId();
1353 }
1354
1355 // called by wxTextTreeCtrl when it marks itself for deletion
1356 void wxGenericTreeCtrl::ResetTextControl()
1357 {
1358 m_textCtrl = NULL;
1359 }
1360
1361 // find the first item starting with the given prefix after the given item
1362 wxTreeItemId wxGenericTreeCtrl::FindItem(const wxTreeItemId& idParent,
1363 const wxString& prefixOrig) const
1364 {
1365 // match is case insensitive as this is more convenient to the user: having
1366 // to press Shift-letter to go to the item starting with a capital letter
1367 // would be too bothersome
1368 wxString prefix = prefixOrig.Lower();
1369
1370 // determine the starting point: we shouldn't take the current item (this
1371 // allows to switch between two items starting with the same letter just by
1372 // pressing it) but we shouldn't jump to the next one if the user is
1373 // continuing to type as otherwise he might easily skip the item he wanted
1374 wxTreeItemId id = idParent;
1375 if ( prefix.length() == 1 )
1376 {
1377 id = GetNext(id);
1378 }
1379
1380 // look for the item starting with the given prefix after it
1381 while ( id.IsOk() && !GetItemText(id).Lower().StartsWith(prefix) )
1382 {
1383 id = GetNext(id);
1384 }
1385
1386 // if we haven't found anything...
1387 if ( !id.IsOk() )
1388 {
1389 // ... wrap to the beginning
1390 id = GetRootItem();
1391 if ( HasFlag(wxTR_HIDE_ROOT) )
1392 {
1393 // can't select virtual root
1394 id = GetNext(id);
1395 }
1396
1397 // and try all the items (stop when we get to the one we started from)
1398 while ( id != idParent && !GetItemText(id).Lower().StartsWith(prefix) )
1399 {
1400 id = GetNext(id);
1401 }
1402 }
1403
1404 return id;
1405 }
1406
1407 // -----------------------------------------------------------------------------
1408 // operations
1409 // -----------------------------------------------------------------------------
1410
1411 wxTreeItemId wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId& parentId,
1412 size_t previous,
1413 const wxString& text,
1414 int image,
1415 int selImage,
1416 wxTreeItemData *data)
1417 {
1418 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
1419 if ( !parent )
1420 {
1421 // should we give a warning here?
1422 return AddRoot(text, image, selImage, data);
1423 }
1424
1425 m_dirty = true; // do this first so stuff below doesn't cause flicker
1426
1427 wxGenericTreeItem *item =
1428 new wxGenericTreeItem( parent, text, image, selImage, data );
1429
1430 if ( data != NULL )
1431 {
1432 data->m_pItem = item;
1433 }
1434
1435 parent->Insert( item, previous == (size_t)-1 ? parent->GetChildren().size()
1436 : previous );
1437
1438 return item;
1439 }
1440
1441 wxTreeItemId wxGenericTreeCtrl::AddRoot(const wxString& text,
1442 int image,
1443 int selImage,
1444 wxTreeItemData *data)
1445 {
1446 wxCHECK_MSG( !m_anchor, wxTreeItemId(), wxT("tree can have only one root") );
1447
1448 m_dirty = true; // do this first so stuff below doesn't cause flicker
1449
1450 m_anchor = new wxGenericTreeItem((wxGenericTreeItem *)NULL, text,
1451 image, selImage, data);
1452 if ( data != NULL )
1453 {
1454 data->m_pItem = m_anchor;
1455 }
1456
1457 if (HasFlag(wxTR_HIDE_ROOT))
1458 {
1459 // if root is hidden, make sure we can navigate
1460 // into children
1461 m_anchor->SetHasPlus();
1462 m_anchor->Expand();
1463 CalculatePositions();
1464 }
1465
1466 if (!HasFlag(wxTR_MULTIPLE))
1467 {
1468 m_current = m_key_current = m_anchor;
1469 m_current->SetHilight( true );
1470 }
1471
1472 return m_anchor;
1473 }
1474
1475 wxTreeItemId wxGenericTreeCtrl::DoInsertAfter(const wxTreeItemId& parentId,
1476 const wxTreeItemId& idPrevious,
1477 const wxString& text,
1478 int image, int selImage,
1479 wxTreeItemData *data)
1480 {
1481 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
1482 if ( !parent )
1483 {
1484 // should we give a warning here?
1485 return AddRoot(text, image, selImage, data);
1486 }
1487
1488 int index = -1;
1489 if (idPrevious.IsOk())
1490 {
1491 index = parent->GetChildren().Index((wxGenericTreeItem*) idPrevious.m_pItem);
1492 wxASSERT_MSG( index != wxNOT_FOUND,
1493 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1494 }
1495
1496 return DoInsertItem(parentId, (size_t)++index, text, image, selImage, data);
1497 }
1498
1499
1500 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem *item)
1501 {
1502 wxTreeEvent event(wxEVT_COMMAND_TREE_DELETE_ITEM, this, item);
1503 ProcessEvent( event );
1504 }
1505
1506 // Don't leave edit or selection on a child which is about to disappear
1507 void wxGenericTreeCtrl::ChildrenClosing(wxGenericTreeItem* item)
1508 {
1509 if (m_textCtrl != NULL && item != m_textCtrl->item() && IsDescendantOf(item, m_textCtrl->item())) {
1510 m_textCtrl->StopEditing();
1511 }
1512 if (item != m_key_current && IsDescendantOf(item, m_key_current)) {
1513 m_key_current = NULL;
1514 }
1515 if (IsDescendantOf(item, m_select_me)) {
1516 m_select_me = item;
1517 }
1518 if (item != m_current && IsDescendantOf(item, m_current)) {
1519 m_current->SetHilight( false );
1520 m_current = NULL;
1521 m_select_me = item;
1522 }
1523 }
1524
1525 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId& itemId)
1526 {
1527 m_dirty = true; // do this first so stuff below doesn't cause flicker
1528
1529 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1530 ChildrenClosing(item);
1531 item->DeleteChildren(this);
1532 }
1533
1534 void wxGenericTreeCtrl::Delete(const wxTreeItemId& itemId)
1535 {
1536 m_dirty = true; // do this first so stuff below doesn't cause flicker
1537
1538 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1539
1540 if (m_textCtrl != NULL && IsDescendantOf(item, m_textCtrl->item()))
1541 {
1542 // can't delete the item being edited, cancel editing it first
1543 m_textCtrl->StopEditing();
1544 }
1545
1546 wxGenericTreeItem *parent = item->GetParent();
1547
1548 // don't keep stale pointers around!
1549 if ( IsDescendantOf(item, m_key_current) )
1550 {
1551 // Don't silently change the selection:
1552 // do it properly in idle time, so event
1553 // handlers get called.
1554
1555 // m_key_current = parent;
1556 m_key_current = NULL;
1557 }
1558
1559 // m_select_me records whether we need to select
1560 // a different item, in idle time.
1561 if ( m_select_me && IsDescendantOf(item, m_select_me) )
1562 {
1563 m_select_me = parent;
1564 }
1565
1566 if ( IsDescendantOf(item, m_current) )
1567 {
1568 // Don't silently change the selection:
1569 // do it properly in idle time, so event
1570 // handlers get called.
1571
1572 // m_current = parent;
1573 m_current = NULL;
1574 m_select_me = parent;
1575 }
1576
1577 // remove the item from the tree
1578 if ( parent )
1579 {
1580 parent->GetChildren().Remove( item ); // remove by value
1581 }
1582 else // deleting the root
1583 {
1584 // nothing will be left in the tree
1585 m_anchor = NULL;
1586 }
1587
1588 // and delete all of its children and the item itself now
1589 item->DeleteChildren(this);
1590 SendDeleteEvent(item);
1591
1592 if (item == m_select_me)
1593 m_select_me = NULL;
1594
1595 delete item;
1596 }
1597
1598 void wxGenericTreeCtrl::DeleteAllItems()
1599 {
1600 if ( m_anchor )
1601 {
1602 Delete(m_anchor);
1603 }
1604 }
1605
1606 void wxGenericTreeCtrl::Expand(const wxTreeItemId& itemId)
1607 {
1608 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1609
1610 wxCHECK_RET( item, _T("invalid item in wxGenericTreeCtrl::Expand") );
1611 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT) || itemId != GetRootItem(),
1612 _T("can't expand hidden root") );
1613
1614 if ( !item->HasPlus() )
1615 return;
1616
1617 if ( item->IsExpanded() )
1618 return;
1619
1620 wxTreeEvent event(wxEVT_COMMAND_TREE_ITEM_EXPANDING, this, item);
1621
1622 if ( ProcessEvent( event ) && !event.IsAllowed() )
1623 {
1624 // cancelled by program
1625 return;
1626 }
1627
1628 item->Expand();
1629 CalculatePositions();
1630
1631 RefreshSubtree(item);
1632
1633 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED);
1634 ProcessEvent( event );
1635 }
1636
1637 void wxGenericTreeCtrl::Collapse(const wxTreeItemId& itemId)
1638 {
1639 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT) || itemId != GetRootItem(),
1640 _T("can't collapse hidden root") );
1641
1642 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1643
1644 if ( !item->IsExpanded() )
1645 return;
1646
1647 wxTreeEvent event(wxEVT_COMMAND_TREE_ITEM_COLLAPSING, this, item);
1648 if ( ProcessEvent( event ) && !event.IsAllowed() )
1649 {
1650 // cancelled by program
1651 return;
1652 }
1653
1654 ChildrenClosing(item);
1655 item->Collapse();
1656
1657 #if 0 // TODO why should items be collapsed recursively?
1658 wxArrayGenericTreeItems& children = item->GetChildren();
1659 size_t count = children.Count();
1660 for ( size_t n = 0; n < count; n++ )
1661 {
1662 Collapse(children[n]);
1663 }
1664 #endif
1665
1666 CalculatePositions();
1667
1668 RefreshSubtree(item);
1669
1670 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED);
1671 ProcessEvent( event );
1672 }
1673
1674 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
1675 {
1676 Collapse(item);
1677 DeleteChildren(item);
1678 }
1679
1680 void wxGenericTreeCtrl::Toggle(const wxTreeItemId& itemId)
1681 {
1682 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1683
1684 if (item->IsExpanded())
1685 Collapse(itemId);
1686 else
1687 Expand(itemId);
1688 }
1689
1690 void wxGenericTreeCtrl::Unselect()
1691 {
1692 if (m_current)
1693 {
1694 m_current->SetHilight( false );
1695 RefreshLine( m_current );
1696
1697 m_current = NULL;
1698 m_select_me = NULL;
1699 }
1700 }
1701
1702 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem *item)
1703 {
1704 if (item->IsSelected())
1705 {
1706 item->SetHilight(false);
1707 RefreshLine(item);
1708 }
1709
1710 if (item->HasChildren())
1711 {
1712 wxArrayGenericTreeItems& children = item->GetChildren();
1713 size_t count = children.Count();
1714 for ( size_t n = 0; n < count; ++n )
1715 {
1716 UnselectAllChildren(children[n]);
1717 }
1718 }
1719 }
1720
1721 void wxGenericTreeCtrl::UnselectAll()
1722 {
1723 wxTreeItemId rootItem = GetRootItem();
1724
1725 // the tree might not have the root item at all
1726 if ( rootItem )
1727 {
1728 UnselectAllChildren((wxGenericTreeItem*) rootItem.m_pItem);
1729 }
1730 }
1731
1732 // Recursive function !
1733 // To stop we must have crt_item<last_item
1734 // Algorithm :
1735 // Tag all next children, when no more children,
1736 // Move to parent (not to tag)
1737 // Keep going... if we found last_item, we stop.
1738 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select)
1739 {
1740 wxGenericTreeItem *parent = crt_item->GetParent();
1741
1742 if (parent == NULL) // This is root item
1743 return TagAllChildrenUntilLast(crt_item, last_item, select);
1744
1745 wxArrayGenericTreeItems& children = parent->GetChildren();
1746 int index = children.Index(crt_item);
1747 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
1748
1749 size_t count = children.Count();
1750 for (size_t n=(size_t)(index+1); n<count; ++n)
1751 {
1752 if (TagAllChildrenUntilLast(children[n], last_item, select)) return true;
1753 }
1754
1755 return TagNextChildren(parent, last_item, select);
1756 }
1757
1758 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select)
1759 {
1760 crt_item->SetHilight(select);
1761 RefreshLine(crt_item);
1762
1763 if (crt_item==last_item)
1764 return true;
1765
1766 if (crt_item->HasChildren())
1767 {
1768 wxArrayGenericTreeItems& children = crt_item->GetChildren();
1769 size_t count = children.Count();
1770 for ( size_t n = 0; n < count; ++n )
1771 {
1772 if (TagAllChildrenUntilLast(children[n], last_item, select))
1773 return true;
1774 }
1775 }
1776
1777 return false;
1778 }
1779
1780 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem *item1, wxGenericTreeItem *item2)
1781 {
1782 m_select_me = NULL;
1783
1784 // item2 is not necessary after item1
1785 // choice first' and 'last' between item1 and item2
1786 wxGenericTreeItem *first= (item1->GetY()<item2->GetY()) ? item1 : item2;
1787 wxGenericTreeItem *last = (item1->GetY()<item2->GetY()) ? item2 : item1;
1788
1789 bool select = m_current->IsSelected();
1790
1791 if ( TagAllChildrenUntilLast(first,last,select) )
1792 return;
1793
1794 TagNextChildren(first,last,select);
1795 }
1796
1797 void wxGenericTreeCtrl::DoSelectItem(const wxTreeItemId& itemId,
1798 bool unselect_others,
1799 bool extended_select)
1800 {
1801 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
1802
1803 m_select_me = NULL;
1804
1805 bool is_single=!(GetWindowStyleFlag() & wxTR_MULTIPLE);
1806 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1807
1808 //wxCHECK_RET( ( (!unselect_others) && is_single),
1809 // wxT("this is a single selection tree") );
1810
1811 // to keep going anyhow !!!
1812 if (is_single)
1813 {
1814 if (item->IsSelected())
1815 return; // nothing to do
1816 unselect_others = true;
1817 extended_select = false;
1818 }
1819 else if ( unselect_others && item->IsSelected() )
1820 {
1821 // selection change if there is more than one item currently selected
1822 wxArrayTreeItemIds selected_items;
1823 if ( GetSelections(selected_items) == 1 )
1824 return;
1825 }
1826
1827 wxTreeEvent event(wxEVT_COMMAND_TREE_SEL_CHANGING, this, item);
1828 event.m_itemOld = m_current;
1829 // TODO : Here we don't send any selection mode yet !
1830
1831 if ( GetEventHandler()->ProcessEvent( event ) && !event.IsAllowed() )
1832 return;
1833
1834 wxTreeItemId parent = GetItemParent( itemId );
1835 while (parent.IsOk())
1836 {
1837 if (!IsExpanded(parent))
1838 Expand( parent );
1839
1840 parent = GetItemParent( parent );
1841 }
1842
1843 // ctrl press
1844 if (unselect_others)
1845 {
1846 if (is_single) Unselect(); // to speed up thing
1847 else UnselectAll();
1848 }
1849
1850 // shift press
1851 if (extended_select)
1852 {
1853 if ( !m_current )
1854 {
1855 m_current = m_key_current = (wxGenericTreeItem*) GetRootItem().m_pItem;
1856 }
1857
1858 // don't change the mark (m_current)
1859 SelectItemRange(m_current, item);
1860 }
1861 else
1862 {
1863 bool select = true; // the default
1864
1865 // Check if we need to toggle hilight (ctrl mode)
1866 if (!unselect_others)
1867 select=!item->IsSelected();
1868
1869 m_current = m_key_current = item;
1870 m_current->SetHilight(select);
1871 RefreshLine( m_current );
1872 }
1873
1874 // This can cause idle processing to select the root
1875 // if no item is selected, so it must be after the
1876 // selection is set
1877 EnsureVisible( itemId );
1878
1879 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
1880 GetEventHandler()->ProcessEvent( event );
1881 }
1882
1883 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId& itemId, bool select)
1884 {
1885 if ( select )
1886 {
1887 DoSelectItem(itemId, !HasFlag(wxTR_MULTIPLE));
1888 }
1889 else // deselect
1890 {
1891 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1892 wxCHECK_RET( item, wxT("SelectItem(): invalid tree item") );
1893
1894 item->SetHilight(false);
1895 RefreshLine(item);
1896 }
1897 }
1898
1899 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem *item,
1900 wxArrayTreeItemIds &array) const
1901 {
1902 if ( item->IsSelected() )
1903 array.Add(wxTreeItemId(item));
1904
1905 if ( item->HasChildren() )
1906 {
1907 wxArrayGenericTreeItems& children = item->GetChildren();
1908 size_t count = children.GetCount();
1909 for ( size_t n = 0; n < count; ++n )
1910 FillArray(children[n], array);
1911 }
1912 }
1913
1914 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds &array) const
1915 {
1916 array.Empty();
1917 wxTreeItemId idRoot = GetRootItem();
1918 if ( idRoot.IsOk() )
1919 {
1920 FillArray((wxGenericTreeItem*) idRoot.m_pItem, array);
1921 }
1922 //else: the tree is empty, so no selections
1923
1924 return array.Count();
1925 }
1926
1927 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId& item)
1928 {
1929 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1930
1931 if (!item.IsOk()) return;
1932
1933 wxGenericTreeItem *gitem = (wxGenericTreeItem*) item.m_pItem;
1934
1935 // first expand all parent branches
1936 wxGenericTreeItem *parent = gitem->GetParent();
1937
1938 if ( HasFlag(wxTR_HIDE_ROOT) )
1939 {
1940 while ( parent && parent != m_anchor )
1941 {
1942 Expand(parent);
1943 parent = parent->GetParent();
1944 }
1945 }
1946 else
1947 {
1948 while ( parent )
1949 {
1950 Expand(parent);
1951 parent = parent->GetParent();
1952 }
1953 }
1954
1955 //if (parent) CalculatePositions();
1956
1957 ScrollTo(item);
1958 }
1959
1960 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId &item)
1961 {
1962 if (!item.IsOk()) return;
1963
1964 // We have to call this here because the label in
1965 // question might just have been added and no screen
1966 // update taken place.
1967 if (m_dirty)
1968 #if defined( __WXMSW__ ) || defined(__WXMAC__)
1969 Update();
1970 #else
1971 DoDirtyProcessing();
1972 #endif
1973 wxGenericTreeItem *gitem = (wxGenericTreeItem*) item.m_pItem;
1974
1975 // now scroll to the item
1976 int item_y = gitem->GetY();
1977
1978 int start_x = 0;
1979 int start_y = 0;
1980 GetViewStart( &start_x, &start_y );
1981 start_y *= PIXELS_PER_UNIT;
1982
1983 int client_h = 0;
1984 int client_w = 0;
1985 GetClientSize( &client_w, &client_h );
1986
1987 if (item_y < start_y+3)
1988 {
1989 // going down
1990 int x = 0;
1991 int y = 0;
1992 m_anchor->GetSize( x, y, this );
1993 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1994 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1995 int x_pos = GetScrollPos( wxHORIZONTAL );
1996 // Item should appear at top
1997 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, item_y/PIXELS_PER_UNIT );
1998 }
1999 else if (item_y+GetLineHeight(gitem) > start_y+client_h)
2000 {
2001 // going up
2002 int x = 0;
2003 int y = 0;
2004 m_anchor->GetSize( x, y, this );
2005 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
2006 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
2007 item_y += PIXELS_PER_UNIT+2;
2008 int x_pos = GetScrollPos( wxHORIZONTAL );
2009 // Item should appear at bottom
2010 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, (item_y+GetLineHeight(gitem)-client_h)/PIXELS_PER_UNIT );
2011 }
2012 }
2013
2014 // FIXME: tree sorting functions are not reentrant and not MT-safe!
2015 static wxGenericTreeCtrl *s_treeBeingSorted = NULL;
2016
2017 static int LINKAGEMODE tree_ctrl_compare_func(wxGenericTreeItem **item1,
2018 wxGenericTreeItem **item2)
2019 {
2020 wxCHECK_MSG( s_treeBeingSorted, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
2021
2022 return s_treeBeingSorted->OnCompareItems(*item1, *item2);
2023 }
2024
2025 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId& itemId)
2026 {
2027 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
2028
2029 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
2030
2031 wxCHECK_RET( !s_treeBeingSorted,
2032 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
2033
2034 wxArrayGenericTreeItems& children = item->GetChildren();
2035 if ( children.Count() > 1 )
2036 {
2037 m_dirty = true;
2038
2039 s_treeBeingSorted = this;
2040 children.Sort(tree_ctrl_compare_func);
2041 s_treeBeingSorted = NULL;
2042 }
2043 //else: don't make the tree dirty as nothing changed
2044 }
2045
2046 void wxGenericTreeCtrl::CalculateLineHeight()
2047 {
2048 wxClientDC dc(this);
2049 m_lineHeight = (int)(dc.GetCharHeight() + 4);
2050
2051 if ( m_imageListNormal )
2052 {
2053 // Calculate a m_lineHeight value from the normal Image sizes.
2054 // May be toggle off. Then wxGenericTreeCtrl will spread when
2055 // necessary (which might look ugly).
2056 int n = m_imageListNormal->GetImageCount();
2057 for (int i = 0; i < n ; i++)
2058 {
2059 int width = 0, height = 0;
2060 m_imageListNormal->GetSize(i, width, height);
2061 if (height > m_lineHeight) m_lineHeight = height;
2062 }
2063 }
2064
2065 if (m_imageListButtons)
2066 {
2067 // Calculate a m_lineHeight value from the Button image sizes.
2068 // May be toggle off. Then wxGenericTreeCtrl will spread when
2069 // necessary (which might look ugly).
2070 int n = m_imageListButtons->GetImageCount();
2071 for (int i = 0; i < n ; i++)
2072 {
2073 int width = 0, height = 0;
2074 m_imageListButtons->GetSize(i, width, height);
2075 if (height > m_lineHeight) m_lineHeight = height;
2076 }
2077 }
2078
2079 if (m_lineHeight < 30)
2080 m_lineHeight += 2; // at least 2 pixels
2081 else
2082 m_lineHeight += m_lineHeight/10; // otherwise 10% extra spacing
2083 }
2084
2085 void wxGenericTreeCtrl::SetImageList(wxImageList *imageList)
2086 {
2087 if (m_ownsImageListNormal) delete m_imageListNormal;
2088 m_imageListNormal = imageList;
2089 m_ownsImageListNormal = false;
2090 m_dirty = true;
2091 // Don't do any drawing if we're setting the list to NULL,
2092 // since we may be in the process of deleting the tree control.
2093 if (imageList)
2094 CalculateLineHeight();
2095 }
2096
2097 void wxGenericTreeCtrl::SetStateImageList(wxImageList *imageList)
2098 {
2099 if (m_ownsImageListState) delete m_imageListState;
2100 m_imageListState = imageList;
2101 m_ownsImageListState = false;
2102 }
2103
2104 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList *imageList)
2105 {
2106 if (m_ownsImageListButtons) delete m_imageListButtons;
2107 m_imageListButtons = imageList;
2108 m_ownsImageListButtons = false;
2109 m_dirty = true;
2110 CalculateLineHeight();
2111 }
2112
2113 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList *imageList)
2114 {
2115 SetButtonsImageList(imageList);
2116 m_ownsImageListButtons = true;
2117 }
2118
2119 // -----------------------------------------------------------------------------
2120 // helpers
2121 // -----------------------------------------------------------------------------
2122
2123 void wxGenericTreeCtrl::AdjustMyScrollbars()
2124 {
2125 if (m_anchor)
2126 {
2127 int x = 0, y = 0;
2128 m_anchor->GetSize( x, y, this );
2129 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
2130 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
2131 int x_pos = GetScrollPos( wxHORIZONTAL );
2132 int y_pos = GetScrollPos( wxVERTICAL );
2133 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, y_pos );
2134 }
2135 else
2136 {
2137 SetScrollbars( 0, 0, 0, 0 );
2138 }
2139 }
2140
2141 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem *item) const
2142 {
2143 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT)
2144 return item->GetHeight();
2145 else
2146 return m_lineHeight;
2147 }
2148
2149 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem *item, wxDC& dc)
2150 {
2151 // TODO implement "state" icon on items
2152
2153 wxTreeItemAttr *attr = item->GetAttributes();
2154 if ( attr && attr->HasFont() )
2155 dc.SetFont(attr->GetFont());
2156 else if (item->IsBold())
2157 dc.SetFont(m_boldFont);
2158
2159 long text_w = 0, text_h = 0;
2160 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
2161
2162 int image_h = 0, image_w = 0;
2163 int image = item->GetCurrentImage();
2164 if ( image != NO_IMAGE )
2165 {
2166 if ( m_imageListNormal )
2167 {
2168 m_imageListNormal->GetSize( image, image_w, image_h );
2169 image_w += MARGIN_BETWEEN_IMAGE_AND_TEXT;
2170 }
2171 else
2172 {
2173 image = NO_IMAGE;
2174 }
2175 }
2176
2177 int total_h = GetLineHeight(item);
2178 bool drawItemBackground = false;
2179
2180 if ( item->IsSelected() )
2181 {
2182 // under mac selections are only a rectangle in case they don't have the focus
2183 #ifdef __WXMAC__
2184 if ( !m_hasFocus )
2185 {
2186 dc.SetBrush( *wxTRANSPARENT_BRUSH ) ;
2187 dc.SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) , 1 , wxSOLID ) ) ;
2188 }
2189 else
2190 {
2191 dc.SetBrush( *m_hilightBrush ) ;
2192 }
2193 #else
2194 dc.SetBrush(*(m_hasFocus ? m_hilightBrush : m_hilightUnfocusedBrush));
2195 #endif
2196 drawItemBackground = true;
2197 }
2198 else
2199 {
2200 wxColour colBg;
2201 if ( attr && attr->HasBackgroundColour() )
2202 {
2203 drawItemBackground = true;
2204 colBg = attr->GetBackgroundColour();
2205 }
2206 else
2207 {
2208 colBg = GetBackgroundColour();
2209 }
2210 dc.SetBrush(wxBrush(colBg, wxSOLID));
2211 }
2212
2213 int offset = HasFlag(wxTR_ROW_LINES) ? 1 : 0;
2214
2215 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT) )
2216 {
2217 int x, y, w, h;
2218
2219 DoGetPosition(&x, &y);
2220 DoGetSize(&w, &h);
2221 dc.DrawRectangle(x, item->GetY()+offset, w, total_h-offset);
2222 }
2223 else
2224 {
2225 if ( item->IsSelected() && image != NO_IMAGE )
2226 {
2227 // If it's selected, and there's an image, then we should
2228 // take care to leave the area under the image painted in the
2229 // background colour.
2230 dc.DrawRectangle( item->GetX() + image_w - 2, item->GetY()+offset,
2231 item->GetWidth() - image_w + 2, total_h-offset );
2232 }
2233 // On GTK+ 2, drawing a 'normal' background is wrong for themes that
2234 // don't allow backgrounds to be customized. Not drawing the background,
2235 // except for custom item backgrounds, works for both kinds of theme.
2236 else if (drawItemBackground)
2237 {
2238 dc.DrawRectangle( item->GetX()-2, item->GetY()+offset,
2239 item->GetWidth()+2, total_h-offset );
2240 }
2241 }
2242
2243 if ( image != NO_IMAGE )
2244 {
2245 dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, total_h );
2246 m_imageListNormal->Draw( image, dc,
2247 item->GetX(),
2248 item->GetY() +((total_h > image_h)?((total_h-image_h)/2):0),
2249 wxIMAGELIST_DRAW_TRANSPARENT );
2250 dc.DestroyClippingRegion();
2251 }
2252
2253 dc.SetBackgroundMode(wxTRANSPARENT);
2254 int extraH = (total_h > text_h) ? (total_h - text_h)/2 : 0;
2255 dc.DrawText( item->GetText(),
2256 (wxCoord)(image_w + item->GetX()),
2257 (wxCoord)(item->GetY() + extraH));
2258
2259 // restore normal font
2260 dc.SetFont( m_normalFont );
2261 }
2262
2263 // Now y stands for the top of the item, whereas it used to stand for middle !
2264 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
2265 {
2266 int x = level*m_indent;
2267 if (!HasFlag(wxTR_HIDE_ROOT))
2268 {
2269 x += m_indent;
2270 }
2271 else if (level == 0)
2272 {
2273 // always expand hidden root
2274 int origY = y;
2275 wxArrayGenericTreeItems& children = item->GetChildren();
2276 int count = children.Count();
2277 if (count > 0)
2278 {
2279 int n = 0, oldY;
2280 do {
2281 oldY = y;
2282 PaintLevel(children[n], dc, 1, y);
2283 } while (++n < count);
2284
2285 if (!HasFlag(wxTR_NO_LINES) && HasFlag(wxTR_LINES_AT_ROOT) && count > 0)
2286 {
2287 // draw line down to last child
2288 origY += GetLineHeight(children[0])>>1;
2289 oldY += GetLineHeight(children[n-1])>>1;
2290 dc.DrawLine(3, origY, 3, oldY);
2291 }
2292 }
2293 return;
2294 }
2295
2296 item->SetX(x+m_spacing);
2297 item->SetY(y);
2298
2299 int h = GetLineHeight(item);
2300 int y_top = y;
2301 int y_mid = y_top + (h>>1);
2302 y += h;
2303
2304 int exposed_x = dc.LogicalToDeviceX(0);
2305 int exposed_y = dc.LogicalToDeviceY(y_top);
2306
2307 if (IsExposed(exposed_x, exposed_y, 10000, h)) // 10000 = very much
2308 {
2309 const wxPen *pen =
2310 #ifndef __WXMAC__
2311 // don't draw rect outline if we already have the
2312 // background color under Mac
2313 (item->IsSelected() && m_hasFocus) ? wxBLACK_PEN :
2314 #endif // !__WXMAC__
2315 wxTRANSPARENT_PEN;
2316
2317 wxColour colText;
2318 if ( item->IsSelected()
2319 #ifdef __WXMAC__
2320 // On wxMac, if the tree doesn't have the focus we draw an empty
2321 // rectangle, so we want to make sure that the text is visible
2322 // against the normal background, not the highlightbackground, so
2323 // don't use the highlight text colour unless we have the focus.
2324 && m_hasFocus
2325 #endif
2326 )
2327 {
2328 colText = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
2329 }
2330 else
2331 {
2332 wxTreeItemAttr *attr = item->GetAttributes();
2333 if (attr && attr->HasTextColour())
2334 colText = attr->GetTextColour();
2335 else
2336 colText = GetForegroundColour();
2337 }
2338
2339 // prepare to draw
2340 dc.SetTextForeground(colText);
2341 dc.SetPen(*pen);
2342
2343 // draw
2344 PaintItem(item, dc);
2345
2346 if (HasFlag(wxTR_ROW_LINES))
2347 {
2348 // if the background colour is white, choose a
2349 // contrasting color for the lines
2350 dc.SetPen(*((GetBackgroundColour() == *wxWHITE)
2351 ? wxMEDIUM_GREY_PEN : wxWHITE_PEN));
2352 dc.DrawLine(0, y_top, 10000, y_top);
2353 dc.DrawLine(0, y, 10000, y);
2354 }
2355
2356 // restore DC objects
2357 dc.SetBrush(*wxWHITE_BRUSH);
2358 dc.SetPen(m_dottedPen);
2359 dc.SetTextForeground(*wxBLACK);
2360
2361 if ( !HasFlag(wxTR_NO_LINES) )
2362 {
2363 // draw the horizontal line here
2364 int x_start = x;
2365 if (x > (signed)m_indent)
2366 x_start -= m_indent;
2367 else if (HasFlag(wxTR_LINES_AT_ROOT))
2368 x_start = 3;
2369 dc.DrawLine(x_start, y_mid, x + m_spacing, y_mid);
2370 }
2371
2372 // should the item show a button?
2373 if ( item->HasPlus() && HasButtons() )
2374 {
2375 if ( m_imageListButtons )
2376 {
2377 // draw the image button here
2378 int image_h = 0,
2379 image_w = 0;
2380 int image = item->IsExpanded() ? wxTreeItemIcon_Expanded
2381 : wxTreeItemIcon_Normal;
2382 if ( item->IsSelected() )
2383 image += wxTreeItemIcon_Selected - wxTreeItemIcon_Normal;
2384
2385 m_imageListButtons->GetSize(image, image_w, image_h);
2386 int xx = x - image_w/2;
2387 int yy = y_mid - image_h/2;
2388
2389 wxDCClipper clip(dc, xx, yy, image_w, image_h);
2390 wxPrintf( wxT("hi\n") );
2391 m_imageListButtons->Draw(image, dc, xx, yy,
2392 wxIMAGELIST_DRAW_TRANSPARENT);
2393 }
2394 else // no custom buttons
2395 {
2396 static const int wImage = 9;
2397 static const int hImage = 9;
2398
2399 int flag = 0;
2400 if (item->IsExpanded())
2401 flag |= wxCONTROL_EXPANDED;
2402 if (item == m_underMouse)
2403 flag |= wxCONTROL_CURRENT;
2404
2405 wxRendererNative::Get().DrawTreeItemButton
2406 (
2407 this,
2408 dc,
2409 wxRect(x - wImage/2,
2410 y_mid - hImage/2,
2411 wImage, hImage),
2412 flag
2413 );
2414 }
2415 }
2416 }
2417
2418 if (item->IsExpanded())
2419 {
2420 wxArrayGenericTreeItems& children = item->GetChildren();
2421 int count = children.Count();
2422 if (count > 0)
2423 {
2424 int n = 0, oldY;
2425 ++level;
2426 do {
2427 oldY = y;
2428 PaintLevel(children[n], dc, level, y);
2429 } while (++n < count);
2430
2431 if (!HasFlag(wxTR_NO_LINES) && count > 0)
2432 {
2433 // draw line down to last child
2434 oldY += GetLineHeight(children[n-1])>>1;
2435 if (HasButtons()) y_mid += 5;
2436
2437 // Only draw the portion of the line that is visible, in case it is huge
2438 wxCoord xOrigin=0, yOrigin=0, width, height;
2439 dc.GetDeviceOrigin(&xOrigin, &yOrigin);
2440 yOrigin = abs(yOrigin);
2441 GetClientSize(&width, &height);
2442
2443 // Move end points to the begining/end of the view?
2444 if (y_mid < yOrigin)
2445 y_mid = yOrigin;
2446 if (oldY > yOrigin + height)
2447 oldY = yOrigin + height;
2448
2449 // after the adjustments if y_mid is larger than oldY then the line
2450 // isn't visible at all so don't draw anything
2451 if (y_mid < oldY)
2452 dc.DrawLine(x, y_mid, x, oldY);
2453 }
2454 }
2455 }
2456 }
2457
2458 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem *item)
2459 {
2460 if ( item )
2461 {
2462 if ( item->HasPlus() )
2463 {
2464 // it's a folder, indicate it by a border
2465 DrawBorder(item);
2466 }
2467 else
2468 {
2469 // draw a line under the drop target because the item will be
2470 // dropped there
2471 DrawLine(item, !m_dropEffectAboveItem );
2472 }
2473
2474 SetCursor(wxCURSOR_BULLSEYE);
2475 }
2476 else
2477 {
2478 // can't drop here
2479 SetCursor(wxCURSOR_NO_ENTRY);
2480 }
2481 }
2482
2483 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId &item)
2484 {
2485 wxCHECK_RET( item.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2486
2487 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
2488
2489 wxClientDC dc(this);
2490 PrepareDC( dc );
2491 dc.SetLogicalFunction(wxINVERT);
2492 dc.SetBrush(*wxTRANSPARENT_BRUSH);
2493
2494 int w = i->GetWidth() + 2;
2495 int h = GetLineHeight(i) + 2;
2496
2497 dc.DrawRectangle( i->GetX() - 1, i->GetY() - 1, w, h);
2498 }
2499
2500 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId &item, bool below)
2501 {
2502 wxCHECK_RET( item.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2503
2504 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
2505
2506 wxClientDC dc(this);
2507 PrepareDC( dc );
2508 dc.SetLogicalFunction(wxINVERT);
2509
2510 int x = i->GetX(),
2511 y = i->GetY();
2512 if ( below )
2513 {
2514 y += GetLineHeight(i) - 1;
2515 }
2516
2517 dc.DrawLine( x, y, x + i->GetWidth(), y);
2518 }
2519
2520 // -----------------------------------------------------------------------------
2521 // wxWidgets callbacks
2522 // -----------------------------------------------------------------------------
2523
2524 void wxGenericTreeCtrl::OnPaint( wxPaintEvent &WXUNUSED(event) )
2525 {
2526 wxPaintDC dc(this);
2527 PrepareDC( dc );
2528
2529 if ( !m_anchor)
2530 return;
2531
2532 dc.SetFont( m_normalFont );
2533 dc.SetPen( m_dottedPen );
2534
2535 // this is now done dynamically
2536 //if(GetImageList() == NULL)
2537 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2538
2539 int y = 2;
2540 PaintLevel( m_anchor, dc, 0, y );
2541 }
2542
2543 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent &event )
2544 {
2545 m_hasFocus = true;
2546
2547 RefreshSelected();
2548
2549 event.Skip();
2550 }
2551
2552 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent &event )
2553 {
2554 m_hasFocus = false;
2555
2556 RefreshSelected();
2557
2558 event.Skip();
2559 }
2560
2561 void wxGenericTreeCtrl::OnChar( wxKeyEvent &event )
2562 {
2563 wxTreeEvent te( wxEVT_COMMAND_TREE_KEY_DOWN, this);
2564 te.m_evtKey = event;
2565 if ( GetEventHandler()->ProcessEvent( te ) )
2566 {
2567 // intercepted by the user code
2568 return;
2569 }
2570
2571 if ( (m_current == 0) || (m_key_current == 0) )
2572 {
2573 event.Skip();
2574 return;
2575 }
2576
2577 // how should the selection work for this event?
2578 bool is_multiple, extended_select, unselect_others;
2579 EventFlagsToSelType(GetWindowStyleFlag(),
2580 event.ShiftDown(),
2581 event.CmdDown(),
2582 is_multiple, extended_select, unselect_others);
2583
2584 if (GetLayoutDirection() == wxLayout_RightToLeft)
2585 {
2586 if (event.GetKeyCode() == WXK_RIGHT)
2587 event.m_keyCode = WXK_LEFT;
2588 else if (event.GetKeyCode() == WXK_LEFT)
2589 event.m_keyCode = WXK_RIGHT;
2590 }
2591
2592 // + : Expand
2593 // - : Collaspe
2594 // * : Expand all/Collapse all
2595 // ' ' | return : activate
2596 // up : go up (not last children!)
2597 // down : go down
2598 // left : go to parent
2599 // right : open if parent and go next
2600 // home : go to root
2601 // end : go to last item without opening parents
2602 // alnum : start or continue searching for the item with this prefix
2603 int keyCode = event.GetKeyCode();
2604 switch ( keyCode )
2605 {
2606 case '+':
2607 case WXK_ADD:
2608 if (m_current->HasPlus() && !IsExpanded(m_current))
2609 {
2610 Expand(m_current);
2611 }
2612 break;
2613
2614 case '*':
2615 case WXK_MULTIPLY:
2616 if ( !IsExpanded(m_current) )
2617 {
2618 // expand all
2619 ExpandAllChildren(m_current);
2620 break;
2621 }
2622 //else: fall through to Collapse() it
2623
2624 case '-':
2625 case WXK_SUBTRACT:
2626 if (IsExpanded(m_current))
2627 {
2628 Collapse(m_current);
2629 }
2630 break;
2631
2632 case WXK_MENU:
2633 {
2634 // Use the item's bounding rectangle to determine position for the event
2635 wxRect ItemRect;
2636 GetBoundingRect(m_current, ItemRect, true);
2637
2638 wxTreeEvent eventMenu(wxEVT_COMMAND_TREE_ITEM_MENU, this, m_current);
2639 // Use the left edge, vertical middle
2640 eventMenu.m_pointDrag = wxPoint(ItemRect.GetX(),
2641 ItemRect.GetY() + ItemRect.GetHeight() / 2);
2642 GetEventHandler()->ProcessEvent( eventMenu );
2643 }
2644 break;
2645
2646 case ' ':
2647 case WXK_RETURN:
2648 if ( !event.HasModifiers() )
2649 {
2650 wxTreeEvent eventAct(wxEVT_COMMAND_TREE_ITEM_ACTIVATED, this, m_current);
2651 GetEventHandler()->ProcessEvent( eventAct );
2652 }
2653
2654 // in any case, also generate the normal key event for this key,
2655 // even if we generated the ACTIVATED event above: this is what
2656 // wxMSW does and it makes sense because you might not want to
2657 // process ACTIVATED event at all and handle Space and Return
2658 // directly (and differently) which would be impossible otherwise
2659 event.Skip();
2660 break;
2661
2662 // up goes to the previous sibling or to the last
2663 // of its children if it's expanded
2664 case WXK_UP:
2665 {
2666 wxTreeItemId prev = GetPrevSibling( m_key_current );
2667 if (!prev)
2668 {
2669 prev = GetItemParent( m_key_current );
2670 if ((prev == GetRootItem()) && HasFlag(wxTR_HIDE_ROOT))
2671 {
2672 break; // don't go to root if it is hidden
2673 }
2674 if (prev)
2675 {
2676 wxTreeItemIdValue cookie;
2677 wxTreeItemId current = m_key_current;
2678 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2679 if (current == GetFirstChild( prev, cookie ))
2680 {
2681 // otherwise we return to where we came from
2682 DoSelectItem( prev, unselect_others, extended_select );
2683 m_key_current= (wxGenericTreeItem*) prev.m_pItem;
2684 break;
2685 }
2686 }
2687 }
2688 if (prev)
2689 {
2690 while ( IsExpanded(prev) && HasChildren(prev) )
2691 {
2692 wxTreeItemId child = GetLastChild(prev);
2693 if ( child )
2694 {
2695 prev = child;
2696 }
2697 }
2698
2699 DoSelectItem( prev, unselect_others, extended_select );
2700 m_key_current=(wxGenericTreeItem*) prev.m_pItem;
2701 }
2702 }
2703 break;
2704
2705 // left arrow goes to the parent
2706 case WXK_LEFT:
2707 {
2708 wxTreeItemId prev = GetItemParent( m_current );
2709 if ((prev == GetRootItem()) && HasFlag(wxTR_HIDE_ROOT))
2710 {
2711 // don't go to root if it is hidden
2712 prev = GetPrevSibling( m_current );
2713 }
2714 if (prev)
2715 {
2716 DoSelectItem( prev, unselect_others, extended_select );
2717 }
2718 }
2719 break;
2720
2721 case WXK_RIGHT:
2722 // this works the same as the down arrow except that we
2723 // also expand the item if it wasn't expanded yet
2724 Expand(m_current);
2725 // fall through
2726
2727 case WXK_DOWN:
2728 {
2729 if (IsExpanded(m_key_current) && HasChildren(m_key_current))
2730 {
2731 wxTreeItemIdValue cookie;
2732 wxTreeItemId child = GetFirstChild( m_key_current, cookie );
2733 DoSelectItem( child, unselect_others, extended_select );
2734 m_key_current=(wxGenericTreeItem*) child.m_pItem;
2735 }
2736 else
2737 {
2738 wxTreeItemId next = GetNextSibling( m_key_current );
2739 if (!next)
2740 {
2741 wxTreeItemId current = m_key_current;
2742 while (current.IsOk() && !next)
2743 {
2744 current = GetItemParent( current );
2745 if (current) next = GetNextSibling( current );
2746 }
2747 }
2748 if (next)
2749 {
2750 DoSelectItem( next, unselect_others, extended_select );
2751 m_key_current=(wxGenericTreeItem*) next.m_pItem;
2752 }
2753 }
2754 }
2755 break;
2756
2757 // <End> selects the last visible tree item
2758 case WXK_END:
2759 {
2760 wxTreeItemId last = GetRootItem();
2761
2762 while ( last.IsOk() && IsExpanded(last) )
2763 {
2764 wxTreeItemId lastChild = GetLastChild(last);
2765
2766 // it may happen if the item was expanded but then all of
2767 // its children have been deleted - so IsExpanded() returned
2768 // true, but GetLastChild() returned invalid item
2769 if ( !lastChild )
2770 break;
2771
2772 last = lastChild;
2773 }
2774
2775 if ( last.IsOk() )
2776 {
2777 DoSelectItem( last, unselect_others, extended_select );
2778 }
2779 }
2780 break;
2781
2782 // <Home> selects the root item
2783 case WXK_HOME:
2784 {
2785 wxTreeItemId prev = GetRootItem();
2786 if (!prev)
2787 break;
2788
2789 if ( HasFlag(wxTR_HIDE_ROOT) )
2790 {
2791 wxTreeItemIdValue cookie;
2792 prev = GetFirstChild(prev, cookie);
2793 if (!prev)
2794 break;
2795 }
2796
2797 DoSelectItem( prev, unselect_others, extended_select );
2798 }
2799 break;
2800
2801 default:
2802 // do not use wxIsalnum() here
2803 if ( !event.HasModifiers() &&
2804 ((keyCode >= '0' && keyCode <= '9') ||
2805 (keyCode >= 'a' && keyCode <= 'z') ||
2806 (keyCode >= 'A' && keyCode <= 'Z' )))
2807 {
2808 // find the next item starting with the given prefix
2809 wxChar ch = (wxChar)keyCode;
2810
2811 wxTreeItemId id = FindItem(m_current, m_findPrefix + ch);
2812 if ( !id.IsOk() )
2813 {
2814 // no such item
2815 break;
2816 }
2817
2818 SelectItem(id);
2819
2820 m_findPrefix += ch;
2821
2822 // also start the timer to reset the current prefix if the user
2823 // doesn't press any more alnum keys soon -- we wouldn't want
2824 // to use this prefix for a new item search
2825 if ( !m_findTimer )
2826 {
2827 m_findTimer = new wxTreeFindTimer(this);
2828 }
2829
2830 m_findTimer->Start(wxTreeFindTimer::DELAY, wxTIMER_ONE_SHOT);
2831 }
2832 else
2833 {
2834 event.Skip();
2835 }
2836 }
2837 }
2838
2839 wxTreeItemId
2840 wxGenericTreeCtrl::DoTreeHitTest(const wxPoint& point, int& flags) const
2841 {
2842 int w, h;
2843 GetSize(&w, &h);
2844 flags=0;
2845 if (point.x<0) flags |= wxTREE_HITTEST_TOLEFT;
2846 if (point.x>w) flags |= wxTREE_HITTEST_TORIGHT;
2847 if (point.y<0) flags |= wxTREE_HITTEST_ABOVE;
2848 if (point.y>h) flags |= wxTREE_HITTEST_BELOW;
2849 if (flags) return wxTreeItemId();
2850
2851 if (m_anchor == NULL)
2852 {
2853 flags = wxTREE_HITTEST_NOWHERE;
2854 return wxTreeItemId();
2855 }
2856
2857 wxGenericTreeItem *hit = m_anchor->HitTest(CalcUnscrolledPosition(point),
2858 this, flags, 0);
2859 if (hit == NULL)
2860 {
2861 flags = wxTREE_HITTEST_NOWHERE;
2862 return wxTreeItemId();
2863 }
2864 return hit;
2865 }
2866
2867 // get the bounding rectangle of the item (or of its label only)
2868 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId& item,
2869 wxRect& rect,
2870 bool textOnly) const
2871 {
2872 wxCHECK_MSG( item.IsOk(), false, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2873
2874 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
2875
2876 if ( textOnly )
2877 {
2878 rect.x = i->GetX();
2879 rect.width = i->GetWidth();
2880
2881 if ( m_imageListNormal )
2882 {
2883 int image_w, image_h;
2884 m_imageListNormal->GetSize( 0, image_w, image_h );
2885 rect.width += image_w + MARGIN_BETWEEN_IMAGE_AND_TEXT;
2886 }
2887 }
2888 else // the entire line
2889 {
2890 rect.x = 0;
2891 rect.width = GetClientSize().x;
2892 }
2893
2894 rect.y = i->GetY();
2895 rect.height = GetLineHeight(i);
2896
2897 // we have to return the logical coordinates, not physical ones
2898 rect.SetTopLeft(CalcScrolledPosition(rect.GetTopLeft()));
2899
2900 return true;
2901 }
2902
2903 wxTextCtrl *wxGenericTreeCtrl::EditLabel(const wxTreeItemId& item,
2904 wxClassInfo * WXUNUSED(textCtrlClass))
2905 {
2906 wxCHECK_MSG( item.IsOk(), NULL, _T("can't edit an invalid item") );
2907
2908 wxGenericTreeItem *itemEdit = (wxGenericTreeItem *)item.m_pItem;
2909
2910 wxTreeEvent te(wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, this, itemEdit);
2911 if ( GetEventHandler()->ProcessEvent( te ) && !te.IsAllowed() )
2912 {
2913 // vetoed by user
2914 return NULL;
2915 }
2916
2917 // We have to call this here because the label in
2918 // question might just have been added and no screen
2919 // update taken place.
2920 if ( m_dirty )
2921 #if defined( __WXMSW__ ) || defined(__WXMAC__)
2922 Update();
2923 #else
2924 DoDirtyProcessing();
2925 #endif
2926
2927 // TODO: use textCtrlClass here to create the control of correct class
2928 m_textCtrl = new wxTreeTextCtrl(this, itemEdit);
2929
2930 m_textCtrl->SetFocus();
2931
2932 return m_textCtrl;
2933 }
2934
2935 // returns a pointer to the text edit control if the item is being
2936 // edited, NULL otherwise (it's assumed that no more than one item may
2937 // be edited simultaneously)
2938 wxTextCtrl* wxGenericTreeCtrl::GetEditControl() const
2939 {
2940 return m_textCtrl;
2941 }
2942
2943 void wxGenericTreeCtrl::EndEditLabel(const wxTreeItemId& WXUNUSED(item),
2944 bool discardChanges)
2945 {
2946 wxCHECK_RET( m_textCtrl, _T("not editing label") );
2947
2948 m_textCtrl->EndEdit(discardChanges);
2949 }
2950
2951 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem *item,
2952 const wxString& value)
2953 {
2954 wxTreeEvent le(wxEVT_COMMAND_TREE_END_LABEL_EDIT, this, item);
2955 le.m_label = value;
2956 le.m_editCancelled = false;
2957
2958 return !GetEventHandler()->ProcessEvent( le ) || le.IsAllowed();
2959 }
2960
2961 void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem *item)
2962 {
2963 // let owner know that the edit was cancelled
2964 wxTreeEvent le(wxEVT_COMMAND_TREE_END_LABEL_EDIT, this, item);
2965 le.m_label = wxEmptyString;
2966 le.m_editCancelled = true;
2967
2968 GetEventHandler()->ProcessEvent( le );
2969 }
2970
2971 void wxGenericTreeCtrl::OnRenameTimer()
2972 {
2973 EditLabel( m_current );
2974 }
2975
2976 void wxGenericTreeCtrl::OnMouse( wxMouseEvent &event )
2977 {
2978 if ( !m_anchor )return;
2979
2980 wxPoint pt = CalcUnscrolledPosition(event.GetPosition());
2981
2982 // Is the mouse over a tree item button?
2983 int flags = 0;
2984 wxGenericTreeItem *thisItem = m_anchor->HitTest(pt, this, flags, 0);
2985 wxGenericTreeItem *underMouse = thisItem;
2986 #if wxUSE_TOOLTIPS
2987 bool underMouseChanged = (underMouse != m_underMouse) ;
2988 #endif // wxUSE_TOOLTIPS
2989
2990 if ((underMouse) &&
2991 (flags & wxTREE_HITTEST_ONITEMBUTTON) &&
2992 (!event.LeftIsDown()) &&
2993 (!m_isDragging) &&
2994 (!m_renameTimer || !m_renameTimer->IsRunning()))
2995 {
2996 }
2997 else
2998 {
2999 underMouse = NULL;
3000 }
3001
3002 if (underMouse != m_underMouse)
3003 {
3004 if (m_underMouse)
3005 {
3006 // unhighlight old item
3007 wxGenericTreeItem *tmp = m_underMouse;
3008 m_underMouse = NULL;
3009 RefreshLine( tmp );
3010 }
3011
3012 m_underMouse = underMouse;
3013 if (m_underMouse)
3014 RefreshLine( m_underMouse );
3015 }
3016
3017 #if wxUSE_TOOLTIPS
3018 // Determines what item we are hovering over and need a tooltip for
3019 wxTreeItemId hoverItem = thisItem;
3020
3021 // We do not want a tooltip if we are dragging, or if the rename timer is running
3022 if (underMouseChanged && hoverItem.IsOk() && !m_isDragging && (!m_renameTimer || !m_renameTimer->IsRunning()))
3023 {
3024 // Ask the tree control what tooltip (if any) should be shown
3025 wxTreeEvent hevent(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP, this, hoverItem);
3026
3027 if ( GetEventHandler()->ProcessEvent(hevent) && hevent.IsAllowed() )
3028 {
3029 SetToolTip(hevent.m_label);
3030 }
3031 }
3032 #endif
3033
3034 // we process left mouse up event (enables in-place edit), middle/right down
3035 // (pass to the user code), left dbl click (activate item) and
3036 // dragging/moving events for items drag-and-drop
3037 if ( !(event.LeftDown() ||
3038 event.LeftUp() ||
3039 event.MiddleDown() ||
3040 event.RightDown() ||
3041 event.LeftDClick() ||
3042 event.Dragging() ||
3043 ((event.Moving() || event.RightUp()) && m_isDragging)) )
3044 {
3045 event.Skip();
3046
3047 return;
3048 }
3049
3050
3051 flags = 0;
3052 wxGenericTreeItem *item = m_anchor->HitTest(pt, this, flags, 0);
3053
3054 if ( event.Dragging() && !m_isDragging )
3055 {
3056 if (m_dragCount == 0)
3057 m_dragStart = pt;
3058
3059 m_dragCount++;
3060
3061 if (m_dragCount != 3)
3062 {
3063 // wait until user drags a bit further...
3064 return;
3065 }
3066
3067 wxEventType command = event.RightIsDown()
3068 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
3069 : wxEVT_COMMAND_TREE_BEGIN_DRAG;
3070
3071 wxTreeEvent nevent(command, this, m_current);
3072 nevent.SetPoint(CalcScrolledPosition(pt));
3073
3074 // by default the dragging is not supported, the user code must
3075 // explicitly allow the event for it to take place
3076 nevent.Veto();
3077
3078 if ( GetEventHandler()->ProcessEvent(nevent) && nevent.IsAllowed() )
3079 {
3080 // we're going to drag this item
3081 m_isDragging = true;
3082
3083 // remember the old cursor because we will change it while
3084 // dragging
3085 m_oldCursor = m_cursor;
3086
3087 // in a single selection control, hide the selection temporarily
3088 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE) )
3089 {
3090 m_oldSelection = (wxGenericTreeItem*) GetSelection().m_pItem;
3091
3092 if ( m_oldSelection )
3093 {
3094 m_oldSelection->SetHilight(false);
3095 RefreshLine(m_oldSelection);
3096 }
3097 }
3098
3099 CaptureMouse();
3100 }
3101 }
3102 else if ( event.Dragging() )
3103 {
3104 if ( item != m_dropTarget )
3105 {
3106 // unhighlight the previous drop target
3107 DrawDropEffect(m_dropTarget);
3108
3109 m_dropTarget = item;
3110
3111 // highlight the current drop target if any
3112 DrawDropEffect(m_dropTarget);
3113
3114 #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXGTK20__)
3115 Update();
3116 #else
3117 wxYieldIfNeeded();
3118 #endif
3119 }
3120 }
3121 else if ( (event.LeftUp() || event.RightUp()) && m_isDragging )
3122 {
3123 ReleaseMouse();
3124
3125 // erase the highlighting
3126 DrawDropEffect(m_dropTarget);
3127
3128 if ( m_oldSelection )
3129 {
3130 m_oldSelection->SetHilight(true);
3131 RefreshLine(m_oldSelection);
3132 m_oldSelection = (wxGenericTreeItem *)NULL;
3133 }
3134
3135 // generate the drag end event
3136 wxTreeEvent eventEndDrag(wxEVT_COMMAND_TREE_END_DRAG, this, item);
3137
3138 eventEndDrag.m_pointDrag = CalcScrolledPosition(pt);
3139
3140 (void)GetEventHandler()->ProcessEvent(eventEndDrag);
3141
3142 m_isDragging = false;
3143 m_dropTarget = (wxGenericTreeItem *)NULL;
3144
3145 SetCursor(m_oldCursor);
3146
3147 #if defined( __WXMSW__ ) || defined(__WXMAC__)
3148 Update();
3149 #else
3150 wxYieldIfNeeded();
3151 #endif
3152 }
3153 else
3154 {
3155 // If we got to this point, we are not dragging or moving the mouse.
3156 // Because the code in carbon/toplevel.cpp will only set focus to the tree
3157 // if we skip for EVT_LEFT_DOWN, we MUST skip this event here for focus to work.
3158 // We skip even if we didn't hit an item because we still should
3159 // restore focus to the tree control even if we didn't exactly hit an item.
3160 if ( event.LeftDown() )
3161 {
3162 event.Skip();
3163 }
3164
3165 // here we process only the messages which happen on tree items
3166
3167 m_dragCount = 0;
3168
3169 if (item == NULL) return; /* we hit the blank area */
3170
3171 if ( event.RightDown() )
3172 {
3173 // If the item is already selected, do not update the selection.
3174 // Multi-selections should not be cleared if a selected item is clicked.
3175 if (!IsSelected(item))
3176 {
3177 DoSelectItem(item, true, false);
3178 }
3179
3180 wxTreeEvent nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK, this, item);
3181 nevent.m_pointDrag = CalcScrolledPosition(pt);
3182 event.Skip(!GetEventHandler()->ProcessEvent(nevent));
3183
3184 // Consistent with MSW (for now), send the ITEM_MENU *after*
3185 // the RIGHT_CLICK event. TODO: This behavior may change.
3186 wxTreeEvent nevent2(wxEVT_COMMAND_TREE_ITEM_MENU, this, item);
3187 nevent2.m_pointDrag = CalcScrolledPosition(pt);
3188 GetEventHandler()->ProcessEvent(nevent2);
3189 }
3190 else if ( event.MiddleDown() )
3191 {
3192 wxTreeEvent nevent(wxEVT_COMMAND_TREE_ITEM_MIDDLE_CLICK, this, item);
3193 nevent.m_pointDrag = CalcScrolledPosition(pt);
3194 event.Skip(!GetEventHandler()->ProcessEvent(nevent));
3195 }
3196 else if ( event.LeftUp() )
3197 {
3198 // this facilitates multiple-item drag-and-drop
3199
3200 if ( /* item && */ HasFlag(wxTR_MULTIPLE))
3201 {
3202 wxArrayTreeItemIds selections;
3203 size_t count = GetSelections(selections);
3204
3205 if (count > 1 &&
3206 !event.CmdDown() &&
3207 !event.ShiftDown())
3208 {
3209 DoSelectItem(item, true, false);
3210 }
3211 }
3212
3213 if ( m_lastOnSame )
3214 {
3215 if ( (item == m_current) &&
3216 (flags & wxTREE_HITTEST_ONITEMLABEL) &&
3217 HasFlag(wxTR_EDIT_LABELS) )
3218 {
3219 if ( m_renameTimer )
3220 {
3221 if ( m_renameTimer->IsRunning() )
3222 m_renameTimer->Stop();
3223 }
3224 else
3225 {
3226 m_renameTimer = new wxTreeRenameTimer( this );
3227 }
3228
3229 m_renameTimer->Start( wxTreeRenameTimer::DELAY, true );
3230 }
3231
3232 m_lastOnSame = false;
3233 }
3234 }
3235 else // !RightDown() && !MiddleDown() && !LeftUp() ==> LeftDown() || LeftDClick()
3236 {
3237 if ( event.LeftDown() )
3238 {
3239 m_lastOnSame = item == m_current;
3240 }
3241
3242 if ( flags & wxTREE_HITTEST_ONITEMBUTTON )
3243 {
3244 // only toggle the item for a single click, double click on
3245 // the button doesn't do anything (it toggles the item twice)
3246 if ( event.LeftDown() )
3247 {
3248 Toggle( item );
3249 }
3250
3251 // don't select the item if the button was clicked
3252 return;
3253 }
3254
3255
3256 // clear the previously selected items, if the
3257 // user clicked outside of the present selection.
3258 // otherwise, perform the deselection on mouse-up.
3259 // this allows multiple drag and drop to work.
3260 // but if Cmd is down, toggle selection of the clicked item
3261 if (!IsSelected(item) || event.CmdDown())
3262 {
3263 // how should the selection work for this event?
3264 bool is_multiple, extended_select, unselect_others;
3265 EventFlagsToSelType(GetWindowStyleFlag(),
3266 event.ShiftDown(),
3267 event.CmdDown(),
3268 is_multiple, extended_select, unselect_others);
3269
3270 DoSelectItem(item, unselect_others, extended_select);
3271 }
3272
3273
3274 // For some reason, Windows isn't recognizing a left double-click,
3275 // so we need to simulate it here. Allow 200 milliseconds for now.
3276 if ( event.LeftDClick() )
3277 {
3278 // double clicking should not start editing the item label
3279 if ( m_renameTimer )
3280 m_renameTimer->Stop();
3281
3282 m_lastOnSame = false;
3283
3284 // send activate event first
3285 wxTreeEvent nevent(wxEVT_COMMAND_TREE_ITEM_ACTIVATED, this, item);
3286 nevent.m_pointDrag = CalcScrolledPosition(pt);
3287 if ( !GetEventHandler()->ProcessEvent( nevent ) )
3288 {
3289 // if the user code didn't process the activate event,
3290 // handle it ourselves by toggling the item when it is
3291 // double clicked
3292 if ( item->HasPlus() )
3293 {
3294 Toggle(item);
3295 }
3296 }
3297 }
3298 }
3299 }
3300 }
3301
3302 void wxGenericTreeCtrl::OnInternalIdle()
3303 {
3304 wxWindow::OnInternalIdle();
3305
3306 // Check if we need to select the root item
3307 // because nothing else has been selected.
3308 // Delaying it means that we can invoke event handlers
3309 // as required, when a first item is selected.
3310 if (!HasFlag(wxTR_MULTIPLE) && !GetSelection().IsOk())
3311 {
3312 if (m_select_me)
3313 SelectItem(m_select_me);
3314 else if (GetRootItem().IsOk())
3315 SelectItem(GetRootItem());
3316 }
3317
3318 // after all changes have been done to the tree control,
3319 // actually redraw the tree when everything is over
3320 if (m_dirty)
3321 DoDirtyProcessing();
3322 }
3323
3324 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem *item, wxDC &dc )
3325 {
3326 wxCoord text_w = 0;
3327 wxCoord text_h = 0;
3328
3329 wxTreeItemAttr *attr = item->GetAttributes();
3330 if ( attr && attr->HasFont() )
3331 dc.SetFont(attr->GetFont());
3332 else if ( item->IsBold() )
3333 dc.SetFont(m_boldFont);
3334 else
3335 dc.SetFont(m_normalFont);
3336
3337 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
3338 text_h+=2;
3339
3340 // restore normal font
3341 dc.SetFont( m_normalFont );
3342
3343 int image_h = 0;
3344 int image_w = 0;
3345 int image = item->GetCurrentImage();
3346 if ( image != NO_IMAGE )
3347 {
3348 if ( m_imageListNormal )
3349 {
3350 m_imageListNormal->GetSize( image, image_w, image_h );
3351 image_w += MARGIN_BETWEEN_IMAGE_AND_TEXT;
3352 }
3353 }
3354
3355 int total_h = (image_h > text_h) ? image_h : text_h;
3356
3357 if (total_h < 30)
3358 total_h += 2; // at least 2 pixels
3359 else
3360 total_h += total_h/10; // otherwise 10% extra spacing
3361
3362 item->SetHeight(total_h);
3363 if (total_h>m_lineHeight)
3364 m_lineHeight=total_h;
3365
3366 item->SetWidth(image_w+text_w+2);
3367 }
3368
3369 // -----------------------------------------------------------------------------
3370 // for developper : y is now the top of the level
3371 // not the middle of it !
3372 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
3373 {
3374 int x = level*m_indent;
3375 if (!HasFlag(wxTR_HIDE_ROOT))
3376 {
3377 x += m_indent;
3378 }
3379 else if (level == 0)
3380 {
3381 // a hidden root is not evaluated, but its
3382 // children are always calculated
3383 goto Recurse;
3384 }
3385
3386 CalculateSize( item, dc );
3387
3388 // set its position
3389 item->SetX( x+m_spacing );
3390 item->SetY( y );
3391 y += GetLineHeight(item);
3392
3393 if ( !item->IsExpanded() )
3394 {
3395 // we don't need to calculate collapsed branches
3396 return;
3397 }
3398
3399 Recurse:
3400 wxArrayGenericTreeItems& children = item->GetChildren();
3401 size_t n, count = children.Count();
3402 ++level;
3403 for (n = 0; n < count; ++n )
3404 CalculateLevel( children[n], dc, level, y ); // recurse
3405 }
3406
3407 void wxGenericTreeCtrl::CalculatePositions()
3408 {
3409 if ( !m_anchor ) return;
3410
3411 wxClientDC dc(this);
3412 PrepareDC( dc );
3413
3414 dc.SetFont( m_normalFont );
3415
3416 dc.SetPen( m_dottedPen );
3417 //if(GetImageList() == NULL)
3418 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3419
3420 int y = 2;
3421 CalculateLevel( m_anchor, dc, 0, y ); // start recursion
3422 }
3423
3424 void wxGenericTreeCtrl::Refresh(bool eraseBackground, const wxRect *rect)
3425 {
3426 if ( !m_freezeCount )
3427 wxTreeCtrlBase::Refresh(eraseBackground, rect);
3428 }
3429
3430 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem *item)
3431 {
3432 if (m_dirty || m_freezeCount)
3433 return;
3434
3435 wxSize client = GetClientSize();
3436
3437 wxRect rect;
3438 CalcScrolledPosition(0, item->GetY(), NULL, &rect.y);
3439 rect.width = client.x;
3440 rect.height = client.y;
3441
3442 Refresh(true, &rect);
3443
3444 AdjustMyScrollbars();
3445 }
3446
3447 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem *item )
3448 {
3449 if (m_dirty || m_freezeCount)
3450 return;
3451
3452 wxRect rect;
3453 CalcScrolledPosition(0, item->GetY(), NULL, &rect.y);
3454 rect.width = GetClientSize().x;
3455 rect.height = GetLineHeight(item); //dc.GetCharHeight() + 6;
3456
3457 Refresh(true, &rect);
3458 }
3459
3460 void wxGenericTreeCtrl::RefreshSelected()
3461 {
3462 if (m_freezeCount)
3463 return;
3464
3465 // TODO: this is awfully inefficient, we should keep the list of all
3466 // selected items internally, should be much faster
3467 if ( m_anchor )
3468 RefreshSelectedUnder(m_anchor);
3469 }
3470
3471 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem *item)
3472 {
3473 if (m_freezeCount)
3474 return;
3475
3476 if ( item->IsSelected() )
3477 RefreshLine(item);
3478
3479 const wxArrayGenericTreeItems& children = item->GetChildren();
3480 size_t count = children.GetCount();
3481 for ( size_t n = 0; n < count; n++ )
3482 {
3483 RefreshSelectedUnder(children[n]);
3484 }
3485 }
3486
3487 void wxGenericTreeCtrl::Freeze()
3488 {
3489 m_freezeCount++;
3490 }
3491
3492 void wxGenericTreeCtrl::Thaw()
3493 {
3494 wxCHECK_RET( m_freezeCount > 0, _T("thawing unfrozen tree control?") );
3495
3496 if ( --m_freezeCount == 0 )
3497 {
3498 Refresh();
3499 }
3500 }
3501
3502 // ----------------------------------------------------------------------------
3503 // changing colours: we need to refresh the tree control
3504 // ----------------------------------------------------------------------------
3505
3506 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour& colour)
3507 {
3508 if ( !wxWindow::SetBackgroundColour(colour) )
3509 return false;
3510
3511 Refresh();
3512
3513 return true;
3514 }
3515
3516 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour& colour)
3517 {
3518 if ( !wxWindow::SetForegroundColour(colour) )
3519 return false;
3520
3521 Refresh();
3522
3523 return true;
3524 }
3525
3526 // Process the tooltip event, to speed up event processing.
3527 // Doesn't actually get a tooltip.
3528 void wxGenericTreeCtrl::OnGetToolTip( wxTreeEvent &event )
3529 {
3530 event.Veto();
3531 }
3532
3533
3534 // NOTE: If using the wxListBox visual attributes works everywhere then this can
3535 // be removed, as well as the #else case below.
3536 #define _USE_VISATTR 0
3537
3538 //static
3539 wxVisualAttributes
3540 #if _USE_VISATTR
3541 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant)
3542 #else
3543 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
3544 #endif
3545 {
3546 #if _USE_VISATTR
3547 // Use the same color scheme as wxListBox
3548 return wxListBox::GetClassDefaultAttributes(variant);
3549 #else
3550 wxVisualAttributes attr;
3551 attr.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
3552 attr.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX);
3553 attr.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
3554 return attr;
3555 #endif
3556 }
3557
3558 #if WXWIN_COMPATIBILITY_2_4
3559
3560 int wxGenericTreeCtrl::GetItemSelectedImage(const wxTreeItemId& item) const
3561 {
3562 return GetItemImage(item, wxTreeItemIcon_Selected);
3563 }
3564
3565 void wxGenericTreeCtrl::SetItemSelectedImage(const wxTreeItemId& item, int image)
3566 {
3567 SetItemImage(item, image, wxTreeItemIcon_Selected);
3568 }
3569
3570 #endif // WXWIN_COMPATIBILITY_2_4
3571
3572 void wxGenericTreeCtrl::DoDirtyProcessing()
3573 {
3574 if (m_freezeCount)
3575 return;
3576
3577 m_dirty = false;
3578
3579 CalculatePositions();
3580 Refresh();
3581 AdjustMyScrollbars();
3582 }
3583
3584 #endif // wxUSE_TREECTRL