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