]> git.saurik.com Git - wxWidgets.git/blame - src/generic/choicbkg.cpp
restore SetSelection(-1, -1) behaviour which was broken during wxTextEntry refactoring
[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
WS
51IMPLEMENT_DYNAMIC_CLASS(wxChoicebookEvent, wxNotifyEvent)
52
53const wxEventType wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING = wxNewEventType();
54const wxEventType wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED = wxNewEventType();
f5e0b4bc 55
61c083e7 56BEGIN_EVENT_TABLE(wxChoicebook, wxBookCtrlBase)
447325a4 57 EVT_CHOICE(wxID_ANY, wxChoicebook::OnChoiceSelected)
f5e0b4bc
WS
58END_EVENT_TABLE()
59
60// ============================================================================
61// wxChoicebook implementation
62// ============================================================================
63
64// ----------------------------------------------------------------------------
65// wxChoicebook creation
66// ----------------------------------------------------------------------------
67
68void wxChoicebook::Init()
69{
f5e0b4bc
WS
70 m_selection = wxNOT_FOUND;
71}
72
73bool
74wxChoicebook::Create(wxWindow *parent,
75 wxWindowID id,
76 const wxPoint& pos,
77 const wxSize& size,
78 long style,
79 const wxString& name)
80{
2ddb4d13 81 if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT )
f5e0b4bc 82 {
2ddb4d13 83 style |= wxBK_TOP;
f5e0b4bc
WS
84 }
85
86 // no border for this control, it doesn't look nice together with
87 // wxChoice border
88 style &= ~wxBORDER_MASK;
89 style |= wxBORDER_NONE;
90
91 if ( !wxControl::Create(parent, id, pos, size, style,
92 wxDefaultValidator, name) )
93 return false;
94
2ddb4d13 95 m_bookctrl = new wxChoice
f5e0b4bc
WS
96 (
97 this,
447325a4 98 wxID_ANY,
f5e0b4bc
WS
99 wxDefaultPosition,
100 wxDefaultSize
101 );
102
87cf52d8 103 wxSizer* mainSizer = new wxBoxSizer(IsVertical() ? wxVERTICAL : wxHORIZONTAL);
e0d5d9af
WS
104
105 if (style & wxBK_RIGHT || style & wxBK_BOTTOM)
87cf52d8 106 mainSizer->Add(0, 0, 1, wxEXPAND, 0);
e0d5d9af 107
87cf52d8
JS
108 m_controlSizer = new wxBoxSizer(IsVertical() ? wxHORIZONTAL : wxVERTICAL);
109 m_controlSizer->Add(m_bookctrl, 1, (IsVertical() ? wxALIGN_CENTRE_VERTICAL : wxALIGN_CENTRE) |wxGROW, 0);
f2b29c64 110 mainSizer->Add(m_controlSizer, 0, (IsVertical() ? (int) wxGROW : (int) wxALIGN_CENTRE_VERTICAL)|wxALL, m_controlMargin);
87cf52d8 111 SetSizer(mainSizer);
f5e0b4bc
WS
112 return true;
113}
114
115// ----------------------------------------------------------------------------
116// wxChoicebook geometry management
117// ----------------------------------------------------------------------------
118
2ddb4d13 119wxSize wxChoicebook::GetControllerSize() const
f5e0b4bc
WS
120{
121 const wxSize sizeClient = GetClientSize(),
87cf52d8 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
deb325e3 208wxBookCtrlBaseEvent* wxChoicebook::CreatePageChangingEvent() const
f5e0b4bc 209{
deb325e3
VZ
210 return new wxChoicebookEvent(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING, m_windowId);
211}
212
213void wxChoicebook::MakeChangedEvent(wxBookCtrlBaseEvent &event)
214{
215 event.SetEventType(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED);
f5e0b4bc
WS
216}
217
f5e0b4bc
WS
218// ----------------------------------------------------------------------------
219// adding/removing the pages
220// ----------------------------------------------------------------------------
221
222bool
223wxChoicebook::InsertPage(size_t n,
224 wxWindow *page,
225 const wxString& text,
226 bool bSelect,
227 int imageId)
228{
61c083e7 229 if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) )
f5e0b4bc
WS
230 return false;
231
2ddb4d13 232 GetChoiceCtrl()->Insert(text, n);
f5e0b4bc 233
716dc245
WS
234 // if the inserted page is before the selected one, we must update the
235 // index of the selected page
236 if ( int(n) <= m_selection )
f5e0b4bc 237 {
716dc245
WS
238 // one extra page added
239 m_selection++;
2ddb4d13 240 GetChoiceCtrl()->Select(m_selection);
f5e0b4bc 241 }
716dc245
WS
242
243 // some page should be selected: either this one or the first one if there
244 // is still no selection
159e6235 245 int selNew = wxNOT_FOUND;
716dc245
WS
246 if ( bSelect )
247 selNew = n;
159e6235 248 else if ( m_selection == wxNOT_FOUND )
716dc245
WS
249 selNew = 0;
250
251 if ( selNew != m_selection )
f5e0b4bc 252 page->Hide();
716dc245 253
159e6235 254 if ( selNew != wxNOT_FOUND )
716dc245 255 SetSelection(selNew);
f5e0b4bc 256
f5e0b4bc
WS
257 return true;
258}
259
260wxWindow *wxChoicebook::DoRemovePage(size_t page)
261{
4a10ea8b 262 const size_t page_count = GetPageCount();
61c083e7 263 wxWindow *win = wxBookCtrlBase::DoRemovePage(page);
bb08a4a1 264
f5e0b4bc
WS
265 if ( win )
266 {
2ddb4d13 267 GetChoiceCtrl()->Delete(page);
bb08a4a1
WS
268
269 if (m_selection >= (int)page)
270 {
271 // force new sel valid if possible
272 int sel = m_selection - 1;
273 if (page_count == 1)
274 sel = wxNOT_FOUND;
275 else if ((page_count == 2) || (sel == -1))
276 sel = 0;
277
278 // force sel invalid if deleting current page - don't try to hide it
279 m_selection = (m_selection == (int)page) ? wxNOT_FOUND : m_selection - 1;
280
281 if ((sel != wxNOT_FOUND) && (sel != m_selection))
282 SetSelection(sel);
283 }
f5e0b4bc
WS
284 }
285
286 return win;
287}
288
289
290bool wxChoicebook::DeleteAllPages()
291{
2ddb4d13 292 GetChoiceCtrl()->Clear();
61c083e7 293 return wxBookCtrlBase::DeleteAllPages();
f5e0b4bc
WS
294}
295
296// ----------------------------------------------------------------------------
297// wxChoicebook events
298// ----------------------------------------------------------------------------
299
300void wxChoicebook::OnChoiceSelected(wxCommandEvent& eventChoice)
301{
447325a4
VZ
302 if ( eventChoice.GetEventObject() != m_bookctrl )
303 {
304 eventChoice.Skip();
305 return;
306 }
307
f5e0b4bc
WS
308 const int selNew = eventChoice.GetSelection();
309
310 if ( selNew == m_selection )
311 {
312 // this event can only come from our own Select(m_selection) below
313 // which we call when the page change is vetoed, so we should simply
314 // ignore it
315 return;
316 }
317
bb08a4a1 318 SetSelection(selNew);
f5e0b4bc 319
716dc245
WS
320 // change wasn't allowed, return to previous state
321 if (m_selection != selNew)
2ddb4d13 322 GetChoiceCtrl()->Select(m_selection);
f5e0b4bc
WS
323}
324
325#endif // wxUSE_CHOICEBOOK