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