]>
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) | |
45 | END_EVENT_TABLE() | |
46 | ||
15aad3b9 VZ |
47 | // ---------------------------------------------------------------------------- |
48 | // constructors and destructors | |
49 | // ---------------------------------------------------------------------------- | |
50 | ||
61c083e7 | 51 | void wxBookCtrlBase::Init() |
15aad3b9 | 52 | { |
2ddb4d13 | 53 | m_bookctrl = NULL; |
15aad3b9 VZ |
54 | m_imageList = NULL; |
55 | m_ownsImageList = false; | |
159e6235 WS |
56 | |
57 | #if defined(__WXWINCE__) | |
58 | m_internalBorder = 1; | |
59 | #else | |
60 | m_internalBorder = 5; | |
61 | #endif | |
15aad3b9 VZ |
62 | } |
63 | ||
64 | bool | |
61c083e7 | 65 | wxBookCtrlBase::Create(wxWindow *parent, |
15aad3b9 VZ |
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 | ||
61c083e7 | 84 | wxBookCtrlBase::~wxBookCtrlBase() |
15aad3b9 VZ |
85 | { |
86 | if ( m_ownsImageList ) | |
87 | { | |
88 | // may be NULL, ok | |
89 | delete m_imageList; | |
90 | } | |
91 | } | |
92 | ||
93 | // ---------------------------------------------------------------------------- | |
94 | // image list | |
95 | // ---------------------------------------------------------------------------- | |
96 | ||
61c083e7 | 97 | void wxBookCtrlBase::SetImageList(wxImageList *imageList) |
15aad3b9 VZ |
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 | ||
61c083e7 | 110 | void wxBookCtrlBase::AssignImageList(wxImageList* imageList) |
15aad3b9 VZ |
111 | { |
112 | SetImageList(imageList); | |
113 | ||
114 | m_ownsImageList = true; | |
115 | } | |
116 | ||
117 | // ---------------------------------------------------------------------------- | |
118 | // geometry | |
119 | // ---------------------------------------------------------------------------- | |
120 | ||
61c083e7 | 121 | void wxBookCtrlBase::SetPageSize(const wxSize& size) |
15aad3b9 VZ |
122 | { |
123 | SetClientSize(CalcSizeFromPage(size)); | |
124 | } | |
125 | ||
61c083e7 | 126 | wxSize wxBookCtrlBase::DoGetBestSize() const |
15aad3b9 VZ |
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 | { | |
eca15c0d VZ |
134 | const wxWindow * const pPage = m_pages[nPage]; |
135 | if( pPage ) | |
136 | { | |
137 | wxSize childBestSize(pPage->GetBestSize()); | |
15aad3b9 | 138 | |
eca15c0d VZ |
139 | if ( childBestSize.x > bestSize.x ) |
140 | bestSize.x = childBestSize.x; | |
15aad3b9 | 141 | |
eca15c0d VZ |
142 | if ( childBestSize.y > bestSize.y ) |
143 | bestSize.y = childBestSize.y; | |
144 | } | |
15aad3b9 VZ |
145 | } |
146 | ||
3103e8a9 | 147 | // convert display area to window area, adding the size necessary for the |
15aad3b9 | 148 | // tabs |
9f884528 RD |
149 | wxSize best = CalcSizeFromPage(bestSize); |
150 | CacheBestSize(best); | |
151 | return best; | |
15aad3b9 VZ |
152 | } |
153 | ||
154 | // ---------------------------------------------------------------------------- | |
155 | // pages management | |
156 | // ---------------------------------------------------------------------------- | |
157 | ||
158 | bool | |
61c083e7 WS |
159 | wxBookCtrlBase::InsertPage(size_t nPage, |
160 | wxWindow *page, | |
161 | const wxString& WXUNUSED(text), | |
162 | bool WXUNUSED(bSelect), | |
163 | int WXUNUSED(imageId)) | |
15aad3b9 | 164 | { |
2ddb4d13 WS |
165 | wxCHECK_MSG( page || AllowNullPage(), false, |
166 | _T("NULL page in wxBookCtrlBase::InsertPage()") ); | |
712f40ca | 167 | wxCHECK_MSG( nPage <= m_pages.size(), false, |
61c083e7 | 168 | _T("invalid page index in wxBookCtrlBase::InsertPage()") ); |
15aad3b9 VZ |
169 | |
170 | m_pages.Insert(page, nPage); | |
37144cf0 | 171 | InvalidateBestSize(); |
c9d59ee7 | 172 | |
15aad3b9 VZ |
173 | return true; |
174 | } | |
175 | ||
61c083e7 | 176 | bool wxBookCtrlBase::DeletePage(size_t nPage) |
15aad3b9 VZ |
177 | { |
178 | wxWindow *page = DoRemovePage(nPage); | |
eca15c0d | 179 | if ( !(page || AllowNullPage()) ) |
15aad3b9 VZ |
180 | return false; |
181 | ||
eca15c0d | 182 | // delete NULL is harmless |
15aad3b9 VZ |
183 | delete page; |
184 | ||
185 | return true; | |
186 | } | |
187 | ||
61c083e7 | 188 | wxWindow *wxBookCtrlBase::DoRemovePage(size_t nPage) |
15aad3b9 VZ |
189 | { |
190 | wxCHECK_MSG( nPage < m_pages.size(), NULL, | |
61c083e7 | 191 | _T("invalid page index in wxBookCtrlBase::DoRemovePage()") ); |
15aad3b9 VZ |
192 | |
193 | wxWindow *pageRemoved = m_pages[nPage]; | |
194 | m_pages.RemoveAt(nPage); | |
37144cf0 | 195 | InvalidateBestSize(); |
15aad3b9 VZ |
196 | |
197 | return pageRemoved; | |
198 | } | |
199 | ||
61c083e7 | 200 | int wxBookCtrlBase::GetNextPage(bool forward) const |
15aad3b9 VZ |
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 | { | |
159e6235 | 217 | nPage = wxNOT_FOUND; |
15aad3b9 VZ |
218 | } |
219 | ||
220 | return nPage; | |
221 | } | |
222 | ||
2ddb4d13 WS |
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 | ||
233387bd JS |
255 | // Lay out controls |
256 | void wxBookCtrlBase::DoSize() | |
2ddb4d13 | 257 | { |
2ddb4d13 WS |
258 | if ( !m_bookctrl ) |
259 | { | |
260 | // we're not fully created yet or OnSize() should be hidden by derived class | |
261 | return; | |
262 | } | |
263 | ||
264 | // resize controller and the page area to fit inside our new size | |
265 | const wxSize sizeClient( GetClientSize() ), | |
266 | sizeBorder( m_bookctrl->GetSize() - m_bookctrl->GetClientSize() ), | |
267 | sizeCtrl( GetControllerSize() ); | |
268 | ||
269 | m_bookctrl->SetClientSize( sizeCtrl.x - sizeBorder.x, sizeCtrl.y - sizeBorder.y ); | |
270 | ||
271 | const wxSize sizeNew = m_bookctrl->GetSize(); | |
272 | wxPoint posCtrl; | |
273 | switch ( GetWindowStyle() & wxBK_ALIGN_MASK ) | |
274 | { | |
275 | default: | |
276 | wxFAIL_MSG( _T("unexpected alignment") ); | |
277 | // fall through | |
278 | ||
279 | case wxBK_TOP: | |
280 | case wxBK_LEFT: | |
281 | // posCtrl is already ok | |
282 | break; | |
283 | ||
284 | case wxBK_BOTTOM: | |
285 | posCtrl.y = sizeClient.y - sizeNew.y; | |
286 | break; | |
287 | ||
288 | case wxBK_RIGHT: | |
289 | posCtrl.x = sizeClient.x - sizeNew.x; | |
290 | break; | |
291 | } | |
292 | ||
293 | if ( m_bookctrl->GetPosition() != posCtrl ) | |
294 | m_bookctrl->Move(posCtrl); | |
295 | ||
296 | // resize the currently shown page | |
297 | if (GetSelection() != wxNOT_FOUND ) | |
298 | { | |
299 | wxWindow *page = m_pages[GetSelection()]; | |
300 | wxCHECK_RET( page, _T("NULL page?") ); | |
301 | page->SetSize(GetPageRect()); | |
302 | } | |
303 | } | |
304 | ||
233387bd JS |
305 | void wxBookCtrlBase::OnSize(wxSizeEvent& event) |
306 | { | |
307 | event.Skip(); | |
308 | ||
309 | DoSize(); | |
310 | } | |
311 | ||
2ddb4d13 WS |
312 | wxSize wxBookCtrlBase::GetControllerSize() const |
313 | { | |
314 | if(!m_bookctrl) | |
315 | return wxSize(0,0); | |
316 | ||
317 | const wxSize sizeClient = GetClientSize(), | |
318 | sizeBorder = m_bookctrl->GetSize() - m_bookctrl->GetClientSize(), | |
319 | sizeCtrl = m_bookctrl->GetBestSize() + sizeBorder; | |
320 | ||
321 | wxSize size; | |
322 | ||
323 | if ( IsVertical() ) | |
324 | { | |
325 | size.x = sizeClient.x; | |
326 | size.y = sizeCtrl.y; | |
327 | } | |
328 | else // left/right aligned | |
329 | { | |
330 | size.x = sizeCtrl.x; | |
331 | size.y = sizeClient.y; | |
332 | } | |
333 | ||
334 | return size; | |
335 | } | |
336 | ||
15aad3b9 | 337 | #endif // wxUSE_BOOKCTRL |