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