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