]> git.saurik.com Git - wxWidgets.git/blob - src/generic/choicbkg.cpp
4b3194bcc0a161ea48657fc039b52d0f7e2ed919
[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 void wxChoicebook::Init()
68 {
69 m_selection = wxNOT_FOUND;
70 }
71
72 bool
73 wxChoicebook::Create(wxWindow *parent,
74 wxWindowID id,
75 const wxPoint& pos,
76 const wxSize& size,
77 long style,
78 const wxString& name)
79 {
80 if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT )
81 {
82 style |= wxBK_TOP;
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
94 m_bookctrl = new wxChoice
95 (
96 this,
97 wxID_ANY,
98 wxDefaultPosition,
99 wxDefaultSize
100 );
101
102 wxSizer* mainSizer = new wxBoxSizer(IsVertical() ? wxVERTICAL : wxHORIZONTAL);
103
104 if (style & wxBK_RIGHT || style & wxBK_BOTTOM)
105 mainSizer->Add(0, 0, 1, wxEXPAND, 0);
106
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, (IsVertical() ? (int) wxGROW : (int) wxALIGN_CENTRE_VERTICAL)|wxALL, m_controlMargin);
110 SetSizer(mainSizer);
111 return true;
112 }
113
114 // ----------------------------------------------------------------------------
115 // wxChoicebook geometry management
116 // ----------------------------------------------------------------------------
117
118 wxSize wxChoicebook::GetControllerSize() const
119 {
120 const wxSize sizeClient = GetClientSize(),
121 sizeChoice = m_controlSizer->CalcMin();
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
138 wxSize wxChoicebook::CalcSizeFromPage(const wxSize& sizePage) const
139 {
140 // we need to add the size of the choice control and the border between
141 const wxSize sizeChoice = GetControllerSize();
142
143 wxSize size = sizePage;
144 if ( IsVertical() )
145 {
146 if ( sizeChoice.x > sizePage.x )
147 size.x = sizeChoice.x;
148 size.y += sizeChoice.y + GetInternalBorder();
149 }
150 else // left/right aligned
151 {
152 size.x += sizeChoice.x + GetInternalBorder();
153 if ( sizeChoice.y > sizePage.y )
154 size.y = sizeChoice.y;
155 }
156
157 return size;
158 }
159
160
161 // ----------------------------------------------------------------------------
162 // accessing the pages
163 // ----------------------------------------------------------------------------
164
165 bool wxChoicebook::SetPageText(size_t n, const wxString& strText)
166 {
167 GetChoiceCtrl()->SetString(n, strText);
168
169 return true;
170 }
171
172 wxString wxChoicebook::GetPageText(size_t n) const
173 {
174 return GetChoiceCtrl()->GetString(n);
175 }
176
177 int wxChoicebook::GetPageImage(size_t WXUNUSED(n)) const
178 {
179 wxFAIL_MSG( _T("wxChoicebook::GetPageImage() not implemented") );
180
181 return wxNOT_FOUND;
182 }
183
184 bool wxChoicebook::SetPageImage(size_t WXUNUSED(n), int WXUNUSED(imageId))
185 {
186 wxFAIL_MSG( _T("wxChoicebook::SetPageImage() not implemented") );
187
188 return false;
189 }
190
191 // ----------------------------------------------------------------------------
192 // image list stuff
193 // ----------------------------------------------------------------------------
194
195 void wxChoicebook::SetImageList(wxImageList *imageList)
196 {
197 // TODO: can be implemented in form of static bitmap near choice control
198
199 wxBookCtrlBase::SetImageList(imageList);
200 }
201
202 // ----------------------------------------------------------------------------
203 // selection
204 // ----------------------------------------------------------------------------
205
206 int wxChoicebook::GetSelection() const
207 {
208 return m_selection;
209 }
210
211 wxBookCtrlEvent* wxChoicebook::CreatePageChangingEvent() const
212 {
213 return new wxBookCtrlEvent(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING, m_windowId);
214 }
215
216 void wxChoicebook::MakeChangedEvent(wxBookCtrlEvent &event)
217 {
218 event.SetEventType(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED);
219 }
220
221 // ----------------------------------------------------------------------------
222 // adding/removing the pages
223 // ----------------------------------------------------------------------------
224
225 bool
226 wxChoicebook::InsertPage(size_t n,
227 wxWindow *page,
228 const wxString& text,
229 bool bSelect,
230 int imageId)
231 {
232 if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) )
233 return false;
234
235 GetChoiceCtrl()->Insert(text, n);
236
237 // if the inserted page is before the selected one, we must update the
238 // index of the selected page
239 if ( int(n) <= m_selection )
240 {
241 // one extra page added
242 m_selection++;
243 GetChoiceCtrl()->Select(m_selection);
244 }
245
246 // some page should be selected: either this one or the first one if there
247 // is still no selection
248 int selNew = wxNOT_FOUND;
249 if ( bSelect )
250 selNew = n;
251 else if ( m_selection == wxNOT_FOUND )
252 selNew = 0;
253
254 if ( selNew != m_selection )
255 page->Hide();
256
257 if ( selNew != wxNOT_FOUND )
258 SetSelection(selNew);
259
260 return true;
261 }
262
263 wxWindow *wxChoicebook::DoRemovePage(size_t page)
264 {
265 wxWindow *win = wxBookCtrlBase::DoRemovePage(page);
266
267 if ( win )
268 {
269 GetChoiceCtrl()->Delete(page);
270
271 if ( m_selection >= (int)page )
272 {
273 // ensure that the selection is valid
274 int sel;
275 if ( GetPageCount() == 0 )
276 sel = wxNOT_FOUND;
277 else
278 sel = m_selection ? m_selection - 1 : 0;
279
280 // if deleting current page we shouldn't try to hide it
281 m_selection = m_selection == (int)page ? wxNOT_FOUND
282 : m_selection - 1;
283
284 if ( sel != wxNOT_FOUND && sel != m_selection )
285 SetSelection(sel);
286 }
287 }
288
289 return win;
290 }
291
292
293 bool wxChoicebook::DeleteAllPages()
294 {
295 m_selection = wxNOT_FOUND;
296 GetChoiceCtrl()->Clear();
297 return wxBookCtrlBase::DeleteAllPages();
298 }
299
300 // ----------------------------------------------------------------------------
301 // wxChoicebook events
302 // ----------------------------------------------------------------------------
303
304 void wxChoicebook::OnChoiceSelected(wxCommandEvent& eventChoice)
305 {
306 if ( eventChoice.GetEventObject() != m_bookctrl )
307 {
308 eventChoice.Skip();
309 return;
310 }
311
312 const int selNew = eventChoice.GetSelection();
313
314 if ( selNew == m_selection )
315 {
316 // this event can only come from our own Select(m_selection) below
317 // which we call when the page change is vetoed, so we should simply
318 // ignore it
319 return;
320 }
321
322 SetSelection(selNew);
323
324 // change wasn't allowed, return to previous state
325 if (m_selection != selNew)
326 GetChoiceCtrl()->Select(m_selection);
327 }
328
329 #endif // wxUSE_CHOICEBOOK