]>
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 | { |
681be2ef | 56 | m_selection = wxNOT_FOUND; |
2ddb4d13 | 57 | m_bookctrl = NULL; |
da817fa6 | 58 | m_fitToCurrentPage = false; |
159e6235 WS |
59 | |
60 | #if defined(__WXWINCE__) | |
61 | m_internalBorder = 1; | |
62 | #else | |
63 | m_internalBorder = 5; | |
64 | #endif | |
87cf52d8 JS |
65 | |
66 | m_controlMargin = 0; | |
67 | m_controlSizer = NULL; | |
15aad3b9 VZ |
68 | } |
69 | ||
70 | bool | |
61c083e7 | 71 | wxBookCtrlBase::Create(wxWindow *parent, |
15aad3b9 VZ |
72 | wxWindowID id, |
73 | const wxPoint& pos, | |
74 | const wxSize& size, | |
75 | long style, | |
76 | const wxString& name) | |
77 | { | |
78 | return wxControl::Create | |
79 | ( | |
80 | parent, | |
81 | id, | |
82 | pos, | |
83 | size, | |
84 | style, | |
85 | wxDefaultValidator, | |
86 | name | |
87 | ); | |
88 | } | |
89 | ||
15aad3b9 VZ |
90 | // ---------------------------------------------------------------------------- |
91 | // geometry | |
92 | // ---------------------------------------------------------------------------- | |
93 | ||
e8a147a6 VZ |
94 | void wxBookCtrlBase::DoInvalidateBestSize() |
95 | { | |
96 | // notice that it is not necessary to invalidate our own best size | |
97 | // explicitly if we have m_bookctrl as it will already invalidate the best | |
98 | // size of its parent when its own size is invalidated and its parent is | |
99 | // this control | |
100 | if ( m_bookctrl ) | |
101 | m_bookctrl->InvalidateBestSize(); | |
102 | else | |
103 | wxControl::InvalidateBestSize(); | |
104 | } | |
105 | ||
175363f6 VZ |
106 | wxSize wxBookCtrlBase::CalcSizeFromPage(const wxSize& sizePage) const |
107 | { | |
402dae7b | 108 | // Add the size of the controller and the border between if it's shown. |
91452726 VZ |
109 | if ( !m_bookctrl || !m_bookctrl->IsShown() ) |
110 | return sizePage; | |
111 | ||
402dae7b | 112 | const wxSize sizeController = GetControllerSize(); |
175363f6 VZ |
113 | |
114 | wxSize size = sizePage; | |
115 | if ( IsVertical() ) | |
116 | { | |
117 | if ( sizeController.x > sizePage.x ) | |
118 | size.x = sizeController.x; | |
119 | size.y += sizeController.y + GetInternalBorder(); | |
120 | } | |
121 | else // left/right aligned | |
122 | { | |
123 | size.x += sizeController.x + GetInternalBorder(); | |
124 | if ( sizeController.y > sizePage.y ) | |
125 | size.y = sizeController.y; | |
126 | } | |
127 | ||
128 | return size; | |
129 | } | |
130 | ||
61c083e7 | 131 | void wxBookCtrlBase::SetPageSize(const wxSize& size) |
15aad3b9 VZ |
132 | { |
133 | SetClientSize(CalcSizeFromPage(size)); | |
134 | } | |
135 | ||
61c083e7 | 136 | wxSize wxBookCtrlBase::DoGetBestSize() const |
15aad3b9 VZ |
137 | { |
138 | wxSize bestSize; | |
139 | ||
140 | // iterate over all pages, get the largest width and height | |
141 | const size_t nCount = m_pages.size(); | |
142 | for ( size_t nPage = 0; nPage < nCount; nPage++ ) | |
143 | { | |
eca15c0d VZ |
144 | const wxWindow * const pPage = m_pages[nPage]; |
145 | if( pPage ) | |
146 | { | |
147 | wxSize childBestSize(pPage->GetBestSize()); | |
15aad3b9 | 148 | |
eca15c0d VZ |
149 | if ( childBestSize.x > bestSize.x ) |
150 | bestSize.x = childBestSize.x; | |
15aad3b9 | 151 | |
eca15c0d VZ |
152 | if ( childBestSize.y > bestSize.y ) |
153 | bestSize.y = childBestSize.y; | |
154 | } | |
15aad3b9 | 155 | } |
a717bd11 | 156 | |
da817fa6 | 157 | if (m_fitToCurrentPage && GetCurrentPage()) |
93bfe545 | 158 | bestSize = GetCurrentPage()->GetBestSize(); |
15aad3b9 | 159 | |
3103e8a9 | 160 | // convert display area to window area, adding the size necessary for the |
15aad3b9 | 161 | // tabs |
9f884528 RD |
162 | wxSize best = CalcSizeFromPage(bestSize); |
163 | CacheBestSize(best); | |
164 | return best; | |
15aad3b9 VZ |
165 | } |
166 | ||
2ddb4d13 WS |
167 | wxRect wxBookCtrlBase::GetPageRect() const |
168 | { | |
169 | const wxSize size = GetControllerSize(); | |
170 | ||
171 | wxPoint pt; | |
172 | wxRect rectPage(pt, GetClientSize()); | |
926395e1 | 173 | |
90f9b8ef | 174 | switch ( GetWindowStyle() & wxBK_ALIGN_MASK ) |
2ddb4d13 WS |
175 | { |
176 | default: | |
9a83f860 | 177 | wxFAIL_MSG( wxT("unexpected alignment") ); |
2ddb4d13 WS |
178 | // fall through |
179 | ||
180 | case wxBK_TOP: | |
181 | rectPage.y = size.y + GetInternalBorder(); | |
182 | // fall through | |
183 | ||
184 | case wxBK_BOTTOM: | |
185 | rectPage.height -= size.y + GetInternalBorder(); | |
6a9e44d5 PC |
186 | if (rectPage.height < 0) |
187 | rectPage.height = 0; | |
2ddb4d13 WS |
188 | break; |
189 | ||
190 | case wxBK_LEFT: | |
191 | rectPage.x = size.x + GetInternalBorder(); | |
192 | // fall through | |
193 | ||
194 | case wxBK_RIGHT: | |
195 | rectPage.width -= size.x + GetInternalBorder(); | |
6a9e44d5 PC |
196 | if (rectPage.width < 0) |
197 | rectPage.width = 0; | |
2ddb4d13 WS |
198 | break; |
199 | } | |
200 | ||
201 | return rectPage; | |
202 | } | |
203 | ||
233387bd JS |
204 | // Lay out controls |
205 | void wxBookCtrlBase::DoSize() | |
2ddb4d13 | 206 | { |
2ddb4d13 WS |
207 | if ( !m_bookctrl ) |
208 | { | |
209 | // we're not fully created yet or OnSize() should be hidden by derived class | |
210 | return; | |
211 | } | |
a717bd11 | 212 | |
87cf52d8 JS |
213 | if (GetSizer()) |
214 | Layout(); | |
215 | else | |
2ddb4d13 | 216 | { |
87cf52d8 JS |
217 | // resize controller and the page area to fit inside our new size |
218 | const wxSize sizeClient( GetClientSize() ), | |
219 | sizeBorder( m_bookctrl->GetSize() - m_bookctrl->GetClientSize() ), | |
220 | sizeCtrl( GetControllerSize() ); | |
2ddb4d13 | 221 | |
87cf52d8 | 222 | m_bookctrl->SetClientSize( sizeCtrl.x - sizeBorder.x, sizeCtrl.y - sizeBorder.y ); |
ecac22d0 SC |
223 | // if this changes the visibility of the scrollbars the best size changes, relayout in this case |
224 | wxSize sizeCtrl2 = GetControllerSize(); | |
225 | if ( sizeCtrl != sizeCtrl2 ) | |
226 | { | |
227 | wxSize sizeBorder2 = m_bookctrl->GetSize() - m_bookctrl->GetClientSize(); | |
228 | m_bookctrl->SetClientSize( sizeCtrl2.x - sizeBorder2.x, sizeCtrl2.y - sizeBorder2.y ); | |
229 | } | |
2ddb4d13 | 230 | |
87cf52d8 JS |
231 | const wxSize sizeNew = m_bookctrl->GetSize(); |
232 | wxPoint posCtrl; | |
233 | switch ( GetWindowStyle() & wxBK_ALIGN_MASK ) | |
234 | { | |
235 | default: | |
9a83f860 | 236 | wxFAIL_MSG( wxT("unexpected alignment") ); |
87cf52d8 JS |
237 | // fall through |
238 | ||
239 | case wxBK_TOP: | |
240 | case wxBK_LEFT: | |
241 | // posCtrl is already ok | |
242 | break; | |
243 | ||
244 | case wxBK_BOTTOM: | |
245 | posCtrl.y = sizeClient.y - sizeNew.y; | |
246 | break; | |
247 | ||
248 | case wxBK_RIGHT: | |
249 | posCtrl.x = sizeClient.x - sizeNew.x; | |
250 | break; | |
251 | } | |
2ddb4d13 | 252 | |
87cf52d8 JS |
253 | if ( m_bookctrl->GetPosition() != posCtrl ) |
254 | m_bookctrl->Move(posCtrl); | |
2ddb4d13 WS |
255 | } |
256 | ||
a717bd11 VZ |
257 | // resize all pages to fit the new control size |
258 | const wxRect pageRect = GetPageRect(); | |
b4a980f4 | 259 | const unsigned pagesCount = m_pages.GetCount(); |
a717bd11 | 260 | for ( unsigned int i = 0; i < pagesCount; ++i ) |
2ddb4d13 | 261 | { |
a717bd11 VZ |
262 | wxWindow * const page = m_pages[i]; |
263 | if ( !page ) | |
264 | { | |
265 | wxASSERT_MSG( AllowNullPage(), | |
9a83f860 | 266 | wxT("Null page in a control that does not allow null pages?") ); |
a717bd11 VZ |
267 | continue; |
268 | } | |
269 | ||
270 | page->SetSize(pageRect); | |
2ddb4d13 WS |
271 | } |
272 | } | |
273 | ||
233387bd JS |
274 | void wxBookCtrlBase::OnSize(wxSizeEvent& event) |
275 | { | |
276 | event.Skip(); | |
a717bd11 | 277 | |
233387bd JS |
278 | DoSize(); |
279 | } | |
280 | ||
2ddb4d13 WS |
281 | wxSize wxBookCtrlBase::GetControllerSize() const |
282 | { | |
448ca228 VZ |
283 | // For at least some book controls (e.g. wxChoicebook) it may make sense to |
284 | // (temporarily?) hide the controller and we shouldn't leave extra space | |
285 | // for the hidden control in this case. | |
286 | if ( !m_bookctrl || !m_bookctrl->IsShown() ) | |
1d2b7f06 | 287 | return wxSize(0, 0); |
2ddb4d13 | 288 | |
402dae7b | 289 | const wxSize sizeClient = GetClientSize(); |
2ddb4d13 WS |
290 | |
291 | wxSize size; | |
292 | ||
402dae7b | 293 | // Ask for the best width/height considering the other direction. |
2ddb4d13 WS |
294 | if ( IsVertical() ) |
295 | { | |
296 | size.x = sizeClient.x; | |
402dae7b | 297 | size.y = m_bookctrl->GetBestHeight(sizeClient.x); |
2ddb4d13 WS |
298 | } |
299 | else // left/right aligned | |
300 | { | |
402dae7b | 301 | size.x = m_bookctrl->GetBestWidth(sizeClient.y); |
2ddb4d13 WS |
302 | size.y = sizeClient.y; |
303 | } | |
304 | ||
305 | return size; | |
306 | } | |
307 | ||
e8a147a6 VZ |
308 | // ---------------------------------------------------------------------------- |
309 | // miscellaneous stuff | |
310 | // ---------------------------------------------------------------------------- | |
311 | ||
312 | #if wxUSE_HELP | |
313 | ||
314 | void wxBookCtrlBase::OnHelp(wxHelpEvent& event) | |
315 | { | |
316 | // determine where does this even originate from to avoid redirecting it | |
317 | // back to the page which generated it (resulting in an infinite loop) | |
318 | ||
319 | // notice that we have to check in the hard(er) way instead of just testing | |
320 | // if the event object == this because the book control can have other | |
321 | // subcontrols inside it (e.g. wxSpinButton in case of a notebook in wxUniv) | |
322 | wxWindow *source = wxStaticCast(event.GetEventObject(), wxWindow); | |
323 | while ( source && source != this && source->GetParent() != this ) | |
324 | { | |
325 | source = source->GetParent(); | |
326 | } | |
327 | ||
328 | if ( source && m_pages.Index(source) == wxNOT_FOUND ) | |
329 | { | |
330 | // this event is for the book control itself, redirect it to the | |
331 | // corresponding page | |
332 | wxWindow *page = NULL; | |
333 | ||
334 | if ( event.GetOrigin() == wxHelpEvent::Origin_HelpButton ) | |
335 | { | |
336 | // show help for the page under the mouse | |
337 | const int pagePos = HitTest(ScreenToClient(event.GetPosition())); | |
338 | ||
339 | if ( pagePos != wxNOT_FOUND) | |
340 | { | |
341 | page = GetPage((size_t)pagePos); | |
342 | } | |
343 | } | |
344 | else // event from keyboard or unknown source | |
345 | { | |
346 | // otherwise show the current page help | |
347 | page = GetCurrentPage(); | |
348 | } | |
349 | ||
350 | if ( page ) | |
351 | { | |
352 | // change event object to the page to avoid infinite recursion if | |
353 | // we get this event ourselves if the page doesn't handle it | |
354 | event.SetEventObject(page); | |
355 | ||
356 | if ( page->GetEventHandler()->ProcessEvent(event) ) | |
357 | { | |
358 | // don't call event.Skip() | |
359 | return; | |
360 | } | |
361 | } | |
362 | } | |
363 | //else: event coming from one of our pages already | |
364 | ||
365 | event.Skip(); | |
366 | } | |
367 | ||
368 | #endif // wxUSE_HELP | |
369 | ||
370 | // ---------------------------------------------------------------------------- | |
371 | // pages management | |
372 | // ---------------------------------------------------------------------------- | |
373 | ||
374 | bool | |
375 | wxBookCtrlBase::InsertPage(size_t nPage, | |
376 | wxWindow *page, | |
377 | const wxString& WXUNUSED(text), | |
378 | bool WXUNUSED(bSelect), | |
379 | int WXUNUSED(imageId)) | |
380 | { | |
381 | wxCHECK_MSG( page || AllowNullPage(), false, | |
9a83f860 | 382 | wxT("NULL page in wxBookCtrlBase::InsertPage()") ); |
e8a147a6 | 383 | wxCHECK_MSG( nPage <= m_pages.size(), false, |
9a83f860 | 384 | wxT("invalid page index in wxBookCtrlBase::InsertPage()") ); |
e8a147a6 VZ |
385 | |
386 | m_pages.Insert(page, nPage); | |
387 | if ( page ) | |
388 | page->SetSize(GetPageRect()); | |
389 | ||
390 | DoInvalidateBestSize(); | |
391 | ||
392 | return true; | |
393 | } | |
394 | ||
395 | bool wxBookCtrlBase::DeletePage(size_t nPage) | |
396 | { | |
397 | wxWindow *page = DoRemovePage(nPage); | |
398 | if ( !(page || AllowNullPage()) ) | |
399 | return false; | |
400 | ||
401 | // delete NULL is harmless | |
402 | delete page; | |
403 | ||
404 | return true; | |
405 | } | |
406 | ||
407 | wxWindow *wxBookCtrlBase::DoRemovePage(size_t nPage) | |
408 | { | |
409 | wxCHECK_MSG( nPage < m_pages.size(), NULL, | |
9a83f860 | 410 | wxT("invalid page index in wxBookCtrlBase::DoRemovePage()") ); |
e8a147a6 VZ |
411 | |
412 | wxWindow *pageRemoved = m_pages[nPage]; | |
413 | m_pages.RemoveAt(nPage); | |
414 | DoInvalidateBestSize(); | |
415 | ||
416 | return pageRemoved; | |
417 | } | |
418 | ||
419 | int wxBookCtrlBase::GetNextPage(bool forward) const | |
420 | { | |
421 | int nPage; | |
422 | ||
423 | int nMax = GetPageCount(); | |
424 | if ( nMax-- ) // decrement it to get the last valid index | |
425 | { | |
426 | int nSel = GetSelection(); | |
427 | ||
428 | // change selection wrapping if it becomes invalid | |
429 | nPage = forward ? nSel == nMax ? 0 | |
430 | : nSel + 1 | |
431 | : nSel == 0 ? nMax | |
432 | : nSel - 1; | |
433 | } | |
434 | else // notebook is empty, no next page | |
435 | { | |
436 | nPage = wxNOT_FOUND; | |
437 | } | |
438 | ||
439 | return nPage; | |
440 | } | |
441 | ||
60d5c563 VZ |
442 | bool wxBookCtrlBase::DoSetSelectionAfterInsertion(size_t n, bool bSelect) |
443 | { | |
444 | if ( bSelect ) | |
445 | SetSelection(n); | |
446 | else if ( m_selection == wxNOT_FOUND ) | |
447 | ChangeSelection(0); | |
448 | else // We're not going to select this page. | |
449 | return false; | |
450 | ||
451 | // Return true to indicate that we selected this page. | |
452 | return true; | |
453 | } | |
454 | ||
deb325e3 | 455 | int wxBookCtrlBase::DoSetSelection(size_t n, int flags) |
1d6fcbcc VZ |
456 | { |
457 | wxCHECK_MSG( n < GetPageCount(), wxNOT_FOUND, | |
458 | wxT("invalid page index in wxBookCtrlBase::DoSetSelection()") ); | |
459 | ||
460 | const int oldSel = GetSelection(); | |
461 | ||
51ad652f | 462 | if ( n != (size_t)oldSel ) |
1d6fcbcc | 463 | { |
3e97a905 | 464 | wxBookCtrlEvent *event = CreatePageChangingEvent(); |
1d6fcbcc VZ |
465 | bool allowed = false; |
466 | ||
467 | if ( flags & SetSelection_SendEvent ) | |
468 | { | |
deb325e3 VZ |
469 | event->SetSelection(n); |
470 | event->SetOldSelection(oldSel); | |
471 | event->SetEventObject(this); | |
1d6fcbcc | 472 | |
deb325e3 | 473 | allowed = !GetEventHandler()->ProcessEvent(*event) || event->IsAllowed(); |
1d6fcbcc VZ |
474 | } |
475 | ||
476 | if ( !(flags & SetSelection_SendEvent) || allowed) | |
477 | { | |
478 | if ( oldSel != wxNOT_FOUND ) | |
2e18fe71 | 479 | DoShowPage(m_pages[oldSel], false); |
1d6fcbcc VZ |
480 | |
481 | wxWindow *page = m_pages[n]; | |
482 | page->SetSize(GetPageRect()); | |
2e18fe71 | 483 | DoShowPage(page, true); |
1d6fcbcc VZ |
484 | |
485 | // change selection now to ignore the selection change event | |
486 | UpdateSelectedPage(n); | |
487 | ||
488 | if ( flags & SetSelection_SendEvent ) | |
489 | { | |
490 | // program allows the page change | |
deb325e3 VZ |
491 | MakeChangedEvent(*event); |
492 | (void)GetEventHandler()->ProcessEvent(*event); | |
1d6fcbcc VZ |
493 | } |
494 | } | |
deb325e3 VZ |
495 | |
496 | delete event; | |
1d6fcbcc VZ |
497 | } |
498 | ||
499 | return oldSel; | |
500 | } | |
501 | ||
3e97a905 | 502 | IMPLEMENT_DYNAMIC_CLASS(wxBookCtrlEvent, wxNotifyEvent) |
1d6fcbcc | 503 | |
15aad3b9 | 504 | #endif // wxUSE_BOOKCTRL |