]>
Commit | Line | Data |
---|---|---|
f5e0b4bc | 1 | /////////////////////////////////////////////////////////////////////////////// |
2ddb4d13 | 2 | // Name: src/generic/choicbkg.cpp |
f5e0b4bc WS |
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 | ||
f5e0b4bc WS |
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 | ||
f5e0b4bc | 29 | #include "wx/choicebk.h" |
9eddec69 WS |
30 | |
31 | #ifndef WX_PRECOMP | |
32 | #include "wx/settings.h" | |
b36e08d0 | 33 | #include "wx/choice.h" |
ed2fbeb8 | 34 | #include "wx/sizer.h" |
9eddec69 WS |
35 | #endif |
36 | ||
f5e0b4bc | 37 | #include "wx/imaglist.h" |
f5e0b4bc | 38 | |
f5e0b4bc WS |
39 | // ---------------------------------------------------------------------------- |
40 | // various wxWidgets macros | |
41 | // ---------------------------------------------------------------------------- | |
42 | ||
1f30c176 WS |
43 | // check that the page index is valid |
44 | #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount()) | |
45 | ||
46 | // ---------------------------------------------------------------------------- | |
47 | // event table | |
48 | // ---------------------------------------------------------------------------- | |
49 | ||
2ddb4d13 | 50 | IMPLEMENT_DYNAMIC_CLASS(wxChoicebook, wxBookCtrlBase) |
f5e0b4bc | 51 | |
9b11752c VZ |
52 | wxDEFINE_EVENT( wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING, wxBookCtrlEvent ); |
53 | wxDEFINE_EVENT( wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED, wxBookCtrlEvent ); | |
f5e0b4bc | 54 | |
61c083e7 | 55 | BEGIN_EVENT_TABLE(wxChoicebook, wxBookCtrlBase) |
447325a4 | 56 | EVT_CHOICE(wxID_ANY, wxChoicebook::OnChoiceSelected) |
f5e0b4bc WS |
57 | END_EVENT_TABLE() |
58 | ||
59 | // ============================================================================ | |
60 | // wxChoicebook implementation | |
61 | // ============================================================================ | |
62 | ||
63 | // ---------------------------------------------------------------------------- | |
64 | // wxChoicebook creation | |
65 | // ---------------------------------------------------------------------------- | |
66 | ||
67 | void wxChoicebook::Init() | |
68 | { | |
f5e0b4bc WS |
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 | { | |
2ddb4d13 | 80 | if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT ) |
f5e0b4bc | 81 | { |
2ddb4d13 | 82 | style |= wxBK_TOP; |
f5e0b4bc WS |
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 | ||
2ddb4d13 | 94 | m_bookctrl = new wxChoice |
f5e0b4bc WS |
95 | ( |
96 | this, | |
447325a4 | 97 | wxID_ANY, |
f5e0b4bc WS |
98 | wxDefaultPosition, |
99 | wxDefaultSize | |
100 | ); | |
101 | ||
87cf52d8 | 102 | wxSizer* mainSizer = new wxBoxSizer(IsVertical() ? wxVERTICAL : wxHORIZONTAL); |
e0d5d9af WS |
103 | |
104 | if (style & wxBK_RIGHT || style & wxBK_BOTTOM) | |
87cf52d8 | 105 | mainSizer->Add(0, 0, 1, wxEXPAND, 0); |
e0d5d9af | 106 | |
87cf52d8 JS |
107 | m_controlSizer = new wxBoxSizer(IsVertical() ? wxHORIZONTAL : wxVERTICAL); |
108 | m_controlSizer->Add(m_bookctrl, 1, (IsVertical() ? wxALIGN_CENTRE_VERTICAL : wxALIGN_CENTRE) |wxGROW, 0); | |
f2b29c64 | 109 | mainSizer->Add(m_controlSizer, 0, (IsVertical() ? (int) wxGROW : (int) wxALIGN_CENTRE_VERTICAL)|wxALL, m_controlMargin); |
87cf52d8 | 110 | SetSizer(mainSizer); |
f5e0b4bc WS |
111 | return true; |
112 | } | |
113 | ||
f5e0b4bc WS |
114 | // ---------------------------------------------------------------------------- |
115 | // accessing the pages | |
116 | // ---------------------------------------------------------------------------- | |
117 | ||
118 | bool wxChoicebook::SetPageText(size_t n, const wxString& strText) | |
119 | { | |
2ddb4d13 | 120 | GetChoiceCtrl()->SetString(n, strText); |
f5e0b4bc WS |
121 | |
122 | return true; | |
123 | } | |
124 | ||
125 | wxString wxChoicebook::GetPageText(size_t n) const | |
126 | { | |
2ddb4d13 | 127 | return GetChoiceCtrl()->GetString(n); |
f5e0b4bc WS |
128 | } |
129 | ||
130 | int wxChoicebook::GetPageImage(size_t WXUNUSED(n)) const | |
131 | { | |
9f29226d | 132 | return wxNOT_FOUND; |
f5e0b4bc WS |
133 | } |
134 | ||
135 | bool wxChoicebook::SetPageImage(size_t WXUNUSED(n), int WXUNUSED(imageId)) | |
136 | { | |
c15cc7fa VZ |
137 | // fail silently, the code may be written to use one of several book |
138 | // classes and call SetPageImage() unconditionally, it's better to just | |
139 | // ignore it (which is the best we can do short of rewriting this class to | |
140 | // use wxBitmapComboBox anyhow) than complain loudly about a rather | |
141 | // harmless problem | |
f5e0b4bc WS |
142 | |
143 | return false; | |
144 | } | |
145 | ||
146 | // ---------------------------------------------------------------------------- | |
64c38b4b | 147 | // miscellaneous other stuff |
f5e0b4bc WS |
148 | // ---------------------------------------------------------------------------- |
149 | ||
64c38b4b VZ |
150 | void wxChoicebook::DoSetWindowVariant(wxWindowVariant variant) |
151 | { | |
b725fa9a KO |
152 | wxBookCtrlBase::DoSetWindowVariant(variant); |
153 | if (m_bookctrl) | |
154 | m_bookctrl->SetWindowVariant(variant); | |
64c38b4b VZ |
155 | } |
156 | ||
f5e0b4bc WS |
157 | void wxChoicebook::SetImageList(wxImageList *imageList) |
158 | { | |
159 | // TODO: can be implemented in form of static bitmap near choice control | |
160 | ||
61c083e7 | 161 | wxBookCtrlBase::SetImageList(imageList); |
f5e0b4bc WS |
162 | } |
163 | ||
164 | // ---------------------------------------------------------------------------- | |
165 | // selection | |
166 | // ---------------------------------------------------------------------------- | |
167 | ||
168 | int wxChoicebook::GetSelection() const | |
169 | { | |
170 | return m_selection; | |
171 | } | |
172 | ||
3e97a905 | 173 | wxBookCtrlEvent* wxChoicebook::CreatePageChangingEvent() const |
f5e0b4bc | 174 | { |
3e97a905 | 175 | return new wxBookCtrlEvent(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING, m_windowId); |
deb325e3 VZ |
176 | } |
177 | ||
3e97a905 | 178 | void wxChoicebook::MakeChangedEvent(wxBookCtrlEvent &event) |
deb325e3 VZ |
179 | { |
180 | event.SetEventType(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED); | |
f5e0b4bc WS |
181 | } |
182 | ||
f5e0b4bc WS |
183 | // ---------------------------------------------------------------------------- |
184 | // adding/removing the pages | |
185 | // ---------------------------------------------------------------------------- | |
186 | ||
187 | bool | |
188 | wxChoicebook::InsertPage(size_t n, | |
189 | wxWindow *page, | |
190 | const wxString& text, | |
191 | bool bSelect, | |
192 | int imageId) | |
193 | { | |
61c083e7 | 194 | if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) ) |
f5e0b4bc WS |
195 | return false; |
196 | ||
2ddb4d13 | 197 | GetChoiceCtrl()->Insert(text, n); |
f5e0b4bc | 198 | |
716dc245 WS |
199 | // if the inserted page is before the selected one, we must update the |
200 | // index of the selected page | |
201 | if ( int(n) <= m_selection ) | |
f5e0b4bc | 202 | { |
716dc245 WS |
203 | // one extra page added |
204 | m_selection++; | |
2ddb4d13 | 205 | GetChoiceCtrl()->Select(m_selection); |
f5e0b4bc | 206 | } |
716dc245 WS |
207 | |
208 | // some page should be selected: either this one or the first one if there | |
209 | // is still no selection | |
159e6235 | 210 | int selNew = wxNOT_FOUND; |
716dc245 WS |
211 | if ( bSelect ) |
212 | selNew = n; | |
159e6235 | 213 | else if ( m_selection == wxNOT_FOUND ) |
716dc245 WS |
214 | selNew = 0; |
215 | ||
216 | if ( selNew != m_selection ) | |
f5e0b4bc | 217 | page->Hide(); |
716dc245 | 218 | |
159e6235 | 219 | if ( selNew != wxNOT_FOUND ) |
716dc245 | 220 | SetSelection(selNew); |
f5e0b4bc | 221 | |
f5e0b4bc WS |
222 | return true; |
223 | } | |
224 | ||
225 | wxWindow *wxChoicebook::DoRemovePage(size_t page) | |
226 | { | |
61c083e7 | 227 | wxWindow *win = wxBookCtrlBase::DoRemovePage(page); |
bb08a4a1 | 228 | |
f5e0b4bc WS |
229 | if ( win ) |
230 | { | |
2ddb4d13 | 231 | GetChoiceCtrl()->Delete(page); |
bb08a4a1 | 232 | |
4922c5f0 | 233 | if ( m_selection >= (int)page ) |
bb08a4a1 | 234 | { |
4922c5f0 VZ |
235 | // ensure that the selection is valid |
236 | int sel; | |
237 | if ( GetPageCount() == 0 ) | |
bb08a4a1 | 238 | sel = wxNOT_FOUND; |
4922c5f0 VZ |
239 | else |
240 | sel = m_selection ? m_selection - 1 : 0; | |
bb08a4a1 | 241 | |
4922c5f0 VZ |
242 | // if deleting current page we shouldn't try to hide it |
243 | m_selection = m_selection == (int)page ? wxNOT_FOUND | |
244 | : m_selection - 1; | |
bb08a4a1 | 245 | |
4922c5f0 | 246 | if ( sel != wxNOT_FOUND && sel != m_selection ) |
bb08a4a1 | 247 | SetSelection(sel); |
4922c5f0 | 248 | } |
f5e0b4bc WS |
249 | } |
250 | ||
251 | return win; | |
252 | } | |
253 | ||
254 | ||
255 | bool wxChoicebook::DeleteAllPages() | |
256 | { | |
4922c5f0 | 257 | m_selection = wxNOT_FOUND; |
2ddb4d13 | 258 | GetChoiceCtrl()->Clear(); |
61c083e7 | 259 | return wxBookCtrlBase::DeleteAllPages(); |
f5e0b4bc WS |
260 | } |
261 | ||
262 | // ---------------------------------------------------------------------------- | |
263 | // wxChoicebook events | |
264 | // ---------------------------------------------------------------------------- | |
265 | ||
266 | void wxChoicebook::OnChoiceSelected(wxCommandEvent& eventChoice) | |
267 | { | |
447325a4 VZ |
268 | if ( eventChoice.GetEventObject() != m_bookctrl ) |
269 | { | |
270 | eventChoice.Skip(); | |
271 | return; | |
272 | } | |
273 | ||
f5e0b4bc WS |
274 | const int selNew = eventChoice.GetSelection(); |
275 | ||
276 | if ( selNew == m_selection ) | |
277 | { | |
278 | // this event can only come from our own Select(m_selection) below | |
279 | // which we call when the page change is vetoed, so we should simply | |
280 | // ignore it | |
281 | return; | |
282 | } | |
283 | ||
bb08a4a1 | 284 | SetSelection(selNew); |
f5e0b4bc | 285 | |
716dc245 WS |
286 | // change wasn't allowed, return to previous state |
287 | if (m_selection != selNew) | |
2ddb4d13 | 288 | GetChoiceCtrl()->Select(m_selection); |
f5e0b4bc WS |
289 | } |
290 | ||
291 | #endif // wxUSE_CHOICEBOOK |