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