]> git.saurik.com Git - wxWidgets.git/blob - src/generic/choicbkg.cpp
52fd088591cea7cd805d9751d00e7d084297bcde
[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
52 wxDEFINE_EVENT( wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING, wxBookCtrlEvent );
53 wxDEFINE_EVENT( wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED, wxBookCtrlEvent );
54
55 BEGIN_EVENT_TABLE(wxChoicebook, wxBookCtrlBase)
56 EVT_CHOICE(wxID_ANY, wxChoicebook::OnChoiceSelected)
57 END_EVENT_TABLE()
58
59 // ============================================================================
60 // wxChoicebook implementation
61 // ============================================================================
62
63 // ----------------------------------------------------------------------------
64 // wxChoicebook creation
65 // ----------------------------------------------------------------------------
66
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 {
75 if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT )
76 {
77 style |= wxBK_TOP;
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
89 m_bookctrl = new wxChoice
90 (
91 this,
92 wxID_ANY,
93 wxDefaultPosition,
94 wxDefaultSize
95 );
96
97 wxSizer* mainSizer = new wxBoxSizer(IsVertical() ? wxVERTICAL : wxHORIZONTAL);
98
99 if (style & wxBK_RIGHT || style & wxBK_BOTTOM)
100 mainSizer->Add(0, 0, 1, wxEXPAND, 0);
101
102 m_controlSizer = new wxBoxSizer(IsVertical() ? wxHORIZONTAL : wxVERTICAL);
103 m_controlSizer->Add(m_bookctrl, 1, (IsVertical() ? wxALIGN_CENTRE_VERTICAL : wxALIGN_CENTRE) |wxGROW, 0);
104 mainSizer->Add(m_controlSizer, 0, (IsVertical() ? (int) wxGROW : (int) wxALIGN_CENTRE_VERTICAL)|wxALL, m_controlMargin);
105 SetSizer(mainSizer);
106 return true;
107 }
108
109 // ----------------------------------------------------------------------------
110 // accessing the pages
111 // ----------------------------------------------------------------------------
112
113 bool wxChoicebook::SetPageText(size_t n, const wxString& strText)
114 {
115 GetChoiceCtrl()->SetString(n, strText);
116
117 return true;
118 }
119
120 wxString wxChoicebook::GetPageText(size_t n) const
121 {
122 return GetChoiceCtrl()->GetString(n);
123 }
124
125 int wxChoicebook::GetPageImage(size_t WXUNUSED(n)) const
126 {
127 return wxNOT_FOUND;
128 }
129
130 bool wxChoicebook::SetPageImage(size_t WXUNUSED(n), int WXUNUSED(imageId))
131 {
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
137
138 return false;
139 }
140
141 // ----------------------------------------------------------------------------
142 // miscellaneous other stuff
143 // ----------------------------------------------------------------------------
144
145 void wxChoicebook::DoSetWindowVariant(wxWindowVariant variant)
146 {
147 wxBookCtrlBase::DoSetWindowVariant(variant);
148 if (m_bookctrl)
149 m_bookctrl->SetWindowVariant(variant);
150 }
151
152 void wxChoicebook::SetImageList(wxImageList *imageList)
153 {
154 // TODO: can be implemented in form of static bitmap near choice control
155
156 wxBookCtrlBase::SetImageList(imageList);
157 }
158
159 // ----------------------------------------------------------------------------
160 // selection
161 // ----------------------------------------------------------------------------
162
163 wxBookCtrlEvent* wxChoicebook::CreatePageChangingEvent() const
164 {
165 return new wxBookCtrlEvent(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING, m_windowId);
166 }
167
168 void wxChoicebook::MakeChangedEvent(wxBookCtrlEvent &event)
169 {
170 event.SetEventType(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED);
171 }
172
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 {
184 if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) )
185 return false;
186
187 GetChoiceCtrl()->Insert(text, n);
188
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 )
192 {
193 // one extra page added
194 m_selection++;
195 GetChoiceCtrl()->Select(m_selection);
196 }
197
198 if ( !DoSetSelectionAfterInsertion(n, bSelect) )
199 page->Hide();
200
201 return true;
202 }
203
204 wxWindow *wxChoicebook::DoRemovePage(size_t page)
205 {
206 wxWindow *win = wxBookCtrlBase::DoRemovePage(page);
207
208 if ( win )
209 {
210 GetChoiceCtrl()->Delete(page);
211
212 if ( m_selection >= (int)page )
213 {
214 // ensure that the selection is valid
215 int sel;
216 if ( GetPageCount() == 0 )
217 sel = wxNOT_FOUND;
218 else
219 sel = m_selection ? m_selection - 1 : 0;
220
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;
224
225 if ( sel != wxNOT_FOUND && sel != m_selection )
226 SetSelection(sel);
227 }
228 }
229
230 return win;
231 }
232
233
234 bool wxChoicebook::DeleteAllPages()
235 {
236 GetChoiceCtrl()->Clear();
237 return wxBookCtrlBase::DeleteAllPages();
238 }
239
240 // ----------------------------------------------------------------------------
241 // wxChoicebook events
242 // ----------------------------------------------------------------------------
243
244 void wxChoicebook::OnChoiceSelected(wxCommandEvent& eventChoice)
245 {
246 if ( eventChoice.GetEventObject() != m_bookctrl )
247 {
248 eventChoice.Skip();
249 return;
250 }
251
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
262 SetSelection(selNew);
263
264 // change wasn't allowed, return to previous state
265 if (m_selection != selNew)
266 GetChoiceCtrl()->Select(m_selection);
267 }
268
269 #endif // wxUSE_CHOICEBOOK