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