Added wxTreebook:
[wxWidgets.git] / src / common / bookctrl.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: common/bookctrl.cpp
3 // Purpose: wxBookCtrlBase implementation
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 19.08.03
7 // RCS-ID: $Id$
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
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_BOOKCTRL
28
29 #include "wx/imaglist.h"
30
31 #include "wx/bookctrl.h"
32
33 // ============================================================================
34 // implementation
35 // ============================================================================
36
37 // ----------------------------------------------------------------------------
38 // constructors and destructors
39 // ----------------------------------------------------------------------------
40
41 void wxBookCtrlBase::Init()
42 {
43 m_imageList = NULL;
44 m_ownsImageList = false;
45 }
46
47 bool
48 wxBookCtrlBase::Create(wxWindow *parent,
49 wxWindowID id,
50 const wxPoint& pos,
51 const wxSize& size,
52 long style,
53 const wxString& name)
54 {
55 return wxControl::Create
56 (
57 parent,
58 id,
59 pos,
60 size,
61 style,
62 wxDefaultValidator,
63 name
64 );
65 }
66
67 wxBookCtrlBase::~wxBookCtrlBase()
68 {
69 if ( m_ownsImageList )
70 {
71 // may be NULL, ok
72 delete m_imageList;
73 }
74 }
75
76 // ----------------------------------------------------------------------------
77 // image list
78 // ----------------------------------------------------------------------------
79
80 void wxBookCtrlBase::SetImageList(wxImageList *imageList)
81 {
82 if ( m_ownsImageList )
83 {
84 // may be NULL, ok
85 delete m_imageList;
86
87 m_ownsImageList = false;
88 }
89
90 m_imageList = imageList;
91 }
92
93 void wxBookCtrlBase::AssignImageList(wxImageList* imageList)
94 {
95 SetImageList(imageList);
96
97 m_ownsImageList = true;
98 }
99
100 // ----------------------------------------------------------------------------
101 // geometry
102 // ----------------------------------------------------------------------------
103
104 void wxBookCtrlBase::SetPageSize(const wxSize& size)
105 {
106 SetClientSize(CalcSizeFromPage(size));
107 }
108
109 wxSize wxBookCtrlBase::DoGetBestSize() const
110 {
111 wxSize bestSize;
112
113 // iterate over all pages, get the largest width and height
114 const size_t nCount = m_pages.size();
115 for ( size_t nPage = 0; nPage < nCount; nPage++ )
116 {
117 const wxWindow * const pPage = m_pages[nPage];
118 if( pPage )
119 {
120 wxSize childBestSize(pPage->GetBestSize());
121
122 if ( childBestSize.x > bestSize.x )
123 bestSize.x = childBestSize.x;
124
125 if ( childBestSize.y > bestSize.y )
126 bestSize.y = childBestSize.y;
127 }
128 }
129
130 // convert display area to window area, adding the size necessary for the
131 // tabs
132 wxSize best = CalcSizeFromPage(bestSize);
133 CacheBestSize(best);
134 return best;
135 }
136
137 // ----------------------------------------------------------------------------
138 // pages management
139 // ----------------------------------------------------------------------------
140
141 bool
142 wxBookCtrlBase::InsertPage(size_t nPage,
143 wxWindow *page,
144 const wxString& WXUNUSED(text),
145 bool WXUNUSED(bSelect),
146 int WXUNUSED(imageId))
147 {
148 wxCHECK_MSG( page || AllowNullPage(), false, _T("NULL page in wxBookCtrlBase::InsertPage()") );
149 wxCHECK_MSG( nPage <= m_pages.size(), false,
150 _T("invalid page index in wxBookCtrlBase::InsertPage()") );
151
152 m_pages.Insert(page, nPage);
153 InvalidateBestSize();
154
155 return true;
156 }
157
158 bool wxBookCtrlBase::DeletePage(size_t nPage)
159 {
160 wxWindow *page = DoRemovePage(nPage);
161 if ( !(page || AllowNullPage()) )
162 return false;
163
164 // delete NULL is harmless
165 delete page;
166
167 return true;
168 }
169
170 wxWindow *wxBookCtrlBase::DoRemovePage(size_t nPage)
171 {
172 wxCHECK_MSG( nPage < m_pages.size(), NULL,
173 _T("invalid page index in wxBookCtrlBase::DoRemovePage()") );
174
175 wxWindow *pageRemoved = m_pages[nPage];
176 m_pages.RemoveAt(nPage);
177 InvalidateBestSize();
178
179 return pageRemoved;
180 }
181
182 int wxBookCtrlBase::GetNextPage(bool forward) const
183 {
184 int nPage;
185
186 int nMax = GetPageCount();
187 if ( nMax-- ) // decrement it to get the last valid index
188 {
189 int nSel = GetSelection();
190
191 // change selection wrapping if it becomes invalid
192 nPage = forward ? nSel == nMax ? 0
193 : nSel + 1
194 : nSel == 0 ? nMax
195 : nSel - 1;
196 }
197 else // notebook is empty, no next page
198 {
199 nPage = -1;
200 }
201
202 return nPage;
203 }
204
205 #endif // wxUSE_BOOKCTRL
206