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