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