]>
Commit | Line | Data |
---|---|---|
15aad3b9 | 1 | /////////////////////////////////////////////////////////////////////////////// |
2ddb4d13 | 2 | // Name: src/common/bookctrl.cpp |
61c083e7 | 3 | // Purpose: wxBookCtrlBase implementation |
15aad3b9 VZ |
4 | // Author: Vadim Zeitlin |
5 | // Modified by: | |
6 | // Created: 19.08.03 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org> | |
65571936 | 9 | // Licence: wxWindows licence |
15aad3b9 VZ |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
15aad3b9 VZ |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | #if wxUSE_BOOKCTRL | |
28 | ||
29 | #include "wx/imaglist.h" | |
30 | ||
31 | #include "wx/bookctrl.h" | |
32 | ||
33 | // ============================================================================ | |
34 | // implementation | |
35 | // ============================================================================ | |
36 | ||
2ddb4d13 WS |
37 | // ---------------------------------------------------------------------------- |
38 | // event table | |
39 | // ---------------------------------------------------------------------------- | |
40 | ||
41 | IMPLEMENT_ABSTRACT_CLASS(wxBookCtrlBase, wxControl) | |
42 | ||
43 | BEGIN_EVENT_TABLE(wxBookCtrlBase, wxControl) | |
44 | EVT_SIZE(wxBookCtrlBase::OnSize) | |
45 | END_EVENT_TABLE() | |
46 | ||
15aad3b9 VZ |
47 | // ---------------------------------------------------------------------------- |
48 | // constructors and destructors | |
49 | // ---------------------------------------------------------------------------- | |
50 | ||
61c083e7 | 51 | void wxBookCtrlBase::Init() |
15aad3b9 | 52 | { |
2ddb4d13 | 53 | m_bookctrl = NULL; |
15aad3b9 VZ |
54 | m_imageList = NULL; |
55 | m_ownsImageList = false; | |
da817fa6 | 56 | m_fitToCurrentPage = false; |
159e6235 WS |
57 | |
58 | #if defined(__WXWINCE__) | |
59 | m_internalBorder = 1; | |
60 | #else | |
61 | m_internalBorder = 5; | |
62 | #endif | |
87cf52d8 JS |
63 | |
64 | m_controlMargin = 0; | |
65 | m_controlSizer = NULL; | |
15aad3b9 VZ |
66 | } |
67 | ||
68 | bool | |
61c083e7 | 69 | wxBookCtrlBase::Create(wxWindow *parent, |
15aad3b9 VZ |
70 | wxWindowID id, |
71 | const wxPoint& pos, | |
72 | const wxSize& size, | |
73 | long style, | |
74 | const wxString& name) | |
75 | { | |
76 | return wxControl::Create | |
77 | ( | |
78 | parent, | |
79 | id, | |
80 | pos, | |
81 | size, | |
82 | style, | |
83 | wxDefaultValidator, | |
84 | name | |
85 | ); | |
86 | } | |
87 | ||
61c083e7 | 88 | wxBookCtrlBase::~wxBookCtrlBase() |
15aad3b9 VZ |
89 | { |
90 | if ( m_ownsImageList ) | |
91 | { | |
92 | // may be NULL, ok | |
93 | delete m_imageList; | |
94 | } | |
95 | } | |
96 | ||
97 | // ---------------------------------------------------------------------------- | |
98 | // image list | |
99 | // ---------------------------------------------------------------------------- | |
100 | ||
61c083e7 | 101 | void wxBookCtrlBase::SetImageList(wxImageList *imageList) |
15aad3b9 VZ |
102 | { |
103 | if ( m_ownsImageList ) | |
104 | { | |
105 | // may be NULL, ok | |
106 | delete m_imageList; | |
107 | ||
108 | m_ownsImageList = false; | |
109 | } | |
110 | ||
111 | m_imageList = imageList; | |
112 | } | |
113 | ||
61c083e7 | 114 | void wxBookCtrlBase::AssignImageList(wxImageList* imageList) |
15aad3b9 VZ |
115 | { |
116 | SetImageList(imageList); | |
117 | ||
118 | m_ownsImageList = true; | |
119 | } | |
120 | ||
121 | // ---------------------------------------------------------------------------- | |
122 | // geometry | |
123 | // ---------------------------------------------------------------------------- | |
124 | ||
61c083e7 | 125 | void wxBookCtrlBase::SetPageSize(const wxSize& size) |
15aad3b9 VZ |
126 | { |
127 | SetClientSize(CalcSizeFromPage(size)); | |
128 | } | |
129 | ||
61c083e7 | 130 | wxSize wxBookCtrlBase::DoGetBestSize() const |
15aad3b9 VZ |
131 | { |
132 | wxSize bestSize; | |
133 | ||
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++ ) | |
137 | { | |
eca15c0d VZ |
138 | const wxWindow * const pPage = m_pages[nPage]; |
139 | if( pPage ) | |
140 | { | |
141 | wxSize childBestSize(pPage->GetBestSize()); | |
15aad3b9 | 142 | |
eca15c0d VZ |
143 | if ( childBestSize.x > bestSize.x ) |
144 | bestSize.x = childBestSize.x; | |
15aad3b9 | 145 | |
eca15c0d VZ |
146 | if ( childBestSize.y > bestSize.y ) |
147 | bestSize.y = childBestSize.y; | |
148 | } | |
15aad3b9 | 149 | } |
a717bd11 | 150 | |
da817fa6 | 151 | if (m_fitToCurrentPage && GetCurrentPage()) |
93bfe545 | 152 | bestSize = GetCurrentPage()->GetBestSize(); |
15aad3b9 | 153 | |
3103e8a9 | 154 | // convert display area to window area, adding the size necessary for the |
15aad3b9 | 155 | // tabs |
9f884528 RD |
156 | wxSize best = CalcSizeFromPage(bestSize); |
157 | CacheBestSize(best); | |
158 | return best; | |
15aad3b9 VZ |
159 | } |
160 | ||
161 | // ---------------------------------------------------------------------------- | |
162 | // pages management | |
163 | // ---------------------------------------------------------------------------- | |
164 | ||
165 | bool | |
61c083e7 WS |
166 | wxBookCtrlBase::InsertPage(size_t nPage, |
167 | wxWindow *page, | |
168 | const wxString& WXUNUSED(text), | |
169 | bool WXUNUSED(bSelect), | |
170 | int WXUNUSED(imageId)) | |
15aad3b9 | 171 | { |
2ddb4d13 WS |
172 | wxCHECK_MSG( page || AllowNullPage(), false, |
173 | _T("NULL page in wxBookCtrlBase::InsertPage()") ); | |
712f40ca | 174 | wxCHECK_MSG( nPage <= m_pages.size(), false, |
61c083e7 | 175 | _T("invalid page index in wxBookCtrlBase::InsertPage()") ); |
15aad3b9 VZ |
176 | |
177 | m_pages.Insert(page, nPage); | |
a717bd11 VZ |
178 | if ( page ) |
179 | page->SetSize(GetPageRect()); | |
180 | ||
37144cf0 | 181 | InvalidateBestSize(); |
c9d59ee7 | 182 | |
15aad3b9 VZ |
183 | return true; |
184 | } | |
185 | ||
61c083e7 | 186 | bool wxBookCtrlBase::DeletePage(size_t nPage) |
15aad3b9 VZ |
187 | { |
188 | wxWindow *page = DoRemovePage(nPage); | |
eca15c0d | 189 | if ( !(page || AllowNullPage()) ) |
15aad3b9 VZ |
190 | return false; |
191 | ||
eca15c0d | 192 | // delete NULL is harmless |
15aad3b9 VZ |
193 | delete page; |
194 | ||
195 | return true; | |
196 | } | |
197 | ||
61c083e7 | 198 | wxWindow *wxBookCtrlBase::DoRemovePage(size_t nPage) |
15aad3b9 VZ |
199 | { |
200 | wxCHECK_MSG( nPage < m_pages.size(), NULL, | |
61c083e7 | 201 | _T("invalid page index in wxBookCtrlBase::DoRemovePage()") ); |
15aad3b9 VZ |
202 | |
203 | wxWindow *pageRemoved = m_pages[nPage]; | |
204 | m_pages.RemoveAt(nPage); | |
37144cf0 | 205 | InvalidateBestSize(); |
15aad3b9 VZ |
206 | |
207 | return pageRemoved; | |
208 | } | |
209 | ||
61c083e7 | 210 | int wxBookCtrlBase::GetNextPage(bool forward) const |
15aad3b9 VZ |
211 | { |
212 | int nPage; | |
213 | ||
214 | int nMax = GetPageCount(); | |
215 | if ( nMax-- ) // decrement it to get the last valid index | |
216 | { | |
217 | int nSel = GetSelection(); | |
218 | ||
219 | // change selection wrapping if it becomes invalid | |
220 | nPage = forward ? nSel == nMax ? 0 | |
221 | : nSel + 1 | |
222 | : nSel == 0 ? nMax | |
223 | : nSel - 1; | |
224 | } | |
225 | else // notebook is empty, no next page | |
226 | { | |
159e6235 | 227 | nPage = wxNOT_FOUND; |
15aad3b9 VZ |
228 | } |
229 | ||
230 | return nPage; | |
231 | } | |
232 | ||
2ddb4d13 WS |
233 | wxRect wxBookCtrlBase::GetPageRect() const |
234 | { | |
235 | const wxSize size = GetControllerSize(); | |
236 | ||
237 | wxPoint pt; | |
238 | wxRect rectPage(pt, GetClientSize()); | |
239 | switch ( GetWindowStyle() & wxBK_ALIGN_MASK ) | |
240 | { | |
241 | default: | |
242 | wxFAIL_MSG( _T("unexpected alignment") ); | |
243 | // fall through | |
244 | ||
245 | case wxBK_TOP: | |
246 | rectPage.y = size.y + GetInternalBorder(); | |
247 | // fall through | |
248 | ||
249 | case wxBK_BOTTOM: | |
250 | rectPage.height -= size.y + GetInternalBorder(); | |
251 | break; | |
252 | ||
253 | case wxBK_LEFT: | |
254 | rectPage.x = size.x + GetInternalBorder(); | |
255 | // fall through | |
256 | ||
257 | case wxBK_RIGHT: | |
258 | rectPage.width -= size.x + GetInternalBorder(); | |
259 | break; | |
260 | } | |
261 | ||
262 | return rectPage; | |
263 | } | |
264 | ||
233387bd JS |
265 | // Lay out controls |
266 | void wxBookCtrlBase::DoSize() | |
2ddb4d13 | 267 | { |
2ddb4d13 WS |
268 | if ( !m_bookctrl ) |
269 | { | |
270 | // we're not fully created yet or OnSize() should be hidden by derived class | |
271 | return; | |
272 | } | |
a717bd11 | 273 | |
87cf52d8 JS |
274 | if (GetSizer()) |
275 | Layout(); | |
276 | else | |
2ddb4d13 | 277 | { |
87cf52d8 JS |
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() ); | |
2ddb4d13 | 282 | |
87cf52d8 | 283 | m_bookctrl->SetClientSize( sizeCtrl.x - sizeBorder.x, sizeCtrl.y - sizeBorder.y ); |
2ddb4d13 | 284 | |
87cf52d8 JS |
285 | const wxSize sizeNew = m_bookctrl->GetSize(); |
286 | wxPoint posCtrl; | |
287 | switch ( GetWindowStyle() & wxBK_ALIGN_MASK ) | |
288 | { | |
289 | default: | |
290 | wxFAIL_MSG( _T("unexpected alignment") ); | |
291 | // fall through | |
292 | ||
293 | case wxBK_TOP: | |
294 | case wxBK_LEFT: | |
295 | // posCtrl is already ok | |
296 | break; | |
297 | ||
298 | case wxBK_BOTTOM: | |
299 | posCtrl.y = sizeClient.y - sizeNew.y; | |
300 | break; | |
301 | ||
302 | case wxBK_RIGHT: | |
303 | posCtrl.x = sizeClient.x - sizeNew.x; | |
304 | break; | |
305 | } | |
2ddb4d13 | 306 | |
87cf52d8 JS |
307 | if ( m_bookctrl->GetPosition() != posCtrl ) |
308 | m_bookctrl->Move(posCtrl); | |
2ddb4d13 WS |
309 | } |
310 | ||
a717bd11 VZ |
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 ) | |
2ddb4d13 | 315 | { |
a717bd11 VZ |
316 | wxWindow * const page = m_pages[i]; |
317 | if ( !page ) | |
318 | { | |
319 | wxASSERT_MSG( AllowNullPage(), | |
320 | _T("Null page in a control that does not allow null pages?") ); | |
321 | continue; | |
322 | } | |
323 | ||
324 | page->SetSize(pageRect); | |
2ddb4d13 WS |
325 | } |
326 | } | |
327 | ||
233387bd JS |
328 | void wxBookCtrlBase::OnSize(wxSizeEvent& event) |
329 | { | |
330 | event.Skip(); | |
a717bd11 | 331 | |
233387bd JS |
332 | DoSize(); |
333 | } | |
334 | ||
2ddb4d13 WS |
335 | wxSize wxBookCtrlBase::GetControllerSize() const |
336 | { | |
337 | if(!m_bookctrl) | |
338 | return wxSize(0,0); | |
339 | ||
340 | const wxSize sizeClient = GetClientSize(), | |
341 | sizeBorder = m_bookctrl->GetSize() - m_bookctrl->GetClientSize(), | |
342 | sizeCtrl = m_bookctrl->GetBestSize() + sizeBorder; | |
343 | ||
344 | wxSize size; | |
345 | ||
346 | if ( IsVertical() ) | |
347 | { | |
348 | size.x = sizeClient.x; | |
349 | size.y = sizeCtrl.y; | |
350 | } | |
351 | else // left/right aligned | |
352 | { | |
353 | size.x = sizeCtrl.x; | |
354 | size.y = sizeClient.y; | |
355 | } | |
356 | ||
357 | return size; | |
358 | } | |
359 | ||
15aad3b9 | 360 | #endif // wxUSE_BOOKCTRL |