Refactor: use wxBookCtrlBase::m_selection in all derived classes.
[wxWidgets.git] / src / generic / choicbkg.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 // 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/choicebk.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/settings.h"
33 #include "wx/choice.h"
34 #include "wx/sizer.h"
35 #endif
36
37 #include "wx/imaglist.h"
38
39 // ----------------------------------------------------------------------------
40 // various wxWidgets macros
41 // ----------------------------------------------------------------------------
42
43 // check that the page index is valid
44 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
45
46 // ----------------------------------------------------------------------------
47 // event table
48 // ----------------------------------------------------------------------------
49
50 IMPLEMENT_DYNAMIC_CLASS(wxChoicebook, wxBookCtrlBase)
51
52 wxDEFINE_EVENT( wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING, wxBookCtrlEvent );
53 wxDEFINE_EVENT( wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED, wxBookCtrlEvent );
54
55 BEGIN_EVENT_TABLE(wxChoicebook, wxBookCtrlBase)
56 EVT_CHOICE(wxID_ANY, wxChoicebook::OnChoiceSelected)
57 END_EVENT_TABLE()
58
59 // ============================================================================
60 // wxChoicebook implementation
61 // ============================================================================
62
63 // ----------------------------------------------------------------------------
64 // wxChoicebook creation
65 // ----------------------------------------------------------------------------
66
67 bool
68 wxChoicebook::Create(wxWindow *parent,
69 wxWindowID id,
70 const wxPoint& pos,
71 const wxSize& size,
72 long style,
73 const wxString& name)
74 {
75 if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT )
76 {
77 style |= wxBK_TOP;
78 }
79
80 // no border for this control, it doesn't look nice together with
81 // wxChoice border
82 style &= ~wxBORDER_MASK;
83 style |= wxBORDER_NONE;
84
85 if ( !wxControl::Create(parent, id, pos, size, style,
86 wxDefaultValidator, name) )
87 return false;
88
89 m_bookctrl = new wxChoice
90 (
91 this,
92 wxID_ANY,
93 wxDefaultPosition,
94 wxDefaultSize
95 );
96
97 wxSizer* mainSizer = new wxBoxSizer(IsVertical() ? wxVERTICAL : wxHORIZONTAL);
98
99 if (style & wxBK_RIGHT || style & wxBK_BOTTOM)
100 mainSizer->Add(0, 0, 1, wxEXPAND, 0);
101
102 m_controlSizer = new wxBoxSizer(IsVertical() ? wxHORIZONTAL : wxVERTICAL);
103 m_controlSizer->Add(m_bookctrl, 1, (IsVertical() ? wxALIGN_CENTRE_VERTICAL : wxALIGN_CENTRE) |wxGROW, 0);
104 mainSizer->Add(m_controlSizer, 0, (IsVertical() ? (int) wxGROW : (int) wxALIGN_CENTRE_VERTICAL)|wxALL, m_controlMargin);
105 SetSizer(mainSizer);
106 return true;
107 }
108
109 // ----------------------------------------------------------------------------
110 // accessing the pages
111 // ----------------------------------------------------------------------------
112
113 bool wxChoicebook::SetPageText(size_t n, const wxString& strText)
114 {
115 GetChoiceCtrl()->SetString(n, strText);
116
117 return true;
118 }
119
120 wxString wxChoicebook::GetPageText(size_t n) const
121 {
122 return GetChoiceCtrl()->GetString(n);
123 }
124
125 int wxChoicebook::GetPageImage(size_t WXUNUSED(n)) const
126 {
127 return wxNOT_FOUND;
128 }
129
130 bool wxChoicebook::SetPageImage(size_t WXUNUSED(n), int WXUNUSED(imageId))
131 {
132 // fail silently, the code may be written to use one of several book
133 // classes and call SetPageImage() unconditionally, it's better to just
134 // ignore it (which is the best we can do short of rewriting this class to
135 // use wxBitmapComboBox anyhow) than complain loudly about a rather
136 // harmless problem
137
138 return false;
139 }
140
141 // ----------------------------------------------------------------------------
142 // miscellaneous other stuff
143 // ----------------------------------------------------------------------------
144
145 void wxChoicebook::DoSetWindowVariant(wxWindowVariant variant)
146 {
147 wxBookCtrlBase::DoSetWindowVariant(variant);
148 if (m_bookctrl)
149 m_bookctrl->SetWindowVariant(variant);
150 }
151
152 void wxChoicebook::SetImageList(wxImageList *imageList)
153 {
154 // TODO: can be implemented in form of static bitmap near choice control
155
156 wxBookCtrlBase::SetImageList(imageList);
157 }
158
159 // ----------------------------------------------------------------------------
160 // selection
161 // ----------------------------------------------------------------------------
162
163 wxBookCtrlEvent* wxChoicebook::CreatePageChangingEvent() const
164 {
165 return new wxBookCtrlEvent(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING, m_windowId);
166 }
167
168 void wxChoicebook::MakeChangedEvent(wxBookCtrlEvent &event)
169 {
170 event.SetEventType(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED);
171 }
172
173 // ----------------------------------------------------------------------------
174 // adding/removing the pages
175 // ----------------------------------------------------------------------------
176
177 bool
178 wxChoicebook::InsertPage(size_t n,
179 wxWindow *page,
180 const wxString& text,
181 bool bSelect,
182 int imageId)
183 {
184 if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) )
185 return false;
186
187 GetChoiceCtrl()->Insert(text, n);
188
189 // if the inserted page is before the selected one, we must update the
190 // index of the selected page
191 if ( int(n) <= m_selection )
192 {
193 // one extra page added
194 m_selection++;
195 GetChoiceCtrl()->Select(m_selection);
196 }
197
198 // some page should be selected: either this one or the first one if there
199 // is still no selection
200 int selNew = wxNOT_FOUND;
201 if ( bSelect )
202 selNew = n;
203 else if ( m_selection == wxNOT_FOUND )
204 selNew = 0;
205
206 if ( selNew != m_selection )
207 page->Hide();
208
209 if ( selNew != wxNOT_FOUND )
210 SetSelection(selNew);
211
212 return true;
213 }
214
215 wxWindow *wxChoicebook::DoRemovePage(size_t page)
216 {
217 wxWindow *win = wxBookCtrlBase::DoRemovePage(page);
218
219 if ( win )
220 {
221 GetChoiceCtrl()->Delete(page);
222
223 if ( m_selection >= (int)page )
224 {
225 // ensure that the selection is valid
226 int sel;
227 if ( GetPageCount() == 0 )
228 sel = wxNOT_FOUND;
229 else
230 sel = m_selection ? m_selection - 1 : 0;
231
232 // if deleting current page we shouldn't try to hide it
233 m_selection = m_selection == (int)page ? wxNOT_FOUND
234 : m_selection - 1;
235
236 if ( sel != wxNOT_FOUND && sel != m_selection )
237 SetSelection(sel);
238 }
239 }
240
241 return win;
242 }
243
244
245 bool wxChoicebook::DeleteAllPages()
246 {
247 GetChoiceCtrl()->Clear();
248 return wxBookCtrlBase::DeleteAllPages();
249 }
250
251 // ----------------------------------------------------------------------------
252 // wxChoicebook events
253 // ----------------------------------------------------------------------------
254
255 void wxChoicebook::OnChoiceSelected(wxCommandEvent& eventChoice)
256 {
257 if ( eventChoice.GetEventObject() != m_bookctrl )
258 {
259 eventChoice.Skip();
260 return;
261 }
262
263 const int selNew = eventChoice.GetSelection();
264
265 if ( selNew == m_selection )
266 {
267 // this event can only come from our own Select(m_selection) below
268 // which we call when the page change is vetoed, so we should simply
269 // ignore it
270 return;
271 }
272
273 SetSelection(selNew);
274
275 // change wasn't allowed, return to previous state
276 if (m_selection != selNew)
277 GetChoiceCtrl()->Select(m_selection);
278 }
279
280 #endif // wxUSE_CHOICEBOOK