/[pcsx2_0.9.7]/branch/r3113_0.9.7_beta/3rdparty/wxWidgets/src/generic/treectlg.cpp
ViewVC logotype

Contents of /branch/r3113_0.9.7_beta/3rdparty/wxWidgets/src/generic/treectlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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