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