]> git.saurik.com Git - wxWidgets.git/blob - src/common/bookctrl.cpp
1. added abstract interface to wxHtmlWindow for use by wxHtmlWinParser and implemente...
[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 END_EVENT_TABLE()
46
47 // ----------------------------------------------------------------------------
48 // constructors and destructors
49 // ----------------------------------------------------------------------------
50
51 void wxBookCtrlBase::Init()
52 {
53 m_bookctrl = NULL;
54 m_imageList = NULL;
55 m_ownsImageList = false;
56 m_fitToCurrentPage = false;
57
58 #if defined(__WXWINCE__)
59 m_internalBorder = 1;
60 #else
61 m_internalBorder = 5;
62 #endif
63
64 m_controlMargin = 0;
65 m_controlSizer = NULL;
66 }
67
68 bool
69 wxBookCtrlBase::Create(wxWindow *parent,
70 wxWindowID id,
71 const wxPoint& pos,
72 const wxSize& size,
73 long style,
74 const wxString& name)
75 {
76 return wxControl::Create
77 (
78 parent,
79 id,
80 pos,
81 size,
82 style,
83 wxDefaultValidator,
84 name
85 );
86 }
87
88 wxBookCtrlBase::~wxBookCtrlBase()
89 {
90 if ( m_ownsImageList )
91 {
92 // may be NULL, ok
93 delete m_imageList;
94 }
95 }
96
97 // ----------------------------------------------------------------------------
98 // image list
99 // ----------------------------------------------------------------------------
100
101 void wxBookCtrlBase::SetImageList(wxImageList *imageList)
102 {
103 if ( m_ownsImageList )
104 {
105 // may be NULL, ok
106 delete m_imageList;
107
108 m_ownsImageList = false;
109 }
110
111 m_imageList = imageList;
112 }
113
114 void wxBookCtrlBase::AssignImageList(wxImageList* imageList)
115 {
116 SetImageList(imageList);
117
118 m_ownsImageList = true;
119 }
120
121 // ----------------------------------------------------------------------------
122 // geometry
123 // ----------------------------------------------------------------------------
124
125 void wxBookCtrlBase::SetPageSize(const wxSize& size)
126 {
127 SetClientSize(CalcSizeFromPage(size));
128 }
129
130 wxSize wxBookCtrlBase::DoGetBestSize() const
131 {
132 wxSize bestSize;
133
134 // iterate over all pages, get the largest width and height
135 const size_t nCount = m_pages.size();
136 for ( size_t nPage = 0; nPage < nCount; nPage++ )
137 {
138 const wxWindow * const pPage = m_pages[nPage];
139 if( pPage )
140 {
141 wxSize childBestSize(pPage->GetBestSize());
142
143 if ( childBestSize.x > bestSize.x )
144 bestSize.x = childBestSize.x;
145
146 if ( childBestSize.y > bestSize.y )
147 bestSize.y = childBestSize.y;
148 }
149 }
150
151 if (m_fitToCurrentPage && GetCurrentPage())
152 bestSize = GetCurrentPage()->GetBestSize();
153
154 // convert display area to window area, adding the size necessary for the
155 // tabs
156 wxSize best = CalcSizeFromPage(bestSize);
157 CacheBestSize(best);
158 return best;
159 }
160
161 // ----------------------------------------------------------------------------
162 // pages management
163 // ----------------------------------------------------------------------------
164
165 bool
166 wxBookCtrlBase::InsertPage(size_t nPage,
167 wxWindow *page,
168 const wxString& WXUNUSED(text),
169 bool WXUNUSED(bSelect),
170 int WXUNUSED(imageId))
171 {
172 wxCHECK_MSG( page || AllowNullPage(), false,
173 _T("NULL page in wxBookCtrlBase::InsertPage()") );
174 wxCHECK_MSG( nPage <= m_pages.size(), false,
175 _T("invalid page index in wxBookCtrlBase::InsertPage()") );
176
177 m_pages.Insert(page, nPage);
178 InvalidateBestSize();
179
180 return true;
181 }
182
183 bool wxBookCtrlBase::DeletePage(size_t nPage)
184 {
185 wxWindow *page = DoRemovePage(nPage);
186 if ( !(page || AllowNullPage()) )
187 return false;
188
189 // delete NULL is harmless
190 delete page;
191
192 return true;
193 }
194
195 wxWindow *wxBookCtrlBase::DoRemovePage(size_t nPage)
196 {
197 wxCHECK_MSG( nPage < m_pages.size(), NULL,
198 _T("invalid page index in wxBookCtrlBase::DoRemovePage()") );
199
200 wxWindow *pageRemoved = m_pages[nPage];
201 m_pages.RemoveAt(nPage);
202 InvalidateBestSize();
203
204 return pageRemoved;
205 }
206
207 int wxBookCtrlBase::GetNextPage(bool forward) const
208 {
209 int nPage;
210
211 int nMax = GetPageCount();
212 if ( nMax-- ) // decrement it to get the last valid index
213 {
214 int nSel = GetSelection();
215
216 // change selection wrapping if it becomes invalid
217 nPage = forward ? nSel == nMax ? 0
218 : nSel + 1
219 : nSel == 0 ? nMax
220 : nSel - 1;
221 }
222 else // notebook is empty, no next page
223 {
224 nPage = wxNOT_FOUND;
225 }
226
227 return nPage;
228 }
229
230 wxRect wxBookCtrlBase::GetPageRect() const
231 {
232 const wxSize size = GetControllerSize();
233
234 wxPoint pt;
235 wxRect rectPage(pt, GetClientSize());
236 switch ( GetWindowStyle() & wxBK_ALIGN_MASK )
237 {
238 default:
239 wxFAIL_MSG( _T("unexpected alignment") );
240 // fall through
241
242 case wxBK_TOP:
243 rectPage.y = size.y + GetInternalBorder();
244 // fall through
245
246 case wxBK_BOTTOM:
247 rectPage.height -= size.y + GetInternalBorder();
248 break;
249
250 case wxBK_LEFT:
251 rectPage.x = size.x + GetInternalBorder();
252 // fall through
253
254 case wxBK_RIGHT:
255 rectPage.width -= size.x + GetInternalBorder();
256 break;
257 }
258
259 return rectPage;
260 }
261
262 // Lay out controls
263 void wxBookCtrlBase::DoSize()
264 {
265 if ( !m_bookctrl )
266 {
267 // we're not fully created yet or OnSize() should be hidden by derived class
268 return;
269 }
270
271 if (GetSizer())
272 Layout();
273 else
274 {
275 // resize controller and the page area to fit inside our new size
276 const wxSize sizeClient( GetClientSize() ),
277 sizeBorder( m_bookctrl->GetSize() - m_bookctrl->GetClientSize() ),
278 sizeCtrl( GetControllerSize() );
279
280 m_bookctrl->SetClientSize( sizeCtrl.x - sizeBorder.x, sizeCtrl.y - sizeBorder.y );
281
282 const wxSize sizeNew = m_bookctrl->GetSize();
283 wxPoint posCtrl;
284 switch ( GetWindowStyle() & wxBK_ALIGN_MASK )
285 {
286 default:
287 wxFAIL_MSG( _T("unexpected alignment") );
288 // fall through
289
290 case wxBK_TOP:
291 case wxBK_LEFT:
292 // posCtrl is already ok
293 break;
294
295 case wxBK_BOTTOM:
296 posCtrl.y = sizeClient.y - sizeNew.y;
297 break;
298
299 case wxBK_RIGHT:
300 posCtrl.x = sizeClient.x - sizeNew.x;
301 break;
302 }
303
304 if ( m_bookctrl->GetPosition() != posCtrl )
305 m_bookctrl->Move(posCtrl);
306 }
307
308 // resize the currently shown page
309 if (GetSelection() != wxNOT_FOUND )
310 {
311 wxWindow *page = m_pages[GetSelection()];
312 wxCHECK_RET( page, _T("NULL page?") );
313 page->SetSize(GetPageRect());
314 }
315 }
316
317 void wxBookCtrlBase::OnSize(wxSizeEvent& event)
318 {
319 event.Skip();
320
321 DoSize();
322 }
323
324 wxSize wxBookCtrlBase::GetControllerSize() const
325 {
326 if(!m_bookctrl)
327 return wxSize(0,0);
328
329 const wxSize sizeClient = GetClientSize(),
330 sizeBorder = m_bookctrl->GetSize() - m_bookctrl->GetClientSize(),
331 sizeCtrl = m_bookctrl->GetBestSize() + sizeBorder;
332
333 wxSize size;
334
335 if ( IsVertical() )
336 {
337 size.x = sizeClient.x;
338 size.y = sizeCtrl.y;
339 }
340 else // left/right aligned
341 {
342 size.x = sizeCtrl.x;
343 size.y = sizeClient.y;
344 }
345
346 return size;
347 }
348
349 #endif // wxUSE_BOOKCTRL