]>
git.saurik.com Git - wxWidgets.git/blob - src/common/bookctrl.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/bookctrl.cpp
3 // Purpose: wxBookCtrlBase implementation
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/imaglist.h"
31 #include "wx/bookctrl.h"
33 // ============================================================================
35 // ============================================================================
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 IMPLEMENT_ABSTRACT_CLASS(wxBookCtrlBase
, wxControl
)
43 BEGIN_EVENT_TABLE(wxBookCtrlBase
, wxControl
)
44 EVT_SIZE(wxBookCtrlBase::OnSize
)
46 EVT_HELP(wxID_ANY
, wxBookCtrlBase::OnHelp
)
50 // ----------------------------------------------------------------------------
51 // constructors and destructors
52 // ----------------------------------------------------------------------------
54 void wxBookCtrlBase::Init()
56 m_selection
= wxNOT_FOUND
;
59 m_ownsImageList
= false;
60 m_fitToCurrentPage
= false;
62 #if defined(__WXWINCE__)
69 m_controlSizer
= NULL
;
73 wxBookCtrlBase::Create(wxWindow
*parent
,
80 return wxControl::Create
92 wxBookCtrlBase::~wxBookCtrlBase()
94 if ( m_ownsImageList
)
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 void wxBookCtrlBase::SetImageList(wxImageList
*imageList
)
107 if ( m_ownsImageList
)
112 m_ownsImageList
= false;
115 m_imageList
= imageList
;
118 void wxBookCtrlBase::AssignImageList(wxImageList
* imageList
)
120 SetImageList(imageList
);
122 m_ownsImageList
= true;
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
129 void wxBookCtrlBase::DoInvalidateBestSize()
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
136 m_bookctrl
->InvalidateBestSize();
138 wxControl::InvalidateBestSize();
141 wxSize
wxBookCtrlBase::CalcSizeFromPage(const wxSize
& sizePage
) const
143 // we need to add the size of the choice control and the border between
144 const wxSize sizeController
= GetControllerSize();
146 wxSize size
= sizePage
;
149 if ( sizeController
.x
> sizePage
.x
)
150 size
.x
= sizeController
.x
;
151 size
.y
+= sizeController
.y
+ GetInternalBorder();
153 else // left/right aligned
155 size
.x
+= sizeController
.x
+ GetInternalBorder();
156 if ( sizeController
.y
> sizePage
.y
)
157 size
.y
= sizeController
.y
;
163 void wxBookCtrlBase::SetPageSize(const wxSize
& size
)
165 SetClientSize(CalcSizeFromPage(size
));
168 wxSize
wxBookCtrlBase::DoGetBestSize() const
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
++ )
176 const wxWindow
* const pPage
= m_pages
[nPage
];
179 wxSize
childBestSize(pPage
->GetBestSize());
181 if ( childBestSize
.x
> bestSize
.x
)
182 bestSize
.x
= childBestSize
.x
;
184 if ( childBestSize
.y
> bestSize
.y
)
185 bestSize
.y
= childBestSize
.y
;
189 if (m_fitToCurrentPage
&& GetCurrentPage())
190 bestSize
= GetCurrentPage()->GetBestSize();
192 // convert display area to window area, adding the size necessary for the
194 wxSize best
= CalcSizeFromPage(bestSize
);
199 wxRect
wxBookCtrlBase::GetPageRect() const
201 const wxSize size
= GetControllerSize();
204 wxRect
rectPage(pt
, GetClientSize());
206 switch ( GetWindowStyle() & wxBK_ALIGN_MASK
)
209 wxFAIL_MSG( wxT("unexpected alignment") );
213 rectPage
.y
= size
.y
+ GetInternalBorder();
217 rectPage
.height
-= size
.y
+ GetInternalBorder();
218 if (rectPage
.height
< 0)
223 rectPage
.x
= size
.x
+ GetInternalBorder();
227 rectPage
.width
-= size
.x
+ GetInternalBorder();
228 if (rectPage
.width
< 0)
237 void wxBookCtrlBase::DoSize()
241 // we're not fully created yet or OnSize() should be hidden by derived class
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() );
254 m_bookctrl
->SetClientSize( sizeCtrl
.x
- sizeBorder
.x
, sizeCtrl
.y
- sizeBorder
.y
);
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
)
259 wxSize sizeBorder2
= m_bookctrl
->GetSize() - m_bookctrl
->GetClientSize();
260 m_bookctrl
->SetClientSize( sizeCtrl2
.x
- sizeBorder2
.x
, sizeCtrl2
.y
- sizeBorder2
.y
);
263 const wxSize sizeNew
= m_bookctrl
->GetSize();
265 switch ( GetWindowStyle() & wxBK_ALIGN_MASK
)
268 wxFAIL_MSG( wxT("unexpected alignment") );
273 // posCtrl is already ok
277 posCtrl
.y
= sizeClient
.y
- sizeNew
.y
;
281 posCtrl
.x
= sizeClient
.x
- sizeNew
.x
;
285 if ( m_bookctrl
->GetPosition() != posCtrl
)
286 m_bookctrl
->Move(posCtrl
);
289 // resize all pages to fit the new control size
290 const wxRect pageRect
= GetPageRect();
291 const unsigned pagesCount
= m_pages
.GetCount();
292 for ( unsigned int i
= 0; i
< pagesCount
; ++i
)
294 wxWindow
* const page
= m_pages
[i
];
297 wxASSERT_MSG( AllowNullPage(),
298 wxT("Null page in a control that does not allow null pages?") );
302 page
->SetSize(pageRect
);
306 void wxBookCtrlBase::OnSize(wxSizeEvent
& event
)
313 wxSize
wxBookCtrlBase::GetControllerSize() const
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() )
321 const wxSize sizeClient
= GetClientSize(),
322 sizeCtrl
= m_bookctrl
->GetBestSize();
328 size
.x
= sizeClient
.x
;
331 else // left/right aligned
334 size
.y
= sizeClient
.y
;
340 // ----------------------------------------------------------------------------
341 // miscellaneous stuff
342 // ----------------------------------------------------------------------------
346 void wxBookCtrlBase::OnHelp(wxHelpEvent
& event
)
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)
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 )
357 source
= source
->GetParent();
360 if ( source
&& m_pages
.Index(source
) == wxNOT_FOUND
)
362 // this event is for the book control itself, redirect it to the
363 // corresponding page
364 wxWindow
*page
= NULL
;
366 if ( event
.GetOrigin() == wxHelpEvent::Origin_HelpButton
)
368 // show help for the page under the mouse
369 const int pagePos
= HitTest(ScreenToClient(event
.GetPosition()));
371 if ( pagePos
!= wxNOT_FOUND
)
373 page
= GetPage((size_t)pagePos
);
376 else // event from keyboard or unknown source
378 // otherwise show the current page help
379 page
= GetCurrentPage();
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
);
388 if ( page
->GetEventHandler()->ProcessEvent(event
) )
390 // don't call event.Skip()
395 //else: event coming from one of our pages already
402 // ----------------------------------------------------------------------------
404 // ----------------------------------------------------------------------------
407 wxBookCtrlBase::InsertPage(size_t nPage
,
409 const wxString
& WXUNUSED(text
),
410 bool WXUNUSED(bSelect
),
411 int WXUNUSED(imageId
))
413 wxCHECK_MSG( page
|| AllowNullPage(), false,
414 wxT("NULL page in wxBookCtrlBase::InsertPage()") );
415 wxCHECK_MSG( nPage
<= m_pages
.size(), false,
416 wxT("invalid page index in wxBookCtrlBase::InsertPage()") );
418 m_pages
.Insert(page
, nPage
);
420 page
->SetSize(GetPageRect());
422 DoInvalidateBestSize();
427 bool wxBookCtrlBase::DeletePage(size_t nPage
)
429 wxWindow
*page
= DoRemovePage(nPage
);
430 if ( !(page
|| AllowNullPage()) )
433 // delete NULL is harmless
439 wxWindow
*wxBookCtrlBase::DoRemovePage(size_t nPage
)
441 wxCHECK_MSG( nPage
< m_pages
.size(), NULL
,
442 wxT("invalid page index in wxBookCtrlBase::DoRemovePage()") );
444 wxWindow
*pageRemoved
= m_pages
[nPage
];
445 m_pages
.RemoveAt(nPage
);
446 DoInvalidateBestSize();
451 int wxBookCtrlBase::GetNextPage(bool forward
) const
455 int nMax
= GetPageCount();
456 if ( nMax
-- ) // decrement it to get the last valid index
458 int nSel
= GetSelection();
460 // change selection wrapping if it becomes invalid
461 nPage
= forward
? nSel
== nMax
? 0
466 else // notebook is empty, no next page
474 bool wxBookCtrlBase::DoSetSelectionAfterInsertion(size_t n
, bool bSelect
)
478 else if ( m_selection
== wxNOT_FOUND
)
480 else // We're not going to select this page.
483 // Return true to indicate that we selected this page.
487 int wxBookCtrlBase::DoSetSelection(size_t n
, int flags
)
489 wxCHECK_MSG( n
< GetPageCount(), wxNOT_FOUND
,
490 wxT("invalid page index in wxBookCtrlBase::DoSetSelection()") );
492 const int oldSel
= GetSelection();
494 if ( n
!= (size_t)oldSel
)
496 wxBookCtrlEvent
*event
= CreatePageChangingEvent();
497 bool allowed
= false;
499 if ( flags
& SetSelection_SendEvent
)
501 event
->SetSelection(n
);
502 event
->SetOldSelection(oldSel
);
503 event
->SetEventObject(this);
505 allowed
= !GetEventHandler()->ProcessEvent(*event
) || event
->IsAllowed();
508 if ( !(flags
& SetSelection_SendEvent
) || allowed
)
510 if ( oldSel
!= wxNOT_FOUND
)
511 m_pages
[oldSel
]->Hide();
513 wxWindow
*page
= m_pages
[n
];
514 page
->SetSize(GetPageRect());
517 // change selection now to ignore the selection change event
518 UpdateSelectedPage(n
);
520 if ( flags
& SetSelection_SendEvent
)
522 // program allows the page change
523 MakeChangedEvent(*event
);
524 (void)GetEventHandler()->ProcessEvent(*event
);
534 IMPLEMENT_DYNAMIC_CLASS(wxBookCtrlEvent
, wxNotifyEvent
)
536 #endif // wxUSE_BOOKCTRL