]>
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
)
47 // ----------------------------------------------------------------------------
48 // constructors and destructors
49 // ----------------------------------------------------------------------------
51 void wxBookCtrlBase::Init()
55 m_ownsImageList
= false;
56 m_fitToCurrentPage
= false;
58 #if defined(__WXWINCE__)
66 wxBookCtrlBase::Create(wxWindow
*parent
,
73 return wxControl::Create
85 wxBookCtrlBase::~wxBookCtrlBase()
87 if ( m_ownsImageList
)
94 // ----------------------------------------------------------------------------
96 // ----------------------------------------------------------------------------
98 void wxBookCtrlBase::SetImageList(wxImageList
*imageList
)
100 if ( m_ownsImageList
)
105 m_ownsImageList
= false;
108 m_imageList
= imageList
;
111 void wxBookCtrlBase::AssignImageList(wxImageList
* imageList
)
113 SetImageList(imageList
);
115 m_ownsImageList
= true;
118 // ----------------------------------------------------------------------------
120 // ----------------------------------------------------------------------------
122 void wxBookCtrlBase::SetPageSize(const wxSize
& size
)
124 SetClientSize(CalcSizeFromPage(size
));
127 wxSize
wxBookCtrlBase::DoGetBestSize() const
131 // iterate over all pages, get the largest width and height
132 const size_t nCount
= m_pages
.size();
133 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ )
135 const wxWindow
* const pPage
= m_pages
[nPage
];
138 wxSize
childBestSize(pPage
->GetBestSize());
140 if ( childBestSize
.x
> bestSize
.x
)
141 bestSize
.x
= childBestSize
.x
;
143 if ( childBestSize
.y
> bestSize
.y
)
144 bestSize
.y
= childBestSize
.y
;
148 if (m_fitToCurrentPage
&& GetCurrentPage())
149 bestSize
= GetCurrentPage()->GetBestSize();
151 // convert display area to window area, adding the size necessary for the
153 wxSize best
= CalcSizeFromPage(bestSize
);
158 // ----------------------------------------------------------------------------
160 // ----------------------------------------------------------------------------
163 wxBookCtrlBase::InsertPage(size_t nPage
,
165 const wxString
& WXUNUSED(text
),
166 bool WXUNUSED(bSelect
),
167 int WXUNUSED(imageId
))
169 wxCHECK_MSG( page
|| AllowNullPage(), false,
170 _T("NULL page in wxBookCtrlBase::InsertPage()") );
171 wxCHECK_MSG( nPage
<= m_pages
.size(), false,
172 _T("invalid page index in wxBookCtrlBase::InsertPage()") );
174 m_pages
.Insert(page
, nPage
);
175 InvalidateBestSize();
180 bool wxBookCtrlBase::DeletePage(size_t nPage
)
182 wxWindow
*page
= DoRemovePage(nPage
);
183 if ( !(page
|| AllowNullPage()) )
186 // delete NULL is harmless
192 wxWindow
*wxBookCtrlBase::DoRemovePage(size_t nPage
)
194 wxCHECK_MSG( nPage
< m_pages
.size(), NULL
,
195 _T("invalid page index in wxBookCtrlBase::DoRemovePage()") );
197 wxWindow
*pageRemoved
= m_pages
[nPage
];
198 m_pages
.RemoveAt(nPage
);
199 InvalidateBestSize();
204 int wxBookCtrlBase::GetNextPage(bool forward
) const
208 int nMax
= GetPageCount();
209 if ( nMax
-- ) // decrement it to get the last valid index
211 int nSel
= GetSelection();
213 // change selection wrapping if it becomes invalid
214 nPage
= forward
? nSel
== nMax
? 0
219 else // notebook is empty, no next page
227 wxRect
wxBookCtrlBase::GetPageRect() const
229 const wxSize size
= GetControllerSize();
232 wxRect
rectPage(pt
, GetClientSize());
233 switch ( GetWindowStyle() & wxBK_ALIGN_MASK
)
236 wxFAIL_MSG( _T("unexpected alignment") );
240 rectPage
.y
= size
.y
+ GetInternalBorder();
244 rectPage
.height
-= size
.y
+ GetInternalBorder();
248 rectPage
.x
= size
.x
+ GetInternalBorder();
252 rectPage
.width
-= size
.x
+ GetInternalBorder();
260 void wxBookCtrlBase::DoSize()
264 // we're not fully created yet or OnSize() should be hidden by derived class
268 // resize controller and the page area to fit inside our new size
269 const wxSize
sizeClient( GetClientSize() ),
270 sizeBorder( m_bookctrl
->GetSize() - m_bookctrl
->GetClientSize() ),
271 sizeCtrl( GetControllerSize() );
273 m_bookctrl
->SetClientSize( sizeCtrl
.x
- sizeBorder
.x
, sizeCtrl
.y
- sizeBorder
.y
);
275 const wxSize sizeNew
= m_bookctrl
->GetSize();
277 switch ( GetWindowStyle() & wxBK_ALIGN_MASK
)
280 wxFAIL_MSG( _T("unexpected alignment") );
285 // posCtrl is already ok
289 posCtrl
.y
= sizeClient
.y
- sizeNew
.y
;
293 posCtrl
.x
= sizeClient
.x
- sizeNew
.x
;
297 if ( m_bookctrl
->GetPosition() != posCtrl
)
298 m_bookctrl
->Move(posCtrl
);
300 // resize the currently shown page
301 if (GetSelection() != wxNOT_FOUND
)
303 wxWindow
*page
= m_pages
[GetSelection()];
304 wxCHECK_RET( page
, _T("NULL page?") );
305 page
->SetSize(GetPageRect());
309 void wxBookCtrlBase::OnSize(wxSizeEvent
& event
)
316 wxSize
wxBookCtrlBase::GetControllerSize() const
321 const wxSize sizeClient
= GetClientSize(),
322 sizeBorder
= m_bookctrl
->GetSize() - m_bookctrl
->GetClientSize(),
323 sizeCtrl
= m_bookctrl
->GetBestSize() + sizeBorder
;
329 size
.x
= sizeClient
.x
;
332 else // left/right aligned
335 size
.y
= sizeClient
.y
;
341 #endif // wxUSE_BOOKCTRL