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