]>
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 controller and the border between if it's
109 // shown. Notice that we don't use GetControllerSize() here as it returns
110 // the actual size while we want the best size here.
111 if ( !m_bookctrl
|| !m_bookctrl
->IsShown() )
114 const wxSize sizeController
= m_bookctrl
->GetBestSize();
116 wxSize size
= sizePage
;
119 if ( sizeController
.x
> sizePage
.x
)
120 size
.x
= sizeController
.x
;
121 size
.y
+= sizeController
.y
+ GetInternalBorder();
123 else // left/right aligned
125 size
.x
+= sizeController
.x
+ GetInternalBorder();
126 if ( sizeController
.y
> sizePage
.y
)
127 size
.y
= sizeController
.y
;
133 void wxBookCtrlBase::SetPageSize(const wxSize
& size
)
135 SetClientSize(CalcSizeFromPage(size
));
138 wxSize
wxBookCtrlBase::DoGetBestSize() const
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
];
149 wxSize
childBestSize(pPage
->GetBestSize());
151 if ( childBestSize
.x
> bestSize
.x
)
152 bestSize
.x
= childBestSize
.x
;
154 if ( childBestSize
.y
> bestSize
.y
)
155 bestSize
.y
= childBestSize
.y
;
159 if (m_fitToCurrentPage
&& GetCurrentPage())
160 bestSize
= GetCurrentPage()->GetBestSize();
162 // convert display area to window area, adding the size necessary for the
164 wxSize best
= CalcSizeFromPage(bestSize
);
169 wxRect
wxBookCtrlBase::GetPageRect() const
171 const wxSize size
= GetControllerSize();
174 wxRect
rectPage(pt
, GetClientSize());
176 switch ( GetWindowStyle() & wxBK_ALIGN_MASK
)
179 wxFAIL_MSG( wxT("unexpected alignment") );
183 rectPage
.y
= size
.y
+ GetInternalBorder();
187 rectPage
.height
-= size
.y
+ GetInternalBorder();
188 if (rectPage
.height
< 0)
193 rectPage
.x
= size
.x
+ GetInternalBorder();
197 rectPage
.width
-= size
.x
+ GetInternalBorder();
198 if (rectPage
.width
< 0)
207 void wxBookCtrlBase::DoSize()
211 // we're not fully created yet or OnSize() should be hidden by derived class
219 // resize controller and the page area to fit inside our new size
220 const wxSize
sizeClient( GetClientSize() ),
221 sizeBorder( m_bookctrl
->GetSize() - m_bookctrl
->GetClientSize() ),
222 sizeCtrl( GetControllerSize() );
224 m_bookctrl
->SetClientSize( sizeCtrl
.x
- sizeBorder
.x
, sizeCtrl
.y
- sizeBorder
.y
);
225 // if this changes the visibility of the scrollbars the best size changes, relayout in this case
226 wxSize sizeCtrl2
= GetControllerSize();
227 if ( sizeCtrl
!= sizeCtrl2
)
229 wxSize sizeBorder2
= m_bookctrl
->GetSize() - m_bookctrl
->GetClientSize();
230 m_bookctrl
->SetClientSize( sizeCtrl2
.x
- sizeBorder2
.x
, sizeCtrl2
.y
- sizeBorder2
.y
);
233 const wxSize sizeNew
= m_bookctrl
->GetSize();
235 switch ( GetWindowStyle() & wxBK_ALIGN_MASK
)
238 wxFAIL_MSG( wxT("unexpected alignment") );
243 // posCtrl is already ok
247 posCtrl
.y
= sizeClient
.y
- sizeNew
.y
;
251 posCtrl
.x
= sizeClient
.x
- sizeNew
.x
;
255 if ( m_bookctrl
->GetPosition() != posCtrl
)
256 m_bookctrl
->Move(posCtrl
);
259 // resize all pages to fit the new control size
260 const wxRect pageRect
= GetPageRect();
261 const unsigned pagesCount
= m_pages
.GetCount();
262 for ( unsigned int i
= 0; i
< pagesCount
; ++i
)
264 wxWindow
* const page
= m_pages
[i
];
267 wxASSERT_MSG( AllowNullPage(),
268 wxT("Null page in a control that does not allow null pages?") );
272 page
->SetSize(pageRect
);
276 void wxBookCtrlBase::OnSize(wxSizeEvent
& event
)
283 wxSize
wxBookCtrlBase::GetControllerSize() const
285 // For at least some book controls (e.g. wxChoicebook) it may make sense to
286 // (temporarily?) hide the controller and we shouldn't leave extra space
287 // for the hidden control in this case.
288 if ( !m_bookctrl
|| !m_bookctrl
->IsShown() )
291 const wxSize sizeClient
= GetClientSize(),
292 sizeCtrl
= m_bookctrl
->GetBestSize();
298 size
.x
= sizeClient
.x
;
301 else // left/right aligned
304 size
.y
= sizeClient
.y
;
310 // ----------------------------------------------------------------------------
311 // miscellaneous stuff
312 // ----------------------------------------------------------------------------
316 void wxBookCtrlBase::OnHelp(wxHelpEvent
& event
)
318 // determine where does this even originate from to avoid redirecting it
319 // back to the page which generated it (resulting in an infinite loop)
321 // notice that we have to check in the hard(er) way instead of just testing
322 // if the event object == this because the book control can have other
323 // subcontrols inside it (e.g. wxSpinButton in case of a notebook in wxUniv)
324 wxWindow
*source
= wxStaticCast(event
.GetEventObject(), wxWindow
);
325 while ( source
&& source
!= this && source
->GetParent() != this )
327 source
= source
->GetParent();
330 if ( source
&& m_pages
.Index(source
) == wxNOT_FOUND
)
332 // this event is for the book control itself, redirect it to the
333 // corresponding page
334 wxWindow
*page
= NULL
;
336 if ( event
.GetOrigin() == wxHelpEvent::Origin_HelpButton
)
338 // show help for the page under the mouse
339 const int pagePos
= HitTest(ScreenToClient(event
.GetPosition()));
341 if ( pagePos
!= wxNOT_FOUND
)
343 page
= GetPage((size_t)pagePos
);
346 else // event from keyboard or unknown source
348 // otherwise show the current page help
349 page
= GetCurrentPage();
354 // change event object to the page to avoid infinite recursion if
355 // we get this event ourselves if the page doesn't handle it
356 event
.SetEventObject(page
);
358 if ( page
->GetEventHandler()->ProcessEvent(event
) )
360 // don't call event.Skip()
365 //else: event coming from one of our pages already
372 // ----------------------------------------------------------------------------
374 // ----------------------------------------------------------------------------
377 wxBookCtrlBase::InsertPage(size_t nPage
,
379 const wxString
& WXUNUSED(text
),
380 bool WXUNUSED(bSelect
),
381 int WXUNUSED(imageId
))
383 wxCHECK_MSG( page
|| AllowNullPage(), false,
384 wxT("NULL page in wxBookCtrlBase::InsertPage()") );
385 wxCHECK_MSG( nPage
<= m_pages
.size(), false,
386 wxT("invalid page index in wxBookCtrlBase::InsertPage()") );
388 m_pages
.Insert(page
, nPage
);
390 page
->SetSize(GetPageRect());
392 DoInvalidateBestSize();
397 bool wxBookCtrlBase::DeletePage(size_t nPage
)
399 wxWindow
*page
= DoRemovePage(nPage
);
400 if ( !(page
|| AllowNullPage()) )
403 // delete NULL is harmless
409 wxWindow
*wxBookCtrlBase::DoRemovePage(size_t nPage
)
411 wxCHECK_MSG( nPage
< m_pages
.size(), NULL
,
412 wxT("invalid page index in wxBookCtrlBase::DoRemovePage()") );
414 wxWindow
*pageRemoved
= m_pages
[nPage
];
415 m_pages
.RemoveAt(nPage
);
416 DoInvalidateBestSize();
421 int wxBookCtrlBase::GetNextPage(bool forward
) const
425 int nMax
= GetPageCount();
426 if ( nMax
-- ) // decrement it to get the last valid index
428 int nSel
= GetSelection();
430 // change selection wrapping if it becomes invalid
431 nPage
= forward
? nSel
== nMax
? 0
436 else // notebook is empty, no next page
444 bool wxBookCtrlBase::DoSetSelectionAfterInsertion(size_t n
, bool bSelect
)
448 else if ( m_selection
== wxNOT_FOUND
)
450 else // We're not going to select this page.
453 // Return true to indicate that we selected this page.
457 int wxBookCtrlBase::DoSetSelection(size_t n
, int flags
)
459 wxCHECK_MSG( n
< GetPageCount(), wxNOT_FOUND
,
460 wxT("invalid page index in wxBookCtrlBase::DoSetSelection()") );
462 const int oldSel
= GetSelection();
464 if ( n
!= (size_t)oldSel
)
466 wxBookCtrlEvent
*event
= CreatePageChangingEvent();
467 bool allowed
= false;
469 if ( flags
& SetSelection_SendEvent
)
471 event
->SetSelection(n
);
472 event
->SetOldSelection(oldSel
);
473 event
->SetEventObject(this);
475 allowed
= !GetEventHandler()->ProcessEvent(*event
) || event
->IsAllowed();
478 if ( !(flags
& SetSelection_SendEvent
) || allowed
)
480 if ( oldSel
!= wxNOT_FOUND
)
481 m_pages
[oldSel
]->Hide();
483 wxWindow
*page
= m_pages
[n
];
484 page
->SetSize(GetPageRect());
487 // change selection now to ignore the selection change event
488 UpdateSelectedPage(n
);
490 if ( flags
& SetSelection_SendEvent
)
492 // program allows the page change
493 MakeChangedEvent(*event
);
494 (void)GetEventHandler()->ProcessEvent(*event
);
504 IMPLEMENT_DYNAMIC_CLASS(wxBookCtrlEvent
, wxNotifyEvent
)
506 #endif // wxUSE_BOOKCTRL