]> git.saurik.com Git - wxWidgets.git/blame - src/generic/choicbkg.cpp
Correctly parse times without seconds in wxDateTime::ParseTime().
[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
f5e0b4bc 29#include "wx/choicebk.h"
9eddec69
WS
30
31#ifndef WX_PRECOMP
32 #include "wx/settings.h"
b36e08d0 33 #include "wx/choice.h"
ed2fbeb8 34 #include "wx/sizer.h"
9eddec69
WS
35#endif
36
f5e0b4bc 37#include "wx/imaglist.h"
f5e0b4bc 38
f5e0b4bc
WS
39// ----------------------------------------------------------------------------
40// various wxWidgets macros
41// ----------------------------------------------------------------------------
42
1f30c176
WS
43// check that the page index is valid
44#define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
45
46// ----------------------------------------------------------------------------
47// event table
48// ----------------------------------------------------------------------------
49
2ddb4d13 50IMPLEMENT_DYNAMIC_CLASS(wxChoicebook, wxBookCtrlBase)
f5e0b4bc 51
9b11752c
VZ
52wxDEFINE_EVENT( wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING, wxBookCtrlEvent );
53wxDEFINE_EVENT( wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED, wxBookCtrlEvent );
f5e0b4bc 54
61c083e7 55BEGIN_EVENT_TABLE(wxChoicebook, wxBookCtrlBase)
447325a4 56 EVT_CHOICE(wxID_ANY, wxChoicebook::OnChoiceSelected)
f5e0b4bc
WS
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,
447325a4 97 wxID_ANY,
f5e0b4bc
WS
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);
f2b29c64 109 mainSizer->Add(m_controlSizer, 0, (IsVertical() ? (int) wxGROW : (int) wxALIGN_CENTRE_VERTICAL)|wxALL, m_controlMargin);
87cf52d8 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 121 sizeChoice = m_controlSizer->CalcMin();
f5e0b4bc
WS
122
123 wxSize size;
124 if ( IsVertical() )
125 {
126 size.x = sizeClient.x;
127 size.y = sizeChoice.y;
128 }
129 else // left/right aligned
130 {
131 size.x = sizeChoice.x;
132 size.y = sizeClient.y;
133 }
134
135 return size;
136}
137
f5e0b4bc
WS
138wxSize wxChoicebook::CalcSizeFromPage(const wxSize& sizePage) const
139{
159e6235 140 // we need to add the size of the choice control and the border between
2ddb4d13 141 const wxSize sizeChoice = GetControllerSize();
f5e0b4bc
WS
142
143 wxSize size = sizePage;
144 if ( IsVertical() )
145 {
758a8114
VZ
146 if ( sizeChoice.x > sizePage.x )
147 size.x = sizeChoice.x;
159e6235 148 size.y += sizeChoice.y + GetInternalBorder();
f5e0b4bc
WS
149 }
150 else // left/right aligned
151 {
159e6235 152 size.x += sizeChoice.x + GetInternalBorder();
758a8114
VZ
153 if ( sizeChoice.y > sizePage.y )
154 size.y = sizeChoice.y;
f5e0b4bc
WS
155 }
156
157 return size;
158}
159
160
161// ----------------------------------------------------------------------------
162// accessing the pages
163// ----------------------------------------------------------------------------
164
165bool wxChoicebook::SetPageText(size_t n, const wxString& strText)
166{
2ddb4d13 167 GetChoiceCtrl()->SetString(n, strText);
f5e0b4bc
WS
168
169 return true;
170}
171
172wxString wxChoicebook::GetPageText(size_t n) const
173{
2ddb4d13 174 return GetChoiceCtrl()->GetString(n);
f5e0b4bc
WS
175}
176
177int wxChoicebook::GetPageImage(size_t WXUNUSED(n)) const
178{
9f29226d 179 return wxNOT_FOUND;
f5e0b4bc
WS
180}
181
182bool wxChoicebook::SetPageImage(size_t WXUNUSED(n), int WXUNUSED(imageId))
183{
c15cc7fa
VZ
184 // fail silently, the code may be written to use one of several book
185 // classes and call SetPageImage() unconditionally, it's better to just
186 // ignore it (which is the best we can do short of rewriting this class to
187 // use wxBitmapComboBox anyhow) than complain loudly about a rather
188 // harmless problem
f5e0b4bc
WS
189
190 return false;
191}
192
193// ----------------------------------------------------------------------------
64c38b4b 194// miscellaneous other stuff
f5e0b4bc
WS
195// ----------------------------------------------------------------------------
196
64c38b4b
VZ
197void wxChoicebook::DoSetWindowVariant(wxWindowVariant variant)
198{
b725fa9a
KO
199 wxBookCtrlBase::DoSetWindowVariant(variant);
200 if (m_bookctrl)
201 m_bookctrl->SetWindowVariant(variant);
64c38b4b
VZ
202}
203
f5e0b4bc
WS
204void wxChoicebook::SetImageList(wxImageList *imageList)
205{
206 // TODO: can be implemented in form of static bitmap near choice control
207
61c083e7 208 wxBookCtrlBase::SetImageList(imageList);
f5e0b4bc
WS
209}
210
211// ----------------------------------------------------------------------------
212// selection
213// ----------------------------------------------------------------------------
214
215int wxChoicebook::GetSelection() const
216{
217 return m_selection;
218}
219
3e97a905 220wxBookCtrlEvent* wxChoicebook::CreatePageChangingEvent() const
f5e0b4bc 221{
3e97a905 222 return new wxBookCtrlEvent(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING, m_windowId);
deb325e3
VZ
223}
224
3e97a905 225void wxChoicebook::MakeChangedEvent(wxBookCtrlEvent &event)
deb325e3
VZ
226{
227 event.SetEventType(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED);
f5e0b4bc
WS
228}
229
f5e0b4bc
WS
230// ----------------------------------------------------------------------------
231// adding/removing the pages
232// ----------------------------------------------------------------------------
233
234bool
235wxChoicebook::InsertPage(size_t n,
236 wxWindow *page,
237 const wxString& text,
238 bool bSelect,
239 int imageId)
240{
61c083e7 241 if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) )
f5e0b4bc
WS
242 return false;
243
2ddb4d13 244 GetChoiceCtrl()->Insert(text, n);
f5e0b4bc 245
716dc245
WS
246 // if the inserted page is before the selected one, we must update the
247 // index of the selected page
248 if ( int(n) <= m_selection )
f5e0b4bc 249 {
716dc245
WS
250 // one extra page added
251 m_selection++;
2ddb4d13 252 GetChoiceCtrl()->Select(m_selection);
f5e0b4bc 253 }
716dc245
WS
254
255 // some page should be selected: either this one or the first one if there
256 // is still no selection
159e6235 257 int selNew = wxNOT_FOUND;
716dc245
WS
258 if ( bSelect )
259 selNew = n;
159e6235 260 else if ( m_selection == wxNOT_FOUND )
716dc245
WS
261 selNew = 0;
262
263 if ( selNew != m_selection )
f5e0b4bc 264 page->Hide();
716dc245 265
159e6235 266 if ( selNew != wxNOT_FOUND )
716dc245 267 SetSelection(selNew);
f5e0b4bc 268
f5e0b4bc
WS
269 return true;
270}
271
272wxWindow *wxChoicebook::DoRemovePage(size_t page)
273{
61c083e7 274 wxWindow *win = wxBookCtrlBase::DoRemovePage(page);
bb08a4a1 275
f5e0b4bc
WS
276 if ( win )
277 {
2ddb4d13 278 GetChoiceCtrl()->Delete(page);
bb08a4a1 279
4922c5f0 280 if ( m_selection >= (int)page )
bb08a4a1 281 {
4922c5f0
VZ
282 // ensure that the selection is valid
283 int sel;
284 if ( GetPageCount() == 0 )
bb08a4a1 285 sel = wxNOT_FOUND;
4922c5f0
VZ
286 else
287 sel = m_selection ? m_selection - 1 : 0;
bb08a4a1 288
4922c5f0
VZ
289 // if deleting current page we shouldn't try to hide it
290 m_selection = m_selection == (int)page ? wxNOT_FOUND
291 : m_selection - 1;
bb08a4a1 292
4922c5f0 293 if ( sel != wxNOT_FOUND && sel != m_selection )
bb08a4a1 294 SetSelection(sel);
4922c5f0 295 }
f5e0b4bc
WS
296 }
297
298 return win;
299}
300
301
302bool wxChoicebook::DeleteAllPages()
303{
4922c5f0 304 m_selection = wxNOT_FOUND;
2ddb4d13 305 GetChoiceCtrl()->Clear();
61c083e7 306 return wxBookCtrlBase::DeleteAllPages();
f5e0b4bc
WS
307}
308
309// ----------------------------------------------------------------------------
310// wxChoicebook events
311// ----------------------------------------------------------------------------
312
313void wxChoicebook::OnChoiceSelected(wxCommandEvent& eventChoice)
314{
447325a4
VZ
315 if ( eventChoice.GetEventObject() != m_bookctrl )
316 {
317 eventChoice.Skip();
318 return;
319 }
320
f5e0b4bc
WS
321 const int selNew = eventChoice.GetSelection();
322
323 if ( selNew == m_selection )
324 {
325 // this event can only come from our own Select(m_selection) below
326 // which we call when the page change is vetoed, so we should simply
327 // ignore it
328 return;
329 }
330
bb08a4a1 331 SetSelection(selNew);
f5e0b4bc 332
716dc245
WS
333 // change wasn't allowed, return to previous state
334 if (m_selection != selNew)
2ddb4d13 335 GetChoiceCtrl()->Select(m_selection);
f5e0b4bc
WS
336}
337
338#endif // wxUSE_CHOICEBOOK