]> git.saurik.com Git - wxWidgets.git/blame - src/generic/choicbkg.cpp
Fix order of arguments in wxOSX/Carbon wxListCtrl::ScrollList().
[wxWidgets.git] / src / generic / choicbkg.cpp
CommitLineData
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 50IMPLEMENT_DYNAMIC_CLASS(wxChoicebook, wxBookCtrlBase)
f5e0b4bc 51
9b11752c
VZ
52wxDEFINE_EVENT( wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING, wxBookCtrlEvent );
53wxDEFINE_EVENT( wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED, wxBookCtrlEvent );
f5e0b4bc 54
61c083e7 55BEGIN_EVENT_TABLE(wxChoicebook, wxBookCtrlBase)
447325a4 56 EVT_CHOICE(wxID_ANY, wxChoicebook::OnChoiceSelected)
f5e0b4bc
WS
57END_EVENT_TABLE()
58
59// ============================================================================
60// wxChoicebook implementation
61// ============================================================================
62
63// ----------------------------------------------------------------------------
64// wxChoicebook creation
65// ----------------------------------------------------------------------------
66
f5e0b4bc
WS
67bool
68wxChoicebook::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
113bool wxChoicebook::SetPageText(size_t n, const wxString& strText)
114{
2ddb4d13 115 GetChoiceCtrl()->SetString(n, strText);
f5e0b4bc
WS
116
117 return true;
118}
119
120wxString wxChoicebook::GetPageText(size_t n) const
121{
2ddb4d13 122 return GetChoiceCtrl()->GetString(n);
f5e0b4bc
WS
123}
124
125int wxChoicebook::GetPageImage(size_t WXUNUSED(n)) const
126{
9f29226d 127 return wxNOT_FOUND;
f5e0b4bc
WS
128}
129
130bool 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
145void 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
152void 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 163wxBookCtrlEvent* wxChoicebook::CreatePageChangingEvent() const
f5e0b4bc 164{
3e97a905 165 return new wxBookCtrlEvent(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING, m_windowId);
deb325e3
VZ
166}
167
3e97a905 168void wxChoicebook::MakeChangedEvent(wxBookCtrlEvent &event)
deb325e3
VZ
169{
170 event.SetEventType(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED);
f5e0b4bc
WS
171}
172
f5e0b4bc
WS
173// ----------------------------------------------------------------------------
174// adding/removing the pages
175// ----------------------------------------------------------------------------
176
177bool
178wxChoicebook::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
WS
197
198 // some page should be selected: either this one or the first one if there
199 // is still no selection
159e6235 200 int selNew = wxNOT_FOUND;
716dc245
WS
201 if ( bSelect )
202 selNew = n;
159e6235 203 else if ( m_selection == wxNOT_FOUND )
716dc245
WS
204 selNew = 0;
205
206 if ( selNew != m_selection )
f5e0b4bc 207 page->Hide();
716dc245 208
159e6235 209 if ( selNew != wxNOT_FOUND )
716dc245 210 SetSelection(selNew);
f5e0b4bc 211
f5e0b4bc
WS
212 return true;
213}
214
215wxWindow *wxChoicebook::DoRemovePage(size_t page)
216{
61c083e7 217 wxWindow *win = wxBookCtrlBase::DoRemovePage(page);
bb08a4a1 218
f5e0b4bc
WS
219 if ( win )
220 {
2ddb4d13 221 GetChoiceCtrl()->Delete(page);
bb08a4a1 222
4922c5f0 223 if ( m_selection >= (int)page )
bb08a4a1 224 {
4922c5f0
VZ
225 // ensure that the selection is valid
226 int sel;
227 if ( GetPageCount() == 0 )
bb08a4a1 228 sel = wxNOT_FOUND;
4922c5f0
VZ
229 else
230 sel = m_selection ? m_selection - 1 : 0;
bb08a4a1 231
4922c5f0
VZ
232 // if deleting current page we shouldn't try to hide it
233 m_selection = m_selection == (int)page ? wxNOT_FOUND
234 : m_selection - 1;
bb08a4a1 235
4922c5f0 236 if ( sel != wxNOT_FOUND && sel != m_selection )
bb08a4a1 237 SetSelection(sel);
4922c5f0 238 }
f5e0b4bc
WS
239 }
240
241 return win;
242}
243
244
245bool wxChoicebook::DeleteAllPages()
246{
2ddb4d13 247 GetChoiceCtrl()->Clear();
61c083e7 248 return wxBookCtrlBase::DeleteAllPages();
f5e0b4bc
WS
249}
250
251// ----------------------------------------------------------------------------
252// wxChoicebook events
253// ----------------------------------------------------------------------------
254
255void wxChoicebook::OnChoiceSelected(wxCommandEvent& eventChoice)
256{
447325a4
VZ
257 if ( eventChoice.GetEventObject() != m_bookctrl )
258 {
259 eventChoice.Skip();
260 return;
261 }
262
f5e0b4bc
WS
263 const int selNew = eventChoice.GetSelection();
264
265 if ( selNew == m_selection )
266 {
267 // this event can only come from our own Select(m_selection) below
268 // which we call when the page change is vetoed, so we should simply
269 // ignore it
270 return;
271 }
272
bb08a4a1 273 SetSelection(selNew);
f5e0b4bc 274
716dc245
WS
275 // change wasn't allowed, return to previous state
276 if (m_selection != selNew)
2ddb4d13 277 GetChoiceCtrl()->Select(m_selection);
f5e0b4bc
WS
278}
279
280#endif // wxUSE_CHOICEBOOK