]> git.saurik.com Git - wxWidgets.git/blob - src/common/bookctrl.cpp
Include wx/dcprint.h according to precompiled headers of wx/wx.h (with other minor...
[wxWidgets.git] / src / common / bookctrl.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 // event table
39 // ----------------------------------------------------------------------------
40
41 IMPLEMENT_ABSTRACT_CLASS(wxBookCtrlBase, wxControl)
42
43 BEGIN_EVENT_TABLE(wxBookCtrlBase, wxControl)
44 EVT_SIZE(wxBookCtrlBase::OnSize)
45 END_EVENT_TABLE()
46
47 // ----------------------------------------------------------------------------
48 // constructors and destructors
49 // ----------------------------------------------------------------------------
50
51 void wxBookCtrlBase::Init()
52 {
53 m_bookctrl = NULL;
54 m_imageList = NULL;
55 m_ownsImageList = false;
56 m_fitToCurrentPage = false;
57
58 #if defined(__WXWINCE__)
59 m_internalBorder = 1;
60 #else
61 m_internalBorder = 5;
62 #endif
63
64 m_controlMargin = 0;
65 m_controlSizer = NULL;
66 }
67
68 bool
69 wxBookCtrlBase::Create(wxWindow *parent,
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
88 wxBookCtrlBase::~wxBookCtrlBase()
89 {
90 if ( m_ownsImageList )
91 {
92 // may be NULL, ok
93 delete m_imageList;
94 }
95 }
96
97 // ----------------------------------------------------------------------------
98 // image list
99 // ----------------------------------------------------------------------------
100
101 void wxBookCtrlBase::SetImageList(wxImageList *imageList)
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
114 void wxBookCtrlBase::AssignImageList(wxImageList* imageList)
115 {
116 SetImageList(imageList);
117
118 m_ownsImageList = true;
119 }
120
121 // ----------------------------------------------------------------------------
122 // geometry
123 // ----------------------------------------------------------------------------
124
125 void wxBookCtrlBase::SetPageSize(const wxSize& size)
126 {
127 SetClientSize(CalcSizeFromPage(size));
128 }
129
130 wxSize wxBookCtrlBase::DoGetBestSize() const
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 {
138 const wxWindow * const pPage = m_pages[nPage];
139 if( pPage )
140 {
141 wxSize childBestSize(pPage->GetBestSize());
142
143 if ( childBestSize.x > bestSize.x )
144 bestSize.x = childBestSize.x;
145
146 if ( childBestSize.y > bestSize.y )
147 bestSize.y = childBestSize.y;
148 }
149 }
150
151 if (m_fitToCurrentPage && GetCurrentPage())
152 bestSize = GetCurrentPage()->GetBestSize();
153
154 // convert display area to window area, adding the size necessary for the
155 // tabs
156 wxSize best = CalcSizeFromPage(bestSize);
157 CacheBestSize(best);
158 return best;
159 }
160
161 // ----------------------------------------------------------------------------
162 // pages management
163 // ----------------------------------------------------------------------------
164
165 bool
166 wxBookCtrlBase::InsertPage(size_t nPage,
167 wxWindow *page,
168 const wxString& WXUNUSED(text),
169 bool WXUNUSED(bSelect),
170 int WXUNUSED(imageId))
171 {
172 wxCHECK_MSG( page || AllowNullPage(), false,
173 _T("NULL page in wxBookCtrlBase::InsertPage()") );
174 wxCHECK_MSG( nPage <= m_pages.size(), false,
175 _T("invalid page index in wxBookCtrlBase::InsertPage()") );
176
177 m_pages.Insert(page, nPage);
178 if ( page )
179 page->SetSize(GetPageRect());
180
181 InvalidateBestSize();
182
183 return true;
184 }
185
186 bool wxBookCtrlBase::DeletePage(size_t nPage)
187 {
188 wxWindow *page = DoRemovePage(nPage);
189 if ( !(page || AllowNullPage()) )
190 return false;
191
192 // delete NULL is harmless
193 delete page;
194
195 return true;
196 }
197
198 wxWindow *wxBookCtrlBase::DoRemovePage(size_t nPage)
199 {
200 wxCHECK_MSG( nPage < m_pages.size(), NULL,
201 _T("invalid page index in wxBookCtrlBase::DoRemovePage()") );
202
203 wxWindow *pageRemoved = m_pages[nPage];
204 m_pages.RemoveAt(nPage);
205 InvalidateBestSize();
206
207 return pageRemoved;
208 }
209
210 int wxBookCtrlBase::GetNextPage(bool forward) const
211 {
212 int nPage;
213
214 int nMax = GetPageCount();
215 if ( nMax-- ) // decrement it to get the last valid index
216 {
217 int nSel = GetSelection();
218
219 // change selection wrapping if it becomes invalid
220 nPage = forward ? nSel == nMax ? 0
221 : nSel + 1
222 : nSel == 0 ? nMax
223 : nSel - 1;
224 }
225 else // notebook is empty, no next page
226 {
227 nPage = wxNOT_FOUND;
228 }
229
230 return nPage;
231 }
232
233 wxRect wxBookCtrlBase::GetPageRect() const
234 {
235 const wxSize size = GetControllerSize();
236
237 wxPoint pt;
238 wxRect rectPage(pt, GetClientSize());
239 switch ( GetWindowStyle() & wxBK_ALIGN_MASK )
240 {
241 default:
242 wxFAIL_MSG( _T("unexpected alignment") );
243 // fall through
244
245 case wxBK_TOP:
246 rectPage.y = size.y + GetInternalBorder();
247 // fall through
248
249 case wxBK_BOTTOM:
250 rectPage.height -= size.y + GetInternalBorder();
251 break;
252
253 case wxBK_LEFT:
254 rectPage.x = size.x + GetInternalBorder();
255 // fall through
256
257 case wxBK_RIGHT:
258 rectPage.width -= size.x + GetInternalBorder();
259 break;
260 }
261
262 return rectPage;
263 }
264
265 // Lay out controls
266 void wxBookCtrlBase::DoSize()
267 {
268 if ( !m_bookctrl )
269 {
270 // we're not fully created yet or OnSize() should be hidden by derived class
271 return;
272 }
273
274 if (GetSizer())
275 Layout();
276 else
277 {
278 // resize controller and the page area to fit inside our new size
279 const wxSize sizeClient( GetClientSize() ),
280 sizeBorder( m_bookctrl->GetSize() - m_bookctrl->GetClientSize() ),
281 sizeCtrl( GetControllerSize() );
282
283 m_bookctrl->SetClientSize( sizeCtrl.x - sizeBorder.x, sizeCtrl.y - sizeBorder.y );
284
285 const wxSize sizeNew = m_bookctrl->GetSize();
286 wxPoint posCtrl;
287 switch ( GetWindowStyle() & wxBK_ALIGN_MASK )
288 {
289 default:
290 wxFAIL_MSG( _T("unexpected alignment") );
291 // fall through
292
293 case wxBK_TOP:
294 case wxBK_LEFT:
295 // posCtrl is already ok
296 break;
297
298 case wxBK_BOTTOM:
299 posCtrl.y = sizeClient.y - sizeNew.y;
300 break;
301
302 case wxBK_RIGHT:
303 posCtrl.x = sizeClient.x - sizeNew.x;
304 break;
305 }
306
307 if ( m_bookctrl->GetPosition() != posCtrl )
308 m_bookctrl->Move(posCtrl);
309 }
310
311 // resize all pages to fit the new control size
312 const wxRect pageRect = GetPageRect();
313 const unsigned pagesCount = m_pages.Count();
314 for ( unsigned int i = 0; i < pagesCount; ++i )
315 {
316 wxWindow * const page = m_pages[i];
317 if ( !page )
318 {
319 wxASSERT_MSG( AllowNullPage(),
320 _T("Null page in a control that does not allow null pages?") );
321 continue;
322 }
323
324 page->SetSize(pageRect);
325 }
326 }
327
328 void wxBookCtrlBase::OnSize(wxSizeEvent& event)
329 {
330 event.Skip();
331
332 DoSize();
333 }
334
335 wxSize wxBookCtrlBase::GetControllerSize() const
336 {
337 if(!m_bookctrl)
338 return wxSize(0,0);
339
340 const wxSize sizeClient = GetClientSize(),
341 sizeBorder = m_bookctrl->GetSize() - m_bookctrl->GetClientSize(),
342 sizeCtrl = m_bookctrl->GetBestSize() + sizeBorder;
343
344 wxSize size;
345
346 if ( IsVertical() )
347 {
348 size.x = sizeClient.x;
349 size.y = sizeCtrl.y;
350 }
351 else // left/right aligned
352 {
353 size.x = sizeCtrl.x;
354 size.y = sizeClient.y;
355 }
356
357 return size;
358 }
359
360 #endif // wxUSE_BOOKCTRL