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