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