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