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