show help for the page under mouse when the [?] button is used to request help
[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::SetPageSize(const wxSize& size)
129 {
130 SetClientSize(CalcSizeFromPage(size));
131 }
132
133 wxSize wxBookCtrlBase::DoGetBestSize() const
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 {
141 const wxWindow * const pPage = m_pages[nPage];
142 if( pPage )
143 {
144 wxSize childBestSize(pPage->GetBestSize());
145
146 if ( childBestSize.x > bestSize.x )
147 bestSize.x = childBestSize.x;
148
149 if ( childBestSize.y > bestSize.y )
150 bestSize.y = childBestSize.y;
151 }
152 }
153
154 if (m_fitToCurrentPage && GetCurrentPage())
155 bestSize = GetCurrentPage()->GetBestSize();
156
157 // convert display area to window area, adding the size necessary for the
158 // tabs
159 wxSize best = CalcSizeFromPage(bestSize);
160 CacheBestSize(best);
161 return best;
162 }
163
164 #if wxUSE_HELP
165 void wxBookCtrlBase::OnHelp(wxHelpEvent& event)
166 {
167 // find the corresponding page
168 wxWindow *page = NULL;
169
170 if ( event.GetOrigin() == wxHelpEvent::Origin_HelpButton )
171 {
172 // show help for the page under the mouse
173 const int pagePos = HitTest(ScreenToClient(event.GetPosition()));
174
175 if ( pagePos != wxNOT_FOUND)
176 {
177 page = GetPage((size_t)pagePos);
178 }
179 }
180
181 if ( !page )
182 {
183 page = GetCurrentPage();
184 }
185
186 if ( !page || !page->GetEventHandler()->ProcessEvent(event) )
187 {
188 event.Skip();
189 }
190 }
191 #endif // wxUSE_HELP
192
193 // ----------------------------------------------------------------------------
194 // pages management
195 // ----------------------------------------------------------------------------
196
197 bool
198 wxBookCtrlBase::InsertPage(size_t nPage,
199 wxWindow *page,
200 const wxString& WXUNUSED(text),
201 bool WXUNUSED(bSelect),
202 int WXUNUSED(imageId))
203 {
204 wxCHECK_MSG( page || AllowNullPage(), false,
205 _T("NULL page in wxBookCtrlBase::InsertPage()") );
206 wxCHECK_MSG( nPage <= m_pages.size(), false,
207 _T("invalid page index in wxBookCtrlBase::InsertPage()") );
208
209 m_pages.Insert(page, nPage);
210 if ( page )
211 page->SetSize(GetPageRect());
212
213 InvalidateBestSize();
214
215 return true;
216 }
217
218 bool wxBookCtrlBase::DeletePage(size_t nPage)
219 {
220 wxWindow *page = DoRemovePage(nPage);
221 if ( !(page || AllowNullPage()) )
222 return false;
223
224 // delete NULL is harmless
225 delete page;
226
227 return true;
228 }
229
230 wxWindow *wxBookCtrlBase::DoRemovePage(size_t nPage)
231 {
232 wxCHECK_MSG( nPage < m_pages.size(), NULL,
233 _T("invalid page index in wxBookCtrlBase::DoRemovePage()") );
234
235 wxWindow *pageRemoved = m_pages[nPage];
236 m_pages.RemoveAt(nPage);
237 InvalidateBestSize();
238
239 return pageRemoved;
240 }
241
242 int wxBookCtrlBase::GetNextPage(bool forward) const
243 {
244 int nPage;
245
246 int nMax = GetPageCount();
247 if ( nMax-- ) // decrement it to get the last valid index
248 {
249 int nSel = GetSelection();
250
251 // change selection wrapping if it becomes invalid
252 nPage = forward ? nSel == nMax ? 0
253 : nSel + 1
254 : nSel == 0 ? nMax
255 : nSel - 1;
256 }
257 else // notebook is empty, no next page
258 {
259 nPage = wxNOT_FOUND;
260 }
261
262 return nPage;
263 }
264
265 wxRect wxBookCtrlBase::GetPageRect() const
266 {
267 const wxSize size = GetControllerSize();
268
269 wxPoint pt;
270 wxRect rectPage(pt, GetClientSize());
271 switch ( GetWindowStyle() & wxBK_ALIGN_MASK )
272 {
273 default:
274 wxFAIL_MSG( _T("unexpected alignment") );
275 // fall through
276
277 case wxBK_TOP:
278 rectPage.y = size.y + GetInternalBorder();
279 // fall through
280
281 case wxBK_BOTTOM:
282 rectPage.height -= size.y + GetInternalBorder();
283 break;
284
285 case wxBK_LEFT:
286 rectPage.x = size.x + GetInternalBorder();
287 // fall through
288
289 case wxBK_RIGHT:
290 rectPage.width -= size.x + GetInternalBorder();
291 break;
292 }
293
294 return rectPage;
295 }
296
297 // Lay out controls
298 void wxBookCtrlBase::DoSize()
299 {
300 if ( !m_bookctrl )
301 {
302 // we're not fully created yet or OnSize() should be hidden by derived class
303 return;
304 }
305
306 if (GetSizer())
307 Layout();
308 else
309 {
310 // resize controller and the page area to fit inside our new size
311 const wxSize sizeClient( GetClientSize() ),
312 sizeBorder( m_bookctrl->GetSize() - m_bookctrl->GetClientSize() ),
313 sizeCtrl( GetControllerSize() );
314
315 m_bookctrl->SetClientSize( sizeCtrl.x - sizeBorder.x, sizeCtrl.y - sizeBorder.y );
316
317 const wxSize sizeNew = m_bookctrl->GetSize();
318 wxPoint posCtrl;
319 switch ( GetWindowStyle() & wxBK_ALIGN_MASK )
320 {
321 default:
322 wxFAIL_MSG( _T("unexpected alignment") );
323 // fall through
324
325 case wxBK_TOP:
326 case wxBK_LEFT:
327 // posCtrl is already ok
328 break;
329
330 case wxBK_BOTTOM:
331 posCtrl.y = sizeClient.y - sizeNew.y;
332 break;
333
334 case wxBK_RIGHT:
335 posCtrl.x = sizeClient.x - sizeNew.x;
336 break;
337 }
338
339 if ( m_bookctrl->GetPosition() != posCtrl )
340 m_bookctrl->Move(posCtrl);
341 }
342
343 // resize all pages to fit the new control size
344 const wxRect pageRect = GetPageRect();
345 const unsigned pagesCount = m_pages.Count();
346 for ( unsigned int i = 0; i < pagesCount; ++i )
347 {
348 wxWindow * const page = m_pages[i];
349 if ( !page )
350 {
351 wxASSERT_MSG( AllowNullPage(),
352 _T("Null page in a control that does not allow null pages?") );
353 continue;
354 }
355
356 page->SetSize(pageRect);
357 }
358 }
359
360 void wxBookCtrlBase::OnSize(wxSizeEvent& event)
361 {
362 event.Skip();
363
364 DoSize();
365 }
366
367 wxSize wxBookCtrlBase::GetControllerSize() const
368 {
369 if(!m_bookctrl)
370 return wxSize(0,0);
371
372 const wxSize sizeClient = GetClientSize(),
373 sizeBorder = m_bookctrl->GetSize() - m_bookctrl->GetClientSize(),
374 sizeCtrl = m_bookctrl->GetBestSize() + sizeBorder;
375
376 wxSize size;
377
378 if ( IsVertical() )
379 {
380 size.x = sizeClient.x;
381 size.y = sizeCtrl.y;
382 }
383 else // left/right aligned
384 {
385 size.x = sizeCtrl.x;
386 size.y = sizeClient.y;
387 }
388
389 return size;
390 }
391
392 #endif // wxUSE_BOOKCTRL