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