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