]>
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 | ||
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 | ||
15aad3b9 | 331 | #endif // wxUSE_BOOKCTRL |