]> git.saurik.com Git - wxWidgets.git/blame - src/common/bookctrl.cpp
don't invalidate the tree size when pages are added/removed, we don't want the tree...
[wxWidgets.git] / src / common / bookctrl.cpp
CommitLineData
15aad3b9 1///////////////////////////////////////////////////////////////////////////////
2ddb4d13 2// Name: src/common/bookctrl.cpp
61c083e7 3// Purpose: wxBookCtrlBase implementation
15aad3b9
VZ
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>
65571936 9// Licence: wxWindows licence
15aad3b9
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
15aad3b9
VZ
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
2ddb4d13
WS
37// ----------------------------------------------------------------------------
38// event table
39// ----------------------------------------------------------------------------
40
41IMPLEMENT_ABSTRACT_CLASS(wxBookCtrlBase, wxControl)
42
43BEGIN_EVENT_TABLE(wxBookCtrlBase, wxControl)
44 EVT_SIZE(wxBookCtrlBase::OnSize)
45END_EVENT_TABLE()
46
15aad3b9
VZ
47// ----------------------------------------------------------------------------
48// constructors and destructors
49// ----------------------------------------------------------------------------
50
61c083e7 51void wxBookCtrlBase::Init()
15aad3b9 52{
2ddb4d13 53 m_bookctrl = NULL;
15aad3b9
VZ
54 m_imageList = NULL;
55 m_ownsImageList = false;
da817fa6 56 m_fitToCurrentPage = false;
159e6235
WS
57
58#if defined(__WXWINCE__)
59 m_internalBorder = 1;
60#else
61 m_internalBorder = 5;
62#endif
87cf52d8
JS
63
64 m_controlMargin = 0;
65 m_controlSizer = NULL;
15aad3b9
VZ
66}
67
68bool
61c083e7 69wxBookCtrlBase::Create(wxWindow *parent,
15aad3b9
VZ
70 wxWindowID id,
71 const wxPoint& pos,
72 const wxSize& size,
73 long style,
74 const wxString& name)
75{
76 return wxControl::Create
77 (
78 parent,
79 id,
80 pos,
81 size,
82 style,
83 wxDefaultValidator,
84 name
85 );
86}
87
61c083e7 88wxBookCtrlBase::~wxBookCtrlBase()
15aad3b9
VZ
89{
90 if ( m_ownsImageList )
91 {
92 // may be NULL, ok
93 delete m_imageList;
94 }
95}
96
97// ----------------------------------------------------------------------------
98// image list
99// ----------------------------------------------------------------------------
100
61c083e7 101void wxBookCtrlBase::SetImageList(wxImageList *imageList)
15aad3b9
VZ
102{
103 if ( m_ownsImageList )
104 {
105 // may be NULL, ok
106 delete m_imageList;
107
108 m_ownsImageList = false;
109 }
110
111 m_imageList = imageList;
112}
113
61c083e7 114void wxBookCtrlBase::AssignImageList(wxImageList* imageList)
15aad3b9
VZ
115{
116 SetImageList(imageList);
117
118 m_ownsImageList = true;
119}
120
121// ----------------------------------------------------------------------------
122// geometry
123// ----------------------------------------------------------------------------
124
61c083e7 125void wxBookCtrlBase::SetPageSize(const wxSize& size)
15aad3b9
VZ
126{
127 SetClientSize(CalcSizeFromPage(size));
128}
129
61c083e7 130wxSize wxBookCtrlBase::DoGetBestSize() const
15aad3b9
VZ
131{
132 wxSize bestSize;
133
134 // iterate over all pages, get the largest width and height
135 const size_t nCount = m_pages.size();
136 for ( size_t nPage = 0; nPage < nCount; nPage++ )
137 {
eca15c0d
VZ
138 const wxWindow * const pPage = m_pages[nPage];
139 if( pPage )
140 {
141 wxSize childBestSize(pPage->GetBestSize());
15aad3b9 142
eca15c0d
VZ
143 if ( childBestSize.x > bestSize.x )
144 bestSize.x = childBestSize.x;
15aad3b9 145
eca15c0d
VZ
146 if ( childBestSize.y > bestSize.y )
147 bestSize.y = childBestSize.y;
148 }
15aad3b9 149 }
93bfe545 150
da817fa6 151 if (m_fitToCurrentPage && GetCurrentPage())
93bfe545 152 bestSize = GetCurrentPage()->GetBestSize();
15aad3b9 153
3103e8a9 154 // convert display area to window area, adding the size necessary for the
15aad3b9 155 // tabs
9f884528
RD
156 wxSize best = CalcSizeFromPage(bestSize);
157 CacheBestSize(best);
158 return best;
15aad3b9
VZ
159}
160
161// ----------------------------------------------------------------------------
162// pages management
163// ----------------------------------------------------------------------------
164
165bool
61c083e7
WS
166wxBookCtrlBase::InsertPage(size_t nPage,
167 wxWindow *page,
168 const wxString& WXUNUSED(text),
169 bool WXUNUSED(bSelect),
170 int WXUNUSED(imageId))
15aad3b9 171{
2ddb4d13
WS
172 wxCHECK_MSG( page || AllowNullPage(), false,
173 _T("NULL page in wxBookCtrlBase::InsertPage()") );
712f40ca 174 wxCHECK_MSG( nPage <= m_pages.size(), false,
61c083e7 175 _T("invalid page index in wxBookCtrlBase::InsertPage()") );
15aad3b9
VZ
176
177 m_pages.Insert(page, nPage);
37144cf0 178 InvalidateBestSize();
c9d59ee7 179
15aad3b9
VZ
180 return true;
181}
182
61c083e7 183bool wxBookCtrlBase::DeletePage(size_t nPage)
15aad3b9
VZ
184{
185 wxWindow *page = DoRemovePage(nPage);
eca15c0d 186 if ( !(page || AllowNullPage()) )
15aad3b9
VZ
187 return false;
188
eca15c0d 189 // delete NULL is harmless
15aad3b9
VZ
190 delete page;
191
192 return true;
193}
194
61c083e7 195wxWindow *wxBookCtrlBase::DoRemovePage(size_t nPage)
15aad3b9
VZ
196{
197 wxCHECK_MSG( nPage < m_pages.size(), NULL,
61c083e7 198 _T("invalid page index in wxBookCtrlBase::DoRemovePage()") );
15aad3b9
VZ
199
200 wxWindow *pageRemoved = m_pages[nPage];
201 m_pages.RemoveAt(nPage);
37144cf0 202 InvalidateBestSize();
15aad3b9
VZ
203
204 return pageRemoved;
205}
206
61c083e7 207int wxBookCtrlBase::GetNextPage(bool forward) const
15aad3b9
VZ
208{
209 int nPage;
210
211 int nMax = GetPageCount();
212 if ( nMax-- ) // decrement it to get the last valid index
213 {
214 int nSel = GetSelection();
215
216 // change selection wrapping if it becomes invalid
217 nPage = forward ? nSel == nMax ? 0
218 : nSel + 1
219 : nSel == 0 ? nMax
220 : nSel - 1;
221 }
222 else // notebook is empty, no next page
223 {
159e6235 224 nPage = wxNOT_FOUND;
15aad3b9
VZ
225 }
226
227 return nPage;
228}
229
2ddb4d13
WS
230wxRect wxBookCtrlBase::GetPageRect() const
231{
232 const wxSize size = GetControllerSize();
233
234 wxPoint pt;
235 wxRect rectPage(pt, GetClientSize());
236 switch ( GetWindowStyle() & wxBK_ALIGN_MASK )
237 {
238 default:
239 wxFAIL_MSG( _T("unexpected alignment") );
240 // fall through
241
242 case wxBK_TOP:
243 rectPage.y = size.y + GetInternalBorder();
244 // fall through
245
246 case wxBK_BOTTOM:
247 rectPage.height -= size.y + GetInternalBorder();
248 break;
249
250 case wxBK_LEFT:
251 rectPage.x = size.x + GetInternalBorder();
252 // fall through
253
254 case wxBK_RIGHT:
255 rectPage.width -= size.x + GetInternalBorder();
256 break;
257 }
258
259 return rectPage;
260}
261
233387bd
JS
262// Lay out controls
263void wxBookCtrlBase::DoSize()
2ddb4d13 264{
2ddb4d13
WS
265 if ( !m_bookctrl )
266 {
267 // we're not fully created yet or OnSize() should be hidden by derived class
268 return;
269 }
87cf52d8
JS
270
271 if (GetSizer())
272 Layout();
273 else
2ddb4d13 274 {
87cf52d8
JS
275 // resize controller and the page area to fit inside our new size
276 const wxSize sizeClient( GetClientSize() ),
277 sizeBorder( m_bookctrl->GetSize() - m_bookctrl->GetClientSize() ),
278 sizeCtrl( GetControllerSize() );
2ddb4d13 279
87cf52d8 280 m_bookctrl->SetClientSize( sizeCtrl.x - sizeBorder.x, sizeCtrl.y - sizeBorder.y );
2ddb4d13 281
87cf52d8
JS
282 const wxSize sizeNew = m_bookctrl->GetSize();
283 wxPoint posCtrl;
284 switch ( GetWindowStyle() & wxBK_ALIGN_MASK )
285 {
286 default:
287 wxFAIL_MSG( _T("unexpected alignment") );
288 // fall through
289
290 case wxBK_TOP:
291 case wxBK_LEFT:
292 // posCtrl is already ok
293 break;
294
295 case wxBK_BOTTOM:
296 posCtrl.y = sizeClient.y - sizeNew.y;
297 break;
298
299 case wxBK_RIGHT:
300 posCtrl.x = sizeClient.x - sizeNew.x;
301 break;
302 }
2ddb4d13 303
87cf52d8
JS
304 if ( m_bookctrl->GetPosition() != posCtrl )
305 m_bookctrl->Move(posCtrl);
2ddb4d13
WS
306 }
307
2ddb4d13
WS
308 // resize the currently shown page
309 if (GetSelection() != wxNOT_FOUND )
310 {
311 wxWindow *page = m_pages[GetSelection()];
312 wxCHECK_RET( page, _T("NULL page?") );
313 page->SetSize(GetPageRect());
314 }
315}
316
233387bd
JS
317void wxBookCtrlBase::OnSize(wxSizeEvent& event)
318{
319 event.Skip();
320
321 DoSize();
322}
323
2ddb4d13
WS
324wxSize wxBookCtrlBase::GetControllerSize() const
325{
326 if(!m_bookctrl)
327 return wxSize(0,0);
328
329 const wxSize sizeClient = GetClientSize(),
330 sizeBorder = m_bookctrl->GetSize() - m_bookctrl->GetClientSize(),
331 sizeCtrl = m_bookctrl->GetBestSize() + sizeBorder;
332
333 wxSize size;
334
335 if ( IsVertical() )
336 {
337 size.x = sizeClient.x;
338 size.y = sizeCtrl.y;
339 }
340 else // left/right aligned
341 {
342 size.x = sizeCtrl.x;
343 size.y = sizeClient.y;
344 }
345
346 return size;
347}
348
15aad3b9 349#endif // wxUSE_BOOKCTRL