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