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