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