introduced a common base class for both MSW and generic wxTreeCtrl implementations
[wxWidgets.git] / src / common / treebase.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: treebase.cpp
3 // Purpose: Base wxTreeCtrl classes
4 // Author: Julian Smart
5 // Created: 01/02/97
6 // Modified:
7 // Id: $Id$
8 // Copyright: (c) 1998 Robert Roebling, Julian Smart et al
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // =============================================================================
13 // declarations
14 // =============================================================================
15
16 // -----------------------------------------------------------------------------
17 // headers
18 // -----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_TREECTRL
28
29 #include "wx/treectrl.h"
30
31 // ----------------------------------------------------------------------------
32 // events
33 // ----------------------------------------------------------------------------
34
35 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_BEGIN_DRAG)
36 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_BEGIN_RDRAG)
37 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT)
38 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_END_LABEL_EDIT)
39 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_DELETE_ITEM)
40 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_GET_INFO)
41 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_SET_INFO)
42 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_EXPANDED)
43 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_EXPANDING)
44 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_COLLAPSED)
45 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_COLLAPSING)
46 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_SEL_CHANGED)
47 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_SEL_CHANGING)
48 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_KEY_DOWN)
49 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_ACTIVATED)
50 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK)
51 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_MIDDLE_CLICK)
52 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_END_DRAG)
53 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK)
54 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP)
55 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_MENU)
56
57 // ----------------------------------------------------------------------------
58 // Tree event
59 // ----------------------------------------------------------------------------
60
61 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent, wxNotifyEvent)
62
63
64 wxTreeEvent::wxTreeEvent(wxEventType commandType, int id)
65 : wxNotifyEvent(commandType, id)
66 {
67 m_itemOld = 0l;
68 m_editCancelled = false;
69 }
70
71 wxTreeEvent::wxTreeEvent(const wxTreeEvent & event)
72 : wxNotifyEvent(event)
73 {
74 m_evtKey = event.m_evtKey;
75 m_item = event.m_item;
76 m_itemOld = event.m_itemOld;
77 m_pointDrag = event.m_pointDrag;
78 m_label = event.m_label;
79 m_editCancelled = event.m_editCancelled;
80 }
81
82 // ----------------------------------------------------------------------------
83 // wxTreeCtrlBase
84 // ----------------------------------------------------------------------------
85
86 wxTreeCtrlBase::~wxTreeCtrlBase()
87 {
88 if (m_ownsImageListNormal)
89 delete m_imageListNormal;
90 if (m_ownsImageListState)
91 delete m_imageListState;
92 }
93
94 wxSize wxTreeCtrlBase::DoGetBestSize() const
95 {
96 wxSize size;
97
98 // this doesn't really compute the total bounding rectangle of all items
99 // but a not too bad guess of it which has the advantage of not having to
100 // examine all (potentially hundreds or thousands) items in the control
101 for ( wxTreeItemId item = GetRootItem();
102 item.IsOk();
103 item = GetLastChild(item) )
104 {
105 wxRect rect;
106
107 // last parameter is "true" to get only the dimensions of the text
108 // label, we don't want to get the entire item width as it's determined
109 // by the current size
110 if ( GetBoundingRect(item, rect, true) )
111 {
112 if ( size.x < rect.x + rect.width )
113 size.x = rect.x + rect.width;
114 if ( size.y < rect.y + rect.height )
115 size.y = rect.y + rect.height;
116 }
117 }
118
119 // need some minimal size even for empty tree
120 if ( !size.x || !size.y )
121 size = wxControl::DoGetBestSize();
122
123 return size;
124 }
125
126 #endif // wxUSE_TREECTRL
127