]>
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 | #if !WXWIN_COMPATIBILITY_EVENT_TYPES | |
50 | const wxEventType wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING = wxNewEventType(); | |
51 | const wxEventType wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED = wxNewEventType(); | |
52 | #endif | |
53 | const int wxID_CHOICEBOOKCHOICE = wxNewId(); | |
54 | ||
55 | BEGIN_EVENT_TABLE(wxChoicebook, wxBookCtrlBase) | |
56 | EVT_CHOICE(wxID_CHOICEBOOKCHOICE, 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_CHOICEBOOKCHOICE, | |
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, wxGROW|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_bookctrl->GetBestFittingSize(); | |
122 | sizeChoice = m_controlSizer->CalcMin(); | |
123 | ||
124 | wxSize size; | |
125 | if ( IsVertical() ) | |
126 | { | |
127 | size.x = sizeClient.x; | |
128 | size.y = sizeChoice.y; | |
129 | } | |
130 | else // left/right aligned | |
131 | { | |
132 | size.x = sizeChoice.x; | |
133 | size.y = sizeClient.y; | |
134 | } | |
135 | ||
136 | return size; | |
137 | } | |
138 | ||
139 | wxSize wxChoicebook::CalcSizeFromPage(const wxSize& sizePage) const | |
140 | { | |
141 | // we need to add the size of the choice control and the border between | |
142 | const wxSize sizeChoice = GetControllerSize(); | |
143 | ||
144 | wxSize size = sizePage; | |
145 | if ( IsVertical() ) | |
146 | { | |
147 | size.y += sizeChoice.y + GetInternalBorder(); | |
148 | } | |
149 | else // left/right aligned | |
150 | { | |
151 | size.x += sizeChoice.x + GetInternalBorder(); | |
152 | } | |
153 | ||
154 | return size; | |
155 | } | |
156 | ||
157 | ||
158 | // ---------------------------------------------------------------------------- | |
159 | // accessing the pages | |
160 | // ---------------------------------------------------------------------------- | |
161 | ||
162 | bool wxChoicebook::SetPageText(size_t n, const wxString& strText) | |
163 | { | |
164 | GetChoiceCtrl()->SetString(n, strText); | |
165 | ||
166 | return true; | |
167 | } | |
168 | ||
169 | wxString wxChoicebook::GetPageText(size_t n) const | |
170 | { | |
171 | return GetChoiceCtrl()->GetString(n); | |
172 | } | |
173 | ||
174 | int wxChoicebook::GetPageImage(size_t WXUNUSED(n)) const | |
175 | { | |
176 | wxFAIL_MSG( _T("wxChoicebook::GetPageImage() not implemented") ); | |
177 | ||
178 | return wxNOT_FOUND; | |
179 | } | |
180 | ||
181 | bool wxChoicebook::SetPageImage(size_t WXUNUSED(n), int WXUNUSED(imageId)) | |
182 | { | |
183 | wxFAIL_MSG( _T("wxChoicebook::SetPageImage() not implemented") ); | |
184 | ||
185 | return false; | |
186 | } | |
187 | ||
188 | // ---------------------------------------------------------------------------- | |
189 | // image list stuff | |
190 | // ---------------------------------------------------------------------------- | |
191 | ||
192 | void wxChoicebook::SetImageList(wxImageList *imageList) | |
193 | { | |
194 | // TODO: can be implemented in form of static bitmap near choice control | |
195 | ||
196 | wxBookCtrlBase::SetImageList(imageList); | |
197 | } | |
198 | ||
199 | // ---------------------------------------------------------------------------- | |
200 | // selection | |
201 | // ---------------------------------------------------------------------------- | |
202 | ||
203 | int wxChoicebook::GetSelection() const | |
204 | { | |
205 | return m_selection; | |
206 | } | |
207 | ||
208 | int wxChoicebook::SetSelection(size_t n) | |
209 | { | |
210 | wxCHECK_MSG( IS_VALID_PAGE(n), wxNOT_FOUND, | |
211 | wxT("invalid page index in wxChoicebook::SetSelection()") ); | |
212 | ||
213 | const int oldSel = m_selection; | |
214 | ||
215 | if ( int(n) != m_selection ) | |
216 | { | |
217 | wxChoicebookEvent event(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING, m_windowId); | |
218 | event.SetSelection(n); | |
219 | event.SetOldSelection(m_selection); | |
220 | event.SetEventObject(this); | |
221 | if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() ) | |
222 | { | |
223 | if ( m_selection != wxNOT_FOUND ) | |
224 | m_pages[m_selection]->Hide(); | |
225 | ||
226 | wxWindow *page = m_pages[n]; | |
227 | page->SetSize(GetPageRect()); | |
228 | page->Show(); | |
229 | ||
230 | // change m_selection now to ignore the selection change event | |
231 | m_selection = n; | |
232 | GetChoiceCtrl()->Select(n); | |
233 | ||
234 | // program allows the page change | |
235 | event.SetEventType(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED); | |
236 | (void)GetEventHandler()->ProcessEvent(event); | |
237 | } | |
238 | } | |
239 | ||
240 | return oldSel; | |
241 | } | |
242 | ||
243 | // ---------------------------------------------------------------------------- | |
244 | // adding/removing the pages | |
245 | // ---------------------------------------------------------------------------- | |
246 | ||
247 | bool | |
248 | wxChoicebook::InsertPage(size_t n, | |
249 | wxWindow *page, | |
250 | const wxString& text, | |
251 | bool bSelect, | |
252 | int imageId) | |
253 | { | |
254 | if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) ) | |
255 | return false; | |
256 | ||
257 | GetChoiceCtrl()->Insert(text, n); | |
258 | ||
259 | // if the inserted page is before the selected one, we must update the | |
260 | // index of the selected page | |
261 | if ( int(n) <= m_selection ) | |
262 | { | |
263 | // one extra page added | |
264 | m_selection++; | |
265 | GetChoiceCtrl()->Select(m_selection); | |
266 | } | |
267 | ||
268 | // some page should be selected: either this one or the first one if there | |
269 | // is still no selection | |
270 | int selNew = wxNOT_FOUND; | |
271 | if ( bSelect ) | |
272 | selNew = n; | |
273 | else if ( m_selection == wxNOT_FOUND ) | |
274 | selNew = 0; | |
275 | ||
276 | if ( selNew != m_selection ) | |
277 | page->Hide(); | |
278 | ||
279 | if ( selNew != wxNOT_FOUND ) | |
280 | SetSelection(selNew); | |
281 | ||
282 | InvalidateBestSize(); | |
283 | return true; | |
284 | } | |
285 | ||
286 | wxWindow *wxChoicebook::DoRemovePage(size_t page) | |
287 | { | |
288 | const size_t page_count = GetPageCount(); | |
289 | wxWindow *win = wxBookCtrlBase::DoRemovePage(page); | |
290 | ||
291 | if ( win ) | |
292 | { | |
293 | GetChoiceCtrl()->Delete(page); | |
294 | ||
295 | if (m_selection >= (int)page) | |
296 | { | |
297 | // force new sel valid if possible | |
298 | int sel = m_selection - 1; | |
299 | if (page_count == 1) | |
300 | sel = wxNOT_FOUND; | |
301 | else if ((page_count == 2) || (sel == -1)) | |
302 | sel = 0; | |
303 | ||
304 | // force sel invalid if deleting current page - don't try to hide it | |
305 | m_selection = (m_selection == (int)page) ? wxNOT_FOUND : m_selection - 1; | |
306 | ||
307 | if ((sel != wxNOT_FOUND) && (sel != m_selection)) | |
308 | SetSelection(sel); | |
309 | } | |
310 | } | |
311 | ||
312 | return win; | |
313 | } | |
314 | ||
315 | ||
316 | bool wxChoicebook::DeleteAllPages() | |
317 | { | |
318 | GetChoiceCtrl()->Clear(); | |
319 | return wxBookCtrlBase::DeleteAllPages(); | |
320 | } | |
321 | ||
322 | // ---------------------------------------------------------------------------- | |
323 | // wxChoicebook events | |
324 | // ---------------------------------------------------------------------------- | |
325 | ||
326 | void wxChoicebook::OnChoiceSelected(wxCommandEvent& eventChoice) | |
327 | { | |
328 | const int selNew = eventChoice.GetSelection(); | |
329 | ||
330 | if ( selNew == m_selection ) | |
331 | { | |
332 | // this event can only come from our own Select(m_selection) below | |
333 | // which we call when the page change is vetoed, so we should simply | |
334 | // ignore it | |
335 | return; | |
336 | } | |
337 | ||
338 | SetSelection(selNew); | |
339 | ||
340 | // change wasn't allowed, return to previous state | |
341 | if (m_selection != selNew) | |
342 | GetChoiceCtrl()->Select(m_selection); | |
343 | } | |
344 | ||
345 | #endif // wxUSE_CHOICEBOOK |