]> git.saurik.com Git - wxWidgets.git/blob - src/generic/treectlg.cpp
reset the tooltip text before changing it, this apparently prevents a spurious redraw...
[wxWidgets.git] / src / generic / treectlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/treectlg.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 and Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // =============================================================================
13 // declarations
14 // =============================================================================
15
16 // -----------------------------------------------------------------------------
17 // headers
18 // -----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_TREECTRL
28
29 #include "wx/treectrl.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/dcclient.h"
33 #include "wx/timer.h"
34 #include "wx/settings.h"
35 #include "wx/listbox.h"
36 #include "wx/textctrl.h"
37 #endif
38
39 #include "wx/generic/treectlg.h"
40 #include "wx/imaglist.h"
41
42 #include "wx/renderer.h"
43
44 #ifdef __WXMAC__
45 #include "wx/osx/private.h"
46 #endif
47
48 // -----------------------------------------------------------------------------
49 // array types
50 // -----------------------------------------------------------------------------
51
52 class WXDLLIMPEXP_FWD_CORE wxGenericTreeItem;
53
54 WX_DEFINE_ARRAY_PTR(wxGenericTreeItem *, wxArrayGenericTreeItems);
55
56 // ----------------------------------------------------------------------------
57 // constants
58 // ----------------------------------------------------------------------------
59
60 static const int NO_IMAGE = -1;
61
62 static const int PIXELS_PER_UNIT = 10;
63
64 // the margin between the item state image and the item normal image
65 static const int MARGIN_BETWEEN_STATE_AND_IMAGE = 2;
66
67 // the margin between the item image and the item text
68 static const int MARGIN_BETWEEN_IMAGE_AND_TEXT = 4;
69
70 // -----------------------------------------------------------------------------
71 // private classes
72 // -----------------------------------------------------------------------------
73
74 // timer used for enabling in-place edit
75 class WXDLLEXPORT wxTreeRenameTimer: public wxTimer
76 {
77 public:
78 // start editing the current item after half a second (if the mouse hasn't
79 // been clicked/moved)
80 enum { DELAY = 500 };
81
82 wxTreeRenameTimer( wxGenericTreeCtrl *owner );
83
84 virtual void Notify();
85
86 private:
87 wxGenericTreeCtrl *m_owner;
88
89 wxDECLARE_NO_COPY_CLASS(wxTreeRenameTimer);
90 };
91
92 // control used for in-place edit
93 class WXDLLEXPORT wxTreeTextCtrl: public wxTextCtrl
94 {
95 public:
96 wxTreeTextCtrl(wxGenericTreeCtrl *owner, wxGenericTreeItem *item);
97
98 void EndEdit( bool discardChanges );
99
100 const wxGenericTreeItem* item() const { return m_itemEdited; }
101
102 protected:
103 void OnChar( wxKeyEvent &event );
104 void OnKeyUp( wxKeyEvent &event );
105 void OnKillFocus( wxFocusEvent &event );
106
107 bool AcceptChanges();
108 void Finish( bool setfocus );
109
110 private:
111 wxGenericTreeCtrl *m_owner;
112 wxGenericTreeItem *m_itemEdited;
113 wxString m_startValue;
114 bool m_aboutToFinish;
115
116 DECLARE_EVENT_TABLE()
117 wxDECLARE_NO_COPY_CLASS(wxTreeTextCtrl);
118 };
119
120 // timer used to clear wxGenericTreeCtrl::m_findPrefix if no key was pressed
121 // for a sufficiently long time
122 class WXDLLEXPORT wxTreeFindTimer : public wxTimer
123 {
124 public:
125 // reset the current prefix after half a second of inactivity
126 enum { DELAY = 500 };
127
128 wxTreeFindTimer( wxGenericTreeCtrl *owner ) { m_owner = owner; }
129
130 virtual void Notify() { m_owner->m_findPrefix.clear(); }
131
132 private:
133 wxGenericTreeCtrl *m_owner;
134
135 wxDECLARE_NO_COPY_CLASS(wxTreeFindTimer);
136 };
137
138 // a tree item
139 class WXDLLEXPORT wxGenericTreeItem
140 {
141 public:
142 // ctors & dtor
143 wxGenericTreeItem()
144 {
145 m_data = NULL;
146 m_widthText =
147 m_heightText = -1;
148 }
149
150 wxGenericTreeItem( wxGenericTreeItem *parent,
151 const wxString& text,
152 int image,
153 int selImage,
154 wxTreeItemData *data );
155
156 ~wxGenericTreeItem();
157
158 // trivial accessors
159 wxArrayGenericTreeItems& GetChildren() { return m_children; }
160
161 const wxString& GetText() const { return m_text; }
162 int GetImage(wxTreeItemIcon which = wxTreeItemIcon_Normal) const
163 { return m_images[which]; }
164 wxTreeItemData *GetData() const { return m_data; }
165 int GetState() const { return m_state; }
166
167 // returns the current image for the item (depending on its
168 // selected/expanded/whatever state)
169 int GetCurrentImage() const;
170
171 void SetText(const wxString& text)
172 {
173 m_text = text;
174
175 ResetTextSize();
176 }
177
178 void SetImage(int image, wxTreeItemIcon which)
179 {
180 m_images[which] = image;
181 m_width = 0;
182 }
183
184 void SetData(wxTreeItemData *data) { m_data = data; }
185 void SetState(int state) { m_state = state; m_width = 0; }
186
187 void SetHasPlus(bool has = true) { m_hasPlus = has; }
188
189 void SetBold(bool bold)
190 {
191 m_isBold = bold;
192
193 ResetTextSize();
194 }
195
196 int GetX() const { return m_x; }
197 int GetY() const { return m_y; }
198
199 void SetX(int x) { m_x = x; }
200 void SetY(int y) { m_y = y; }
201
202 int GetHeight() const { return m_height; }
203 int GetWidth() const { return m_width; }
204
205 int GetTextHeight() const
206 {
207 wxASSERT_MSG( m_heightText != -1, "must call CalculateSize() first" );
208
209 return m_heightText;
210 }
211
212 int GetTextWidth() const
213 {
214 wxASSERT_MSG( m_widthText != -1, "must call CalculateSize() first" );
215
216 return m_widthText;
217 }
218
219 wxGenericTreeItem *GetParent() const { return m_parent; }
220
221 // sets the items font for the specified DC if it uses any special font or
222 // simply returns false otherwise
223 bool SetFont(wxGenericTreeCtrl *control, wxDC& dc) const
224 {
225 wxFont font;
226
227 wxTreeItemAttr * const attr = GetAttributes();
228 if ( attr && attr->HasFont() )
229 font = attr->GetFont();
230 else if ( IsBold() )
231 font = control->m_boldFont;
232 else
233 return false;
234
235 dc.SetFont(font);
236 return true;
237 }
238
239 // operations
240
241 // deletes all children notifying the treectrl about it
242 void DeleteChildren(wxGenericTreeCtrl *tree);
243
244 // get count of all children (and grand children if 'recursively')
245 size_t GetChildrenCount(bool recursively = true) const;
246
247 void Insert(wxGenericTreeItem *child, size_t index)
248 { m_children.Insert(child, index); }
249
250 // calculate and cache the item size using either the provided DC (which is
251 // supposed to have wxGenericTreeCtrl::m_normalFont selected into it!) or a
252 // wxClientDC on the control window
253 void CalculateSize(wxGenericTreeCtrl *control, wxDC& dc)
254 { DoCalculateSize(control, dc, true /* dc uses normal font */); }
255 void CalculateSize(wxGenericTreeCtrl *control);
256
257 void GetSize( int &x, int &y, const wxGenericTreeCtrl* );
258
259 void ResetSize() { m_width = 0; }
260 void ResetTextSize() { m_width = 0; m_widthText = -1; }
261 void RecursiveResetSize();
262 void RecursiveResetTextSize();
263
264 // return the item at given position (or NULL if no item), onButton is
265 // true if the point belongs to the item's button, otherwise it lies
266 // on the item's label
267 wxGenericTreeItem *HitTest( const wxPoint& point,
268 const wxGenericTreeCtrl *,
269 int &flags,
270 int level );
271
272 void Expand() { m_isCollapsed = false; }
273 void Collapse() { m_isCollapsed = true; }
274
275 void SetHilight( bool set = true ) { m_hasHilight = set; }
276
277 // status inquiries
278 bool HasChildren() const { return !m_children.IsEmpty(); }
279 bool IsSelected() const { return m_hasHilight != 0; }
280 bool IsExpanded() const { return !m_isCollapsed; }
281 bool HasPlus() const { return m_hasPlus || HasChildren(); }
282 bool IsBold() const { return m_isBold != 0; }
283
284 // attributes
285 // get them - may be NULL
286 wxTreeItemAttr *GetAttributes() const { return m_attr; }
287 // get them ensuring that the pointer is not NULL
288 wxTreeItemAttr& Attr()
289 {
290 if ( !m_attr )
291 {
292 m_attr = new wxTreeItemAttr;
293 m_ownsAttr = true;
294 }
295 return *m_attr;
296 }
297 // set them
298 void SetAttributes(wxTreeItemAttr *attr)
299 {
300 if ( m_ownsAttr ) delete m_attr;
301 m_attr = attr;
302 m_ownsAttr = false;
303 m_width = 0;
304 m_widthText = -1;
305 }
306 // set them and delete when done
307 void AssignAttributes(wxTreeItemAttr *attr)
308 {
309 SetAttributes(attr);
310 m_ownsAttr = true;
311 m_width = 0;
312 m_widthText = -1;
313 }
314
315 private:
316 // calculate the size of this item, i.e. set m_width, m_height and
317 // m_widthText and m_heightText properly
318 //
319 // if dcUsesNormalFont is true, the current dc font must be the normal tree
320 // control font
321 void DoCalculateSize(wxGenericTreeCtrl *control,
322 wxDC& dc,
323 bool dcUsesNormalFont);
324
325 // since there can be very many of these, we save size by chosing
326 // the smallest representation for the elements and by ordering
327 // the members to avoid padding.
328 wxString m_text; // label to be rendered for item
329 int m_widthText;
330 int m_heightText;
331
332 wxTreeItemData *m_data; // user-provided data
333
334 int m_state; // item state
335
336 wxArrayGenericTreeItems m_children; // list of children
337 wxGenericTreeItem *m_parent; // parent of this item
338
339 wxTreeItemAttr *m_attr; // attributes???
340
341 // tree ctrl images for the normal, selected, expanded and
342 // expanded+selected states
343 int m_images[wxTreeItemIcon_Max];
344
345 wxCoord m_x; // (virtual) offset from top
346 wxCoord m_y; // (virtual) offset from left
347 int m_width; // width of this item
348 int m_height; // height of this item
349
350 // use bitfields to save size
351 unsigned int m_isCollapsed :1;
352 unsigned int m_hasHilight :1; // same as focused
353 unsigned int m_hasPlus :1; // used for item which doesn't have
354 // children but has a [+] button
355 unsigned int m_isBold :1; // render the label in bold font
356 unsigned int m_ownsAttr :1; // delete attribute when done
357
358 wxDECLARE_NO_COPY_CLASS(wxGenericTreeItem);
359 };
360
361 // =============================================================================
362 // implementation
363 // =============================================================================
364
365 // ----------------------------------------------------------------------------
366 // private functions
367 // ----------------------------------------------------------------------------
368
369 // translate the key or mouse event flags to the type of selection we're
370 // dealing with
371 static void EventFlagsToSelType(long style,
372 bool shiftDown,
373 bool ctrlDown,
374 bool &is_multiple,
375 bool &extended_select,
376 bool &unselect_others)
377 {
378 is_multiple = (style & wxTR_MULTIPLE) != 0;
379 extended_select = shiftDown && is_multiple;
380 unselect_others = !(extended_select || (ctrlDown && is_multiple));
381 }
382
383 // check if the given item is under another one
384 static bool
385 IsDescendantOf(const wxGenericTreeItem *parent, const wxGenericTreeItem *item)
386 {
387 while ( item )
388 {
389 if ( item == parent )
390 {
391 // item is a descendant of parent
392 return true;
393 }
394
395 item = item->GetParent();
396 }
397
398 return false;
399 }
400
401 // -----------------------------------------------------------------------------
402 // wxTreeRenameTimer (internal)
403 // -----------------------------------------------------------------------------
404
405 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl *owner )
406 {
407 m_owner = owner;
408 }
409
410 void wxTreeRenameTimer::Notify()
411 {
412 m_owner->OnRenameTimer();
413 }
414
415 //-----------------------------------------------------------------------------
416 // wxTreeTextCtrl (internal)
417 //-----------------------------------------------------------------------------
418
419 BEGIN_EVENT_TABLE(wxTreeTextCtrl,wxTextCtrl)
420 EVT_CHAR (wxTreeTextCtrl::OnChar)
421 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp)
422 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus)
423 END_EVENT_TABLE()
424
425 wxTreeTextCtrl::wxTreeTextCtrl(wxGenericTreeCtrl *owner,
426 wxGenericTreeItem *item)
427 : m_itemEdited(item), m_startValue(item->GetText())
428 {
429 m_owner = owner;
430 m_aboutToFinish = false;
431
432 wxRect rect;
433 m_owner->GetBoundingRect(m_itemEdited, rect, true);
434
435 // corrects position and size for better appearance
436 #ifdef __WXMSW__
437 rect.x -= 5;
438 rect.width += 10;
439 #elif defined(__WXGTK__)
440 rect.x -= 5;
441 rect.y -= 2;
442 rect.width += 8;
443 rect.height += 4;
444 #elif defined(__WXMAC__)
445 int bestHeight = GetBestSize().y - 8;
446 if ( rect.height > bestHeight )
447 {
448 int diff = rect.height - bestHeight;
449 rect.height -= diff;
450 rect.y += diff / 2;
451 }
452 #endif // platforms
453
454 (void)Create(m_owner, wxID_ANY, m_startValue,
455 rect.GetPosition(), rect.GetSize());
456
457 SetSelection(-1, -1);
458 }
459
460 void wxTreeTextCtrl::EndEdit(bool discardChanges)
461 {
462 m_aboutToFinish = true;
463
464 if ( discardChanges )
465 {
466 m_owner->OnRenameCancelled(m_itemEdited);
467
468 Finish( true );
469 }
470 else
471 {
472 // Notify the owner about the changes
473 AcceptChanges();
474
475 // Even if vetoed, close the control (consistent with MSW)
476 Finish( true );
477 }
478 }
479
480 bool wxTreeTextCtrl::AcceptChanges()
481 {
482 const wxString value = GetValue();
483
484 if ( value == m_startValue )
485 {
486 // nothing changed, always accept
487 // when an item remains unchanged, the owner
488 // needs to be notified that the user decided
489 // not to change the tree item label, and that
490 // the edit has been cancelled
491
492 m_owner->OnRenameCancelled(m_itemEdited);
493 return true;
494 }
495
496 if ( !m_owner->OnRenameAccept(m_itemEdited, value) )
497 {
498 // vetoed by the user
499 return false;
500 }
501
502 // accepted, do rename the item
503 m_owner->SetItemText(m_itemEdited, value);
504
505 return true;
506 }
507
508 void wxTreeTextCtrl::Finish( bool setfocus )
509 {
510 m_owner->ResetTextControl();
511
512 wxPendingDelete.Append(this);
513
514 if (setfocus)
515 m_owner->SetFocus();
516 }
517
518 void wxTreeTextCtrl::OnChar( wxKeyEvent &event )
519 {
520 switch ( event.m_keyCode )
521 {
522 case WXK_RETURN:
523 EndEdit( false );
524 break;
525
526 case WXK_ESCAPE:
527 EndEdit( true );
528 break;
529
530 default:
531 event.Skip();
532 }
533 }
534
535 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent &event )
536 {
537 if ( !m_aboutToFinish )
538 {
539 // auto-grow the textctrl:
540 wxSize parentSize = m_owner->GetSize();
541 wxPoint myPos = GetPosition();
542 wxSize mySize = GetSize();
543 int sx, sy;
544 GetTextExtent(GetValue() + _T("M"), &sx, &sy);
545 if (myPos.x + sx > parentSize.x)
546 sx = parentSize.x - myPos.x;
547 if (mySize.x > sx)
548 sx = mySize.x;
549 SetSize(sx, wxDefaultCoord);
550 }
551
552 event.Skip();
553 }
554
555 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent &event )
556 {
557 if ( !m_aboutToFinish )
558 {
559 if ( !AcceptChanges() )
560 m_owner->OnRenameCancelled( m_itemEdited );
561
562 Finish( false );
563 }
564
565 // We should let the native text control handle focus, too.
566 event.Skip();
567 }
568
569 // -----------------------------------------------------------------------------
570 // wxGenericTreeItem
571 // -----------------------------------------------------------------------------
572
573 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem *parent,
574 const wxString& text,
575 int image, int selImage,
576 wxTreeItemData *data)
577 : m_text(text)
578 {
579 m_images[wxTreeItemIcon_Normal] = image;
580 m_images[wxTreeItemIcon_Selected] = selImage;
581 m_images[wxTreeItemIcon_Expanded] = NO_IMAGE;
582 m_images[wxTreeItemIcon_SelectedExpanded] = NO_IMAGE;
583
584 m_data = data;
585 m_state = wxTREE_ITEMSTATE_NONE;
586 m_x = m_y = 0;
587
588 m_isCollapsed = true;
589 m_hasHilight = false;
590 m_hasPlus = false;
591 m_isBold = false;
592
593 m_parent = parent;
594
595 m_attr = NULL;
596 m_ownsAttr = false;
597
598 // We don't know the height here yet.
599 m_width = 0;
600 m_height = 0;
601
602 m_widthText = -1;
603 m_heightText = -1;
604 }
605
606 wxGenericTreeItem::~wxGenericTreeItem()
607 {
608 delete m_data;
609
610 if (m_ownsAttr) delete m_attr;
611
612 wxASSERT_MSG( m_children.IsEmpty(),
613 "must call DeleteChildren() before deleting the item" );
614 }
615
616 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl *tree)
617 {
618 size_t count = m_children.GetCount();
619 for ( size_t n = 0; n < count; n++ )
620 {
621 wxGenericTreeItem *child = m_children[n];
622 tree->SendDeleteEvent(child);
623
624 child->DeleteChildren(tree);
625 if ( child == tree->m_select_me )
626 tree->m_select_me = NULL;
627 delete child;
628 }
629
630 m_children.Empty();
631 }
632
633 size_t wxGenericTreeItem::GetChildrenCount(bool recursively) const
634 {
635 size_t count = m_children.GetCount();
636 if ( !recursively )
637 return count;
638
639 size_t total = count;
640 for (size_t n = 0; n < count; ++n)
641 {
642 total += m_children[n]->GetChildrenCount();
643 }
644
645 return total;
646 }
647
648 void wxGenericTreeItem::GetSize( int &x, int &y,
649 const wxGenericTreeCtrl *theButton )
650 {
651 int bottomY=m_y+theButton->GetLineHeight(this);
652 if ( y < bottomY ) y = bottomY;
653 int width = m_x + m_width;
654 if ( x < width ) x = width;
655
656 if (IsExpanded())
657 {
658 size_t count = m_children.GetCount();
659 for ( size_t n = 0; n < count; ++n )
660 {
661 m_children[n]->GetSize( x, y, theButton );
662 }
663 }
664 }
665
666 wxGenericTreeItem *wxGenericTreeItem::HitTest(const wxPoint& point,
667 const wxGenericTreeCtrl *theCtrl,
668 int &flags,
669 int level)
670 {
671 // for a hidden root node, don't evaluate it, but do evaluate children
672 if ( !(level == 0 && theCtrl->HasFlag(wxTR_HIDE_ROOT)) )
673 {
674 // evaluate the item
675 int h = theCtrl->GetLineHeight(this);
676 if ((point.y > m_y) && (point.y < m_y + h))
677 {
678 int y_mid = m_y + h/2;
679 if (point.y < y_mid )
680 flags |= wxTREE_HITTEST_ONITEMUPPERPART;
681 else
682 flags |= wxTREE_HITTEST_ONITEMLOWERPART;
683
684 int xCross = m_x - theCtrl->GetSpacing();
685 #ifdef __WXMAC__
686 // according to the drawing code the triangels are drawn
687 // at -4 , -4 from the position up to +10/+10 max
688 if ((point.x > xCross-4) && (point.x < xCross+10) &&
689 (point.y > y_mid-4) && (point.y < y_mid+10) &&
690 HasPlus() && theCtrl->HasButtons() )
691 #else
692 // 5 is the size of the plus sign
693 if ((point.x > xCross-6) && (point.x < xCross+6) &&
694 (point.y > y_mid-6) && (point.y < y_mid+6) &&
695 HasPlus() && theCtrl->HasButtons() )
696 #endif
697 {
698 flags |= wxTREE_HITTEST_ONITEMBUTTON;
699 return this;
700 }
701
702 if ((point.x >= m_x) && (point.x <= m_x+m_width))
703 {
704 int image_w = -1;
705 int image_h;
706
707 // assuming every image (normal and selected) has the same size!
708 if ( (GetImage() != NO_IMAGE) && theCtrl->m_imageListNormal )
709 {
710 theCtrl->m_imageListNormal->GetSize(GetImage(),
711 image_w, image_h);
712 }
713
714 int state_w = -1;
715 int state_h;
716
717 if ( (GetState() != wxTREE_ITEMSTATE_NONE) &&
718 theCtrl->m_imageListState )
719 {
720 theCtrl->m_imageListState->GetSize(GetState(),
721 state_w, state_h);
722 }
723
724 if ((state_w != -1) && (point.x <= m_x + state_w + 1))
725 flags |= wxTREE_HITTEST_ONITEMSTATEICON;
726 else if ((image_w != -1) &&
727 (point.x <= m_x +
728 (state_w != -1 ? state_w +
729 MARGIN_BETWEEN_STATE_AND_IMAGE
730 : 0)
731 + image_w + 1))
732 flags |= wxTREE_HITTEST_ONITEMICON;
733 else
734 flags |= wxTREE_HITTEST_ONITEMLABEL;
735
736 return this;
737 }
738
739 if (point.x < m_x)
740 flags |= wxTREE_HITTEST_ONITEMINDENT;
741 if (point.x > m_x+m_width)
742 flags |= wxTREE_HITTEST_ONITEMRIGHT;
743
744 return this;
745 }
746
747 // if children are expanded, fall through to evaluate them
748 if (m_isCollapsed) return NULL;
749 }
750
751 // evaluate children
752 size_t count = m_children.GetCount();
753 for ( size_t n = 0; n < count; n++ )
754 {
755 wxGenericTreeItem *res = m_children[n]->HitTest( point,
756 theCtrl,
757 flags,
758 level + 1 );
759 if ( res != NULL )
760 return res;
761 }
762
763 return NULL;
764 }
765
766 int wxGenericTreeItem::GetCurrentImage() const
767 {
768 int image = NO_IMAGE;
769 if ( IsExpanded() )
770 {
771 if ( IsSelected() )
772 {
773 image = GetImage(wxTreeItemIcon_SelectedExpanded);
774 }
775
776 if ( image == NO_IMAGE )
777 {
778 // we usually fall back to the normal item, but try just the
779 // expanded one (and not selected) first in this case
780 image = GetImage(wxTreeItemIcon_Expanded);
781 }
782 }
783 else // not expanded
784 {
785 if ( IsSelected() )
786 image = GetImage(wxTreeItemIcon_Selected);
787 }
788
789 // maybe it doesn't have the specific image we want,
790 // try the default one instead
791 if ( image == NO_IMAGE ) image = GetImage();
792
793 return image;
794 }
795
796 void wxGenericTreeItem::CalculateSize(wxGenericTreeCtrl* control)
797 {
798 // check if we need to do anything before creating the DC
799 if ( m_width != 0 )
800 return;
801
802 wxClientDC dc(control);
803 DoCalculateSize(control, dc, false /* normal font not used */);
804 }
805
806 void
807 wxGenericTreeItem::DoCalculateSize(wxGenericTreeCtrl* control,
808 wxDC& dc,
809 bool dcUsesNormalFont)
810 {
811 if ( m_width != 0 ) // Size known, nothing to do
812 return;
813
814 if ( m_widthText == -1 )
815 {
816 bool fontChanged;
817 if ( SetFont(control, dc) )
818 {
819 fontChanged = true;
820 }
821 else // we have no special font
822 {
823 if ( !dcUsesNormalFont )
824 {
825 // but we do need to ensure that the normal font is used: notice
826 // that this doesn't count as changing the font as we don't need
827 // to restore it
828 dc.SetFont(control->m_normalFont);
829 }
830
831 fontChanged = false;
832 }
833
834 dc.GetTextExtent( GetText(), &m_widthText, &m_heightText );
835
836 // restore normal font if the DC used it previously and we changed it
837 if ( fontChanged )
838 dc.SetFont(control->m_normalFont);
839 }
840
841 int text_h = m_heightText + 2;
842
843 int image_h = 0, image_w = 0;
844 int image = GetCurrentImage();
845 if ( image != NO_IMAGE && control->m_imageListNormal )
846 {
847 control->m_imageListNormal->GetSize(image, image_w, image_h);
848 image_w += MARGIN_BETWEEN_IMAGE_AND_TEXT;
849 }
850
851 int state_h = 0, state_w = 0;
852 int state = GetState();
853 if ( state != wxTREE_ITEMSTATE_NONE && control->m_imageListState )
854 {
855 control->m_imageListState->GetSize(state, state_w, state_h);
856 if ( image_w != 0 )
857 state_w += MARGIN_BETWEEN_STATE_AND_IMAGE;
858 else
859 state_w += MARGIN_BETWEEN_IMAGE_AND_TEXT;
860 }
861
862 int img_h = wxMax(state_h, image_h);
863 m_height = wxMax(img_h, text_h);
864
865 if (m_height < 30)
866 m_height += 2; // at least 2 pixels
867 else
868 m_height += m_height / 10; // otherwise 10% extra spacing
869
870 if (m_height > control->m_lineHeight)
871 control->m_lineHeight = m_height;
872
873 m_width = state_w + image_w + m_widthText + 2;
874 }
875
876 void wxGenericTreeItem::RecursiveResetSize()
877 {
878 m_width = 0;
879
880 const size_t count = m_children.Count();
881 for (size_t i = 0; i < count; i++ )
882 m_children[i]->RecursiveResetSize();
883 }
884
885 void wxGenericTreeItem::RecursiveResetTextSize()
886 {
887 m_width = 0;
888 m_widthText = -1;
889
890 const size_t count = m_children.Count();
891 for (size_t i = 0; i < count; i++ )
892 m_children[i]->RecursiveResetTextSize();
893 }
894
895 // -----------------------------------------------------------------------------
896 // wxGenericTreeCtrl implementation
897 // -----------------------------------------------------------------------------
898
899 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl, wxControl)
900
901 BEGIN_EVENT_TABLE(wxGenericTreeCtrl, wxTreeCtrlBase)
902 EVT_PAINT (wxGenericTreeCtrl::OnPaint)
903 EVT_SIZE (wxGenericTreeCtrl::OnSize)
904 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse)
905 EVT_CHAR (wxGenericTreeCtrl::OnChar)
906 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus)
907 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus)
908 EVT_TREE_ITEM_GETTOOLTIP(wxID_ANY, wxGenericTreeCtrl::OnGetToolTip)
909 END_EVENT_TABLE()
910
911 #if !defined(__WXMSW__) || defined(__WXUNIVERSAL__)
912 /*
913 * wxTreeCtrl has to be a real class or we have problems with
914 * the run-time information.
915 */
916
917 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxGenericTreeCtrl)
918 #endif
919
920 // -----------------------------------------------------------------------------
921 // construction/destruction
922 // -----------------------------------------------------------------------------
923
924 void wxGenericTreeCtrl::Init()
925 {
926 m_current =
927 m_key_current =
928 m_anchor =
929 m_select_me = NULL;
930 m_hasFocus = false;
931 m_dirty = false;
932
933 m_lineHeight = 10;
934 m_indent = 15;
935 m_spacing = 18;
936
937 m_hilightBrush = new wxBrush
938 (
939 wxSystemSettings::GetColour
940 (
941 wxSYS_COLOUR_HIGHLIGHT
942 ),
943 wxBRUSHSTYLE_SOLID
944 );
945
946 m_hilightUnfocusedBrush = new wxBrush
947 (
948 wxSystemSettings::GetColour
949 (
950 wxSYS_COLOUR_BTNSHADOW
951 ),
952 wxBRUSHSTYLE_SOLID
953 );
954
955 m_imageListButtons = NULL;
956 m_ownsImageListButtons = false;
957
958 m_dragCount = 0;
959 m_isDragging = false;
960 m_dropTarget = m_oldSelection = NULL;
961 m_underMouse = NULL;
962 m_textCtrl = NULL;
963
964 m_renameTimer = NULL;
965
966 m_findTimer = NULL;
967
968 m_dropEffectAboveItem = false;
969
970 m_dndEffect = NoEffect;
971 m_dndEffectItem = NULL;
972
973 m_lastOnSame = false;
974
975 #if defined( __WXMAC__ )
976 #if wxOSX_USE_ATSU_TEXT
977 m_normalFont.MacCreateFromThemeFont( kThemeViewsFont ) ;
978 #else
979 m_normalFont.MacCreateFromUIFont( kCTFontViewsFontType ) ;
980 #endif
981 #else
982 m_normalFont = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT );
983 #endif
984 m_boldFont = wxFont(m_normalFont.GetPointSize(),
985 m_normalFont.GetFamily(),
986 m_normalFont.GetStyle(),
987 wxBOLD,
988 m_normalFont.GetUnderlined(),
989 m_normalFont.GetFaceName(),
990 m_normalFont.GetEncoding());
991 }
992
993 bool wxGenericTreeCtrl::Create(wxWindow *parent,
994 wxWindowID id,
995 const wxPoint& pos,
996 const wxSize& size,
997 long style,
998 const wxValidator& validator,
999 const wxString& name )
1000 {
1001 #ifdef __WXMAC__
1002 int major, minor;
1003 wxGetOsVersion(&major, &minor);
1004
1005 if (major < 10)
1006 style |= wxTR_ROW_LINES;
1007 #endif // __WXMAC__
1008
1009 if ( !wxControl::Create( parent, id, pos, size,
1010 style|wxHSCROLL|wxVSCROLL,
1011 validator,
1012 name ) )
1013 return false;
1014
1015 // If the tree display has no buttons, but does have
1016 // connecting lines, we can use a narrower layout.
1017 // It may not be a good idea to force this...
1018 if (!HasButtons() && !HasFlag(wxTR_NO_LINES))
1019 {
1020 m_indent= 10;
1021 m_spacing = 10;
1022 }
1023
1024 wxVisualAttributes attr = GetDefaultAttributes();
1025 SetOwnForegroundColour( attr.colFg );
1026 SetOwnBackgroundColour( attr.colBg );
1027 if (!m_hasFont)
1028 SetOwnFont(attr.font);
1029
1030 // this is a misnomer: it's called "dotted pen" but uses (default) wxSOLID
1031 // style because we apparently get performance problems when using dotted
1032 // pen for drawing in some ports -- but under MSW it seems to work fine
1033 #ifdef __WXMSW__
1034 m_dottedPen = wxPen(*wxLIGHT_GREY, 0, wxPENSTYLE_DOT);
1035 #else
1036 m_dottedPen = *wxGREY_PEN;
1037 #endif
1038
1039 SetInitialSize(size);
1040
1041 return true;
1042 }
1043
1044 wxGenericTreeCtrl::~wxGenericTreeCtrl()
1045 {
1046 delete m_hilightBrush;
1047 delete m_hilightUnfocusedBrush;
1048
1049 DeleteAllItems();
1050
1051 delete m_renameTimer;
1052 delete m_findTimer;
1053
1054 if (m_ownsImageListButtons)
1055 delete m_imageListButtons;
1056 }
1057
1058 // -----------------------------------------------------------------------------
1059 // accessors
1060 // -----------------------------------------------------------------------------
1061
1062 unsigned int wxGenericTreeCtrl::GetCount() const
1063 {
1064 if ( !m_anchor )
1065 {
1066 // the tree is empty
1067 return 0;
1068 }
1069
1070 unsigned int count = m_anchor->GetChildrenCount();
1071 if ( !HasFlag(wxTR_HIDE_ROOT) )
1072 {
1073 // take the root itself into account
1074 count++;
1075 }
1076
1077 return count;
1078 }
1079
1080 void wxGenericTreeCtrl::SetIndent(unsigned int indent)
1081 {
1082 m_indent = (unsigned short) indent;
1083 m_dirty = true;
1084 }
1085
1086 size_t
1087 wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId& item,
1088 bool recursively) const
1089 {
1090 wxCHECK_MSG( item.IsOk(), 0u, wxT("invalid tree item") );
1091
1092 return ((wxGenericTreeItem*) item.m_pItem)->GetChildrenCount(recursively);
1093 }
1094
1095 void wxGenericTreeCtrl::SetWindowStyle(const long styles)
1096 {
1097 // Do not try to expand the root node if it hasn't been created yet
1098 if (m_anchor && !HasFlag(wxTR_HIDE_ROOT) && (styles & wxTR_HIDE_ROOT))
1099 {
1100 // if we will hide the root, make sure children are visible
1101 m_anchor->SetHasPlus();
1102 m_anchor->Expand();
1103 CalculatePositions();
1104 }
1105
1106 // right now, just sets the styles. Eventually, we may
1107 // want to update the inherited styles, but right now
1108 // none of the parents has updatable styles
1109 m_windowStyle = styles;
1110 m_dirty = true;
1111 }
1112
1113 // -----------------------------------------------------------------------------
1114 // functions to work with tree items
1115 // -----------------------------------------------------------------------------
1116
1117 wxString wxGenericTreeCtrl::GetItemText(const wxTreeItemId& item) const
1118 {
1119 wxCHECK_MSG( item.IsOk(), wxEmptyString, wxT("invalid tree item") );
1120
1121 return ((wxGenericTreeItem*) item.m_pItem)->GetText();
1122 }
1123
1124 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId& item,
1125 wxTreeItemIcon which) const
1126 {
1127 wxCHECK_MSG( item.IsOk(), -1, wxT("invalid tree item") );
1128
1129 return ((wxGenericTreeItem*) item.m_pItem)->GetImage(which);
1130 }
1131
1132 wxTreeItemData *wxGenericTreeCtrl::GetItemData(const wxTreeItemId& item) const
1133 {
1134 wxCHECK_MSG( item.IsOk(), NULL, wxT("invalid tree item") );
1135
1136 return ((wxGenericTreeItem*) item.m_pItem)->GetData();
1137 }
1138
1139 int wxGenericTreeCtrl::DoGetItemState(const wxTreeItemId& item) const
1140 {
1141 wxCHECK_MSG( item.IsOk(), wxTREE_ITEMSTATE_NONE, wxT("invalid tree item") );
1142
1143 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
1144 return pItem->GetState();
1145 }
1146
1147 wxColour wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId& item) const
1148 {
1149 wxCHECK_MSG( item.IsOk(), wxNullColour, wxT("invalid tree item") );
1150
1151 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
1152 return pItem->Attr().GetTextColour();
1153 }
1154
1155 wxColour
1156 wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId& item) const
1157 {
1158 wxCHECK_MSG( item.IsOk(), wxNullColour, wxT("invalid tree item") );
1159
1160 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
1161 return pItem->Attr().GetBackgroundColour();
1162 }
1163
1164 wxFont wxGenericTreeCtrl::GetItemFont(const wxTreeItemId& item) const
1165 {
1166 wxCHECK_MSG( item.IsOk(), wxNullFont, wxT("invalid tree item") );
1167
1168 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
1169 return pItem->Attr().GetFont();
1170 }
1171
1172 void
1173 wxGenericTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text)
1174 {
1175 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1176
1177 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
1178 pItem->SetText(text);
1179 pItem->CalculateSize(this);
1180 RefreshLine(pItem);
1181 }
1182
1183 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId& item,
1184 int image,
1185 wxTreeItemIcon which)
1186 {
1187 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1188
1189 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
1190 pItem->SetImage(image, which);
1191 pItem->CalculateSize(this);
1192 RefreshLine(pItem);
1193 }
1194
1195 void
1196 wxGenericTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
1197 {
1198 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1199
1200 if (data)
1201 data->SetId( item );
1202
1203 ((wxGenericTreeItem*) item.m_pItem)->SetData(data);
1204 }
1205
1206 void wxGenericTreeCtrl::DoSetItemState(const wxTreeItemId& item, int state)
1207 {
1208 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1209
1210 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
1211 pItem->SetState(state);
1212 pItem->CalculateSize(this);
1213 RefreshLine(pItem);
1214 }
1215
1216 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
1217 {
1218 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1219
1220 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
1221 pItem->SetHasPlus(has);
1222 RefreshLine(pItem);
1223 }
1224
1225 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
1226 {
1227 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1228
1229 // avoid redrawing the tree if no real change
1230 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
1231 if ( pItem->IsBold() != bold )
1232 {
1233 pItem->SetBold(bold);
1234
1235 // recalculate the item size as bold and non bold fonts have different
1236 // widths
1237 pItem->CalculateSize(this);
1238 RefreshLine(pItem);
1239 }
1240 }
1241
1242 void wxGenericTreeCtrl::SetItemDropHighlight(const wxTreeItemId& item,
1243 bool highlight)
1244 {
1245 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1246
1247 wxColour fg, bg;
1248
1249 if (highlight)
1250 {
1251 bg = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
1252 fg = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
1253 }
1254
1255 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
1256 pItem->Attr().SetTextColour(fg);
1257 pItem->Attr().SetBackgroundColour(bg);
1258 RefreshLine(pItem);
1259 }
1260
1261 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId& item,
1262 const wxColour& col)
1263 {
1264 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1265
1266 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
1267 pItem->Attr().SetTextColour(col);
1268 RefreshLine(pItem);
1269 }
1270
1271 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId& item,
1272 const wxColour& col)
1273 {
1274 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1275
1276 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
1277 pItem->Attr().SetBackgroundColour(col);
1278 RefreshLine(pItem);
1279 }
1280
1281 void
1282 wxGenericTreeCtrl::SetItemFont(const wxTreeItemId& item, const wxFont& font)
1283 {
1284 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1285
1286 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
1287 pItem->Attr().SetFont(font);
1288 pItem->ResetTextSize();
1289 pItem->CalculateSize(this);
1290 RefreshLine(pItem);
1291 }
1292
1293 bool wxGenericTreeCtrl::SetFont( const wxFont &font )
1294 {
1295 wxTreeCtrlBase::SetFont(font);
1296
1297 m_normalFont = font ;
1298 m_boldFont = wxFont(m_normalFont.GetPointSize(),
1299 m_normalFont.GetFamily(),
1300 m_normalFont.GetStyle(),
1301 wxBOLD,
1302 m_normalFont.GetUnderlined(),
1303 m_normalFont.GetFaceName(),
1304 m_normalFont.GetEncoding());
1305
1306 if (m_anchor)
1307 m_anchor->RecursiveResetTextSize();
1308
1309 return true;
1310 }
1311
1312
1313 // -----------------------------------------------------------------------------
1314 // item status inquiries
1315 // -----------------------------------------------------------------------------
1316
1317 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId& item) const
1318 {
1319 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
1320
1321 // An item is only visible if it's not a descendant of a collapsed item
1322 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
1323 wxGenericTreeItem* parent = pItem->GetParent();
1324 while (parent)
1325 {
1326 if (!parent->IsExpanded())
1327 return false;
1328 parent = parent->GetParent();
1329 }
1330
1331 int startX, startY;
1332 GetViewStart(& startX, & startY);
1333
1334 wxSize clientSize = GetClientSize();
1335
1336 wxRect rect;
1337 if (!GetBoundingRect(item, rect))
1338 return false;
1339 if (rect.GetWidth() == 0 || rect.GetHeight() == 0)
1340 return false;
1341 if (rect.GetBottom() < 0 || rect.GetTop() > clientSize.y)
1342 return false;
1343 if (rect.GetRight() < 0 || rect.GetLeft() > clientSize.x)
1344 return false;
1345
1346 return true;
1347 }
1348
1349 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
1350 {
1351 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
1352
1353 // consider that the item does have children if it has the "+" button: it
1354 // might not have them (if it had never been expanded yet) but then it
1355 // could have them as well and it's better to err on this side rather than
1356 // disabling some operations which are restricted to the items with
1357 // children for an item which does have them
1358 return ((wxGenericTreeItem*) item.m_pItem)->HasPlus();
1359 }
1360
1361 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId& item) const
1362 {
1363 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
1364
1365 return ((wxGenericTreeItem*) item.m_pItem)->IsExpanded();
1366 }
1367
1368 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId& item) const
1369 {
1370 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
1371
1372 return ((wxGenericTreeItem*) item.m_pItem)->IsSelected();
1373 }
1374
1375 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId& item) const
1376 {
1377 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
1378
1379 return ((wxGenericTreeItem*) item.m_pItem)->IsBold();
1380 }
1381
1382 // -----------------------------------------------------------------------------
1383 // navigation
1384 // -----------------------------------------------------------------------------
1385
1386 wxTreeItemId wxGenericTreeCtrl::GetItemParent(const wxTreeItemId& item) const
1387 {
1388 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1389
1390 return ((wxGenericTreeItem*) item.m_pItem)->GetParent();
1391 }
1392
1393 wxTreeItemId wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId& item,
1394 wxTreeItemIdValue& cookie) const
1395 {
1396 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1397
1398 cookie = 0;
1399 return GetNextChild(item, cookie);
1400 }
1401
1402 wxTreeItemId wxGenericTreeCtrl::GetNextChild(const wxTreeItemId& item,
1403 wxTreeItemIdValue& cookie) const
1404 {
1405 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1406
1407 wxArrayGenericTreeItems&
1408 children = ((wxGenericTreeItem*) item.m_pItem)->GetChildren();
1409
1410 // it's ok to cast cookie to size_t, we never have indices big enough to
1411 // overflow "void *"
1412 size_t *pIndex = (size_t *)&cookie;
1413 if ( *pIndex < children.GetCount() )
1414 {
1415 return children.Item((*pIndex)++);
1416 }
1417 else
1418 {
1419 // there are no more of them
1420 return wxTreeItemId();
1421 }
1422 }
1423
1424 wxTreeItemId wxGenericTreeCtrl::GetLastChild(const wxTreeItemId& item) const
1425 {
1426 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1427
1428 wxArrayGenericTreeItems&
1429 children = ((wxGenericTreeItem*) item.m_pItem)->GetChildren();
1430 return children.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children.Last());
1431 }
1432
1433 wxTreeItemId wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
1434 {
1435 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1436
1437 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
1438 wxGenericTreeItem *parent = i->GetParent();
1439 if ( parent == NULL )
1440 {
1441 // root item doesn't have any siblings
1442 return wxTreeItemId();
1443 }
1444
1445 wxArrayGenericTreeItems& siblings = parent->GetChildren();
1446 int index = siblings.Index(i);
1447 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
1448
1449 size_t n = (size_t)(index + 1);
1450 return n == siblings.GetCount() ? wxTreeItemId()
1451 : wxTreeItemId(siblings[n]);
1452 }
1453
1454 wxTreeItemId wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
1455 {
1456 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1457
1458 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
1459 wxGenericTreeItem *parent = i->GetParent();
1460 if ( parent == NULL )
1461 {
1462 // root item doesn't have any siblings
1463 return wxTreeItemId();
1464 }
1465
1466 wxArrayGenericTreeItems& siblings = parent->GetChildren();
1467 int index = siblings.Index(i);
1468 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
1469
1470 return index == 0 ? wxTreeItemId()
1471 : wxTreeItemId(siblings[(size_t)(index - 1)]);
1472 }
1473
1474 // Only for internal use right now, but should probably be public
1475 wxTreeItemId wxGenericTreeCtrl::GetNext(const wxTreeItemId& item) const
1476 {
1477 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1478
1479 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
1480
1481 // First see if there are any children.
1482 wxArrayGenericTreeItems& children = i->GetChildren();
1483 if (children.GetCount() > 0)
1484 {
1485 return children.Item(0);
1486 }
1487 else
1488 {
1489 // Try a sibling of this or ancestor instead
1490 wxTreeItemId p = item;
1491 wxTreeItemId toFind;
1492 do
1493 {
1494 toFind = GetNextSibling(p);
1495 p = GetItemParent(p);
1496 } while (p.IsOk() && !toFind.IsOk());
1497 return toFind;
1498 }
1499 }
1500
1501 wxTreeItemId wxGenericTreeCtrl::GetFirstVisibleItem() const
1502 {
1503 wxTreeItemId id = GetRootItem();
1504 if (!id.IsOk())
1505 return id;
1506
1507 do
1508 {
1509 if (IsVisible(id))
1510 return id;
1511 id = GetNext(id);
1512 } while (id.IsOk());
1513
1514 return wxTreeItemId();
1515 }
1516
1517 wxTreeItemId wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
1518 {
1519 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1520 wxASSERT_MSG( IsVisible(item), wxT("this item itself should be visible") );
1521
1522 wxTreeItemId id = item;
1523 if (id.IsOk())
1524 {
1525 while (id = GetNext(id), id.IsOk())
1526 {
1527 if (IsVisible(id))
1528 return id;
1529 }
1530 }
1531 return wxTreeItemId();
1532 }
1533
1534 wxTreeItemId wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
1535 {
1536 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1537 wxASSERT_MSG( IsVisible(item), wxT("this item itself should be visible") );
1538
1539 // find out the starting point
1540 wxTreeItemId prevItem = GetPrevSibling(item);
1541 if ( !prevItem.IsOk() )
1542 {
1543 prevItem = GetItemParent(item);
1544 }
1545
1546 // find the first visible item after it
1547 while ( prevItem.IsOk() && !IsVisible(prevItem) )
1548 {
1549 prevItem = GetNext(prevItem);
1550 if ( !prevItem.IsOk() || prevItem == item )
1551 {
1552 // there are no visible items before item
1553 return wxTreeItemId();
1554 }
1555 }
1556
1557 // from there we must be able to navigate until this item
1558 while ( prevItem.IsOk() )
1559 {
1560 const wxTreeItemId nextItem = GetNextVisible(prevItem);
1561 if ( !nextItem.IsOk() || nextItem == item )
1562 break;
1563
1564 prevItem = nextItem;
1565 }
1566
1567 return prevItem;
1568 }
1569
1570 // called by wxTextTreeCtrl when it marks itself for deletion
1571 void wxGenericTreeCtrl::ResetTextControl()
1572 {
1573 m_textCtrl = NULL;
1574 }
1575
1576 // find the first item starting with the given prefix after the given item
1577 wxTreeItemId wxGenericTreeCtrl::FindItem(const wxTreeItemId& idParent,
1578 const wxString& prefixOrig) const
1579 {
1580 // match is case insensitive as this is more convenient to the user: having
1581 // to press Shift-letter to go to the item starting with a capital letter
1582 // would be too bothersome
1583 wxString prefix = prefixOrig.Lower();
1584
1585 // determine the starting point: we shouldn't take the current item (this
1586 // allows to switch between two items starting with the same letter just by
1587 // pressing it) but we shouldn't jump to the next one if the user is
1588 // continuing to type as otherwise he might easily skip the item he wanted
1589 wxTreeItemId id = idParent;
1590 if ( prefix.length() == 1 )
1591 {
1592 id = GetNext(id);
1593 }
1594
1595 // look for the item starting with the given prefix after it
1596 while ( id.IsOk() && !GetItemText(id).Lower().StartsWith(prefix) )
1597 {
1598 id = GetNext(id);
1599 }
1600
1601 // if we haven't found anything...
1602 if ( !id.IsOk() )
1603 {
1604 // ... wrap to the beginning
1605 id = GetRootItem();
1606 if ( HasFlag(wxTR_HIDE_ROOT) )
1607 {
1608 // can't select virtual root
1609 id = GetNext(id);
1610 }
1611
1612 // and try all the items (stop when we get to the one we started from)
1613 while ( id.IsOk() && id != idParent &&
1614 !GetItemText(id).Lower().StartsWith(prefix) )
1615 {
1616 id = GetNext(id);
1617 }
1618 // If we haven't found the item, id.IsOk() will be false, as per
1619 // documentation
1620 }
1621
1622 return id;
1623 }
1624
1625 // -----------------------------------------------------------------------------
1626 // operations
1627 // -----------------------------------------------------------------------------
1628
1629 wxTreeItemId wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId& parentId,
1630 size_t previous,
1631 const wxString& text,
1632 int image,
1633 int selImage,
1634 wxTreeItemData *data)
1635 {
1636 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
1637 if ( !parent )
1638 {
1639 // should we give a warning here?
1640 return AddRoot(text, image, selImage, data);
1641 }
1642
1643 m_dirty = true; // do this first so stuff below doesn't cause flicker
1644
1645 wxGenericTreeItem *item =
1646 new wxGenericTreeItem( parent, text, image, selImage, data );
1647
1648 if ( data != NULL )
1649 {
1650 data->m_pItem = item;
1651 }
1652
1653 parent->Insert( item, previous == (size_t)-1 ? parent->GetChildren().size()
1654 : previous );
1655
1656 InvalidateBestSize();
1657 return item;
1658 }
1659
1660 wxTreeItemId wxGenericTreeCtrl::AddRoot(const wxString& text,
1661 int image,
1662 int selImage,
1663 wxTreeItemData *data)
1664 {
1665 wxCHECK_MSG( !m_anchor, wxTreeItemId(), "tree can have only one root" );
1666
1667 m_dirty = true; // do this first so stuff below doesn't cause flicker
1668
1669 m_anchor = new wxGenericTreeItem(NULL, text,
1670 image, selImage, data);
1671 if ( data != NULL )
1672 {
1673 data->m_pItem = m_anchor;
1674 }
1675
1676 if (HasFlag(wxTR_HIDE_ROOT))
1677 {
1678 // if root is hidden, make sure we can navigate
1679 // into children
1680 m_anchor->SetHasPlus();
1681 m_anchor->Expand();
1682 CalculatePositions();
1683 }
1684
1685 if (!HasFlag(wxTR_MULTIPLE))
1686 {
1687 m_current = m_key_current = m_anchor;
1688 m_current->SetHilight( true );
1689 }
1690
1691 InvalidateBestSize();
1692 return m_anchor;
1693 }
1694
1695 wxTreeItemId wxGenericTreeCtrl::DoInsertAfter(const wxTreeItemId& parentId,
1696 const wxTreeItemId& idPrevious,
1697 const wxString& text,
1698 int image, int selImage,
1699 wxTreeItemData *data)
1700 {
1701 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
1702 if ( !parent )
1703 {
1704 // should we give a warning here?
1705 return AddRoot(text, image, selImage, data);
1706 }
1707
1708 int index = -1;
1709 if (idPrevious.IsOk())
1710 {
1711 index = parent->GetChildren().Index(
1712 (wxGenericTreeItem*) idPrevious.m_pItem);
1713 wxASSERT_MSG( index != wxNOT_FOUND,
1714 "previous item in wxGenericTreeCtrl::InsertItem() "
1715 "is not a sibling" );
1716 }
1717
1718 return DoInsertItem(parentId, (size_t)++index, text, image, selImage, data);
1719 }
1720
1721
1722 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem *item)
1723 {
1724 wxTreeEvent event(wxEVT_COMMAND_TREE_DELETE_ITEM, this, item);
1725 GetEventHandler()->ProcessEvent( event );
1726 }
1727
1728 // Don't leave edit or selection on a child which is about to disappear
1729 void wxGenericTreeCtrl::ChildrenClosing(wxGenericTreeItem* item)
1730 {
1731 if ( m_textCtrl && item != m_textCtrl->item() &&
1732 IsDescendantOf(item, m_textCtrl->item()) )
1733 {
1734 m_textCtrl->EndEdit( true );
1735 }
1736
1737 if ( item != m_key_current && IsDescendantOf(item, m_key_current) )
1738 {
1739 m_key_current = NULL;
1740 }
1741
1742 if ( IsDescendantOf(item, m_select_me) )
1743 {
1744 m_select_me = item;
1745 }
1746
1747 if ( item != m_current && IsDescendantOf(item, m_current) )
1748 {
1749 m_current->SetHilight( false );
1750 m_current = NULL;
1751 m_select_me = item;
1752 }
1753 }
1754
1755 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId& itemId)
1756 {
1757 m_dirty = true; // do this first so stuff below doesn't cause flicker
1758
1759 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1760 ChildrenClosing(item);
1761 item->DeleteChildren(this);
1762 InvalidateBestSize();
1763 }
1764
1765 void wxGenericTreeCtrl::Delete(const wxTreeItemId& itemId)
1766 {
1767 m_dirty = true; // do this first so stuff below doesn't cause flicker
1768
1769 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1770
1771 if (m_textCtrl != NULL && IsDescendantOf(item, m_textCtrl->item()))
1772 {
1773 // can't delete the item being edited, cancel editing it first
1774 m_textCtrl->EndEdit( true );
1775 }
1776
1777 wxGenericTreeItem *parent = item->GetParent();
1778
1779 // if the selected item will be deleted, select the parent ...
1780 wxGenericTreeItem *to_be_selected = parent;
1781 if (parent)
1782 {
1783 // .. unless there is a next sibling like wxMSW does it
1784 int pos = parent->GetChildren().Index( item );
1785 if ((int)(parent->GetChildren().GetCount()) > pos+1)
1786 to_be_selected = parent->GetChildren().Item( pos+1 );
1787 }
1788
1789 // don't keep stale pointers around!
1790 if ( IsDescendantOf(item, m_key_current) )
1791 {
1792 // Don't silently change the selection:
1793 // do it properly in idle time, so event
1794 // handlers get called.
1795
1796 // m_key_current = parent;
1797 m_key_current = NULL;
1798 }
1799
1800 // m_select_me records whether we need to select
1801 // a different item, in idle time.
1802 if ( m_select_me && IsDescendantOf(item, m_select_me) )
1803 {
1804 m_select_me = to_be_selected;
1805 }
1806
1807 if ( IsDescendantOf(item, m_current) )
1808 {
1809 // Don't silently change the selection:
1810 // do it properly in idle time, so event
1811 // handlers get called.
1812
1813 // m_current = parent;
1814 m_current = NULL;
1815 m_select_me = to_be_selected;
1816 }
1817
1818 // remove the item from the tree
1819 if ( parent )
1820 {
1821 parent->GetChildren().Remove( item ); // remove by value
1822 }
1823 else // deleting the root
1824 {
1825 // nothing will be left in the tree
1826 m_anchor = NULL;
1827 }
1828
1829 // and delete all of its children and the item itself now
1830 item->DeleteChildren(this);
1831 SendDeleteEvent(item);
1832
1833 if (item == m_select_me)
1834 m_select_me = NULL;
1835
1836 delete item;
1837
1838 InvalidateBestSize();
1839 }
1840
1841 void wxGenericTreeCtrl::DeleteAllItems()
1842 {
1843 if ( m_anchor )
1844 {
1845 Delete(m_anchor);
1846 }
1847 }
1848
1849 void wxGenericTreeCtrl::Expand(const wxTreeItemId& itemId)
1850 {
1851 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1852
1853 wxCHECK_RET( item, _T("invalid item in wxGenericTreeCtrl::Expand") );
1854 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT) || itemId != GetRootItem(),
1855 _T("can't expand hidden root") );
1856
1857 if ( !item->HasPlus() )
1858 return;
1859
1860 if ( item->IsExpanded() )
1861 return;
1862
1863 wxTreeEvent event(wxEVT_COMMAND_TREE_ITEM_EXPANDING, this, item);
1864
1865 if ( GetEventHandler()->ProcessEvent( event ) && !event.IsAllowed() )
1866 {
1867 // cancelled by program
1868 return;
1869 }
1870
1871 item->Expand();
1872 if ( !IsFrozen() )
1873 {
1874 CalculatePositions();
1875
1876 RefreshSubtree(item);
1877 }
1878 else // frozen
1879 {
1880 m_dirty = true;
1881 }
1882
1883 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED);
1884 GetEventHandler()->ProcessEvent( event );
1885 }
1886
1887 void wxGenericTreeCtrl::Collapse(const wxTreeItemId& itemId)
1888 {
1889 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT) || itemId != GetRootItem(),
1890 _T("can't collapse hidden root") );
1891
1892 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1893
1894 if ( !item->IsExpanded() )
1895 return;
1896
1897 wxTreeEvent event(wxEVT_COMMAND_TREE_ITEM_COLLAPSING, this, item);
1898 if ( GetEventHandler()->ProcessEvent( event ) && !event.IsAllowed() )
1899 {
1900 // cancelled by program
1901 return;
1902 }
1903
1904 ChildrenClosing(item);
1905 item->Collapse();
1906
1907 #if 0 // TODO why should items be collapsed recursively?
1908 wxArrayGenericTreeItems& children = item->GetChildren();
1909 size_t count = children.GetCount();
1910 for ( size_t n = 0; n < count; n++ )
1911 {
1912 Collapse(children[n]);
1913 }
1914 #endif
1915
1916 CalculatePositions();
1917
1918 RefreshSubtree(item);
1919
1920 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED);
1921 GetEventHandler()->ProcessEvent( event );
1922 }
1923
1924 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
1925 {
1926 Collapse(item);
1927 DeleteChildren(item);
1928 }
1929
1930 void wxGenericTreeCtrl::Toggle(const wxTreeItemId& itemId)
1931 {
1932 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1933
1934 if (item->IsExpanded())
1935 Collapse(itemId);
1936 else
1937 Expand(itemId);
1938 }
1939
1940 void wxGenericTreeCtrl::Unselect()
1941 {
1942 if (m_current)
1943 {
1944 m_current->SetHilight( false );
1945 RefreshLine( m_current );
1946
1947 m_current = NULL;
1948 m_select_me = NULL;
1949 }
1950 }
1951
1952 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem *item)
1953 {
1954 if (item->IsSelected())
1955 {
1956 item->SetHilight(false);
1957 RefreshLine(item);
1958 }
1959
1960 if (item->HasChildren())
1961 {
1962 wxArrayGenericTreeItems& children = item->GetChildren();
1963 size_t count = children.GetCount();
1964 for ( size_t n = 0; n < count; ++n )
1965 {
1966 UnselectAllChildren(children[n]);
1967 }
1968 }
1969 }
1970
1971 void wxGenericTreeCtrl::UnselectAll()
1972 {
1973 wxTreeItemId rootItem = GetRootItem();
1974
1975 // the tree might not have the root item at all
1976 if ( rootItem )
1977 {
1978 UnselectAllChildren((wxGenericTreeItem*) rootItem.m_pItem);
1979 }
1980 }
1981
1982 // Recursive function !
1983 // To stop we must have crt_item<last_item
1984 // Algorithm :
1985 // Tag all next children, when no more children,
1986 // Move to parent (not to tag)
1987 // Keep going... if we found last_item, we stop.
1988 bool
1989 wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem *crt_item,
1990 wxGenericTreeItem *last_item,
1991 bool select)
1992 {
1993 wxGenericTreeItem *parent = crt_item->GetParent();
1994
1995 if (parent == NULL) // This is root item
1996 return TagAllChildrenUntilLast(crt_item, last_item, select);
1997
1998 wxArrayGenericTreeItems& children = parent->GetChildren();
1999 int index = children.Index(crt_item);
2000 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
2001
2002 size_t count = children.GetCount();
2003 for (size_t n=(size_t)(index+1); n<count; ++n)
2004 {
2005 if ( TagAllChildrenUntilLast(children[n], last_item, select) )
2006 return true;
2007 }
2008
2009 return TagNextChildren(parent, last_item, select);
2010 }
2011
2012 bool
2013 wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem *crt_item,
2014 wxGenericTreeItem *last_item,
2015 bool select)
2016 {
2017 crt_item->SetHilight(select);
2018 RefreshLine(crt_item);
2019
2020 if (crt_item==last_item)
2021 return true;
2022
2023 if (crt_item->HasChildren())
2024 {
2025 wxArrayGenericTreeItems& children = crt_item->GetChildren();
2026 size_t count = children.GetCount();
2027 for ( size_t n = 0; n < count; ++n )
2028 {
2029 if (TagAllChildrenUntilLast(children[n], last_item, select))
2030 return true;
2031 }
2032 }
2033
2034 return false;
2035 }
2036
2037 void
2038 wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem *item1,
2039 wxGenericTreeItem *item2)
2040 {
2041 m_select_me = NULL;
2042
2043 // item2 is not necessary after item1
2044 // choice first' and 'last' between item1 and item2
2045 wxGenericTreeItem *first= (item1->GetY()<item2->GetY()) ? item1 : item2;
2046 wxGenericTreeItem *last = (item1->GetY()<item2->GetY()) ? item2 : item1;
2047
2048 bool select = m_current->IsSelected();
2049
2050 if ( TagAllChildrenUntilLast(first,last,select) )
2051 return;
2052
2053 TagNextChildren(first,last,select);
2054 }
2055
2056 void wxGenericTreeCtrl::DoSelectItem(const wxTreeItemId& itemId,
2057 bool unselect_others,
2058 bool extended_select)
2059 {
2060 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
2061
2062 m_select_me = NULL;
2063
2064 bool is_single=!(GetWindowStyleFlag() & wxTR_MULTIPLE);
2065 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
2066
2067 //wxCHECK_RET( ( (!unselect_others) && is_single),
2068 // wxT("this is a single selection tree") );
2069
2070 // to keep going anyhow !!!
2071 if (is_single)
2072 {
2073 if (item->IsSelected())
2074 return; // nothing to do
2075 unselect_others = true;
2076 extended_select = false;
2077 }
2078 else if ( unselect_others && item->IsSelected() )
2079 {
2080 // selection change if there is more than one item currently selected
2081 wxArrayTreeItemIds selected_items;
2082 if ( GetSelections(selected_items) == 1 )
2083 return;
2084 }
2085
2086 wxTreeEvent event(wxEVT_COMMAND_TREE_SEL_CHANGING, this, item);
2087 event.m_itemOld = m_current;
2088 // TODO : Here we don't send any selection mode yet !
2089
2090 if ( GetEventHandler()->ProcessEvent( event ) && !event.IsAllowed() )
2091 return;
2092
2093 wxTreeItemId parent = GetItemParent( itemId );
2094 while (parent.IsOk())
2095 {
2096 if (!IsExpanded(parent))
2097 Expand( parent );
2098
2099 parent = GetItemParent( parent );
2100 }
2101
2102 // ctrl press
2103 if (unselect_others)
2104 {
2105 if (is_single) Unselect(); // to speed up thing
2106 else UnselectAll();
2107 }
2108
2109 // shift press
2110 if (extended_select)
2111 {
2112 if ( !m_current )
2113 {
2114 m_current =
2115 m_key_current = (wxGenericTreeItem*) GetRootItem().m_pItem;
2116 }
2117
2118 // don't change the mark (m_current)
2119 SelectItemRange(m_current, item);
2120 }
2121 else
2122 {
2123 bool select = true; // the default
2124
2125 // Check if we need to toggle hilight (ctrl mode)
2126 if (!unselect_others)
2127 select=!item->IsSelected();
2128
2129 m_current = m_key_current = item;
2130 m_current->SetHilight(select);
2131 RefreshLine( m_current );
2132 }
2133
2134 // This can cause idle processing to select the root
2135 // if no item is selected, so it must be after the
2136 // selection is set
2137 EnsureVisible( itemId );
2138
2139 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
2140 GetEventHandler()->ProcessEvent( event );
2141 }
2142
2143 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId& itemId, bool select)
2144 {
2145 if ( select )
2146 {
2147 DoSelectItem(itemId, !HasFlag(wxTR_MULTIPLE));
2148 }
2149 else // deselect
2150 {
2151 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
2152 wxCHECK_RET( item, wxT("SelectItem(): invalid tree item") );
2153
2154 wxTreeEvent event(wxEVT_COMMAND_TREE_SEL_CHANGING, this, item);
2155 if ( GetEventHandler()->ProcessEvent( event ) && !event.IsAllowed() )
2156 return;
2157
2158 item->SetHilight(false);
2159 RefreshLine(item);
2160
2161 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
2162 GetEventHandler()->ProcessEvent( event );
2163 }
2164 }
2165
2166 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem *item,
2167 wxArrayTreeItemIds &array) const
2168 {
2169 if ( item->IsSelected() )
2170 array.Add(wxTreeItemId(item));
2171
2172 if ( item->HasChildren() )
2173 {
2174 wxArrayGenericTreeItems& children = item->GetChildren();
2175 size_t count = children.GetCount();
2176 for ( size_t n = 0; n < count; ++n )
2177 FillArray(children[n], array);
2178 }
2179 }
2180
2181 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds &array) const
2182 {
2183 array.Empty();
2184 wxTreeItemId idRoot = GetRootItem();
2185 if ( idRoot.IsOk() )
2186 {
2187 FillArray((wxGenericTreeItem*) idRoot.m_pItem, array);
2188 }
2189 //else: the tree is empty, so no selections
2190
2191 return array.GetCount();
2192 }
2193
2194 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId& item)
2195 {
2196 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
2197
2198 if (!item.IsOk()) return;
2199
2200 wxGenericTreeItem *gitem = (wxGenericTreeItem*) item.m_pItem;
2201
2202 // first expand all parent branches
2203 wxGenericTreeItem *parent = gitem->GetParent();
2204
2205 if ( HasFlag(wxTR_HIDE_ROOT) )
2206 {
2207 while ( parent && parent != m_anchor )
2208 {
2209 Expand(parent);
2210 parent = parent->GetParent();
2211 }
2212 }
2213 else
2214 {
2215 while ( parent )
2216 {
2217 Expand(parent);
2218 parent = parent->GetParent();
2219 }
2220 }
2221
2222 //if (parent) CalculatePositions();
2223
2224 ScrollTo(item);
2225 }
2226
2227 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId &item)
2228 {
2229 if (!item.IsOk()) return;
2230
2231 // We have to call this here because the label in
2232 // question might just have been added and no screen
2233 // update taken place.
2234 if (m_dirty)
2235 #if defined( __WXMSW__ ) || defined(__WXMAC__)
2236 Update();
2237 #else
2238 DoDirtyProcessing();
2239 #endif
2240 wxGenericTreeItem *gitem = (wxGenericTreeItem*) item.m_pItem;
2241
2242 // now scroll to the item
2243 int item_y = gitem->GetY();
2244
2245 int start_x = 0;
2246 int start_y = 0;
2247 GetViewStart( &start_x, &start_y );
2248 start_y *= PIXELS_PER_UNIT;
2249
2250 int client_h = 0;
2251 int client_w = 0;
2252 GetClientSize( &client_w, &client_h );
2253
2254 if (item_y < start_y+3)
2255 {
2256 // going down
2257 int x = 0;
2258 int y = 0;
2259 m_anchor->GetSize( x, y, this );
2260 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
2261 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
2262 int x_pos = GetScrollPos( wxHORIZONTAL );
2263 // Item should appear at top
2264 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT,
2265 x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT,
2266 x_pos, item_y/PIXELS_PER_UNIT );
2267 }
2268 else if (item_y+GetLineHeight(gitem) > start_y+client_h)
2269 {
2270 // going up
2271 int x = 0;
2272 int y = 0;
2273 m_anchor->GetSize( x, y, this );
2274 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
2275 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
2276 item_y += PIXELS_PER_UNIT+2;
2277 int x_pos = GetScrollPos( wxHORIZONTAL );
2278 // Item should appear at bottom
2279 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT,
2280 x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT,
2281 x_pos,
2282 (item_y+GetLineHeight(gitem)-client_h)/PIXELS_PER_UNIT );
2283 }
2284 }
2285
2286 // FIXME: tree sorting functions are not reentrant and not MT-safe!
2287 static wxGenericTreeCtrl *s_treeBeingSorted = NULL;
2288
2289 static int LINKAGEMODE tree_ctrl_compare_func(wxGenericTreeItem **item1,
2290 wxGenericTreeItem **item2)
2291 {
2292 wxCHECK_MSG( s_treeBeingSorted, 0,
2293 "bug in wxGenericTreeCtrl::SortChildren()" );
2294
2295 return s_treeBeingSorted->OnCompareItems(*item1, *item2);
2296 }
2297
2298 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId& itemId)
2299 {
2300 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
2301
2302 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
2303
2304 wxCHECK_RET( !s_treeBeingSorted,
2305 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
2306
2307 wxArrayGenericTreeItems& children = item->GetChildren();
2308 if ( children.GetCount() > 1 )
2309 {
2310 m_dirty = true;
2311
2312 s_treeBeingSorted = this;
2313 children.Sort(tree_ctrl_compare_func);
2314 s_treeBeingSorted = NULL;
2315 }
2316 //else: don't make the tree dirty as nothing changed
2317 }
2318
2319 void wxGenericTreeCtrl::CalculateLineHeight()
2320 {
2321 wxClientDC dc(this);
2322 m_lineHeight = (int)(dc.GetCharHeight() + 4);
2323
2324 if ( m_imageListNormal )
2325 {
2326 // Calculate a m_lineHeight value from the normal Image sizes.
2327 // May be toggle off. Then wxGenericTreeCtrl will spread when
2328 // necessary (which might look ugly).
2329 int n = m_imageListNormal->GetImageCount();
2330 for (int i = 0; i < n ; i++)
2331 {
2332 int width = 0, height = 0;
2333 m_imageListNormal->GetSize(i, width, height);
2334 if (height > m_lineHeight) m_lineHeight = height;
2335 }
2336 }
2337
2338 if ( m_imageListState )
2339 {
2340 // Calculate a m_lineHeight value from the state Image sizes.
2341 // May be toggle off. Then wxGenericTreeCtrl will spread when
2342 // necessary (which might look ugly).
2343 int n = m_imageListState->GetImageCount();
2344 for (int i = 0; i < n ; i++)
2345 {
2346 int width = 0, height = 0;
2347 m_imageListState->GetSize(i, width, height);
2348 if (height > m_lineHeight) m_lineHeight = height;
2349 }
2350 }
2351
2352 if (m_imageListButtons)
2353 {
2354 // Calculate a m_lineHeight value from the Button image sizes.
2355 // May be toggle off. Then wxGenericTreeCtrl will spread when
2356 // necessary (which might look ugly).
2357 int n = m_imageListButtons->GetImageCount();
2358 for (int i = 0; i < n ; i++)
2359 {
2360 int width = 0, height = 0;
2361 m_imageListButtons->GetSize(i, width, height);
2362 if (height > m_lineHeight) m_lineHeight = height;
2363 }
2364 }
2365
2366 if (m_lineHeight < 30)
2367 m_lineHeight += 2; // at least 2 pixels
2368 else
2369 m_lineHeight += m_lineHeight/10; // otherwise 10% extra spacing
2370 }
2371
2372 void wxGenericTreeCtrl::SetImageList(wxImageList *imageList)
2373 {
2374 if (m_ownsImageListNormal) delete m_imageListNormal;
2375 m_imageListNormal = imageList;
2376 m_ownsImageListNormal = false;
2377 m_dirty = true;
2378
2379 if (m_anchor)
2380 m_anchor->RecursiveResetSize();
2381
2382 // Don't do any drawing if we're setting the list to NULL,
2383 // since we may be in the process of deleting the tree control.
2384 if (imageList)
2385 CalculateLineHeight();
2386 }
2387
2388 void wxGenericTreeCtrl::SetStateImageList(wxImageList *imageList)
2389 {
2390 if (m_ownsImageListState) delete m_imageListState;
2391 m_imageListState = imageList;
2392 m_ownsImageListState = false;
2393 m_dirty = true;
2394
2395 if (m_anchor)
2396 m_anchor->RecursiveResetSize();
2397
2398 // Don't do any drawing if we're setting the list to NULL,
2399 // since we may be in the process of deleting the tree control.
2400 if (imageList)
2401 CalculateLineHeight();
2402 }
2403
2404 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList *imageList)
2405 {
2406 if (m_ownsImageListButtons) delete m_imageListButtons;
2407 m_imageListButtons = imageList;
2408 m_ownsImageListButtons = false;
2409 m_dirty = true;
2410
2411 if (m_anchor)
2412 m_anchor->RecursiveResetSize();
2413
2414 CalculateLineHeight();
2415 }
2416
2417 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList *imageList)
2418 {
2419 SetButtonsImageList(imageList);
2420 m_ownsImageListButtons = true;
2421 }
2422
2423 // -----------------------------------------------------------------------------
2424 // helpers
2425 // -----------------------------------------------------------------------------
2426
2427 void wxGenericTreeCtrl::AdjustMyScrollbars()
2428 {
2429 if (m_anchor)
2430 {
2431 int x = 0, y = 0;
2432 m_anchor->GetSize( x, y, this );
2433 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
2434 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
2435 int x_pos = GetScrollPos( wxHORIZONTAL );
2436 int y_pos = GetScrollPos( wxVERTICAL );
2437 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT,
2438 x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT,
2439 x_pos, y_pos );
2440 }
2441 else
2442 {
2443 SetScrollbars( 0, 0, 0, 0 );
2444 }
2445 }
2446
2447 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem *item) const
2448 {
2449 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT)
2450 return item->GetHeight();
2451 else
2452 return m_lineHeight;
2453 }
2454
2455 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem *item, wxDC& dc)
2456 {
2457 item->SetFont(this, dc);
2458 item->CalculateSize(this, dc);
2459
2460 wxCoord text_h = item->GetTextHeight();
2461
2462 int image_h = 0, image_w = 0;
2463 int image = item->GetCurrentImage();
2464 if ( image != NO_IMAGE )
2465 {
2466 if ( m_imageListNormal )
2467 {
2468 m_imageListNormal->GetSize(image, image_w, image_h);
2469 image_w += MARGIN_BETWEEN_IMAGE_AND_TEXT;
2470 }
2471 else
2472 {
2473 image = NO_IMAGE;
2474 }
2475 }
2476
2477 int state_h = 0, state_w = 0;
2478 int state = item->GetState();
2479 if ( state != wxTREE_ITEMSTATE_NONE )
2480 {
2481 if ( m_imageListState )
2482 {
2483 m_imageListState->GetSize(state, state_w, state_h);
2484 if ( image_w != 0 )
2485 state_w += MARGIN_BETWEEN_STATE_AND_IMAGE;
2486 else
2487 state_w += MARGIN_BETWEEN_IMAGE_AND_TEXT;
2488 }
2489 else
2490 {
2491 state = wxTREE_ITEMSTATE_NONE;
2492 }
2493 }
2494
2495 int total_h = GetLineHeight(item);
2496 bool drawItemBackground = false,
2497 hasBgColour = false;
2498
2499 if ( item->IsSelected() )
2500 {
2501 dc.SetBrush(*(m_hasFocus ? m_hilightBrush : m_hilightUnfocusedBrush));
2502 drawItemBackground = true;
2503 }
2504 else
2505 {
2506 wxColour colBg;
2507 wxTreeItemAttr * const attr = item->GetAttributes();
2508 if ( attr && attr->HasBackgroundColour() )
2509 {
2510 drawItemBackground =
2511 hasBgColour = true;
2512 colBg = attr->GetBackgroundColour();
2513 }
2514 else
2515 {
2516 colBg = GetBackgroundColour();
2517 }
2518 dc.SetBrush(wxBrush(colBg, wxBRUSHSTYLE_SOLID));
2519 }
2520
2521 int offset = HasFlag(wxTR_ROW_LINES) ? 1 : 0;
2522
2523 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT) )
2524 {
2525 int x, w, h;
2526 x=0;
2527 GetVirtualSize(&w, &h);
2528 wxRect rect( x, item->GetY()+offset, w, total_h-offset);
2529 if (!item->IsSelected())
2530 {
2531 dc.DrawRectangle(rect);
2532 }
2533 else
2534 {
2535 int flags = wxCONTROL_SELECTED;
2536 if (m_hasFocus
2537 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON // TODO CS
2538 && IsControlActive( (ControlRef)GetHandle() )
2539 #endif
2540 )
2541 flags |= wxCONTROL_FOCUSED;
2542 if ((item == m_current) && (m_hasFocus))
2543 flags |= wxCONTROL_CURRENT;
2544
2545 wxRendererNative::Get().
2546 DrawItemSelectionRect(this, dc, rect, flags);
2547 }
2548 }
2549 else // no full row highlight
2550 {
2551 if ( item->IsSelected() &&
2552 (state != wxTREE_ITEMSTATE_NONE || image != NO_IMAGE) )
2553 {
2554 // If it's selected, and there's an state image or normal image,
2555 // then we should take care to leave the area under the image
2556 // painted in the background colour.
2557 wxRect rect( item->GetX() + state_w + image_w - 2,
2558 item->GetY() + offset,
2559 item->GetWidth() - state_w - image_w + 2,
2560 total_h - offset );
2561 #if !defined(__WXGTK20__) && !defined(__WXMAC__)
2562 dc.DrawRectangle( rect );
2563 #else
2564 rect.x -= 1;
2565 rect.width += 2;
2566
2567 int flags = wxCONTROL_SELECTED;
2568 if (m_hasFocus)
2569 flags |= wxCONTROL_FOCUSED;
2570 if ((item == m_current) && (m_hasFocus))
2571 flags |= wxCONTROL_CURRENT;
2572 wxRendererNative::Get().
2573 DrawItemSelectionRect(this, dc, rect, flags);
2574 #endif
2575 }
2576 // On GTK+ 2, drawing a 'normal' background is wrong for themes that
2577 // don't allow backgrounds to be customized. Not drawing the background,
2578 // except for custom item backgrounds, works for both kinds of theme.
2579 else if (drawItemBackground)
2580 {
2581 wxRect rect( item->GetX() + state_w + image_w - 2,
2582 item->GetY() + offset,
2583 item->GetWidth() - state_w - image_w + 2,
2584 total_h - offset );
2585 if ( hasBgColour )
2586 {
2587 dc.DrawRectangle( rect );
2588 }
2589 else // no specific background colour
2590 {
2591 rect.x -= 1;
2592 rect.width += 2;
2593
2594 int flags = wxCONTROL_SELECTED;
2595 if (m_hasFocus)
2596 flags |= wxCONTROL_FOCUSED;
2597 if ((item == m_current) && (m_hasFocus))
2598 flags |= wxCONTROL_CURRENT;
2599 wxRendererNative::Get().
2600 DrawItemSelectionRect(this, dc, rect, flags);
2601 }
2602 }
2603 }
2604
2605 if ( state != wxTREE_ITEMSTATE_NONE )
2606 {
2607 dc.SetClippingRegion( item->GetX(), item->GetY(), state_w, total_h );
2608 m_imageListState->Draw( state, dc,
2609 item->GetX(),
2610 item->GetY() +
2611 (total_h > state_h ? (total_h-state_h)/2
2612 : 0),
2613 wxIMAGELIST_DRAW_TRANSPARENT );
2614 dc.DestroyClippingRegion();
2615 }
2616
2617 if ( image != NO_IMAGE )
2618 {
2619 dc.SetClippingRegion(item->GetX() + state_w, item->GetY(),
2620 image_w, total_h);
2621 m_imageListNormal->Draw( image, dc,
2622 item->GetX() + state_w,
2623 item->GetY() +
2624 (total_h > image_h ? (total_h-image_h)/2
2625 : 0),
2626 wxIMAGELIST_DRAW_TRANSPARENT );
2627 dc.DestroyClippingRegion();
2628 }
2629
2630 dc.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT);
2631 int extraH = (total_h > text_h) ? (total_h - text_h)/2 : 0;
2632 dc.DrawText( item->GetText(),
2633 (wxCoord)(state_w + image_w + item->GetX()),
2634 (wxCoord)(item->GetY() + extraH));
2635
2636 // restore normal font
2637 dc.SetFont( m_normalFont );
2638
2639 if (item == m_dndEffectItem)
2640 {
2641 dc.SetPen( *wxBLACK_PEN );
2642 // DnD visual effects
2643 switch (m_dndEffect)
2644 {
2645 case BorderEffect:
2646 {
2647 dc.SetBrush(*wxTRANSPARENT_BRUSH);
2648 int w = item->GetWidth() + 2;
2649 int h = total_h + 2;
2650 dc.DrawRectangle( item->GetX() - 1, item->GetY() - 1, w, h);
2651 break;
2652 }
2653 case AboveEffect:
2654 {
2655 int x = item->GetX(),
2656 y = item->GetY();
2657 dc.DrawLine( x, y, x + item->GetWidth(), y);
2658 break;
2659 }
2660 case BelowEffect:
2661 {
2662 int x = item->GetX(),
2663 y = item->GetY();
2664 y += total_h - 1;
2665 dc.DrawLine( x, y, x + item->GetWidth(), y);
2666 break;
2667 }
2668 case NoEffect:
2669 break;
2670 }
2671 }
2672 }
2673
2674 void
2675 wxGenericTreeCtrl::PaintLevel(wxGenericTreeItem *item,
2676 wxDC &dc,
2677 int level,
2678 int &y)
2679 {
2680 int x = level*m_indent;
2681 if (!HasFlag(wxTR_HIDE_ROOT))
2682 {
2683 x += m_indent;
2684 }
2685 else if (level == 0)
2686 {
2687 // always expand hidden root
2688 int origY = y;
2689 wxArrayGenericTreeItems& children = item->GetChildren();
2690 int count = children.GetCount();
2691 if (count > 0)
2692 {
2693 int n = 0, oldY;
2694 do {
2695 oldY = y;
2696 PaintLevel(children[n], dc, 1, y);
2697 } while (++n < count);
2698
2699 if ( !HasFlag(wxTR_NO_LINES) && HasFlag(wxTR_LINES_AT_ROOT)
2700 && count > 0 )
2701 {
2702 // draw line down to last child
2703 origY += GetLineHeight(children[0])>>1;
2704 oldY += GetLineHeight(children[n-1])>>1;
2705 dc.DrawLine(3, origY, 3, oldY);
2706 }
2707 }
2708 return;
2709 }
2710
2711 item->SetX(x+m_spacing);
2712 item->SetY(y);
2713
2714 int h = GetLineHeight(item);
2715 int y_top = y;
2716 int y_mid = y_top + (h>>1);
2717 y += h;
2718
2719 int exposed_x = dc.LogicalToDeviceX(0);
2720 int exposed_y = dc.LogicalToDeviceY(y_top);
2721
2722 if (IsExposed(exposed_x, exposed_y, 10000, h)) // 10000 = very much
2723 {
2724 const wxPen *pen =
2725 #ifndef __WXMAC__
2726 // don't draw rect outline if we already have the
2727 // background color under Mac
2728 (item->IsSelected() && m_hasFocus) ? wxBLACK_PEN :
2729 #endif // !__WXMAC__
2730 wxTRANSPARENT_PEN;
2731
2732 wxColour colText;
2733 if ( item->IsSelected()
2734 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON // TODO CS
2735 // On wxMac, if the tree doesn't have the focus we draw an empty
2736 // rectangle, so we want to make sure that the text is visible
2737 // against the normal background, not the highlightbackground, so
2738 // don't use the highlight text colour unless we have the focus.
2739 && m_hasFocus && IsControlActive( (ControlRef)GetHandle() )
2740 #endif
2741 )
2742 {
2743 #ifdef __WXMAC__
2744 colText = *wxWHITE;
2745 #else
2746 colText = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
2747 #endif
2748 }
2749 else
2750 {
2751 wxTreeItemAttr *attr = item->GetAttributes();
2752 if (attr && attr->HasTextColour())
2753 colText = attr->GetTextColour();
2754 else
2755 colText = GetForegroundColour();
2756 }
2757
2758 // prepare to draw
2759 dc.SetTextForeground(colText);
2760 dc.SetPen(*pen);
2761
2762 // draw
2763 PaintItem(item, dc);
2764
2765 if (HasFlag(wxTR_ROW_LINES))
2766 {
2767 // if the background colour is white, choose a
2768 // contrasting color for the lines
2769 dc.SetPen(*((GetBackgroundColour() == *wxWHITE)
2770 ? wxMEDIUM_GREY_PEN : wxWHITE_PEN));
2771 dc.DrawLine(0, y_top, 10000, y_top);
2772 dc.DrawLine(0, y, 10000, y);
2773 }
2774
2775 // restore DC objects
2776 dc.SetBrush(*wxWHITE_BRUSH);
2777 dc.SetPen(m_dottedPen);
2778 dc.SetTextForeground(*wxBLACK);
2779
2780 if ( !HasFlag(wxTR_NO_LINES) )
2781 {
2782 // draw the horizontal line here
2783 int x_start = x;
2784 if (x > (signed)m_indent)
2785 x_start -= m_indent;
2786 else if (HasFlag(wxTR_LINES_AT_ROOT))
2787 x_start = 3;
2788 dc.DrawLine(x_start, y_mid, x + m_spacing, y_mid);
2789 }
2790
2791 // should the item show a button?
2792 if ( item->HasPlus() && HasButtons() )
2793 {
2794 if ( m_imageListButtons )
2795 {
2796 // draw the image button here
2797 int image_h = 0,
2798 image_w = 0;
2799 int image = item->IsExpanded() ? wxTreeItemIcon_Expanded
2800 : wxTreeItemIcon_Normal;
2801 if ( item->IsSelected() )
2802 image += wxTreeItemIcon_Selected - wxTreeItemIcon_Normal;
2803
2804 m_imageListButtons->GetSize(image, image_w, image_h);
2805 int xx = x - image_w/2;
2806 int yy = y_mid - image_h/2;
2807
2808 wxDCClipper clip(dc, xx, yy, image_w, image_h);
2809 m_imageListButtons->Draw(image, dc, xx, yy,
2810 wxIMAGELIST_DRAW_TRANSPARENT);
2811 }
2812 else // no custom buttons
2813 {
2814 static const int wImage = 9;
2815 static const int hImage = 9;
2816
2817 int flag = 0;
2818 if (item->IsExpanded())
2819 flag |= wxCONTROL_EXPANDED;
2820 if (item == m_underMouse)
2821 flag |= wxCONTROL_CURRENT;
2822
2823 wxRendererNative::Get().DrawTreeItemButton
2824 (
2825 this,
2826 dc,
2827 wxRect(x - wImage/2,
2828 y_mid - hImage/2,
2829 wImage, hImage),
2830 flag
2831 );
2832 }
2833 }
2834 }
2835
2836 if (item->IsExpanded())
2837 {
2838 wxArrayGenericTreeItems& children = item->GetChildren();
2839 int count = children.GetCount();
2840 if (count > 0)
2841 {
2842 int n = 0, oldY;
2843 ++level;
2844 do {
2845 oldY = y;
2846 PaintLevel(children[n], dc, level, y);
2847 } while (++n < count);
2848
2849 if (!HasFlag(wxTR_NO_LINES) && count > 0)
2850 {
2851 // draw line down to last child
2852 oldY += GetLineHeight(children[n-1])>>1;
2853 if (HasButtons()) y_mid += 5;
2854
2855 // Only draw the portion of the line that is visible, in case
2856 // it is huge
2857 wxCoord xOrigin=0, yOrigin=0, width, height;
2858 dc.GetDeviceOrigin(&xOrigin, &yOrigin);
2859 yOrigin = abs(yOrigin);
2860 GetClientSize(&width, &height);
2861
2862 // Move end points to the begining/end of the view?
2863 if (y_mid < yOrigin)
2864 y_mid = yOrigin;
2865 if (oldY > yOrigin + height)
2866 oldY = yOrigin + height;
2867
2868 // after the adjustments if y_mid is larger than oldY then the
2869 // line isn't visible at all so don't draw anything
2870 if (y_mid < oldY)
2871 dc.DrawLine(x, y_mid, x, oldY);
2872 }
2873 }
2874 }
2875 }
2876
2877 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem *item)
2878 {
2879 if ( item )
2880 {
2881 if ( item->HasPlus() )
2882 {
2883 // it's a folder, indicate it by a border
2884 DrawBorder(item);
2885 }
2886 else
2887 {
2888 // draw a line under the drop target because the item will be
2889 // dropped there
2890 DrawLine(item, !m_dropEffectAboveItem );
2891 }
2892
2893 SetCursor(*wxSTANDARD_CURSOR);
2894 }
2895 else
2896 {
2897 // can't drop here
2898 SetCursor(wxCURSOR_NO_ENTRY);
2899 }
2900 }
2901
2902 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId &item)
2903 {
2904 wxCHECK_RET( item.IsOk(), "invalid item in wxGenericTreeCtrl::DrawLine" );
2905
2906 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
2907
2908 if (m_dndEffect == NoEffect)
2909 {
2910 m_dndEffect = BorderEffect;
2911 m_dndEffectItem = i;
2912 }
2913 else
2914 {
2915 m_dndEffect = NoEffect;
2916 m_dndEffectItem = NULL;
2917 }
2918
2919 wxRect rect( i->GetX()-1, i->GetY()-1, i->GetWidth()+2, GetLineHeight(i)+2 );
2920 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2921 RefreshRect( rect );
2922 }
2923
2924 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId &item, bool below)
2925 {
2926 wxCHECK_RET( item.IsOk(), "invalid item in wxGenericTreeCtrl::DrawLine" );
2927
2928 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
2929
2930 if (m_dndEffect == NoEffect)
2931 {
2932 if (below)
2933 m_dndEffect = BelowEffect;
2934 else
2935 m_dndEffect = AboveEffect;
2936 m_dndEffectItem = i;
2937 }
2938 else
2939 {
2940 m_dndEffect = NoEffect;
2941 m_dndEffectItem = NULL;
2942 }
2943
2944 wxRect rect( i->GetX()-1, i->GetY()-1, i->GetWidth()+2, GetLineHeight(i)+2 );
2945 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2946 RefreshRect( rect );
2947 }
2948
2949 // -----------------------------------------------------------------------------
2950 // wxWidgets callbacks
2951 // -----------------------------------------------------------------------------
2952
2953 void wxGenericTreeCtrl::OnSize( wxSizeEvent &event )
2954 {
2955 #ifdef __WXGTK__
2956 if (HasFlag( wxTR_FULL_ROW_HIGHLIGHT) && m_current)
2957 RefreshLine( m_current );
2958 #endif
2959
2960 event.Skip(true);
2961 }
2962
2963 void wxGenericTreeCtrl::OnPaint( wxPaintEvent &WXUNUSED(event) )
2964 {
2965 wxPaintDC dc(this);
2966 PrepareDC( dc );
2967
2968 if ( !m_anchor)
2969 return;
2970
2971 dc.SetFont( m_normalFont );
2972 dc.SetPen( m_dottedPen );
2973
2974 // this is now done dynamically
2975 //if(GetImageList() == NULL)
2976 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2977
2978 int y = 2;
2979 PaintLevel( m_anchor, dc, 0, y );
2980 }
2981
2982 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent &event )
2983 {
2984 m_hasFocus = true;
2985
2986 RefreshSelected();
2987
2988 event.Skip();
2989 }
2990
2991 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent &event )
2992 {
2993 m_hasFocus = false;
2994
2995 RefreshSelected();
2996
2997 event.Skip();
2998 }
2999
3000 void wxGenericTreeCtrl::OnChar( wxKeyEvent &event )
3001 {
3002 wxTreeEvent te( wxEVT_COMMAND_TREE_KEY_DOWN, this);
3003 te.m_evtKey = event;
3004 if ( GetEventHandler()->ProcessEvent( te ) )
3005 {
3006 // intercepted by the user code
3007 return;
3008 }
3009
3010 if ( (m_current == 0) || (m_key_current == 0) )
3011 {
3012 event.Skip();
3013 return;
3014 }
3015
3016 // how should the selection work for this event?
3017 bool is_multiple, extended_select, unselect_others;
3018 EventFlagsToSelType(GetWindowStyleFlag(),
3019 event.ShiftDown(),
3020 event.CmdDown(),
3021 is_multiple, extended_select, unselect_others);
3022
3023 if (GetLayoutDirection() == wxLayout_RightToLeft)
3024 {
3025 if (event.GetKeyCode() == WXK_RIGHT)
3026 event.m_keyCode = WXK_LEFT;
3027 else if (event.GetKeyCode() == WXK_LEFT)
3028 event.m_keyCode = WXK_RIGHT;
3029 }
3030
3031 // + : Expand
3032 // - : Collaspe
3033 // * : Expand all/Collapse all
3034 // ' ' | return : activate
3035 // up : go up (not last children!)
3036 // down : go down
3037 // left : go to parent
3038 // right : open if parent and go next
3039 // home : go to root
3040 // end : go to last item without opening parents
3041 // alnum : start or continue searching for the item with this prefix
3042 int keyCode = event.GetKeyCode();
3043 switch ( keyCode )
3044 {
3045 case '+':
3046 case WXK_ADD:
3047 if (m_current->HasPlus() && !IsExpanded(m_current))
3048 {
3049 Expand(m_current);
3050 }
3051 break;
3052
3053 case '*':
3054 case WXK_MULTIPLY:
3055 if ( !IsExpanded(m_current) )
3056 {
3057 // expand all
3058 ExpandAllChildren(m_current);
3059 break;
3060 }
3061 //else: fall through to Collapse() it
3062
3063 case '-':
3064 case WXK_SUBTRACT:
3065 if (IsExpanded(m_current))
3066 {
3067 Collapse(m_current);
3068 }
3069 break;
3070
3071 case WXK_MENU:
3072 {
3073 // Use the item's bounding rectangle to determine position for
3074 // the event
3075 wxRect ItemRect;
3076 GetBoundingRect(m_current, ItemRect, true);
3077
3078 wxTreeEvent
3079 eventMenu(wxEVT_COMMAND_TREE_ITEM_MENU, this, m_current);
3080 // Use the left edge, vertical middle
3081 eventMenu.m_pointDrag = wxPoint(ItemRect.GetX(),
3082 ItemRect.GetY() +
3083 ItemRect.GetHeight() / 2);
3084 GetEventHandler()->ProcessEvent( eventMenu );
3085 }
3086 break;
3087
3088 case ' ':
3089 case WXK_RETURN:
3090 if ( !event.HasModifiers() )
3091 {
3092 wxTreeEvent
3093 eventAct(wxEVT_COMMAND_TREE_ITEM_ACTIVATED, this, m_current);
3094 GetEventHandler()->ProcessEvent( eventAct );
3095 }
3096
3097 // in any case, also generate the normal key event for this key,
3098 // even if we generated the ACTIVATED event above: this is what
3099 // wxMSW does and it makes sense because you might not want to
3100 // process ACTIVATED event at all and handle Space and Return
3101 // directly (and differently) which would be impossible otherwise
3102 event.Skip();
3103 break;
3104
3105 // up goes to the previous sibling or to the last
3106 // of its children if it's expanded
3107 case WXK_UP:
3108 {
3109 wxTreeItemId prev = GetPrevSibling( m_key_current );
3110 if (!prev)
3111 {
3112 prev = GetItemParent( m_key_current );
3113 if ((prev == GetRootItem()) && HasFlag(wxTR_HIDE_ROOT))
3114 {
3115 break; // don't go to root if it is hidden
3116 }
3117 if (prev)
3118 {
3119 wxTreeItemIdValue cookie;
3120 wxTreeItemId current = m_key_current;
3121 // TODO: Huh? If we get here, we'd better be the first
3122 // child of our parent. How else could it be?
3123 if (current == GetFirstChild( prev, cookie ))
3124 {
3125 // otherwise we return to where we came from
3126 DoSelectItem(prev,
3127 unselect_others,
3128 extended_select);
3129 m_key_current = (wxGenericTreeItem*) prev.m_pItem;
3130 break;
3131 }
3132 }
3133 }
3134 if (prev)
3135 {
3136 while ( IsExpanded(prev) && HasChildren(prev) )
3137 {
3138 wxTreeItemId child = GetLastChild(prev);
3139 if ( child )
3140 {
3141 prev = child;
3142 }
3143 }
3144
3145 DoSelectItem( prev, unselect_others, extended_select );
3146 m_key_current=(wxGenericTreeItem*) prev.m_pItem;
3147 }
3148 }
3149 break;
3150
3151 // left arrow goes to the parent
3152 case WXK_LEFT:
3153 {
3154 wxTreeItemId prev = GetItemParent( m_current );
3155 if ((prev == GetRootItem()) && HasFlag(wxTR_HIDE_ROOT))
3156 {
3157 // don't go to root if it is hidden
3158 prev = GetPrevSibling( m_current );
3159 }
3160 if (prev)
3161 {
3162 DoSelectItem( prev, unselect_others, extended_select );
3163 }
3164 }
3165 break;
3166
3167 case WXK_RIGHT:
3168 // this works the same as the down arrow except that we
3169 // also expand the item if it wasn't expanded yet
3170 if (m_current != GetRootItem().m_pItem || !HasFlag(wxTR_HIDE_ROOT))
3171 Expand(m_current);
3172 //else: don't try to expand hidden root item (which can be the
3173 // current one when the tree is empty)
3174
3175 // fall through
3176
3177 case WXK_DOWN:
3178 {
3179 if (IsExpanded(m_key_current) && HasChildren(m_key_current))
3180 {
3181 wxTreeItemIdValue cookie;
3182 wxTreeItemId child = GetFirstChild( m_key_current, cookie );
3183 if ( !child )
3184 break;
3185
3186 DoSelectItem( child, unselect_others, extended_select );
3187 m_key_current=(wxGenericTreeItem*) child.m_pItem;
3188 }
3189 else
3190 {
3191 wxTreeItemId next = GetNextSibling( m_key_current );
3192 if (!next)
3193 {
3194 wxTreeItemId current = m_key_current;
3195 while (current.IsOk() && !next)
3196 {
3197 current = GetItemParent( current );
3198 if (current) next = GetNextSibling( current );
3199 }
3200 }
3201 if (next)
3202 {
3203 DoSelectItem( next, unselect_others, extended_select );
3204 m_key_current=(wxGenericTreeItem*) next.m_pItem;
3205 }
3206 }
3207 }
3208 break;
3209
3210 // <End> selects the last visible tree item
3211 case WXK_END:
3212 {
3213 wxTreeItemId last = GetRootItem();
3214
3215 while ( last.IsOk() && IsExpanded(last) )
3216 {
3217 wxTreeItemId lastChild = GetLastChild(last);
3218
3219 // it may happen if the item was expanded but then all of
3220 // its children have been deleted - so IsExpanded() returned
3221 // true, but GetLastChild() returned invalid item
3222 if ( !lastChild )
3223 break;
3224
3225 last = lastChild;
3226 }
3227
3228 if ( last.IsOk() )
3229 {
3230 DoSelectItem( last, unselect_others, extended_select );
3231 }
3232 }
3233 break;
3234
3235 // <Home> selects the root item
3236 case WXK_HOME:
3237 {
3238 wxTreeItemId prev = GetRootItem();
3239 if (!prev)
3240 break;
3241
3242 if ( HasFlag(wxTR_HIDE_ROOT) )
3243 {
3244 wxTreeItemIdValue cookie;
3245 prev = GetFirstChild(prev, cookie);
3246 if (!prev)
3247 break;
3248 }
3249
3250 DoSelectItem( prev, unselect_others, extended_select );
3251 }
3252 break;
3253
3254 default:
3255 // do not use wxIsalnum() here
3256 if ( !event.HasModifiers() &&
3257 ((keyCode >= '0' && keyCode <= '9') ||
3258 (keyCode >= 'a' && keyCode <= 'z') ||
3259 (keyCode >= 'A' && keyCode <= 'Z' )))
3260 {
3261 // find the next item starting with the given prefix
3262 wxChar ch = (wxChar)keyCode;
3263
3264 wxTreeItemId id = FindItem(m_current, m_findPrefix + ch);
3265 if ( !id.IsOk() )
3266 {
3267 // no such item
3268 break;
3269 }
3270
3271 SelectItem(id);
3272
3273 m_findPrefix += ch;
3274
3275 // also start the timer to reset the current prefix if the user
3276 // doesn't press any more alnum keys soon -- we wouldn't want
3277 // to use this prefix for a new item search
3278 if ( !m_findTimer )
3279 {
3280 m_findTimer = new wxTreeFindTimer(this);
3281 }
3282
3283 m_findTimer->Start(wxTreeFindTimer::DELAY, wxTIMER_ONE_SHOT);
3284 }
3285 else
3286 {
3287 event.Skip();
3288 }
3289 }
3290 }
3291
3292 wxTreeItemId
3293 wxGenericTreeCtrl::DoTreeHitTest(const wxPoint& point, int& flags) const
3294 {
3295 int w, h;
3296 GetSize(&w, &h);
3297 flags=0;
3298 if (point.x<0) flags |= wxTREE_HITTEST_TOLEFT;
3299 if (point.x>w) flags |= wxTREE_HITTEST_TORIGHT;
3300 if (point.y<0) flags |= wxTREE_HITTEST_ABOVE;
3301 if (point.y>h) flags |= wxTREE_HITTEST_BELOW;
3302 if (flags) return wxTreeItemId();
3303
3304 if (m_anchor == NULL)
3305 {
3306 flags = wxTREE_HITTEST_NOWHERE;
3307 return wxTreeItemId();
3308 }
3309
3310 wxGenericTreeItem *hit = m_anchor->HitTest(CalcUnscrolledPosition(point),
3311 this, flags, 0);
3312 if (hit == NULL)
3313 {
3314 flags = wxTREE_HITTEST_NOWHERE;
3315 return wxTreeItemId();
3316 }
3317 return hit;
3318 }
3319
3320 // get the bounding rectangle of the item (or of its label only)
3321 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId& item,
3322 wxRect& rect,
3323 bool textOnly) const
3324 {
3325 wxCHECK_MSG( item.IsOk(), false,
3326 "invalid item in wxGenericTreeCtrl::GetBoundingRect" );
3327
3328 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
3329
3330 if ( textOnly )
3331 {
3332 int image_h = 0, image_w = 0;
3333 int image = ((wxGenericTreeItem*) item.m_pItem)->GetCurrentImage();
3334 if ( image != NO_IMAGE && m_imageListNormal )
3335 {
3336 m_imageListNormal->GetSize( image, image_w, image_h );
3337 image_w += MARGIN_BETWEEN_IMAGE_AND_TEXT;
3338 }
3339
3340 int state_h = 0, state_w = 0;
3341 int state = ((wxGenericTreeItem*) item.m_pItem)->GetState();
3342 if ( state != wxTREE_ITEMSTATE_NONE && m_imageListState )
3343 {
3344 m_imageListState->GetSize( state, state_w, state_h );
3345 if ( image_w != 0 )
3346 state_w += MARGIN_BETWEEN_STATE_AND_IMAGE;
3347 else
3348 state_w += MARGIN_BETWEEN_IMAGE_AND_TEXT;
3349 }
3350
3351 rect.x = i->GetX() + state_w + image_w;
3352 rect.width = i->GetWidth() - state_w - image_w;
3353
3354 }
3355 else // the entire line
3356 {
3357 rect.x = 0;
3358 rect.width = GetClientSize().x;
3359 }
3360
3361 rect.y = i->GetY();
3362 rect.height = GetLineHeight(i);
3363
3364 // we have to return the logical coordinates, not physical ones
3365 rect.SetTopLeft(CalcScrolledPosition(rect.GetTopLeft()));
3366
3367 return true;
3368 }
3369
3370 wxTextCtrl *wxGenericTreeCtrl::EditLabel(const wxTreeItemId& item,
3371 wxClassInfo * WXUNUSED(textCtrlClass))
3372 {
3373 wxCHECK_MSG( item.IsOk(), NULL, _T("can't edit an invalid item") );
3374
3375 wxGenericTreeItem *itemEdit = (wxGenericTreeItem *)item.m_pItem;
3376
3377 wxTreeEvent te(wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, this, itemEdit);
3378 if ( GetEventHandler()->ProcessEvent( te ) && !te.IsAllowed() )
3379 {
3380 // vetoed by user
3381 return NULL;
3382 }
3383
3384 // We have to call this here because the label in
3385 // question might just have been added and no screen
3386 // update taken place.
3387 if ( m_dirty )
3388 #if defined( __WXMSW__ ) || defined(__WXMAC__)
3389 Update();
3390 #else
3391 DoDirtyProcessing();
3392 #endif
3393
3394 // TODO: use textCtrlClass here to create the control of correct class
3395 m_textCtrl = new wxTreeTextCtrl(this, itemEdit);
3396
3397 m_textCtrl->SetFocus();
3398
3399 return m_textCtrl;
3400 }
3401
3402 // returns a pointer to the text edit control if the item is being
3403 // edited, NULL otherwise (it's assumed that no more than one item may
3404 // be edited simultaneously)
3405 wxTextCtrl* wxGenericTreeCtrl::GetEditControl() const
3406 {
3407 return m_textCtrl;
3408 }
3409
3410 void wxGenericTreeCtrl::EndEditLabel(const wxTreeItemId& WXUNUSED(item),
3411 bool discardChanges)
3412 {
3413 wxCHECK_RET( m_textCtrl, _T("not editing label") );
3414
3415 m_textCtrl->EndEdit(discardChanges);
3416 }
3417
3418 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem *item,
3419 const wxString& value)
3420 {
3421 wxTreeEvent le(wxEVT_COMMAND_TREE_END_LABEL_EDIT, this, item);
3422 le.m_label = value;
3423 le.m_editCancelled = false;
3424
3425 return !GetEventHandler()->ProcessEvent( le ) || le.IsAllowed();
3426 }
3427
3428 void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem *item)
3429 {
3430 // let owner know that the edit was cancelled
3431 wxTreeEvent le(wxEVT_COMMAND_TREE_END_LABEL_EDIT, this, item);
3432 le.m_label = wxEmptyString;
3433 le.m_editCancelled = true;
3434
3435 GetEventHandler()->ProcessEvent( le );
3436 }
3437
3438 void wxGenericTreeCtrl::OnRenameTimer()
3439 {
3440 EditLabel( m_current );
3441 }
3442
3443 void wxGenericTreeCtrl::OnMouse( wxMouseEvent &event )
3444 {
3445 if ( !m_anchor )return;
3446
3447 wxPoint pt = CalcUnscrolledPosition(event.GetPosition());
3448
3449 // Is the mouse over a tree item button?
3450 int flags = 0;
3451 wxGenericTreeItem *thisItem = m_anchor->HitTest(pt, this, flags, 0);
3452 wxGenericTreeItem *underMouse = thisItem;
3453 #if wxUSE_TOOLTIPS
3454 bool underMouseChanged = (underMouse != m_underMouse) ;
3455 #endif // wxUSE_TOOLTIPS
3456
3457 if ((underMouse) &&
3458 (flags & wxTREE_HITTEST_ONITEMBUTTON) &&
3459 (!event.LeftIsDown()) &&
3460 (!m_isDragging) &&
3461 (!m_renameTimer || !m_renameTimer->IsRunning()))
3462 {
3463 }
3464 else
3465 {
3466 underMouse = NULL;
3467 }
3468
3469 if (underMouse != m_underMouse)
3470 {
3471 if (m_underMouse)
3472 {
3473 // unhighlight old item
3474 wxGenericTreeItem *tmp = m_underMouse;
3475 m_underMouse = NULL;
3476 RefreshLine( tmp );
3477 }
3478
3479 m_underMouse = underMouse;
3480 if (m_underMouse)
3481 RefreshLine( m_underMouse );
3482 }
3483
3484 #if wxUSE_TOOLTIPS
3485 // Determines what item we are hovering over and need a tooltip for
3486 wxTreeItemId hoverItem = thisItem;
3487
3488 // We do not want a tooltip if we are dragging, or if the rename timer is
3489 // running
3490 if ( underMouseChanged &&
3491 hoverItem.IsOk() &&
3492 !m_isDragging &&
3493 (!m_renameTimer || !m_renameTimer->IsRunning()) )
3494 {
3495 // Ask the tree control what tooltip (if any) should be shown
3496 wxTreeEvent
3497 hevent(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP, this, hoverItem);
3498
3499 if ( GetEventHandler()->ProcessEvent(hevent) && hevent.IsAllowed() )
3500 {
3501 SetToolTip(hevent.m_label);
3502 }
3503 }
3504 #endif
3505
3506 // we process left mouse up event (enables in-place edit), middle/right down
3507 // (pass to the user code), left dbl click (activate item) and
3508 // dragging/moving events for items drag-and-drop
3509 if ( !(event.LeftDown() ||
3510 event.LeftUp() ||
3511 event.MiddleDown() ||
3512 event.RightDown() ||
3513 event.LeftDClick() ||
3514 event.Dragging() ||
3515 ((event.Moving() || event.RightUp()) && m_isDragging)) )
3516 {
3517 event.Skip();
3518
3519 return;
3520 }
3521
3522
3523 flags = 0;
3524 wxGenericTreeItem *item = m_anchor->HitTest(pt, this, flags, 0);
3525
3526 if ( event.Dragging() && !m_isDragging )
3527 {
3528 if (m_dragCount == 0)
3529 m_dragStart = pt;
3530
3531 m_dragCount++;
3532
3533 if (m_dragCount != 3)
3534 {
3535 // wait until user drags a bit further...
3536 return;
3537 }
3538
3539 wxEventType command = event.RightIsDown()
3540 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
3541 : wxEVT_COMMAND_TREE_BEGIN_DRAG;
3542
3543 wxTreeEvent nevent(command, this, m_current);
3544 nevent.SetPoint(CalcScrolledPosition(pt));
3545
3546 // by default the dragging is not supported, the user code must
3547 // explicitly allow the event for it to take place
3548 nevent.Veto();
3549
3550 if ( GetEventHandler()->ProcessEvent(nevent) && nevent.IsAllowed() )
3551 {
3552 // we're going to drag this item
3553 m_isDragging = true;
3554
3555 // remember the old cursor because we will change it while
3556 // dragging
3557 m_oldCursor = m_cursor;
3558
3559 // in a single selection control, hide the selection temporarily
3560 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE) )
3561 {
3562 m_oldSelection = (wxGenericTreeItem*) GetSelection().m_pItem;
3563
3564 if ( m_oldSelection )
3565 {
3566 m_oldSelection->SetHilight(false);
3567 RefreshLine(m_oldSelection);
3568 }
3569 }
3570
3571 CaptureMouse();
3572 }
3573 }
3574 else if ( event.Dragging() )
3575 {
3576 if ( item != m_dropTarget )
3577 {
3578 // unhighlight the previous drop target
3579 DrawDropEffect(m_dropTarget);
3580
3581 m_dropTarget = item;
3582
3583 // highlight the current drop target if any
3584 DrawDropEffect(m_dropTarget);
3585
3586 #if defined(__WXMSW__) || defined(__WXMAC__) || defined(__WXGTK20__)
3587 Update();
3588 #else
3589 // TODO: remove this call or use wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI)
3590 // instead (needs to be tested!)
3591 wxYieldIfNeeded();
3592 #endif
3593 }
3594 }
3595 else if ( (event.LeftUp() || event.RightUp()) && m_isDragging )
3596 {
3597 ReleaseMouse();
3598
3599 // erase the highlighting
3600 DrawDropEffect(m_dropTarget);
3601
3602 if ( m_oldSelection )
3603 {
3604 m_oldSelection->SetHilight(true);
3605 RefreshLine(m_oldSelection);
3606 m_oldSelection = NULL;
3607 }
3608
3609 // generate the drag end event
3610 wxTreeEvent eventEndDrag(wxEVT_COMMAND_TREE_END_DRAG, this, item);
3611
3612 eventEndDrag.m_pointDrag = CalcScrolledPosition(pt);
3613
3614 (void)GetEventHandler()->ProcessEvent(eventEndDrag);
3615
3616 m_isDragging = false;
3617 m_dropTarget = NULL;
3618
3619 SetCursor(m_oldCursor);
3620
3621 #if defined( __WXMSW__ ) || defined(__WXMAC__) || defined(__WXGTK20__)
3622 Update();
3623 #else
3624 // TODO: remove this call or use wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI)
3625 // instead (needs to be tested!)
3626 wxYieldIfNeeded();
3627 #endif
3628 }
3629 else
3630 {
3631 // If we got to this point, we are not dragging or moving the mouse.
3632 // Because the code in carbon/toplevel.cpp will only set focus to the
3633 // tree if we skip for EVT_LEFT_DOWN, we MUST skip this event here for
3634 // focus to work.
3635 // We skip even if we didn't hit an item because we still should
3636 // restore focus to the tree control even if we didn't exactly hit an
3637 // item.
3638 if ( event.LeftDown() )
3639 {
3640 event.Skip();
3641 }
3642
3643 // here we process only the messages which happen on tree items
3644
3645 m_dragCount = 0;
3646
3647 if (item == NULL) return; /* we hit the blank area */
3648
3649 if ( event.RightDown() )
3650 {
3651 // If the item is already selected, do not update the selection.
3652 // Multi-selections should not be cleared if a selected item is
3653 // clicked.
3654 if (!IsSelected(item))
3655 {
3656 DoSelectItem(item, true, false);
3657 }
3658
3659 wxTreeEvent
3660 nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK, this, item);
3661 nevent.m_pointDrag = CalcScrolledPosition(pt);
3662 event.Skip(!GetEventHandler()->ProcessEvent(nevent));
3663
3664 // Consistent with MSW (for now), send the ITEM_MENU *after*
3665 // the RIGHT_CLICK event. TODO: This behavior may change.
3666 wxTreeEvent nevent2(wxEVT_COMMAND_TREE_ITEM_MENU, this, item);
3667 nevent2.m_pointDrag = CalcScrolledPosition(pt);
3668 GetEventHandler()->ProcessEvent(nevent2);
3669 }
3670 else if ( event.MiddleDown() )
3671 {
3672 wxTreeEvent
3673 nevent(wxEVT_COMMAND_TREE_ITEM_MIDDLE_CLICK, this, item);
3674 nevent.m_pointDrag = CalcScrolledPosition(pt);
3675 event.Skip(!GetEventHandler()->ProcessEvent(nevent));
3676 }
3677 else if ( event.LeftUp() )
3678 {
3679 if (flags & wxTREE_HITTEST_ONITEMSTATEICON)
3680 {
3681 wxTreeEvent
3682 nevent(wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK, this, item);
3683 GetEventHandler()->ProcessEvent(nevent);
3684 }
3685
3686 // this facilitates multiple-item drag-and-drop
3687
3688 if ( /* item && */ HasFlag(wxTR_MULTIPLE))
3689 {
3690 wxArrayTreeItemIds selections;
3691 size_t count = GetSelections(selections);
3692
3693 if (count > 1 &&
3694 !event.CmdDown() &&
3695 !event.ShiftDown())
3696 {
3697 DoSelectItem(item, true, false);
3698 }
3699 }
3700
3701 if ( m_lastOnSame )
3702 {
3703 if ( (item == m_current) &&
3704 (flags & wxTREE_HITTEST_ONITEMLABEL) &&
3705 HasFlag(wxTR_EDIT_LABELS) )
3706 {
3707 if ( m_renameTimer )
3708 {
3709 if ( m_renameTimer->IsRunning() )
3710 m_renameTimer->Stop();
3711 }
3712 else
3713 {
3714 m_renameTimer = new wxTreeRenameTimer( this );
3715 }
3716
3717 m_renameTimer->Start( wxTreeRenameTimer::DELAY, true );
3718 }
3719
3720 m_lastOnSame = false;
3721 }
3722 }
3723 else // !RightDown() && !MiddleDown() && !LeftUp()
3724 {
3725 // ==> LeftDown() || LeftDClick()
3726 if ( event.LeftDown() )
3727 {
3728 m_lastOnSame = item == m_current;
3729 }
3730
3731 if ( flags & wxTREE_HITTEST_ONITEMBUTTON )
3732 {
3733 // only toggle the item for a single click, double click on
3734 // the button doesn't do anything (it toggles the item twice)
3735 if ( event.LeftDown() )
3736 {
3737 Toggle( item );
3738 }
3739
3740 // don't select the item if the button was clicked
3741 return;
3742 }
3743
3744
3745 // clear the previously selected items, if the
3746 // user clicked outside of the present selection.
3747 // otherwise, perform the deselection on mouse-up.
3748 // this allows multiple drag and drop to work.
3749 // but if Cmd is down, toggle selection of the clicked item
3750 if (!IsSelected(item) || event.CmdDown())
3751 {
3752 // how should the selection work for this event?
3753 bool is_multiple, extended_select, unselect_others;
3754 EventFlagsToSelType(GetWindowStyleFlag(),
3755 event.ShiftDown(),
3756 event.CmdDown(),
3757 is_multiple,
3758 extended_select,
3759 unselect_others);
3760
3761 DoSelectItem(item, unselect_others, extended_select);
3762 }
3763
3764
3765 // For some reason, Windows isn't recognizing a left double-click,
3766 // so we need to simulate it here. Allow 200 milliseconds for now.
3767 if ( event.LeftDClick() )
3768 {
3769 // double clicking should not start editing the item label
3770 if ( m_renameTimer )
3771 m_renameTimer->Stop();
3772
3773 m_lastOnSame = false;
3774
3775 // send activate event first
3776 wxTreeEvent
3777 nevent(wxEVT_COMMAND_TREE_ITEM_ACTIVATED, this, item);
3778 nevent.m_pointDrag = CalcScrolledPosition(pt);
3779 if ( !GetEventHandler()->ProcessEvent( nevent ) )
3780 {
3781 // if the user code didn't process the activate event,
3782 // handle it ourselves by toggling the item when it is
3783 // double clicked
3784 if ( item->HasPlus() )
3785 {
3786 Toggle(item);
3787 }
3788 }
3789 }
3790 }
3791 }
3792 }
3793
3794 void wxGenericTreeCtrl::OnInternalIdle()
3795 {
3796 wxWindow::OnInternalIdle();
3797
3798 // Check if we need to select the root item
3799 // because nothing else has been selected.
3800 // Delaying it means that we can invoke event handlers
3801 // as required, when a first item is selected.
3802 if (!HasFlag(wxTR_MULTIPLE) && !GetSelection().IsOk())
3803 {
3804 if (m_select_me)
3805 SelectItem(m_select_me);
3806 else if (GetRootItem().IsOk())
3807 SelectItem(GetRootItem());
3808 }
3809
3810 // after all changes have been done to the tree control,
3811 // actually redraw the tree when everything is over
3812 if (m_dirty)
3813 DoDirtyProcessing();
3814 }
3815
3816 void
3817 wxGenericTreeCtrl::CalculateLevel(wxGenericTreeItem *item,
3818 wxDC &dc,
3819 int level,
3820 int &y )
3821 {
3822 int x = level*m_indent;
3823 if (!HasFlag(wxTR_HIDE_ROOT))
3824 {
3825 x += m_indent;
3826 }
3827 else if (level == 0)
3828 {
3829 // a hidden root is not evaluated, but its
3830 // children are always calculated
3831 goto Recurse;
3832 }
3833
3834 item->CalculateSize(this, dc);
3835
3836 // set its position
3837 item->SetX( x+m_spacing );
3838 item->SetY( y );
3839 y += GetLineHeight(item);
3840
3841 if ( !item->IsExpanded() )
3842 {
3843 // we don't need to calculate collapsed branches
3844 return;
3845 }
3846
3847 Recurse:
3848 wxArrayGenericTreeItems& children = item->GetChildren();
3849 size_t n, count = children.GetCount();
3850 ++level;
3851 for (n = 0; n < count; ++n )
3852 CalculateLevel( children[n], dc, level, y ); // recurse
3853 }
3854
3855 void wxGenericTreeCtrl::CalculatePositions()
3856 {
3857 if ( !m_anchor ) return;
3858
3859 wxClientDC dc(this);
3860 PrepareDC( dc );
3861
3862 dc.SetFont( m_normalFont );
3863
3864 dc.SetPen( m_dottedPen );
3865 //if(GetImageList() == NULL)
3866 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3867
3868 int y = 2;
3869 CalculateLevel( m_anchor, dc, 0, y ); // start recursion
3870 }
3871
3872 void wxGenericTreeCtrl::Refresh(bool eraseBackground, const wxRect *rect)
3873 {
3874 if ( !IsFrozen() )
3875 wxTreeCtrlBase::Refresh(eraseBackground, rect);
3876 }
3877
3878 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem *item)
3879 {
3880 if (m_dirty || IsFrozen() )
3881 return;
3882
3883 wxSize client = GetClientSize();
3884
3885 wxRect rect;
3886 CalcScrolledPosition(0, item->GetY(), NULL, &rect.y);
3887 rect.width = client.x;
3888 rect.height = client.y;
3889
3890 Refresh(true, &rect);
3891
3892 AdjustMyScrollbars();
3893 }
3894
3895 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem *item )
3896 {
3897 if (m_dirty || IsFrozen() )
3898 return;
3899
3900 wxRect rect;
3901 CalcScrolledPosition(0, item->GetY(), NULL, &rect.y);
3902 rect.width = GetClientSize().x;
3903 rect.height = GetLineHeight(item); //dc.GetCharHeight() + 6;
3904
3905 Refresh(true, &rect);
3906 }
3907
3908 void wxGenericTreeCtrl::RefreshSelected()
3909 {
3910 if (IsFrozen())
3911 return;
3912
3913 // TODO: this is awfully inefficient, we should keep the list of all
3914 // selected items internally, should be much faster
3915 if ( m_anchor )
3916 RefreshSelectedUnder(m_anchor);
3917 }
3918
3919 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem *item)
3920 {
3921 if (IsFrozen())
3922 return;
3923
3924 if ( item->IsSelected() )
3925 RefreshLine(item);
3926
3927 const wxArrayGenericTreeItems& children = item->GetChildren();
3928 size_t count = children.GetCount();
3929 for ( size_t n = 0; n < count; n++ )
3930 {
3931 RefreshSelectedUnder(children[n]);
3932 }
3933 }
3934
3935 void wxGenericTreeCtrl::DoThaw()
3936 {
3937 wxTreeCtrlBase::DoThaw();
3938
3939 if ( m_dirty )
3940 DoDirtyProcessing();
3941 else
3942 Refresh();
3943 }
3944
3945 // ----------------------------------------------------------------------------
3946 // changing colours: we need to refresh the tree control
3947 // ----------------------------------------------------------------------------
3948
3949 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour& colour)
3950 {
3951 if ( !wxWindow::SetBackgroundColour(colour) )
3952 return false;
3953
3954 Refresh();
3955
3956 return true;
3957 }
3958
3959 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour& colour)
3960 {
3961 if ( !wxWindow::SetForegroundColour(colour) )
3962 return false;
3963
3964 Refresh();
3965
3966 return true;
3967 }
3968
3969 // Process the tooltip event, to speed up event processing.
3970 // Doesn't actually get a tooltip.
3971 void wxGenericTreeCtrl::OnGetToolTip( wxTreeEvent &event )
3972 {
3973 event.Veto();
3974 }
3975
3976
3977 // NOTE: If using the wxListBox visual attributes works everywhere then this can
3978 // be removed, as well as the #else case below.
3979 #define _USE_VISATTR 0
3980
3981 //static
3982 wxVisualAttributes
3983 #if _USE_VISATTR
3984 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant)
3985 #else
3986 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
3987 #endif
3988 {
3989 #if _USE_VISATTR
3990 // Use the same color scheme as wxListBox
3991 return wxListBox::GetClassDefaultAttributes(variant);
3992 #else
3993 wxVisualAttributes attr;
3994 attr.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXTEXT);
3995 attr.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX);
3996 attr.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
3997 return attr;
3998 #endif
3999 }
4000
4001 void wxGenericTreeCtrl::DoDirtyProcessing()
4002 {
4003 if (IsFrozen())
4004 return;
4005
4006 m_dirty = false;
4007
4008 CalculatePositions();
4009 Refresh();
4010 AdjustMyScrollbars();
4011 }
4012
4013 wxSize wxGenericTreeCtrl::DoGetBestSize() const
4014 {
4015 // make sure all positions are calculated as normally this only done during
4016 // idle time but we need them for base class DoGetBestSize() to return the
4017 // correct result
4018 wxConstCast(this, wxGenericTreeCtrl)->CalculatePositions();
4019
4020 wxSize size = wxTreeCtrlBase::DoGetBestSize();
4021
4022 // there seems to be an implicit extra border around the items, although
4023 // I'm not really sure where does it come from -- but without this, the
4024 // scrollbars appear in a tree with default/best size
4025 size.IncBy(4, 4);
4026
4027 // and the border has to be rounded up to a multiple of PIXELS_PER_UNIT or
4028 // scrollbars still appear
4029 const wxSize& borderSize = GetWindowBorderSize();
4030
4031 int dx = (size.x - borderSize.x) % PIXELS_PER_UNIT;
4032 if ( dx )
4033 size.x += PIXELS_PER_UNIT - dx;
4034 int dy = (size.y - borderSize.y) % PIXELS_PER_UNIT;
4035 if ( dy )
4036 size.y += PIXELS_PER_UNIT - dy;
4037
4038 // we need to update the cache too as the base class cached its own value
4039 CacheBestSize(size);
4040
4041 return size;
4042 }
4043
4044 #endif // wxUSE_TREECTRL