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