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