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