]>
git.saurik.com Git - wxWidgets.git/blob - src/common/bookctrl.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/bookctrl.cpp
3 // Purpose: wxBookCtrlBase implementation
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/imaglist.h"
31 #include "wx/bookctrl.h"
33 // ============================================================================
35 // ============================================================================
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 IMPLEMENT_ABSTRACT_CLASS(wxBookCtrlBase
, wxControl
)
43 BEGIN_EVENT_TABLE(wxBookCtrlBase
, wxControl
)
44 EVT_SIZE(wxBookCtrlBase::OnSize
)
46 EVT_HELP(wxID_ANY
, wxBookCtrlBase::OnHelp
)
50 // ----------------------------------------------------------------------------
51 // constructors and destructors
52 // ----------------------------------------------------------------------------
54 void wxBookCtrlBase::Init()
58 m_ownsImageList
= false;
59 m_fitToCurrentPage
= false;
61 #if defined(__WXWINCE__)
68 m_controlSizer
= NULL
;
72 wxBookCtrlBase::Create(wxWindow
*parent
,
79 return wxControl::Create
91 wxBookCtrlBase::~wxBookCtrlBase()
93 if ( m_ownsImageList
)
100 // ----------------------------------------------------------------------------
102 // ----------------------------------------------------------------------------
104 void wxBookCtrlBase::SetImageList(wxImageList
*imageList
)
106 if ( m_ownsImageList
)
111 m_ownsImageList
= false;
114 m_imageList
= imageList
;
117 void wxBookCtrlBase::AssignImageList(wxImageList
* imageList
)
119 SetImageList(imageList
);
121 m_ownsImageList
= true;
124 // ----------------------------------------------------------------------------
126 // ----------------------------------------------------------------------------
128 void wxBookCtrlBase::DoInvalidateBestSize()
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
135 m_bookctrl
->InvalidateBestSize();
137 wxControl::InvalidateBestSize();
140 void wxBookCtrlBase::SetPageSize(const wxSize
& size
)
142 SetClientSize(CalcSizeFromPage(size
));
145 wxSize
wxBookCtrlBase::DoGetBestSize() const
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
++ )
153 const wxWindow
* const pPage
= m_pages
[nPage
];
156 wxSize
childBestSize(pPage
->GetBestSize());
158 if ( childBestSize
.x
> bestSize
.x
)
159 bestSize
.x
= childBestSize
.x
;
161 if ( childBestSize
.y
> bestSize
.y
)
162 bestSize
.y
= childBestSize
.y
;
166 if (m_fitToCurrentPage
&& GetCurrentPage())
167 bestSize
= GetCurrentPage()->GetBestSize();
169 // convert display area to window area, adding the size necessary for the
171 wxSize best
= CalcSizeFromPage(bestSize
);
176 wxRect
wxBookCtrlBase::GetPageRect() const
178 const wxSize size
= GetControllerSize();
181 wxRect
rectPage(pt
, GetClientSize());
183 switch ( GetWindowStyle() & wxBK_ALIGN_MASK
)
186 wxFAIL_MSG( wxT("unexpected alignment") );
190 rectPage
.y
= size
.y
+ GetInternalBorder();
194 rectPage
.height
-= size
.y
+ GetInternalBorder();
195 if (rectPage
.height
< 0)
200 rectPage
.x
= size
.x
+ GetInternalBorder();
204 rectPage
.width
-= size
.x
+ GetInternalBorder();
205 if (rectPage
.width
< 0)
214 void wxBookCtrlBase::DoSize()
218 // we're not fully created yet or OnSize() should be hidden by derived class
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() );
231 m_bookctrl
->SetClientSize( sizeCtrl
.x
- sizeBorder
.x
, sizeCtrl
.y
- sizeBorder
.y
);
232 // if this changes the visibility of the scrollbars the best size changes, relayout in this case
233 wxSize sizeCtrl2
= GetControllerSize();
234 if ( sizeCtrl
!= sizeCtrl2
)
236 wxSize sizeBorder2
= m_bookctrl
->GetSize() - m_bookctrl
->GetClientSize();
237 m_bookctrl
->SetClientSize( sizeCtrl2
.x
- sizeBorder2
.x
, sizeCtrl2
.y
- sizeBorder2
.y
);
240 const wxSize sizeNew
= m_bookctrl
->GetSize();
242 switch ( GetWindowStyle() & wxBK_ALIGN_MASK
)
245 wxFAIL_MSG( wxT("unexpected alignment") );
250 // posCtrl is already ok
254 posCtrl
.y
= sizeClient
.y
- sizeNew
.y
;
258 posCtrl
.x
= sizeClient
.x
- sizeNew
.x
;
262 if ( m_bookctrl
->GetPosition() != posCtrl
)
263 m_bookctrl
->Move(posCtrl
);
266 // resize all pages to fit the new control size
267 const wxRect pageRect
= GetPageRect();
268 const unsigned pagesCount
= m_pages
.GetCount();
269 for ( unsigned int i
= 0; i
< pagesCount
; ++i
)
271 wxWindow
* const page
= m_pages
[i
];
274 wxASSERT_MSG( AllowNullPage(),
275 wxT("Null page in a control that does not allow null pages?") );
279 page
->SetSize(pageRect
);
283 void wxBookCtrlBase::OnSize(wxSizeEvent
& event
)
290 wxSize
wxBookCtrlBase::GetControllerSize() const
295 const wxSize sizeClient
= GetClientSize(),
296 sizeBorder
= m_bookctrl
->GetSize() - m_bookctrl
->GetClientSize(),
297 sizeCtrl
= m_bookctrl
->GetBestSize() + sizeBorder
;
303 size
.x
= sizeClient
.x
;
306 else // left/right aligned
309 size
.y
= sizeClient
.y
;
315 // ----------------------------------------------------------------------------
316 // miscellaneous stuff
317 // ----------------------------------------------------------------------------
321 void wxBookCtrlBase::OnHelp(wxHelpEvent
& event
)
323 // determine where does this even originate from to avoid redirecting it
324 // back to the page which generated it (resulting in an infinite loop)
326 // notice that we have to check in the hard(er) way instead of just testing
327 // if the event object == this because the book control can have other
328 // subcontrols inside it (e.g. wxSpinButton in case of a notebook in wxUniv)
329 wxWindow
*source
= wxStaticCast(event
.GetEventObject(), wxWindow
);
330 while ( source
&& source
!= this && source
->GetParent() != this )
332 source
= source
->GetParent();
335 if ( source
&& m_pages
.Index(source
) == wxNOT_FOUND
)
337 // this event is for the book control itself, redirect it to the
338 // corresponding page
339 wxWindow
*page
= NULL
;
341 if ( event
.GetOrigin() == wxHelpEvent::Origin_HelpButton
)
343 // show help for the page under the mouse
344 const int pagePos
= HitTest(ScreenToClient(event
.GetPosition()));
346 if ( pagePos
!= wxNOT_FOUND
)
348 page
= GetPage((size_t)pagePos
);
351 else // event from keyboard or unknown source
353 // otherwise show the current page help
354 page
= GetCurrentPage();
359 // change event object to the page to avoid infinite recursion if
360 // we get this event ourselves if the page doesn't handle it
361 event
.SetEventObject(page
);
363 if ( page
->GetEventHandler()->ProcessEvent(event
) )
365 // don't call event.Skip()
370 //else: event coming from one of our pages already
377 // ----------------------------------------------------------------------------
379 // ----------------------------------------------------------------------------
382 wxBookCtrlBase::InsertPage(size_t nPage
,
384 const wxString
& WXUNUSED(text
),
385 bool WXUNUSED(bSelect
),
386 int WXUNUSED(imageId
))
388 wxCHECK_MSG( page
|| AllowNullPage(), false,
389 wxT("NULL page in wxBookCtrlBase::InsertPage()") );
390 wxCHECK_MSG( nPage
<= m_pages
.size(), false,
391 wxT("invalid page index in wxBookCtrlBase::InsertPage()") );
393 m_pages
.Insert(page
, nPage
);
395 page
->SetSize(GetPageRect());
397 DoInvalidateBestSize();
402 bool wxBookCtrlBase::DeletePage(size_t nPage
)
404 wxWindow
*page
= DoRemovePage(nPage
);
405 if ( !(page
|| AllowNullPage()) )
408 // delete NULL is harmless
414 wxWindow
*wxBookCtrlBase::DoRemovePage(size_t nPage
)
416 wxCHECK_MSG( nPage
< m_pages
.size(), NULL
,
417 wxT("invalid page index in wxBookCtrlBase::DoRemovePage()") );
419 wxWindow
*pageRemoved
= m_pages
[nPage
];
420 m_pages
.RemoveAt(nPage
);
421 DoInvalidateBestSize();
426 int wxBookCtrlBase::GetNextPage(bool forward
) const
430 int nMax
= GetPageCount();
431 if ( nMax
-- ) // decrement it to get the last valid index
433 int nSel
= GetSelection();
435 // change selection wrapping if it becomes invalid
436 nPage
= forward
? nSel
== nMax
? 0
441 else // notebook is empty, no next page
449 int wxBookCtrlBase::DoSetSelection(size_t n
, int flags
)
451 wxCHECK_MSG( n
< GetPageCount(), wxNOT_FOUND
,
452 wxT("invalid page index in wxBookCtrlBase::DoSetSelection()") );
454 const int oldSel
= GetSelection();
456 if ( n
!= (size_t)oldSel
)
458 wxBookCtrlEvent
*event
= CreatePageChangingEvent();
459 bool allowed
= false;
461 if ( flags
& SetSelection_SendEvent
)
463 event
->SetSelection(n
);
464 event
->SetOldSelection(oldSel
);
465 event
->SetEventObject(this);
467 allowed
= !GetEventHandler()->ProcessEvent(*event
) || event
->IsAllowed();
470 if ( !(flags
& SetSelection_SendEvent
) || allowed
)
472 if ( oldSel
!= wxNOT_FOUND
)
473 m_pages
[oldSel
]->Hide();
475 wxWindow
*page
= m_pages
[n
];
476 page
->SetSize(GetPageRect());
479 // change selection now to ignore the selection change event
480 UpdateSelectedPage(n
);
482 if ( flags
& SetSelection_SendEvent
)
484 // program allows the page change
485 MakeChangedEvent(*event
);
486 (void)GetEventHandler()->ProcessEvent(*event
);
496 IMPLEMENT_DYNAMIC_CLASS(wxBookCtrlEvent
, wxNotifyEvent
)
498 #endif // wxUSE_BOOKCTRL