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