]> git.saurik.com Git - wxWidgets.git/blame - src/generic/choicbkg.cpp
Fixed wxAtomicInc/Dec() to not use asm/atomic.h header on Linux - it's kernel interna...
[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
WS
51IMPLEMENT_DYNAMIC_CLASS(wxChoicebookEvent, wxNotifyEvent)
52
9cf0f6ae 53#if !WXWIN_COMPATIBILITY_EVENT_TYPES
f5e0b4bc
WS
54const wxEventType wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING = wxNewEventType();
55const wxEventType wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED = wxNewEventType();
9cf0f6ae 56#endif
f5e0b4bc 57
61c083e7 58BEGIN_EVENT_TABLE(wxChoicebook, wxBookCtrlBase)
447325a4 59 EVT_CHOICE(wxID_ANY, wxChoicebook::OnChoiceSelected)
f5e0b4bc
WS
60END_EVENT_TABLE()
61
62// ============================================================================
63// wxChoicebook implementation
64// ============================================================================
65
66// ----------------------------------------------------------------------------
67// wxChoicebook creation
68// ----------------------------------------------------------------------------
69
70void wxChoicebook::Init()
71{
f5e0b4bc
WS
72 m_selection = wxNOT_FOUND;
73}
74
75bool
76wxChoicebook::Create(wxWindow *parent,
77 wxWindowID id,
78 const wxPoint& pos,
79 const wxSize& size,
80 long style,
81 const wxString& name)
82{
2ddb4d13 83 if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT )
f5e0b4bc 84 {
2ddb4d13 85 style |= wxBK_TOP;
f5e0b4bc
WS
86 }
87
88 // no border for this control, it doesn't look nice together with
89 // wxChoice border
90 style &= ~wxBORDER_MASK;
91 style |= wxBORDER_NONE;
92
93 if ( !wxControl::Create(parent, id, pos, size, style,
94 wxDefaultValidator, name) )
95 return false;
96
2ddb4d13 97 m_bookctrl = new wxChoice
f5e0b4bc
WS
98 (
99 this,
447325a4 100 wxID_ANY,
f5e0b4bc
WS
101 wxDefaultPosition,
102 wxDefaultSize
103 );
104
87cf52d8 105 wxSizer* mainSizer = new wxBoxSizer(IsVertical() ? wxVERTICAL : wxHORIZONTAL);
e0d5d9af
WS
106
107 if (style & wxBK_RIGHT || style & wxBK_BOTTOM)
87cf52d8 108 mainSizer->Add(0, 0, 1, wxEXPAND, 0);
e0d5d9af 109
87cf52d8
JS
110 m_controlSizer = new wxBoxSizer(IsVertical() ? wxHORIZONTAL : wxVERTICAL);
111 m_controlSizer->Add(m_bookctrl, 1, (IsVertical() ? wxALIGN_CENTRE_VERTICAL : wxALIGN_CENTRE) |wxGROW, 0);
112 mainSizer->Add(m_controlSizer, 0, wxGROW|wxALL, m_controlMargin);
113 SetSizer(mainSizer);
f5e0b4bc
WS
114 return true;
115}
116
117// ----------------------------------------------------------------------------
118// wxChoicebook geometry management
119// ----------------------------------------------------------------------------
120
2ddb4d13 121wxSize wxChoicebook::GetControllerSize() const
f5e0b4bc
WS
122{
123 const wxSize sizeClient = GetClientSize(),
87cf52d8 124 sizeChoice = m_controlSizer->CalcMin();
f5e0b4bc
WS
125
126 wxSize size;
127 if ( IsVertical() )
128 {
129 size.x = sizeClient.x;
130 size.y = sizeChoice.y;
131 }
132 else // left/right aligned
133 {
134 size.x = sizeChoice.x;
135 size.y = sizeClient.y;
136 }
137
138 return size;
139}
140
f5e0b4bc
WS
141wxSize wxChoicebook::CalcSizeFromPage(const wxSize& sizePage) const
142{
159e6235 143 // we need to add the size of the choice control and the border between
2ddb4d13 144 const wxSize sizeChoice = GetControllerSize();
f5e0b4bc
WS
145
146 wxSize size = sizePage;
147 if ( IsVertical() )
148 {
159e6235 149 size.y += sizeChoice.y + GetInternalBorder();
f5e0b4bc
WS
150 }
151 else // left/right aligned
152 {
159e6235 153 size.x += sizeChoice.x + GetInternalBorder();
f5e0b4bc
WS
154 }
155
156 return size;
157}
158
159
160// ----------------------------------------------------------------------------
161// accessing the pages
162// ----------------------------------------------------------------------------
163
164bool wxChoicebook::SetPageText(size_t n, const wxString& strText)
165{
2ddb4d13 166 GetChoiceCtrl()->SetString(n, strText);
f5e0b4bc
WS
167
168 return true;
169}
170
171wxString wxChoicebook::GetPageText(size_t n) const
172{
2ddb4d13 173 return GetChoiceCtrl()->GetString(n);
f5e0b4bc
WS
174}
175
176int wxChoicebook::GetPageImage(size_t WXUNUSED(n)) const
177{
178 wxFAIL_MSG( _T("wxChoicebook::GetPageImage() not implemented") );
179
9f29226d 180 return wxNOT_FOUND;
f5e0b4bc
WS
181}
182
183bool wxChoicebook::SetPageImage(size_t WXUNUSED(n), int WXUNUSED(imageId))
184{
185 wxFAIL_MSG( _T("wxChoicebook::SetPageImage() not implemented") );
186
187 return false;
188}
189
190// ----------------------------------------------------------------------------
191// image list stuff
192// ----------------------------------------------------------------------------
193
194void wxChoicebook::SetImageList(wxImageList *imageList)
195{
196 // TODO: can be implemented in form of static bitmap near choice control
197
61c083e7 198 wxBookCtrlBase::SetImageList(imageList);
f5e0b4bc
WS
199}
200
201// ----------------------------------------------------------------------------
202// selection
203// ----------------------------------------------------------------------------
204
205int wxChoicebook::GetSelection() const
206{
207 return m_selection;
208}
209
deb325e3 210wxBookCtrlBaseEvent* wxChoicebook::CreatePageChangingEvent() const
f5e0b4bc 211{
deb325e3
VZ
212 return new wxChoicebookEvent(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING, m_windowId);
213}
214
215void wxChoicebook::MakeChangedEvent(wxBookCtrlBaseEvent &event)
216{
217 event.SetEventType(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED);
f5e0b4bc
WS
218}
219
f5e0b4bc
WS
220// ----------------------------------------------------------------------------
221// adding/removing the pages
222// ----------------------------------------------------------------------------
223
224bool
225wxChoicebook::InsertPage(size_t n,
226 wxWindow *page,
227 const wxString& text,
228 bool bSelect,
229 int imageId)
230{
61c083e7 231 if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) )
f5e0b4bc
WS
232 return false;
233
2ddb4d13 234 GetChoiceCtrl()->Insert(text, n);
f5e0b4bc 235
716dc245
WS
236 // if the inserted page is before the selected one, we must update the
237 // index of the selected page
238 if ( int(n) <= m_selection )
f5e0b4bc 239 {
716dc245
WS
240 // one extra page added
241 m_selection++;
2ddb4d13 242 GetChoiceCtrl()->Select(m_selection);
f5e0b4bc 243 }
716dc245
WS
244
245 // some page should be selected: either this one or the first one if there
246 // is still no selection
159e6235 247 int selNew = wxNOT_FOUND;
716dc245
WS
248 if ( bSelect )
249 selNew = n;
159e6235 250 else if ( m_selection == wxNOT_FOUND )
716dc245
WS
251 selNew = 0;
252
253 if ( selNew != m_selection )
f5e0b4bc 254 page->Hide();
716dc245 255
159e6235 256 if ( selNew != wxNOT_FOUND )
716dc245 257 SetSelection(selNew);
f5e0b4bc 258
f5e0b4bc
WS
259 return true;
260}
261
262wxWindow *wxChoicebook::DoRemovePage(size_t page)
263{
4a10ea8b 264 const size_t page_count = GetPageCount();
61c083e7 265 wxWindow *win = wxBookCtrlBase::DoRemovePage(page);
bb08a4a1 266
f5e0b4bc
WS
267 if ( win )
268 {
2ddb4d13 269 GetChoiceCtrl()->Delete(page);
bb08a4a1
WS
270
271 if (m_selection >= (int)page)
272 {
273 // force new sel valid if possible
274 int sel = m_selection - 1;
275 if (page_count == 1)
276 sel = wxNOT_FOUND;
277 else if ((page_count == 2) || (sel == -1))
278 sel = 0;
279
280 // force sel invalid if deleting current page - don't try to hide it
281 m_selection = (m_selection == (int)page) ? wxNOT_FOUND : m_selection - 1;
282
283 if ((sel != wxNOT_FOUND) && (sel != m_selection))
284 SetSelection(sel);
285 }
f5e0b4bc
WS
286 }
287
288 return win;
289}
290
291
292bool wxChoicebook::DeleteAllPages()
293{
2ddb4d13 294 GetChoiceCtrl()->Clear();
61c083e7 295 return wxBookCtrlBase::DeleteAllPages();
f5e0b4bc
WS
296}
297
298// ----------------------------------------------------------------------------
299// wxChoicebook events
300// ----------------------------------------------------------------------------
301
302void wxChoicebook::OnChoiceSelected(wxCommandEvent& eventChoice)
303{
447325a4
VZ
304 if ( eventChoice.GetEventObject() != m_bookctrl )
305 {
306 eventChoice.Skip();
307 return;
308 }
309
f5e0b4bc
WS
310 const int selNew = eventChoice.GetSelection();
311
312 if ( selNew == m_selection )
313 {
314 // this event can only come from our own Select(m_selection) below
315 // which we call when the page change is vetoed, so we should simply
316 // ignore it
317 return;
318 }
319
bb08a4a1 320 SetSelection(selNew);
f5e0b4bc 321
716dc245
WS
322 // change wasn't allowed, return to previous state
323 if (m_selection != selNew)
2ddb4d13 324 GetChoiceCtrl()->Select(m_selection);
f5e0b4bc
WS
325}
326
327#endif // wxUSE_CHOICEBOOK