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