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