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