]> git.saurik.com Git - wxWidgets.git/blob - src/generic/treectrl.cpp
fixed bug in scaling images
[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 dc.DrawRectangle( item->GetX()-2, item->GetY(), item->GetWidth()+2, total_h );
1527
1528 if ( image != NO_IMAGE )
1529 {
1530 dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, total_h );
1531 m_imageListNormal->Draw( image, dc,
1532 item->GetX(),
1533 item->GetY() +((total_h > image_h)?((total_h-image_h)/2):0),
1534 wxIMAGELIST_DRAW_TRANSPARENT );
1535 dc.DestroyClippingRegion();
1536 }
1537
1538 bool hasBgCol = attr && attr->HasBackgroundColour();
1539 dc.SetBackgroundMode(hasBgCol ? wxSOLID : wxTRANSPARENT);
1540 if ( hasBgCol )
1541 dc.SetTextBackground(attr->GetBackgroundColour());
1542 dc.DrawText( item->GetText(), image_w + item->GetX(), item->GetY()
1543 + ((total_h > text_h) ? (total_h - text_h)/2 : 0));
1544
1545 // restore normal font
1546 dc.SetFont( m_normalFont );
1547 }
1548
1549 // Now y stands for the top of the item, whereas it used to stand for middle !
1550 void wxTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
1551 {
1552 int horizX = level*m_indent;
1553
1554 item->SetX( horizX+m_indent+m_spacing );
1555 item->SetY( y );
1556
1557 int oldY = y;
1558 y+=GetLineHeight(item)/2;
1559
1560 item->SetCross( horizX+m_indent, y );
1561
1562 int exposed_x = dc.LogicalToDeviceX( 0 );
1563 int exposed_y = dc.LogicalToDeviceY( item->GetY() );
1564
1565 if (IsExposed( exposed_x, exposed_y, 10000, GetLineHeight(item) )) // 10000 = very much
1566 {
1567 int startX = horizX;
1568 int endX = horizX + (m_indent-5);
1569
1570 // if (!item->HasChildren()) endX += (m_indent+5);
1571 if (!item->HasChildren()) endX += 20;
1572
1573 dc.DrawLine( startX, y, endX, y );
1574
1575 if (item->HasPlus())
1576 {
1577 dc.DrawLine( horizX+(m_indent+5), y, horizX+(m_indent+15), y );
1578 dc.SetPen( *wxGREY_PEN );
1579 dc.SetBrush( *wxWHITE_BRUSH );
1580 dc.DrawRectangle( horizX+(m_indent-5), y-4, 11, 9 );
1581
1582 dc.SetPen( *wxBLACK_PEN );
1583 dc.DrawLine( horizX+(m_indent-2), y, horizX+(m_indent+3), y );
1584 if (!item->IsExpanded())
1585 dc.DrawLine( horizX+m_indent, y-2, horizX+m_indent, y+3 );
1586
1587 dc.SetPen( m_dottedPen );
1588 }
1589
1590 wxPen *pen = wxTRANSPARENT_PEN;
1591 wxBrush *brush; // FIXME is this really needed?
1592 wxColour colText;
1593
1594 if ( item->IsSelected() )
1595 {
1596 colText = wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT );
1597
1598 brush = m_hilightBrush;
1599
1600 if ( m_hasFocus )
1601 pen = wxBLACK_PEN;
1602
1603 }
1604 else
1605 {
1606 wxTreeItemAttr *attr = item->GetAttributes();
1607 if ( attr && attr->HasTextColour() )
1608 colText = attr->GetTextColour();
1609 else
1610 colText = *wxBLACK;
1611
1612 brush = wxWHITE_BRUSH;
1613 }
1614
1615 // prepare to draw
1616 dc.SetTextForeground(colText);
1617 dc.SetPen(*pen);
1618 dc.SetBrush(*brush);
1619
1620 // draw
1621 PaintItem(item, dc);
1622
1623 // restore DC objects
1624 dc.SetBrush( *wxWHITE_BRUSH );
1625 dc.SetPen( m_dottedPen );
1626 dc.SetTextForeground( *wxBLACK );
1627 }
1628
1629 y = oldY+GetLineHeight(item);
1630
1631 if (item->IsExpanded())
1632 {
1633 oldY+=GetLineHeight(item)/2;
1634 int semiOldY=0;
1635
1636 wxArrayGenericTreeItems& children = item->GetChildren();
1637 size_t n, count = children.Count();
1638 for ( n = 0; n < count; ++n )
1639 {
1640 semiOldY=y;
1641 PaintLevel( children[n], dc, level+1, y );
1642 }
1643
1644 // it may happen that the item is expanded but has no items (when you
1645 // delete all its children for example) - don't draw the vertical line
1646 // in this case
1647 if (count > 0)
1648 {
1649 semiOldY+=GetLineHeight(children[--n])/2;
1650 dc.DrawLine( horizX+m_indent, oldY+5, horizX+m_indent, semiOldY );
1651 }
1652 }
1653 }
1654
1655 void wxTreeCtrl::DrawBorder(wxTreeItemId &item)
1656 {
1657 if (!item) return;
1658
1659 wxGenericTreeItem *i=item.m_pItem;
1660
1661 wxClientDC dc(this);
1662 PrepareDC( dc );
1663 dc.SetLogicalFunction(wxINVERT);
1664
1665 int w,h,x;
1666 ViewStart(&x,&h); // we only need x
1667 GetClientSize(&w,&h); // we only need w
1668
1669 h=GetLineHeight(i)+1;
1670 // 2 white column at border
1671 dc.DrawRectangle( PIXELS_PER_UNIT*x+2, i->GetY()-1, w-6, h);
1672 }
1673
1674 void wxTreeCtrl::DrawLine(wxTreeItemId &item, bool below)
1675 {
1676 if (!item) return;
1677
1678 wxGenericTreeItem *i=item.m_pItem;
1679
1680 wxClientDC dc(this);
1681 PrepareDC( dc );
1682 dc.SetLogicalFunction(wxINVERT);
1683
1684 int w,h,y;
1685 GetSize(&w,&h);
1686
1687 if (below) y=i->GetY()+GetLineHeight(i)-1;
1688 else y=i->GetY();
1689
1690 dc.DrawLine( 0, y, w, y);
1691 }
1692
1693 // -----------------------------------------------------------------------------
1694 // wxWindows callbacks
1695 // -----------------------------------------------------------------------------
1696
1697 void wxTreeCtrl::OnPaint( wxPaintEvent &WXUNUSED(event) )
1698 {
1699 if ( !m_anchor)
1700 return;
1701
1702 wxPaintDC dc(this);
1703 PrepareDC( dc );
1704
1705 dc.SetFont( m_normalFont );
1706 dc.SetPen( m_dottedPen );
1707
1708 // this is now done dynamically
1709 //if(GetImageList() == NULL)
1710 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
1711
1712 int y = 2;
1713 PaintLevel( m_anchor, dc, 0, y );
1714 }
1715
1716 void wxTreeCtrl::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
1717 {
1718 m_hasFocus = TRUE;
1719
1720 if (m_current) RefreshLine( m_current );
1721 }
1722
1723 void wxTreeCtrl::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
1724 {
1725 m_hasFocus = FALSE;
1726
1727 if (m_current) RefreshLine( m_current );
1728 }
1729
1730 void wxTreeCtrl::OnChar( wxKeyEvent &event )
1731 {
1732 wxTreeEvent te( wxEVT_COMMAND_TREE_KEY_DOWN, GetId() );
1733 te.m_code = (int)event.KeyCode();
1734 te.SetEventObject( this );
1735 GetEventHandler()->ProcessEvent( te );
1736
1737 if ( (m_current == 0) || (m_key_current == 0) )
1738 {
1739 event.Skip();
1740 return;
1741 }
1742
1743 // how should the selection work for this event?
1744 bool is_multiple, extended_select, unselect_others;
1745 EventFlagsToSelType(GetWindowStyleFlag(),
1746 event.ShiftDown(),
1747 event.ControlDown(),
1748 &is_multiple, &extended_select, &unselect_others);
1749
1750 switch (event.KeyCode())
1751 {
1752 case '+':
1753 case WXK_ADD:
1754 if (m_current->HasPlus() && !IsExpanded(m_current))
1755 {
1756 Expand(m_current);
1757 }
1758 break;
1759
1760 case '-':
1761 case WXK_SUBTRACT:
1762 if (IsExpanded(m_current))
1763 {
1764 Collapse(m_current);
1765 }
1766 break;
1767
1768 case '*':
1769 case WXK_MULTIPLY:
1770 Toggle(m_current);
1771 break;
1772
1773 case ' ':
1774 case WXK_RETURN:
1775 {
1776 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
1777 event.m_item = m_current;
1778 event.m_code = 0;
1779 event.SetEventObject( this );
1780 GetEventHandler()->ProcessEvent( event );
1781 }
1782 break;
1783
1784 // up goes to the previous sibling or to the last of its children if
1785 // it's expanded
1786 case WXK_UP:
1787 {
1788 wxTreeItemId prev = GetPrevSibling( m_key_current );
1789 if (!prev)
1790 {
1791 prev = GetParent( m_key_current );
1792 if (prev)
1793 {
1794 long cockie = 0;
1795 wxTreeItemId current = m_key_current;
1796 if (current == GetFirstChild( prev, cockie ))
1797 {
1798 // otherwise we return to where we came from
1799 SelectItem( prev, unselect_others, extended_select );
1800 m_key_current=prev.m_pItem;
1801 EnsureVisible( prev );
1802 break;
1803 }
1804 }
1805 }
1806 if (prev)
1807 {
1808 while ( IsExpanded(prev) && HasChildren(prev) )
1809 {
1810 wxTreeItemId child = GetLastChild(prev);
1811 if ( child )
1812 {
1813 prev = child;
1814 }
1815 }
1816
1817 SelectItem( prev, unselect_others, extended_select );
1818 m_key_current=prev.m_pItem;
1819 EnsureVisible( prev );
1820 }
1821 }
1822 break;
1823
1824 // left arrow goes to the parent
1825 case WXK_LEFT:
1826 {
1827 wxTreeItemId prev = GetParent( m_current );
1828 if (prev)
1829 {
1830 EnsureVisible( prev );
1831 SelectItem( prev, unselect_others, extended_select );
1832 }
1833 }
1834 break;
1835
1836 case WXK_RIGHT:
1837 // this works the same as the down arrow except that we also expand the
1838 // item if it wasn't expanded yet
1839 Expand(m_current);
1840 // fall through
1841
1842 case WXK_DOWN:
1843 {
1844 if (IsExpanded(m_key_current) && HasChildren(m_key_current))
1845 {
1846 long cookie = 0;
1847 wxTreeItemId child = GetFirstChild( m_key_current, cookie );
1848 SelectItem( child, unselect_others, extended_select );
1849 m_key_current=child.m_pItem;
1850 EnsureVisible( child );
1851 }
1852 else
1853 {
1854 wxTreeItemId next = GetNextSibling( m_key_current );
1855 // if (next == 0)
1856 if (!next)
1857 {
1858 wxTreeItemId current = m_key_current;
1859 while (current && !next)
1860 {
1861 current = GetParent( current );
1862 if (current) next = GetNextSibling( current );
1863 }
1864 }
1865 // if (next != 0)
1866 if (next)
1867 {
1868 SelectItem( next, unselect_others, extended_select );
1869 m_key_current=next.m_pItem;
1870 EnsureVisible( next );
1871 }
1872 }
1873 }
1874 break;
1875
1876 // <End> selects the last visible tree item
1877 case WXK_END:
1878 {
1879 wxTreeItemId last = GetRootItem();
1880
1881 while ( last.IsOk() && IsExpanded(last) )
1882 {
1883 wxTreeItemId lastChild = GetLastChild(last);
1884
1885 // it may happen if the item was expanded but then all of
1886 // its children have been deleted - so IsExpanded() returned
1887 // TRUE, but GetLastChild() returned invalid item
1888 if ( !lastChild )
1889 break;
1890
1891 last = lastChild;
1892 }
1893
1894 if ( last.IsOk() )
1895 {
1896 EnsureVisible( last );
1897 SelectItem( last, unselect_others, extended_select );
1898 }
1899 }
1900 break;
1901
1902 // <Home> selects the root item
1903 case WXK_HOME:
1904 {
1905 wxTreeItemId prev = GetRootItem();
1906 if (prev)
1907 {
1908 EnsureVisible( prev );
1909 SelectItem( prev, unselect_others, extended_select );
1910 }
1911 }
1912 break;
1913
1914 default:
1915 event.Skip();
1916 }
1917 }
1918
1919 wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& flags)
1920 {
1921 // We have to call this here because the label in
1922 // question might just have been added and no screen
1923 // update taken place.
1924 if (m_dirty) wxYield();
1925
1926 wxClientDC dc(this);
1927 PrepareDC(dc);
1928 wxCoord x = dc.DeviceToLogicalX( point.x );
1929 wxCoord y = dc.DeviceToLogicalY( point.y );
1930 int w, h;
1931 GetSize(&w, &h);
1932
1933 flags=0;
1934 if (point.x<0) flags|=wxTREE_HITTEST_TOLEFT;
1935 if (point.x>w) flags|=wxTREE_HITTEST_TORIGHT;
1936 if (point.y<0) flags|=wxTREE_HITTEST_ABOVE;
1937 if (point.y>h) flags|=wxTREE_HITTEST_BELOW;
1938
1939 return m_anchor->HitTest( wxPoint(x, y), this, flags);
1940 }
1941
1942 /* **** */
1943
1944 void wxTreeCtrl::Edit( const wxTreeItemId& item )
1945 {
1946 if (!item.IsOk()) return;
1947
1948 m_currentEdit = item.m_pItem;
1949
1950 wxTreeEvent te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, GetId() );
1951 te.m_item = m_currentEdit;
1952 te.SetEventObject( this );
1953 GetEventHandler()->ProcessEvent( te );
1954
1955 if (!te.IsAllowed()) return;
1956
1957 // We have to call this here because the label in
1958 // question might just have been added and no screen
1959 // update taken place.
1960 if (m_dirty) wxYield();
1961
1962 wxString s = m_currentEdit->GetText();
1963 int x = m_currentEdit->GetX();
1964 int y = m_currentEdit->GetY();
1965 int w = m_currentEdit->GetWidth();
1966 int h = m_currentEdit->GetHeight();
1967
1968 int image_h = 0;
1969 int image_w = 0;
1970
1971 int image = m_currentEdit->GetCurrentImage();
1972 if ( image != NO_IMAGE )
1973 {
1974 m_imageListNormal->GetSize( image, image_w, image_h );
1975 image_w += 4;
1976 }
1977 x += image_w;
1978 w -= image_w + 4; // I don't know why +4 is needed
1979
1980 wxClientDC dc(this);
1981 PrepareDC( dc );
1982 x = dc.LogicalToDeviceX( x );
1983 y = dc.LogicalToDeviceY( y );
1984
1985 wxTreeTextCtrl *text = new wxTreeTextCtrl(
1986 this, -1, &m_renameAccept, &m_renameRes, this, s, wxPoint(x-4,y-4), wxSize(w+11,h+8) );
1987 text->SetFocus();
1988 }
1989
1990 void wxTreeCtrl::OnRenameTimer()
1991 {
1992 Edit( m_current );
1993 }
1994
1995 void wxTreeCtrl::OnRenameAccept()
1996 {
1997 wxTreeEvent le( wxEVT_COMMAND_TREE_END_LABEL_EDIT, GetId() );
1998 le.m_item = m_currentEdit;
1999 le.SetEventObject( this );
2000 le.m_label = m_renameRes;
2001 GetEventHandler()->ProcessEvent( le );
2002
2003 if (!le.IsAllowed()) return;
2004
2005 SetItemText( m_currentEdit, m_renameRes );
2006 }
2007
2008 void wxTreeCtrl::OnMouse( wxMouseEvent &event )
2009 {
2010 if ( !(event.LeftUp() || event.RightDown() || event.LeftDClick() || event.Dragging()) ) return;
2011
2012 if ( !m_anchor ) return;
2013
2014 wxClientDC dc(this);
2015 PrepareDC(dc);
2016 wxCoord x = dc.DeviceToLogicalX( event.GetX() );
2017 wxCoord y = dc.DeviceToLogicalY( event.GetY() );
2018
2019 int flags=0;
2020 wxGenericTreeItem *item = m_anchor->HitTest( wxPoint(x,y), this, flags);
2021 bool onButton = flags & wxTREE_HITTEST_ONITEMBUTTON;
2022
2023 if (event.Dragging())
2024 {
2025 if (m_dragCount == 0)
2026 m_dragStart = wxPoint(x,y);
2027
2028 m_dragCount++;
2029
2030 if (m_dragCount != 3) return;
2031
2032 int command = wxEVT_COMMAND_TREE_BEGIN_DRAG;
2033 if (event.RightIsDown()) command = wxEVT_COMMAND_TREE_BEGIN_RDRAG;
2034
2035 wxTreeEvent nevent( command, GetId() );
2036 nevent.m_item = m_current;
2037 nevent.SetEventObject(this);
2038 GetEventHandler()->ProcessEvent(nevent);
2039 return;
2040 }
2041 else
2042 {
2043 m_dragCount = 0;
2044 }
2045
2046 if (item == NULL) return; /* we hit the blank area */
2047
2048 if (event.RightDown()) {
2049 wxTreeEvent nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK,GetId());
2050 nevent.m_item=item;
2051 nevent.m_code=0;
2052 nevent.SetEventObject(this);
2053 GetEventHandler()->ProcessEvent(nevent);
2054 return;
2055 }
2056
2057 if (event.LeftUp() && (item == m_current) &&
2058 (flags & wxTREE_HITTEST_ONITEMLABEL) &&
2059 HasFlag(wxTR_EDIT_LABELS) )
2060 {
2061 m_renameTimer->Start( 100, TRUE );
2062 return;
2063 }
2064
2065 // how should the selection work for this event?
2066 bool is_multiple, extended_select, unselect_others;
2067 EventFlagsToSelType(GetWindowStyleFlag(),
2068 event.ShiftDown(),
2069 event.ControlDown(),
2070 &is_multiple, &extended_select, &unselect_others);
2071
2072 if (onButton)
2073 {
2074 Toggle( item );
2075 if (is_multiple)
2076 return;
2077 }
2078
2079 SelectItem(item, unselect_others, extended_select);
2080
2081 if (event.LeftDClick())
2082 {
2083 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
2084 event.m_item = item;
2085 event.m_code = 0;
2086 event.SetEventObject( this );
2087 GetEventHandler()->ProcessEvent( event );
2088 }
2089 }
2090
2091 void wxTreeCtrl::OnIdle( wxIdleEvent &WXUNUSED(event) )
2092 {
2093 /* after all changes have been done to the tree control,
2094 * we actually redraw the tree when everything is over */
2095
2096 if (!m_dirty)
2097 return;
2098
2099 m_dirty = FALSE;
2100
2101 CalculatePositions();
2102 Refresh();
2103 AdjustMyScrollbars();
2104 }
2105
2106 void wxTreeCtrl::CalculateSize( wxGenericTreeItem *item, wxDC &dc )
2107 {
2108 long text_w = 0;
2109 long text_h = 0;
2110
2111 if (item->IsBold())
2112 dc.SetFont(m_boldFont);
2113
2114 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
2115 text_h+=2;
2116
2117 // restore normal font
2118 dc.SetFont( m_normalFont );
2119
2120 int image_h = 0;
2121 int image_w = 0;
2122 int image = item->GetCurrentImage();
2123 if ( image != NO_IMAGE )
2124 {
2125 m_imageListNormal->GetSize( image, image_w, image_h );
2126 image_w += 4;
2127 }
2128
2129 int total_h = (image_h > text_h) ? image_h : text_h;
2130
2131 if (total_h < 40)
2132 total_h += 2; // at least 2 pixels
2133 else
2134 total_h += total_h/10; // otherwise 10% extra spacing
2135
2136 item->SetHeight(total_h);
2137 if (total_h>m_lineHeight) m_lineHeight=total_h;
2138
2139 item->SetWidth(image_w+text_w+2);
2140 }
2141
2142 // -----------------------------------------------------------------------------
2143 // for developper : y is now the top of the level
2144 // not the middle of it !
2145 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
2146 {
2147 int horizX = level*m_indent;
2148
2149 CalculateSize( item, dc );
2150
2151 // set its position
2152 item->SetX( horizX+m_indent+m_spacing );
2153 item->SetY( y );
2154 y+=GetLineHeight(item);
2155
2156 if ( !item->IsExpanded() )
2157 {
2158 // we dont need to calculate collapsed branches
2159 return;
2160 }
2161
2162 wxArrayGenericTreeItems& children = item->GetChildren();
2163 size_t n, count = children.Count();
2164 for (n = 0; n < count; ++n )
2165 CalculateLevel( children[n], dc, level+1, y ); // recurse
2166 }
2167
2168 void wxTreeCtrl::CalculatePositions()
2169 {
2170 if ( !m_anchor ) return;
2171
2172 wxClientDC dc(this);
2173 PrepareDC( dc );
2174
2175 dc.SetFont( m_normalFont );
2176
2177 dc.SetPen( m_dottedPen );
2178 //if(GetImageList() == NULL)
2179 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2180
2181 int y = 2;
2182 CalculateLevel( m_anchor, dc, 0, y ); // start recursion
2183 }
2184
2185 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem *item)
2186 {
2187 if (m_dirty) return;
2188
2189 wxClientDC dc(this);
2190 PrepareDC(dc);
2191
2192 int cw = 0;
2193 int ch = 0;
2194 GetClientSize( &cw, &ch );
2195
2196 wxRect rect;
2197 rect.x = dc.LogicalToDeviceX( 0 );
2198 rect.width = cw;
2199 rect.y = dc.LogicalToDeviceY( item->GetY() );
2200 rect.height = ch;
2201
2202 Refresh( TRUE, &rect );
2203
2204 AdjustMyScrollbars();
2205 }
2206
2207 void wxTreeCtrl::RefreshLine( wxGenericTreeItem *item )
2208 {
2209 if (m_dirty) return;
2210
2211 wxClientDC dc(this);
2212 PrepareDC( dc );
2213
2214 int cw = 0;
2215 int ch = 0;
2216 GetClientSize( &cw, &ch );
2217
2218 wxRect rect;
2219 rect.x = dc.LogicalToDeviceX( 0 );
2220 rect.y = dc.LogicalToDeviceY( item->GetY() );
2221 rect.width = cw;
2222 rect.height = GetLineHeight(item); //dc.GetCharHeight() + 6;
2223
2224 Refresh( TRUE, &rect );
2225 }
2226