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