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