]>
Commit | Line | Data |
---|---|---|
15aad3b9 | 1 | /////////////////////////////////////////////////////////////////////////////// |
2ddb4d13 | 2 | // Name: src/common/bookctrl.cpp |
61c083e7 | 3 | // Purpose: wxBookCtrlBase implementation |
15aad3b9 VZ |
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> | |
65571936 | 9 | // Licence: wxWindows licence |
15aad3b9 VZ |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
15aad3b9 VZ |
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 | ||
2ddb4d13 WS |
37 | // ---------------------------------------------------------------------------- |
38 | // event table | |
39 | // ---------------------------------------------------------------------------- | |
40 | ||
41 | IMPLEMENT_ABSTRACT_CLASS(wxBookCtrlBase, wxControl) | |
42 | ||
43 | BEGIN_EVENT_TABLE(wxBookCtrlBase, wxControl) | |
44 | EVT_SIZE(wxBookCtrlBase::OnSize) | |
a18c21f0 VZ |
45 | #if wxUSE_HELP |
46 | EVT_HELP(wxID_ANY, wxBookCtrlBase::OnHelp) | |
47 | #endif // wxUSE_HELP | |
2ddb4d13 WS |
48 | END_EVENT_TABLE() |
49 | ||
15aad3b9 VZ |
50 | // ---------------------------------------------------------------------------- |
51 | // constructors and destructors | |
52 | // ---------------------------------------------------------------------------- | |
53 | ||
61c083e7 | 54 | void wxBookCtrlBase::Init() |
15aad3b9 | 55 | { |
2ddb4d13 | 56 | m_bookctrl = NULL; |
15aad3b9 VZ |
57 | m_imageList = NULL; |
58 | m_ownsImageList = false; | |
da817fa6 | 59 | m_fitToCurrentPage = false; |
159e6235 WS |
60 | |
61 | #if defined(__WXWINCE__) | |
62 | m_internalBorder = 1; | |
63 | #else | |
64 | m_internalBorder = 5; | |
65 | #endif | |
87cf52d8 JS |
66 | |
67 | m_controlMargin = 0; | |
68 | m_controlSizer = NULL; | |
15aad3b9 VZ |
69 | } |
70 | ||
71 | bool | |
61c083e7 | 72 | wxBookCtrlBase::Create(wxWindow *parent, |
15aad3b9 VZ |
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 | ||
61c083e7 | 91 | wxBookCtrlBase::~wxBookCtrlBase() |
15aad3b9 VZ |
92 | { |
93 | if ( m_ownsImageList ) | |
94 | { | |
95 | // may be NULL, ok | |
96 | delete m_imageList; | |
97 | } | |
98 | } | |
99 | ||
100 | // ---------------------------------------------------------------------------- | |
101 | // image list | |
102 | // ---------------------------------------------------------------------------- | |
103 | ||
61c083e7 | 104 | void wxBookCtrlBase::SetImageList(wxImageList *imageList) |
15aad3b9 VZ |
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 | ||
61c083e7 | 117 | void wxBookCtrlBase::AssignImageList(wxImageList* imageList) |
15aad3b9 VZ |
118 | { |
119 | SetImageList(imageList); | |
120 | ||
121 | m_ownsImageList = true; | |
122 | } | |
123 | ||
124 | // ---------------------------------------------------------------------------- | |
125 | // geometry | |
126 | // ---------------------------------------------------------------------------- | |
127 | ||
61c083e7 | 128 | void wxBookCtrlBase::SetPageSize(const wxSize& size) |
15aad3b9 VZ |
129 | { |
130 | SetClientSize(CalcSizeFromPage(size)); | |
131 | } | |
132 | ||
61c083e7 | 133 | wxSize wxBookCtrlBase::DoGetBestSize() const |
15aad3b9 VZ |
134 | { |
135 | wxSize bestSize; | |
136 | ||
137 | // iterate over all pages, get the largest width and height | |
138 | const size_t nCount = m_pages.size(); | |
139 | for ( size_t nPage = 0; nPage < nCount; nPage++ ) | |
140 | { | |
eca15c0d VZ |
141 | const wxWindow * const pPage = m_pages[nPage]; |
142 | if( pPage ) | |
143 | { | |
144 | wxSize childBestSize(pPage->GetBestSize()); | |
15aad3b9 | 145 | |
eca15c0d VZ |
146 | if ( childBestSize.x > bestSize.x ) |
147 | bestSize.x = childBestSize.x; | |
15aad3b9 | 148 | |
eca15c0d VZ |
149 | if ( childBestSize.y > bestSize.y ) |
150 | bestSize.y = childBestSize.y; | |
151 | } | |
15aad3b9 | 152 | } |
a717bd11 | 153 | |
da817fa6 | 154 | if (m_fitToCurrentPage && GetCurrentPage()) |
93bfe545 | 155 | bestSize = GetCurrentPage()->GetBestSize(); |
15aad3b9 | 156 | |
3103e8a9 | 157 | // convert display area to window area, adding the size necessary for the |
15aad3b9 | 158 | // tabs |
9f884528 RD |
159 | wxSize best = CalcSizeFromPage(bestSize); |
160 | CacheBestSize(best); | |
161 | return best; | |
15aad3b9 VZ |
162 | } |
163 | ||
a18c21f0 | 164 | #if wxUSE_HELP |
ec09e18d | 165 | |
a18c21f0 VZ |
166 | void wxBookCtrlBase::OnHelp(wxHelpEvent& event) |
167 | { | |
ec09e18d VZ |
168 | // determine where does this even originate from to avoid redirecting it |
169 | // back to the page which generated it (resulting in an infinite loop) | |
170 | ||
171 | // notice that we have to check in the hard(er) way instead of just testing | |
172 | // if the event object == this because the book control can have other | |
173 | // subcontrols inside it (e.g. wxSpinButton in case of a notebook in wxUniv) | |
174 | wxWindow *source = wxStaticCast(event.GetEventObject(), wxWindow); | |
cb5bc9b7 | 175 | while ( source && source != this && source->GetParent() != this ) |
b6b46954 | 176 | { |
ec09e18d | 177 | source = source->GetParent(); |
b6b46954 VZ |
178 | } |
179 | ||
ec09e18d | 180 | if ( source && m_pages.Index(source) == wxNOT_FOUND ) |
a18c21f0 | 181 | { |
ec09e18d VZ |
182 | // this event is for the book control itself, redirect it to the |
183 | // corresponding page | |
184 | wxWindow *page = NULL; | |
a18c21f0 | 185 | |
ec09e18d | 186 | if ( event.GetOrigin() == wxHelpEvent::Origin_HelpButton ) |
a18c21f0 | 187 | { |
ec09e18d VZ |
188 | // show help for the page under the mouse |
189 | const int pagePos = HitTest(ScreenToClient(event.GetPosition())); | |
190 | ||
191 | if ( pagePos != wxNOT_FOUND) | |
192 | { | |
193 | page = GetPage((size_t)pagePos); | |
194 | } | |
195 | } | |
196 | else // event from keyboard or unknown source | |
197 | { | |
198 | // otherwise show the current page help | |
199 | page = GetCurrentPage(); | |
a18c21f0 | 200 | } |
a18c21f0 | 201 | |
ec09e18d VZ |
202 | if ( page ) |
203 | { | |
204 | // change event object to the page to avoid infinite recursion if | |
205 | // we get this event ourselves if the page doesn't handle it | |
206 | event.SetEventObject(page); | |
207 | ||
208 | if ( page->GetEventHandler()->ProcessEvent(event) ) | |
209 | { | |
210 | // don't call event.Skip() | |
211 | return; | |
212 | } | |
213 | } | |
a18c21f0 | 214 | } |
ec09e18d VZ |
215 | //else: event coming from one of our pages already |
216 | ||
217 | event.Skip(); | |
a18c21f0 | 218 | } |
ec09e18d | 219 | |
a18c21f0 VZ |
220 | #endif // wxUSE_HELP |
221 | ||
15aad3b9 VZ |
222 | // ---------------------------------------------------------------------------- |
223 | // pages management | |
224 | // ---------------------------------------------------------------------------- | |
225 | ||
226 | bool | |
61c083e7 WS |
227 | wxBookCtrlBase::InsertPage(size_t nPage, |
228 | wxWindow *page, | |
229 | const wxString& WXUNUSED(text), | |
230 | bool WXUNUSED(bSelect), | |
231 | int WXUNUSED(imageId)) | |
15aad3b9 | 232 | { |
2ddb4d13 WS |
233 | wxCHECK_MSG( page || AllowNullPage(), false, |
234 | _T("NULL page in wxBookCtrlBase::InsertPage()") ); | |
712f40ca | 235 | wxCHECK_MSG( nPage <= m_pages.size(), false, |
61c083e7 | 236 | _T("invalid page index in wxBookCtrlBase::InsertPage()") ); |
15aad3b9 VZ |
237 | |
238 | m_pages.Insert(page, nPage); | |
a717bd11 VZ |
239 | if ( page ) |
240 | page->SetSize(GetPageRect()); | |
241 | ||
37144cf0 | 242 | InvalidateBestSize(); |
c9d59ee7 | 243 | |
15aad3b9 VZ |
244 | return true; |
245 | } | |
246 | ||
61c083e7 | 247 | bool wxBookCtrlBase::DeletePage(size_t nPage) |
15aad3b9 VZ |
248 | { |
249 | wxWindow *page = DoRemovePage(nPage); | |
eca15c0d | 250 | if ( !(page || AllowNullPage()) ) |
15aad3b9 VZ |
251 | return false; |
252 | ||
eca15c0d | 253 | // delete NULL is harmless |
15aad3b9 VZ |
254 | delete page; |
255 | ||
256 | return true; | |
257 | } | |
258 | ||
61c083e7 | 259 | wxWindow *wxBookCtrlBase::DoRemovePage(size_t nPage) |
15aad3b9 VZ |
260 | { |
261 | wxCHECK_MSG( nPage < m_pages.size(), NULL, | |
61c083e7 | 262 | _T("invalid page index in wxBookCtrlBase::DoRemovePage()") ); |
15aad3b9 VZ |
263 | |
264 | wxWindow *pageRemoved = m_pages[nPage]; | |
265 | m_pages.RemoveAt(nPage); | |
37144cf0 | 266 | InvalidateBestSize(); |
15aad3b9 VZ |
267 | |
268 | return pageRemoved; | |
269 | } | |
270 | ||
61c083e7 | 271 | int wxBookCtrlBase::GetNextPage(bool forward) const |
15aad3b9 VZ |
272 | { |
273 | int nPage; | |
274 | ||
275 | int nMax = GetPageCount(); | |
276 | if ( nMax-- ) // decrement it to get the last valid index | |
277 | { | |
278 | int nSel = GetSelection(); | |
279 | ||
280 | // change selection wrapping if it becomes invalid | |
281 | nPage = forward ? nSel == nMax ? 0 | |
282 | : nSel + 1 | |
283 | : nSel == 0 ? nMax | |
284 | : nSel - 1; | |
285 | } | |
286 | else // notebook is empty, no next page | |
287 | { | |
159e6235 | 288 | nPage = wxNOT_FOUND; |
15aad3b9 VZ |
289 | } |
290 | ||
291 | return nPage; | |
292 | } | |
293 | ||
2ddb4d13 WS |
294 | wxRect wxBookCtrlBase::GetPageRect() const |
295 | { | |
296 | const wxSize size = GetControllerSize(); | |
297 | ||
298 | wxPoint pt; | |
299 | wxRect rectPage(pt, GetClientSize()); | |
926395e1 | 300 | |
90f9b8ef | 301 | switch ( GetWindowStyle() & wxBK_ALIGN_MASK ) |
2ddb4d13 WS |
302 | { |
303 | default: | |
304 | wxFAIL_MSG( _T("unexpected alignment") ); | |
305 | // fall through | |
306 | ||
307 | case wxBK_TOP: | |
308 | rectPage.y = size.y + GetInternalBorder(); | |
309 | // fall through | |
310 | ||
311 | case wxBK_BOTTOM: | |
312 | rectPage.height -= size.y + GetInternalBorder(); | |
6a9e44d5 PC |
313 | if (rectPage.height < 0) |
314 | rectPage.height = 0; | |
2ddb4d13 WS |
315 | break; |
316 | ||
317 | case wxBK_LEFT: | |
318 | rectPage.x = size.x + GetInternalBorder(); | |
319 | // fall through | |
320 | ||
321 | case wxBK_RIGHT: | |
322 | rectPage.width -= size.x + GetInternalBorder(); | |
6a9e44d5 PC |
323 | if (rectPage.width < 0) |
324 | rectPage.width = 0; | |
2ddb4d13 WS |
325 | break; |
326 | } | |
327 | ||
328 | return rectPage; | |
329 | } | |
330 | ||
233387bd JS |
331 | // Lay out controls |
332 | void wxBookCtrlBase::DoSize() | |
2ddb4d13 | 333 | { |
2ddb4d13 WS |
334 | if ( !m_bookctrl ) |
335 | { | |
336 | // we're not fully created yet or OnSize() should be hidden by derived class | |
337 | return; | |
338 | } | |
a717bd11 | 339 | |
87cf52d8 JS |
340 | if (GetSizer()) |
341 | Layout(); | |
342 | else | |
2ddb4d13 | 343 | { |
87cf52d8 JS |
344 | // resize controller and the page area to fit inside our new size |
345 | const wxSize sizeClient( GetClientSize() ), | |
346 | sizeBorder( m_bookctrl->GetSize() - m_bookctrl->GetClientSize() ), | |
347 | sizeCtrl( GetControllerSize() ); | |
2ddb4d13 | 348 | |
87cf52d8 | 349 | m_bookctrl->SetClientSize( sizeCtrl.x - sizeBorder.x, sizeCtrl.y - sizeBorder.y ); |
2ddb4d13 | 350 | |
87cf52d8 JS |
351 | const wxSize sizeNew = m_bookctrl->GetSize(); |
352 | wxPoint posCtrl; | |
353 | switch ( GetWindowStyle() & wxBK_ALIGN_MASK ) | |
354 | { | |
355 | default: | |
356 | wxFAIL_MSG( _T("unexpected alignment") ); | |
357 | // fall through | |
358 | ||
359 | case wxBK_TOP: | |
360 | case wxBK_LEFT: | |
361 | // posCtrl is already ok | |
362 | break; | |
363 | ||
364 | case wxBK_BOTTOM: | |
365 | posCtrl.y = sizeClient.y - sizeNew.y; | |
366 | break; | |
367 | ||
368 | case wxBK_RIGHT: | |
369 | posCtrl.x = sizeClient.x - sizeNew.x; | |
370 | break; | |
371 | } | |
2ddb4d13 | 372 | |
87cf52d8 JS |
373 | if ( m_bookctrl->GetPosition() != posCtrl ) |
374 | m_bookctrl->Move(posCtrl); | |
2ddb4d13 WS |
375 | } |
376 | ||
a717bd11 VZ |
377 | // resize all pages to fit the new control size |
378 | const wxRect pageRect = GetPageRect(); | |
379 | const unsigned pagesCount = m_pages.Count(); | |
380 | for ( unsigned int i = 0; i < pagesCount; ++i ) | |
2ddb4d13 | 381 | { |
a717bd11 VZ |
382 | wxWindow * const page = m_pages[i]; |
383 | if ( !page ) | |
384 | { | |
385 | wxASSERT_MSG( AllowNullPage(), | |
386 | _T("Null page in a control that does not allow null pages?") ); | |
387 | continue; | |
388 | } | |
389 | ||
390 | page->SetSize(pageRect); | |
2ddb4d13 WS |
391 | } |
392 | } | |
393 | ||
233387bd JS |
394 | void wxBookCtrlBase::OnSize(wxSizeEvent& event) |
395 | { | |
396 | event.Skip(); | |
a717bd11 | 397 | |
233387bd JS |
398 | DoSize(); |
399 | } | |
400 | ||
2ddb4d13 WS |
401 | wxSize wxBookCtrlBase::GetControllerSize() const |
402 | { | |
403 | if(!m_bookctrl) | |
404 | return wxSize(0,0); | |
405 | ||
406 | const wxSize sizeClient = GetClientSize(), | |
407 | sizeBorder = m_bookctrl->GetSize() - m_bookctrl->GetClientSize(), | |
408 | sizeCtrl = m_bookctrl->GetBestSize() + sizeBorder; | |
409 | ||
410 | wxSize size; | |
411 | ||
412 | if ( IsVertical() ) | |
413 | { | |
414 | size.x = sizeClient.x; | |
415 | size.y = sizeCtrl.y; | |
416 | } | |
417 | else // left/right aligned | |
418 | { | |
419 | size.x = sizeCtrl.x; | |
420 | size.y = sizeClient.y; | |
421 | } | |
422 | ||
423 | return size; | |
424 | } | |
425 | ||
1d6fcbcc VZ |
426 | int wxBookCtrlBase::DoSetSelection(size_t n, int flags, wxBookCtrlBaseEvent &event) |
427 | { | |
428 | wxCHECK_MSG( n < GetPageCount(), wxNOT_FOUND, | |
429 | wxT("invalid page index in wxBookCtrlBase::DoSetSelection()") ); | |
430 | ||
431 | const int oldSel = GetSelection(); | |
432 | ||
433 | if ( oldSel != wxNOT_FOUND && n != (size_t)oldSel ) | |
434 | { | |
435 | bool allowed = false; | |
436 | ||
437 | if ( flags & SetSelection_SendEvent ) | |
438 | { | |
439 | event.SetSelection(n); | |
440 | event.SetOldSelection(oldSel); | |
441 | event.SetEventObject(this); | |
442 | ||
443 | allowed = !GetEventHandler()->ProcessEvent(event) || event.IsAllowed(); | |
444 | } | |
445 | ||
446 | if ( !(flags & SetSelection_SendEvent) || allowed) | |
447 | { | |
448 | if ( oldSel != wxNOT_FOUND ) | |
449 | m_pages[oldSel]->Hide(); | |
450 | ||
451 | wxWindow *page = m_pages[n]; | |
452 | page->SetSize(GetPageRect()); | |
453 | page->Show(); | |
454 | ||
455 | // change selection now to ignore the selection change event | |
456 | UpdateSelectedPage(n); | |
457 | ||
458 | if ( flags & SetSelection_SendEvent ) | |
459 | { | |
460 | // program allows the page change | |
461 | MakeChangedEvent(event); | |
462 | (void)GetEventHandler()->ProcessEvent(event); | |
463 | } | |
464 | } | |
465 | } | |
466 | ||
467 | return oldSel; | |
468 | } | |
469 | ||
470 | ||
15aad3b9 | 471 | #endif // wxUSE_BOOKCTRL |