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