]> git.saurik.com Git - wxWidgets.git/blob - src/generic/treectrl.cpp
Added configure patch for DEC
[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/generic/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/dcclient.h"
38 #include "wx/msgdlg.h"
39
40 // -----------------------------------------------------------------------------
41 // array types
42 // -----------------------------------------------------------------------------
43
44 class WXDLLEXPORT wxGenericTreeItem;
45
46 WX_DEFINE_ARRAY(wxGenericTreeItem *, wxArrayTreeItems);
47
48 // -----------------------------------------------------------------------------
49 // private classes
50 // -----------------------------------------------------------------------------
51
52 // a tree item
53 class WXDLLEXPORT wxGenericTreeItem
54 {
55 public:
56 // ctors & dtor
57 wxGenericTreeItem() { m_data = NULL; }
58 wxGenericTreeItem( wxGenericTreeItem *parent,
59 const wxString& text,
60 wxDC& dc,
61 int image, int selImage,
62 wxTreeItemData *data );
63
64 ~wxGenericTreeItem();
65
66 // trivial accessors
67 wxArrayTreeItems& GetChildren() { return m_children; }
68
69 const wxString& GetText() const { return m_text; }
70 int GetImage() const { return m_image; }
71 int GetSelectedImage() const { return m_selImage; }
72 wxTreeItemData *GetData() const { return m_data; }
73
74 void SetText( const wxString &text, wxDC& dc );
75 void SetImage(int image) { m_image = image; }
76 void SetSelectedImage(int image) { m_selImage = image; }
77 void SetData(wxTreeItemData *data) { m_data = data; }
78
79 void SetHasPlus(bool has = TRUE) { m_hasPlus = has; }
80
81 void SetBold(bool bold) { m_isBold = bold; }
82
83 int GetX() const { return m_x; }
84 int GetY() const { return m_y; }
85
86 void SetHeight(int h) { m_height = h; }
87
88 void SetX(int x) { m_x = x; }
89 void SetY(int y) { m_y = y; }
90
91 wxGenericTreeItem *GetParent() const { return m_parent; }
92
93 // operations
94 // deletes all children notifying the treectrl about it if !NULL pointer
95 // given
96 void DeleteChildren(wxTreeCtrl *tree = NULL);
97 // FIXME don't know what is it for
98 void Reset();
99
100 // get count of all children (and grand children if 'recursively')
101 size_t GetChildrenCount(bool recursively = TRUE) const;
102
103 void Insert(wxGenericTreeItem *child, size_t index)
104 { m_children.Insert(child, index); }
105
106 void SetCross( int x, int y );
107 void GetSize( int &x, int &y );
108
109 // return the item at given position (or NULL if no item), onButton is TRUE
110 // if the point belongs to the item's button, otherwise it lies on the
111 // button's label
112 wxGenericTreeItem *HitTest( const wxPoint& point, bool &onButton );
113
114 void Expand() { m_isCollapsed = FALSE; }
115 void Collapse() { m_isCollapsed = TRUE; }
116
117 void SetHilight( bool set = TRUE ) { m_hasHilight = set; }
118
119 // status inquiries
120 bool HasChildren() const { return !m_children.IsEmpty(); }
121 bool HasHilight() const { return m_hasHilight; }
122 bool IsExpanded() const { return !m_isCollapsed; }
123 bool HasPlus() const { return m_hasPlus || HasChildren(); }
124 bool IsBold() const { return m_isBold; }
125
126 private:
127 wxString m_text;
128
129 int m_image,
130 m_selImage;
131
132 wxTreeItemData *m_data;
133
134 // use bitfields to save size
135 int m_isCollapsed :1;
136 int m_hasHilight :1; // same as focused
137 int m_hasPlus :1; // used for item which doesn't have
138 // children but still has a [+] button
139 int m_isBold :1; // render the label in bold font
140
141 int m_x, m_y;
142 long m_height, m_width;
143 int m_xCross, m_yCross;
144 int m_level;
145 wxArrayTreeItems m_children;
146 wxGenericTreeItem *m_parent;
147 };
148
149 // =============================================================================
150 // implementation
151 // =============================================================================
152
153 // -----------------------------------------------------------------------------
154 // wxTreeEvent
155 // -----------------------------------------------------------------------------
156
157 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent, wxNotifyEvent)
158
159 wxTreeEvent::wxTreeEvent( wxEventType commandType, int id )
160 : wxNotifyEvent( commandType, id )
161 {
162 m_code = 0;
163 m_itemOld = (wxGenericTreeItem *)NULL;
164 }
165
166 // -----------------------------------------------------------------------------
167 // wxGenericTreeItem
168 // -----------------------------------------------------------------------------
169
170 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem *parent,
171 const wxString& text,
172 wxDC& dc,
173 int image, int selImage,
174 wxTreeItemData *data)
175 : m_text(text)
176 {
177 m_image = image;
178 m_selImage = selImage;
179 m_data = data;
180 m_x = m_y = 0;
181 m_xCross = m_yCross = 0;
182
183 m_level = 0;
184
185 m_isCollapsed = TRUE;
186 m_hasHilight = FALSE;
187 m_hasPlus = FALSE;
188 m_isBold = FALSE;
189
190 m_parent = parent;
191
192 dc.GetTextExtent( m_text, &m_width, &m_height );
193 }
194
195 wxGenericTreeItem::~wxGenericTreeItem()
196 {
197 delete m_data;
198
199 wxASSERT_MSG( m_children.IsEmpty(),
200 _T("please call DeleteChildren() before deleting the item") );
201 }
202
203 void wxGenericTreeItem::DeleteChildren(wxTreeCtrl *tree)
204 {
205 size_t count = m_children.Count();
206 for ( size_t n = 0; n < count; n++ )
207 {
208 wxGenericTreeItem *child = m_children[n];
209 if ( tree )
210 {
211 tree->SendDeleteEvent(child);
212 }
213
214 child->DeleteChildren(tree);
215 delete child;
216 }
217
218 m_children.Empty();
219 }
220
221 void wxGenericTreeItem::SetText( const wxString &text, wxDC& dc )
222 {
223 m_text = text;
224
225 dc.GetTextExtent( m_text, &m_width, &m_height );
226 }
227
228 void wxGenericTreeItem::Reset()
229 {
230 m_text.Empty();
231 m_image =
232 m_selImage = -1;
233 m_data = NULL;
234 m_x = m_y =
235 m_height = m_width = 0;
236 m_xCross =
237 m_yCross = 0;
238
239 m_level = 0;
240
241 DeleteChildren();
242 m_isCollapsed = TRUE;
243
244 m_parent = (wxGenericTreeItem *)NULL;
245 }
246
247 size_t wxGenericTreeItem::GetChildrenCount(bool recursively) const
248 {
249 size_t count = m_children.Count();
250 if ( !recursively )
251 return count;
252
253 size_t total = count;
254 for ( size_t n = 0; n < count; n++ )
255 {
256 total += m_children[n]->GetChildrenCount();
257 }
258
259 return total;
260 }
261
262 void wxGenericTreeItem::SetCross( int x, int y )
263 {
264 m_xCross = x;
265 m_yCross = y;
266 }
267
268 void wxGenericTreeItem::GetSize( int &x, int &y )
269 {
270 if ( y < m_y ) y = m_y;
271 int width = m_x + m_width;
272 if (width > x) x = width;
273
274 if (IsExpanded())
275 {
276 size_t count = m_children.Count();
277 for ( size_t n = 0; n < count; n++ )
278 {
279 m_children[n]->GetSize( x, y );
280 }
281 }
282 }
283
284 wxGenericTreeItem *wxGenericTreeItem::HitTest( const wxPoint& point,
285 bool &onButton )
286 {
287 if ((point.y > m_y) && (point.y < m_y + m_height))
288 {
289 // FIXME why +5?
290 // Because that is the size of the plus sign, RR
291 if ((point.x > m_xCross-5) && (point.x < m_xCross+5) &&
292 (point.y > m_yCross-5) && (point.y < m_yCross+5) &&
293 (IsExpanded() || HasPlus()))
294 {
295 onButton = TRUE;
296 return this;
297 }
298
299 /* TODO: we should do a query here like
300 m_imageListNormal->GetSize( item->GetImage(), image_w, image_h ); */
301 int w = m_width;
302 if (m_image != -1) w += 24;
303
304 if ((point.x > m_x) && (point.x < m_x+w))
305 {
306 onButton = FALSE;
307 return this;
308 }
309 }
310 else
311 {
312 if (!m_isCollapsed)
313 {
314 size_t count = m_children.Count();
315 for ( size_t n = 0; n < count; n++ )
316 {
317 wxGenericTreeItem *res = m_children[n]->HitTest( point, onButton );
318 if ( res != NULL )
319 return res;
320 }
321 }
322 }
323
324 return NULL;
325 }
326
327 // -----------------------------------------------------------------------------
328 // wxTreeCtrl implementation
329 // -----------------------------------------------------------------------------
330
331 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxScrolledWindow)
332
333 BEGIN_EVENT_TABLE(wxTreeCtrl,wxScrolledWindow)
334 EVT_PAINT (wxTreeCtrl::OnPaint)
335 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse)
336 EVT_CHAR (wxTreeCtrl::OnChar)
337 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus)
338 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus)
339 EVT_IDLE (wxTreeCtrl::OnIdle)
340 END_EVENT_TABLE()
341
342 // -----------------------------------------------------------------------------
343 // construction/destruction
344 // -----------------------------------------------------------------------------
345 void wxTreeCtrl::Init()
346 {
347 m_current =
348 m_anchor = (wxGenericTreeItem *) NULL;
349 m_hasFocus = FALSE;
350 m_dirty = FALSE;
351
352 m_xScroll = 0;
353 m_yScroll = 0;
354 m_lineHeight = 10;
355 m_indent = 15;
356
357 m_hilightBrush = new wxBrush
358 (
359 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT),
360 wxSOLID
361 );
362
363 m_imageListNormal =
364 m_imageListState = (wxImageList *) NULL;
365
366 m_dragCount = 0;
367 }
368
369 bool wxTreeCtrl::Create(wxWindow *parent, wxWindowID id,
370 const wxPoint& pos, const wxSize& size,
371 long style,
372 const wxValidator &validator,
373 const wxString& name )
374 {
375 Init();
376
377 wxScrolledWindow::Create( parent, id, pos, size, style|wxHSCROLL|wxVSCROLL, name );
378
379 SetValidator( validator );
380
381 SetBackgroundColour( *wxWHITE );
382 m_dottedPen = wxPen( *wxBLACK, 0, 0 );
383
384 return TRUE;
385 }
386
387 wxTreeCtrl::~wxTreeCtrl()
388 {
389 wxDELETE( m_hilightBrush );
390
391 DeleteAllItems();
392 }
393
394 // -----------------------------------------------------------------------------
395 // accessors
396 // -----------------------------------------------------------------------------
397
398 size_t wxTreeCtrl::GetCount() const
399 {
400 return m_anchor == NULL ? 0u : m_anchor->GetChildrenCount();
401 }
402
403 void wxTreeCtrl::SetIndent(unsigned int indent)
404 {
405 m_indent = indent;
406 Refresh();
407 }
408
409 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item, bool recursively)
410 {
411 wxCHECK_MSG( item.IsOk(), 0u, _T("invalid tree item") );
412
413 return item.m_pItem->GetChildrenCount(recursively);
414 }
415
416 // -----------------------------------------------------------------------------
417 // functions to work with tree items
418 // -----------------------------------------------------------------------------
419
420 wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const
421 {
422 wxCHECK_MSG( item.IsOk(), _T(""), _T("invalid tree item") );
423
424 return item.m_pItem->GetText();
425 }
426
427 int wxTreeCtrl::GetItemImage(const wxTreeItemId& item) const
428 {
429 wxCHECK_MSG( item.IsOk(), -1, _T("invalid tree item") );
430
431 return item.m_pItem->GetImage();
432 }
433
434 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId& item) const
435 {
436 wxCHECK_MSG( item.IsOk(), -1, _T("invalid tree item") );
437
438 return item.m_pItem->GetSelectedImage();
439 }
440
441 wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const
442 {
443 wxCHECK_MSG( item.IsOk(), NULL, _T("invalid tree item") );
444
445 return item.m_pItem->GetData();
446 }
447
448 void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text)
449 {
450 wxCHECK_RET( item.IsOk(), _T("invalid tree item") );
451
452 wxClientDC dc(this);
453 wxGenericTreeItem *pItem = item.m_pItem;
454 pItem->SetText(text, dc);
455 RefreshLine(pItem);
456 }
457
458 void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, int image)
459 {
460 wxCHECK_RET( item.IsOk(), _T("invalid tree item") );
461
462 wxGenericTreeItem *pItem = item.m_pItem;
463 pItem->SetImage(image);
464 RefreshLine(pItem);
465 }
466
467 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId& item, int image)
468 {
469 wxCHECK_RET( item.IsOk(), _T("invalid tree item") );
470
471 wxGenericTreeItem *pItem = item.m_pItem;
472 pItem->SetSelectedImage(image);
473 RefreshLine(pItem);
474 }
475
476 void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
477 {
478 wxCHECK_RET( item.IsOk(), _T("invalid tree item") );
479
480 item.m_pItem->SetData(data);
481 }
482
483 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
484 {
485 wxCHECK_RET( item.IsOk(), _T("invalid tree item") );
486
487 wxGenericTreeItem *pItem = item.m_pItem;
488 pItem->SetHasPlus(has);
489 RefreshLine(pItem);
490 }
491
492 void wxTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
493 {
494 wxCHECK_RET( item.IsOk(), _T("invalid tree item") );
495
496 // avoid redrawing the tree if no real change
497 wxGenericTreeItem *pItem = item.m_pItem;
498 if ( pItem->IsBold() != bold )
499 {
500 pItem->SetBold(bold);
501 RefreshLine(pItem);
502 }
503 }
504
505 // -----------------------------------------------------------------------------
506 // item status inquiries
507 // -----------------------------------------------------------------------------
508
509 bool wxTreeCtrl::IsVisible(const wxTreeItemId& WXUNUSED(item)) const
510 {
511 wxFAIL_MSG(_T("not implemented"));
512
513 return TRUE;
514 }
515
516 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
517 {
518 wxCHECK_MSG( item.IsOk(), FALSE, _T("invalid tree item") );
519
520 return !item.m_pItem->GetChildren().IsEmpty();
521 }
522
523 bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const
524 {
525 wxCHECK_MSG( item.IsOk(), FALSE, _T("invalid tree item") );
526
527 return item.m_pItem->IsExpanded();
528 }
529
530 bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const
531 {
532 wxCHECK_MSG( item.IsOk(), FALSE, _T("invalid tree item") );
533
534 return item.m_pItem->HasHilight();
535 }
536
537 bool wxTreeCtrl::IsBold(const wxTreeItemId& item) const
538 {
539 wxCHECK_MSG( item.IsOk(), FALSE, _T("invalid tree item") );
540
541 return item.m_pItem->IsBold();
542 }
543
544 // -----------------------------------------------------------------------------
545 // navigation
546 // -----------------------------------------------------------------------------
547
548 wxTreeItemId wxTreeCtrl::GetParent(const wxTreeItemId& item) const
549 {
550 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), _T("invalid tree item") );
551
552 return item.m_pItem->GetParent();
553 }
554
555 wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item, long& cookie) const
556 {
557 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), _T("invalid tree item") );
558
559 cookie = 0;
560 return GetNextChild(item, cookie);
561 }
562
563 wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& item, long& cookie) const
564 {
565 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), _T("invalid tree item") );
566
567 wxArrayTreeItems& children = item.m_pItem->GetChildren();
568 if ( (size_t)cookie < children.Count() )
569 {
570 return children.Item(cookie++);
571 }
572 else
573 {
574 // there are no more of them
575 return wxTreeItemId();
576 }
577 }
578
579 wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const
580 {
581 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), _T("invalid tree item") );
582
583 wxArrayTreeItems& children = item.m_pItem->GetChildren();
584 return (children.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children.Last()));
585 }
586
587 wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
588 {
589 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), _T("invalid tree item") );
590
591 wxGenericTreeItem *i = item.m_pItem;
592 wxGenericTreeItem *parent = i->GetParent();
593 if ( parent == NULL )
594 {
595 // root item doesn't have any siblings
596 return wxTreeItemId();
597 }
598
599 wxArrayTreeItems& siblings = parent->GetChildren();
600 int index = siblings.Index(i);
601 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
602
603 size_t n = (size_t)(index + 1);
604 return n == siblings.Count() ? wxTreeItemId() : wxTreeItemId(siblings[n]);
605 }
606
607 wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
608 {
609 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), _T("invalid tree item") );
610
611 wxGenericTreeItem *i = item.m_pItem;
612 wxGenericTreeItem *parent = i->GetParent();
613 if ( parent == NULL )
614 {
615 // root item doesn't have any siblings
616 return wxTreeItemId();
617 }
618
619 wxArrayTreeItems& siblings = parent->GetChildren();
620 int index = siblings.Index(i);
621 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
622
623 return index == 0 ? wxTreeItemId()
624 : wxTreeItemId(siblings[(size_t)(index - 1)]);
625 }
626
627 wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const
628 {
629 wxFAIL_MSG(_T("not implemented"));
630
631 return wxTreeItemId();
632 }
633
634 wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
635 {
636 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), _T("invalid tree item") );
637
638 wxFAIL_MSG(_T("not implemented"));
639
640 return wxTreeItemId();
641 }
642
643 wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
644 {
645 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), _T("invalid tree item") );
646
647 wxFAIL_MSG(_T("not implemented"));
648
649 return wxTreeItemId();
650 }
651
652 // -----------------------------------------------------------------------------
653 // operations
654 // -----------------------------------------------------------------------------
655
656 wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parentId,
657 size_t previous,
658 const wxString& text,
659 int image, int selImage,
660 wxTreeItemData *data)
661 {
662 wxGenericTreeItem *parent = parentId.m_pItem;
663 if ( !parent )
664 {
665 // should we give a warning here?
666 return AddRoot(text, image, selImage, data);
667 }
668
669 wxClientDC dc(this);
670 wxGenericTreeItem *item = new wxGenericTreeItem(parent,
671 text, dc,
672 image, selImage,
673 data);
674
675 if ( data != NULL )
676 {
677 data->m_pItem = item;
678 }
679
680 parent->Insert( item, previous );
681
682 m_dirty = TRUE;
683
684 return item;
685 }
686
687 wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text,
688 int image, int selImage,
689 wxTreeItemData *data)
690 {
691 wxCHECK_MSG( !m_anchor, wxTreeItemId(), _T("tree can have only one root") );
692
693 wxClientDC dc(this);
694 m_anchor = new wxGenericTreeItem((wxGenericTreeItem *)NULL, text, dc,
695 image, selImage, data);
696 if ( data != NULL )
697 {
698 data->m_pItem = m_anchor;
699 }
700
701 AdjustMyScrollbars();
702 Refresh();
703
704 return m_anchor;
705 }
706
707 wxTreeItemId wxTreeCtrl::PrependItem(const wxTreeItemId& parent,
708 const wxString& text,
709 int image, int selImage,
710 wxTreeItemData *data)
711 {
712 return DoInsertItem(parent, 0u, text, image, selImage, data);
713 }
714
715 wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parentId,
716 const wxTreeItemId& idPrevious,
717 const wxString& text,
718 int image, int selImage,
719 wxTreeItemData *data)
720 {
721 wxGenericTreeItem *parent = parentId.m_pItem;
722 if ( !parent )
723 {
724 // should we give a warning here?
725 return AddRoot(text, image, selImage, data);
726 }
727
728 int index = parent->GetChildren().Index(idPrevious.m_pItem);
729 wxASSERT_MSG( index != wxNOT_FOUND,
730 _T("previous item in wxTreeCtrl::InsertItem() is not a sibling") );
731 return DoInsertItem(parentId, (size_t)index, text, image, selImage, data);
732 }
733
734 wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parentId,
735 const wxString& text,
736 int image, int selImage,
737 wxTreeItemData *data)
738 {
739 wxGenericTreeItem *parent = parentId.m_pItem;
740 if ( !parent )
741 {
742 // should we give a warning here?
743 return AddRoot(text, image, selImage, data);
744 }
745
746 return DoInsertItem(parent, parent->GetChildren().Count(), text,
747 image, selImage, data);
748 }
749
750 void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem *item)
751 {
752 wxTreeEvent event( wxEVT_COMMAND_TREE_DELETE_ITEM, GetId() );
753 event.m_item = item;
754 event.SetEventObject( this );
755 ProcessEvent( event );
756 }
757
758 void wxTreeCtrl::DeleteChildren(const wxTreeItemId& itemId)
759 {
760 wxGenericTreeItem *item = itemId.m_pItem;
761 item->DeleteChildren(this);
762
763 m_dirty = TRUE;
764 }
765
766 void wxTreeCtrl::Delete(const wxTreeItemId& itemId)
767 {
768 wxGenericTreeItem *item = itemId.m_pItem;
769 wxGenericTreeItem *parent = item->GetParent();
770
771 if ( parent )
772 {
773 parent->GetChildren().Remove(item);
774 }
775
776 item->DeleteChildren(this);
777 SendDeleteEvent(item);
778 delete item;
779
780 m_dirty = TRUE;
781 }
782
783 void wxTreeCtrl::DeleteAllItems()
784 {
785 if ( m_anchor )
786 {
787 m_anchor->DeleteChildren(this);
788 delete m_anchor;
789
790 m_anchor = NULL;
791
792 m_dirty = TRUE;
793 }
794 }
795
796 void wxTreeCtrl::Expand(const wxTreeItemId& itemId)
797 {
798 wxGenericTreeItem *item = itemId.m_pItem;
799
800 if ( !item->HasPlus() )
801 return;
802
803 if ( item->IsExpanded() )
804 return;
805
806 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_EXPANDING, GetId() );
807 event.m_item = item;
808 event.SetEventObject( this );
809 if ( ProcessEvent( event ) && event.m_code )
810 {
811 // cancelled by program
812 return;
813 }
814
815 item->Expand();
816 CalculatePositions();
817
818 RefreshSubtree(item);
819
820 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED);
821 ProcessEvent( event );
822 }
823
824 void wxTreeCtrl::Collapse(const wxTreeItemId& itemId)
825 {
826 wxGenericTreeItem *item = itemId.m_pItem;
827
828 if ( !item->IsExpanded() )
829 return;
830
831 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING, GetId() );
832 event.m_item = item;
833 event.SetEventObject( this );
834 if ( ProcessEvent( event ) && event.m_code )
835 {
836 // cancelled by program
837 return;
838 }
839
840 item->Collapse();
841
842 wxArrayTreeItems& children = item->GetChildren();
843 size_t count = children.Count();
844 for ( size_t n = 0; n < count; n++ )
845 {
846 Collapse(children[n]);
847 }
848
849 CalculatePositions();
850
851 RefreshSubtree(item);
852
853 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED);
854 ProcessEvent( event );
855 }
856
857 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
858 {
859 Collapse(item);
860 DeleteChildren(item);
861 }
862
863 void wxTreeCtrl::Toggle(const wxTreeItemId& itemId)
864 {
865 wxGenericTreeItem *item = itemId.m_pItem;
866
867 if ( item->IsExpanded() )
868 Collapse(itemId);
869 else
870 Expand(itemId);
871 }
872
873 void wxTreeCtrl::Unselect()
874 {
875 if ( m_current )
876 {
877 m_current->SetHilight( FALSE );
878 RefreshLine( m_current );
879 }
880 }
881
882 void wxTreeCtrl::SelectItem(const wxTreeItemId& itemId)
883 {
884 wxGenericTreeItem *item = itemId.m_pItem;
885
886 if ( m_current != item )
887 {
888 wxTreeEvent event( wxEVT_COMMAND_TREE_SEL_CHANGING, GetId() );
889 event.m_item = item;
890 event.m_itemOld = m_current;
891 event.SetEventObject( this );
892 if ( GetEventHandler()->ProcessEvent( event ) && event.WasVetoed() )
893 return;
894
895 if ( m_current )
896 {
897 m_current->SetHilight( FALSE );
898 RefreshLine( m_current );
899 }
900
901 m_current = item;
902 m_current->SetHilight( TRUE );
903 RefreshLine( m_current );
904
905 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
906 GetEventHandler()->ProcessEvent( event );
907 }
908 }
909
910 void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item)
911 {
912 wxGenericTreeItem *gitem = item.m_pItem;
913
914 // first expand all parent branches
915 wxGenericTreeItem *parent = gitem->GetParent();
916 while ( parent && !parent->IsExpanded() )
917 {
918 Expand(parent);
919
920 parent = parent->GetParent();
921 }
922
923 // now scroll to the item
924 int item_y = gitem->GetY();
925
926 int start_x = 0;
927 int start_y = 0;
928 ViewStart( &start_x, &start_y );
929 start_y *= 10;
930
931 int client_h = 0;
932 int client_w = 0;
933 GetClientSize( &client_w, &client_h );
934
935 if (item_y < start_y+3)
936 {
937 int x = 0;
938 int y = 0;
939 m_anchor->GetSize( x, y );
940 y += 2*m_lineHeight;
941 int x_pos = GetScrollPos( wxHORIZONTAL );
942 SetScrollbars( 10, 10, x/10, y/10, x_pos, (item_y-client_h/2)/10 );
943 }
944 else if (item_y > start_y+client_h-16)
945 {
946 int x = 0;
947 int y = 0;
948 m_anchor->GetSize( x, y );
949 y += 2*m_lineHeight;
950 int x_pos = GetScrollPos( wxHORIZONTAL );
951 SetScrollbars( 10, 10, x/10, y/10, x_pos, (item_y-client_h/2)/10 );
952 }
953 }
954
955 void wxTreeCtrl::ScrollTo(const wxTreeItemId& WXUNUSED(item))
956 {
957 wxFAIL_MSG(_T("not implemented"));
958 }
959
960 wxTextCtrl *wxTreeCtrl::EditLabel( const wxTreeItemId& WXUNUSED(item),
961 wxClassInfo* WXUNUSED(textCtrlClass) )
962 {
963 wxFAIL_MSG(_T("not implemented"));
964
965 return (wxTextCtrl*)NULL;
966 }
967
968 wxTextCtrl *wxTreeCtrl::GetEditControl() const
969 {
970 wxFAIL_MSG(_T("not implemented"));
971
972 return (wxTextCtrl*)NULL;
973 }
974
975 void wxTreeCtrl::EndEditLabel(const wxTreeItemId& WXUNUSED(item), bool WXUNUSED(discardChanges))
976 {
977 wxFAIL_MSG(_T("not implemented"));
978 }
979
980 // FIXME: tree sorting functions are not reentrant and not MT-safe!
981 static wxTreeCtrl *s_treeBeingSorted = NULL;
982
983 static int tree_ctrl_compare_func(wxGenericTreeItem **item1,
984 wxGenericTreeItem **item2)
985 {
986 wxCHECK_MSG( s_treeBeingSorted, 0, _T("bug in wxTreeCtrl::SortChildren()") );
987
988 return s_treeBeingSorted->OnCompareItems(*item1, *item2);
989 }
990
991 int wxTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
992 const wxTreeItemId& item2)
993 {
994 return wxStrcmp(GetItemText(item1), GetItemText(item2));
995 }
996
997 void wxTreeCtrl::SortChildren(const wxTreeItemId& itemId)
998 {
999 wxCHECK_RET( itemId.IsOk(), _T("invalid tree item") );
1000
1001 wxGenericTreeItem *item = itemId.m_pItem;
1002
1003 wxCHECK_RET( !s_treeBeingSorted,
1004 _T("wxTreeCtrl::SortChildren is not reentrant") );
1005
1006 wxArrayTreeItems& children = item->GetChildren();
1007 if ( children.Count() > 1 )
1008 {
1009 s_treeBeingSorted = this;
1010 children.Sort(tree_ctrl_compare_func);
1011 s_treeBeingSorted = NULL;
1012
1013 m_dirty = TRUE;
1014 }
1015 //else: don't make the tree dirty as nothing changed
1016 }
1017
1018 wxImageList *wxTreeCtrl::GetImageList() const
1019 {
1020 return m_imageListNormal;
1021 }
1022
1023 wxImageList *wxTreeCtrl::GetStateImageList() const
1024 {
1025 return m_imageListState;
1026 }
1027
1028 void wxTreeCtrl::SetImageList(wxImageList *imageList)
1029 {
1030 m_imageListNormal = imageList;
1031 // calculate a m_lineHeight value from the image sizes
1032 wxPaintDC dc(this);
1033 PrepareDC( dc );
1034 m_lineHeight = (int)(dc.GetCharHeight() + 4);
1035 int
1036 width = 0,
1037 height = 0,
1038 n = m_imageListNormal->GetImageCount();
1039 for(int i = 0; i < n ; i++)
1040 {
1041 m_imageListNormal->GetSize(i, width, height);
1042 height += height/5; //20% extra spacing
1043 if(height > m_lineHeight) m_lineHeight = height;
1044 }
1045 }
1046
1047 void wxTreeCtrl::SetStateImageList(wxImageList *imageList)
1048 {
1049 m_imageListState = imageList;
1050 }
1051
1052 // -----------------------------------------------------------------------------
1053 // helpers
1054 // -----------------------------------------------------------------------------
1055
1056 void wxTreeCtrl::AdjustMyScrollbars()
1057 {
1058 if (m_anchor)
1059 {
1060 int x = 0;
1061 int y = 0;
1062 m_anchor->GetSize( x, y );
1063 y += 2*m_lineHeight;
1064 int x_pos = GetScrollPos( wxHORIZONTAL );
1065 int y_pos = GetScrollPos( wxVERTICAL );
1066 SetScrollbars( 10, 10, x/10, y/10, x_pos, y_pos );
1067 }
1068 else
1069 {
1070 SetScrollbars( 0, 0, 0, 0 );
1071 }
1072 }
1073
1074 void wxTreeCtrl::PaintItem(wxGenericTreeItem *item, wxDC& dc)
1075 {
1076 // render bold items in bold
1077 wxFont fontOld;
1078 wxFont fontNew;
1079
1080 if (item->IsBold())
1081 {
1082 fontOld = dc.GetFont();
1083 if (fontOld.Ok())
1084 {
1085 // VZ: is there any better way to make a bold variant of old font?
1086 fontNew = wxFont( fontOld.GetPointSize(),
1087 fontOld.GetFamily(),
1088 fontOld.GetStyle(),
1089 wxBOLD,
1090 fontOld.GetUnderlined());
1091 dc.SetFont(fontNew);
1092 }
1093 else
1094 {
1095 wxFAIL_MSG(_T("wxDC::GetFont() failed!"));
1096 }
1097 }
1098
1099 long text_w = 0;
1100 long text_h = 0;
1101 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
1102
1103 int image_h = 0;
1104 int image_w = 0;
1105 if ((item->IsExpanded()) && (item->GetSelectedImage() != -1))
1106 {
1107 m_imageListNormal->GetSize( item->GetSelectedImage(), image_w, image_h );
1108 image_w += 4;
1109 }
1110 else if (item->GetImage() != -1)
1111 {
1112 m_imageListNormal->GetSize( item->GetImage(), image_w, image_h );
1113 image_w += 4;
1114 }
1115
1116 int total_h = (image_h > text_h) ? image_h : text_h;
1117 if(m_lineHeight > total_h) total_h = m_lineHeight;
1118
1119 dc.DrawRectangle( item->GetX()-2, item->GetY(), image_w+text_w+4, total_h );
1120
1121 if ((item->IsExpanded()) && (item->GetSelectedImage() != -1))
1122 {
1123 dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, total_h );
1124 m_imageListNormal->Draw( item->GetSelectedImage(), dc,
1125 item->GetX(),
1126 item->GetY() +((total_h > image_h)?((total_h-image_h)/2):0),
1127 wxIMAGELIST_DRAW_TRANSPARENT );
1128 dc.DestroyClippingRegion();
1129 }
1130 else if (item->GetImage() != -1)
1131 {
1132 dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, total_h );
1133 m_imageListNormal->Draw( item->GetImage(), dc,
1134 item->GetX(),
1135 item->GetY() +((total_h > image_h)?((total_h-image_h)/2):0),
1136 wxIMAGELIST_DRAW_TRANSPARENT );
1137 dc.DestroyClippingRegion();
1138 }
1139
1140 dc.SetBackgroundMode(wxTRANSPARENT);
1141 dc.DrawText( item->GetText(), image_w + item->GetX(), item->GetY()
1142 + ((total_h > text_h) ? (total_h - text_h)/2 : 0));
1143
1144 // restore normal font for bold items
1145 if (fontOld.Ok())
1146 {
1147 dc.SetFont( fontOld);
1148 }
1149 }
1150
1151 void wxTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
1152 {
1153 int horizX = level*m_indent;
1154
1155 item->SetX( horizX+33 );
1156 item->SetY( y-m_lineHeight/2 );
1157 item->SetHeight( m_lineHeight );
1158
1159 item->SetCross( horizX+15, y );
1160
1161 int oldY = y;
1162
1163 int exposed_x = dc.LogicalToDeviceX( 0 );
1164 int exposed_y = dc.LogicalToDeviceY( item->GetY()-2 );
1165
1166 if (IsExposed( exposed_x, exposed_y, 10000, m_lineHeight+4 )) // 10000 = very much
1167 {
1168 int startX = horizX;
1169 int endX = horizX + 10;
1170
1171 if (!item->HasChildren()) endX += 20;
1172
1173 dc.DrawLine( startX, y, endX, y );
1174
1175 if (item->HasPlus())
1176 {
1177 dc.DrawLine( horizX+20, y, horizX+30, y );
1178 dc.SetPen( *wxGREY_PEN );
1179 dc.SetBrush( *wxWHITE_BRUSH );
1180 dc.DrawRectangle( horizX+10, y-4, 11, 9 );
1181 dc.SetPen( *wxBLACK_PEN );
1182 dc.DrawLine( horizX+13, y, horizX+18, y );
1183
1184 if (!item->IsExpanded())
1185 {
1186 dc.DrawLine( horizX+15, y-2, horizX+15, y+3 );
1187 }
1188 }
1189
1190 if (item->HasHilight())
1191 {
1192 dc.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT ) );
1193
1194 dc.SetBrush( *m_hilightBrush );
1195
1196 if (m_hasFocus)
1197 dc.SetPen( *wxBLACK_PEN );
1198 else
1199 dc.SetPen( *wxTRANSPARENT_PEN );
1200
1201 PaintItem(item, dc);
1202
1203 dc.SetPen( *wxBLACK_PEN );
1204 dc.SetTextForeground( *wxBLACK );
1205 dc.SetBrush( *wxWHITE_BRUSH );
1206 }
1207 else
1208 {
1209 dc.SetBrush( *wxWHITE_BRUSH );
1210 dc.SetPen( *wxTRANSPARENT_PEN );
1211
1212 PaintItem(item, dc);
1213
1214 dc.SetPen( *wxBLACK_PEN );
1215 }
1216 }
1217
1218 if (item->IsExpanded())
1219 {
1220 int semiOldY = y;
1221
1222 wxArrayTreeItems& children = item->GetChildren();
1223 size_t count = children.Count();
1224 for ( size_t n = 0; n < count; n++ )
1225 {
1226 y += m_lineHeight;
1227 semiOldY = y;
1228 PaintLevel( children[n], dc, level+1, y );
1229 }
1230
1231 // it may happen that the item is expanded but has no items (when you
1232 // delete all its children for example) - don't draw the vertical line
1233 // in this case
1234 if (count > 0)
1235 dc.DrawLine( horizX+15, oldY+5, horizX+15, semiOldY );
1236 }
1237 }
1238
1239 // -----------------------------------------------------------------------------
1240 // wxWindows callbacks
1241 // -----------------------------------------------------------------------------
1242
1243 void wxTreeCtrl::OnPaint( wxPaintEvent &WXUNUSED(event) )
1244 {
1245 if ( !m_anchor )
1246 return;
1247
1248 wxPaintDC dc(this);
1249 PrepareDC( dc );
1250
1251 dc.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT ) );
1252
1253 dc.SetPen( m_dottedPen );
1254 if(GetImageList() == NULL)
1255 m_lineHeight = (int)(dc.GetCharHeight() + 4);
1256
1257 int y = m_lineHeight / 2 + 2;
1258 PaintLevel( m_anchor, dc, 0, y );
1259 }
1260
1261 void wxTreeCtrl::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
1262 {
1263 m_hasFocus = TRUE;
1264
1265 if (m_current) RefreshLine( m_current );
1266 }
1267
1268 void wxTreeCtrl::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
1269 {
1270 m_hasFocus = FALSE;
1271
1272 if (m_current) RefreshLine( m_current );
1273 }
1274
1275 void wxTreeCtrl::OnChar( wxKeyEvent &event )
1276 {
1277 wxTreeEvent te( wxEVT_COMMAND_TREE_KEY_DOWN, GetId() );
1278 te.m_code = event.KeyCode();
1279 te.SetEventObject( this );
1280 GetEventHandler()->ProcessEvent( te );
1281
1282 if (m_current == 0)
1283 {
1284 event.Skip();
1285 return;
1286 }
1287
1288 switch (event.KeyCode())
1289 {
1290 case '+':
1291 case WXK_ADD:
1292 if (m_current->HasPlus() && !IsExpanded(m_current))
1293 {
1294 Expand(m_current);
1295 }
1296 break;
1297
1298 case '-':
1299 case WXK_SUBTRACT:
1300 if (IsExpanded(m_current))
1301 {
1302 Collapse(m_current);
1303 }
1304 break;
1305
1306 case '*':
1307 case WXK_MULTIPLY:
1308 Toggle(m_current);
1309 break;
1310
1311 case ' ':
1312 case WXK_RETURN:
1313 {
1314 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
1315 event.m_item = m_current;
1316 event.m_code = 0;
1317 event.SetEventObject( this );
1318 GetEventHandler()->ProcessEvent( event );
1319 }
1320 break;
1321
1322 // up goes to the previous sibling or to the last of its children if
1323 // it's expanded
1324 case WXK_UP:
1325 {
1326 wxTreeItemId prev = GetPrevSibling( m_current );
1327 if (!prev)
1328 {
1329 prev = GetParent( m_current );
1330 if (prev)
1331 {
1332 long cockie = 0;
1333 wxTreeItemId current = m_current;
1334 if (current == GetFirstChild( prev, cockie ))
1335 {
1336 // otherwise we return to where we came from
1337 SelectItem( prev );
1338 EnsureVisible( prev );
1339 break;
1340 }
1341 }
1342 }
1343 if (prev)
1344 {
1345 while ( IsExpanded(prev) && HasChildren(prev) )
1346 {
1347 wxTreeItemId child = GetLastChild(prev);
1348 if ( child )
1349 {
1350 prev = child;
1351 }
1352 }
1353
1354 SelectItem( prev );
1355 EnsureVisible( prev );
1356 }
1357 }
1358 break;
1359
1360 // left arrow goes to the parent
1361 case WXK_LEFT:
1362 {
1363 wxTreeItemId prev = GetParent( m_current );
1364 if (prev)
1365 {
1366 EnsureVisible( prev );
1367 SelectItem( prev );
1368 }
1369 }
1370 break;
1371
1372 case WXK_RIGHT:
1373 // this works the same as the down arrow except that we also expand the
1374 // item if it wasn't expanded yet
1375 Expand(m_current);
1376 // fall through
1377
1378 case WXK_DOWN:
1379 {
1380 if (IsExpanded(m_current) && HasChildren(m_current))
1381 {
1382 long cookie = 0;
1383 wxTreeItemId child = GetFirstChild( m_current, cookie );
1384 SelectItem( child );
1385 EnsureVisible( child );
1386 }
1387 else
1388 {
1389 wxTreeItemId next = GetNextSibling( m_current );
1390 if (next == 0)
1391 {
1392 wxTreeItemId current = m_current;
1393 while (current && !next)
1394 {
1395 current = GetParent( current );
1396 if (current) next = GetNextSibling( current );
1397 }
1398 }
1399 if (next != 0)
1400 {
1401 SelectItem( next );
1402 EnsureVisible( next );
1403 }
1404 }
1405 }
1406 break;
1407
1408 // <End> selects the last visible tree item
1409 case WXK_END:
1410 {
1411 wxTreeItemId last = GetRootItem();
1412
1413 while ( last.IsOk() && IsExpanded(last) )
1414 {
1415 wxTreeItemId lastChild = GetLastChild(last);
1416
1417 // it may happen if the item was expanded but then all of
1418 // its children have been deleted - so IsExpanded() returned
1419 // TRUE, but GetLastChild() returned invalid item
1420 if ( !lastChild )
1421 break;
1422
1423 last = lastChild;
1424 }
1425
1426 if ( last.IsOk() )
1427 {
1428 EnsureVisible( last );
1429 SelectItem( last );
1430 }
1431 }
1432 break;
1433
1434 // <Home> selects the root item
1435 case WXK_HOME:
1436 {
1437 wxTreeItemId prev = GetRootItem();
1438 if (prev)
1439 {
1440 EnsureVisible( prev );
1441 SelectItem( prev );
1442 }
1443 }
1444 break;
1445
1446 default:
1447 event.Skip();
1448 }
1449 }
1450
1451 wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& WXUNUSED(flags))
1452 {
1453 wxClientDC dc(this);
1454 PrepareDC(dc);
1455 long x = dc.DeviceToLogicalX( (long)point.x );
1456 long y = dc.DeviceToLogicalY( (long)point.y );
1457
1458 bool onButton = FALSE;
1459 return m_anchor->HitTest( wxPoint(x, y), onButton );
1460 }
1461
1462 void wxTreeCtrl::OnMouse( wxMouseEvent &event )
1463 {
1464 if (!event.LeftIsDown()) m_dragCount = 0;
1465
1466 if ( !(event.LeftDown() || event.LeftDClick() || event.Dragging()) ) return;
1467
1468 if ( !m_anchor ) return;
1469
1470 wxClientDC dc(this);
1471 PrepareDC(dc);
1472 long x = dc.DeviceToLogicalX( (long)event.GetX() );
1473 long y = dc.DeviceToLogicalY( (long)event.GetY() );
1474
1475 bool onButton = FALSE;
1476 wxGenericTreeItem *item = m_anchor->HitTest( wxPoint(x,y), onButton );
1477
1478 if (item == NULL) return; /* we hit the blank area */
1479
1480 if (event.Dragging())
1481 {
1482 if (m_dragCount == 2) /* small drag latency (3?) */
1483 {
1484 m_dragCount = 0;
1485
1486 wxTreeEvent nevent(wxEVT_COMMAND_TREE_BEGIN_DRAG, GetId());
1487 nevent.m_item = m_current;
1488 nevent.SetEventObject(this);
1489 GetEventHandler()->ProcessEvent(nevent);
1490 }
1491 else
1492 {
1493 m_dragCount++;
1494 }
1495 return;
1496 }
1497
1498 if (!IsSelected(item))
1499 SelectItem(item); /* we dont support multiple selections, BTW */
1500
1501 if (event.LeftDClick())
1502 {
1503 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
1504 event.m_item = item;
1505 event.m_code = 0;
1506 event.SetEventObject( this );
1507 GetEventHandler()->ProcessEvent( event );
1508 }
1509
1510 if (onButton)
1511 {
1512 Toggle( item );
1513 }
1514 }
1515
1516 void wxTreeCtrl::OnIdle( wxIdleEvent &WXUNUSED(event) )
1517 {
1518 /* after all changes have been done to the tree control,
1519 * we actually redraw the tree when everything is over */
1520
1521 if (!m_dirty)
1522 return;
1523
1524 m_dirty = FALSE;
1525
1526 CalculatePositions();
1527
1528 AdjustMyScrollbars();
1529 }
1530
1531 // -----------------------------------------------------------------------------
1532
1533 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
1534 {
1535 int horizX = level*m_indent;
1536
1537 item->SetX( horizX+33 );
1538 item->SetY( y-m_lineHeight/2 );
1539 item->SetHeight( m_lineHeight );
1540
1541 if ( !item->IsExpanded() )
1542 {
1543 // we dont need to calculate collapsed branches
1544 return;
1545 }
1546
1547 wxArrayTreeItems& children = item->GetChildren();
1548 size_t count = children.Count();
1549 for ( size_t n = 0; n < count; n++ )
1550 {
1551 y += m_lineHeight;
1552 CalculateLevel( children[n], dc, level+1, y ); // recurse
1553 }
1554 }
1555
1556 void wxTreeCtrl::CalculatePositions()
1557 {
1558 if ( !m_anchor ) return;
1559
1560 wxClientDC dc(this);
1561 PrepareDC( dc );
1562
1563 dc.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT ) );
1564
1565 dc.SetPen( m_dottedPen );
1566 if(GetImageList() == NULL)
1567 m_lineHeight = (int)(dc.GetCharHeight() + 4);
1568
1569 int y = m_lineHeight / 2 + 2;
1570 CalculateLevel( m_anchor, dc, 0, y ); // start recursion
1571 }
1572
1573 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem *item)
1574 {
1575 wxClientDC dc(this);
1576 PrepareDC(dc);
1577
1578 int cw = 0;
1579 int ch = 0;
1580 GetClientSize( &cw, &ch );
1581
1582 wxRect rect;
1583 rect.x = dc.LogicalToDeviceX( 0 );
1584 rect.width = cw;
1585 rect.y = dc.LogicalToDeviceY( item->GetY() );
1586 rect.height = ch;
1587
1588 Refresh( TRUE, &rect );
1589
1590 AdjustMyScrollbars();
1591 }
1592
1593 void wxTreeCtrl::RefreshLine( wxGenericTreeItem *item )
1594 {
1595 wxClientDC dc(this);
1596 PrepareDC( dc );
1597
1598 wxRect rect;
1599 rect.x = dc.LogicalToDeviceX( item->GetX() - 2 );
1600 rect.y = dc.LogicalToDeviceY( item->GetY() - 2 );
1601 rect.width = 1000;
1602 rect.height = dc.GetCharHeight() + 6;
1603
1604 Refresh( TRUE, &rect );
1605 }
1606