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