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