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