]> git.saurik.com Git - wxWidgets.git/blob - src/common/bookctrl.cpp
minor tab height bug fix
[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 void wxBookCtrlBase::SetPageSize(const wxSize& size)
141 {
142 SetClientSize(CalcSizeFromPage(size));
143 }
144
145 wxSize wxBookCtrlBase::DoGetBestSize() const
146 {
147 wxSize bestSize;
148
149 // iterate over all pages, get the largest width and height
150 const size_t nCount = m_pages.size();
151 for ( size_t nPage = 0; nPage < nCount; nPage++ )
152 {
153 const wxWindow * const pPage = m_pages[nPage];
154 if( pPage )
155 {
156 wxSize childBestSize(pPage->GetBestSize());
157
158 if ( childBestSize.x > bestSize.x )
159 bestSize.x = childBestSize.x;
160
161 if ( childBestSize.y > bestSize.y )
162 bestSize.y = childBestSize.y;
163 }
164 }
165
166 if (m_fitToCurrentPage && GetCurrentPage())
167 bestSize = GetCurrentPage()->GetBestSize();
168
169 // convert display area to window area, adding the size necessary for the
170 // tabs
171 wxSize best = CalcSizeFromPage(bestSize);
172 CacheBestSize(best);
173 return best;
174 }
175
176 wxRect wxBookCtrlBase::GetPageRect() const
177 {
178 const wxSize size = GetControllerSize();
179
180 wxPoint pt;
181 wxRect rectPage(pt, GetClientSize());
182
183 switch ( GetWindowStyle() & wxBK_ALIGN_MASK )
184 {
185 default:
186 wxFAIL_MSG( _T("unexpected alignment") );
187 // fall through
188
189 case wxBK_TOP:
190 rectPage.y = size.y + GetInternalBorder();
191 // fall through
192
193 case wxBK_BOTTOM:
194 rectPage.height -= size.y + GetInternalBorder();
195 if (rectPage.height < 0)
196 rectPage.height = 0;
197 break;
198
199 case wxBK_LEFT:
200 rectPage.x = size.x + GetInternalBorder();
201 // fall through
202
203 case wxBK_RIGHT:
204 rectPage.width -= size.x + GetInternalBorder();
205 if (rectPage.width < 0)
206 rectPage.width = 0;
207 break;
208 }
209
210 return rectPage;
211 }
212
213 // Lay out controls
214 void wxBookCtrlBase::DoSize()
215 {
216 if ( !m_bookctrl )
217 {
218 // we're not fully created yet or OnSize() should be hidden by derived class
219 return;
220 }
221
222 if (GetSizer())
223 Layout();
224 else
225 {
226 // resize controller and the page area to fit inside our new size
227 const wxSize sizeClient( GetClientSize() ),
228 sizeBorder( m_bookctrl->GetSize() - m_bookctrl->GetClientSize() ),
229 sizeCtrl( GetControllerSize() );
230
231 m_bookctrl->SetClientSize( sizeCtrl.x - sizeBorder.x, sizeCtrl.y - sizeBorder.y );
232
233 const wxSize sizeNew = m_bookctrl->GetSize();
234 wxPoint posCtrl;
235 switch ( GetWindowStyle() & wxBK_ALIGN_MASK )
236 {
237 default:
238 wxFAIL_MSG( _T("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.Count();
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 _T("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 if(!m_bookctrl)
286 return wxSize(0,0);
287
288 const wxSize sizeClient = GetClientSize(),
289 sizeBorder = m_bookctrl->GetSize() - m_bookctrl->GetClientSize(),
290 sizeCtrl = m_bookctrl->GetBestSize() + sizeBorder;
291
292 wxSize size;
293
294 if ( IsVertical() )
295 {
296 size.x = sizeClient.x;
297 size.y = sizeCtrl.y;
298 }
299 else // left/right aligned
300 {
301 size.x = sizeCtrl.x;
302 size.y = sizeClient.y;
303 }
304
305 return size;
306 }
307
308 // ----------------------------------------------------------------------------
309 // miscellaneous stuff
310 // ----------------------------------------------------------------------------
311
312 #if wxUSE_HELP
313
314 void wxBookCtrlBase::OnHelp(wxHelpEvent& event)
315 {
316 // determine where does this even originate from to avoid redirecting it
317 // back to the page which generated it (resulting in an infinite loop)
318
319 // notice that we have to check in the hard(er) way instead of just testing
320 // if the event object == this because the book control can have other
321 // subcontrols inside it (e.g. wxSpinButton in case of a notebook in wxUniv)
322 wxWindow *source = wxStaticCast(event.GetEventObject(), wxWindow);
323 while ( source && source != this && source->GetParent() != this )
324 {
325 source = source->GetParent();
326 }
327
328 if ( source && m_pages.Index(source) == wxNOT_FOUND )
329 {
330 // this event is for the book control itself, redirect it to the
331 // corresponding page
332 wxWindow *page = NULL;
333
334 if ( event.GetOrigin() == wxHelpEvent::Origin_HelpButton )
335 {
336 // show help for the page under the mouse
337 const int pagePos = HitTest(ScreenToClient(event.GetPosition()));
338
339 if ( pagePos != wxNOT_FOUND)
340 {
341 page = GetPage((size_t)pagePos);
342 }
343 }
344 else // event from keyboard or unknown source
345 {
346 // otherwise show the current page help
347 page = GetCurrentPage();
348 }
349
350 if ( page )
351 {
352 // change event object to the page to avoid infinite recursion if
353 // we get this event ourselves if the page doesn't handle it
354 event.SetEventObject(page);
355
356 if ( page->GetEventHandler()->ProcessEvent(event) )
357 {
358 // don't call event.Skip()
359 return;
360 }
361 }
362 }
363 //else: event coming from one of our pages already
364
365 event.Skip();
366 }
367
368 #endif // wxUSE_HELP
369
370 // ----------------------------------------------------------------------------
371 // pages management
372 // ----------------------------------------------------------------------------
373
374 bool
375 wxBookCtrlBase::InsertPage(size_t nPage,
376 wxWindow *page,
377 const wxString& WXUNUSED(text),
378 bool WXUNUSED(bSelect),
379 int WXUNUSED(imageId))
380 {
381 wxCHECK_MSG( page || AllowNullPage(), false,
382 _T("NULL page in wxBookCtrlBase::InsertPage()") );
383 wxCHECK_MSG( nPage <= m_pages.size(), false,
384 _T("invalid page index in wxBookCtrlBase::InsertPage()") );
385
386 m_pages.Insert(page, nPage);
387 if ( page )
388 page->SetSize(GetPageRect());
389
390 DoInvalidateBestSize();
391
392 return true;
393 }
394
395 bool wxBookCtrlBase::DeletePage(size_t nPage)
396 {
397 wxWindow *page = DoRemovePage(nPage);
398 if ( !(page || AllowNullPage()) )
399 return false;
400
401 // delete NULL is harmless
402 delete page;
403
404 return true;
405 }
406
407 wxWindow *wxBookCtrlBase::DoRemovePage(size_t nPage)
408 {
409 wxCHECK_MSG( nPage < m_pages.size(), NULL,
410 _T("invalid page index in wxBookCtrlBase::DoRemovePage()") );
411
412 wxWindow *pageRemoved = m_pages[nPage];
413 m_pages.RemoveAt(nPage);
414 DoInvalidateBestSize();
415
416 return pageRemoved;
417 }
418
419 int wxBookCtrlBase::GetNextPage(bool forward) const
420 {
421 int nPage;
422
423 int nMax = GetPageCount();
424 if ( nMax-- ) // decrement it to get the last valid index
425 {
426 int nSel = GetSelection();
427
428 // change selection wrapping if it becomes invalid
429 nPage = forward ? nSel == nMax ? 0
430 : nSel + 1
431 : nSel == 0 ? nMax
432 : nSel - 1;
433 }
434 else // notebook is empty, no next page
435 {
436 nPage = wxNOT_FOUND;
437 }
438
439 return nPage;
440 }
441
442 int wxBookCtrlBase::DoSetSelection(size_t n, int flags)
443 {
444 wxCHECK_MSG( n < GetPageCount(), wxNOT_FOUND,
445 wxT("invalid page index in wxBookCtrlBase::DoSetSelection()") );
446
447 const int oldSel = GetSelection();
448
449 if ( n != (size_t)oldSel )
450 {
451 wxBookCtrlBaseEvent *event = CreatePageChangingEvent();
452 bool allowed = false;
453
454 if ( flags & SetSelection_SendEvent )
455 {
456 event->SetSelection(n);
457 event->SetOldSelection(oldSel);
458 event->SetEventObject(this);
459
460 allowed = !GetEventHandler()->ProcessEvent(*event) || event->IsAllowed();
461 }
462
463 if ( !(flags & SetSelection_SendEvent) || allowed)
464 {
465 if ( oldSel != wxNOT_FOUND )
466 m_pages[oldSel]->Hide();
467
468 wxWindow *page = m_pages[n];
469 page->SetSize(GetPageRect());
470 page->Show();
471
472 // change selection now to ignore the selection change event
473 UpdateSelectedPage(n);
474
475 if ( flags & SetSelection_SendEvent )
476 {
477 // program allows the page change
478 MakeChangedEvent(*event);
479 (void)GetEventHandler()->ProcessEvent(*event);
480 }
481 }
482
483 delete event;
484 }
485
486 return oldSel;
487 }
488
489
490 #endif // wxUSE_BOOKCTRL