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