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