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