]>
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 | |
ce7fe42e VZ |
52 | wxDEFINE_EVENT( wxEVT_CHOICEBOOK_PAGE_CHANGING, wxBookCtrlEvent ); |
53 | wxDEFINE_EVENT( wxEVT_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 | ||
f5e0b4bc WS |
67 | bool |
68 | wxChoicebook::Create(wxWindow *parent, | |
69 | wxWindowID id, | |
70 | const wxPoint& pos, | |
71 | const wxSize& size, | |
72 | long style, | |
73 | const wxString& name) | |
74 | { | |
2ddb4d13 | 75 | if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT ) |
f5e0b4bc | 76 | { |
2ddb4d13 | 77 | style |= wxBK_TOP; |
f5e0b4bc WS |
78 | } |
79 | ||
80 | // no border for this control, it doesn't look nice together with | |
81 | // wxChoice border | |
82 | style &= ~wxBORDER_MASK; | |
83 | style |= wxBORDER_NONE; | |
84 | ||
85 | if ( !wxControl::Create(parent, id, pos, size, style, | |
86 | wxDefaultValidator, name) ) | |
87 | return false; | |
88 | ||
2ddb4d13 | 89 | m_bookctrl = new wxChoice |
f5e0b4bc WS |
90 | ( |
91 | this, | |
447325a4 | 92 | wxID_ANY, |
f5e0b4bc WS |
93 | wxDefaultPosition, |
94 | wxDefaultSize | |
95 | ); | |
96 | ||
87cf52d8 | 97 | wxSizer* mainSizer = new wxBoxSizer(IsVertical() ? wxVERTICAL : wxHORIZONTAL); |
e0d5d9af WS |
98 | |
99 | if (style & wxBK_RIGHT || style & wxBK_BOTTOM) | |
87cf52d8 | 100 | mainSizer->Add(0, 0, 1, wxEXPAND, 0); |
e0d5d9af | 101 | |
87cf52d8 JS |
102 | m_controlSizer = new wxBoxSizer(IsVertical() ? wxHORIZONTAL : wxVERTICAL); |
103 | m_controlSizer->Add(m_bookctrl, 1, (IsVertical() ? wxALIGN_CENTRE_VERTICAL : wxALIGN_CENTRE) |wxGROW, 0); | |
f2b29c64 | 104 | mainSizer->Add(m_controlSizer, 0, (IsVertical() ? (int) wxGROW : (int) wxALIGN_CENTRE_VERTICAL)|wxALL, m_controlMargin); |
87cf52d8 | 105 | SetSizer(mainSizer); |
f5e0b4bc WS |
106 | return true; |
107 | } | |
108 | ||
f5e0b4bc WS |
109 | // ---------------------------------------------------------------------------- |
110 | // accessing the pages | |
111 | // ---------------------------------------------------------------------------- | |
112 | ||
113 | bool wxChoicebook::SetPageText(size_t n, const wxString& strText) | |
114 | { | |
2ddb4d13 | 115 | GetChoiceCtrl()->SetString(n, strText); |
f5e0b4bc WS |
116 | |
117 | return true; | |
118 | } | |
119 | ||
120 | wxString wxChoicebook::GetPageText(size_t n) const | |
121 | { | |
2ddb4d13 | 122 | return GetChoiceCtrl()->GetString(n); |
f5e0b4bc WS |
123 | } |
124 | ||
125 | int wxChoicebook::GetPageImage(size_t WXUNUSED(n)) const | |
126 | { | |
9f29226d | 127 | return wxNOT_FOUND; |
f5e0b4bc WS |
128 | } |
129 | ||
130 | bool wxChoicebook::SetPageImage(size_t WXUNUSED(n), int WXUNUSED(imageId)) | |
131 | { | |
c15cc7fa VZ |
132 | // fail silently, the code may be written to use one of several book |
133 | // classes and call SetPageImage() unconditionally, it's better to just | |
134 | // ignore it (which is the best we can do short of rewriting this class to | |
135 | // use wxBitmapComboBox anyhow) than complain loudly about a rather | |
136 | // harmless problem | |
f5e0b4bc WS |
137 | |
138 | return false; | |
139 | } | |
140 | ||
141 | // ---------------------------------------------------------------------------- | |
64c38b4b | 142 | // miscellaneous other stuff |
f5e0b4bc WS |
143 | // ---------------------------------------------------------------------------- |
144 | ||
64c38b4b VZ |
145 | void wxChoicebook::DoSetWindowVariant(wxWindowVariant variant) |
146 | { | |
b725fa9a KO |
147 | wxBookCtrlBase::DoSetWindowVariant(variant); |
148 | if (m_bookctrl) | |
149 | m_bookctrl->SetWindowVariant(variant); | |
64c38b4b VZ |
150 | } |
151 | ||
f5e0b4bc WS |
152 | void wxChoicebook::SetImageList(wxImageList *imageList) |
153 | { | |
154 | // TODO: can be implemented in form of static bitmap near choice control | |
155 | ||
61c083e7 | 156 | wxBookCtrlBase::SetImageList(imageList); |
f5e0b4bc WS |
157 | } |
158 | ||
159 | // ---------------------------------------------------------------------------- | |
160 | // selection | |
161 | // ---------------------------------------------------------------------------- | |
162 | ||
3e97a905 | 163 | wxBookCtrlEvent* wxChoicebook::CreatePageChangingEvent() const |
f5e0b4bc | 164 | { |
ce7fe42e | 165 | return new wxBookCtrlEvent(wxEVT_CHOICEBOOK_PAGE_CHANGING, m_windowId); |
deb325e3 VZ |
166 | } |
167 | ||
3e97a905 | 168 | void wxChoicebook::MakeChangedEvent(wxBookCtrlEvent &event) |
deb325e3 | 169 | { |
ce7fe42e | 170 | event.SetEventType(wxEVT_CHOICEBOOK_PAGE_CHANGED); |
f5e0b4bc WS |
171 | } |
172 | ||
f5e0b4bc WS |
173 | // ---------------------------------------------------------------------------- |
174 | // adding/removing the pages | |
175 | // ---------------------------------------------------------------------------- | |
176 | ||
177 | bool | |
178 | wxChoicebook::InsertPage(size_t n, | |
179 | wxWindow *page, | |
180 | const wxString& text, | |
181 | bool bSelect, | |
182 | int imageId) | |
183 | { | |
61c083e7 | 184 | if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) ) |
f5e0b4bc WS |
185 | return false; |
186 | ||
2ddb4d13 | 187 | GetChoiceCtrl()->Insert(text, n); |
f5e0b4bc | 188 | |
716dc245 WS |
189 | // if the inserted page is before the selected one, we must update the |
190 | // index of the selected page | |
191 | if ( int(n) <= m_selection ) | |
f5e0b4bc | 192 | { |
716dc245 WS |
193 | // one extra page added |
194 | m_selection++; | |
2ddb4d13 | 195 | GetChoiceCtrl()->Select(m_selection); |
f5e0b4bc | 196 | } |
716dc245 | 197 | |
60d5c563 | 198 | if ( !DoSetSelectionAfterInsertion(n, bSelect) ) |
f5e0b4bc | 199 | page->Hide(); |
716dc245 | 200 | |
f5e0b4bc WS |
201 | return true; |
202 | } | |
203 | ||
204 | wxWindow *wxChoicebook::DoRemovePage(size_t page) | |
205 | { | |
61c083e7 | 206 | wxWindow *win = wxBookCtrlBase::DoRemovePage(page); |
bb08a4a1 | 207 | |
f5e0b4bc WS |
208 | if ( win ) |
209 | { | |
2ddb4d13 | 210 | GetChoiceCtrl()->Delete(page); |
bb08a4a1 | 211 | |
4922c5f0 | 212 | if ( m_selection >= (int)page ) |
bb08a4a1 | 213 | { |
4922c5f0 VZ |
214 | // ensure that the selection is valid |
215 | int sel; | |
216 | if ( GetPageCount() == 0 ) | |
bb08a4a1 | 217 | sel = wxNOT_FOUND; |
4922c5f0 VZ |
218 | else |
219 | sel = m_selection ? m_selection - 1 : 0; | |
bb08a4a1 | 220 | |
4922c5f0 VZ |
221 | // if deleting current page we shouldn't try to hide it |
222 | m_selection = m_selection == (int)page ? wxNOT_FOUND | |
223 | : m_selection - 1; | |
bb08a4a1 | 224 | |
4922c5f0 | 225 | if ( sel != wxNOT_FOUND && sel != m_selection ) |
bb08a4a1 | 226 | SetSelection(sel); |
4922c5f0 | 227 | } |
f5e0b4bc WS |
228 | } |
229 | ||
230 | return win; | |
231 | } | |
232 | ||
233 | ||
234 | bool wxChoicebook::DeleteAllPages() | |
235 | { | |
2ddb4d13 | 236 | GetChoiceCtrl()->Clear(); |
61c083e7 | 237 | return wxBookCtrlBase::DeleteAllPages(); |
f5e0b4bc WS |
238 | } |
239 | ||
240 | // ---------------------------------------------------------------------------- | |
241 | // wxChoicebook events | |
242 | // ---------------------------------------------------------------------------- | |
243 | ||
244 | void wxChoicebook::OnChoiceSelected(wxCommandEvent& eventChoice) | |
245 | { | |
447325a4 VZ |
246 | if ( eventChoice.GetEventObject() != m_bookctrl ) |
247 | { | |
248 | eventChoice.Skip(); | |
249 | return; | |
250 | } | |
251 | ||
f5e0b4bc WS |
252 | const int selNew = eventChoice.GetSelection(); |
253 | ||
254 | if ( selNew == m_selection ) | |
255 | { | |
256 | // this event can only come from our own Select(m_selection) below | |
257 | // which we call when the page change is vetoed, so we should simply | |
258 | // ignore it | |
259 | return; | |
260 | } | |
261 | ||
bb08a4a1 | 262 | SetSelection(selNew); |
f5e0b4bc | 263 | |
716dc245 WS |
264 | // change wasn't allowed, return to previous state |
265 | if (m_selection != selNew) | |
2ddb4d13 | 266 | GetChoiceCtrl()->Select(m_selection); |
f5e0b4bc WS |
267 | } |
268 | ||
269 | #endif // wxUSE_CHOICEBOOK |