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