wxChoicebook follows wxListBook internal changes. Styles centralized for all wxBookCtrls.
[wxWidgets.git] / src / generic / choicbkg.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: generic/choicbkg.cpp
3 // Purpose: generic implementation of wxChoicebook
4 // Author: Vadim Zeitlin
5 // Modified by: Wlodzimierz ABX Skiba from generic/listbkg.cpp
6 // Created: 15.09.04
7 // RCS-ID: $Id$
8 // Copyright: (c) Vadim Zeitlin, Wlodzimierz Skiba
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "choicebook.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #if wxUSE_CHOICEBOOK
32
33 #include "wx/choice.h"
34 #include "wx/choicebk.h"
35 #include "wx/imaglist.h"
36 #include "wx/settings.h"
37
38 // ----------------------------------------------------------------------------
39 // constants
40 // ----------------------------------------------------------------------------
41
42 // margin between the choice and the page
43 #if defined(__WXWINCE__)
44 const wxCoord MARGIN = 1;
45 #else
46 const wxCoord MARGIN = 5;
47 #endif
48
49 // ----------------------------------------------------------------------------
50 // various wxWidgets macros
51 // ----------------------------------------------------------------------------
52
53 IMPLEMENT_DYNAMIC_CLASS(wxChoicebook, wxControl)
54 IMPLEMENT_DYNAMIC_CLASS(wxChoicebookEvent, wxNotifyEvent)
55
56 const wxEventType wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING = wxNewEventType();
57 const wxEventType wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED = wxNewEventType();
58 const int wxID_CHOICEBOOKCHOICE = wxNewId();
59
60 BEGIN_EVENT_TABLE(wxChoicebook, wxBookCtrl)
61 EVT_SIZE(wxChoicebook::OnSize)
62 EVT_CHOICE(wxID_CHOICEBOOKCHOICE, wxChoicebook::OnChoiceSelected)
63 END_EVENT_TABLE()
64
65 // ============================================================================
66 // wxChoicebook implementation
67 // ============================================================================
68
69 // ----------------------------------------------------------------------------
70 // wxChoicebook creation
71 // ----------------------------------------------------------------------------
72
73 void wxChoicebook::Init()
74 {
75 m_choice = NULL;
76 m_selection = wxNOT_FOUND;
77 }
78
79 bool
80 wxChoicebook::Create(wxWindow *parent,
81 wxWindowID id,
82 const wxPoint& pos,
83 const wxSize& size,
84 long style,
85 const wxString& name)
86 {
87 if ( (style & wxCHB_ALIGN_MASK) == wxCHB_DEFAULT )
88 {
89 style |= wxCHB_TOP;
90 }
91
92 // no border for this control, it doesn't look nice together with
93 // wxChoice border
94 style &= ~wxBORDER_MASK;
95 style |= wxBORDER_NONE;
96
97 if ( !wxControl::Create(parent, id, pos, size, style,
98 wxDefaultValidator, name) )
99 return false;
100
101 m_choice = new wxChoice
102 (
103 this,
104 wxID_CHOICEBOOKCHOICE,
105 wxDefaultPosition,
106 wxDefaultSize
107 );
108
109 return true;
110 }
111
112 // ----------------------------------------------------------------------------
113 // wxChoicebook geometry management
114 // ----------------------------------------------------------------------------
115
116 wxSize wxChoicebook::GetChoiceSize() const
117 {
118 const wxSize sizeClient = GetClientSize(),
119 sizeChoice = m_choice->GetBestSize();
120
121 wxSize size;
122 if ( IsVertical() )
123 {
124 size.x = sizeClient.x;
125 size.y = sizeChoice.y;
126 }
127 else // left/right aligned
128 {
129 size.x = sizeChoice.x;
130 size.y = sizeClient.y;
131 }
132
133 return size;
134 }
135
136 wxRect wxChoicebook::GetPageRect() const
137 {
138 const wxSize sizeChoice = m_choice->GetSize();
139
140 wxRect rectPage(wxPoint(0, 0), GetClientSize());
141 switch ( GetWindowStyle() & wxCHB_ALIGN_MASK )
142 {
143 default:
144 wxFAIL_MSG( _T("unexpected wxChoicebook alignment") );
145 // fall through
146
147 case wxCHB_TOP:
148 rectPage.y = sizeChoice.y + MARGIN;
149 // fall through
150
151 case wxCHB_BOTTOM:
152 rectPage.height -= sizeChoice.y + MARGIN;
153 break;
154
155 case wxCHB_LEFT:
156 rectPage.x = sizeChoice.x + MARGIN;
157 // fall through
158
159 case wxCHB_RIGHT:
160 rectPage.width -= sizeChoice.x + MARGIN;
161 break;
162 }
163
164 return rectPage;
165 }
166
167 void wxChoicebook::OnSize(wxSizeEvent& event)
168 {
169 event.Skip();
170
171 if ( !m_choice )
172 {
173 // we're not fully created yet
174 return;
175 }
176
177 // resize the choice control and the page area to fit inside our new size
178 const wxSize sizeClient = GetClientSize(),
179 sizeChoice = GetChoiceSize();
180
181 wxPoint posChoice;
182 switch ( GetWindowStyle() & wxCHB_ALIGN_MASK )
183 {
184 default:
185 wxFAIL_MSG( _T("unexpected wxChoicebook alignment") );
186 // fall through
187
188 case wxCHB_TOP:
189 case wxCHB_LEFT:
190 // posChoice is already ok
191 break;
192
193 case wxCHB_BOTTOM:
194 posChoice.y = sizeClient.y - sizeChoice.y;
195 break;
196
197 case wxCHB_RIGHT:
198 posChoice.x = sizeClient.x - sizeChoice.x;
199 break;
200 }
201
202 m_choice->Move(posChoice.x, posChoice.y);
203 m_choice->SetSize(sizeChoice.x, sizeChoice.y);
204
205 // resize the currently shown page
206 if ( m_selection != wxNOT_FOUND )
207 {
208 wxWindow *page = m_pages[m_selection];
209 wxCHECK_RET( page, _T("NULL page in wxChoicebook?") );
210 page->SetSize(GetPageRect());
211 }
212 }
213
214 wxSize wxChoicebook::CalcSizeFromPage(const wxSize& sizePage) const
215 {
216 // we need to add the size of the choice control and the margin
217 const wxSize sizeChoice = GetChoiceSize();
218
219 wxSize size = sizePage;
220 if ( IsVertical() )
221 {
222 size.y += sizeChoice.y + MARGIN;
223 }
224 else // left/right aligned
225 {
226 size.x += sizeChoice.x + MARGIN;
227 }
228
229 return size;
230 }
231
232
233 // ----------------------------------------------------------------------------
234 // accessing the pages
235 // ----------------------------------------------------------------------------
236
237 bool wxChoicebook::SetPageText(size_t n, const wxString& strText)
238 {
239 m_choice->SetString(n, strText);
240
241 return true;
242 }
243
244 wxString wxChoicebook::GetPageText(size_t n) const
245 {
246 return m_choice->GetString(n);
247 }
248
249 int wxChoicebook::GetPageImage(size_t WXUNUSED(n)) const
250 {
251 wxFAIL_MSG( _T("wxChoicebook::GetPageImage() not implemented") );
252
253 return -1;
254 }
255
256 bool wxChoicebook::SetPageImage(size_t WXUNUSED(n), int WXUNUSED(imageId))
257 {
258 wxFAIL_MSG( _T("wxChoicebook::SetPageImage() not implemented") );
259
260 return false;
261 }
262
263 // ----------------------------------------------------------------------------
264 // image list stuff
265 // ----------------------------------------------------------------------------
266
267 void wxChoicebook::SetImageList(wxImageList *imageList)
268 {
269 // TODO: can be implemented in form of static bitmap near choice control
270
271 wxBookCtrl::SetImageList(imageList);
272 }
273
274 // ----------------------------------------------------------------------------
275 // selection
276 // ----------------------------------------------------------------------------
277
278 int wxChoicebook::GetSelection() const
279 {
280 return m_selection;
281 }
282
283 int wxChoicebook::SetSelection(size_t n)
284 {
285 wxCHECK_MSG( n < GetPageCount(), wxNOT_FOUND,
286 _T("invalid page index in wxChoicebook::SetSelection()") );
287
288 const int selOld = m_selection;
289
290 if ( (int)n != m_selection )
291 {
292 if ( m_selection != wxNOT_FOUND )
293 m_pages[m_selection]->Hide();
294 wxWindow *page = m_pages[n];
295 page->SetSize(GetPageRect());
296 page->Show();
297
298 // change m_selection only now to ignore the selection change event
299 m_selection = n;
300 m_choice->Select(n);
301 }
302
303 return selOld;
304 }
305
306 // ----------------------------------------------------------------------------
307 // adding/removing the pages
308 // ----------------------------------------------------------------------------
309
310 bool
311 wxChoicebook::InsertPage(size_t n,
312 wxWindow *page,
313 const wxString& text,
314 bool bSelect,
315 int imageId)
316 {
317 if ( !wxBookCtrl::InsertPage(n, page, text, bSelect, imageId) )
318 return false;
319
320 m_choice->Insert(text, n);
321
322 // we should always have some selection if possible
323 if ( bSelect || (m_selection == wxNOT_FOUND) )
324 {
325 SetSelection(n);
326 }
327 else // don't select this page
328 {
329 // it will be shown only when selected
330 page->Hide();
331 }
332
333 InvalidateBestSize();
334 return true;
335 }
336
337 wxWindow *wxChoicebook::DoRemovePage(size_t page)
338 {
339 const int page_count = GetPageCount();
340 wxWindow *win = wxBookCtrl::DoRemovePage(page);
341
342 if ( win )
343 {
344 m_choice->Delete(page);
345
346 if (m_selection >= (int)page)
347 {
348 // force new sel valid if possible
349 int sel = m_selection - 1;
350 if (page_count == 1)
351 sel = wxNOT_FOUND;
352 else if ((page_count == 2) || (sel == -1))
353 sel = 0;
354
355 // force sel invalid if deleting current page - don't try to hide it
356 m_selection = (m_selection == (int)page) ? wxNOT_FOUND : m_selection - 1;
357
358 if ((sel != wxNOT_FOUND) && (sel != m_selection))
359 SetSelection(sel);
360 }
361 }
362
363 return win;
364 }
365
366
367 bool wxChoicebook::DeleteAllPages()
368 {
369 m_choice->Clear();
370 return wxBookCtrl::DeleteAllPages();
371 }
372
373 // ----------------------------------------------------------------------------
374 // wxChoicebook events
375 // ----------------------------------------------------------------------------
376
377 void wxChoicebook::OnChoiceSelected(wxCommandEvent& eventChoice)
378 {
379 const int selNew = eventChoice.GetSelection();
380 const int selOld = m_selection;
381
382 if ( selNew == m_selection )
383 {
384 // this event can only come from our own Select(m_selection) below
385 // which we call when the page change is vetoed, so we should simply
386 // ignore it
387 return;
388 }
389
390 // first send "change in progress" event which may be vetoed by user
391 wxChoicebookEvent eventIng(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING, GetId());
392
393 eventIng.SetEventObject(this);
394 eventIng.SetSelection(selNew);
395 eventIng.SetOldSelection(selOld);
396 if ( GetEventHandler()->ProcessEvent(eventIng) && !eventIng.IsAllowed() )
397 {
398 m_choice->Select(m_selection);
399 return;
400 }
401
402 // change allowed: do change the page and notify the user about it
403 SetSelection(selNew);
404
405 wxChoicebookEvent eventEd(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED, GetId());
406
407 eventEd.SetEventObject(this);
408 eventEd.SetSelection(selNew);
409 eventEd.SetOldSelection(selOld);
410
411 (void)GetEventHandler()->ProcessEvent(eventEd);
412 }
413
414 #endif // wxUSE_CHOICEBOOK