]> git.saurik.com Git - wxWidgets.git/blob - src/generic/treectrl.cpp
OnEnter/OnDragOver API update (compiles, links, somewhat runs under wxGTK)
[wxWidgets.git] / src / generic / treectrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: treectrl.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 "treectrl.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 #include "wx/treectrl.h"
32 #include "wx/generic/imaglist.h"
33 #include "wx/settings.h"
34 #include "wx/log.h"
35 #include "wx/intl.h"
36 #include "wx/dynarray.h"
37 #include "wx/arrimpl.cpp"
38 #include "wx/dcclient.h"
39 #include "wx/msgdlg.h"
40
41 // -----------------------------------------------------------------------------
42 // array types
43 // -----------------------------------------------------------------------------
44
45 class WXDLLEXPORT wxGenericTreeItem;
46
47 WX_DEFINE_ARRAY(wxGenericTreeItem *, wxArrayGenericTreeItems);
48 WX_DEFINE_OBJARRAY(wxArrayTreeItemIds);
49
50 // ----------------------------------------------------------------------------
51 // constants
52 // ----------------------------------------------------------------------------
53
54 static const int NO_IMAGE = -1;
55
56 // -----------------------------------------------------------------------------
57 // private classes
58 // -----------------------------------------------------------------------------
59
60 // a tree item
61 class WXDLLEXPORT wxGenericTreeItem
62 {
63 public:
64 // ctors & dtor
65 wxGenericTreeItem() { m_data = NULL; }
66 wxGenericTreeItem( wxGenericTreeItem *parent,
67 const wxString& text,
68 wxDC& dc,
69 int image, int selImage,
70 wxTreeItemData *data );
71
72 ~wxGenericTreeItem();
73
74 // trivial accessors
75 wxArrayGenericTreeItems& GetChildren() { return m_children; }
76
77 const wxString& GetText() const { return m_text; }
78 int GetImage(wxTreeItemIcon which = wxTreeItemIcon_Normal) const
79 { return m_images[which]; }
80 wxTreeItemData *GetData() const { return m_data; }
81
82 // returns the current image for the item (depending on its
83 // selected/expanded/whatever state)
84 int GetCurrentImage() const;
85
86 void SetText( const wxString &text );
87 void SetImage(int image, wxTreeItemIcon which) { m_images[which] = image; }
88 void SetData(wxTreeItemData *data) { m_data = data; }
89
90 void SetHasPlus(bool has = TRUE) { m_hasPlus = has; }
91
92 void SetBold(bool bold) { m_isBold = bold; }
93
94 int GetX() const { return m_x; }
95 int GetY() const { return m_y; }
96
97 void SetX(int x) { m_x = x; }
98 void SetY(int y) { m_y = y; }
99
100 int GetHeight() const { return m_height; }
101 int GetWidth() const { return m_width; }
102
103 void SetHeight(int h) { m_height = h; }
104 void SetWidth(int w) { m_width = w; }
105
106
107 wxGenericTreeItem *GetParent() const { return m_parent; }
108
109 // operations
110 // deletes all children notifying the treectrl about it if !NULL pointer
111 // given
112 void DeleteChildren(wxTreeCtrl *tree = NULL);
113 // FIXME don't know what is it for
114 void Reset();
115
116 // get count of all children (and grand children if 'recursively')
117 size_t GetChildrenCount(bool recursively = TRUE) const;
118
119 void Insert(wxGenericTreeItem *child, size_t index)
120 { m_children.Insert(child, index); }
121
122 void SetCross( int x, int y );
123 void GetSize( int &x, int &y, const wxTreeCtrl* );
124
125 // return the item at given position (or NULL if no item), onButton is TRUE
126 // if the point belongs to the item's button, otherwise it lies on the
127 // button's label
128 wxGenericTreeItem *HitTest( const wxPoint& point, const wxTreeCtrl *, int &flags);
129
130 void Expand() { m_isCollapsed = FALSE; }
131 void Collapse() { m_isCollapsed = TRUE; }
132
133 void SetHilight( bool set = TRUE ) { m_hasHilight = set; }
134
135 // status inquiries
136 bool HasChildren() const { return !m_children.IsEmpty(); }
137 bool IsSelected() const { return m_hasHilight; }
138 bool IsExpanded() const { return !m_isCollapsed; }
139 bool HasPlus() const { return m_hasPlus || HasChildren(); }
140 bool IsBold() const { return m_isBold; }
141
142 private:
143 wxString m_text;
144
145 // tree ctrl images for the normal, selected, expanded and expanded+selected
146 // states
147 int m_images[wxTreeItemIcon_Max];
148
149 wxTreeItemData *m_data;
150
151 // use bitfields to save size
152 int m_isCollapsed :1;
153 int m_hasHilight :1; // same as focused
154 int m_hasPlus :1; // used for item which doesn't have
155 // children but still has a [+] button
156 int m_isBold :1; // render the label in bold font
157
158 int m_x, m_y;
159 long m_height, m_width;
160 int m_xCross, m_yCross;
161 int m_level;
162 wxArrayGenericTreeItems m_children;
163 wxGenericTreeItem *m_parent;
164 };
165
166 // =============================================================================
167 // implementation
168 // =============================================================================
169
170
171 // -----------------------------------------------------------------------------
172 // wxTreeRenameTimer (internal)
173 // -----------------------------------------------------------------------------
174
175 wxTreeRenameTimer::wxTreeRenameTimer( wxTreeCtrl *owner )
176 {
177 m_owner = owner;
178 }
179
180 void wxTreeRenameTimer::Notify()
181 {
182 m_owner->OnRenameTimer();
183 }
184
185 //-----------------------------------------------------------------------------
186 // wxTreeTextCtrl (internal)
187 //-----------------------------------------------------------------------------
188
189 IMPLEMENT_DYNAMIC_CLASS(wxTreeTextCtrl,wxTextCtrl);
190
191 BEGIN_EVENT_TABLE(wxTreeTextCtrl,wxTextCtrl)
192 EVT_CHAR (wxTreeTextCtrl::OnChar)
193 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus)
194 END_EVENT_TABLE()
195
196 wxTreeTextCtrl::wxTreeTextCtrl( wxWindow *parent, const wxWindowID id,
197 bool *accept, wxString *res, wxTreeCtrl *owner,
198 const wxString &value, const wxPoint &pos, const wxSize &size,
199 int style, const wxValidator& validator, const wxString &name ) :
200 wxTextCtrl( parent, id, value, pos, size, style, validator, name )
201 {
202 m_res = res;
203 m_accept = accept;
204 m_owner = owner;
205 (*m_accept) = FALSE;
206 (*m_res) = "";
207 m_startValue = value;
208 }
209
210 void wxTreeTextCtrl::OnChar( wxKeyEvent &event )
211 {
212 if (event.m_keyCode == WXK_RETURN)
213 {
214 (*m_accept) = TRUE;
215 (*m_res) = GetValue();
216 m_owner->SetFocus();
217 return;
218 }
219 if (event.m_keyCode == WXK_ESCAPE)
220 {
221 (*m_accept) = FALSE;
222 (*m_res) = "";
223 m_owner->SetFocus();
224 return;
225 }
226 event.Skip();
227 }
228
229 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
230 {
231 if (wxPendingDelete.Member(this)) return;
232
233 wxPendingDelete.Append(this);
234
235 if ((*m_accept) && ((*m_res) != m_startValue))
236 m_owner->OnRenameAccept();
237 }
238
239 #define PIXELS_PER_UNIT 10
240 // -----------------------------------------------------------------------------
241 // wxTreeEvent
242 // -----------------------------------------------------------------------------
243
244 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent, wxNotifyEvent)
245
246 wxTreeEvent::wxTreeEvent( wxEventType commandType, int id )
247 : wxNotifyEvent( commandType, id )
248 {
249 m_code = 0;
250 m_itemOld = (wxGenericTreeItem *)NULL;
251 }
252
253 // -----------------------------------------------------------------------------
254 // wxGenericTreeItem
255 // -----------------------------------------------------------------------------
256
257 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem *parent,
258 const wxString& text,
259 wxDC& dc,
260 int image, int selImage,
261 wxTreeItemData *data)
262 : m_text(text)
263 {
264 m_images[wxTreeItemIcon_Normal] = image;
265 m_images[wxTreeItemIcon_Selected] = selImage;
266 m_images[wxTreeItemIcon_Expanded] = NO_IMAGE;
267 m_images[wxTreeItemIcon_SelectedExpanded] = NO_IMAGE;
268
269 m_data = data;
270 m_x = m_y = 0;
271 m_xCross = m_yCross = 0;
272
273 m_level = 0;
274
275 m_isCollapsed = TRUE;
276 m_hasHilight = FALSE;
277 m_hasPlus = FALSE;
278 m_isBold = FALSE;
279
280 m_parent = parent;
281
282 dc.GetTextExtent( m_text, &m_width, &m_height );
283 // TODO : Add the width of the image
284 // PB : We don't know which image is shown (image, selImage)
285 // We don't even know imageList from the treectrl this item belongs to !!!
286 // At this point m_width doesn't mean much, this can be remove !
287 }
288
289 wxGenericTreeItem::~wxGenericTreeItem()
290 {
291 delete m_data;
292
293 wxASSERT_MSG( m_children.IsEmpty(),
294 wxT("please call DeleteChildren() before deleting the item") );
295 }
296
297 void wxGenericTreeItem::DeleteChildren(wxTreeCtrl *tree)
298 {
299 size_t count = m_children.Count();
300 for ( size_t n = 0; n < count; n++ )
301 {
302 wxGenericTreeItem *child = m_children[n];
303 if ( tree )
304 {
305 tree->SendDeleteEvent(child);
306 }
307
308 child->DeleteChildren(tree);
309 delete child;
310 }
311
312 m_children.Empty();
313 }
314
315 void wxGenericTreeItem::SetText( const wxString &text )
316 {
317 m_text = text;
318 }
319
320 void wxGenericTreeItem::Reset()
321 {
322 m_text.Empty();
323 for ( int i = 0; i < wxTreeItemIcon_Max; i++ )
324 {
325 m_images[i] = NO_IMAGE;
326 }
327
328 m_data = NULL;
329 m_x = m_y =
330 m_height = m_width = 0;
331 m_xCross =
332 m_yCross = 0;
333
334 m_level = 0;
335
336 DeleteChildren();
337 m_isCollapsed = TRUE;
338
339 m_parent = (wxGenericTreeItem *)NULL;
340 }
341
342 size_t wxGenericTreeItem::GetChildrenCount(bool recursively) const
343 {
344 size_t count = m_children.Count();
345 if ( !recursively )
346 return count;
347
348 size_t total = count;
349 for ( size_t n = 0; n < count; ++n )
350 {
351 total += m_children[n]->GetChildrenCount();
352 }
353
354 return total;
355 }
356
357 void wxGenericTreeItem::SetCross( int x, int y )
358 {
359 m_xCross = x;
360 m_yCross = y;
361 }
362
363 void wxGenericTreeItem::GetSize( int &x, int &y, const wxTreeCtrl *theTree )
364 {
365 int bottomY=m_y+theTree->GetLineHeight(this);
366 if ( y < bottomY ) y = bottomY;
367 int width = m_x + m_width;
368 if ( x < width ) x = width;
369
370 if (IsExpanded())
371 {
372 size_t count = m_children.Count();
373 for ( size_t n = 0; n < count; ++n )
374 {
375 m_children[n]->GetSize( x, y, theTree );
376 }
377 }
378 }
379
380 wxGenericTreeItem *wxGenericTreeItem::HitTest( const wxPoint& point,
381 const wxTreeCtrl *theTree,
382 int &flags)
383 {
384 if ((point.y > m_y) && (point.y < m_y + theTree->GetLineHeight(this)))
385 {
386 if (point.y<m_y+theTree->GetLineHeight(this)/2)
387 flags |= wxTREE_HITTEST_ONITEMUPPERPART;
388 else
389 flags |= wxTREE_HITTEST_ONITEMLOWERPART;
390
391 // 5 is the size of the plus sign
392 if ((point.x > m_xCross-5) && (point.x < m_xCross+5) &&
393 (point.y > m_yCross-5) && (point.y < m_yCross+5) &&
394 (IsExpanded() || HasPlus()))
395 {
396 flags|=wxTREE_HITTEST_ONITEMBUTTON;
397 return this;
398 }
399
400 if ((point.x >= m_x) && (point.x <= m_x+m_width))
401 {
402 int image_w = -1;
403 int image_h;
404
405 // assuming every image (normal and selected ) has the same size !
406 if ( (GetImage() != NO_IMAGE) && theTree->m_imageListNormal )
407 theTree->m_imageListNormal->GetSize(GetImage(), image_w, image_h);
408
409 if ((image_w != -1) && (point.x <= m_x + image_w + 1))
410 flags |= wxTREE_HITTEST_ONITEMICON;
411 else
412 flags |= wxTREE_HITTEST_ONITEMLABEL;
413
414 return this;
415 }
416
417 if (point.x < m_x)
418 flags |= wxTREE_HITTEST_ONITEMIDENT;
419 if (point.x > m_x+m_width)
420 flags |= wxTREE_HITTEST_ONITEMRIGHT;
421
422 return this;
423 }
424 else
425 {
426 if (!m_isCollapsed)
427 {
428 size_t count = m_children.Count();
429 for ( size_t n = 0; n < count; n++ )
430 {
431 wxGenericTreeItem *res = m_children[n]->HitTest( point, theTree, flags );
432 if ( res != NULL )
433 return res;
434 }
435 }
436 }
437
438 flags|=wxTREE_HITTEST_NOWHERE;
439 return NULL;
440 }
441
442 int wxGenericTreeItem::GetCurrentImage() const
443 {
444 int image = NO_IMAGE;
445 if ( IsExpanded() )
446 {
447 if ( IsSelected() )
448 {
449 image = GetImage(wxTreeItemIcon_SelectedExpanded);
450 }
451
452 if ( image == NO_IMAGE )
453 {
454 // we usually fall back to the normal item, but try just the
455 // expanded one (and not selected) first in this case
456 image = GetImage(wxTreeItemIcon_Expanded);
457 }
458 }
459 else // not expanded
460 {
461 if ( IsSelected() )
462 image = GetImage(wxTreeItemIcon_Selected);
463 }
464
465 // may be it doesn't have the specific image we want, try the default one
466 // instead
467 if ( image == NO_IMAGE )
468 {
469 image = GetImage();
470 }
471
472 return image;
473 }
474
475 // -----------------------------------------------------------------------------
476 // wxTreeCtrl implementation
477 // -----------------------------------------------------------------------------
478
479 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxScrolledWindow)
480
481 BEGIN_EVENT_TABLE(wxTreeCtrl,wxScrolledWindow)
482 EVT_PAINT (wxTreeCtrl::OnPaint)
483 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse)
484 EVT_CHAR (wxTreeCtrl::OnChar)
485 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus)
486 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus)
487 EVT_IDLE (wxTreeCtrl::OnIdle)
488 END_EVENT_TABLE()
489
490 // -----------------------------------------------------------------------------
491 // construction/destruction
492 // -----------------------------------------------------------------------------
493
494 void wxTreeCtrl::Init()
495 {
496 m_current =
497 m_key_current =
498 m_anchor = (wxGenericTreeItem *) NULL;
499 m_hasFocus = FALSE;
500 m_dirty = FALSE;
501
502 m_xScroll = 0;
503 m_yScroll = 0;
504 m_lineHeight = 10;
505 m_indent = 15;
506 m_spacing = 18;
507
508 m_hilightBrush = new wxBrush
509 (
510 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT),
511 wxSOLID
512 );
513
514 m_imageListNormal =
515 m_imageListState = (wxImageList *) NULL;
516
517 m_dragCount = 0;
518
519 m_renameTimer = new wxTreeRenameTimer( this );
520 }
521
522 bool wxTreeCtrl::Create(wxWindow *parent, wxWindowID id,
523 const wxPoint& pos, const wxSize& size,
524 long style,
525 const wxValidator &validator,
526 const wxString& name )
527 {
528 Init();
529
530 wxScrolledWindow::Create( parent, id, pos, size, style|wxHSCROLL|wxVSCROLL, name );
531
532 #if wxUSE_VALIDATORS
533 SetValidator( validator );
534 #endif
535
536 SetBackgroundColour( *wxWHITE );
537 // m_dottedPen = wxPen( "grey", 0, wxDOT );
538 m_dottedPen = wxPen( "grey", 0, 0 );
539
540 return TRUE;
541 }
542
543 wxTreeCtrl::~wxTreeCtrl()
544 {
545 wxDELETE( m_hilightBrush );
546
547 DeleteAllItems();
548
549 delete m_renameTimer;
550 }
551
552 // -----------------------------------------------------------------------------
553 // accessors
554 // -----------------------------------------------------------------------------
555
556 size_t wxTreeCtrl::GetCount() const
557 {
558 return m_anchor == NULL ? 0u : m_anchor->GetChildrenCount();
559 }
560
561 void wxTreeCtrl::SetIndent(unsigned int indent)
562 {
563 m_indent = indent;
564 m_dirty = TRUE;
565 Refresh();
566 }
567
568 void wxTreeCtrl::SetSpacing(unsigned int spacing)
569 {
570 m_spacing = spacing;
571 m_dirty = TRUE;
572 Refresh();
573 }
574
575 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item, bool recursively)
576 {
577 wxCHECK_MSG( item.IsOk(), 0u, wxT("invalid tree item") );
578
579 return item.m_pItem->GetChildrenCount(recursively);
580 }
581
582 // -----------------------------------------------------------------------------
583 // functions to work with tree items
584 // -----------------------------------------------------------------------------
585
586 wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const
587 {
588 wxCHECK_MSG( item.IsOk(), wxT(""), wxT("invalid tree item") );
589
590 return item.m_pItem->GetText();
591 }
592
593 int wxTreeCtrl::GetItemImage(const wxTreeItemId& item,
594 wxTreeItemIcon which) const
595 {
596 wxCHECK_MSG( item.IsOk(), -1, wxT("invalid tree item") );
597
598 return item.m_pItem->GetImage(which);
599 }
600
601 wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const
602 {
603 wxCHECK_MSG( item.IsOk(), NULL, wxT("invalid tree item") );
604
605 return item.m_pItem->GetData();
606 }
607
608 void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text)
609 {
610 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
611
612 wxClientDC dc(this);
613 wxGenericTreeItem *pItem = item.m_pItem;
614 pItem->SetText(text);
615 CalculateSize(pItem, dc);
616 RefreshLine(pItem);
617 }
618
619 void wxTreeCtrl::SetItemImage(const wxTreeItemId& item,
620 int image,
621 wxTreeItemIcon which)
622 {
623 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
624
625 wxGenericTreeItem *pItem = item.m_pItem;
626 pItem->SetImage(image, which);
627
628 wxClientDC dc(this);
629 CalculateSize(pItem, dc);
630 RefreshLine(pItem);
631 }
632
633 void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
634 {
635 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
636
637 item.m_pItem->SetData(data);
638 }
639
640 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
641 {
642 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
643
644 wxGenericTreeItem *pItem = item.m_pItem;
645 pItem->SetHasPlus(has);
646 RefreshLine(pItem);
647 }
648
649 void wxTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
650 {
651 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
652
653 // avoid redrawing the tree if no real change
654 wxGenericTreeItem *pItem = item.m_pItem;
655 if ( pItem->IsBold() != bold )
656 {
657 pItem->SetBold(bold);
658 RefreshLine(pItem);
659 }
660 }
661
662 // -----------------------------------------------------------------------------
663 // item status inquiries
664 // -----------------------------------------------------------------------------
665
666 bool wxTreeCtrl::IsVisible(const wxTreeItemId& WXUNUSED(item)) const
667 {
668 wxFAIL_MSG(wxT("not implemented"));
669
670 return TRUE;
671 }
672
673 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
674 {
675 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
676
677 return !item.m_pItem->GetChildren().IsEmpty();
678 }
679
680 bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const
681 {
682 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
683
684 return item.m_pItem->IsExpanded();
685 }
686
687 bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const
688 {
689 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
690
691 return item.m_pItem->IsSelected();
692 }
693
694 bool wxTreeCtrl::IsBold(const wxTreeItemId& item) const
695 {
696 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
697
698 return item.m_pItem->IsBold();
699 }
700
701 // -----------------------------------------------------------------------------
702 // navigation
703 // -----------------------------------------------------------------------------
704
705 wxTreeItemId wxTreeCtrl::GetParent(const wxTreeItemId& item) const
706 {
707 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
708
709 return item.m_pItem->GetParent();
710 }
711
712 wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item, long& cookie) const
713 {
714 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
715
716 cookie = 0;
717 return GetNextChild(item, cookie);
718 }
719
720 wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& item, long& cookie) const
721 {
722 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
723
724 wxArrayGenericTreeItems& children = item.m_pItem->GetChildren();
725 if ( (size_t)cookie < children.Count() )
726 {
727 return children.Item(cookie++);
728 }
729 else
730 {
731 // there are no more of them
732 return wxTreeItemId();
733 }
734 }
735
736 wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const
737 {
738 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
739
740 wxArrayGenericTreeItems& children = item.m_pItem->GetChildren();
741 return (children.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children.Last()));
742 }
743
744 wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
745 {
746 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
747
748 wxGenericTreeItem *i = item.m_pItem;
749 wxGenericTreeItem *parent = i->GetParent();
750 if ( parent == NULL )
751 {
752 // root item doesn't have any siblings
753 return wxTreeItemId();
754 }
755
756 wxArrayGenericTreeItems& siblings = parent->GetChildren();
757 int index = siblings.Index(i);
758 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
759
760 size_t n = (size_t)(index + 1);
761 return n == siblings.Count() ? wxTreeItemId() : wxTreeItemId(siblings[n]);
762 }
763
764 wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
765 {
766 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
767
768 wxGenericTreeItem *i = item.m_pItem;
769 wxGenericTreeItem *parent = i->GetParent();
770 if ( parent == NULL )
771 {
772 // root item doesn't have any siblings
773 return wxTreeItemId();
774 }
775
776 wxArrayGenericTreeItems& siblings = parent->GetChildren();
777 int index = siblings.Index(i);
778 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
779
780 return index == 0 ? wxTreeItemId()
781 : wxTreeItemId(siblings[(size_t)(index - 1)]);
782 }
783
784 wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const
785 {
786 wxFAIL_MSG(wxT("not implemented"));
787
788 return wxTreeItemId();
789 }
790
791 wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
792 {
793 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
794
795 wxFAIL_MSG(wxT("not implemented"));
796
797 return wxTreeItemId();
798 }
799
800 wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
801 {
802 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
803
804 wxFAIL_MSG(wxT("not implemented"));
805
806 return wxTreeItemId();
807 }
808
809 // -----------------------------------------------------------------------------
810 // operations
811 // -----------------------------------------------------------------------------
812
813 wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parentId,
814 size_t previous,
815 const wxString& text,
816 int image, int selImage,
817 wxTreeItemData *data)
818 {
819 wxGenericTreeItem *parent = parentId.m_pItem;
820 if ( !parent )
821 {
822 // should we give a warning here?
823 return AddRoot(text, image, selImage, data);
824 }
825
826 wxClientDC dc(this);
827 wxGenericTreeItem *item = new wxGenericTreeItem(parent,
828 text, dc,
829 image, selImage,
830 data);
831
832 if ( data != NULL )
833 {
834 data->m_pItem = item;
835 }
836
837 parent->Insert( item, previous );
838
839 m_dirty = TRUE;
840
841 return item;
842 }
843
844 wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text,
845 int image, int selImage,
846 wxTreeItemData *data)
847 {
848 wxCHECK_MSG( !m_anchor, wxTreeItemId(), wxT("tree can have only one root") );
849
850 wxClientDC dc(this);
851 m_anchor = new wxGenericTreeItem((wxGenericTreeItem *)NULL, text, dc,
852 image, selImage, data);
853 if ( data != NULL )
854 {
855 data->m_pItem = m_anchor;
856 }
857
858 Refresh();
859 AdjustMyScrollbars();
860
861 return m_anchor;
862 }
863
864 wxTreeItemId wxTreeCtrl::PrependItem(const wxTreeItemId& parent,
865 const wxString& text,
866 int image, int selImage,
867 wxTreeItemData *data)
868 {
869 return DoInsertItem(parent, 0u, text, image, selImage, data);
870 }
871
872 wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parentId,
873 const wxTreeItemId& idPrevious,
874 const wxString& text,
875 int image, int selImage,
876 wxTreeItemData *data)
877 {
878 wxGenericTreeItem *parent = parentId.m_pItem;
879 if ( !parent )
880 {
881 // should we give a warning here?
882 return AddRoot(text, image, selImage, data);
883 }
884
885 int index = parent->GetChildren().Index(idPrevious.m_pItem);
886 wxASSERT_MSG( index != wxNOT_FOUND,
887 wxT("previous item in wxTreeCtrl::InsertItem() is not a sibling") );
888 return DoInsertItem(parentId, (size_t)++index, text, image, selImage, data);
889 }
890
891 wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parentId,
892 const wxString& text,
893 int image, int selImage,
894 wxTreeItemData *data)
895 {
896 wxGenericTreeItem *parent = parentId.m_pItem;
897 if ( !parent )
898 {
899 // should we give a warning here?
900 return AddRoot(text, image, selImage, data);
901 }
902
903 return DoInsertItem(parent, parent->GetChildren().Count(), text,
904 image, selImage, data);
905 }
906
907 void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem *item)
908 {
909 wxTreeEvent event( wxEVT_COMMAND_TREE_DELETE_ITEM, GetId() );
910 event.m_item = item;
911 event.SetEventObject( this );
912 ProcessEvent( event );
913 }
914
915 void wxTreeCtrl::DeleteChildren(const wxTreeItemId& itemId)
916 {
917 wxGenericTreeItem *item = itemId.m_pItem;
918 item->DeleteChildren(this);
919
920 m_dirty = TRUE;
921 }
922
923 void wxTreeCtrl::Delete(const wxTreeItemId& itemId)
924 {
925 wxGenericTreeItem *item = itemId.m_pItem;
926 wxGenericTreeItem *parent = item->GetParent();
927
928 if ( parent )
929 {
930 parent->GetChildren().Remove(item);
931 }
932
933 item->DeleteChildren(this);
934 SendDeleteEvent(item);
935 delete item;
936
937 m_dirty = TRUE;
938 }
939
940 void wxTreeCtrl::DeleteAllItems()
941 {
942 if ( m_anchor )
943 {
944 m_anchor->DeleteChildren(this);
945 delete m_anchor;
946
947 m_anchor = NULL;
948
949 m_dirty = TRUE;
950 }
951 }
952
953 void wxTreeCtrl::Expand(const wxTreeItemId& itemId)
954 {
955 wxGenericTreeItem *item = itemId.m_pItem;
956
957 if ( !item->HasPlus() )
958 return;
959
960 if ( item->IsExpanded() )
961 return;
962
963 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_EXPANDING, GetId() );
964 event.m_item = item;
965 event.SetEventObject( this );
966
967 // if ( ProcessEvent( event ) && event.m_code ) TODO: Was this a typo ?
968 if ( ProcessEvent( event ) && !event.IsAllowed() )
969 {
970 // cancelled by program
971 return;
972 }
973
974 item->Expand();
975 CalculatePositions();
976
977 RefreshSubtree(item);
978
979 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED);
980 ProcessEvent( event );
981 }
982
983 void wxTreeCtrl::Collapse(const wxTreeItemId& itemId)
984 {
985 wxGenericTreeItem *item = itemId.m_pItem;
986
987 if ( !item->IsExpanded() )
988 return;
989
990 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING, GetId() );
991 event.m_item = item;
992 event.SetEventObject( this );
993 if ( ProcessEvent( event ) && !event.IsAllowed() )
994 {
995 // cancelled by program
996 return;
997 }
998
999 item->Collapse();
1000
1001 wxArrayGenericTreeItems& children = item->GetChildren();
1002 size_t count = children.Count();
1003 for ( size_t n = 0; n < count; n++ )
1004 {
1005 Collapse(children[n]);
1006 }
1007
1008 CalculatePositions();
1009
1010 RefreshSubtree(item);
1011
1012 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED);
1013 ProcessEvent( event );
1014 }
1015
1016 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
1017 {
1018 Collapse(item);
1019 DeleteChildren(item);
1020 }
1021
1022 void wxTreeCtrl::Toggle(const wxTreeItemId& itemId)
1023 {
1024 wxGenericTreeItem *item = itemId.m_pItem;
1025
1026 if ( item->IsExpanded() )
1027 Collapse(itemId);
1028 else
1029 Expand(itemId);
1030 }
1031
1032 void wxTreeCtrl::Unselect()
1033 {
1034 if ( m_current )
1035 {
1036 m_current->SetHilight( FALSE );
1037 RefreshLine( m_current );
1038 }
1039 }
1040
1041 void wxTreeCtrl::UnselectAllChildren(wxGenericTreeItem *item)
1042 {
1043 item->SetHilight(FALSE);
1044 RefreshLine(item);
1045
1046 if (item->HasChildren())
1047 {
1048 wxArrayGenericTreeItems& children = item->GetChildren();
1049 size_t count = children.Count();
1050 for ( size_t n = 0; n < count; ++n )
1051 UnselectAllChildren(children[n]);
1052 }
1053 }
1054
1055 void wxTreeCtrl::UnselectAll()
1056 {
1057 UnselectAllChildren(GetRootItem().m_pItem);
1058 }
1059
1060 // Recursive function !
1061 // To stop we must have crt_item<last_item
1062 // Algorithm :
1063 // Tag all next children, when no more children,
1064 // Move to parent (not to tag)
1065 // Keep going... if we found last_item, we stop.
1066 bool wxTreeCtrl::TagNextChildren(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select)
1067 {
1068 wxGenericTreeItem *parent = crt_item->GetParent();
1069
1070 if ( parent == NULL ) // This is root item
1071 return TagAllChildrenUntilLast(crt_item, last_item, select);
1072
1073 wxArrayGenericTreeItems& children = parent->GetChildren();
1074 int index = children.Index(crt_item);
1075 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
1076
1077 size_t count = children.Count();
1078 for (size_t n=(size_t)(index+1); n<count; ++n)
1079 if (TagAllChildrenUntilLast(children[n], last_item, select)) return TRUE;
1080
1081 return TagNextChildren(parent, last_item, select);
1082 }
1083
1084 bool wxTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select)
1085 {
1086 crt_item->SetHilight(select);
1087 RefreshLine(crt_item);
1088
1089 if (crt_item==last_item) return TRUE;
1090
1091 if (crt_item->HasChildren())
1092 {
1093 wxArrayGenericTreeItems& children = crt_item->GetChildren();
1094 size_t count = children.Count();
1095 for ( size_t n = 0; n < count; ++n )
1096 if (TagAllChildrenUntilLast(children[n], last_item, select)) return TRUE;
1097 }
1098
1099 return FALSE;
1100 }
1101
1102 void wxTreeCtrl::SelectItemRange(wxGenericTreeItem *item1, wxGenericTreeItem *item2)
1103 {
1104 // item2 is not necessary after item1
1105 wxGenericTreeItem *first=NULL, *last=NULL;
1106
1107 // choice first' and 'last' between item1 and item2
1108 if (item1->GetY()<item2->GetY())
1109 {
1110 first=item1;
1111 last=item2;
1112 }
1113 else
1114 {
1115 first=item2;
1116 last=item1;
1117 }
1118
1119 bool select = m_current->IsSelected();
1120
1121 if ( TagAllChildrenUntilLast(first,last,select) )
1122 return;
1123
1124 TagNextChildren(first,last,select);
1125 }
1126
1127 void wxTreeCtrl::SelectItem(const wxTreeItemId& itemId,
1128 bool unselect_others,
1129 bool extended_select)
1130 {
1131 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
1132
1133 bool is_single=!(GetWindowStyleFlag() & wxTR_MULTIPLE);
1134 wxGenericTreeItem *item = itemId.m_pItem;
1135
1136 //wxCHECK_RET( ( (!unselect_others) && is_single),
1137 // wxT("this is a single selection tree") );
1138
1139 // to keep going anyhow !!!
1140 if (is_single)
1141 {
1142 if (item->IsSelected())
1143 return; // nothing to do
1144 unselect_others = TRUE;
1145 extended_select = FALSE;
1146 }
1147 else if ( unselect_others && item->IsSelected() )
1148 {
1149 // selection change if there is more than one item currently selected
1150 wxArrayTreeItemIds selected_items;
1151 if ( GetSelections(selected_items) == 1 )
1152 return;
1153 }
1154
1155 wxTreeEvent event( wxEVT_COMMAND_TREE_SEL_CHANGING, GetId() );
1156 event.m_item = item;
1157 event.m_itemOld = m_current;
1158 event.SetEventObject( this );
1159 // TODO : Here we don't send any selection mode yet !
1160
1161 if ( GetEventHandler()->ProcessEvent( event ) && !event.IsAllowed() )
1162 return;
1163
1164 // ctrl press
1165 if (unselect_others)
1166 {
1167 if (is_single) Unselect(); // to speed up thing
1168 else UnselectAll();
1169 }
1170
1171 // shift press
1172 if (extended_select)
1173 {
1174 if (m_current == NULL) m_current=m_key_current=GetRootItem().m_pItem;
1175 // don't change the mark (m_current)
1176 SelectItemRange(m_current, item);
1177 }
1178 else
1179 {
1180 bool select=TRUE; // the default
1181
1182 // Check if we need to toggle hilight (ctrl mode)
1183 if (!unselect_others)
1184 select=!item->IsSelected();
1185
1186 m_current = m_key_current = item;
1187 m_current->SetHilight(select);
1188 RefreshLine( m_current );
1189 }
1190
1191 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
1192 GetEventHandler()->ProcessEvent( event );
1193 }
1194
1195 void wxTreeCtrl::FillArray(wxGenericTreeItem *item,
1196 wxArrayTreeItemIds &array) const
1197 {
1198 if ( item->IsSelected() )
1199 array.Add(wxTreeItemId(item));
1200
1201 if ( item->HasChildren() )
1202 {
1203 wxArrayGenericTreeItems& children = item->GetChildren();
1204 size_t count = children.GetCount();
1205 for ( size_t n = 0; n < count; ++n )
1206 FillArray(children[n],array);
1207 }
1208 }
1209
1210 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds &array) const
1211 {
1212 array.Empty();
1213 FillArray(GetRootItem().m_pItem, array);
1214
1215 return array.Count();
1216 }
1217
1218 void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item)
1219 {
1220 if (!item.IsOk()) return;
1221
1222 wxGenericTreeItem *gitem = item.m_pItem;
1223
1224 // first expand all parent branches
1225 wxGenericTreeItem *parent = gitem->GetParent();
1226 while ( parent )
1227 {
1228 Expand(parent);
1229 parent = parent->GetParent();
1230 }
1231
1232 //if (parent) CalculatePositions();
1233
1234 ScrollTo(item);
1235 }
1236
1237 void wxTreeCtrl::ScrollTo(const wxTreeItemId &item)
1238 {
1239 if (!item.IsOk()) return;
1240
1241 // We have to call this here because the label in
1242 // question might just have been added and no screen
1243 // update taken place.
1244 if (m_dirty) wxYield();
1245
1246 wxGenericTreeItem *gitem = item.m_pItem;
1247
1248 // now scroll to the item
1249 int item_y = gitem->GetY();
1250
1251 int start_x = 0;
1252 int start_y = 0;
1253 ViewStart( &start_x, &start_y );
1254 start_y *= PIXELS_PER_UNIT;
1255
1256 int client_h = 0;
1257 int client_w = 0;
1258 GetClientSize( &client_w, &client_h );
1259
1260 if (item_y < start_y+3)
1261 {
1262 // going down
1263 int x = 0;
1264 int y = 0;
1265 m_anchor->GetSize( x, y, this );
1266 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1267 int x_pos = GetScrollPos( wxHORIZONTAL );
1268 // Item should appear at top
1269 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, item_y/PIXELS_PER_UNIT );
1270 }
1271 else if (item_y+GetLineHeight(gitem) > start_y+client_h)
1272 {
1273 // going up
1274 int x = 0;
1275 int y = 0;
1276 m_anchor->GetSize( x, y, this );
1277 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1278 item_y += PIXELS_PER_UNIT+2;
1279 int x_pos = GetScrollPos( wxHORIZONTAL );
1280 // Item should appear at bottom
1281 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 );
1282 }
1283 }
1284
1285 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1286 static wxTreeCtrl *s_treeBeingSorted = NULL;
1287
1288 static int tree_ctrl_compare_func(wxGenericTreeItem **item1,
1289 wxGenericTreeItem **item2)
1290 {
1291 wxCHECK_MSG( s_treeBeingSorted, 0, wxT("bug in wxTreeCtrl::SortChildren()") );
1292
1293 return s_treeBeingSorted->OnCompareItems(*item1, *item2);
1294 }
1295
1296 int wxTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
1297 const wxTreeItemId& item2)
1298 {
1299 return wxStrcmp(GetItemText(item1), GetItemText(item2));
1300 }
1301
1302 void wxTreeCtrl::SortChildren(const wxTreeItemId& itemId)
1303 {
1304 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
1305
1306 wxGenericTreeItem *item = itemId.m_pItem;
1307
1308 wxCHECK_RET( !s_treeBeingSorted,
1309 wxT("wxTreeCtrl::SortChildren is not reentrant") );
1310
1311 wxArrayGenericTreeItems& children = item->GetChildren();
1312 if ( children.Count() > 1 )
1313 {
1314 s_treeBeingSorted = this;
1315 children.Sort(tree_ctrl_compare_func);
1316 s_treeBeingSorted = NULL;
1317
1318 m_dirty = TRUE;
1319 }
1320 //else: don't make the tree dirty as nothing changed
1321 }
1322
1323 wxImageList *wxTreeCtrl::GetImageList() const
1324 {
1325 return m_imageListNormal;
1326 }
1327
1328 wxImageList *wxTreeCtrl::GetStateImageList() const
1329 {
1330 return m_imageListState;
1331 }
1332
1333 void wxTreeCtrl::SetImageList(wxImageList *imageList)
1334 {
1335 m_imageListNormal = imageList;
1336
1337 // Calculate a m_lineHeight value from the image sizes.
1338 // May be toggle off. Then wxTreeCtrl will spread when
1339 // necessary (which might look ugly).
1340 #if 1
1341 wxClientDC dc(this);
1342 m_lineHeight = (int)(dc.GetCharHeight() + 4);
1343 int
1344 width = 0,
1345 height = 0,
1346 n = m_imageListNormal->GetImageCount();
1347 for(int i = 0; i < n ; i++)
1348 {
1349 m_imageListNormal->GetSize(i, width, height);
1350 if(height > m_lineHeight) m_lineHeight = height;
1351 }
1352
1353 if (m_lineHeight<40) m_lineHeight+=4; // at least 4 pixels (odd such that a line can be drawn in between)
1354 else m_lineHeight+=m_lineHeight/10; // otherwise 10% extra spacing
1355
1356 #endif
1357 }
1358
1359 void wxTreeCtrl::SetStateImageList(wxImageList *imageList)
1360 {
1361 m_imageListState = imageList;
1362 }
1363
1364 // -----------------------------------------------------------------------------
1365 // helpers
1366 // -----------------------------------------------------------------------------
1367
1368 void wxTreeCtrl::AdjustMyScrollbars()
1369 {
1370 if (m_anchor)
1371 {
1372 int x = 0;
1373 int y = 0;
1374 m_anchor->GetSize( x, y, this );
1375 //y += GetLineHeight(m_anchor);
1376 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1377 int x_pos = GetScrollPos( wxHORIZONTAL );
1378 int y_pos = GetScrollPos( wxVERTICAL );
1379 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, y_pos );
1380 }
1381 else
1382 {
1383 SetScrollbars( 0, 0, 0, 0 );
1384 }
1385 }
1386
1387 int wxTreeCtrl::GetLineHeight(wxGenericTreeItem *item) const
1388 {
1389 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT)
1390 return item->GetHeight();
1391 else
1392 return m_lineHeight;
1393 }
1394
1395 void wxTreeCtrl::PaintItem(wxGenericTreeItem *item, wxDC& dc)
1396 {
1397 // render bold items in bold
1398 wxFont fontOld;
1399 wxFont fontNew;
1400
1401 if (item->IsBold())
1402 {
1403 fontOld = dc.GetFont();
1404 if (fontOld.Ok())
1405 {
1406 // VZ: is there any better way to make a bold variant of old font?
1407 fontNew = wxFont( fontOld.GetPointSize(),
1408 fontOld.GetFamily(),
1409 fontOld.GetStyle(),
1410 wxBOLD,
1411 fontOld.GetUnderlined());
1412 dc.SetFont(fontNew);
1413 }
1414 else
1415 {
1416 wxFAIL_MSG(wxT("wxDC::GetFont() failed!"));
1417 }
1418 }
1419
1420 long text_w = 0;
1421 long text_h = 0;
1422 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
1423
1424 int image_h = 0;
1425 int image_w = 0;
1426 int image = item->GetCurrentImage();
1427 if ( image != NO_IMAGE )
1428 {
1429 m_imageListNormal->GetSize( image, image_w, image_h );
1430 image_w += 4;
1431 }
1432
1433 int total_h = GetLineHeight(item);
1434
1435 dc.DrawRectangle( item->GetX()-2, item->GetY(), item->GetWidth()+2, total_h );
1436
1437 if ( image != NO_IMAGE )
1438 {
1439 dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, total_h );
1440 m_imageListNormal->Draw( image, dc,
1441 item->GetX(),
1442 item->GetY() +((total_h > image_h)?((total_h-image_h)/2):0),
1443 wxIMAGELIST_DRAW_TRANSPARENT );
1444 dc.DestroyClippingRegion();
1445 }
1446
1447 dc.SetBackgroundMode(wxTRANSPARENT);
1448 dc.DrawText( item->GetText(), image_w + item->GetX(), item->GetY()
1449 + ((total_h > text_h) ? (total_h - text_h)/2 : 0));
1450
1451 // restore normal font for bold items
1452 if (fontOld.Ok())
1453 {
1454 dc.SetFont( fontOld);
1455 }
1456 }
1457
1458 // Now y stands for the top of the item, whereas it used to stand for middle !
1459 void wxTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
1460 {
1461 int horizX = level*m_indent;
1462
1463 item->SetX( horizX+m_indent+m_spacing );
1464 item->SetY( y );
1465
1466 int oldY = y;
1467 y+=GetLineHeight(item)/2;
1468
1469 item->SetCross( horizX+m_indent, y );
1470
1471 int exposed_x = dc.LogicalToDeviceX( 0 );
1472 int exposed_y = dc.LogicalToDeviceY( item->GetY() );
1473
1474 if (IsExposed( exposed_x, exposed_y, 10000, GetLineHeight(item) )) // 10000 = very much
1475 {
1476 int startX = horizX;
1477 int endX = horizX + (m_indent-5);
1478
1479 // if (!item->HasChildren()) endX += (m_indent+5);
1480 if (!item->HasChildren()) endX += 20;
1481
1482 dc.DrawLine( startX, y, endX, y );
1483
1484 if (item->HasPlus())
1485 {
1486 dc.DrawLine( horizX+(m_indent+5), y, horizX+(m_indent+15), y );
1487 dc.SetPen( *wxGREY_PEN );
1488 dc.SetBrush( *wxWHITE_BRUSH );
1489 dc.DrawRectangle( horizX+(m_indent-5), y-4, 11, 9 );
1490
1491 dc.SetPen( *wxBLACK_PEN );
1492 dc.DrawLine( horizX+(m_indent-2), y, horizX+(m_indent+3), y );
1493 if (!item->IsExpanded())
1494 dc.DrawLine( horizX+m_indent, y-2, horizX+m_indent, y+3 );
1495
1496 dc.SetPen( m_dottedPen );
1497 }
1498
1499 if (item->IsSelected())
1500 {
1501 dc.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT ) );
1502
1503 dc.SetBrush( *m_hilightBrush );
1504
1505 if (m_hasFocus)
1506 dc.SetPen( *wxBLACK_PEN );
1507 else
1508 dc.SetPen( *wxTRANSPARENT_PEN );
1509
1510 PaintItem(item, dc);
1511
1512 dc.SetPen( m_dottedPen );
1513 dc.SetTextForeground( *wxBLACK );
1514 dc.SetBrush( *wxWHITE_BRUSH );
1515 }
1516 else
1517 {
1518 dc.SetBrush( *wxWHITE_BRUSH );
1519 dc.SetPen( *wxTRANSPARENT_PEN );
1520
1521 PaintItem(item, dc);
1522
1523 dc.SetPen( m_dottedPen );
1524 }
1525 }
1526
1527 y = oldY+GetLineHeight(item);
1528
1529 if (item->IsExpanded())
1530 {
1531 oldY+=GetLineHeight(item)/2;
1532 int semiOldY=y; // (=y) for stupid compilator
1533
1534 wxArrayGenericTreeItems& children = item->GetChildren();
1535 size_t n, count = children.Count();
1536 for ( n = 0; n < count; ++n )
1537 {
1538 semiOldY=y;
1539 PaintLevel( children[n], dc, level+1, y );
1540 }
1541
1542 // it may happen that the item is expanded but has no items (when you
1543 // delete all its children for example) - don't draw the vertical line
1544 // in this case
1545 if (count > 0)
1546 {
1547 semiOldY+=GetLineHeight(children[--n])/2;
1548 dc.DrawLine( horizX+m_indent, oldY+5, horizX+m_indent, semiOldY );
1549 }
1550 }
1551 }
1552
1553 void wxTreeCtrl::DrawBorder(wxTreeItemId &item)
1554 {
1555 if (!item) return;
1556
1557 wxGenericTreeItem *i=item.m_pItem;
1558
1559 wxClientDC dc(this);
1560 PrepareDC( dc );
1561 dc.SetLogicalFunction(wxINVERT);
1562
1563 int w,h,x;
1564 ViewStart(&x,&h); // we only need x
1565 GetClientSize(&w,&h); // we only need w
1566
1567 h=GetLineHeight(i)+1;
1568 // 2 white column at border
1569 dc.DrawRectangle( PIXELS_PER_UNIT*x+2, i->GetY()-1, w-6, h);
1570 }
1571
1572 void wxTreeCtrl::DrawLine(wxTreeItemId &item, bool below)
1573 {
1574 if (!item) return;
1575
1576 wxGenericTreeItem *i=item.m_pItem;
1577
1578 wxClientDC dc(this);
1579 PrepareDC( dc );
1580 dc.SetLogicalFunction(wxINVERT);
1581
1582 int w,h,y;
1583 GetSize(&w,&h);
1584
1585 if (below) y=i->GetY()+GetLineHeight(i)-1;
1586 else y=i->GetY();
1587
1588 dc.DrawLine( 0, y, w, y);
1589 }
1590
1591 // -----------------------------------------------------------------------------
1592 // wxWindows callbacks
1593 // -----------------------------------------------------------------------------
1594
1595 void wxTreeCtrl::OnPaint( wxPaintEvent &WXUNUSED(event) )
1596 {
1597 if ( !m_anchor)
1598 return;
1599
1600 wxPaintDC dc(this);
1601 PrepareDC( dc );
1602
1603 dc.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT ) );
1604
1605 dc.SetPen( m_dottedPen );
1606 //if(GetImageList() == NULL)
1607 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
1608
1609 int y = 2;
1610 PaintLevel( m_anchor, dc, 0, y );
1611 }
1612
1613 void wxTreeCtrl::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
1614 {
1615 m_hasFocus = TRUE;
1616
1617 if (m_current) RefreshLine( m_current );
1618 }
1619
1620 void wxTreeCtrl::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
1621 {
1622 m_hasFocus = FALSE;
1623
1624 if (m_current) RefreshLine( m_current );
1625 }
1626
1627 void wxTreeCtrl::OnChar( wxKeyEvent &event )
1628 {
1629 wxTreeEvent te( wxEVT_COMMAND_TREE_KEY_DOWN, GetId() );
1630 te.m_code = event.KeyCode();
1631 te.SetEventObject( this );
1632 GetEventHandler()->ProcessEvent( te );
1633
1634 if ( (m_current == 0) || (m_key_current == 0) )
1635 {
1636 event.Skip();
1637 return;
1638 }
1639
1640 bool is_multiple=(GetWindowStyleFlag() & wxTR_MULTIPLE);
1641 bool extended_select=(event.ShiftDown() && is_multiple);
1642 bool unselect_others=!(extended_select || (event.ControlDown() && is_multiple));
1643
1644 switch (event.KeyCode())
1645 {
1646 case '+':
1647 case WXK_ADD:
1648 if (m_current->HasPlus() && !IsExpanded(m_current))
1649 {
1650 Expand(m_current);
1651 }
1652 break;
1653
1654 case '-':
1655 case WXK_SUBTRACT:
1656 if (IsExpanded(m_current))
1657 {
1658 Collapse(m_current);
1659 }
1660 break;
1661
1662 case '*':
1663 case WXK_MULTIPLY:
1664 Toggle(m_current);
1665 break;
1666
1667 case ' ':
1668 case WXK_RETURN:
1669 {
1670 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
1671 event.m_item = m_current;
1672 event.m_code = 0;
1673 event.SetEventObject( this );
1674 GetEventHandler()->ProcessEvent( event );
1675 }
1676 break;
1677
1678 // up goes to the previous sibling or to the last of its children if
1679 // it's expanded
1680 case WXK_UP:
1681 {
1682 wxTreeItemId prev = GetPrevSibling( m_key_current );
1683 if (!prev)
1684 {
1685 prev = GetParent( m_key_current );
1686 if (prev)
1687 {
1688 long cockie = 0;
1689 wxTreeItemId current = m_key_current;
1690 if (current == GetFirstChild( prev, cockie ))
1691 {
1692 // otherwise we return to where we came from
1693 SelectItem( prev, unselect_others, extended_select );
1694 m_key_current=prev.m_pItem;
1695 EnsureVisible( prev );
1696 break;
1697 }
1698 }
1699 }
1700 if (prev)
1701 {
1702 while ( IsExpanded(prev) && HasChildren(prev) )
1703 {
1704 wxTreeItemId child = GetLastChild(prev);
1705 if ( child )
1706 {
1707 prev = child;
1708 }
1709 }
1710
1711 SelectItem( prev, unselect_others, extended_select );
1712 m_key_current=prev.m_pItem;
1713 EnsureVisible( prev );
1714 }
1715 }
1716 break;
1717
1718 // left arrow goes to the parent
1719 case WXK_LEFT:
1720 {
1721 wxTreeItemId prev = GetParent( m_current );
1722 if (prev)
1723 {
1724 EnsureVisible( prev );
1725 SelectItem( prev, unselect_others, extended_select );
1726 }
1727 }
1728 break;
1729
1730 case WXK_RIGHT:
1731 // this works the same as the down arrow except that we also expand the
1732 // item if it wasn't expanded yet
1733 Expand(m_current);
1734 // fall through
1735
1736 case WXK_DOWN:
1737 {
1738 if (IsExpanded(m_key_current) && HasChildren(m_key_current))
1739 {
1740 long cookie = 0;
1741 wxTreeItemId child = GetFirstChild( m_key_current, cookie );
1742 SelectItem( child, unselect_others, extended_select );
1743 m_key_current=child.m_pItem;
1744 EnsureVisible( child );
1745 }
1746 else
1747 {
1748 wxTreeItemId next = GetNextSibling( m_key_current );
1749 // if (next == 0)
1750 if (!next)
1751 {
1752 wxTreeItemId current = m_key_current;
1753 while (current && !next)
1754 {
1755 current = GetParent( current );
1756 if (current) next = GetNextSibling( current );
1757 }
1758 }
1759 // if (next != 0)
1760 if (next)
1761 {
1762 SelectItem( next, unselect_others, extended_select );
1763 m_key_current=next.m_pItem;
1764 EnsureVisible( next );
1765 }
1766 }
1767 }
1768 break;
1769
1770 // <End> selects the last visible tree item
1771 case WXK_END:
1772 {
1773 wxTreeItemId last = GetRootItem();
1774
1775 while ( last.IsOk() && IsExpanded(last) )
1776 {
1777 wxTreeItemId lastChild = GetLastChild(last);
1778
1779 // it may happen if the item was expanded but then all of
1780 // its children have been deleted - so IsExpanded() returned
1781 // TRUE, but GetLastChild() returned invalid item
1782 if ( !lastChild )
1783 break;
1784
1785 last = lastChild;
1786 }
1787
1788 if ( last.IsOk() )
1789 {
1790 EnsureVisible( last );
1791 SelectItem( last, unselect_others, extended_select );
1792 }
1793 }
1794 break;
1795
1796 // <Home> selects the root item
1797 case WXK_HOME:
1798 {
1799 wxTreeItemId prev = GetRootItem();
1800 if (prev)
1801 {
1802 EnsureVisible( prev );
1803 SelectItem( prev, unselect_others, extended_select );
1804 }
1805 }
1806 break;
1807
1808 default:
1809 event.Skip();
1810 }
1811 }
1812
1813 wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& flags)
1814 {
1815 // We have to call this here because the label in
1816 // question might just have been added and no screen
1817 // update taken place.
1818 if (m_dirty) wxYield();
1819
1820 wxClientDC dc(this);
1821 PrepareDC(dc);
1822 long x = dc.DeviceToLogicalX( (long)point.x );
1823 long y = dc.DeviceToLogicalY( (long)point.y );
1824 int w, h;
1825 GetSize(&w, &h);
1826
1827 flags=0;
1828 if (point.x<0) flags|=wxTREE_HITTEST_TOLEFT;
1829 if (point.x>w) flags|=wxTREE_HITTEST_TORIGHT;
1830 if (point.y<0) flags|=wxTREE_HITTEST_ABOVE;
1831 if (point.y>h) flags|=wxTREE_HITTEST_BELOW;
1832
1833 return m_anchor->HitTest( wxPoint(x, y), this, flags);
1834 }
1835
1836 /* **** */
1837
1838 void wxTreeCtrl::Edit( const wxTreeItemId& item )
1839 {
1840 if (!item.IsOk()) return;
1841
1842 m_currentEdit = item.m_pItem;
1843
1844 wxTreeEvent te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, GetId() );
1845 te.m_item = m_currentEdit;
1846 te.SetEventObject( this );
1847 GetEventHandler()->ProcessEvent( te );
1848
1849 if (!te.IsAllowed()) return;
1850
1851 // We have to call this here because the label in
1852 // question might just have been added and no screen
1853 // update taken place.
1854 if (m_dirty) wxYield();
1855
1856 wxString s = m_currentEdit->GetText();
1857 int x = m_currentEdit->GetX();
1858 int y = m_currentEdit->GetY();
1859 int w = m_currentEdit->GetWidth();
1860 int h = m_currentEdit->GetHeight();
1861
1862 int image_h = 0;
1863 int image_w = 0;
1864
1865 int image = m_currentEdit->GetCurrentImage();
1866 if ( image != NO_IMAGE )
1867 {
1868 m_imageListNormal->GetSize( image, image_w, image_h );
1869 image_w += 4;
1870 }
1871 x += image_w;
1872 w -= image_w + 4; // I don't know why +4 is needed
1873
1874 wxClientDC dc(this);
1875 PrepareDC( dc );
1876 x = dc.LogicalToDeviceX( x );
1877 y = dc.LogicalToDeviceY( y );
1878
1879 wxTreeTextCtrl *text = new wxTreeTextCtrl(
1880 this, -1, &m_renameAccept, &m_renameRes, this, s, wxPoint(x-4,y-4), wxSize(w+11,h+8) );
1881 text->SetFocus();
1882 }
1883
1884 void wxTreeCtrl::OnRenameTimer()
1885 {
1886 Edit( m_current );
1887 }
1888
1889 void wxTreeCtrl::OnRenameAccept()
1890 {
1891 wxTreeEvent le( wxEVT_COMMAND_TREE_END_LABEL_EDIT, GetId() );
1892 le.m_item = m_currentEdit;
1893 le.SetEventObject( this );
1894 le.m_label = m_renameRes;
1895 GetEventHandler()->ProcessEvent( le );
1896
1897 if (!le.IsAllowed()) return;
1898
1899 SetItemText( m_currentEdit, m_renameRes );
1900 }
1901
1902 void wxTreeCtrl::OnMouse( wxMouseEvent &event )
1903 {
1904 if ( !(event.LeftUp() || event.RightDown() || event.LeftDClick() || event.Dragging()) ) return;
1905
1906 if ( !m_anchor ) return;
1907
1908 wxClientDC dc(this);
1909 PrepareDC(dc);
1910 long x = dc.DeviceToLogicalX( (long)event.GetX() );
1911 long y = dc.DeviceToLogicalY( (long)event.GetY() );
1912
1913 int flags=0;
1914 wxGenericTreeItem *item = m_anchor->HitTest( wxPoint(x,y), this, flags);
1915 bool onButton = flags & wxTREE_HITTEST_ONITEMBUTTON;
1916
1917 if (event.Dragging())
1918 {
1919 if (m_dragCount == 0)
1920 m_dragStart = wxPoint(x,y);
1921
1922 m_dragCount++;
1923
1924 if (m_dragCount != 3) return;
1925
1926 int command = wxEVT_COMMAND_TREE_BEGIN_DRAG;
1927 if (event.RightIsDown()) command = wxEVT_COMMAND_TREE_BEGIN_RDRAG;
1928
1929 wxTreeEvent nevent( command, GetId() );
1930 nevent.m_item = m_current;
1931 nevent.SetEventObject(this);
1932 GetEventHandler()->ProcessEvent(nevent);
1933 return;
1934 }
1935 else
1936 {
1937 m_dragCount = 0;
1938 }
1939
1940 if (item == NULL) return; /* we hit the blank area */
1941
1942 if (event.RightDown()) {
1943 wxTreeEvent nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK,GetId());
1944 nevent.m_item=item;
1945 nevent.m_code=0;
1946 nevent.SetEventObject(this);
1947 GetEventHandler()->ProcessEvent(nevent);
1948 return;
1949 }
1950
1951 if (event.LeftUp() && (item == m_current) &&
1952 (flags & wxTREE_HITTEST_ONITEMLABEL) &&
1953 HasFlag(wxTR_EDIT_LABELS) )
1954 {
1955 m_renameTimer->Start( 100, TRUE );
1956 return;
1957 }
1958
1959 bool is_multiple=(GetWindowStyleFlag() & wxTR_MULTIPLE);
1960 bool extended_select=(event.ShiftDown() && is_multiple);
1961 bool unselect_others=!(extended_select || (event.ControlDown() && is_multiple));
1962
1963 if (onButton)
1964 {
1965 Toggle( item );
1966 if (is_multiple)
1967 return;
1968 }
1969
1970 SelectItem(item, unselect_others, extended_select);
1971
1972 if (event.LeftDClick())
1973 {
1974 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
1975 event.m_item = item;
1976 event.m_code = 0;
1977 event.SetEventObject( this );
1978 GetEventHandler()->ProcessEvent( event );
1979 }
1980 }
1981
1982 void wxTreeCtrl::OnIdle( wxIdleEvent &WXUNUSED(event) )
1983 {
1984 /* after all changes have been done to the tree control,
1985 * we actually redraw the tree when everything is over */
1986
1987 if (!m_dirty)
1988 return;
1989
1990 m_dirty = FALSE;
1991
1992 CalculatePositions();
1993 Refresh();
1994 AdjustMyScrollbars();
1995 }
1996
1997 void wxTreeCtrl::CalculateSize( wxGenericTreeItem *item, wxDC &dc )
1998 {
1999 long text_w = 0;
2000 long text_h = 0;
2001
2002 wxFont fontOld;
2003 wxFont fontNew;
2004 if (item->IsBold())
2005 {
2006 fontOld = dc.GetFont();
2007 if (fontOld.Ok())
2008 {
2009 // VZ: is there any better way to make a bold variant of old font?
2010 fontNew = wxFont( fontOld.GetPointSize(),
2011 fontOld.GetFamily(),
2012 fontOld.GetStyle(),
2013 wxBOLD,
2014 fontOld.GetUnderlined());
2015 dc.SetFont(fontNew);
2016 }
2017 else
2018 {
2019 wxFAIL_MSG(wxT("wxDC::GetFont() failed!"));
2020 }
2021 }
2022
2023 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
2024 text_h+=2;
2025
2026 // restore normal font for bold items
2027 if (fontOld.Ok())
2028 dc.SetFont( fontOld);
2029
2030 int image_h = 0;
2031 int image_w = 0;
2032 int image = item->GetCurrentImage();
2033 if ( image != NO_IMAGE )
2034 {
2035 m_imageListNormal->GetSize( image, image_w, image_h );
2036 image_w += 4;
2037 }
2038
2039 int total_h = (image_h > text_h) ? image_h : text_h;
2040
2041 if (total_h<40) total_h+=4; // at least 4 pixels
2042 else total_h+=total_h/10; // otherwise 10% extra spacing
2043
2044 item->SetHeight(total_h);
2045 if (total_h>m_lineHeight) m_lineHeight=total_h;
2046
2047 item->SetWidth(image_w+text_w+2);
2048 }
2049
2050 // -----------------------------------------------------------------------------
2051 // for developper : y is now the top of the level
2052 // not the middle of it !
2053 void wxTreeCtrl::CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
2054 {
2055 int horizX = level*m_indent;
2056
2057 CalculateSize( item, dc );
2058
2059 // set its position
2060 item->SetX( horizX+m_indent+m_spacing );
2061 item->SetY( y );
2062 y+=GetLineHeight(item);
2063
2064 if ( !item->IsExpanded() )
2065 {
2066 // we dont need to calculate collapsed branches
2067 return;
2068 }
2069
2070 wxArrayGenericTreeItems& children = item->GetChildren();
2071 size_t n, count = children.Count();
2072 for (n = 0; n < count; ++n )
2073 CalculateLevel( children[n], dc, level+1, y ); // recurse
2074 }
2075
2076 void wxTreeCtrl::CalculatePositions()
2077 {
2078 if ( !m_anchor ) return;
2079
2080 wxClientDC dc(this);
2081 PrepareDC( dc );
2082
2083 dc.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT ) );
2084
2085 dc.SetPen( m_dottedPen );
2086 //if(GetImageList() == NULL)
2087 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2088
2089 int y = 2;
2090 CalculateLevel( m_anchor, dc, 0, y ); // start recursion
2091 }
2092
2093 void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem *item)
2094 {
2095 wxClientDC dc(this);
2096 PrepareDC(dc);
2097
2098 int cw = 0;
2099 int ch = 0;
2100 GetClientSize( &cw, &ch );
2101
2102 wxRect rect;
2103 rect.x = dc.LogicalToDeviceX( 0 );
2104 rect.width = cw;
2105 rect.y = dc.LogicalToDeviceY( item->GetY() );
2106 rect.height = ch;
2107
2108 Refresh( TRUE, &rect );
2109
2110 AdjustMyScrollbars();
2111 }
2112
2113 void wxTreeCtrl::RefreshLine( wxGenericTreeItem *item )
2114 {
2115 wxClientDC dc(this);
2116 PrepareDC( dc );
2117
2118 int cw = 0;
2119 int ch = 0;
2120 GetClientSize( &cw, &ch );
2121
2122 wxRect rect;
2123 rect.x = dc.LogicalToDeviceX( 0 );
2124 rect.y = dc.LogicalToDeviceY( item->GetY() );
2125 rect.width = cw;
2126 rect.height = GetLineHeight(item); //dc.GetCharHeight() + 6;
2127
2128 Refresh( TRUE, &rect );
2129 }
2130