use wxWindow::NewControlId() instead of wxNewId() to avoid clashes with the user...
[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 const int wxID_CHOICEBOOKCHOICE = wxWindow::NewControlId();
58
59 BEGIN_EVENT_TABLE(wxChoicebook, wxBookCtrlBase)
60 EVT_CHOICE(wxID_CHOICEBOOKCHOICE, wxChoicebook::OnChoiceSelected)
61 END_EVENT_TABLE()
62
63 // ============================================================================
64 // wxChoicebook implementation
65 // ============================================================================
66
67 // ----------------------------------------------------------------------------
68 // wxChoicebook creation
69 // ----------------------------------------------------------------------------
70
71 void wxChoicebook::Init()
72 {
73 m_selection = wxNOT_FOUND;
74 }
75
76 bool
77 wxChoicebook::Create(wxWindow *parent,
78 wxWindowID id,
79 const wxPoint& pos,
80 const wxSize& size,
81 long style,
82 const wxString& name)
83 {
84 if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT )
85 {
86 style |= wxBK_TOP;
87 }
88
89 // no border for this control, it doesn't look nice together with
90 // wxChoice border
91 style &= ~wxBORDER_MASK;
92 style |= wxBORDER_NONE;
93
94 if ( !wxControl::Create(parent, id, pos, size, style,
95 wxDefaultValidator, name) )
96 return false;
97
98 m_bookctrl = new wxChoice
99 (
100 this,
101 wxID_CHOICEBOOKCHOICE,
102 wxDefaultPosition,
103 wxDefaultSize
104 );
105
106 wxSizer* mainSizer = new wxBoxSizer(IsVertical() ? wxVERTICAL : wxHORIZONTAL);
107
108 if (style & wxBK_RIGHT || style & wxBK_BOTTOM)
109 mainSizer->Add(0, 0, 1, wxEXPAND, 0);
110
111 m_controlSizer = new wxBoxSizer(IsVertical() ? wxHORIZONTAL : wxVERTICAL);
112 m_controlSizer->Add(m_bookctrl, 1, (IsVertical() ? wxALIGN_CENTRE_VERTICAL : wxALIGN_CENTRE) |wxGROW, 0);
113 mainSizer->Add(m_controlSizer, 0, wxGROW|wxALL, m_controlMargin);
114 SetSizer(mainSizer);
115 return true;
116 }
117
118 // ----------------------------------------------------------------------------
119 // wxChoicebook geometry management
120 // ----------------------------------------------------------------------------
121
122 wxSize wxChoicebook::GetControllerSize() const
123 {
124 const wxSize sizeClient = GetClientSize(),
125 sizeChoice = m_controlSizer->CalcMin();
126
127 wxSize size;
128 if ( IsVertical() )
129 {
130 size.x = sizeClient.x;
131 size.y = sizeChoice.y;
132 }
133 else // left/right aligned
134 {
135 size.x = sizeChoice.x;
136 size.y = sizeClient.y;
137 }
138
139 return size;
140 }
141
142 wxSize wxChoicebook::CalcSizeFromPage(const wxSize& sizePage) const
143 {
144 // we need to add the size of the choice control and the border between
145 const wxSize sizeChoice = GetControllerSize();
146
147 wxSize size = sizePage;
148 if ( IsVertical() )
149 {
150 size.y += sizeChoice.y + GetInternalBorder();
151 }
152 else // left/right aligned
153 {
154 size.x += sizeChoice.x + GetInternalBorder();
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 wxBookCtrlBaseEvent* wxChoicebook::CreatePageChangingEvent() const
212 {
213 return new wxChoicebookEvent(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING, m_windowId);
214 }
215
216 void wxChoicebook::MakeChangedEvent(wxBookCtrlBaseEvent &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 const size_t page_count = GetPageCount();
266 wxWindow *win = wxBookCtrlBase::DoRemovePage(page);
267
268 if ( win )
269 {
270 GetChoiceCtrl()->Delete(page);
271
272 if (m_selection >= (int)page)
273 {
274 // force new sel valid if possible
275 int sel = m_selection - 1;
276 if (page_count == 1)
277 sel = wxNOT_FOUND;
278 else if ((page_count == 2) || (sel == -1))
279 sel = 0;
280
281 // force sel invalid if deleting current page - don't try to hide it
282 m_selection = (m_selection == (int)page) ? wxNOT_FOUND : 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 GetChoiceCtrl()->Clear();
296 return wxBookCtrlBase::DeleteAllPages();
297 }
298
299 // ----------------------------------------------------------------------------
300 // wxChoicebook events
301 // ----------------------------------------------------------------------------
302
303 void wxChoicebook::OnChoiceSelected(wxCommandEvent& eventChoice)
304 {
305 const int selNew = eventChoice.GetSelection();
306
307 if ( selNew == m_selection )
308 {
309 // this event can only come from our own Select(m_selection) below
310 // which we call when the page change is vetoed, so we should simply
311 // ignore it
312 return;
313 }
314
315 SetSelection(selNew);
316
317 // change wasn't allowed, return to previous state
318 if (m_selection != selNew)
319 GetChoiceCtrl()->Select(m_selection);
320 }
321
322 #endif // wxUSE_CHOICEBOOK