]>
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) | |
a18c21f0 VZ |
45 | #if wxUSE_HELP |
46 | EVT_HELP(wxID_ANY, wxBookCtrlBase::OnHelp) | |
47 | #endif // wxUSE_HELP | |
2ddb4d13 WS |
48 | END_EVENT_TABLE() |
49 | ||
15aad3b9 VZ |
50 | // ---------------------------------------------------------------------------- |
51 | // constructors and destructors | |
52 | // ---------------------------------------------------------------------------- | |
53 | ||
61c083e7 | 54 | void wxBookCtrlBase::Init() |
15aad3b9 | 55 | { |
2ddb4d13 | 56 | m_bookctrl = NULL; |
15aad3b9 VZ |
57 | m_imageList = NULL; |
58 | m_ownsImageList = false; | |
da817fa6 | 59 | m_fitToCurrentPage = false; |
159e6235 WS |
60 | |
61 | #if defined(__WXWINCE__) | |
62 | m_internalBorder = 1; | |
63 | #else | |
64 | m_internalBorder = 5; | |
65 | #endif | |
87cf52d8 JS |
66 | |
67 | m_controlMargin = 0; | |
68 | m_controlSizer = NULL; | |
15aad3b9 VZ |
69 | } |
70 | ||
71 | bool | |
61c083e7 | 72 | wxBookCtrlBase::Create(wxWindow *parent, |
15aad3b9 VZ |
73 | wxWindowID id, |
74 | const wxPoint& pos, | |
75 | const wxSize& size, | |
76 | long style, | |
77 | const wxString& name) | |
78 | { | |
79 | return wxControl::Create | |
80 | ( | |
81 | parent, | |
82 | id, | |
83 | pos, | |
84 | size, | |
85 | style, | |
86 | wxDefaultValidator, | |
87 | name | |
88 | ); | |
89 | } | |
90 | ||
61c083e7 | 91 | wxBookCtrlBase::~wxBookCtrlBase() |
15aad3b9 VZ |
92 | { |
93 | if ( m_ownsImageList ) | |
94 | { | |
95 | // may be NULL, ok | |
96 | delete m_imageList; | |
97 | } | |
98 | } | |
99 | ||
100 | // ---------------------------------------------------------------------------- | |
101 | // image list | |
102 | // ---------------------------------------------------------------------------- | |
103 | ||
61c083e7 | 104 | void wxBookCtrlBase::SetImageList(wxImageList *imageList) |
15aad3b9 VZ |
105 | { |
106 | if ( m_ownsImageList ) | |
107 | { | |
108 | // may be NULL, ok | |
109 | delete m_imageList; | |
110 | ||
111 | m_ownsImageList = false; | |
112 | } | |
113 | ||
114 | m_imageList = imageList; | |
115 | } | |
116 | ||
61c083e7 | 117 | void wxBookCtrlBase::AssignImageList(wxImageList* imageList) |
15aad3b9 VZ |
118 | { |
119 | SetImageList(imageList); | |
120 | ||
121 | m_ownsImageList = true; | |
122 | } | |
123 | ||
124 | // ---------------------------------------------------------------------------- | |
125 | // geometry | |
126 | // ---------------------------------------------------------------------------- | |
127 | ||
61c083e7 | 128 | void wxBookCtrlBase::SetPageSize(const wxSize& size) |
15aad3b9 VZ |
129 | { |
130 | SetClientSize(CalcSizeFromPage(size)); | |
131 | } | |
132 | ||
61c083e7 | 133 | wxSize wxBookCtrlBase::DoGetBestSize() const |
15aad3b9 VZ |
134 | { |
135 | wxSize bestSize; | |
136 | ||
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++ ) | |
140 | { | |
eca15c0d VZ |
141 | const wxWindow * const pPage = m_pages[nPage]; |
142 | if( pPage ) | |
143 | { | |
144 | wxSize childBestSize(pPage->GetBestSize()); | |
15aad3b9 | 145 | |
eca15c0d VZ |
146 | if ( childBestSize.x > bestSize.x ) |
147 | bestSize.x = childBestSize.x; | |
15aad3b9 | 148 | |
eca15c0d VZ |
149 | if ( childBestSize.y > bestSize.y ) |
150 | bestSize.y = childBestSize.y; | |
151 | } | |
15aad3b9 | 152 | } |
a717bd11 | 153 | |
da817fa6 | 154 | if (m_fitToCurrentPage && GetCurrentPage()) |
93bfe545 | 155 | bestSize = GetCurrentPage()->GetBestSize(); |
15aad3b9 | 156 | |
3103e8a9 | 157 | // convert display area to window area, adding the size necessary for the |
15aad3b9 | 158 | // tabs |
9f884528 RD |
159 | wxSize best = CalcSizeFromPage(bestSize); |
160 | CacheBestSize(best); | |
161 | return best; | |
15aad3b9 VZ |
162 | } |
163 | ||
a18c21f0 VZ |
164 | #if wxUSE_HELP |
165 | void wxBookCtrlBase::OnHelp(wxHelpEvent& event) | |
166 | { | |
167 | // find the corresponding page | |
168 | wxWindow *page = NULL; | |
169 | ||
170 | if ( event.GetOrigin() == wxHelpEvent::Origin_HelpButton ) | |
171 | { | |
172 | // show help for the page under the mouse | |
173 | const int pagePos = HitTest(ScreenToClient(event.GetPosition())); | |
174 | ||
175 | if ( pagePos != wxNOT_FOUND) | |
176 | { | |
177 | page = GetPage((size_t)pagePos); | |
178 | } | |
179 | } | |
180 | ||
181 | if ( !page ) | |
182 | { | |
183 | page = GetCurrentPage(); | |
184 | } | |
185 | ||
186 | if ( !page || !page->GetEventHandler()->ProcessEvent(event) ) | |
187 | { | |
188 | event.Skip(); | |
189 | } | |
190 | } | |
191 | #endif // wxUSE_HELP | |
192 | ||
15aad3b9 VZ |
193 | // ---------------------------------------------------------------------------- |
194 | // pages management | |
195 | // ---------------------------------------------------------------------------- | |
196 | ||
197 | bool | |
61c083e7 WS |
198 | wxBookCtrlBase::InsertPage(size_t nPage, |
199 | wxWindow *page, | |
200 | const wxString& WXUNUSED(text), | |
201 | bool WXUNUSED(bSelect), | |
202 | int WXUNUSED(imageId)) | |
15aad3b9 | 203 | { |
2ddb4d13 WS |
204 | wxCHECK_MSG( page || AllowNullPage(), false, |
205 | _T("NULL page in wxBookCtrlBase::InsertPage()") ); | |
712f40ca | 206 | wxCHECK_MSG( nPage <= m_pages.size(), false, |
61c083e7 | 207 | _T("invalid page index in wxBookCtrlBase::InsertPage()") ); |
15aad3b9 VZ |
208 | |
209 | m_pages.Insert(page, nPage); | |
a717bd11 VZ |
210 | if ( page ) |
211 | page->SetSize(GetPageRect()); | |
212 | ||
37144cf0 | 213 | InvalidateBestSize(); |
c9d59ee7 | 214 | |
15aad3b9 VZ |
215 | return true; |
216 | } | |
217 | ||
61c083e7 | 218 | bool wxBookCtrlBase::DeletePage(size_t nPage) |
15aad3b9 VZ |
219 | { |
220 | wxWindow *page = DoRemovePage(nPage); | |
eca15c0d | 221 | if ( !(page || AllowNullPage()) ) |
15aad3b9 VZ |
222 | return false; |
223 | ||
eca15c0d | 224 | // delete NULL is harmless |
15aad3b9 VZ |
225 | delete page; |
226 | ||
227 | return true; | |
228 | } | |
229 | ||
61c083e7 | 230 | wxWindow *wxBookCtrlBase::DoRemovePage(size_t nPage) |
15aad3b9 VZ |
231 | { |
232 | wxCHECK_MSG( nPage < m_pages.size(), NULL, | |
61c083e7 | 233 | _T("invalid page index in wxBookCtrlBase::DoRemovePage()") ); |
15aad3b9 VZ |
234 | |
235 | wxWindow *pageRemoved = m_pages[nPage]; | |
236 | m_pages.RemoveAt(nPage); | |
37144cf0 | 237 | InvalidateBestSize(); |
15aad3b9 VZ |
238 | |
239 | return pageRemoved; | |
240 | } | |
241 | ||
61c083e7 | 242 | int wxBookCtrlBase::GetNextPage(bool forward) const |
15aad3b9 VZ |
243 | { |
244 | int nPage; | |
245 | ||
246 | int nMax = GetPageCount(); | |
247 | if ( nMax-- ) // decrement it to get the last valid index | |
248 | { | |
249 | int nSel = GetSelection(); | |
250 | ||
251 | // change selection wrapping if it becomes invalid | |
252 | nPage = forward ? nSel == nMax ? 0 | |
253 | : nSel + 1 | |
254 | : nSel == 0 ? nMax | |
255 | : nSel - 1; | |
256 | } | |
257 | else // notebook is empty, no next page | |
258 | { | |
159e6235 | 259 | nPage = wxNOT_FOUND; |
15aad3b9 VZ |
260 | } |
261 | ||
262 | return nPage; | |
263 | } | |
264 | ||
2ddb4d13 WS |
265 | wxRect wxBookCtrlBase::GetPageRect() const |
266 | { | |
267 | const wxSize size = GetControllerSize(); | |
268 | ||
269 | wxPoint pt; | |
270 | wxRect rectPage(pt, GetClientSize()); | |
271 | switch ( GetWindowStyle() & wxBK_ALIGN_MASK ) | |
272 | { | |
273 | default: | |
274 | wxFAIL_MSG( _T("unexpected alignment") ); | |
275 | // fall through | |
276 | ||
277 | case wxBK_TOP: | |
278 | rectPage.y = size.y + GetInternalBorder(); | |
279 | // fall through | |
280 | ||
281 | case wxBK_BOTTOM: | |
282 | rectPage.height -= size.y + GetInternalBorder(); | |
283 | break; | |
284 | ||
285 | case wxBK_LEFT: | |
286 | rectPage.x = size.x + GetInternalBorder(); | |
287 | // fall through | |
288 | ||
289 | case wxBK_RIGHT: | |
290 | rectPage.width -= size.x + GetInternalBorder(); | |
291 | break; | |
292 | } | |
293 | ||
294 | return rectPage; | |
295 | } | |
296 | ||
233387bd JS |
297 | // Lay out controls |
298 | void wxBookCtrlBase::DoSize() | |
2ddb4d13 | 299 | { |
2ddb4d13 WS |
300 | if ( !m_bookctrl ) |
301 | { | |
302 | // we're not fully created yet or OnSize() should be hidden by derived class | |
303 | return; | |
304 | } | |
a717bd11 | 305 | |
87cf52d8 JS |
306 | if (GetSizer()) |
307 | Layout(); | |
308 | else | |
2ddb4d13 | 309 | { |
87cf52d8 JS |
310 | // resize controller and the page area to fit inside our new size |
311 | const wxSize sizeClient( GetClientSize() ), | |
312 | sizeBorder( m_bookctrl->GetSize() - m_bookctrl->GetClientSize() ), | |
313 | sizeCtrl( GetControllerSize() ); | |
2ddb4d13 | 314 | |
87cf52d8 | 315 | m_bookctrl->SetClientSize( sizeCtrl.x - sizeBorder.x, sizeCtrl.y - sizeBorder.y ); |
2ddb4d13 | 316 | |
87cf52d8 JS |
317 | const wxSize sizeNew = m_bookctrl->GetSize(); |
318 | wxPoint posCtrl; | |
319 | switch ( GetWindowStyle() & wxBK_ALIGN_MASK ) | |
320 | { | |
321 | default: | |
322 | wxFAIL_MSG( _T("unexpected alignment") ); | |
323 | // fall through | |
324 | ||
325 | case wxBK_TOP: | |
326 | case wxBK_LEFT: | |
327 | // posCtrl is already ok | |
328 | break; | |
329 | ||
330 | case wxBK_BOTTOM: | |
331 | posCtrl.y = sizeClient.y - sizeNew.y; | |
332 | break; | |
333 | ||
334 | case wxBK_RIGHT: | |
335 | posCtrl.x = sizeClient.x - sizeNew.x; | |
336 | break; | |
337 | } | |
2ddb4d13 | 338 | |
87cf52d8 JS |
339 | if ( m_bookctrl->GetPosition() != posCtrl ) |
340 | m_bookctrl->Move(posCtrl); | |
2ddb4d13 WS |
341 | } |
342 | ||
a717bd11 VZ |
343 | // resize all pages to fit the new control size |
344 | const wxRect pageRect = GetPageRect(); | |
345 | const unsigned pagesCount = m_pages.Count(); | |
346 | for ( unsigned int i = 0; i < pagesCount; ++i ) | |
2ddb4d13 | 347 | { |
a717bd11 VZ |
348 | wxWindow * const page = m_pages[i]; |
349 | if ( !page ) | |
350 | { | |
351 | wxASSERT_MSG( AllowNullPage(), | |
352 | _T("Null page in a control that does not allow null pages?") ); | |
353 | continue; | |
354 | } | |
355 | ||
356 | page->SetSize(pageRect); | |
2ddb4d13 WS |
357 | } |
358 | } | |
359 | ||
233387bd JS |
360 | void wxBookCtrlBase::OnSize(wxSizeEvent& event) |
361 | { | |
362 | event.Skip(); | |
a717bd11 | 363 | |
233387bd JS |
364 | DoSize(); |
365 | } | |
366 | ||
2ddb4d13 WS |
367 | wxSize wxBookCtrlBase::GetControllerSize() const |
368 | { | |
369 | if(!m_bookctrl) | |
370 | return wxSize(0,0); | |
371 | ||
372 | const wxSize sizeClient = GetClientSize(), | |
373 | sizeBorder = m_bookctrl->GetSize() - m_bookctrl->GetClientSize(), | |
374 | sizeCtrl = m_bookctrl->GetBestSize() + sizeBorder; | |
375 | ||
376 | wxSize size; | |
377 | ||
378 | if ( IsVertical() ) | |
379 | { | |
380 | size.x = sizeClient.x; | |
381 | size.y = sizeCtrl.y; | |
382 | } | |
383 | else // left/right aligned | |
384 | { | |
385 | size.x = sizeCtrl.x; | |
386 | size.y = sizeClient.y; | |
387 | } | |
388 | ||
389 | return size; | |
390 | } | |
391 | ||
15aad3b9 | 392 | #endif // wxUSE_BOOKCTRL |