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