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