]>
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 // we need to add the size of the choice control and the border between
109 const wxSize sizeController
= GetControllerSize();
111 wxSize size
= sizePage
;
114 if ( sizeController
.x
> sizePage
.x
)
115 size
.x
= sizeController
.x
;
116 size
.y
+= sizeController
.y
+ GetInternalBorder();
118 else // left/right aligned
120 size
.x
+= sizeController
.x
+ GetInternalBorder();
121 if ( sizeController
.y
> sizePage
.y
)
122 size
.y
= sizeController
.y
;
128 void wxBookCtrlBase::SetPageSize(const wxSize
& size
)
130 SetClientSize(CalcSizeFromPage(size
));
133 wxSize
wxBookCtrlBase::DoGetBestSize() const
137 // iterate over all pages, get the largest width and height
138 const size_t nCount
= m_pages
.size();
139 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ )
141 const wxWindow
* const pPage
= m_pages
[nPage
];
144 wxSize
childBestSize(pPage
->GetBestSize());
146 if ( childBestSize
.x
> bestSize
.x
)
147 bestSize
.x
= childBestSize
.x
;
149 if ( childBestSize
.y
> bestSize
.y
)
150 bestSize
.y
= childBestSize
.y
;
154 if (m_fitToCurrentPage
&& GetCurrentPage())
155 bestSize
= GetCurrentPage()->GetBestSize();
157 // convert display area to window area, adding the size necessary for the
159 wxSize best
= CalcSizeFromPage(bestSize
);
164 wxRect
wxBookCtrlBase::GetPageRect() const
166 const wxSize size
= GetControllerSize();
169 wxRect
rectPage(pt
, GetClientSize());
171 switch ( GetWindowStyle() & wxBK_ALIGN_MASK
)
174 wxFAIL_MSG( wxT("unexpected alignment") );
178 rectPage
.y
= size
.y
+ GetInternalBorder();
182 rectPage
.height
-= size
.y
+ GetInternalBorder();
183 if (rectPage
.height
< 0)
188 rectPage
.x
= size
.x
+ GetInternalBorder();
192 rectPage
.width
-= size
.x
+ GetInternalBorder();
193 if (rectPage
.width
< 0)
202 void wxBookCtrlBase::DoSize()
206 // we're not fully created yet or OnSize() should be hidden by derived class
214 // resize controller and the page area to fit inside our new size
215 const wxSize
sizeClient( GetClientSize() ),
216 sizeBorder( m_bookctrl
->GetSize() - m_bookctrl
->GetClientSize() ),
217 sizeCtrl( GetControllerSize() );
219 m_bookctrl
->SetClientSize( sizeCtrl
.x
- sizeBorder
.x
, sizeCtrl
.y
- sizeBorder
.y
);
220 // if this changes the visibility of the scrollbars the best size changes, relayout in this case
221 wxSize sizeCtrl2
= GetControllerSize();
222 if ( sizeCtrl
!= sizeCtrl2
)
224 wxSize sizeBorder2
= m_bookctrl
->GetSize() - m_bookctrl
->GetClientSize();
225 m_bookctrl
->SetClientSize( sizeCtrl2
.x
- sizeBorder2
.x
, sizeCtrl2
.y
- sizeBorder2
.y
);
228 const wxSize sizeNew
= m_bookctrl
->GetSize();
230 switch ( GetWindowStyle() & wxBK_ALIGN_MASK
)
233 wxFAIL_MSG( wxT("unexpected alignment") );
238 // posCtrl is already ok
242 posCtrl
.y
= sizeClient
.y
- sizeNew
.y
;
246 posCtrl
.x
= sizeClient
.x
- sizeNew
.x
;
250 if ( m_bookctrl
->GetPosition() != posCtrl
)
251 m_bookctrl
->Move(posCtrl
);
254 // resize all pages to fit the new control size
255 const wxRect pageRect
= GetPageRect();
256 const unsigned pagesCount
= m_pages
.GetCount();
257 for ( unsigned int i
= 0; i
< pagesCount
; ++i
)
259 wxWindow
* const page
= m_pages
[i
];
262 wxASSERT_MSG( AllowNullPage(),
263 wxT("Null page in a control that does not allow null pages?") );
267 page
->SetSize(pageRect
);
271 void wxBookCtrlBase::OnSize(wxSizeEvent
& event
)
278 wxSize
wxBookCtrlBase::GetControllerSize() const
280 // For at least some book controls (e.g. wxChoicebook) it may make sense to
281 // (temporarily?) hide the controller and we shouldn't leave extra space
282 // for the hidden control in this case.
283 if ( !m_bookctrl
|| !m_bookctrl
->IsShown() )
286 const wxSize sizeClient
= GetClientSize(),
287 sizeCtrl
= m_bookctrl
->GetBestSize();
293 size
.x
= sizeClient
.x
;
296 else // left/right aligned
299 size
.y
= sizeClient
.y
;
305 // ----------------------------------------------------------------------------
306 // miscellaneous stuff
307 // ----------------------------------------------------------------------------
311 void wxBookCtrlBase::OnHelp(wxHelpEvent
& event
)
313 // determine where does this even originate from to avoid redirecting it
314 // back to the page which generated it (resulting in an infinite loop)
316 // notice that we have to check in the hard(er) way instead of just testing
317 // if the event object == this because the book control can have other
318 // subcontrols inside it (e.g. wxSpinButton in case of a notebook in wxUniv)
319 wxWindow
*source
= wxStaticCast(event
.GetEventObject(), wxWindow
);
320 while ( source
&& source
!= this && source
->GetParent() != this )
322 source
= source
->GetParent();
325 if ( source
&& m_pages
.Index(source
) == wxNOT_FOUND
)
327 // this event is for the book control itself, redirect it to the
328 // corresponding page
329 wxWindow
*page
= NULL
;
331 if ( event
.GetOrigin() == wxHelpEvent::Origin_HelpButton
)
333 // show help for the page under the mouse
334 const int pagePos
= HitTest(ScreenToClient(event
.GetPosition()));
336 if ( pagePos
!= wxNOT_FOUND
)
338 page
= GetPage((size_t)pagePos
);
341 else // event from keyboard or unknown source
343 // otherwise show the current page help
344 page
= GetCurrentPage();
349 // change event object to the page to avoid infinite recursion if
350 // we get this event ourselves if the page doesn't handle it
351 event
.SetEventObject(page
);
353 if ( page
->GetEventHandler()->ProcessEvent(event
) )
355 // don't call event.Skip()
360 //else: event coming from one of our pages already
367 // ----------------------------------------------------------------------------
369 // ----------------------------------------------------------------------------
372 wxBookCtrlBase::InsertPage(size_t nPage
,
374 const wxString
& WXUNUSED(text
),
375 bool WXUNUSED(bSelect
),
376 int WXUNUSED(imageId
))
378 wxCHECK_MSG( page
|| AllowNullPage(), false,
379 wxT("NULL page in wxBookCtrlBase::InsertPage()") );
380 wxCHECK_MSG( nPage
<= m_pages
.size(), false,
381 wxT("invalid page index in wxBookCtrlBase::InsertPage()") );
383 m_pages
.Insert(page
, nPage
);
385 page
->SetSize(GetPageRect());
387 DoInvalidateBestSize();
392 bool wxBookCtrlBase::DeletePage(size_t nPage
)
394 wxWindow
*page
= DoRemovePage(nPage
);
395 if ( !(page
|| AllowNullPage()) )
398 // delete NULL is harmless
404 wxWindow
*wxBookCtrlBase::DoRemovePage(size_t nPage
)
406 wxCHECK_MSG( nPage
< m_pages
.size(), NULL
,
407 wxT("invalid page index in wxBookCtrlBase::DoRemovePage()") );
409 wxWindow
*pageRemoved
= m_pages
[nPage
];
410 m_pages
.RemoveAt(nPage
);
411 DoInvalidateBestSize();
416 int wxBookCtrlBase::GetNextPage(bool forward
) const
420 int nMax
= GetPageCount();
421 if ( nMax
-- ) // decrement it to get the last valid index
423 int nSel
= GetSelection();
425 // change selection wrapping if it becomes invalid
426 nPage
= forward
? nSel
== nMax
? 0
431 else // notebook is empty, no next page
439 bool wxBookCtrlBase::DoSetSelectionAfterInsertion(size_t n
, bool bSelect
)
443 else if ( m_selection
== wxNOT_FOUND
)
445 else // We're not going to select this page.
448 // Return true to indicate that we selected this page.
452 int wxBookCtrlBase::DoSetSelection(size_t n
, int flags
)
454 wxCHECK_MSG( n
< GetPageCount(), wxNOT_FOUND
,
455 wxT("invalid page index in wxBookCtrlBase::DoSetSelection()") );
457 const int oldSel
= GetSelection();
459 if ( n
!= (size_t)oldSel
)
461 wxBookCtrlEvent
*event
= CreatePageChangingEvent();
462 bool allowed
= false;
464 if ( flags
& SetSelection_SendEvent
)
466 event
->SetSelection(n
);
467 event
->SetOldSelection(oldSel
);
468 event
->SetEventObject(this);
470 allowed
= !GetEventHandler()->ProcessEvent(*event
) || event
->IsAllowed();
473 if ( !(flags
& SetSelection_SendEvent
) || allowed
)
475 if ( oldSel
!= wxNOT_FOUND
)
476 m_pages
[oldSel
]->Hide();
478 wxWindow
*page
= m_pages
[n
];
479 page
->SetSize(GetPageRect());
482 // change selection now to ignore the selection change event
483 UpdateSelectedPage(n
);
485 if ( flags
& SetSelection_SendEvent
)
487 // program allows the page change
488 MakeChangedEvent(*event
);
489 (void)GetEventHandler()->ProcessEvent(*event
);
499 IMPLEMENT_DYNAMIC_CLASS(wxBookCtrlEvent
, wxNotifyEvent
)
501 #endif // wxUSE_BOOKCTRL