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