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