]> git.saurik.com Git - wxWidgets.git/blob - src/generic/treectrl.cpp
TreeCtrl indentation and spacing
[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 m_spacing = 18;
357
358 m_hilightBrush = new wxBrush
359 (
360 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT),
361 wxSOLID
362 );
363
364 m_imageListNormal =
365 m_imageListState = (wxImageList *) NULL;
366
367 m_dragCount = 0;
368 }
369
370 bool wxTreeCtrl::Create(wxWindow *parent, wxWindowID id,
371 const wxPoint& pos, const wxSize& size,
372 long style,
373 const wxValidator &validator,
374 const wxString& name )
375 {
376 Init();
377
378 wxScrolledWindow::Create( parent, id, pos, size, style|wxHSCROLL|wxVSCROLL, name );
379
380 SetValidator( validator );
381
382 SetBackgroundColour( *wxWHITE );
383 m_dottedPen = wxPen( *wxBLACK, 0, 0 );
384
385 return TRUE;
386 }
387
388 wxTreeCtrl::~wxTreeCtrl()
389 {
390 wxDELETE( m_hilightBrush );
391
392 DeleteAllItems();
393 }
394
395 // -----------------------------------------------------------------------------
396 // accessors
397 // -----------------------------------------------------------------------------
398
399 size_t wxTreeCtrl::GetCount() const
400 {
401 return m_anchor == NULL ? 0u : m_anchor->GetChildrenCount();
402 }
403
404 void wxTreeCtrl::SetIndent(unsigned int indent)
405 {
406 m_indent = indent;
407 m_dirty = TRUE;
408 Refresh();
409 }
410
411 void wxTreeCtrl::SetSpacing(unsigned int spacing)
412 {
413 m_spacing = spacing;
414 m_dirty = TRUE;
415 Refresh();
416 }
417
418 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item, bool recursively)
419 {
420 wxCHECK_MSG( item.IsOk(), 0u, _T("invalid tree item") );
421
422 return item.m_pItem->GetChildrenCount(recursively);
423 }
424
425 // -----------------------------------------------------------------------------
426 // functions to work with tree items
427 // -----------------------------------------------------------------------------
428
429 wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const
430 {
431 wxCHECK_MSG( item.IsOk(), _T(""), _T("invalid tree item") );
432
433 return item.m_pItem->GetText();
434 }
435
436 int wxTreeCtrl::GetItemImage(const wxTreeItemId& item) const
437 {
438 wxCHECK_MSG( item.IsOk(), -1, _T("invalid tree item") );
439
440 return item.m_pItem->GetImage();
441 }
442
443 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId& item) const
444 {
445 wxCHECK_MSG( item.IsOk(), -1, _T("invalid tree item") );
446
447 return item.m_pItem->GetSelectedImage();
448 }
449
450 wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const
451 {
452 wxCHECK_MSG( item.IsOk(), NULL, _T("invalid tree item") );
453
454 return item.m_pItem->GetData();
455 }
456
457 void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text)
458 {
459 wxCHECK_RET( item.IsOk(), _T("invalid tree item") );
460
461 wxClientDC dc(this);
462 wxGenericTreeItem *pItem = item.m_pItem;
463 pItem->SetText(text, dc);
464 RefreshLine(pItem);
465 }
466
467 void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, int image)
468 {
469 wxCHECK_RET( item.IsOk(), _T("invalid tree item") );
470
471 wxGenericTreeItem *pItem = item.m_pItem;
472 pItem->SetImage(image);
473 RefreshLine(pItem);
474 }
475
476 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId& item, int image)
477 {
478 wxCHECK_RET( item.IsOk(), _T("invalid tree item") );
479
480 wxGenericTreeItem *pItem = item.m_pItem;
481 pItem->SetSelectedImage(image);
482 RefreshLine(pItem);
483 }
484
485 void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
486 {
487 wxCHECK_RET( item.IsOk(), _T("invalid tree item") );
488
489 item.m_pItem->SetData(data);
490 }
491
492 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
493 {
494 wxCHECK_RET( item.IsOk(), _T("invalid tree item") );
495
496 wxGenericTreeItem *pItem = item.m_pItem;
497 pItem->SetHasPlus(has);
498 RefreshLine(pItem);
499 }
500
501 void wxTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
502 {
503 wxCHECK_RET( item.IsOk(), _T("invalid tree item") );
504
505 // avoid redrawing the tree if no real change
506 wxGenericTreeItem *pItem = item.m_pItem;
507 if ( pItem->IsBold() != bold )
508 {
509 pItem->SetBold(bold);
510 RefreshLine(pItem);
511 }
512 }
513
514 // -----------------------------------------------------------------------------
515 // item status inquiries
516 // -----------------------------------------------------------------------------
517
518 bool wxTreeCtrl::IsVisible(const wxTreeItemId& WXUNUSED(item)) const
519 {
520 wxFAIL_MSG(_T("not implemented"));
521
522 return TRUE;
523 }
524
525 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
526 {
527 wxCHECK_MSG( item.IsOk(), FALSE, _T("invalid tree item") );
528
529 return !item.m_pItem->GetChildren().IsEmpty();
530 }
531
532 bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const
533 {
534 wxCHECK_MSG( item.IsOk(), FALSE, _T("invalid tree item") );
535
536 return item.m_pItem->IsExpanded();
537 }
538
539 bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const
540 {
541 wxCHECK_MSG( item.IsOk(), FALSE, _T("invalid tree item") );
542
543 return item.m_pItem->HasHilight();
544 }
545
546 bool wxTreeCtrl::IsBold(const wxTreeItemId& item) const
547 {
548 wxCHECK_MSG( item.IsOk(), FALSE, _T("invalid tree item") );
549
550 return item.m_pItem->IsBold();
551 }
552
553 // -----------------------------------------------------------------------------
554 // navigation
555 // -----------------------------------------------------------------------------
556
557 wxTreeItemId wxTreeCtrl::GetParent(const wxTreeItemId& item) const
558 {
559 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), _T("invalid tree item") );
560
561 return item.m_pItem->GetParent();
562 }
563
564 wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item, long& cookie) const
565 {
566 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), _T("invalid tree item") );
567
568 cookie = 0;
569 return GetNextChild(item, cookie);
570 }
571
572 wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& item, long& cookie) const
573 {
574 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), _T("invalid tree item") );
575
576 wxArrayTreeItems& children = item.m_pItem->GetChildren();
577 if ( (size_t)cookie < children.Count() )
578 {
579 return children.Item(cookie++);
580 }
581 else
582 {
583 // there are no more of them
584 return wxTreeItemId();
585 }
586 }
587
588 wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const
589 {
590 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), _T("invalid tree item") );
591
592 wxArrayTreeItems& children = item.m_pItem->GetChildren();
593 return (children.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children.Last()));
594 }
595
596 wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
597 {
598 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), _T("invalid tree item") );
599
600 wxGenericTreeItem *i = item.m_pItem;
601 wxGenericTreeItem *parent = i->GetParent();
602 if ( parent == NULL )
603 {
604 // root item doesn't have any siblings
605 return wxTreeItemId();
606 }
607
608 wxArrayTreeItems& siblings = parent->GetChildren();
609 int index = siblings.Index(i);
610 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
611
612 size_t n = (size_t)(index + 1);
613 return n == siblings.Count() ? wxTreeItemId() : wxTreeItemId(siblings[n]);
614 }
615
616 wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
617 {
618 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), _T("invalid tree item") );
619
620 wxGenericTreeItem *i = item.m_pItem;
621 wxGenericTreeItem *parent = i->GetParent();
622 if ( parent == NULL )
623 {
624 // root item doesn't have any siblings
625 return wxTreeItemId();
626 }
627
628 wxArrayTreeItems& siblings = parent->GetChildren();
629 int index = siblings.Index(i);
630 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
631
632 return index == 0 ? wxTreeItemId()
633 : wxTreeItemId(siblings[(size_t)(index - 1)]);
634 }
635
636 wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const
637 {
638 wxFAIL_MSG(_T("not implemented"));
639
640 return wxTreeItemId();
641 }
642
643 wxTreeItemId wxTreeCtrl::GetNextVisible(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 wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
653 {
654 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), _T("invalid tree item") );
655
656 wxFAIL_MSG(_T("not implemented"));
657
658 return wxTreeItemId();
659 }
660
661 // -----------------------------------------------------------------------------
662 // operations
663 // -----------------------------------------------------------------------------
664
665 wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parentId,
666 size_t previous,
667 const wxString& text,
668 int image, int selImage,
669 wxTreeItemData *data)
670 {
671 wxGenericTreeItem *parent = parentId.m_pItem;
672 if ( !parent )
673 {
674 // should we give a warning here?
675 return AddRoot(text, image, selImage, data);
676 }
677
678 wxClientDC dc(this);
679 wxGenericTreeItem *item = new wxGenericTreeItem(parent,
680 text, dc,
681 image, selImage,
682 data);
683
684 if ( data != NULL )
685 {
686 data->m_pItem = item;
687 }
688
689 parent->Insert( item, previous );
690
691 m_dirty = TRUE;
692
693 return item;
694 }
695
696 wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text,
697 int image, int selImage,
698 wxTreeItemData *data)
699 {
700 wxCHECK_MSG( !m_anchor, wxTreeItemId(), _T("tree can have only one root") );
701
702 wxClientDC dc(this);
703 m_anchor = new wxGenericTreeItem((wxGenericTreeItem *)NULL, text, dc,
704 image, selImage, data);
705 if ( data != NULL )
706 {
707 data->m_pItem = m_anchor;
708 }
709
710 AdjustMyScrollbars();
711 Refresh();
712
713 return m_anchor;
714 }
715
716 wxTreeItemId wxTreeCtrl::PrependItem(const wxTreeItemId& parent,
717 const wxString& text,
718 int image, int selImage,
719 wxTreeItemData *data)
720 {
721 return DoInsertItem(parent, 0u, text, image, selImage, data);
722 }
723
724 wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parentId,
725 const wxTreeItemId& idPrevious,
726 const wxString& text,
727 int image, int selImage,
728 wxTreeItemData *data)
729 {
730 wxGenericTreeItem *parent = parentId.m_pItem;
731 if ( !parent )
732 {
733 // should we give a warning here?
734 return AddRoot(text, image, selImage, data);
735 }
736
737 int index = parent->GetChildren().Index(idPrevious.m_pItem);
738 wxASSERT_MSG( index != wxNOT_FOUND,
739 _T("previous item in wxTreeCtrl::InsertItem() is not a sibling") );
740 return DoInsertItem(parentId, (size_t)index, text, image, selImage, data);
741 }
742
743 wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parentId,
744 const wxString& text,
745 int image, int selImage,
746 wxTreeItemData *data)
747 {
748 wxGenericTreeItem *parent = parentId.m_pItem;
749 if ( !parent )
750 {
751 // should we give a warning here?
752 return AddRoot(text, image, selImage, data);
753 }
754
755 return DoInsertItem(parent, parent->GetChildren().Count(), text,
756 image, selImage, data);
757 }
758
759 void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem *item)
760 {
761 wxTreeEvent event( wxEVT_COMMAND_TREE_DELETE_ITEM, GetId() );
762 event.m_item = item;
763 event.SetEventObject( this );
764 ProcessEvent( event );
765 }
766
767 void wxTreeCtrl::DeleteChildren(const wxTreeItemId& itemId)
768 {
769 wxGenericTreeItem *item = itemId.m_pItem;
770 item->DeleteChildren(this);
771
772 m_dirty = TRUE;
773 }
774
775 void wxTreeCtrl::Delete(const wxTreeItemId& itemId)
776 {
777 wxGenericTreeItem *item = itemId.m_pItem;
778 wxGenericTreeItem *parent = item->GetParent();
779
780 if ( parent )
781 {
782 parent->GetChildren().Remove(item);
783 }
784
785 item->DeleteChildren(this);
786 SendDeleteEvent(item);
787 delete item;
788
789 m_dirty = TRUE;
790 }
791
792 void wxTreeCtrl::DeleteAllItems()
793 {
794 if ( m_anchor )
795 {
796 m_anchor->DeleteChildren(this);
797 delete m_anchor;
798
799 m_anchor = NULL;
800
801 m_dirty = TRUE;
802 }
803 }
804
805 void wxTreeCtrl::Expand(const wxTreeItemId& itemId)
806 {
807 wxGenericTreeItem *item = itemId.m_pItem;
808
809 if ( !item->HasPlus() )
810 return;
811
812 if ( item->IsExpanded() )
813 return;
814
815 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_EXPANDING, GetId() );
816 event.m_item = item;
817 event.SetEventObject( this );
818 if ( ProcessEvent( event ) && event.m_code )
819 {
820 // cancelled by program
821 return;
822 }
823
824 item->Expand();
825 CalculatePositions();
826
827 RefreshSubtree(item);
828
829 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED);
830 ProcessEvent( event );
831 }
832
833 void wxTreeCtrl::Collapse(const wxTreeItemId& itemId)
834 {
835 wxGenericTreeItem *item = itemId.m_pItem;
836
837 if ( !item->IsExpanded() )
838 return;
839
840 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING, GetId() );
841 event.m_item = item;
842 event.SetEventObject( this );
843 if ( ProcessEvent( event ) && event.m_code )
844 {
845 // cancelled by program
846 return;
847 }
848
849 item->Collapse();
850
851 wxArrayTreeItems& children = item->GetChildren();
852 size_t count = children.Count();
853 for ( size_t n = 0; n < count; n++ )
854 {
855 Collapse(children[n]);
856 }
857
858 CalculatePositions();
859
860 RefreshSubtree(item);
861
862 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED);
863 ProcessEvent( event );
864 }
865
866 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
867 {
868 Collapse(item);
869 DeleteChildren(item);
870 }
871
872 void wxTreeCtrl::Toggle(const wxTreeItemId& itemId)
873 {
874 wxGenericTreeItem *item = itemId.m_pItem;
875
876 if ( item->IsExpanded() )
877 Collapse(itemId);
878 else
879 Expand(itemId);
880 }
881
882 void wxTreeCtrl::Unselect()
883 {
884 if ( m_current )
885 {
886 m_current->SetHilight( FALSE );
887 RefreshLine( m_current );
888 }
889 }
890
891 void wxTreeCtrl::SelectItem(const wxTreeItemId& itemId)
892 {
893 wxGenericTreeItem *item = itemId.m_pItem;
894
895 if ( m_current != item )
896 {
897 wxTreeEvent event( wxEVT_COMMAND_TREE_SEL_CHANGING, GetId() );
898 event.m_item = item;
899 event.m_itemOld = m_current;
900 event.SetEventObject( this );
901 if ( GetEventHandler()->ProcessEvent( event ) && event.WasVetoed() )
902 return;
903
904 if ( m_current )
905 {
906 m_current->SetHilight( FALSE );
907 RefreshLine( m_current );
908 }
909
910 m_current = item;
911 m_current->SetHilight( TRUE );
912 RefreshLine( m_current );
913
914 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
915 GetEventHandler()->ProcessEvent( event );
916 }
917 }
918
919 void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item)
920 {
921 wxGenericTreeItem *gitem = item.m_pItem;
922
923 // first expand all parent branches
924 wxGenericTreeItem *parent = gitem->GetParent();
925 while ( parent && !parent->IsExpanded() )
926 {
927 Expand(parent);
928
929 parent = parent->GetParent();
930 }
931
932 // now scroll to the item
933 int item_y = gitem->GetY();
934
935 int start_x = 0;
936 int start_y = 0;
937 ViewStart( &start_x, &start_y );
938 start_y *= 10;
939
940 int client_h = 0;
941 int client_w = 0;
942 GetClientSize( &client_w, &client_h );
943
944 if (item_y < start_y+3)
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 else if (item_y > start_y+client_h-16)
954 {
955 int x = 0;
956 int y = 0;
957 m_anchor->GetSize( x, y );
958 y += 2*m_lineHeight;
959 int x_pos = GetScrollPos( wxHORIZONTAL );
960 SetScrollbars( 10, 10, x/10, y/10, x_pos, (item_y-client_h/2)/10 );
961 }
962 }
963
964 void wxTreeCtrl::ScrollTo(const wxTreeItemId& WXUNUSED(item))
965 {
966 wxFAIL_MSG(_T("not implemented"));
967 }
968
969 wxTextCtrl *wxTreeCtrl::EditLabel( const wxTreeItemId& WXUNUSED(item),
970 wxClassInfo* WXUNUSED(textCtrlClass) )
971 {
972 wxFAIL_MSG(_T("not implemented"));
973
974 return (wxTextCtrl*)NULL;
975 }
976
977 wxTextCtrl *wxTreeCtrl::GetEditControl() const
978 {
979 wxFAIL_MSG(_T("not implemented"));
980
981 return (wxTextCtrl*)NULL;
982 }
983
984 void wxTreeCtrl::EndEditLabel(const wxTreeItemId& WXUNUSED(item), bool WXUNUSED(discardChanges))
985 {
986 wxFAIL_MSG(_T("not implemented"));
987 }
988
989 // FIXME: tree sorting functions are not reentrant and not MT-safe!
990 static wxTreeCtrl *s_treeBeingSorted = NULL;
991
992 static int tree_ctrl_compare_func(wxGenericTreeItem **item1,
993 wxGenericTreeItem **item2)
994 {
995 wxCHECK_MSG( s_treeBeingSorted, 0, _T("bug in wxTreeCtrl::SortChildren()") );
996
997 return s_treeBeingSorted->OnCompareItems(*item1, *item2);
998 }
999
1000 int wxTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
1001 const wxTreeItemId& item2)
1002 {
1003 return wxStrcmp(GetItemText(item1), GetItemText(item2));
1004 }
1005
1006 void wxTreeCtrl::SortChildren(const wxTreeItemId& itemId)
1007 {
1008 wxCHECK_RET( itemId.IsOk(), _T("invalid tree item") );
1009
1010 wxGenericTreeItem *item = itemId.m_pItem;
1011
1012 wxCHECK_RET( !s_treeBeingSorted,
1013 _T("wxTreeCtrl::SortChildren is not reentrant") );
1014
1015 wxArrayTreeItems& children = item->GetChildren();
1016 if ( children.Count() > 1 )
1017 {
1018 s_treeBeingSorted = this;
1019 children.Sort(tree_ctrl_compare_func);
1020 s_treeBeingSorted = NULL;
1021
1022 m_dirty = TRUE;
1023 }
1024 //else: don't make the tree dirty as nothing changed
1025 }
1026
1027 wxImageList *wxTreeCtrl::GetImageList() const
1028 {
1029 return m_imageListNormal;
1030 }
1031
1032 wxImageList *wxTreeCtrl::GetStateImageList() const
1033 {
1034 return m_imageListState;
1035 }
1036
1037 void wxTreeCtrl::SetImageList(wxImageList *imageList)
1038 {
1039 m_imageListNormal = imageList;
1040 // calculate a m_lineHeight value from the image sizes
1041 wxPaintDC dc(this);
1042 PrepareDC( dc );
1043 m_lineHeight = (int)(dc.GetCharHeight() + 4);
1044 int
1045 width = 0,
1046 height = 0,
1047 n = m_imageListNormal->GetImageCount();
1048 for(int i = 0; i < n ; i++)
1049 {
1050 m_imageListNormal->GetSize(i, width, height);
1051 height += height/5; //20% extra spacing
1052 if(height > m_lineHeight) m_lineHeight = height;
1053 }
1054 }
1055
1056 void wxTreeCtrl::SetStateImageList(wxImageList *imageList)
1057 {
1058 m_imageListState = imageList;
1059 }
1060
1061 // -----------------------------------------------------------------------------
1062 // helpers
1063 // -----------------------------------------------------------------------------
1064
1065 void wxTreeCtrl::AdjustMyScrollbars()
1066 {
1067 if (m_anchor)
1068 {
1069 int x = 0;
1070 int y = 0;
1071 m_anchor->GetSize( x, y );
1072 y += 2*m_lineHeight;
1073 int x_pos = GetScrollPos( wxHORIZONTAL );
1074 int y_pos = GetScrollPos( wxVERTICAL );
1075 SetScrollbars( 10, 10, x/10, y/10, x_pos, y_pos );
1076 }
1077 else
1078 {
1079 SetScrollbars( 0, 0, 0, 0 );
1080 }
1081 }
1082
1083 void wxTreeCtrl::PaintItem(wxGenericTreeItem *item, wxDC& dc)
1084 {
1085 // render bold items in bold
1086 wxFont fontOld;
1087 wxFont fontNew;
1088
1089 if (item->IsBold())
1090 {
1091 fontOld = dc.GetFont();
1092 if (fontOld.Ok())
1093 {
1094 // VZ: is there any better way to make a bold variant of old font?
1095 fontNew = wxFont( fontOld.GetPointSize(),
1096 fontOld.GetFamily(),
1097 fontOld.GetStyle(),
1098 wxBOLD,
1099 fontOld.GetUnderlined());
1100 dc.SetFont(fontNew);
1101 }
1102 else
1103 {
1104 wxFAIL_MSG(_T("wxDC::GetFont() failed!"));
1105 }
1106 }
1107
1108 long text_w = 0;
1109 long text_h = 0;
1110 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
1111
1112 int image_h = 0;
1113 int image_w = 0;
1114 if ((item->IsExpanded()) && (item->GetSelectedImage() != -1))
1115 {
1116 m_imageListNormal->GetSize( item->GetSelectedImage(), image_w, image_h );
1117 image_w += 4;
1118 }
1119 else if (item->GetImage() != -1)
1120 {
1121 m_imageListNormal->GetSize( item->GetImage(), image_w, image_h );
1122 image_w += 4;
1123 }
1124
1125 int total_h = (image_h > text_h) ? image_h : text_h;
1126 if(m_lineHeight > total_h) total_h = m_lineHeight;
1127
1128 dc.DrawRectangle( item->GetX()-2, item->GetY(), image_w+text_w+4, total_h );
1129
1130 if ((item->IsExpanded()) && (item->GetSelectedImage() != -1))
1131 {
1132 dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, total_h );
1133 m_imageListNormal->Draw( item->GetSelectedImage(), 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 else if (item->GetImage() != -1)
1140 {
1141 dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, total_h );
1142 m_imageListNormal->Draw( item->GetImage(), dc,
1143 item->GetX(),
1144 item->GetY() +((total_h > image_h)?((total_h-image_h)/2):0),
1145 wxIMAGELIST_DRAW_TRANSPARENT );
1146 dc.DestroyClippingRegion();
1147 }
1148
1149 dc.SetBackgroundMode(wxTRANSPARENT);
1150 dc.DrawText( item->GetText(), image_w + item->GetX(), item->GetY()
1151 + ((total_h > text_h) ? (total_h - text_h)/2 : 0));
1152
1153 // restore normal font for bold items
1154 if (fontOld.Ok())
1155 {
1156 dc.SetFont( fontOld);
1157 }
1158 }
1159
1160 void wxTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
1161 {
1162 int horizX = level*m_indent;
1163
1164 item->SetX( horizX+m_indent+m_spacing );
1165 item->SetY( y-m_lineHeight/2 );
1166 item->SetHeight( m_lineHeight );
1167
1168 item->SetCross( horizX+m_indent, y );
1169
1170 int oldY = y;
1171
1172 int exposed_x = dc.LogicalToDeviceX( 0 );
1173 int exposed_y = dc.LogicalToDeviceY( item->GetY()-2 );
1174
1175 if (IsExposed( exposed_x, exposed_y, 10000, m_lineHeight+4 )) // 10000 = very much
1176 {
1177 int startX = horizX;
1178 int endX = horizX + (m_indent-5);
1179
1180 // if (!item->HasChildren()) endX += (m_indent+5);
1181 if (!item->HasChildren()) endX += 20;
1182
1183 dc.DrawLine( startX, y, endX, y );
1184
1185 if (item->HasPlus())
1186 {
1187 dc.DrawLine( horizX+(m_indent+5), y, horizX+(m_indent+15), y );
1188 dc.SetPen( *wxGREY_PEN );
1189 dc.SetBrush( *wxWHITE_BRUSH );
1190 dc.DrawRectangle( horizX+(m_indent-5), y-4, 11, 9 );
1191 dc.SetPen( *wxBLACK_PEN );
1192 dc.DrawLine( horizX+(m_indent-2), y, horizX+(m_indent+3), y );
1193
1194 if (!item->IsExpanded())
1195 {
1196 dc.DrawLine( horizX+m_indent, y-2, horizX+m_indent, y+3 );
1197 }
1198 }
1199
1200 if (item->HasHilight())
1201 {
1202 dc.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT ) );
1203
1204 dc.SetBrush( *m_hilightBrush );
1205
1206 if (m_hasFocus)
1207 dc.SetPen( *wxBLACK_PEN );
1208 else
1209 dc.SetPen( *wxTRANSPARENT_PEN );
1210
1211 PaintItem(item, dc);
1212
1213 dc.SetPen( *wxBLACK_PEN );
1214 dc.SetTextForeground( *wxBLACK );
1215 dc.SetBrush( *wxWHITE_BRUSH );
1216 }
1217 else
1218 {
1219 dc.SetBrush( *wxWHITE_BRUSH );
1220 dc.SetPen( *wxTRANSPARENT_PEN );
1221
1222 PaintItem(item, dc);
1223
1224 dc.SetPen( *wxBLACK_PEN );
1225 }
1226 }
1227
1228 if (item->IsExpanded())
1229 {
1230 int semiOldY = y;
1231
1232 wxArrayTreeItems& children = item->GetChildren();
1233 size_t count = children.Count();
1234 for ( size_t n = 0; n < count; n++ )
1235 {
1236 y += m_lineHeight;
1237 semiOldY = y;
1238 PaintLevel( children[n], dc, level+1, y );
1239 }
1240
1241 // it may happen that the item is expanded but has no items (when you
1242 // delete all its children for example) - don't draw the vertical line
1243 // in this case
1244 if (count > 0)
1245 dc.DrawLine( horizX+m_indent, oldY+5, horizX+m_indent, semiOldY );
1246 }
1247 }
1248
1249 // -----------------------------------------------------------------------------
1250 // wxWindows callbacks
1251 // -----------------------------------------------------------------------------
1252
1253 void wxTreeCtrl::OnPaint( wxPaintEvent &WXUNUSED(event) )
1254 {
1255 if ( !m_anchor )
1256 return;
1257
1258 wxPaintDC dc(this);
1259 PrepareDC( dc );
1260
1261 dc.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT ) );
1262
1263 dc.SetPen( m_dottedPen );
1264 if(GetImageList() == NULL)
1265 m_lineHeight = (int)(dc.GetCharHeight() + 4);
1266
1267 int y = m_lineHeight / 2 + 2;
1268 PaintLevel( m_anchor, dc, 0, y );
1269 }
1270
1271 void wxTreeCtrl::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
1272 {
1273 m_hasFocus = TRUE;
1274
1275 if (m_current) RefreshLine( m_current );
1276 }
1277
1278 void wxTreeCtrl::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
1279 {
1280 m_hasFocus = FALSE;
1281
1282 if (m_current) RefreshLine( m_current );
1283 }
1284
1285 void wxTreeCtrl::OnChar( wxKeyEvent &event )
1286 {
1287 wxTreeEvent te( wxEVT_COMMAND_TREE_KEY_DOWN, GetId() );
1288 te.m_code = event.KeyCode();
1289 te.SetEventObject( this );
1290 GetEventHandler()->ProcessEvent( te );
1291
1292 if (m_current == 0)
1293 {
1294 event.Skip();
1295 return;
1296 }
1297
1298 switch (event.KeyCode())
1299 {
1300 case '+':
1301 case WXK_ADD:
1302 if (m_current->HasPlus() && !IsExpanded(m_current))
1303 {
1304 Expand(m_current);
1305 }
1306 break;
1307
1308 case '-':
1309 case WXK_SUBTRACT:
1310 if (IsExpanded(m_current))
1311 {
1312 Collapse(m_current);
1313 }
1314 break;
1315
1316 case '*':
1317 case WXK_MULTIPLY:
1318 Toggle(m_current);
1319 break;
1320
1321 case ' ':
1322 case WXK_RETURN:
1323 {
1324 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
1325 event.m_item = m_current;
1326 event.m_code = 0;
1327 event.SetEventObject( this );
1328 GetEventHandler()->ProcessEvent( event );
1329 }
1330 break;
1331
1332 // up goes to the previous sibling or to the last of its children if
1333 // it's expanded
1334 case WXK_UP:
1335 {
1336 wxTreeItemId prev = GetPrevSibling( m_current );
1337 if (!prev)
1338 {
1339 prev = GetParent( m_current );
1340 if (prev)
1341 {
1342 long cockie = 0;
1343 wxTreeItemId current = m_current;
1344 if (current == GetFirstChild( prev, cockie ))
1345 {
1346 // otherwise we return to where we came from
1347 SelectItem( prev );
1348 EnsureVisible( prev );
1349 break;
1350 }
1351 }
1352 }
1353 if (prev)
1354 {
1355 while ( IsExpanded(prev) && HasChildren(prev) )
1356 {
1357 wxTreeItemId child = GetLastChild(prev);
1358 if ( child )
1359 {
1360 prev = child;
1361 }
1362 }
1363
1364 SelectItem( prev );
1365 EnsureVisible( prev );
1366 }
1367 }
1368 break;
1369
1370 // left arrow goes to the parent
1371 case WXK_LEFT:
1372 {
1373 wxTreeItemId prev = GetParent( m_current );
1374 if (prev)
1375 {
1376 EnsureVisible( prev );
1377 SelectItem( prev );
1378 }
1379 }
1380 break;
1381
1382 case WXK_RIGHT:
1383 // this works the same as the down arrow except that we also expand the
1384 // item if it wasn't expanded yet
1385 Expand(m_current);
1386 // fall through
1387
1388 case WXK_DOWN:
1389 {
1390 if (IsExpanded(m_current) && HasChildren(m_current))
1391 {
1392 long cookie = 0;
1393 wxTreeItemId child = GetFirstChild( m_current, cookie );
1394 SelectItem( child );
1395 EnsureVisible( child );
1396 }
1397 else
1398 {
1399 wxTreeItemId next = GetNextSibling( m_current );
1400 if (next == 0)
1401 {
1402 wxTreeItemId current = m_current;
1403 while (current && !next)
1404 {
1405 current = GetParent( current );
1406 if (current) next = GetNextSibling( current );
1407 }
1408 }
1409 if (next != 0)
1410 {
1411 SelectItem( next );
1412 EnsureVisible( next );
1413 }
1414 }
1415 }
1416 break;
1417
1418 // <End> selects the last visible tree item
1419 case WXK_END:
1420 {
1421 wxTreeItemId last = GetRootItem();
1422
1423 while ( last.IsOk() && IsExpanded(last) )
1424 {
1425 wxTreeItemId lastChild = GetLastChild(last);
1426
1427 // it may happen if the item was expanded but then all of
1428 // its children have been deleted - so IsExpanded() returned
1429 // TRUE, but GetLastChild() returned invalid item
1430 if ( !lastChild )
1431 break;
1432
1433 last = lastChild;
1434 }
1435
1436 if ( last.IsOk() )
1437 {
1438 EnsureVisible( last );
1439 SelectItem( last );
1440 }
1441 }
1442 break;
1443
1444 // <Home> selects the root item
1445 case WXK_HOME:
1446 {
1447 wxTreeItemId prev = GetRootItem();
1448 if (prev)
1449 {
1450 EnsureVisible( prev );
1451 SelectItem( prev );
1452 }
1453 }
1454 break;
1455
1456 default:
1457 event.Skip();
1458 }
1459 }
1460
1461 wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& WXUNUSED(flags))
1462 {
1463 wxClientDC dc(this);
1464 PrepareDC(dc);
1465 long x = dc.DeviceToLogicalX( (long)point.x );
1466 long y = dc.DeviceToLogicalY( (long)point.y );
1467
1468 bool onButton = FALSE;
1469 return m_anchor->HitTest( wxPoint(x, y), onButton );
1470 }
1471
1472 void wxTreeCtrl::OnMouse( wxMouseEvent &event )
1473 {
1474 if (!event.LeftIsDown()) m_dragCount = 0;
1475
1476 if ( !(event.LeftDown() || event.LeftDClick() || event.Dragging()) ) return;
1477
1478 if ( !m_anchor ) return;
1479
1480 wxClientDC dc(this);
1481 PrepareDC(dc);
1482 long x = dc.DeviceToLogicalX( (long)event.GetX() );
1483 long y = dc.DeviceToLogicalY( (long)event.GetY() );
1484
1485 bool onButton = FALSE;
1486 wxGenericTreeItem *item = m_anchor->HitTest( wxPoint(x,y), onButton );
1487
1488 if (item == NULL) return; /* we hit the blank area */
1489
1490 if (event.Dragging())
1491 {
1492 if (m_dragCount == 2) /* small drag latency (3?) */
1493 {
1494 m_dragCount = 0;
1495
1496 wxTreeEvent nevent(wxEVT_COMMAND_TREE_BEGIN_DRAG, GetId());
1497 nevent.m_item = m_current;
1498 nevent.SetEventObject(this);
1499 GetEventHandler()->ProcessEvent(nevent);
1500 }
1501 else
1502 {
1503 m_dragCount++;
1504 }
1505 return;
1506 }
1507
1508 if (!IsSelected(item))
1509 SelectItem(item); /* we dont support multiple selections, BTW */
1510
1511 if (event.LeftDClick())
1512 {
1513 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
1514 event.m_item = item;
1515 event.m_code = 0;
1516 event.SetEventObject( this );
1517 GetEventHandler()->ProcessEvent( event );
1518 }
1519
1520 if (onButton)
1521 {
1522 Toggle( item );
1523 }
1524 }
1525
1526 void wxTreeCtrl::OnIdle( wxIdleEvent &WXUNUSED(event) )
1527 {
1528 /* after all changes have been done to the tree control,
1529 * we actually redraw the tree when everything is over */
1530
1531 if (!m_dirty)
1532 return;
1533
1534 m_dirty = FALSE;
1535
1536 CalculatePositions();
1537
1538 AdjustMyScrollbars();
1539 }
1540
1541 // -----------------------------------------------------------------------------
1542
1543 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
1544 {
1545 int horizX = level*m_indent;
1546
1547 item->SetX( horizX+m_indent+m_spacing );
1548 item->SetY( y-m_lineHeight/2 );
1549 item->SetHeight( m_lineHeight );
1550
1551 if ( !item->IsExpanded() )
1552 {
1553 // we dont need to calculate collapsed branches
1554 return;
1555 }
1556
1557 wxArrayTreeItems& children = item->GetChildren();
1558 size_t count = children.Count();
1559 for ( size_t n = 0; n < count; n++ )
1560 {
1561 y += m_lineHeight;
1562 CalculateLevel( children[n], dc, level+1, y ); // recurse
1563 }
1564 }
1565
1566 void wxTreeCtrl::CalculatePositions()
1567 {
1568 if ( !m_anchor ) return;
1569
1570 wxClientDC dc(this);
1571 PrepareDC( dc );
1572
1573 dc.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT ) );
1574
1575 dc.SetPen( m_dottedPen );
1576 if(GetImageList() == NULL)
1577 m_lineHeight = (int)(dc.GetCharHeight() + 4);
1578
1579 int y = m_lineHeight / 2 + 2;
1580 CalculateLevel( m_anchor, dc, 0, y ); // start recursion
1581 }
1582
1583 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem *item)
1584 {
1585 wxClientDC dc(this);
1586 PrepareDC(dc);
1587
1588 int cw = 0;
1589 int ch = 0;
1590 GetClientSize( &cw, &ch );
1591
1592 wxRect rect;
1593 rect.x = dc.LogicalToDeviceX( 0 );
1594 rect.width = cw;
1595 rect.y = dc.LogicalToDeviceY( item->GetY() );
1596 rect.height = ch;
1597
1598 Refresh( TRUE, &rect );
1599
1600 AdjustMyScrollbars();
1601 }
1602
1603 void wxTreeCtrl::RefreshLine( wxGenericTreeItem *item )
1604 {
1605 wxClientDC dc(this);
1606 PrepareDC( dc );
1607
1608 wxRect rect;
1609 rect.x = dc.LogicalToDeviceX( item->GetX() - 2 );
1610 rect.y = dc.LogicalToDeviceY( item->GetY() - 2 );
1611 rect.width = 1000;
1612 rect.height = dc.GetCharHeight() + 6;
1613
1614 Refresh( TRUE, &rect );
1615 }
1616