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