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