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