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