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