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