]> git.saurik.com Git - wxWidgets.git/blame - src/generic/choicbkg.cpp
Include wx/timer.h according to precompiled headers of wx/wx.h (with other minor...
[wxWidgets.git] / src / generic / choicbkg.cpp
CommitLineData
f5e0b4bc 1///////////////////////////////////////////////////////////////////////////////
2ddb4d13 2// Name: src/generic/choicbkg.cpp
f5e0b4bc
WS
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
f5e0b4bc
WS
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_CHOICEBOOK
28
29#include "wx/choice.h"
30#include "wx/choicebk.h"
31#include "wx/imaglist.h"
32#include "wx/settings.h"
9916dbf6 33#include "wx/sizer.h"
f5e0b4bc 34
f5e0b4bc
WS
35// ----------------------------------------------------------------------------
36// various wxWidgets macros
37// ----------------------------------------------------------------------------
38
1f30c176
WS
39// check that the page index is valid
40#define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
41
42// ----------------------------------------------------------------------------
43// event table
44// ----------------------------------------------------------------------------
45
2ddb4d13 46IMPLEMENT_DYNAMIC_CLASS(wxChoicebook, wxBookCtrlBase)
f5e0b4bc
WS
47IMPLEMENT_DYNAMIC_CLASS(wxChoicebookEvent, wxNotifyEvent)
48
9cf0f6ae 49#if !WXWIN_COMPATIBILITY_EVENT_TYPES
f5e0b4bc
WS
50const wxEventType wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING = wxNewEventType();
51const wxEventType wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED = wxNewEventType();
9cf0f6ae 52#endif
f5e0b4bc
WS
53const int wxID_CHOICEBOOKCHOICE = wxNewId();
54
61c083e7 55BEGIN_EVENT_TABLE(wxChoicebook, wxBookCtrlBase)
f5e0b4bc
WS
56 EVT_CHOICE(wxID_CHOICEBOOKCHOICE, wxChoicebook::OnChoiceSelected)
57END_EVENT_TABLE()
58
59// ============================================================================
60// wxChoicebook implementation
61// ============================================================================
62
63// ----------------------------------------------------------------------------
64// wxChoicebook creation
65// ----------------------------------------------------------------------------
66
67void wxChoicebook::Init()
68{
f5e0b4bc
WS
69 m_selection = wxNOT_FOUND;
70}
71
72bool
73wxChoicebook::Create(wxWindow *parent,
74 wxWindowID id,
75 const wxPoint& pos,
76 const wxSize& size,
77 long style,
78 const wxString& name)
79{
2ddb4d13 80 if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT )
f5e0b4bc 81 {
2ddb4d13 82 style |= wxBK_TOP;
f5e0b4bc
WS
83 }
84
85 // no border for this control, it doesn't look nice together with
86 // wxChoice border
87 style &= ~wxBORDER_MASK;
88 style |= wxBORDER_NONE;
89
90 if ( !wxControl::Create(parent, id, pos, size, style,
91 wxDefaultValidator, name) )
92 return false;
93
2ddb4d13 94 m_bookctrl = new wxChoice
f5e0b4bc
WS
95 (
96 this,
97 wxID_CHOICEBOOKCHOICE,
98 wxDefaultPosition,
99 wxDefaultSize
100 );
101
87cf52d8 102 wxSizer* mainSizer = new wxBoxSizer(IsVertical() ? wxVERTICAL : wxHORIZONTAL);
e0d5d9af
WS
103
104 if (style & wxBK_RIGHT || style & wxBK_BOTTOM)
87cf52d8 105 mainSizer->Add(0, 0, 1, wxEXPAND, 0);
e0d5d9af 106
87cf52d8
JS
107 m_controlSizer = new wxBoxSizer(IsVertical() ? wxHORIZONTAL : wxVERTICAL);
108 m_controlSizer->Add(m_bookctrl, 1, (IsVertical() ? wxALIGN_CENTRE_VERTICAL : wxALIGN_CENTRE) |wxGROW, 0);
109 mainSizer->Add(m_controlSizer, 0, wxGROW|wxALL, m_controlMargin);
110 SetSizer(mainSizer);
f5e0b4bc
WS
111 return true;
112}
113
114// ----------------------------------------------------------------------------
115// wxChoicebook geometry management
116// ----------------------------------------------------------------------------
117
2ddb4d13 118wxSize wxChoicebook::GetControllerSize() const
f5e0b4bc
WS
119{
120 const wxSize sizeClient = GetClientSize(),
87cf52d8
JS
121 // sizeChoice = m_bookctrl->GetBestFittingSize();
122 sizeChoice = m_controlSizer->CalcMin();
f5e0b4bc
WS
123
124 wxSize size;
125 if ( IsVertical() )
126 {
127 size.x = sizeClient.x;
128 size.y = sizeChoice.y;
129 }
130 else // left/right aligned
131 {
132 size.x = sizeChoice.x;
133 size.y = sizeClient.y;
134 }
135
136 return size;
137}
138
f5e0b4bc
WS
139wxSize wxChoicebook::CalcSizeFromPage(const wxSize& sizePage) const
140{
159e6235 141 // we need to add the size of the choice control and the border between
2ddb4d13 142 const wxSize sizeChoice = GetControllerSize();
f5e0b4bc
WS
143
144 wxSize size = sizePage;
145 if ( IsVertical() )
146 {
159e6235 147 size.y += sizeChoice.y + GetInternalBorder();
f5e0b4bc
WS
148 }
149 else // left/right aligned
150 {
159e6235 151 size.x += sizeChoice.x + GetInternalBorder();
f5e0b4bc
WS
152 }
153
154 return size;
155}
156
157
158// ----------------------------------------------------------------------------
159// accessing the pages
160// ----------------------------------------------------------------------------
161
162bool wxChoicebook::SetPageText(size_t n, const wxString& strText)
163{
2ddb4d13 164 GetChoiceCtrl()->SetString(n, strText);
f5e0b4bc
WS
165
166 return true;
167}
168
169wxString wxChoicebook::GetPageText(size_t n) const
170{
2ddb4d13 171 return GetChoiceCtrl()->GetString(n);
f5e0b4bc
WS
172}
173
174int wxChoicebook::GetPageImage(size_t WXUNUSED(n)) const
175{
176 wxFAIL_MSG( _T("wxChoicebook::GetPageImage() not implemented") );
177
9f29226d 178 return wxNOT_FOUND;
f5e0b4bc
WS
179}
180
181bool wxChoicebook::SetPageImage(size_t WXUNUSED(n), int WXUNUSED(imageId))
182{
183 wxFAIL_MSG( _T("wxChoicebook::SetPageImage() not implemented") );
184
185 return false;
186}
187
188// ----------------------------------------------------------------------------
189// image list stuff
190// ----------------------------------------------------------------------------
191
192void wxChoicebook::SetImageList(wxImageList *imageList)
193{
194 // TODO: can be implemented in form of static bitmap near choice control
195
61c083e7 196 wxBookCtrlBase::SetImageList(imageList);
f5e0b4bc
WS
197}
198
199// ----------------------------------------------------------------------------
200// selection
201// ----------------------------------------------------------------------------
202
203int wxChoicebook::GetSelection() const
204{
205 return m_selection;
206}
207
208int wxChoicebook::SetSelection(size_t n)
209{
1f30c176
WS
210 wxCHECK_MSG( IS_VALID_PAGE(n), wxNOT_FOUND,
211 wxT("invalid page index in wxChoicebook::SetSelection()") );
f5e0b4bc 212
1f30c176 213 const int oldSel = m_selection;
f5e0b4bc 214
1f30c176 215 if ( int(n) != m_selection )
f5e0b4bc 216 {
1f30c176
WS
217 wxChoicebookEvent event(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING, m_windowId);
218 event.SetSelection(n);
219 event.SetOldSelection(m_selection);
220 event.SetEventObject(this);
221 if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
222 {
223 if ( m_selection != wxNOT_FOUND )
224 m_pages[m_selection]->Hide();
225
226 wxWindow *page = m_pages[n];
227 page->SetSize(GetPageRect());
228 page->Show();
229
716dc245 230 // change m_selection now to ignore the selection change event
1f30c176 231 m_selection = n;
2ddb4d13 232 GetChoiceCtrl()->Select(n);
1f30c176
WS
233
234 // program allows the page change
235 event.SetEventType(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED);
236 (void)GetEventHandler()->ProcessEvent(event);
237 }
f5e0b4bc
WS
238 }
239
1f30c176 240 return oldSel;
f5e0b4bc
WS
241}
242
f5e0b4bc
WS
243// ----------------------------------------------------------------------------
244// adding/removing the pages
245// ----------------------------------------------------------------------------
246
247bool
248wxChoicebook::InsertPage(size_t n,
249 wxWindow *page,
250 const wxString& text,
251 bool bSelect,
252 int imageId)
253{
61c083e7 254 if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) )
f5e0b4bc
WS
255 return false;
256
2ddb4d13 257 GetChoiceCtrl()->Insert(text, n);
f5e0b4bc 258
716dc245
WS
259 // if the inserted page is before the selected one, we must update the
260 // index of the selected page
261 if ( int(n) <= m_selection )
f5e0b4bc 262 {
716dc245
WS
263 // one extra page added
264 m_selection++;
2ddb4d13 265 GetChoiceCtrl()->Select(m_selection);
f5e0b4bc 266 }
716dc245
WS
267
268 // some page should be selected: either this one or the first one if there
269 // is still no selection
159e6235 270 int selNew = wxNOT_FOUND;
716dc245
WS
271 if ( bSelect )
272 selNew = n;
159e6235 273 else if ( m_selection == wxNOT_FOUND )
716dc245
WS
274 selNew = 0;
275
276 if ( selNew != m_selection )
f5e0b4bc 277 page->Hide();
716dc245 278
159e6235 279 if ( selNew != wxNOT_FOUND )
716dc245 280 SetSelection(selNew);
f5e0b4bc
WS
281
282 InvalidateBestSize();
283 return true;
284}
285
286wxWindow *wxChoicebook::DoRemovePage(size_t page)
287{
4a10ea8b 288 const size_t page_count = GetPageCount();
61c083e7 289 wxWindow *win = wxBookCtrlBase::DoRemovePage(page);
bb08a4a1 290
f5e0b4bc
WS
291 if ( win )
292 {
2ddb4d13 293 GetChoiceCtrl()->Delete(page);
bb08a4a1
WS
294
295 if (m_selection >= (int)page)
296 {
297 // force new sel valid if possible
298 int sel = m_selection - 1;
299 if (page_count == 1)
300 sel = wxNOT_FOUND;
301 else if ((page_count == 2) || (sel == -1))
302 sel = 0;
303
304 // force sel invalid if deleting current page - don't try to hide it
305 m_selection = (m_selection == (int)page) ? wxNOT_FOUND : m_selection - 1;
306
307 if ((sel != wxNOT_FOUND) && (sel != m_selection))
308 SetSelection(sel);
309 }
f5e0b4bc
WS
310 }
311
312 return win;
313}
314
315
316bool wxChoicebook::DeleteAllPages()
317{
2ddb4d13 318 GetChoiceCtrl()->Clear();
61c083e7 319 return wxBookCtrlBase::DeleteAllPages();
f5e0b4bc
WS
320}
321
322// ----------------------------------------------------------------------------
323// wxChoicebook events
324// ----------------------------------------------------------------------------
325
326void wxChoicebook::OnChoiceSelected(wxCommandEvent& eventChoice)
327{
328 const int selNew = eventChoice.GetSelection();
329
330 if ( selNew == m_selection )
331 {
332 // this event can only come from our own Select(m_selection) below
333 // which we call when the page change is vetoed, so we should simply
334 // ignore it
335 return;
336 }
337
bb08a4a1 338 SetSelection(selNew);
f5e0b4bc 339
716dc245
WS
340 // change wasn't allowed, return to previous state
341 if (m_selection != selNew)
2ddb4d13 342 GetChoiceCtrl()->Select(m_selection);
f5e0b4bc
WS
343}
344
345#endif // wxUSE_CHOICEBOOK