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