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