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