]>
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::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
);
165 void wxBookCtrlBase::OnHelp(wxHelpEvent
& event
)
167 // find the corresponding page
168 wxWindow
*page
= NULL
;
170 if ( event
.GetOrigin() == wxHelpEvent::Origin_HelpButton
)
172 // show help for the page under the mouse
173 const int pagePos
= HitTest(ScreenToClient(event
.GetPosition()));
175 if ( pagePos
!= wxNOT_FOUND
)
177 page
= GetPage((size_t)pagePos
);
183 page
= GetCurrentPage();
186 if ( !page
|| !page
->GetEventHandler()->ProcessEvent(event
) )
193 // ----------------------------------------------------------------------------
195 // ----------------------------------------------------------------------------
198 wxBookCtrlBase::InsertPage(size_t nPage
,
200 const wxString
& WXUNUSED(text
),
201 bool WXUNUSED(bSelect
),
202 int WXUNUSED(imageId
))
204 wxCHECK_MSG( page
|| AllowNullPage(), false,
205 _T("NULL page in wxBookCtrlBase::InsertPage()") );
206 wxCHECK_MSG( nPage
<= m_pages
.size(), false,
207 _T("invalid page index in wxBookCtrlBase::InsertPage()") );
209 m_pages
.Insert(page
, nPage
);
211 page
->SetSize(GetPageRect());
213 InvalidateBestSize();
218 bool wxBookCtrlBase::DeletePage(size_t nPage
)
220 wxWindow
*page
= DoRemovePage(nPage
);
221 if ( !(page
|| AllowNullPage()) )
224 // delete NULL is harmless
230 wxWindow
*wxBookCtrlBase::DoRemovePage(size_t nPage
)
232 wxCHECK_MSG( nPage
< m_pages
.size(), NULL
,
233 _T("invalid page index in wxBookCtrlBase::DoRemovePage()") );
235 wxWindow
*pageRemoved
= m_pages
[nPage
];
236 m_pages
.RemoveAt(nPage
);
237 InvalidateBestSize();
242 int wxBookCtrlBase::GetNextPage(bool forward
) const
246 int nMax
= GetPageCount();
247 if ( nMax
-- ) // decrement it to get the last valid index
249 int nSel
= GetSelection();
251 // change selection wrapping if it becomes invalid
252 nPage
= forward
? nSel
== nMax
? 0
257 else // notebook is empty, no next page
265 wxRect
wxBookCtrlBase::GetPageRect() const
267 const wxSize size
= GetControllerSize();
270 wxRect
rectPage(pt
, GetClientSize());
272 switch ( GetWindowStyle() & wxBK_ALIGN_MASK
)
275 wxFAIL_MSG( _T("unexpected alignment") );
279 rectPage
.y
= size
.y
+ GetInternalBorder();
283 rectPage
.height
-= size
.y
+ GetInternalBorder();
287 rectPage
.x
= size
.x
+ GetInternalBorder();
291 rectPage
.width
-= size
.x
+ GetInternalBorder();
299 void wxBookCtrlBase::DoSize()
303 // we're not fully created yet or OnSize() should be hidden by derived class
311 // resize controller and the page area to fit inside our new size
312 const wxSize
sizeClient( GetClientSize() ),
313 sizeBorder( m_bookctrl
->GetSize() - m_bookctrl
->GetClientSize() ),
314 sizeCtrl( GetControllerSize() );
316 m_bookctrl
->SetClientSize( sizeCtrl
.x
- sizeBorder
.x
, sizeCtrl
.y
- sizeBorder
.y
);
318 const wxSize sizeNew
= m_bookctrl
->GetSize();
320 switch ( GetWindowStyle() & wxBK_ALIGN_MASK
)
323 wxFAIL_MSG( _T("unexpected alignment") );
328 // posCtrl is already ok
332 posCtrl
.y
= sizeClient
.y
- sizeNew
.y
;
336 posCtrl
.x
= sizeClient
.x
- sizeNew
.x
;
340 if ( m_bookctrl
->GetPosition() != posCtrl
)
341 m_bookctrl
->Move(posCtrl
);
344 // resize all pages to fit the new control size
345 const wxRect pageRect
= GetPageRect();
346 const unsigned pagesCount
= m_pages
.Count();
347 for ( unsigned int i
= 0; i
< pagesCount
; ++i
)
349 wxWindow
* const page
= m_pages
[i
];
352 wxASSERT_MSG( AllowNullPage(),
353 _T("Null page in a control that does not allow null pages?") );
357 page
->SetSize(pageRect
);
361 void wxBookCtrlBase::OnSize(wxSizeEvent
& event
)
368 wxSize
wxBookCtrlBase::GetControllerSize() const
373 const wxSize sizeClient
= GetClientSize(),
374 sizeBorder
= m_bookctrl
->GetSize() - m_bookctrl
->GetClientSize(),
375 sizeCtrl
= m_bookctrl
->GetBestSize() + sizeBorder
;
381 size
.x
= sizeClient
.x
;
384 else // left/right aligned
387 size
.y
= sizeClient
.y
;
393 #endif // wxUSE_BOOKCTRL