]> git.saurik.com Git - wxWidgets.git/blob - src/generic/treectrl.cpp
0f45e548b7c38fa01250cd9ed5b4b0c1babf82e6
[wxWidgets.git] / src / generic / treectrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: treectrl.cpp
3 // Purpose: generic tree control implementation
4 // Author: Robert Roebling
5 // Created: 01/02/97
6 // Modified: 22/10/98 - almost total rewrite, simpler interface (VZ)
7 // Id: $Id$
8 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // =============================================================================
13 // declarations
14 // =============================================================================
15
16 // -----------------------------------------------------------------------------
17 // headers
18 // -----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "treectrl.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #include "wx/treectrl.h"
32 #include "wx/generic/imaglist.h"
33 #include "wx/settings.h"
34 #include "wx/log.h"
35 #include "wx/intl.h"
36 #include "wx/dynarray.h"
37 #include "wx/arrimpl.cpp"
38 #include "wx/dcclient.h"
39 #include "wx/msgdlg.h"
40
41 // -----------------------------------------------------------------------------
42 // array types
43 // -----------------------------------------------------------------------------
44
45 class WXDLLEXPORT wxGenericTreeItem;
46
47 WX_DEFINE_ARRAY(wxGenericTreeItem *, wxArrayGenericTreeItems);
48 WX_DEFINE_OBJARRAY(wxArrayTreeItemIds);
49
50 // ----------------------------------------------------------------------------
51 // constants
52 // ----------------------------------------------------------------------------
53
54 static const int NO_IMAGE = -1;
55
56 // -----------------------------------------------------------------------------
57 // private classes
58 // -----------------------------------------------------------------------------
59
60 // a tree item
61 class WXDLLEXPORT wxGenericTreeItem
62 {
63 public:
64 // ctors & dtor
65 wxGenericTreeItem() { m_data = NULL; }
66 wxGenericTreeItem( wxGenericTreeItem *parent,
67 const wxString& text,
68 wxDC& dc,
69 int image, int selImage,
70 wxTreeItemData *data );
71
72 ~wxGenericTreeItem();
73
74 // trivial accessors
75 wxArrayGenericTreeItems& GetChildren() { return m_children; }
76
77 const wxString& GetText() const { return m_text; }
78 int GetImage(wxTreeItemIcon which = wxTreeItemIcon_Normal) const
79 { return m_images[which]; }
80 wxTreeItemData *GetData() const { return m_data; }
81
82 // returns the current image for the item (depending on its
83 // selected/expanded/whatever state)
84 int GetCurrentImage() const;
85
86 void SetText( const wxString &text );
87 void SetImage(int image, wxTreeItemIcon which) { m_images[which] = image; }
88 void SetData(wxTreeItemData *data) { m_data = data; }
89
90 void SetHasPlus(bool has = TRUE) { m_hasPlus = has; }
91
92 void SetBold(bool bold) { m_isBold = bold; }
93
94 int GetX() const { return m_x; }
95 int GetY() const { return m_y; }
96
97 void SetX(int x) { m_x = x; }
98 void SetY(int y) { m_y = y; }
99
100 int GetHeight() const { return m_height; }
101 int GetWidth() const { return m_width; }
102
103 void SetHeight(int h) { m_height = h; }
104 void SetWidth(int w) { m_width = w; }
105
106
107 wxGenericTreeItem *GetParent() const { return m_parent; }
108
109 // operations
110 // deletes all children notifying the treectrl about it if !NULL
111 // pointer given
112 void DeleteChildren(wxTreeCtrl *tree = NULL);
113 // FIXME don't know what is it for
114 void Reset();
115
116 // get count of all children (and grand children if 'recursively')
117 size_t GetChildrenCount(bool recursively = TRUE) const;
118
119 void Insert(wxGenericTreeItem *child, size_t index)
120 { m_children.Insert(child, index); }
121
122 void SetCross( int x, int y );
123 void GetSize( int &x, int &y, const wxTreeCtrl* );
124
125 // return the item at given position (or NULL if no item), onButton is
126 // TRUE if the point belongs to the item's button, otherwise it lies
127 // on the button's label
128 wxGenericTreeItem *HitTest( const wxPoint& point, const wxTreeCtrl *, int &flags);
129
130 void Expand() { m_isCollapsed = FALSE; }
131 void Collapse() { m_isCollapsed = TRUE; }
132
133 void SetHilight( bool set = TRUE ) { m_hasHilight = set; }
134
135 // status inquiries
136 bool HasChildren() const { return !m_children.IsEmpty(); }
137 bool IsSelected() const { return m_hasHilight; }
138 bool IsExpanded() const { return !m_isCollapsed; }
139 bool HasPlus() const { return m_hasPlus || HasChildren(); }
140 bool IsBold() const { return m_isBold; }
141
142 // attributes
143 // get them - may be NULL
144 wxTreeItemAttr *GetAttributes() const { return m_attr; }
145 // get them ensuring that the pointer is not NULL
146 wxTreeItemAttr& Attr()
147 {
148 if ( !m_attr )
149 m_attr = new wxTreeItemAttr;
150
151 return *m_attr;
152 }
153
154 private:
155 wxString m_text;
156
157 // tree ctrl images for the normal, selected, expanded and
158 // expanded+selected states
159 int m_images[wxTreeItemIcon_Max];
160
161 wxTreeItemData *m_data;
162
163 // use bitfields to save size
164 int m_isCollapsed :1;
165 int m_hasHilight :1; // same as focused
166 int m_hasPlus :1; // used for item which doesn't have
167 // children but has a [+] button
168 int m_isBold :1; // render the label in bold font
169
170 wxCoord m_x, m_y;
171 wxCoord m_height, m_width;
172 int m_xCross, m_yCross;
173 int m_level;
174
175 wxArrayGenericTreeItems m_children;
176 wxGenericTreeItem *m_parent;
177
178 wxTreeItemAttr *m_attr;
179 };
180
181 // =============================================================================
182 // implementation
183 // =============================================================================
184
185 // ----------------------------------------------------------------------------
186 // private functions
187 // ----------------------------------------------------------------------------
188
189 // translate the key or mouse event flags to the type of selection we're
190 // dealing with
191 static void EventFlagsToSelType(long style,
192 bool shiftDown,
193 bool ctrlDown,
194 bool *is_multiple,
195 bool *extended_select,
196 bool *unselect_others)
197 {
198 *is_multiple = (style & wxTR_MULTIPLE) != 0;
199 *extended_select = shiftDown && is_multiple;
200 *unselect_others = !(extended_select || (ctrlDown && is_multiple));
201 }
202
203 // -----------------------------------------------------------------------------
204 // wxTreeRenameTimer (internal)
205 // -----------------------------------------------------------------------------
206
207 wxTreeRenameTimer::wxTreeRenameTimer( wxTreeCtrl *owner )
208 {
209 m_owner = owner;
210 }
211
212 void wxTreeRenameTimer::Notify()
213 {
214 m_owner->OnRenameTimer();
215 }
216
217 //-----------------------------------------------------------------------------
218 // wxTreeTextCtrl (internal)
219 //-----------------------------------------------------------------------------
220
221 IMPLEMENT_DYNAMIC_CLASS(wxTreeTextCtrl,wxTextCtrl);
222
223 BEGIN_EVENT_TABLE(wxTreeTextCtrl,wxTextCtrl)
224 EVT_CHAR (wxTreeTextCtrl::OnChar)
225 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus)
226 END_EVENT_TABLE()
227
228 wxTreeTextCtrl::wxTreeTextCtrl( wxWindow *parent,
229 const wxWindowID id,
230 bool *accept,
231 wxString *res,
232 wxTreeCtrl *owner,
233 const wxString &value,
234 const wxPoint &pos,
235 const wxSize &size,
236 int style,
237 const wxValidator& validator,
238 const wxString &name )
239 : wxTextCtrl( parent, id, value, pos, size, style, validator, name )
240 {
241 m_res = res;
242 m_accept = accept;
243 m_owner = owner;
244 (*m_accept) = FALSE;
245 (*m_res) = "";
246 m_startValue = value;
247 }
248
249 void wxTreeTextCtrl::OnChar( wxKeyEvent &event )
250 {
251 if (event.m_keyCode == WXK_RETURN)
252 {
253 (*m_accept) = TRUE;
254 (*m_res) = GetValue();
255 m_owner->SetFocus();
256 return;
257 }
258 if (event.m_keyCode == WXK_ESCAPE)
259 {
260 (*m_accept) = FALSE;
261 (*m_res) = "";
262 m_owner->SetFocus();
263 return;
264 }
265 event.Skip();
266 }
267
268 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
269 {
270 if (wxPendingDelete.Member(this)) return;
271
272 wxPendingDelete.Append(this);
273
274 if ((*m_accept) && ((*m_res) != m_startValue))
275 m_owner->OnRenameAccept();
276 }
277
278 #define PIXELS_PER_UNIT 10
279 // -----------------------------------------------------------------------------
280 // wxTreeEvent
281 // -----------------------------------------------------------------------------
282
283 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent, wxNotifyEvent)
284
285 wxTreeEvent::wxTreeEvent( wxEventType commandType, int id )
286 : wxNotifyEvent( commandType, id )
287 {
288 m_code = 0;
289 m_itemOld = (wxGenericTreeItem *)NULL;
290 }
291
292 // -----------------------------------------------------------------------------
293 // wxGenericTreeItem
294 // -----------------------------------------------------------------------------
295
296 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem *parent,
297 const wxString& text,
298 wxDC& WXUNUSED(dc),
299 int image, int selImage,
300 wxTreeItemData *data)
301 : m_text(text)
302 {
303 m_images[wxTreeItemIcon_Normal] = image;
304 m_images[wxTreeItemIcon_Selected] = selImage;
305 m_images[wxTreeItemIcon_Expanded] = NO_IMAGE;
306 m_images[wxTreeItemIcon_SelectedExpanded] = NO_IMAGE;
307
308 m_data = data;
309 m_x = m_y = 0;
310 m_xCross = m_yCross = 0;
311
312 m_level = 0;
313
314 m_isCollapsed = TRUE;
315 m_hasHilight = FALSE;
316 m_hasPlus = FALSE;
317 m_isBold = FALSE;
318
319 m_parent = parent;
320
321 m_attr = (wxTreeItemAttr *)NULL;
322
323 // We don't know the height here yet.
324 m_width = 0;
325 m_height = 0;
326 }
327
328 wxGenericTreeItem::~wxGenericTreeItem()
329 {
330 delete m_data;
331
332 delete m_attr;
333
334 wxASSERT_MSG( m_children.IsEmpty(),
335 wxT("please call DeleteChildren() before deleting the item") );
336 }
337
338 void wxGenericTreeItem::DeleteChildren(wxTreeCtrl *tree)
339 {
340 size_t count = m_children.Count();
341 for ( size_t n = 0; n < count; n++ )
342 {
343 wxGenericTreeItem *child = m_children[n];
344 if (tree)
345 tree->SendDeleteEvent(child);
346
347 child->DeleteChildren(tree);
348 delete child;
349 }
350
351 m_children.Empty();
352 }
353
354 void wxGenericTreeItem::SetText( const wxString &text )
355 {
356 m_text = text;
357 }
358
359 void wxGenericTreeItem::Reset()
360 {
361 m_text.Empty();
362 for ( int i = 0; i < wxTreeItemIcon_Max; i++ )
363 {
364 m_images[i] = NO_IMAGE;
365 }
366
367 m_data = NULL;
368 m_x = m_y =
369 m_height = m_width = 0;
370 m_xCross =
371 m_yCross = 0;
372
373 m_level = 0;
374
375 DeleteChildren();
376 m_isCollapsed = TRUE;
377
378 m_parent = (wxGenericTreeItem *)NULL;
379 }
380
381 size_t wxGenericTreeItem::GetChildrenCount(bool recursively) const
382 {
383 size_t count = m_children.Count();
384 if ( !recursively )
385 return count;
386
387 size_t total = count;
388 for (size_t n = 0; n < count; ++n)
389 {
390 total += m_children[n]->GetChildrenCount();
391 }
392
393 return total;
394 }
395
396 void wxGenericTreeItem::SetCross( int x, int y )
397 {
398 m_xCross = x;
399 m_yCross = y;
400 }
401
402 void wxGenericTreeItem::GetSize( int &x, int &y, const wxTreeCtrl *theTree )
403 {
404 int bottomY=m_y+theTree->GetLineHeight(this);
405 if ( y < bottomY ) y = bottomY;
406 int width = m_x + m_width;
407 if ( x < width ) x = width;
408
409 if (IsExpanded())
410 {
411 size_t count = m_children.Count();
412 for ( size_t n = 0; n < count; ++n )
413 {
414 m_children[n]->GetSize( x, y, theTree );
415 }
416 }
417 }
418
419 wxGenericTreeItem *wxGenericTreeItem::HitTest( const wxPoint& point,
420 const wxTreeCtrl *theTree,
421 int &flags)
422 {
423 if ((point.y > m_y) && (point.y < m_y + theTree->GetLineHeight(this)))
424 {
425 if (point.y<m_y+theTree->GetLineHeight(this)/2)
426 flags |= wxTREE_HITTEST_ONITEMUPPERPART;
427 else
428 flags |= wxTREE_HITTEST_ONITEMLOWERPART;
429
430 // 5 is the size of the plus sign
431 if ((point.x > m_xCross-5) && (point.x < m_xCross+5) &&
432 (point.y > m_yCross-5) && (point.y < m_yCross+5) &&
433 (IsExpanded() || HasPlus()))
434 {
435 flags|=wxTREE_HITTEST_ONITEMBUTTON;
436 return this;
437 }
438
439 if ((point.x >= m_x) && (point.x <= m_x+m_width))
440 {
441 int image_w = -1;
442 int image_h;
443
444 // assuming every image (normal and selected ) has the same size !
445 if ( (GetImage() != NO_IMAGE) && theTree->m_imageListNormal )
446 theTree->m_imageListNormal->GetSize(GetImage(), image_w, image_h);
447
448 if ((image_w != -1) && (point.x <= m_x + image_w + 1))
449 flags |= wxTREE_HITTEST_ONITEMICON;
450 else
451 flags |= wxTREE_HITTEST_ONITEMLABEL;
452
453 return this;
454 }
455
456 if (point.x < m_x)
457 flags |= wxTREE_HITTEST_ONITEMINDENT;
458 if (point.x > m_x+m_width)
459 flags |= wxTREE_HITTEST_ONITEMRIGHT;
460
461 return this;
462 }
463 else
464 {
465 if (!m_isCollapsed)
466 {
467 size_t count = m_children.Count();
468 for ( size_t n = 0; n < count; n++ )
469 {
470 wxGenericTreeItem *res = m_children[n]->HitTest( point, theTree, flags );
471 if ( res != NULL )
472 return res;
473 }
474 }
475 }
476
477 flags|=wxTREE_HITTEST_NOWHERE;
478
479 return (wxGenericTreeItem*) NULL;
480 }
481
482 int wxGenericTreeItem::GetCurrentImage() const
483 {
484 int image = NO_IMAGE;
485 if ( IsExpanded() )
486 {
487 if ( IsSelected() )
488 {
489 image = GetImage(wxTreeItemIcon_SelectedExpanded);
490 }
491
492 if ( image == NO_IMAGE )
493 {
494 // we usually fall back to the normal item, but try just the
495 // expanded one (and not selected) first in this case
496 image = GetImage(wxTreeItemIcon_Expanded);
497 }
498 }
499 else // not expanded
500 {
501 if ( IsSelected() )
502 image = GetImage(wxTreeItemIcon_Selected);
503 }
504
505 // may be it doesn't have the specific image we want, try the default one
506 // instead
507 if ( image == NO_IMAGE )
508 {
509 image = GetImage();
510 }
511
512 return image;
513 }
514
515 // -----------------------------------------------------------------------------
516 // wxTreeCtrl implementation
517 // -----------------------------------------------------------------------------
518
519 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxScrolledWindow)
520
521 BEGIN_EVENT_TABLE(wxTreeCtrl,wxScrolledWindow)
522 EVT_PAINT (wxTreeCtrl::OnPaint)
523 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse)
524 EVT_CHAR (wxTreeCtrl::OnChar)
525 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus)
526 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus)
527 EVT_IDLE (wxTreeCtrl::OnIdle)
528 END_EVENT_TABLE()
529
530 // -----------------------------------------------------------------------------
531 // construction/destruction
532 // -----------------------------------------------------------------------------
533
534 void wxTreeCtrl::Init()
535 {
536 m_current =
537 m_key_current =
538 m_anchor = (wxGenericTreeItem *) NULL;
539 m_hasFocus = FALSE;
540 m_dirty = FALSE;
541
542 m_xScroll = 0;
543 m_yScroll = 0;
544 m_lineHeight = 10;
545 m_indent = 15;
546 m_spacing = 18;
547
548 m_hilightBrush = new wxBrush
549 (
550 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT),
551 wxSOLID
552 );
553
554 m_imageListNormal =
555 m_imageListState = (wxImageList *) NULL;
556
557 m_dragCount = 0;
558
559 m_renameTimer = new wxTreeRenameTimer( this );
560
561 m_normalFont = wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT );
562 m_boldFont = wxFont( m_normalFont.GetPointSize(),
563 m_normalFont.GetFamily(),
564 m_normalFont.GetStyle(),
565 wxBOLD,
566 m_normalFont.GetUnderlined());
567 }
568
569 bool wxTreeCtrl::Create(wxWindow *parent, wxWindowID id,
570 const wxPoint& pos, const wxSize& size,
571 long style,
572 const wxValidator &validator,
573 const wxString& name )
574 {
575 Init();
576
577 wxScrolledWindow::Create( parent, id, pos, size, style|wxHSCROLL|wxVSCROLL, name );
578
579 #if wxUSE_VALIDATORS
580 SetValidator( validator );
581 #endif
582
583 SetBackgroundColour( *wxWHITE );
584 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
585 m_dottedPen = wxPen( "grey", 0, 0 );
586
587 return TRUE;
588 }
589
590 wxTreeCtrl::~wxTreeCtrl()
591 {
592 wxDELETE( m_hilightBrush );
593
594 DeleteAllItems();
595
596 delete m_renameTimer;
597 }
598
599 // -----------------------------------------------------------------------------
600 // accessors
601 // -----------------------------------------------------------------------------
602
603 size_t wxTreeCtrl::GetCount() const
604 {
605 return m_anchor == NULL ? 0u : m_anchor->GetChildrenCount();
606 }
607
608 void wxTreeCtrl::SetIndent(unsigned int indent)
609 {
610 m_indent = indent;
611 m_dirty = TRUE;
612 }
613
614 void wxTreeCtrl::SetSpacing(unsigned int spacing)
615 {
616 m_spacing = spacing;
617 m_dirty = TRUE;
618 }
619
620 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item, bool recursively)
621 {
622 wxCHECK_MSG( item.IsOk(), 0u, wxT("invalid tree item") );
623
624 return item.m_pItem->GetChildrenCount(recursively);
625 }
626
627 // -----------------------------------------------------------------------------
628 // functions to work with tree items
629 // -----------------------------------------------------------------------------
630
631 wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const
632 {
633 wxCHECK_MSG( item.IsOk(), wxT(""), wxT("invalid tree item") );
634
635 return item.m_pItem->GetText();
636 }
637
638 int wxTreeCtrl::GetItemImage(const wxTreeItemId& item,
639 wxTreeItemIcon which) const
640 {
641 wxCHECK_MSG( item.IsOk(), -1, wxT("invalid tree item") );
642
643 return item.m_pItem->GetImage(which);
644 }
645
646 wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const
647 {
648 wxCHECK_MSG( item.IsOk(), NULL, wxT("invalid tree item") );
649
650 return item.m_pItem->GetData();
651 }
652
653 void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text)
654 {
655 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
656
657 wxClientDC dc(this);
658 wxGenericTreeItem *pItem = item.m_pItem;
659 pItem->SetText(text);
660 CalculateSize(pItem, dc);
661 RefreshLine(pItem);
662 }
663
664 void wxTreeCtrl::SetItemImage(const wxTreeItemId& item,
665 int image,
666 wxTreeItemIcon which)
667 {
668 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
669
670 wxGenericTreeItem *pItem = item.m_pItem;
671 pItem->SetImage(image, which);
672
673 wxClientDC dc(this);
674 CalculateSize(pItem, dc);
675 RefreshLine(pItem);
676 }
677
678 void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
679 {
680 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
681
682 item.m_pItem->SetData(data);
683 }
684
685 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
686 {
687 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
688
689 wxGenericTreeItem *pItem = item.m_pItem;
690 pItem->SetHasPlus(has);
691 RefreshLine(pItem);
692 }
693
694 void wxTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
695 {
696 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
697
698 // avoid redrawing the tree if no real change
699 wxGenericTreeItem *pItem = item.m_pItem;
700 if ( pItem->IsBold() != bold )
701 {
702 pItem->SetBold(bold);
703 RefreshLine(pItem);
704 }
705 }
706
707 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId& item,
708 const wxColour& col)
709 {
710 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
711
712 wxGenericTreeItem *pItem = item.m_pItem;
713 pItem->Attr().SetTextColour(col);
714 RefreshLine(pItem);
715 }
716
717 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId& item,
718 const wxColour& col)
719 {
720 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
721
722 wxGenericTreeItem *pItem = item.m_pItem;
723 pItem->Attr().SetBackgroundColour(col);
724 RefreshLine(pItem);
725 }
726
727 void wxTreeCtrl::SetItemFont(const wxTreeItemId& item, const wxFont& font)
728 {
729 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
730
731 wxGenericTreeItem *pItem = item.m_pItem;
732 pItem->Attr().SetFont(font);
733 RefreshLine(pItem);
734 }
735
736 // -----------------------------------------------------------------------------
737 // item status inquiries
738 // -----------------------------------------------------------------------------
739
740 bool wxTreeCtrl::IsVisible(const wxTreeItemId& WXUNUSED(item)) const
741 {
742 wxFAIL_MSG(wxT("not implemented"));
743
744 return TRUE;
745 }
746
747 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
748 {
749 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
750
751 return !item.m_pItem->GetChildren().IsEmpty();
752 }
753
754 bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const
755 {
756 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
757
758 return item.m_pItem->IsExpanded();
759 }
760
761 bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const
762 {
763 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
764
765 return item.m_pItem->IsSelected();
766 }
767
768 bool wxTreeCtrl::IsBold(const wxTreeItemId& item) const
769 {
770 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
771
772 return item.m_pItem->IsBold();
773 }
774
775 // -----------------------------------------------------------------------------
776 // navigation
777 // -----------------------------------------------------------------------------
778
779 wxTreeItemId wxTreeCtrl::GetParent(const wxTreeItemId& item) const
780 {
781 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
782
783 return item.m_pItem->GetParent();
784 }
785
786 wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item, long& cookie) const
787 {
788 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
789
790 cookie = 0;
791 return GetNextChild(item, cookie);
792 }
793
794 wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& item, long& cookie) const
795 {
796 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
797
798 wxArrayGenericTreeItems& children = item.m_pItem->GetChildren();
799 if ( (size_t)cookie < children.Count() )
800 {
801 return children.Item((size_t)cookie++);
802 }
803 else
804 {
805 // there are no more of them
806 return wxTreeItemId();
807 }
808 }
809
810 wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const
811 {
812 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
813
814 wxArrayGenericTreeItems& children = item.m_pItem->GetChildren();
815 return (children.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children.Last()));
816 }
817
818 wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
819 {
820 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
821
822 wxGenericTreeItem *i = item.m_pItem;
823 wxGenericTreeItem *parent = i->GetParent();
824 if ( parent == NULL )
825 {
826 // root item doesn't have any siblings
827 return wxTreeItemId();
828 }
829
830 wxArrayGenericTreeItems& siblings = parent->GetChildren();
831 int index = siblings.Index(i);
832 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
833
834 size_t n = (size_t)(index + 1);
835 return n == siblings.Count() ? wxTreeItemId() : wxTreeItemId(siblings[n]);
836 }
837
838 wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
839 {
840 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
841
842 wxGenericTreeItem *i = item.m_pItem;
843 wxGenericTreeItem *parent = i->GetParent();
844 if ( parent == NULL )
845 {
846 // root item doesn't have any siblings
847 return wxTreeItemId();
848 }
849
850 wxArrayGenericTreeItems& siblings = parent->GetChildren();
851 int index = siblings.Index(i);
852 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
853
854 return index == 0 ? wxTreeItemId()
855 : wxTreeItemId(siblings[(size_t)(index - 1)]);
856 }
857
858 wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const
859 {
860 wxFAIL_MSG(wxT("not implemented"));
861
862 return wxTreeItemId();
863 }
864
865 wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
866 {
867 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
868
869 wxFAIL_MSG(wxT("not implemented"));
870
871 return wxTreeItemId();
872 }
873
874 wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
875 {
876 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
877
878 wxFAIL_MSG(wxT("not implemented"));
879
880 return wxTreeItemId();
881 }
882
883 // -----------------------------------------------------------------------------
884 // operations
885 // -----------------------------------------------------------------------------
886
887 wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parentId,
888 size_t previous,
889 const wxString& text,
890 int image, int selImage,
891 wxTreeItemData *data)
892 {
893 wxGenericTreeItem *parent = parentId.m_pItem;
894 if ( !parent )
895 {
896 // should we give a warning here?
897 return AddRoot(text, image, selImage, data);
898 }
899
900 wxClientDC dc(this);
901 wxGenericTreeItem *item =
902 new wxGenericTreeItem( parent, text, dc, image, selImage, data );
903
904 if ( data != NULL )
905 {
906 data->m_pItem = item;
907 }
908
909 parent->Insert( item, previous );
910
911 m_dirty = TRUE;
912
913 return item;
914 }
915
916 wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text,
917 int image, int selImage,
918 wxTreeItemData *data)
919 {
920 wxCHECK_MSG( !m_anchor, wxTreeItemId(), wxT("tree can have only one root") );
921
922 wxClientDC dc(this);
923 m_anchor = new wxGenericTreeItem((wxGenericTreeItem *)NULL, text, dc,
924 image, selImage, data);
925 if ( data != NULL )
926 {
927 data->m_pItem = m_anchor;
928 }
929
930 if (!HasFlag(wxTR_MULTIPLE))
931 {
932 m_current = m_key_current = m_anchor;
933 m_current->SetHilight( TRUE );
934 }
935
936 Refresh();
937 AdjustMyScrollbars();
938
939 return m_anchor;
940 }
941
942 wxTreeItemId wxTreeCtrl::PrependItem(const wxTreeItemId& parent,
943 const wxString& text,
944 int image, int selImage,
945 wxTreeItemData *data)
946 {
947 return DoInsertItem(parent, 0u, text, image, selImage, data);
948 }
949
950 wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parentId,
951 const wxTreeItemId& idPrevious,
952 const wxString& text,
953 int image, int selImage,
954 wxTreeItemData *data)
955 {
956 wxGenericTreeItem *parent = parentId.m_pItem;
957 if ( !parent )
958 {
959 // should we give a warning here?
960 return AddRoot(text, image, selImage, data);
961 }
962
963 int index = parent->GetChildren().Index(idPrevious.m_pItem);
964 wxASSERT_MSG( index != wxNOT_FOUND,
965 wxT("previous item in wxTreeCtrl::InsertItem() is not a sibling") );
966
967 return DoInsertItem(parentId, (size_t)++index, text, image, selImage, data);
968 }
969
970 wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parentId,
971 size_t before,
972 const wxString& text,
973 int image, int selImage,
974 wxTreeItemData *data)
975 {
976 wxGenericTreeItem *parent = parentId.m_pItem;
977 if ( !parent )
978 {
979 // should we give a warning here?
980 return AddRoot(text, image, selImage, data);
981 }
982
983 return DoInsertItem(parentId, before, text, image, selImage, data);
984 }
985
986 wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parentId,
987 const wxString& text,
988 int image, int selImage,
989 wxTreeItemData *data)
990 {
991 wxGenericTreeItem *parent = parentId.m_pItem;
992 if ( !parent )
993 {
994 // should we give a warning here?
995 return AddRoot(text, image, selImage, data);
996 }
997
998 return DoInsertItem( parent, parent->GetChildren().Count(), text,
999 image, selImage, data);
1000 }
1001
1002 void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem *item)
1003 {
1004 wxTreeEvent event( wxEVT_COMMAND_TREE_DELETE_ITEM, GetId() );
1005 event.m_item = item;
1006 event.SetEventObject( this );
1007 ProcessEvent( event );
1008 }
1009
1010 void wxTreeCtrl::DeleteChildren(const wxTreeItemId& itemId)
1011 {
1012 wxGenericTreeItem *item = itemId.m_pItem;
1013 item->DeleteChildren(this);
1014
1015 m_dirty = TRUE;
1016 }
1017
1018 void wxTreeCtrl::Delete(const wxTreeItemId& itemId)
1019 {
1020 wxGenericTreeItem *item = itemId.m_pItem;
1021 wxGenericTreeItem *parent = item->GetParent();
1022
1023 if ( parent )
1024 {
1025 parent->GetChildren().Remove( item ); // remove by value
1026 }
1027
1028 item->DeleteChildren(this);
1029 SendDeleteEvent(item);
1030 delete item;
1031
1032 m_dirty = TRUE;
1033 }
1034
1035 void wxTreeCtrl::DeleteAllItems()
1036 {
1037 if ( m_anchor )
1038 {
1039 m_anchor->DeleteChildren(this);
1040 delete m_anchor;
1041
1042 m_anchor = NULL;
1043
1044 m_dirty = TRUE;
1045 }
1046 }
1047
1048 void wxTreeCtrl::Expand(const wxTreeItemId& itemId)
1049 {
1050 wxGenericTreeItem *item = itemId.m_pItem;
1051
1052 if ( !item->HasPlus() )
1053 return;
1054
1055 if ( item->IsExpanded() )
1056 return;
1057
1058 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_EXPANDING, GetId() );
1059 event.m_item = item;
1060 event.SetEventObject( this );
1061
1062 // if ( ProcessEvent( event ) && event.m_code ) TODO: Was this a typo ?
1063 if ( ProcessEvent( event ) && !event.IsAllowed() )
1064 {
1065 // cancelled by program
1066 return;
1067 }
1068
1069 item->Expand();
1070 CalculatePositions();
1071
1072 RefreshSubtree(item);
1073
1074 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED);
1075 ProcessEvent( event );
1076 }
1077
1078 void wxTreeCtrl::Collapse(const wxTreeItemId& itemId)
1079 {
1080 wxGenericTreeItem *item = itemId.m_pItem;
1081
1082 if ( !item->IsExpanded() )
1083 return;
1084
1085 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING, GetId() );
1086 event.m_item = item;
1087 event.SetEventObject( this );
1088 if ( ProcessEvent( event ) && !event.IsAllowed() )
1089 {
1090 // cancelled by program
1091 return;
1092 }
1093
1094 item->Collapse();
1095
1096 wxArrayGenericTreeItems& children = item->GetChildren();
1097 size_t count = children.Count();
1098 for ( size_t n = 0; n < count; n++ )
1099 {
1100 Collapse(children[n]);
1101 }
1102
1103 CalculatePositions();
1104
1105 RefreshSubtree(item);
1106
1107 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED);
1108 ProcessEvent( event );
1109 }
1110
1111 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
1112 {
1113 Collapse(item);
1114 DeleteChildren(item);
1115 }
1116
1117 void wxTreeCtrl::Toggle(const wxTreeItemId& itemId)
1118 {
1119 wxGenericTreeItem *item = itemId.m_pItem;
1120
1121 if (item->IsExpanded())
1122 Collapse(itemId);
1123 else
1124 Expand(itemId);
1125 }
1126
1127 void wxTreeCtrl::Unselect()
1128 {
1129 if (m_current)
1130 {
1131 m_current->SetHilight( FALSE );
1132 RefreshLine( m_current );
1133 }
1134 }
1135
1136 void wxTreeCtrl::UnselectAllChildren(wxGenericTreeItem *item)
1137 {
1138 if (item->IsSelected())
1139 {
1140 item->SetHilight(FALSE);
1141 RefreshLine(item);
1142 }
1143
1144 if (item->HasChildren())
1145 {
1146 wxArrayGenericTreeItems& children = item->GetChildren();
1147 size_t count = children.Count();
1148 for ( size_t n = 0; n < count; ++n )
1149 {
1150 UnselectAllChildren(children[n]);
1151 }
1152 }
1153 }
1154
1155 void wxTreeCtrl::UnselectAll()
1156 {
1157 UnselectAllChildren(GetRootItem().m_pItem);
1158 }
1159
1160 // Recursive function !
1161 // To stop we must have crt_item<last_item
1162 // Algorithm :
1163 // Tag all next children, when no more children,
1164 // Move to parent (not to tag)
1165 // Keep going... if we found last_item, we stop.
1166 bool wxTreeCtrl::TagNextChildren(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select)
1167 {
1168 wxGenericTreeItem *parent = crt_item->GetParent();
1169
1170 if (parent == NULL) // This is root item
1171 return TagAllChildrenUntilLast(crt_item, last_item, select);
1172
1173 wxArrayGenericTreeItems& children = parent->GetChildren();
1174 int index = children.Index(crt_item);
1175 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
1176
1177 size_t count = children.Count();
1178 for (size_t n=(size_t)(index+1); n<count; ++n)
1179 {
1180 if (TagAllChildrenUntilLast(children[n], last_item, select)) return TRUE;
1181 }
1182
1183 return TagNextChildren(parent, last_item, select);
1184 }
1185
1186 bool wxTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select)
1187 {
1188 crt_item->SetHilight(select);
1189 RefreshLine(crt_item);
1190
1191 if (crt_item==last_item)
1192 return TRUE;
1193
1194 if (crt_item->HasChildren())
1195 {
1196 wxArrayGenericTreeItems& children = crt_item->GetChildren();
1197 size_t count = children.Count();
1198 for ( size_t n = 0; n < count; ++n )
1199 {
1200 if (TagAllChildrenUntilLast(children[n], last_item, select))
1201 return TRUE;
1202 }
1203 }
1204
1205 return FALSE;
1206 }
1207
1208 void wxTreeCtrl::SelectItemRange(wxGenericTreeItem *item1, wxGenericTreeItem *item2)
1209 {
1210 // item2 is not necessary after item1
1211 wxGenericTreeItem *first=NULL, *last=NULL;
1212
1213 // choice first' and 'last' between item1 and item2
1214 if (item1->GetY()<item2->GetY())
1215 {
1216 first=item1;
1217 last=item2;
1218 }
1219 else
1220 {
1221 first=item2;
1222 last=item1;
1223 }
1224
1225 bool select = m_current->IsSelected();
1226
1227 if ( TagAllChildrenUntilLast(first,last,select) )
1228 return;
1229
1230 TagNextChildren(first,last,select);
1231 }
1232
1233 void wxTreeCtrl::SelectItem(const wxTreeItemId& itemId,
1234 bool unselect_others,
1235 bool extended_select)
1236 {
1237 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
1238
1239 bool is_single=!(GetWindowStyleFlag() & wxTR_MULTIPLE);
1240 wxGenericTreeItem *item = itemId.m_pItem;
1241
1242 //wxCHECK_RET( ( (!unselect_others) && is_single),
1243 // wxT("this is a single selection tree") );
1244
1245 // to keep going anyhow !!!
1246 if (is_single)
1247 {
1248 if (item->IsSelected())
1249 return; // nothing to do
1250 unselect_others = TRUE;
1251 extended_select = FALSE;
1252 }
1253 else if ( unselect_others && item->IsSelected() )
1254 {
1255 // selection change if there is more than one item currently selected
1256 wxArrayTreeItemIds selected_items;
1257 if ( GetSelections(selected_items) == 1 )
1258 return;
1259 }
1260
1261 wxTreeEvent event( wxEVT_COMMAND_TREE_SEL_CHANGING, GetId() );
1262 event.m_item = item;
1263 event.m_itemOld = m_current;
1264 event.SetEventObject( this );
1265 // TODO : Here we don't send any selection mode yet !
1266
1267 if ( GetEventHandler()->ProcessEvent( event ) && !event.IsAllowed() )
1268 return;
1269
1270 // ctrl press
1271 if (unselect_others)
1272 {
1273 if (is_single) Unselect(); // to speed up thing
1274 else UnselectAll();
1275 }
1276
1277 // shift press
1278 if (extended_select)
1279 {
1280 if (m_current == NULL) m_current=m_key_current=GetRootItem().m_pItem;
1281 // don't change the mark (m_current)
1282 SelectItemRange(m_current, item);
1283 }
1284 else
1285 {
1286 bool select=TRUE; // the default
1287
1288 // Check if we need to toggle hilight (ctrl mode)
1289 if (!unselect_others)
1290 select=!item->IsSelected();
1291
1292 m_current = m_key_current = item;
1293 m_current->SetHilight(select);
1294 RefreshLine( m_current );
1295 }
1296
1297 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
1298 GetEventHandler()->ProcessEvent( event );
1299 }
1300
1301 void wxTreeCtrl::FillArray(wxGenericTreeItem *item,
1302 wxArrayTreeItemIds &array) const
1303 {
1304 if ( item->IsSelected() )
1305 array.Add(wxTreeItemId(item));
1306
1307 if ( item->HasChildren() )
1308 {
1309 wxArrayGenericTreeItems& children = item->GetChildren();
1310 size_t count = children.GetCount();
1311 for ( size_t n = 0; n < count; ++n )
1312 FillArray(children[n],array);
1313 }
1314 }
1315
1316 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds &array) const
1317 {
1318 array.Empty();
1319 FillArray(GetRootItem().m_pItem, array);
1320
1321 return array.Count();
1322 }
1323
1324 void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item)
1325 {
1326 if (!item.IsOk()) return;
1327
1328 wxGenericTreeItem *gitem = item.m_pItem;
1329
1330 // first expand all parent branches
1331 wxGenericTreeItem *parent = gitem->GetParent();
1332 while ( parent )
1333 {
1334 Expand(parent);
1335 parent = parent->GetParent();
1336 }
1337
1338 //if (parent) CalculatePositions();
1339
1340 ScrollTo(item);
1341 }
1342
1343 void wxTreeCtrl::ScrollTo(const wxTreeItemId &item)
1344 {
1345 if (!item.IsOk()) return;
1346
1347 // We have to call this here because the label in
1348 // question might just have been added and no screen
1349 // update taken place.
1350 if (m_dirty) wxYield();
1351
1352 wxGenericTreeItem *gitem = item.m_pItem;
1353
1354 // now scroll to the item
1355 int item_y = gitem->GetY();
1356
1357 int start_x = 0;
1358 int start_y = 0;
1359 ViewStart( &start_x, &start_y );
1360 start_y *= PIXELS_PER_UNIT;
1361
1362 int client_h = 0;
1363 int client_w = 0;
1364 GetClientSize( &client_w, &client_h );
1365
1366 if (item_y < start_y+3)
1367 {
1368 // going down
1369 int x = 0;
1370 int y = 0;
1371 m_anchor->GetSize( x, y, this );
1372 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1373 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1374 int x_pos = GetScrollPos( wxHORIZONTAL );
1375 // Item should appear at top
1376 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, item_y/PIXELS_PER_UNIT );
1377 }
1378 else if (item_y+GetLineHeight(gitem) > start_y+client_h)
1379 {
1380 // going up
1381 int x = 0;
1382 int y = 0;
1383 m_anchor->GetSize( x, y, this );
1384 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1385 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1386 item_y += PIXELS_PER_UNIT+2;
1387 int x_pos = GetScrollPos( wxHORIZONTAL );
1388 // Item should appear at bottom
1389 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 );
1390 }
1391 }
1392
1393 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1394 static wxTreeCtrl *s_treeBeingSorted = NULL;
1395
1396 static int LINKAGEMODE tree_ctrl_compare_func(wxGenericTreeItem **item1,
1397 wxGenericTreeItem **item2)
1398 {
1399 wxCHECK_MSG( s_treeBeingSorted, 0, wxT("bug in wxTreeCtrl::SortChildren()") );
1400
1401 return s_treeBeingSorted->OnCompareItems(*item1, *item2);
1402 }
1403
1404 int wxTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
1405 const wxTreeItemId& item2)
1406 {
1407 return wxStrcmp(GetItemText(item1), GetItemText(item2));
1408 }
1409
1410 void wxTreeCtrl::SortChildren(const wxTreeItemId& itemId)
1411 {
1412 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
1413
1414 wxGenericTreeItem *item = itemId.m_pItem;
1415
1416 wxCHECK_RET( !s_treeBeingSorted,
1417 wxT("wxTreeCtrl::SortChildren is not reentrant") );
1418
1419 wxArrayGenericTreeItems& children = item->GetChildren();
1420 if ( children.Count() > 1 )
1421 {
1422 s_treeBeingSorted = this;
1423 children.Sort(tree_ctrl_compare_func);
1424 s_treeBeingSorted = NULL;
1425
1426 m_dirty = TRUE;
1427 }
1428 //else: don't make the tree dirty as nothing changed
1429 }
1430
1431 wxImageList *wxTreeCtrl::GetImageList() const
1432 {
1433 return m_imageListNormal;
1434 }
1435
1436 wxImageList *wxTreeCtrl::GetStateImageList() const
1437 {
1438 return m_imageListState;
1439 }
1440
1441 void wxTreeCtrl::SetImageList(wxImageList *imageList)
1442 {
1443 m_imageListNormal = imageList;
1444
1445 // Calculate a m_lineHeight value from the image sizes.
1446 // May be toggle off. Then wxTreeCtrl will spread when
1447 // necessary (which might look ugly).
1448 #if 1
1449 wxClientDC dc(this);
1450 m_lineHeight = (int)(dc.GetCharHeight() + 4);
1451 int width = 0, height = 0,
1452 n = m_imageListNormal->GetImageCount();
1453
1454 for (int i = 0; i < n ; i++)
1455 {
1456 m_imageListNormal->GetSize(i, width, height);
1457 if (height > m_lineHeight) m_lineHeight = height;
1458 }
1459
1460 if (m_lineHeight < 40)
1461 m_lineHeight += 2; // at least 2 pixels
1462 else
1463 m_lineHeight += m_lineHeight/10; // otherwise 10% extra spacing
1464 #endif
1465 }
1466
1467 void wxTreeCtrl::SetStateImageList(wxImageList *imageList)
1468 {
1469 m_imageListState = imageList;
1470 }
1471
1472 // -----------------------------------------------------------------------------
1473 // helpers
1474 // -----------------------------------------------------------------------------
1475
1476 void wxTreeCtrl::AdjustMyScrollbars()
1477 {
1478 if (m_anchor)
1479 {
1480 int x = 0;
1481 int y = 0;
1482 m_anchor->GetSize( x, y, this );
1483 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1484 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1485 int x_pos = GetScrollPos( wxHORIZONTAL );
1486 int y_pos = GetScrollPos( wxVERTICAL );
1487 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, y_pos );
1488 }
1489 else
1490 {
1491 SetScrollbars( 0, 0, 0, 0 );
1492 }
1493 }
1494
1495 int wxTreeCtrl::GetLineHeight(wxGenericTreeItem *item) const
1496 {
1497 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT)
1498 return item->GetHeight();
1499 else
1500 return m_lineHeight;
1501 }
1502
1503 void wxTreeCtrl::PaintItem(wxGenericTreeItem *item, wxDC& dc)
1504 {
1505 wxTreeItemAttr *attr = item->GetAttributes();
1506 if ( attr && attr->HasFont() )
1507 dc.SetFont(attr->GetFont());
1508 else if (item->IsBold())
1509 dc.SetFont(m_boldFont);
1510
1511 long text_w = 0;
1512 long text_h = 0;
1513 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
1514
1515 int image_h = 0;
1516 int image_w = 0;
1517 int image = item->GetCurrentImage();
1518 if ( image != NO_IMAGE )
1519 {
1520 m_imageListNormal->GetSize( image, image_w, image_h );
1521 image_w += 4;
1522 }
1523
1524 int total_h = GetLineHeight(item);
1525
1526 wxColour colBg;
1527 if ( attr && attr->HasBackgroundColour() )
1528 colBg = attr->GetBackgroundColour();
1529 else
1530 colBg = m_backgroundColour;
1531 dc.SetBrush(wxBrush(colBg, wxSOLID));
1532
1533 dc.DrawRectangle( item->GetX()-2, item->GetY(), item->GetWidth()+2, total_h );
1534
1535 if ( image != NO_IMAGE )
1536 {
1537 dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, total_h );
1538 m_imageListNormal->Draw( image, dc,
1539 item->GetX(),
1540 item->GetY() +((total_h > image_h)?((total_h-image_h)/2):0),
1541 wxIMAGELIST_DRAW_TRANSPARENT );
1542 dc.DestroyClippingRegion();
1543 }
1544
1545 dc.SetBackgroundMode(wxTRANSPARENT);
1546 dc.DrawText( item->GetText(), image_w + item->GetX(), item->GetY()
1547 + ((total_h > text_h) ? (total_h - text_h)/2 : 0));
1548
1549 // restore normal font
1550 dc.SetFont( m_normalFont );
1551 }
1552
1553 // Now y stands for the top of the item, whereas it used to stand for middle !
1554 void wxTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
1555 {
1556 int horizX = level*m_indent;
1557
1558 item->SetX( horizX+m_indent+m_spacing );
1559 item->SetY( y );
1560
1561 int oldY = y;
1562 y+=GetLineHeight(item)/2;
1563
1564 item->SetCross( horizX+m_indent, y );
1565
1566 int exposed_x = dc.LogicalToDeviceX( 0 );
1567 int exposed_y = dc.LogicalToDeviceY( item->GetY() );
1568
1569 if (IsExposed( exposed_x, exposed_y, 10000, GetLineHeight(item) )) // 10000 = very much
1570 {
1571 int startX = horizX;
1572 int endX = horizX + (m_indent-5);
1573
1574 // if (!item->HasChildren()) endX += (m_indent+5);
1575 if (!item->HasChildren()) endX += 20;
1576
1577 dc.DrawLine( startX, y, endX, y );
1578
1579 if (item->HasPlus())
1580 {
1581 dc.DrawLine( horizX+(m_indent+5), y, horizX+(m_indent+15), y );
1582 dc.SetPen( *wxGREY_PEN );
1583 dc.SetBrush( *wxWHITE_BRUSH );
1584 dc.DrawRectangle( horizX+(m_indent-5), y-4, 11, 9 );
1585
1586 dc.SetPen( *wxBLACK_PEN );
1587 dc.DrawLine( horizX+(m_indent-2), y, horizX+(m_indent+3), y );
1588 if (!item->IsExpanded())
1589 dc.DrawLine( horizX+m_indent, y-2, horizX+m_indent, y+3 );
1590
1591 dc.SetPen( m_dottedPen );
1592 }
1593
1594 wxPen *pen = wxTRANSPARENT_PEN;
1595 wxBrush *brush; // FIXME is this really needed?
1596 wxColour colText;
1597
1598 if ( item->IsSelected() )
1599 {
1600 colText = wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT );
1601
1602 brush = m_hilightBrush;
1603
1604 if ( m_hasFocus )
1605 pen = wxBLACK_PEN;
1606
1607 }
1608 else
1609 {
1610 wxTreeItemAttr *attr = item->GetAttributes();
1611 if ( attr && attr->HasTextColour() )
1612 colText = attr->GetTextColour();
1613 else
1614 colText = *wxBLACK;
1615
1616 brush = wxWHITE_BRUSH;
1617 }
1618
1619 // prepare to draw
1620 dc.SetTextForeground(colText);
1621 dc.SetPen(*pen);
1622 dc.SetBrush(*brush);
1623
1624 // draw
1625 PaintItem(item, dc);
1626
1627 // restore DC objects
1628 dc.SetBrush( *wxWHITE_BRUSH );
1629 dc.SetPen( m_dottedPen );
1630 dc.SetTextForeground( *wxBLACK );
1631 }
1632
1633 y = oldY+GetLineHeight(item);
1634
1635 if (item->IsExpanded())
1636 {
1637 oldY+=GetLineHeight(item)/2;
1638 int semiOldY=0;
1639
1640 wxArrayGenericTreeItems& children = item->GetChildren();
1641 size_t n, count = children.Count();
1642 for ( n = 0; n < count; ++n )
1643 {
1644 semiOldY=y;
1645 PaintLevel( children[n], dc, level+1, y );
1646 }
1647
1648 // it may happen that the item is expanded but has no items (when you
1649 // delete all its children for example) - don't draw the vertical line
1650 // in this case
1651 if (count > 0)
1652 {
1653 semiOldY+=GetLineHeight(children[--n])/2;
1654 dc.DrawLine( horizX+m_indent, oldY+5, horizX+m_indent, semiOldY );
1655 }
1656 }
1657 }
1658
1659 void wxTreeCtrl::DrawBorder(wxTreeItemId &item)
1660 {
1661 if (!item) return;
1662
1663 wxGenericTreeItem *i=item.m_pItem;
1664
1665 wxClientDC dc(this);
1666 PrepareDC( dc );
1667 dc.SetLogicalFunction(wxINVERT);
1668
1669 int w,h,x;
1670 ViewStart(&x,&h); // we only need x
1671 GetClientSize(&w,&h); // we only need w
1672
1673 h=GetLineHeight(i)+1;
1674 // 2 white column at border
1675 dc.DrawRectangle( PIXELS_PER_UNIT*x+2, i->GetY()-1, w-6, h);
1676 }
1677
1678 void wxTreeCtrl::DrawLine(wxTreeItemId &item, bool below)
1679 {
1680 if (!item) return;
1681
1682 wxGenericTreeItem *i=item.m_pItem;
1683
1684 wxClientDC dc(this);
1685 PrepareDC( dc );
1686 dc.SetLogicalFunction(wxINVERT);
1687
1688 int w,h,y;
1689 GetSize(&w,&h);
1690
1691 if (below) y=i->GetY()+GetLineHeight(i)-1;
1692 else y=i->GetY();
1693
1694 dc.DrawLine( 0, y, w, y);
1695 }
1696
1697 // -----------------------------------------------------------------------------
1698 // wxWindows callbacks
1699 // -----------------------------------------------------------------------------
1700
1701 void wxTreeCtrl::OnPaint( wxPaintEvent &WXUNUSED(event) )
1702 {
1703 if ( !m_anchor)
1704 return;
1705
1706 wxPaintDC dc(this);
1707 PrepareDC( dc );
1708
1709 dc.SetFont( m_normalFont );
1710 dc.SetPen( m_dottedPen );
1711
1712 // this is now done dynamically
1713 //if(GetImageList() == NULL)
1714 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
1715
1716 int y = 2;
1717 PaintLevel( m_anchor, dc, 0, y );
1718 }
1719
1720 void wxTreeCtrl::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
1721 {
1722 m_hasFocus = TRUE;
1723
1724 if (m_current) RefreshLine( m_current );
1725 }
1726
1727 void wxTreeCtrl::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
1728 {
1729 m_hasFocus = FALSE;
1730
1731 if (m_current) RefreshLine( m_current );
1732 }
1733
1734 void wxTreeCtrl::OnChar( wxKeyEvent &event )
1735 {
1736 wxTreeEvent te( wxEVT_COMMAND_TREE_KEY_DOWN, GetId() );
1737 te.m_code = (int)event.KeyCode();
1738 te.SetEventObject( this );
1739 GetEventHandler()->ProcessEvent( te );
1740
1741 if ( (m_current == 0) || (m_key_current == 0) )
1742 {
1743 event.Skip();
1744 return;
1745 }
1746
1747 // how should the selection work for this event?
1748 bool is_multiple, extended_select, unselect_others;
1749 EventFlagsToSelType(GetWindowStyleFlag(),
1750 event.ShiftDown(),
1751 event.ControlDown(),
1752 &is_multiple, &extended_select, &unselect_others);
1753
1754 switch (event.KeyCode())
1755 {
1756 case '+':
1757 case WXK_ADD:
1758 if (m_current->HasPlus() && !IsExpanded(m_current))
1759 {
1760 Expand(m_current);
1761 }
1762 break;
1763
1764 case '-':
1765 case WXK_SUBTRACT:
1766 if (IsExpanded(m_current))
1767 {
1768 Collapse(m_current);
1769 }
1770 break;
1771
1772 case '*':
1773 case WXK_MULTIPLY:
1774 Toggle(m_current);
1775 break;
1776
1777 case ' ':
1778 case WXK_RETURN:
1779 {
1780 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
1781 event.m_item = m_current;
1782 event.m_code = 0;
1783 event.SetEventObject( this );
1784 GetEventHandler()->ProcessEvent( event );
1785 }
1786 break;
1787
1788 // up goes to the previous sibling or to the last of its children if
1789 // it's expanded
1790 case WXK_UP:
1791 {
1792 wxTreeItemId prev = GetPrevSibling( m_key_current );
1793 if (!prev)
1794 {
1795 prev = GetParent( m_key_current );
1796 if (prev)
1797 {
1798 long cockie = 0;
1799 wxTreeItemId current = m_key_current;
1800 if (current == GetFirstChild( prev, cockie ))
1801 {
1802 // otherwise we return to where we came from
1803 SelectItem( prev, unselect_others, extended_select );
1804 m_key_current=prev.m_pItem;
1805 EnsureVisible( prev );
1806 break;
1807 }
1808 }
1809 }
1810 if (prev)
1811 {
1812 while ( IsExpanded(prev) && HasChildren(prev) )
1813 {
1814 wxTreeItemId child = GetLastChild(prev);
1815 if ( child )
1816 {
1817 prev = child;
1818 }
1819 }
1820
1821 SelectItem( prev, unselect_others, extended_select );
1822 m_key_current=prev.m_pItem;
1823 EnsureVisible( prev );
1824 }
1825 }
1826 break;
1827
1828 // left arrow goes to the parent
1829 case WXK_LEFT:
1830 {
1831 wxTreeItemId prev = GetParent( m_current );
1832 if (prev)
1833 {
1834 EnsureVisible( prev );
1835 SelectItem( prev, unselect_others, extended_select );
1836 }
1837 }
1838 break;
1839
1840 case WXK_RIGHT:
1841 // this works the same as the down arrow except that we also expand the
1842 // item if it wasn't expanded yet
1843 Expand(m_current);
1844 // fall through
1845
1846 case WXK_DOWN:
1847 {
1848 if (IsExpanded(m_key_current) && HasChildren(m_key_current))
1849 {
1850 long cookie = 0;
1851 wxTreeItemId child = GetFirstChild( m_key_current, cookie );
1852 SelectItem( child, unselect_others, extended_select );
1853 m_key_current=child.m_pItem;
1854 EnsureVisible( child );
1855 }
1856 else
1857 {
1858 wxTreeItemId next = GetNextSibling( m_key_current );
1859 // if (next == 0)
1860 if (!next)
1861 {
1862 wxTreeItemId current = m_key_current;
1863 while (current && !next)
1864 {
1865 current = GetParent( current );
1866 if (current) next = GetNextSibling( current );
1867 }
1868 }
1869 // if (next != 0)
1870 if (next)
1871 {
1872 SelectItem( next, unselect_others, extended_select );
1873 m_key_current=next.m_pItem;
1874 EnsureVisible( next );
1875 }
1876 }
1877 }
1878 break;
1879
1880 // <End> selects the last visible tree item
1881 case WXK_END:
1882 {
1883 wxTreeItemId last = GetRootItem();
1884
1885 while ( last.IsOk() && IsExpanded(last) )
1886 {
1887 wxTreeItemId lastChild = GetLastChild(last);
1888
1889 // it may happen if the item was expanded but then all of
1890 // its children have been deleted - so IsExpanded() returned
1891 // TRUE, but GetLastChild() returned invalid item
1892 if ( !lastChild )
1893 break;
1894
1895 last = lastChild;
1896 }
1897
1898 if ( last.IsOk() )
1899 {
1900 EnsureVisible( last );
1901 SelectItem( last, unselect_others, extended_select );
1902 }
1903 }
1904 break;
1905
1906 // <Home> selects the root item
1907 case WXK_HOME:
1908 {
1909 wxTreeItemId prev = GetRootItem();
1910 if (prev)
1911 {
1912 EnsureVisible( prev );
1913 SelectItem( prev, unselect_others, extended_select );
1914 }
1915 }
1916 break;
1917
1918 default:
1919 event.Skip();
1920 }
1921 }
1922
1923 wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& flags)
1924 {
1925 // We have to call this here because the label in
1926 // question might just have been added and no screen
1927 // update taken place.
1928 if (m_dirty) wxYield();
1929
1930 wxClientDC dc(this);
1931 PrepareDC(dc);
1932 wxCoord x = dc.DeviceToLogicalX( point.x );
1933 wxCoord y = dc.DeviceToLogicalY( point.y );
1934 int w, h;
1935 GetSize(&w, &h);
1936
1937 flags=0;
1938 if (point.x<0) flags|=wxTREE_HITTEST_TOLEFT;
1939 if (point.x>w) flags|=wxTREE_HITTEST_TORIGHT;
1940 if (point.y<0) flags|=wxTREE_HITTEST_ABOVE;
1941 if (point.y>h) flags|=wxTREE_HITTEST_BELOW;
1942
1943 return m_anchor->HitTest( wxPoint(x, y), this, flags);
1944 }
1945
1946 /* **** */
1947
1948 void wxTreeCtrl::Edit( const wxTreeItemId& item )
1949 {
1950 if (!item.IsOk()) return;
1951
1952 m_currentEdit = item.m_pItem;
1953
1954 wxTreeEvent te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, GetId() );
1955 te.m_item = m_currentEdit;
1956 te.SetEventObject( this );
1957 GetEventHandler()->ProcessEvent( te );
1958
1959 if (!te.IsAllowed()) return;
1960
1961 // We have to call this here because the label in
1962 // question might just have been added and no screen
1963 // update taken place.
1964 if (m_dirty) wxYield();
1965
1966 wxString s = m_currentEdit->GetText();
1967 int x = m_currentEdit->GetX();
1968 int y = m_currentEdit->GetY();
1969 int w = m_currentEdit->GetWidth();
1970 int h = m_currentEdit->GetHeight();
1971
1972 int image_h = 0;
1973 int image_w = 0;
1974
1975 int image = m_currentEdit->GetCurrentImage();
1976 if ( image != NO_IMAGE )
1977 {
1978 m_imageListNormal->GetSize( image, image_w, image_h );
1979 image_w += 4;
1980 }
1981 x += image_w;
1982 w -= image_w + 4; // I don't know why +4 is needed
1983
1984 wxClientDC dc(this);
1985 PrepareDC( dc );
1986 x = dc.LogicalToDeviceX( x );
1987 y = dc.LogicalToDeviceY( y );
1988
1989 wxTreeTextCtrl *text = new wxTreeTextCtrl(
1990 this, -1, &m_renameAccept, &m_renameRes, this, s, wxPoint(x-4,y-4), wxSize(w+11,h+8) );
1991 text->SetFocus();
1992 }
1993
1994 void wxTreeCtrl::OnRenameTimer()
1995 {
1996 Edit( m_current );
1997 }
1998
1999 void wxTreeCtrl::OnRenameAccept()
2000 {
2001 wxTreeEvent le( wxEVT_COMMAND_TREE_END_LABEL_EDIT, GetId() );
2002 le.m_item = m_currentEdit;
2003 le.SetEventObject( this );
2004 le.m_label = m_renameRes;
2005 GetEventHandler()->ProcessEvent( le );
2006
2007 if (!le.IsAllowed()) return;
2008
2009 SetItemText( m_currentEdit, m_renameRes );
2010 }
2011
2012 void wxTreeCtrl::OnMouse( wxMouseEvent &event )
2013 {
2014 if ( !(event.LeftUp() || event.RightDown() || event.LeftDClick() || event.Dragging()) ) return;
2015
2016 if ( !m_anchor ) return;
2017
2018 wxClientDC dc(this);
2019 PrepareDC(dc);
2020 wxCoord x = dc.DeviceToLogicalX( event.GetX() );
2021 wxCoord y = dc.DeviceToLogicalY( event.GetY() );
2022
2023 int flags=0;
2024 wxGenericTreeItem *item = m_anchor->HitTest( wxPoint(x,y), this, flags);
2025 bool onButton = flags & wxTREE_HITTEST_ONITEMBUTTON;
2026
2027 if (event.Dragging())
2028 {
2029 if (m_dragCount == 0)
2030 m_dragStart = wxPoint(x,y);
2031
2032 m_dragCount++;
2033
2034 if (m_dragCount != 3) return;
2035
2036 int command = wxEVT_COMMAND_TREE_BEGIN_DRAG;
2037 if (event.RightIsDown()) command = wxEVT_COMMAND_TREE_BEGIN_RDRAG;
2038
2039 wxTreeEvent nevent( command, GetId() );
2040 nevent.m_item = m_current;
2041 nevent.SetEventObject(this);
2042 GetEventHandler()->ProcessEvent(nevent);
2043 return;
2044 }
2045 else
2046 {
2047 m_dragCount = 0;
2048 }
2049
2050 if (item == NULL) return; /* we hit the blank area */
2051
2052 if (event.RightDown()) {
2053 wxTreeEvent nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK,GetId());
2054 nevent.m_item=item;
2055 nevent.m_code=0;
2056 nevent.SetEventObject(this);
2057 GetEventHandler()->ProcessEvent(nevent);
2058 return;
2059 }
2060
2061 if (event.LeftUp() && (item == m_current) &&
2062 (flags & wxTREE_HITTEST_ONITEMLABEL) &&
2063 HasFlag(wxTR_EDIT_LABELS) )
2064 {
2065 m_renameTimer->Start( 100, TRUE );
2066 return;
2067 }
2068
2069 // how should the selection work for this event?
2070 bool is_multiple, extended_select, unselect_others;
2071 EventFlagsToSelType(GetWindowStyleFlag(),
2072 event.ShiftDown(),
2073 event.ControlDown(),
2074 &is_multiple, &extended_select, &unselect_others);
2075
2076 if (onButton)
2077 {
2078 Toggle( item );
2079 if (is_multiple)
2080 return;
2081 }
2082
2083 SelectItem(item, unselect_others, extended_select);
2084
2085 if (event.LeftDClick())
2086 {
2087 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
2088 event.m_item = item;
2089 event.m_code = 0;
2090 event.SetEventObject( this );
2091 GetEventHandler()->ProcessEvent( event );
2092 }
2093 }
2094
2095 void wxTreeCtrl::OnIdle( wxIdleEvent &WXUNUSED(event) )
2096 {
2097 /* after all changes have been done to the tree control,
2098 * we actually redraw the tree when everything is over */
2099
2100 if (!m_dirty)
2101 return;
2102
2103 m_dirty = FALSE;
2104
2105 CalculatePositions();
2106 Refresh();
2107 AdjustMyScrollbars();
2108 }
2109
2110 void wxTreeCtrl::CalculateSize( wxGenericTreeItem *item, wxDC &dc )
2111 {
2112 long text_w = 0;
2113 long text_h = 0;
2114
2115 if (item->IsBold())
2116 dc.SetFont(m_boldFont);
2117
2118 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
2119 text_h+=2;
2120
2121 // restore normal font
2122 dc.SetFont( m_normalFont );
2123
2124 int image_h = 0;
2125 int image_w = 0;
2126 int image = item->GetCurrentImage();
2127 if ( image != NO_IMAGE )
2128 {
2129 m_imageListNormal->GetSize( image, image_w, image_h );
2130 image_w += 4;
2131 }
2132
2133 int total_h = (image_h > text_h) ? image_h : text_h;
2134
2135 if (total_h < 40)
2136 total_h += 2; // at least 2 pixels
2137 else
2138 total_h += total_h/10; // otherwise 10% extra spacing
2139
2140 item->SetHeight(total_h);
2141 if (total_h>m_lineHeight) m_lineHeight=total_h;
2142
2143 item->SetWidth(image_w+text_w+2);
2144 }
2145
2146 // -----------------------------------------------------------------------------
2147 // for developper : y is now the top of the level
2148 // not the middle of it !
2149 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
2150 {
2151 int horizX = level*m_indent;
2152
2153 CalculateSize( item, dc );
2154
2155 // set its position
2156 item->SetX( horizX+m_indent+m_spacing );
2157 item->SetY( y );
2158 y+=GetLineHeight(item);
2159
2160 if ( !item->IsExpanded() )
2161 {
2162 // we dont need to calculate collapsed branches
2163 return;
2164 }
2165
2166 wxArrayGenericTreeItems& children = item->GetChildren();
2167 size_t n, count = children.Count();
2168 for (n = 0; n < count; ++n )
2169 CalculateLevel( children[n], dc, level+1, y ); // recurse
2170 }
2171
2172 void wxTreeCtrl::CalculatePositions()
2173 {
2174 if ( !m_anchor ) return;
2175
2176 wxClientDC dc(this);
2177 PrepareDC( dc );
2178
2179 dc.SetFont( m_normalFont );
2180
2181 dc.SetPen( m_dottedPen );
2182 //if(GetImageList() == NULL)
2183 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2184
2185 int y = 2;
2186 CalculateLevel( m_anchor, dc, 0, y ); // start recursion
2187 }
2188
2189 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem *item)
2190 {
2191 if (m_dirty) return;
2192
2193 wxClientDC dc(this);
2194 PrepareDC(dc);
2195
2196 int cw = 0;
2197 int ch = 0;
2198 GetClientSize( &cw, &ch );
2199
2200 wxRect rect;
2201 rect.x = dc.LogicalToDeviceX( 0 );
2202 rect.width = cw;
2203 rect.y = dc.LogicalToDeviceY( item->GetY() );
2204 rect.height = ch;
2205
2206 Refresh( TRUE, &rect );
2207
2208 AdjustMyScrollbars();
2209 }
2210
2211 void wxTreeCtrl::RefreshLine( wxGenericTreeItem *item )
2212 {
2213 if (m_dirty) return;
2214
2215 wxClientDC dc(this);
2216 PrepareDC( dc );
2217
2218 int cw = 0;
2219 int ch = 0;
2220 GetClientSize( &cw, &ch );
2221
2222 wxRect rect;
2223 rect.x = dc.LogicalToDeviceX( 0 );
2224 rect.y = dc.LogicalToDeviceY( item->GetY() );
2225 rect.width = cw;
2226 rect.height = GetLineHeight(item); //dc.GetCharHeight() + 6;
2227
2228 Refresh( TRUE, &rect );
2229 }
2230