]> git.saurik.com Git - wxWidgets.git/blame - src/generic/choicbkg.cpp
warning fixes unsigned->signed
[wxWidgets.git] / src / generic / choicbkg.cpp
CommitLineData
f5e0b4bc
WS
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__)
44const wxCoord MARGIN = 1;
45#else
46const wxCoord MARGIN = 5;
47#endif
48
49// ----------------------------------------------------------------------------
50// various wxWidgets macros
51// ----------------------------------------------------------------------------
52
1f30c176
WS
53// check that the page index is valid
54#define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
55
56// ----------------------------------------------------------------------------
57// event table
58// ----------------------------------------------------------------------------
59
f5e0b4bc
WS
60IMPLEMENT_DYNAMIC_CLASS(wxChoicebook, wxControl)
61IMPLEMENT_DYNAMIC_CLASS(wxChoicebookEvent, wxNotifyEvent)
62
63const wxEventType wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING = wxNewEventType();
64const wxEventType wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED = wxNewEventType();
65const int wxID_CHOICEBOOKCHOICE = wxNewId();
66
67BEGIN_EVENT_TABLE(wxChoicebook, wxBookCtrl)
68 EVT_SIZE(wxChoicebook::OnSize)
69 EVT_CHOICE(wxID_CHOICEBOOKCHOICE, wxChoicebook::OnChoiceSelected)
70END_EVENT_TABLE()
71
72// ============================================================================
73// wxChoicebook implementation
74// ============================================================================
75
76// ----------------------------------------------------------------------------
77// wxChoicebook creation
78// ----------------------------------------------------------------------------
79
80void wxChoicebook::Init()
81{
82 m_choice = NULL;
83 m_selection = wxNOT_FOUND;
84}
85
86bool
87wxChoicebook::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
123wxSize 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
143wxRect 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
174void 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
bb08a4a1 212 // resize the currently shown page
f5e0b4bc
WS
213 if ( m_selection != wxNOT_FOUND )
214 {
215 wxWindow *page = m_pages[m_selection];
216 wxCHECK_RET( page, _T("NULL page in wxChoicebook?") );
f5e0b4bc 217 page->SetSize(GetPageRect());
f5e0b4bc
WS
218 }
219}
220
221wxSize 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
244bool wxChoicebook::SetPageText(size_t n, const wxString& strText)
245{
246 m_choice->SetString(n, strText);
247
248 return true;
249}
250
251wxString wxChoicebook::GetPageText(size_t n) const
252{
253 return m_choice->GetString(n);
254}
255
256int wxChoicebook::GetPageImage(size_t WXUNUSED(n)) const
257{
258 wxFAIL_MSG( _T("wxChoicebook::GetPageImage() not implemented") );
259
260 return -1;
261}
262
263bool 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
274void 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
285int wxChoicebook::GetSelection() const
286{
287 return m_selection;
288}
289
290int wxChoicebook::SetSelection(size_t n)
291{
1f30c176
WS
292 wxCHECK_MSG( IS_VALID_PAGE(n), wxNOT_FOUND,
293 wxT("invalid page index in wxChoicebook::SetSelection()") );
f5e0b4bc 294
1f30c176 295 const int oldSel = m_selection;
f5e0b4bc 296
1f30c176 297 if ( int(n) != m_selection )
f5e0b4bc 298 {
1f30c176
WS
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 m_selection = n;
313 m_choice->Select(n);
314
315 // program allows the page change
316 event.SetEventType(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED);
317 (void)GetEventHandler()->ProcessEvent(event);
318 }
f5e0b4bc
WS
319 }
320
1f30c176 321 return oldSel;
f5e0b4bc
WS
322}
323
f5e0b4bc
WS
324// ----------------------------------------------------------------------------
325// adding/removing the pages
326// ----------------------------------------------------------------------------
327
328bool
329wxChoicebook::InsertPage(size_t n,
330 wxWindow *page,
331 const wxString& text,
332 bool bSelect,
333 int imageId)
334{
335 if ( !wxBookCtrl::InsertPage(n, page, text, bSelect, imageId) )
336 return false;
337
338 m_choice->Insert(text, n);
339
bb08a4a1
WS
340 // we should always have some selection if possible
341 if ( bSelect || (m_selection == wxNOT_FOUND) )
f5e0b4bc 342 {
bb08a4a1 343 SetSelection(n);
f5e0b4bc
WS
344 }
345 else // don't select this page
346 {
347 // it will be shown only when selected
348 page->Hide();
349 }
350
351 InvalidateBestSize();
352 return true;
353}
354
355wxWindow *wxChoicebook::DoRemovePage(size_t page)
356{
bb08a4a1 357 const int page_count = GetPageCount();
f5e0b4bc 358 wxWindow *win = wxBookCtrl::DoRemovePage(page);
bb08a4a1 359
f5e0b4bc
WS
360 if ( win )
361 {
362 m_choice->Delete(page);
bb08a4a1
WS
363
364 if (m_selection >= (int)page)
365 {
366 // force new sel valid if possible
367 int sel = m_selection - 1;
368 if (page_count == 1)
369 sel = wxNOT_FOUND;
370 else if ((page_count == 2) || (sel == -1))
371 sel = 0;
372
373 // force sel invalid if deleting current page - don't try to hide it
374 m_selection = (m_selection == (int)page) ? wxNOT_FOUND : m_selection - 1;
375
376 if ((sel != wxNOT_FOUND) && (sel != m_selection))
377 SetSelection(sel);
378 }
f5e0b4bc
WS
379 }
380
381 return win;
382}
383
384
385bool wxChoicebook::DeleteAllPages()
386{
387 m_choice->Clear();
388 return wxBookCtrl::DeleteAllPages();
389}
390
391// ----------------------------------------------------------------------------
392// wxChoicebook events
393// ----------------------------------------------------------------------------
394
395void wxChoicebook::OnChoiceSelected(wxCommandEvent& eventChoice)
396{
397 const int selNew = eventChoice.GetSelection();
bb08a4a1 398 const int selOld = m_selection;
f5e0b4bc
WS
399
400 if ( selNew == m_selection )
401 {
402 // this event can only come from our own Select(m_selection) below
403 // which we call when the page change is vetoed, so we should simply
404 // ignore it
405 return;
406 }
407
408 // first send "change in progress" event which may be vetoed by user
409 wxChoicebookEvent eventIng(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING, GetId());
410
411 eventIng.SetEventObject(this);
412 eventIng.SetSelection(selNew);
bb08a4a1 413 eventIng.SetOldSelection(selOld);
f5e0b4bc
WS
414 if ( GetEventHandler()->ProcessEvent(eventIng) && !eventIng.IsAllowed() )
415 {
416 m_choice->Select(m_selection);
417 return;
418 }
419
420 // change allowed: do change the page and notify the user about it
bb08a4a1 421 SetSelection(selNew);
f5e0b4bc
WS
422
423 wxChoicebookEvent eventEd(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED, GetId());
424
425 eventEd.SetEventObject(this);
426 eventEd.SetSelection(selNew);
bb08a4a1 427 eventEd.SetOldSelection(selOld);
f5e0b4bc
WS
428
429 (void)GetEventHandler()->ProcessEvent(eventEd);
430}
431
432#endif // wxUSE_CHOICEBOOK