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