]> git.saurik.com Git - wxWidgets.git/blob - src/generic/choicbkg.cpp
Committed William Osborne's wxPalmOS port
[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 wxPoint pt(0, 0);
148 wxRect rectPage(pt, GetClientSize());
149 switch ( GetWindowStyle() & wxCHB_ALIGN_MASK )
150 {
151 default:
152 wxFAIL_MSG( _T("unexpected wxChoicebook alignment") );
153 // fall through
154
155 case wxCHB_TOP:
156 rectPage.y = sizeChoice.y + MARGIN;
157 // fall through
158
159 case wxCHB_BOTTOM:
160 rectPage.height -= sizeChoice.y + MARGIN;
161 break;
162
163 case wxCHB_LEFT:
164 rectPage.x = sizeChoice.x + MARGIN;
165 // fall through
166
167 case wxCHB_RIGHT:
168 rectPage.width -= sizeChoice.x + MARGIN;
169 break;
170 }
171
172 return rectPage;
173 }
174
175 void wxChoicebook::OnSize(wxSizeEvent& event)
176 {
177 event.Skip();
178
179 if ( !m_choice )
180 {
181 // we're not fully created yet
182 return;
183 }
184
185 // resize the choice control and the page area to fit inside our new size
186 const wxSize sizeClient = GetClientSize(),
187 sizeChoice = GetChoiceSize();
188
189 wxPoint posChoice;
190 switch ( GetWindowStyle() & wxCHB_ALIGN_MASK )
191 {
192 default:
193 wxFAIL_MSG( _T("unexpected wxChoicebook alignment") );
194 // fall through
195
196 case wxCHB_TOP:
197 case wxCHB_LEFT:
198 // posChoice is already ok
199 break;
200
201 case wxCHB_BOTTOM:
202 posChoice.y = sizeClient.y - sizeChoice.y;
203 break;
204
205 case wxCHB_RIGHT:
206 posChoice.x = sizeClient.x - sizeChoice.x;
207 break;
208 }
209
210 m_choice->Move(posChoice.x, posChoice.y);
211 m_choice->SetSize(sizeChoice.x, sizeChoice.y);
212
213 // resize the currently shown page
214 if ( m_selection != wxNOT_FOUND )
215 {
216 wxWindow *page = m_pages[m_selection];
217 wxCHECK_RET( page, _T("NULL page in wxChoicebook?") );
218 page->SetSize(GetPageRect());
219 }
220 }
221
222 wxSize wxChoicebook::CalcSizeFromPage(const wxSize& sizePage) const
223 {
224 // we need to add the size of the choice control and the margin
225 const wxSize sizeChoice = GetChoiceSize();
226
227 wxSize size = sizePage;
228 if ( IsVertical() )
229 {
230 size.y += sizeChoice.y + MARGIN;
231 }
232 else // left/right aligned
233 {
234 size.x += sizeChoice.x + MARGIN;
235 }
236
237 return size;
238 }
239
240
241 // ----------------------------------------------------------------------------
242 // accessing the pages
243 // ----------------------------------------------------------------------------
244
245 bool wxChoicebook::SetPageText(size_t n, const wxString& strText)
246 {
247 m_choice->SetString(n, strText);
248
249 return true;
250 }
251
252 wxString wxChoicebook::GetPageText(size_t n) const
253 {
254 return m_choice->GetString(n);
255 }
256
257 int wxChoicebook::GetPageImage(size_t WXUNUSED(n)) const
258 {
259 wxFAIL_MSG( _T("wxChoicebook::GetPageImage() not implemented") );
260
261 return -1;
262 }
263
264 bool wxChoicebook::SetPageImage(size_t WXUNUSED(n), int WXUNUSED(imageId))
265 {
266 wxFAIL_MSG( _T("wxChoicebook::SetPageImage() not implemented") );
267
268 return false;
269 }
270
271 // ----------------------------------------------------------------------------
272 // image list stuff
273 // ----------------------------------------------------------------------------
274
275 void wxChoicebook::SetImageList(wxImageList *imageList)
276 {
277 // TODO: can be implemented in form of static bitmap near choice control
278
279 wxBookCtrl::SetImageList(imageList);
280 }
281
282 // ----------------------------------------------------------------------------
283 // selection
284 // ----------------------------------------------------------------------------
285
286 int wxChoicebook::GetSelection() const
287 {
288 return m_selection;
289 }
290
291 int wxChoicebook::SetSelection(size_t n)
292 {
293 wxCHECK_MSG( IS_VALID_PAGE(n), wxNOT_FOUND,
294 wxT("invalid page index in wxChoicebook::SetSelection()") );
295
296 const int oldSel = m_selection;
297
298 if ( int(n) != m_selection )
299 {
300 wxChoicebookEvent event(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING, m_windowId);
301 event.SetSelection(n);
302 event.SetOldSelection(m_selection);
303 event.SetEventObject(this);
304 if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
305 {
306 if ( m_selection != wxNOT_FOUND )
307 m_pages[m_selection]->Hide();
308
309 wxWindow *page = m_pages[n];
310 page->SetSize(GetPageRect());
311 page->Show();
312
313 // change m_selection now to ignore the selection change event
314 m_selection = n;
315 m_choice->Select(n);
316
317 // program allows the page change
318 event.SetEventType(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED);
319 (void)GetEventHandler()->ProcessEvent(event);
320 }
321 }
322
323 return oldSel;
324 }
325
326 // ----------------------------------------------------------------------------
327 // adding/removing the pages
328 // ----------------------------------------------------------------------------
329
330 bool
331 wxChoicebook::InsertPage(size_t n,
332 wxWindow *page,
333 const wxString& text,
334 bool bSelect,
335 int imageId)
336 {
337 if ( !wxBookCtrl::InsertPage(n, page, text, bSelect, imageId) )
338 return false;
339
340 m_choice->Insert(text, n);
341
342 // if the inserted page is before the selected one, we must update the
343 // index of the selected page
344 if ( int(n) <= m_selection )
345 {
346 // one extra page added
347 m_selection++;
348 m_choice->Select(m_selection);
349 }
350
351 // some page should be selected: either this one or the first one if there
352 // is still no selection
353 int selNew = -1;
354 if ( bSelect )
355 selNew = n;
356 else if ( m_selection == -1 )
357 selNew = 0;
358
359 if ( selNew != m_selection )
360 page->Hide();
361
362 if ( selNew != -1 )
363 SetSelection(selNew);
364
365 InvalidateBestSize();
366 return true;
367 }
368
369 wxWindow *wxChoicebook::DoRemovePage(size_t page)
370 {
371 const int page_count = GetPageCount();
372 wxWindow *win = wxBookCtrl::DoRemovePage(page);
373
374 if ( win )
375 {
376 m_choice->Delete(page);
377
378 if (m_selection >= (int)page)
379 {
380 // force new sel valid if possible
381 int sel = m_selection - 1;
382 if (page_count == 1)
383 sel = wxNOT_FOUND;
384 else if ((page_count == 2) || (sel == -1))
385 sel = 0;
386
387 // force sel invalid if deleting current page - don't try to hide it
388 m_selection = (m_selection == (int)page) ? wxNOT_FOUND : m_selection - 1;
389
390 if ((sel != wxNOT_FOUND) && (sel != m_selection))
391 SetSelection(sel);
392 }
393 }
394
395 return win;
396 }
397
398
399 bool wxChoicebook::DeleteAllPages()
400 {
401 m_choice->Clear();
402 return wxBookCtrl::DeleteAllPages();
403 }
404
405 // ----------------------------------------------------------------------------
406 // wxChoicebook events
407 // ----------------------------------------------------------------------------
408
409 void wxChoicebook::OnChoiceSelected(wxCommandEvent& eventChoice)
410 {
411 const int selNew = eventChoice.GetSelection();
412
413 if ( selNew == m_selection )
414 {
415 // this event can only come from our own Select(m_selection) below
416 // which we call when the page change is vetoed, so we should simply
417 // ignore it
418 return;
419 }
420
421 SetSelection(selNew);
422
423 // change wasn't allowed, return to previous state
424 if (m_selection != selNew)
425 m_choice->Select(m_selection);
426 }
427
428 #endif // wxUSE_CHOICEBOOK