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