]>
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__)
65 m_controlSizer
= NULL
;
69 wxBookCtrlBase::Create(wxWindow
*parent
,
76 return wxControl::Create
88 wxBookCtrlBase::~wxBookCtrlBase()
90 if ( m_ownsImageList
)
97 // ----------------------------------------------------------------------------
99 // ----------------------------------------------------------------------------
101 void wxBookCtrlBase::SetImageList(wxImageList
*imageList
)
103 if ( m_ownsImageList
)
108 m_ownsImageList
= false;
111 m_imageList
= imageList
;
114 void wxBookCtrlBase::AssignImageList(wxImageList
* imageList
)
116 SetImageList(imageList
);
118 m_ownsImageList
= true;
121 // ----------------------------------------------------------------------------
123 // ----------------------------------------------------------------------------
125 void wxBookCtrlBase::SetPageSize(const wxSize
& size
)
127 SetClientSize(CalcSizeFromPage(size
));
130 wxSize
wxBookCtrlBase::DoGetBestSize() const
134 // iterate over all pages, get the largest width and height
135 const size_t nCount
= m_pages
.size();
136 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ )
138 const wxWindow
* const pPage
= m_pages
[nPage
];
141 wxSize
childBestSize(pPage
->GetBestSize());
143 if ( childBestSize
.x
> bestSize
.x
)
144 bestSize
.x
= childBestSize
.x
;
146 if ( childBestSize
.y
> bestSize
.y
)
147 bestSize
.y
= childBestSize
.y
;
151 if (m_fitToCurrentPage
&& GetCurrentPage())
152 bestSize
= GetCurrentPage()->GetBestSize();
154 // convert display area to window area, adding the size necessary for the
156 wxSize best
= CalcSizeFromPage(bestSize
);
161 // ----------------------------------------------------------------------------
163 // ----------------------------------------------------------------------------
166 wxBookCtrlBase::InsertPage(size_t nPage
,
168 const wxString
& WXUNUSED(text
),
169 bool WXUNUSED(bSelect
),
170 int WXUNUSED(imageId
))
172 wxCHECK_MSG( page
|| AllowNullPage(), false,
173 _T("NULL page in wxBookCtrlBase::InsertPage()") );
174 wxCHECK_MSG( nPage
<= m_pages
.size(), false,
175 _T("invalid page index in wxBookCtrlBase::InsertPage()") );
177 m_pages
.Insert(page
, nPage
);
179 page
->SetSize(GetPageRect());
181 InvalidateBestSize();
186 bool wxBookCtrlBase::DeletePage(size_t nPage
)
188 wxWindow
*page
= DoRemovePage(nPage
);
189 if ( !(page
|| AllowNullPage()) )
192 // delete NULL is harmless
198 wxWindow
*wxBookCtrlBase::DoRemovePage(size_t nPage
)
200 wxCHECK_MSG( nPage
< m_pages
.size(), NULL
,
201 _T("invalid page index in wxBookCtrlBase::DoRemovePage()") );
203 wxWindow
*pageRemoved
= m_pages
[nPage
];
204 m_pages
.RemoveAt(nPage
);
205 InvalidateBestSize();
210 int wxBookCtrlBase::GetNextPage(bool forward
) const
214 int nMax
= GetPageCount();
215 if ( nMax
-- ) // decrement it to get the last valid index
217 int nSel
= GetSelection();
219 // change selection wrapping if it becomes invalid
220 nPage
= forward
? nSel
== nMax
? 0
225 else // notebook is empty, no next page
233 wxRect
wxBookCtrlBase::GetPageRect() const
235 const wxSize size
= GetControllerSize();
238 wxRect
rectPage(pt
, GetClientSize());
239 switch ( GetWindowStyle() & wxBK_ALIGN_MASK
)
242 wxFAIL_MSG( _T("unexpected alignment") );
246 rectPage
.y
= size
.y
+ GetInternalBorder();
250 rectPage
.height
-= size
.y
+ GetInternalBorder();
254 rectPage
.x
= size
.x
+ GetInternalBorder();
258 rectPage
.width
-= size
.x
+ GetInternalBorder();
266 void wxBookCtrlBase::DoSize()
270 // we're not fully created yet or OnSize() should be hidden by derived class
278 // resize controller and the page area to fit inside our new size
279 const wxSize
sizeClient( GetClientSize() ),
280 sizeBorder( m_bookctrl
->GetSize() - m_bookctrl
->GetClientSize() ),
281 sizeCtrl( GetControllerSize() );
283 m_bookctrl
->SetClientSize( sizeCtrl
.x
- sizeBorder
.x
, sizeCtrl
.y
- sizeBorder
.y
);
285 const wxSize sizeNew
= m_bookctrl
->GetSize();
287 switch ( GetWindowStyle() & wxBK_ALIGN_MASK
)
290 wxFAIL_MSG( _T("unexpected alignment") );
295 // posCtrl is already ok
299 posCtrl
.y
= sizeClient
.y
- sizeNew
.y
;
303 posCtrl
.x
= sizeClient
.x
- sizeNew
.x
;
307 if ( m_bookctrl
->GetPosition() != posCtrl
)
308 m_bookctrl
->Move(posCtrl
);
311 // resize all pages to fit the new control size
312 const wxRect pageRect
= GetPageRect();
313 const unsigned pagesCount
= m_pages
.Count();
314 for ( unsigned int i
= 0; i
< pagesCount
; ++i
)
316 wxWindow
* const page
= m_pages
[i
];
319 wxASSERT_MSG( AllowNullPage(),
320 _T("Null page in a control that does not allow null pages?") );
324 page
->SetSize(pageRect
);
328 void wxBookCtrlBase::OnSize(wxSizeEvent
& event
)
335 wxSize
wxBookCtrlBase::GetControllerSize() const
340 const wxSize sizeClient
= GetClientSize(),
341 sizeBorder
= m_bookctrl
->GetSize() - m_bookctrl
->GetClientSize(),
342 sizeCtrl
= m_bookctrl
->GetBestSize() + sizeBorder
;
348 size
.x
= sizeClient
.x
;
351 else // left/right aligned
354 size
.y
= sizeClient
.y
;
360 #endif // wxUSE_BOOKCTRL