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