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