]> git.saurik.com Git - wxWidgets.git/blame - src/common/bookctrl.cpp
remove run-time check for now-required GTK 2.4
[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)
a18c21f0
VZ
45#if wxUSE_HELP
46 EVT_HELP(wxID_ANY, wxBookCtrlBase::OnHelp)
47#endif // wxUSE_HELP
2ddb4d13
WS
48END_EVENT_TABLE()
49
15aad3b9
VZ
50// ----------------------------------------------------------------------------
51// constructors and destructors
52// ----------------------------------------------------------------------------
53
61c083e7 54void wxBookCtrlBase::Init()
15aad3b9 55{
681be2ef 56 m_selection = wxNOT_FOUND;
2ddb4d13 57 m_bookctrl = NULL;
da817fa6 58 m_fitToCurrentPage = false;
159e6235
WS
59
60#if defined(__WXWINCE__)
61 m_internalBorder = 1;
62#else
63 m_internalBorder = 5;
64#endif
87cf52d8
JS
65
66 m_controlMargin = 0;
67 m_controlSizer = NULL;
15aad3b9
VZ
68}
69
70bool
61c083e7 71wxBookCtrlBase::Create(wxWindow *parent,
15aad3b9
VZ
72 wxWindowID id,
73 const wxPoint& pos,
74 const wxSize& size,
75 long style,
76 const wxString& name)
77{
78 return wxControl::Create
79 (
80 parent,
81 id,
82 pos,
83 size,
84 style,
85 wxDefaultValidator,
86 name
87 );
88}
89
15aad3b9
VZ
90// ----------------------------------------------------------------------------
91// geometry
92// ----------------------------------------------------------------------------
93
e8a147a6
VZ
94void wxBookCtrlBase::DoInvalidateBestSize()
95{
96 // notice that it is not necessary to invalidate our own best size
97 // explicitly if we have m_bookctrl as it will already invalidate the best
98 // size of its parent when its own size is invalidated and its parent is
99 // this control
100 if ( m_bookctrl )
101 m_bookctrl->InvalidateBestSize();
102 else
103 wxControl::InvalidateBestSize();
104}
105
175363f6
VZ
106wxSize wxBookCtrlBase::CalcSizeFromPage(const wxSize& sizePage) const
107{
91452726
VZ
108 // We need to add the size of the controller and the border between if it's
109 // shown. Notice that we don't use GetControllerSize() here as it returns
110 // the actual size while we want the best size here.
111 if ( !m_bookctrl || !m_bookctrl->IsShown() )
112 return sizePage;
113
114 const wxSize sizeController = m_bookctrl->GetBestSize();
175363f6
VZ
115
116 wxSize size = sizePage;
117 if ( IsVertical() )
118 {
119 if ( sizeController.x > sizePage.x )
120 size.x = sizeController.x;
121 size.y += sizeController.y + GetInternalBorder();
122 }
123 else // left/right aligned
124 {
125 size.x += sizeController.x + GetInternalBorder();
126 if ( sizeController.y > sizePage.y )
127 size.y = sizeController.y;
128 }
129
130 return size;
131}
132
61c083e7 133void wxBookCtrlBase::SetPageSize(const wxSize& size)
15aad3b9
VZ
134{
135 SetClientSize(CalcSizeFromPage(size));
136}
137
61c083e7 138wxSize wxBookCtrlBase::DoGetBestSize() const
15aad3b9
VZ
139{
140 wxSize bestSize;
141
142 // iterate over all pages, get the largest width and height
143 const size_t nCount = m_pages.size();
144 for ( size_t nPage = 0; nPage < nCount; nPage++ )
145 {
eca15c0d
VZ
146 const wxWindow * const pPage = m_pages[nPage];
147 if( pPage )
148 {
149 wxSize childBestSize(pPage->GetBestSize());
15aad3b9 150
eca15c0d
VZ
151 if ( childBestSize.x > bestSize.x )
152 bestSize.x = childBestSize.x;
15aad3b9 153
eca15c0d
VZ
154 if ( childBestSize.y > bestSize.y )
155 bestSize.y = childBestSize.y;
156 }
15aad3b9 157 }
a717bd11 158
da817fa6 159 if (m_fitToCurrentPage && GetCurrentPage())
93bfe545 160 bestSize = GetCurrentPage()->GetBestSize();
15aad3b9 161
3103e8a9 162 // convert display area to window area, adding the size necessary for the
15aad3b9 163 // tabs
9f884528
RD
164 wxSize best = CalcSizeFromPage(bestSize);
165 CacheBestSize(best);
166 return best;
15aad3b9
VZ
167}
168
2ddb4d13
WS
169wxRect wxBookCtrlBase::GetPageRect() const
170{
171 const wxSize size = GetControllerSize();
172
173 wxPoint pt;
174 wxRect rectPage(pt, GetClientSize());
926395e1 175
90f9b8ef 176 switch ( GetWindowStyle() & wxBK_ALIGN_MASK )
2ddb4d13
WS
177 {
178 default:
9a83f860 179 wxFAIL_MSG( wxT("unexpected alignment") );
2ddb4d13
WS
180 // fall through
181
182 case wxBK_TOP:
183 rectPage.y = size.y + GetInternalBorder();
184 // fall through
185
186 case wxBK_BOTTOM:
187 rectPage.height -= size.y + GetInternalBorder();
6a9e44d5
PC
188 if (rectPage.height < 0)
189 rectPage.height = 0;
2ddb4d13
WS
190 break;
191
192 case wxBK_LEFT:
193 rectPage.x = size.x + GetInternalBorder();
194 // fall through
195
196 case wxBK_RIGHT:
197 rectPage.width -= size.x + GetInternalBorder();
6a9e44d5
PC
198 if (rectPage.width < 0)
199 rectPage.width = 0;
2ddb4d13
WS
200 break;
201 }
202
203 return rectPage;
204}
205
233387bd
JS
206// Lay out controls
207void wxBookCtrlBase::DoSize()
2ddb4d13 208{
2ddb4d13
WS
209 if ( !m_bookctrl )
210 {
211 // we're not fully created yet or OnSize() should be hidden by derived class
212 return;
213 }
a717bd11 214
87cf52d8
JS
215 if (GetSizer())
216 Layout();
217 else
2ddb4d13 218 {
87cf52d8
JS
219 // resize controller and the page area to fit inside our new size
220 const wxSize sizeClient( GetClientSize() ),
221 sizeBorder( m_bookctrl->GetSize() - m_bookctrl->GetClientSize() ),
222 sizeCtrl( GetControllerSize() );
2ddb4d13 223
87cf52d8 224 m_bookctrl->SetClientSize( sizeCtrl.x - sizeBorder.x, sizeCtrl.y - sizeBorder.y );
ecac22d0
SC
225 // if this changes the visibility of the scrollbars the best size changes, relayout in this case
226 wxSize sizeCtrl2 = GetControllerSize();
227 if ( sizeCtrl != sizeCtrl2 )
228 {
229 wxSize sizeBorder2 = m_bookctrl->GetSize() - m_bookctrl->GetClientSize();
230 m_bookctrl->SetClientSize( sizeCtrl2.x - sizeBorder2.x, sizeCtrl2.y - sizeBorder2.y );
231 }
2ddb4d13 232
87cf52d8
JS
233 const wxSize sizeNew = m_bookctrl->GetSize();
234 wxPoint posCtrl;
235 switch ( GetWindowStyle() & wxBK_ALIGN_MASK )
236 {
237 default:
9a83f860 238 wxFAIL_MSG( wxT("unexpected alignment") );
87cf52d8
JS
239 // fall through
240
241 case wxBK_TOP:
242 case wxBK_LEFT:
243 // posCtrl is already ok
244 break;
245
246 case wxBK_BOTTOM:
247 posCtrl.y = sizeClient.y - sizeNew.y;
248 break;
249
250 case wxBK_RIGHT:
251 posCtrl.x = sizeClient.x - sizeNew.x;
252 break;
253 }
2ddb4d13 254
87cf52d8
JS
255 if ( m_bookctrl->GetPosition() != posCtrl )
256 m_bookctrl->Move(posCtrl);
2ddb4d13
WS
257 }
258
a717bd11
VZ
259 // resize all pages to fit the new control size
260 const wxRect pageRect = GetPageRect();
b4a980f4 261 const unsigned pagesCount = m_pages.GetCount();
a717bd11 262 for ( unsigned int i = 0; i < pagesCount; ++i )
2ddb4d13 263 {
a717bd11
VZ
264 wxWindow * const page = m_pages[i];
265 if ( !page )
266 {
267 wxASSERT_MSG( AllowNullPage(),
9a83f860 268 wxT("Null page in a control that does not allow null pages?") );
a717bd11
VZ
269 continue;
270 }
271
272 page->SetSize(pageRect);
2ddb4d13
WS
273 }
274}
275
233387bd
JS
276void wxBookCtrlBase::OnSize(wxSizeEvent& event)
277{
278 event.Skip();
a717bd11 279
233387bd
JS
280 DoSize();
281}
282
2ddb4d13
WS
283wxSize wxBookCtrlBase::GetControllerSize() const
284{
448ca228
VZ
285 // For at least some book controls (e.g. wxChoicebook) it may make sense to
286 // (temporarily?) hide the controller and we shouldn't leave extra space
287 // for the hidden control in this case.
288 if ( !m_bookctrl || !m_bookctrl->IsShown() )
1d2b7f06 289 return wxSize(0, 0);
2ddb4d13
WS
290
291 const wxSize sizeClient = GetClientSize(),
1d2b7f06 292 sizeCtrl = m_bookctrl->GetBestSize();
2ddb4d13
WS
293
294 wxSize size;
295
296 if ( IsVertical() )
297 {
298 size.x = sizeClient.x;
299 size.y = sizeCtrl.y;
300 }
301 else // left/right aligned
302 {
303 size.x = sizeCtrl.x;
304 size.y = sizeClient.y;
305 }
306
307 return size;
308}
309
e8a147a6
VZ
310// ----------------------------------------------------------------------------
311// miscellaneous stuff
312// ----------------------------------------------------------------------------
313
314#if wxUSE_HELP
315
316void wxBookCtrlBase::OnHelp(wxHelpEvent& event)
317{
318 // determine where does this even originate from to avoid redirecting it
319 // back to the page which generated it (resulting in an infinite loop)
320
321 // notice that we have to check in the hard(er) way instead of just testing
322 // if the event object == this because the book control can have other
323 // subcontrols inside it (e.g. wxSpinButton in case of a notebook in wxUniv)
324 wxWindow *source = wxStaticCast(event.GetEventObject(), wxWindow);
325 while ( source && source != this && source->GetParent() != this )
326 {
327 source = source->GetParent();
328 }
329
330 if ( source && m_pages.Index(source) == wxNOT_FOUND )
331 {
332 // this event is for the book control itself, redirect it to the
333 // corresponding page
334 wxWindow *page = NULL;
335
336 if ( event.GetOrigin() == wxHelpEvent::Origin_HelpButton )
337 {
338 // show help for the page under the mouse
339 const int pagePos = HitTest(ScreenToClient(event.GetPosition()));
340
341 if ( pagePos != wxNOT_FOUND)
342 {
343 page = GetPage((size_t)pagePos);
344 }
345 }
346 else // event from keyboard or unknown source
347 {
348 // otherwise show the current page help
349 page = GetCurrentPage();
350 }
351
352 if ( page )
353 {
354 // change event object to the page to avoid infinite recursion if
355 // we get this event ourselves if the page doesn't handle it
356 event.SetEventObject(page);
357
358 if ( page->GetEventHandler()->ProcessEvent(event) )
359 {
360 // don't call event.Skip()
361 return;
362 }
363 }
364 }
365 //else: event coming from one of our pages already
366
367 event.Skip();
368}
369
370#endif // wxUSE_HELP
371
372// ----------------------------------------------------------------------------
373// pages management
374// ----------------------------------------------------------------------------
375
376bool
377wxBookCtrlBase::InsertPage(size_t nPage,
378 wxWindow *page,
379 const wxString& WXUNUSED(text),
380 bool WXUNUSED(bSelect),
381 int WXUNUSED(imageId))
382{
383 wxCHECK_MSG( page || AllowNullPage(), false,
9a83f860 384 wxT("NULL page in wxBookCtrlBase::InsertPage()") );
e8a147a6 385 wxCHECK_MSG( nPage <= m_pages.size(), false,
9a83f860 386 wxT("invalid page index in wxBookCtrlBase::InsertPage()") );
e8a147a6
VZ
387
388 m_pages.Insert(page, nPage);
389 if ( page )
390 page->SetSize(GetPageRect());
391
392 DoInvalidateBestSize();
393
394 return true;
395}
396
397bool wxBookCtrlBase::DeletePage(size_t nPage)
398{
399 wxWindow *page = DoRemovePage(nPage);
400 if ( !(page || AllowNullPage()) )
401 return false;
402
403 // delete NULL is harmless
404 delete page;
405
406 return true;
407}
408
409wxWindow *wxBookCtrlBase::DoRemovePage(size_t nPage)
410{
411 wxCHECK_MSG( nPage < m_pages.size(), NULL,
9a83f860 412 wxT("invalid page index in wxBookCtrlBase::DoRemovePage()") );
e8a147a6
VZ
413
414 wxWindow *pageRemoved = m_pages[nPage];
415 m_pages.RemoveAt(nPage);
416 DoInvalidateBestSize();
417
418 return pageRemoved;
419}
420
421int wxBookCtrlBase::GetNextPage(bool forward) const
422{
423 int nPage;
424
425 int nMax = GetPageCount();
426 if ( nMax-- ) // decrement it to get the last valid index
427 {
428 int nSel = GetSelection();
429
430 // change selection wrapping if it becomes invalid
431 nPage = forward ? nSel == nMax ? 0
432 : nSel + 1
433 : nSel == 0 ? nMax
434 : nSel - 1;
435 }
436 else // notebook is empty, no next page
437 {
438 nPage = wxNOT_FOUND;
439 }
440
441 return nPage;
442}
443
60d5c563
VZ
444bool wxBookCtrlBase::DoSetSelectionAfterInsertion(size_t n, bool bSelect)
445{
446 if ( bSelect )
447 SetSelection(n);
448 else if ( m_selection == wxNOT_FOUND )
449 ChangeSelection(0);
450 else // We're not going to select this page.
451 return false;
452
453 // Return true to indicate that we selected this page.
454 return true;
455}
456
deb325e3 457int wxBookCtrlBase::DoSetSelection(size_t n, int flags)
1d6fcbcc
VZ
458{
459 wxCHECK_MSG( n < GetPageCount(), wxNOT_FOUND,
460 wxT("invalid page index in wxBookCtrlBase::DoSetSelection()") );
461
462 const int oldSel = GetSelection();
463
51ad652f 464 if ( n != (size_t)oldSel )
1d6fcbcc 465 {
3e97a905 466 wxBookCtrlEvent *event = CreatePageChangingEvent();
1d6fcbcc
VZ
467 bool allowed = false;
468
469 if ( flags & SetSelection_SendEvent )
470 {
deb325e3
VZ
471 event->SetSelection(n);
472 event->SetOldSelection(oldSel);
473 event->SetEventObject(this);
1d6fcbcc 474
deb325e3 475 allowed = !GetEventHandler()->ProcessEvent(*event) || event->IsAllowed();
1d6fcbcc
VZ
476 }
477
478 if ( !(flags & SetSelection_SendEvent) || allowed)
479 {
480 if ( oldSel != wxNOT_FOUND )
481 m_pages[oldSel]->Hide();
482
483 wxWindow *page = m_pages[n];
484 page->SetSize(GetPageRect());
485 page->Show();
486
487 // change selection now to ignore the selection change event
488 UpdateSelectedPage(n);
489
490 if ( flags & SetSelection_SendEvent )
491 {
492 // program allows the page change
deb325e3
VZ
493 MakeChangedEvent(*event);
494 (void)GetEventHandler()->ProcessEvent(*event);
1d6fcbcc
VZ
495 }
496 }
deb325e3
VZ
497
498 delete event;
1d6fcbcc
VZ
499 }
500
501 return oldSel;
502}
503
3e97a905 504IMPLEMENT_DYNAMIC_CLASS(wxBookCtrlEvent, wxNotifyEvent)
1d6fcbcc 505
15aad3b9 506#endif // wxUSE_BOOKCTRL