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