Removed doubled events, corrected behaviour (patch #1037434)
[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 // check that the page index is valid
54 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
55
56 // ----------------------------------------------------------------------------
57 // event table
58 // ----------------------------------------------------------------------------
59
60 IMPLEMENT_DYNAMIC_CLASS(wxChoicebook, wxControl)
61 IMPLEMENT_DYNAMIC_CLASS(wxChoicebookEvent, wxNotifyEvent)
62
63 const wxEventType wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING = wxNewEventType();
64 const wxEventType wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED = wxNewEventType();
65 const int wxID_CHOICEBOOKCHOICE = wxNewId();
66
67 BEGIN_EVENT_TABLE(wxChoicebook, wxBookCtrl)
68 EVT_SIZE(wxChoicebook::OnSize)
69 EVT_CHOICE(wxID_CHOICEBOOKCHOICE, wxChoicebook::OnChoiceSelected)
70 END_EVENT_TABLE()
71
72 // ============================================================================
73 // wxChoicebook implementation
74 // ============================================================================
75
76 // ----------------------------------------------------------------------------
77 // wxChoicebook creation
78 // ----------------------------------------------------------------------------
79
80 void wxChoicebook::Init()
81 {
82 m_choice = NULL;
83 m_selection = wxNOT_FOUND;
84 }
85
86 bool
87 wxChoicebook::Create(wxWindow *parent,
88 wxWindowID id,
89 const wxPoint& pos,
90 const wxSize& size,
91 long style,
92 const wxString& name)
93 {
94 if ( (style & wxCHB_ALIGN_MASK) == wxCHB_DEFAULT )
95 {
96 style |= wxCHB_TOP;
97 }
98
99 // no border for this control, it doesn't look nice together with
100 // wxChoice border
101 style &= ~wxBORDER_MASK;
102 style |= wxBORDER_NONE;
103
104 if ( !wxControl::Create(parent, id, pos, size, style,
105 wxDefaultValidator, name) )
106 return false;
107
108 m_choice = new wxChoice
109 (
110 this,
111 wxID_CHOICEBOOKCHOICE,
112 wxDefaultPosition,
113 wxDefaultSize
114 );
115
116 return true;
117 }
118
119 // ----------------------------------------------------------------------------
120 // wxChoicebook geometry management
121 // ----------------------------------------------------------------------------
122
123 wxSize wxChoicebook::GetChoiceSize() const
124 {
125 const wxSize sizeClient = GetClientSize(),
126 sizeChoice = m_choice->GetBestSize();
127
128 wxSize size;
129 if ( IsVertical() )
130 {
131 size.x = sizeClient.x;
132 size.y = sizeChoice.y;
133 }
134 else // left/right aligned
135 {
136 size.x = sizeChoice.x;
137 size.y = sizeClient.y;
138 }
139
140 return size;
141 }
142
143 wxRect wxChoicebook::GetPageRect() const
144 {
145 const wxSize sizeChoice = m_choice->GetSize();
146
147 wxRect rectPage(wxPoint(0, 0), GetClientSize());
148 switch ( GetWindowStyle() & wxCHB_ALIGN_MASK )
149 {
150 default:
151 wxFAIL_MSG( _T("unexpected wxChoicebook alignment") );
152 // fall through
153
154 case wxCHB_TOP:
155 rectPage.y = sizeChoice.y + MARGIN;
156 // fall through
157
158 case wxCHB_BOTTOM:
159 rectPage.height -= sizeChoice.y + MARGIN;
160 break;
161
162 case wxCHB_LEFT:
163 rectPage.x = sizeChoice.x + MARGIN;
164 // fall through
165
166 case wxCHB_RIGHT:
167 rectPage.width -= sizeChoice.x + MARGIN;
168 break;
169 }
170
171 return rectPage;
172 }
173
174 void wxChoicebook::OnSize(wxSizeEvent& event)
175 {
176 event.Skip();
177
178 if ( !m_choice )
179 {
180 // we're not fully created yet
181 return;
182 }
183
184 // resize the choice control and the page area to fit inside our new size
185 const wxSize sizeClient = GetClientSize(),
186 sizeChoice = GetChoiceSize();
187
188 wxPoint posChoice;
189 switch ( GetWindowStyle() & wxCHB_ALIGN_MASK )
190 {
191 default:
192 wxFAIL_MSG( _T("unexpected wxChoicebook alignment") );
193 // fall through
194
195 case wxCHB_TOP:
196 case wxCHB_LEFT:
197 // posChoice is already ok
198 break;
199
200 case wxCHB_BOTTOM:
201 posChoice.y = sizeClient.y - sizeChoice.y;
202 break;
203
204 case wxCHB_RIGHT:
205 posChoice.x = sizeClient.x - sizeChoice.x;
206 break;
207 }
208
209 m_choice->Move(posChoice.x, posChoice.y);
210 m_choice->SetSize(sizeChoice.x, sizeChoice.y);
211
212 // resize the currently shown page
213 if ( m_selection != wxNOT_FOUND )
214 {
215 wxWindow *page = m_pages[m_selection];
216 wxCHECK_RET( page, _T("NULL page in wxChoicebook?") );
217 page->SetSize(GetPageRect());
218 }
219 }
220
221 wxSize wxChoicebook::CalcSizeFromPage(const wxSize& sizePage) const
222 {
223 // we need to add the size of the choice control and the margin
224 const wxSize sizeChoice = GetChoiceSize();
225
226 wxSize size = sizePage;
227 if ( IsVertical() )
228 {
229 size.y += sizeChoice.y + MARGIN;
230 }
231 else // left/right aligned
232 {
233 size.x += sizeChoice.x + MARGIN;
234 }
235
236 return size;
237 }
238
239
240 // ----------------------------------------------------------------------------
241 // accessing the pages
242 // ----------------------------------------------------------------------------
243
244 bool wxChoicebook::SetPageText(size_t n, const wxString& strText)
245 {
246 m_choice->SetString(n, strText);
247
248 return true;
249 }
250
251 wxString wxChoicebook::GetPageText(size_t n) const
252 {
253 return m_choice->GetString(n);
254 }
255
256 int wxChoicebook::GetPageImage(size_t WXUNUSED(n)) const
257 {
258 wxFAIL_MSG( _T("wxChoicebook::GetPageImage() not implemented") );
259
260 return -1;
261 }
262
263 bool wxChoicebook::SetPageImage(size_t WXUNUSED(n), int WXUNUSED(imageId))
264 {
265 wxFAIL_MSG( _T("wxChoicebook::SetPageImage() not implemented") );
266
267 return false;
268 }
269
270 // ----------------------------------------------------------------------------
271 // image list stuff
272 // ----------------------------------------------------------------------------
273
274 void wxChoicebook::SetImageList(wxImageList *imageList)
275 {
276 // TODO: can be implemented in form of static bitmap near choice control
277
278 wxBookCtrl::SetImageList(imageList);
279 }
280
281 // ----------------------------------------------------------------------------
282 // selection
283 // ----------------------------------------------------------------------------
284
285 int wxChoicebook::GetSelection() const
286 {
287 return m_selection;
288 }
289
290 int wxChoicebook::SetSelection(size_t n)
291 {
292 wxCHECK_MSG( IS_VALID_PAGE(n), wxNOT_FOUND,
293 wxT("invalid page index in wxChoicebook::SetSelection()") );
294
295 const int oldSel = m_selection;
296
297 if ( int(n) != m_selection )
298 {
299 wxChoicebookEvent event(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING, m_windowId);
300 event.SetSelection(n);
301 event.SetOldSelection(m_selection);
302 event.SetEventObject(this);
303 if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
304 {
305 if ( m_selection != wxNOT_FOUND )
306 m_pages[m_selection]->Hide();
307
308 wxWindow *page = m_pages[n];
309 page->SetSize(GetPageRect());
310 page->Show();
311
312 // change m_selection now to ignore the selection change event
313 m_selection = n;
314 m_choice->Select(n);
315
316 // program allows the page change
317 event.SetEventType(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED);
318 (void)GetEventHandler()->ProcessEvent(event);
319 }
320 }
321
322 return oldSel;
323 }
324
325 // ----------------------------------------------------------------------------
326 // adding/removing the pages
327 // ----------------------------------------------------------------------------
328
329 bool
330 wxChoicebook::InsertPage(size_t n,
331 wxWindow *page,
332 const wxString& text,
333 bool bSelect,
334 int imageId)
335 {
336 if ( !wxBookCtrl::InsertPage(n, page, text, bSelect, imageId) )
337 return false;
338
339 m_choice->Insert(text, n);
340
341 // if the inserted page is before the selected one, we must update the
342 // index of the selected page
343 if ( int(n) <= m_selection )
344 {
345 // one extra page added
346 m_selection++;
347 m_choice->Select(m_selection);
348 }
349
350 // some page should be selected: either this one or the first one if there
351 // is still no selection
352 int selNew = -1;
353 if ( bSelect )
354 selNew = n;
355 else if ( m_selection == -1 )
356 selNew = 0;
357
358 if ( selNew != m_selection )
359 page->Hide();
360
361 if ( selNew != -1 )
362 SetSelection(selNew);
363
364 InvalidateBestSize();
365 return true;
366 }
367
368 wxWindow *wxChoicebook::DoRemovePage(size_t page)
369 {
370 const int page_count = GetPageCount();
371 wxWindow *win = wxBookCtrl::DoRemovePage(page);
372
373 if ( win )
374 {
375 m_choice->Delete(page);
376
377 if (m_selection >= (int)page)
378 {
379 // force new sel valid if possible
380 int sel = m_selection - 1;
381 if (page_count == 1)
382 sel = wxNOT_FOUND;
383 else if ((page_count == 2) || (sel == -1))
384 sel = 0;
385
386 // force sel invalid if deleting current page - don't try to hide it
387 m_selection = (m_selection == (int)page) ? wxNOT_FOUND : m_selection - 1;
388
389 if ((sel != wxNOT_FOUND) && (sel != m_selection))
390 SetSelection(sel);
391 }
392 }
393
394 return win;
395 }
396
397
398 bool wxChoicebook::DeleteAllPages()
399 {
400 m_choice->Clear();
401 return wxBookCtrl::DeleteAllPages();
402 }
403
404 // ----------------------------------------------------------------------------
405 // wxChoicebook events
406 // ----------------------------------------------------------------------------
407
408 void wxChoicebook::OnChoiceSelected(wxCommandEvent& eventChoice)
409 {
410 const int selNew = eventChoice.GetSelection();
411
412 if ( selNew == m_selection )
413 {
414 // this event can only come from our own Select(m_selection) below
415 // which we call when the page change is vetoed, so we should simply
416 // ignore it
417 return;
418 }
419
420 SetSelection(selNew);
421
422 // change wasn't allowed, return to previous state
423 if (m_selection != selNew)
424 m_choice->Select(m_selection);
425 }
426
427 #endif // wxUSE_CHOICEBOOK