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