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