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