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