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