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