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