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