]>
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;
57 #if defined(__WXWINCE__)
65 wxBookCtrlBase::Create(wxWindow
*parent
,
72 return wxControl::Create
84 wxBookCtrlBase::~wxBookCtrlBase()
86 if ( m_ownsImageList
)
93 // ----------------------------------------------------------------------------
95 // ----------------------------------------------------------------------------
97 void wxBookCtrlBase::SetImageList(wxImageList
*imageList
)
99 if ( m_ownsImageList
)
104 m_ownsImageList
= false;
107 m_imageList
= imageList
;
110 void wxBookCtrlBase::AssignImageList(wxImageList
* imageList
)
112 SetImageList(imageList
);
114 m_ownsImageList
= true;
117 // ----------------------------------------------------------------------------
119 // ----------------------------------------------------------------------------
121 void wxBookCtrlBase::SetPageSize(const wxSize
& size
)
123 SetClientSize(CalcSizeFromPage(size
));
126 wxSize
wxBookCtrlBase::DoGetBestSize() const
130 // iterate over all pages, get the largest width and height
131 const size_t nCount
= m_pages
.size();
132 for ( size_t nPage
= 0; nPage
< nCount
; nPage
++ )
134 const wxWindow
* const pPage
= m_pages
[nPage
];
137 wxSize
childBestSize(pPage
->GetBestSize());
139 if ( childBestSize
.x
> bestSize
.x
)
140 bestSize
.x
= childBestSize
.x
;
142 if ( childBestSize
.y
> bestSize
.y
)
143 bestSize
.y
= childBestSize
.y
;
147 // convert display area to window area, adding the size necessary for the
149 wxSize best
= CalcSizeFromPage(bestSize
);
154 // ----------------------------------------------------------------------------
156 // ----------------------------------------------------------------------------
159 wxBookCtrlBase::InsertPage(size_t nPage
,
161 const wxString
& WXUNUSED(text
),
162 bool WXUNUSED(bSelect
),
163 int WXUNUSED(imageId
))
165 wxCHECK_MSG( page
|| AllowNullPage(), false,
166 _T("NULL page in wxBookCtrlBase::InsertPage()") );
167 wxCHECK_MSG( nPage
<= m_pages
.size(), false,
168 _T("invalid page index in wxBookCtrlBase::InsertPage()") );
170 m_pages
.Insert(page
, nPage
);
171 InvalidateBestSize();
176 bool wxBookCtrlBase::DeletePage(size_t nPage
)
178 wxWindow
*page
= DoRemovePage(nPage
);
179 if ( !(page
|| AllowNullPage()) )
182 // delete NULL is harmless
188 wxWindow
*wxBookCtrlBase::DoRemovePage(size_t nPage
)
190 wxCHECK_MSG( nPage
< m_pages
.size(), NULL
,
191 _T("invalid page index in wxBookCtrlBase::DoRemovePage()") );
193 wxWindow
*pageRemoved
= m_pages
[nPage
];
194 m_pages
.RemoveAt(nPage
);
195 InvalidateBestSize();
200 int wxBookCtrlBase::GetNextPage(bool forward
) const
204 int nMax
= GetPageCount();
205 if ( nMax
-- ) // decrement it to get the last valid index
207 int nSel
= GetSelection();
209 // change selection wrapping if it becomes invalid
210 nPage
= forward
? nSel
== nMax
? 0
215 else // notebook is empty, no next page
223 wxRect
wxBookCtrlBase::GetPageRect() const
225 const wxSize size
= GetControllerSize();
228 wxRect
rectPage(pt
, GetClientSize());
229 switch ( GetWindowStyle() & wxBK_ALIGN_MASK
)
232 wxFAIL_MSG( _T("unexpected alignment") );
236 rectPage
.y
= size
.y
+ GetInternalBorder();
240 rectPage
.height
-= size
.y
+ GetInternalBorder();
244 rectPage
.x
= size
.x
+ GetInternalBorder();
248 rectPage
.width
-= size
.x
+ GetInternalBorder();
255 void wxBookCtrlBase::OnSize(wxSizeEvent
& event
)
261 // we're not fully created yet or OnSize() should be hidden by derived class
265 // resize controller and the page area to fit inside our new size
266 const wxSize
sizeClient( GetClientSize() ),
267 sizeBorder( m_bookctrl
->GetSize() - m_bookctrl
->GetClientSize() ),
268 sizeCtrl( GetControllerSize() );
270 m_bookctrl
->SetClientSize( sizeCtrl
.x
- sizeBorder
.x
, sizeCtrl
.y
- sizeBorder
.y
);
272 const wxSize sizeNew
= m_bookctrl
->GetSize();
274 switch ( GetWindowStyle() & wxBK_ALIGN_MASK
)
277 wxFAIL_MSG( _T("unexpected alignment") );
282 // posCtrl is already ok
286 posCtrl
.y
= sizeClient
.y
- sizeNew
.y
;
290 posCtrl
.x
= sizeClient
.x
- sizeNew
.x
;
294 if ( m_bookctrl
->GetPosition() != posCtrl
)
295 m_bookctrl
->Move(posCtrl
);
297 // resize the currently shown page
298 if (GetSelection() != wxNOT_FOUND
)
300 wxWindow
*page
= m_pages
[GetSelection()];
301 wxCHECK_RET( page
, _T("NULL page?") );
302 page
->SetSize(GetPageRect());
306 wxSize
wxBookCtrlBase::GetControllerSize() const
311 const wxSize sizeClient
= GetClientSize(),
312 sizeBorder
= m_bookctrl
->GetSize() - m_bookctrl
->GetClientSize(),
313 sizeCtrl
= m_bookctrl
->GetBestSize() + sizeBorder
;
319 size
.x
= sizeClient
.x
;
322 else // left/right aligned
325 size
.y
= sizeClient
.y
;
331 #endif // wxUSE_BOOKCTRL