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