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