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