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