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