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