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