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