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