]>
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()
58 m_ownsImageList
= false;
59 m_fitToCurrentPage
= false;
61 #if defined(__WXWINCE__)
68 m_controlSizer
= NULL
;
72 wxBookCtrlBase::Create(wxWindow
*parent
,
79 return wxControl::Create
91 wxBookCtrlBase::~wxBookCtrlBase()
93 if ( m_ownsImageList
)
100 // ----------------------------------------------------------------------------
102 // ----------------------------------------------------------------------------
104 void wxBookCtrlBase::SetImageList(wxImageList
*imageList
)
106 if ( m_ownsImageList
)
111 m_ownsImageList
= false;
114 m_imageList
= imageList
;
117 void wxBookCtrlBase::AssignImageList(wxImageList
* imageList
)
119 SetImageList(imageList
);
121 m_ownsImageList
= true;
124 // ----------------------------------------------------------------------------
126 // ----------------------------------------------------------------------------
128 void wxBookCtrlBase::DoInvalidateBestSize()
130 // notice that it is not necessary to invalidate our own best size
131 // explicitly if we have m_bookctrl as it will already invalidate the best
132 // size of its parent when its own size is invalidated and its parent is
135 m_bookctrl
->InvalidateBestSize();
137 wxControl::InvalidateBestSize();
140 wxSize
wxBookCtrlBase::CalcSizeFromPage(const wxSize
& sizePage
) const
142 // we need to add the size of the choice control and the border between
143 const wxSize sizeController
= GetControllerSize();
145 wxSize size
= sizePage
;
148 if ( sizeController
.x
> sizePage
.x
)
149 size
.x
= sizeController
.x
;
150 size
.y
+= sizeController
.y
+ GetInternalBorder();
152 else // left/right aligned
154 size
.x
+= sizeController
.x
+ GetInternalBorder();
155 if ( sizeController
.y
> sizePage
.y
)
156 size
.y
= sizeController
.y
;
162 void wxBookCtrlBase::SetPageSize(const wxSize
& size
)
164 SetClientSize(CalcSizeFromPage(size
));
167 wxSize
wxBookCtrlBase::DoGetBestSize() const
171 // iterate over all pages, get the largest width and height
172 const size_t nCount
= m_pages
.size();
173 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ )
175 const wxWindow
* const pPage
= m_pages
[nPage
];
178 wxSize
childBestSize(pPage
->GetBestSize());
180 if ( childBestSize
.x
> bestSize
.x
)
181 bestSize
.x
= childBestSize
.x
;
183 if ( childBestSize
.y
> bestSize
.y
)
184 bestSize
.y
= childBestSize
.y
;
188 if (m_fitToCurrentPage
&& GetCurrentPage())
189 bestSize
= GetCurrentPage()->GetBestSize();
191 // convert display area to window area, adding the size necessary for the
193 wxSize best
= CalcSizeFromPage(bestSize
);
198 wxRect
wxBookCtrlBase::GetPageRect() const
200 const wxSize size
= GetControllerSize();
203 wxRect
rectPage(pt
, GetClientSize());
205 switch ( GetWindowStyle() & wxBK_ALIGN_MASK
)
208 wxFAIL_MSG( wxT("unexpected alignment") );
212 rectPage
.y
= size
.y
+ GetInternalBorder();
216 rectPage
.height
-= size
.y
+ GetInternalBorder();
217 if (rectPage
.height
< 0)
222 rectPage
.x
= size
.x
+ GetInternalBorder();
226 rectPage
.width
-= size
.x
+ GetInternalBorder();
227 if (rectPage
.width
< 0)
236 void wxBookCtrlBase::DoSize()
240 // we're not fully created yet or OnSize() should be hidden by derived class
248 // resize controller and the page area to fit inside our new size
249 const wxSize
sizeClient( GetClientSize() ),
250 sizeBorder( m_bookctrl
->GetSize() - m_bookctrl
->GetClientSize() ),
251 sizeCtrl( GetControllerSize() );
253 m_bookctrl
->SetClientSize( sizeCtrl
.x
- sizeBorder
.x
, sizeCtrl
.y
- sizeBorder
.y
);
254 // if this changes the visibility of the scrollbars the best size changes, relayout in this case
255 wxSize sizeCtrl2
= GetControllerSize();
256 if ( sizeCtrl
!= sizeCtrl2
)
258 wxSize sizeBorder2
= m_bookctrl
->GetSize() - m_bookctrl
->GetClientSize();
259 m_bookctrl
->SetClientSize( sizeCtrl2
.x
- sizeBorder2
.x
, sizeCtrl2
.y
- sizeBorder2
.y
);
262 const wxSize sizeNew
= m_bookctrl
->GetSize();
264 switch ( GetWindowStyle() & wxBK_ALIGN_MASK
)
267 wxFAIL_MSG( wxT("unexpected alignment") );
272 // posCtrl is already ok
276 posCtrl
.y
= sizeClient
.y
- sizeNew
.y
;
280 posCtrl
.x
= sizeClient
.x
- sizeNew
.x
;
284 if ( m_bookctrl
->GetPosition() != posCtrl
)
285 m_bookctrl
->Move(posCtrl
);
288 // resize all pages to fit the new control size
289 const wxRect pageRect
= GetPageRect();
290 const unsigned pagesCount
= m_pages
.GetCount();
291 for ( unsigned int i
= 0; i
< pagesCount
; ++i
)
293 wxWindow
* const page
= m_pages
[i
];
296 wxASSERT_MSG( AllowNullPage(),
297 wxT("Null page in a control that does not allow null pages?") );
301 page
->SetSize(pageRect
);
305 void wxBookCtrlBase::OnSize(wxSizeEvent
& event
)
312 wxSize
wxBookCtrlBase::GetControllerSize() const
317 const wxSize sizeClient
= GetClientSize(),
318 sizeCtrl
= m_bookctrl
->GetBestSize();
324 size
.x
= sizeClient
.x
;
327 else // left/right aligned
330 size
.y
= sizeClient
.y
;
336 // ----------------------------------------------------------------------------
337 // miscellaneous stuff
338 // ----------------------------------------------------------------------------
342 void wxBookCtrlBase::OnHelp(wxHelpEvent
& event
)
344 // determine where does this even originate from to avoid redirecting it
345 // back to the page which generated it (resulting in an infinite loop)
347 // notice that we have to check in the hard(er) way instead of just testing
348 // if the event object == this because the book control can have other
349 // subcontrols inside it (e.g. wxSpinButton in case of a notebook in wxUniv)
350 wxWindow
*source
= wxStaticCast(event
.GetEventObject(), wxWindow
);
351 while ( source
&& source
!= this && source
->GetParent() != this )
353 source
= source
->GetParent();
356 if ( source
&& m_pages
.Index(source
) == wxNOT_FOUND
)
358 // this event is for the book control itself, redirect it to the
359 // corresponding page
360 wxWindow
*page
= NULL
;
362 if ( event
.GetOrigin() == wxHelpEvent::Origin_HelpButton
)
364 // show help for the page under the mouse
365 const int pagePos
= HitTest(ScreenToClient(event
.GetPosition()));
367 if ( pagePos
!= wxNOT_FOUND
)
369 page
= GetPage((size_t)pagePos
);
372 else // event from keyboard or unknown source
374 // otherwise show the current page help
375 page
= GetCurrentPage();
380 // change event object to the page to avoid infinite recursion if
381 // we get this event ourselves if the page doesn't handle it
382 event
.SetEventObject(page
);
384 if ( page
->GetEventHandler()->ProcessEvent(event
) )
386 // don't call event.Skip()
391 //else: event coming from one of our pages already
398 // ----------------------------------------------------------------------------
400 // ----------------------------------------------------------------------------
403 wxBookCtrlBase::InsertPage(size_t nPage
,
405 const wxString
& WXUNUSED(text
),
406 bool WXUNUSED(bSelect
),
407 int WXUNUSED(imageId
))
409 wxCHECK_MSG( page
|| AllowNullPage(), false,
410 wxT("NULL page in wxBookCtrlBase::InsertPage()") );
411 wxCHECK_MSG( nPage
<= m_pages
.size(), false,
412 wxT("invalid page index in wxBookCtrlBase::InsertPage()") );
414 m_pages
.Insert(page
, nPage
);
416 page
->SetSize(GetPageRect());
418 DoInvalidateBestSize();
423 bool wxBookCtrlBase::DeletePage(size_t nPage
)
425 wxWindow
*page
= DoRemovePage(nPage
);
426 if ( !(page
|| AllowNullPage()) )
429 // delete NULL is harmless
435 wxWindow
*wxBookCtrlBase::DoRemovePage(size_t nPage
)
437 wxCHECK_MSG( nPage
< m_pages
.size(), NULL
,
438 wxT("invalid page index in wxBookCtrlBase::DoRemovePage()") );
440 wxWindow
*pageRemoved
= m_pages
[nPage
];
441 m_pages
.RemoveAt(nPage
);
442 DoInvalidateBestSize();
447 int wxBookCtrlBase::GetNextPage(bool forward
) const
451 int nMax
= GetPageCount();
452 if ( nMax
-- ) // decrement it to get the last valid index
454 int nSel
= GetSelection();
456 // change selection wrapping if it becomes invalid
457 nPage
= forward
? nSel
== nMax
? 0
462 else // notebook is empty, no next page
470 int wxBookCtrlBase::DoSetSelection(size_t n
, int flags
)
472 wxCHECK_MSG( n
< GetPageCount(), wxNOT_FOUND
,
473 wxT("invalid page index in wxBookCtrlBase::DoSetSelection()") );
475 const int oldSel
= GetSelection();
477 if ( n
!= (size_t)oldSel
)
479 wxBookCtrlEvent
*event
= CreatePageChangingEvent();
480 bool allowed
= false;
482 if ( flags
& SetSelection_SendEvent
)
484 event
->SetSelection(n
);
485 event
->SetOldSelection(oldSel
);
486 event
->SetEventObject(this);
488 allowed
= !GetEventHandler()->ProcessEvent(*event
) || event
->IsAllowed();
491 if ( !(flags
& SetSelection_SendEvent
) || allowed
)
493 if ( oldSel
!= wxNOT_FOUND
)
494 m_pages
[oldSel
]->Hide();
496 wxWindow
*page
= m_pages
[n
];
497 page
->SetSize(GetPageRect());
500 // change selection now to ignore the selection change event
501 UpdateSelectedPage(n
);
503 if ( flags
& SetSelection_SendEvent
)
505 // program allows the page change
506 MakeChangedEvent(*event
);
507 (void)GetEventHandler()->ProcessEvent(*event
);
517 IMPLEMENT_DYNAMIC_CLASS(wxBookCtrlEvent
, wxNotifyEvent
)
519 #endif // wxUSE_BOOKCTRL