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