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