]>
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
;
58 m_fitToCurrentPage
= false;
60 #if defined(__WXWINCE__)
67 m_controlSizer
= NULL
;
71 wxBookCtrlBase::Create(wxWindow
*parent
,
78 return wxControl::Create
90 // ----------------------------------------------------------------------------
92 // ----------------------------------------------------------------------------
94 void wxBookCtrlBase::DoInvalidateBestSize()
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
101 m_bookctrl
->InvalidateBestSize();
103 wxControl::InvalidateBestSize();
106 wxSize
wxBookCtrlBase::CalcSizeFromPage(const wxSize
& sizePage
) const
108 // Add the size of the controller and the border between if it's shown.
109 if ( !m_bookctrl
|| !m_bookctrl
->IsShown() )
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.
116 const wxSize sizeController
= GetControllerSize();
118 wxSize size
= sizePage
;
120 size
.y
+= sizeController
.y
+ GetInternalBorder();
121 else // left/right aligned
122 size
.x
+= sizeController
.x
+ GetInternalBorder();
127 void wxBookCtrlBase::SetPageSize(const wxSize
& size
)
129 SetClientSize(CalcSizeFromPage(size
));
132 wxSize
wxBookCtrlBase::DoGetBestSize() const
136 if (m_fitToCurrentPage
&& GetCurrentPage())
138 bestSize
= GetCurrentPage()->GetBestSize();
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
++ )
146 const wxWindow
* const pPage
= m_pages
[nPage
];
148 bestSize
.IncTo(pPage
->GetBestSize());
152 // convert display area to window area, adding the size necessary for the
154 wxSize best
= CalcSizeFromPage(bestSize
);
159 wxRect
wxBookCtrlBase::GetPageRect() const
161 const wxSize size
= GetControllerSize();
164 wxRect
rectPage(pt
, GetClientSize());
166 switch ( GetWindowStyle() & wxBK_ALIGN_MASK
)
169 wxFAIL_MSG( wxT("unexpected alignment") );
173 rectPage
.y
= size
.y
+ GetInternalBorder();
177 rectPage
.height
-= size
.y
+ GetInternalBorder();
178 if (rectPage
.height
< 0)
183 rectPage
.x
= size
.x
+ GetInternalBorder();
187 rectPage
.width
-= size
.x
+ GetInternalBorder();
188 if (rectPage
.width
< 0)
197 void wxBookCtrlBase::DoSize()
201 // we're not fully created yet or OnSize() should be hidden by derived class
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() );
214 m_bookctrl
->SetClientSize( sizeCtrl
.x
- sizeBorder
.x
, sizeCtrl
.y
- sizeBorder
.y
);
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
)
219 wxSize sizeBorder2
= m_bookctrl
->GetSize() - m_bookctrl
->GetClientSize();
220 m_bookctrl
->SetClientSize( sizeCtrl2
.x
- sizeBorder2
.x
, sizeCtrl2
.y
- sizeBorder2
.y
);
223 const wxSize sizeNew
= m_bookctrl
->GetSize();
225 switch ( GetWindowStyle() & wxBK_ALIGN_MASK
)
228 wxFAIL_MSG( wxT("unexpected alignment") );
233 // posCtrl is already ok
237 posCtrl
.y
= sizeClient
.y
- sizeNew
.y
;
241 posCtrl
.x
= sizeClient
.x
- sizeNew
.x
;
245 if ( m_bookctrl
->GetPosition() != posCtrl
)
246 m_bookctrl
->Move(posCtrl
);
249 // resize all pages to fit the new control size
250 const wxRect pageRect
= GetPageRect();
251 const unsigned pagesCount
= m_pages
.GetCount();
252 for ( unsigned int i
= 0; i
< pagesCount
; ++i
)
254 wxWindow
* const page
= m_pages
[i
];
257 wxASSERT_MSG( AllowNullPage(),
258 wxT("Null page in a control that does not allow null pages?") );
262 page
->SetSize(pageRect
);
266 void wxBookCtrlBase::OnSize(wxSizeEvent
& event
)
273 wxSize
wxBookCtrlBase::GetControllerSize() const
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() )
281 const wxSize sizeClient
= GetClientSize();
285 // Ask for the best width/height considering the other direction.
288 size
.x
= sizeClient
.x
;
289 size
.y
= m_bookctrl
->GetBestHeight(sizeClient
.x
);
291 else // left/right aligned
293 size
.x
= m_bookctrl
->GetBestWidth(sizeClient
.y
);
294 size
.y
= sizeClient
.y
;
300 // ----------------------------------------------------------------------------
301 // miscellaneous stuff
302 // ----------------------------------------------------------------------------
306 void wxBookCtrlBase::OnHelp(wxHelpEvent
& event
)
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)
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 )
317 source
= source
->GetParent();
320 if ( source
&& m_pages
.Index(source
) == wxNOT_FOUND
)
322 // this event is for the book control itself, redirect it to the
323 // corresponding page
324 wxWindow
*page
= NULL
;
326 if ( event
.GetOrigin() == wxHelpEvent::Origin_HelpButton
)
328 // show help for the page under the mouse
329 const int pagePos
= HitTest(ScreenToClient(event
.GetPosition()));
331 if ( pagePos
!= wxNOT_FOUND
)
333 page
= GetPage((size_t)pagePos
);
336 else // event from keyboard or unknown source
338 // otherwise show the current page help
339 page
= GetCurrentPage();
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
);
348 if ( page
->GetEventHandler()->ProcessEvent(event
) )
350 // don't call event.Skip()
355 //else: event coming from one of our pages already
362 // ----------------------------------------------------------------------------
364 // ----------------------------------------------------------------------------
367 wxBookCtrlBase::InsertPage(size_t nPage
,
369 const wxString
& WXUNUSED(text
),
370 bool WXUNUSED(bSelect
),
371 int WXUNUSED(imageId
))
373 wxCHECK_MSG( page
|| AllowNullPage(), false,
374 wxT("NULL page in wxBookCtrlBase::InsertPage()") );
375 wxCHECK_MSG( nPage
<= m_pages
.size(), false,
376 wxT("invalid page index in wxBookCtrlBase::InsertPage()") );
378 m_pages
.Insert(page
, nPage
);
380 page
->SetSize(GetPageRect());
382 DoInvalidateBestSize();
387 bool wxBookCtrlBase::DeletePage(size_t nPage
)
389 wxWindow
*page
= DoRemovePage(nPage
);
390 if ( !(page
|| AllowNullPage()) )
393 // delete NULL is harmless
399 wxWindow
*wxBookCtrlBase::DoRemovePage(size_t nPage
)
401 wxCHECK_MSG( nPage
< m_pages
.size(), NULL
,
402 wxT("invalid page index in wxBookCtrlBase::DoRemovePage()") );
404 wxWindow
*pageRemoved
= m_pages
[nPage
];
405 m_pages
.RemoveAt(nPage
);
406 DoInvalidateBestSize();
411 int wxBookCtrlBase::GetNextPage(bool forward
) const
415 int nMax
= GetPageCount();
416 if ( nMax
-- ) // decrement it to get the last valid index
418 int nSel
= GetSelection();
420 // change selection wrapping if it becomes invalid
421 nPage
= forward
? nSel
== nMax
? 0
426 else // notebook is empty, no next page
434 bool wxBookCtrlBase::DoSetSelectionAfterInsertion(size_t n
, bool bSelect
)
438 else if ( m_selection
== wxNOT_FOUND
)
440 else // We're not going to select this page.
443 // Return true to indicate that we selected this page.
447 int wxBookCtrlBase::DoSetSelection(size_t n
, int flags
)
449 wxCHECK_MSG( n
< GetPageCount(), wxNOT_FOUND
,
450 wxT("invalid page index in wxBookCtrlBase::DoSetSelection()") );
452 const int oldSel
= GetSelection();
454 if ( n
!= (size_t)oldSel
)
456 wxBookCtrlEvent
*event
= CreatePageChangingEvent();
457 bool allowed
= false;
459 if ( flags
& SetSelection_SendEvent
)
461 event
->SetSelection(n
);
462 event
->SetOldSelection(oldSel
);
463 event
->SetEventObject(this);
465 allowed
= !GetEventHandler()->ProcessEvent(*event
) || event
->IsAllowed();
468 if ( !(flags
& SetSelection_SendEvent
) || allowed
)
470 if ( oldSel
!= wxNOT_FOUND
)
471 DoShowPage(m_pages
[oldSel
], false);
473 wxWindow
*page
= m_pages
[n
];
474 page
->SetSize(GetPageRect());
475 DoShowPage(page
, true);
477 // change selection now to ignore the selection change event
478 UpdateSelectedPage(n
);
480 if ( flags
& SetSelection_SendEvent
)
482 // program allows the page change
483 MakeChangedEvent(*event
);
484 (void)GetEventHandler()->ProcessEvent(*event
);
494 IMPLEMENT_DYNAMIC_CLASS(wxBookCtrlEvent
, wxNotifyEvent
)
496 #endif // wxUSE_BOOKCTRL