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