]> git.saurik.com Git - wxWidgets.git/blob - src/common/bookctrl.cpp
Fix typo in last commit
[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_imageList = NULL;
59 m_ownsImageList = false;
60 m_fitToCurrentPage = false;
61
62 #if defined(__WXWINCE__)
63 m_internalBorder = 1;
64 #else
65 m_internalBorder = 5;
66 #endif
67
68 m_controlMargin = 0;
69 m_controlSizer = NULL;
70 }
71
72 bool
73 wxBookCtrlBase::Create(wxWindow *parent,
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
92 wxBookCtrlBase::~wxBookCtrlBase()
93 {
94 if ( m_ownsImageList )
95 {
96 // may be NULL, ok
97 delete m_imageList;
98 }
99 }
100
101 // ----------------------------------------------------------------------------
102 // image list
103 // ----------------------------------------------------------------------------
104
105 void wxBookCtrlBase::SetImageList(wxImageList *imageList)
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
118 void wxBookCtrlBase::AssignImageList(wxImageList* imageList)
119 {
120 SetImageList(imageList);
121
122 m_ownsImageList = true;
123 }
124
125 // ----------------------------------------------------------------------------
126 // geometry
127 // ----------------------------------------------------------------------------
128
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
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
163 void wxBookCtrlBase::SetPageSize(const wxSize& size)
164 {
165 SetClientSize(CalcSizeFromPage(size));
166 }
167
168 wxSize wxBookCtrlBase::DoGetBestSize() const
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 {
176 const wxWindow * const pPage = m_pages[nPage];
177 if( pPage )
178 {
179 wxSize childBestSize(pPage->GetBestSize());
180
181 if ( childBestSize.x > bestSize.x )
182 bestSize.x = childBestSize.x;
183
184 if ( childBestSize.y > bestSize.y )
185 bestSize.y = childBestSize.y;
186 }
187 }
188
189 if (m_fitToCurrentPage && GetCurrentPage())
190 bestSize = GetCurrentPage()->GetBestSize();
191
192 // convert display area to window area, adding the size necessary for the
193 // tabs
194 wxSize best = CalcSizeFromPage(bestSize);
195 CacheBestSize(best);
196 return best;
197 }
198
199 wxRect wxBookCtrlBase::GetPageRect() const
200 {
201 const wxSize size = GetControllerSize();
202
203 wxPoint pt;
204 wxRect rectPage(pt, GetClientSize());
205
206 switch ( GetWindowStyle() & wxBK_ALIGN_MASK )
207 {
208 default:
209 wxFAIL_MSG( wxT("unexpected alignment") );
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();
218 if (rectPage.height < 0)
219 rectPage.height = 0;
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();
228 if (rectPage.width < 0)
229 rectPage.width = 0;
230 break;
231 }
232
233 return rectPage;
234 }
235
236 // Lay out controls
237 void wxBookCtrlBase::DoSize()
238 {
239 if ( !m_bookctrl )
240 {
241 // we're not fully created yet or OnSize() should be hidden by derived class
242 return;
243 }
244
245 if (GetSizer())
246 Layout();
247 else
248 {
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() );
253
254 m_bookctrl->SetClientSize( sizeCtrl.x - sizeBorder.x, sizeCtrl.y - sizeBorder.y );
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 }
262
263 const wxSize sizeNew = m_bookctrl->GetSize();
264 wxPoint posCtrl;
265 switch ( GetWindowStyle() & wxBK_ALIGN_MASK )
266 {
267 default:
268 wxFAIL_MSG( wxT("unexpected alignment") );
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 }
284
285 if ( m_bookctrl->GetPosition() != posCtrl )
286 m_bookctrl->Move(posCtrl);
287 }
288
289 // resize all pages to fit the new control size
290 const wxRect pageRect = GetPageRect();
291 const unsigned pagesCount = m_pages.GetCount();
292 for ( unsigned int i = 0; i < pagesCount; ++i )
293 {
294 wxWindow * const page = m_pages[i];
295 if ( !page )
296 {
297 wxASSERT_MSG( AllowNullPage(),
298 wxT("Null page in a control that does not allow null pages?") );
299 continue;
300 }
301
302 page->SetSize(pageRect);
303 }
304 }
305
306 void wxBookCtrlBase::OnSize(wxSizeEvent& event)
307 {
308 event.Skip();
309
310 DoSize();
311 }
312
313 wxSize wxBookCtrlBase::GetControllerSize() const
314 {
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() )
319 return wxSize(0, 0);
320
321 const wxSize sizeClient = GetClientSize(),
322 sizeCtrl = m_bookctrl->GetBestSize();
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
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,
414 wxT("NULL page in wxBookCtrlBase::InsertPage()") );
415 wxCHECK_MSG( nPage <= m_pages.size(), false,
416 wxT("invalid page index in wxBookCtrlBase::InsertPage()") );
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,
442 wxT("invalid page index in wxBookCtrlBase::DoRemovePage()") );
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
474 bool wxBookCtrlBase::DoSetSelectionAfterInsertion(size_t n, bool bSelect)
475 {
476 if ( bSelect )
477 SetSelection(n);
478 else if ( m_selection == wxNOT_FOUND )
479 ChangeSelection(0);
480 else // We're not going to select this page.
481 return false;
482
483 // Return true to indicate that we selected this page.
484 return true;
485 }
486
487 int wxBookCtrlBase::DoSetSelection(size_t n, int flags)
488 {
489 wxCHECK_MSG( n < GetPageCount(), wxNOT_FOUND,
490 wxT("invalid page index in wxBookCtrlBase::DoSetSelection()") );
491
492 const int oldSel = GetSelection();
493
494 if ( n != (size_t)oldSel )
495 {
496 wxBookCtrlEvent *event = CreatePageChangingEvent();
497 bool allowed = false;
498
499 if ( flags & SetSelection_SendEvent )
500 {
501 event->SetSelection(n);
502 event->SetOldSelection(oldSel);
503 event->SetEventObject(this);
504
505 allowed = !GetEventHandler()->ProcessEvent(*event) || event->IsAllowed();
506 }
507
508 if ( !(flags & SetSelection_SendEvent) || allowed)
509 {
510 if ( oldSel != wxNOT_FOUND )
511 m_pages[oldSel]->Hide();
512
513 wxWindow *page = m_pages[n];
514 page->SetSize(GetPageRect());
515 page->Show();
516
517 // change selection now to ignore the selection change event
518 UpdateSelectedPage(n);
519
520 if ( flags & SetSelection_SendEvent )
521 {
522 // program allows the page change
523 MakeChangedEvent(*event);
524 (void)GetEventHandler()->ProcessEvent(*event);
525 }
526 }
527
528 delete event;
529 }
530
531 return oldSel;
532 }
533
534 IMPLEMENT_DYNAMIC_CLASS(wxBookCtrlEvent, wxNotifyEvent)
535
536 #endif // wxUSE_BOOKCTRL