1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/choicbkg.cpp
3 // Purpose: generic implementation of wxChoicebook
4 // Author: Vadim Zeitlin
5 // Modified by: Wlodzimierz ABX Skiba from generic/listbkg.cpp
8 // Copyright: (c) Vadim Zeitlin, Wlodzimierz Skiba
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/choicebk.h"
32 #include "wx/settings.h"
33 #include "wx/choice.h"
37 #include "wx/imaglist.h"
39 // ----------------------------------------------------------------------------
40 // various wxWidgets macros
41 // ----------------------------------------------------------------------------
43 // check that the page index is valid
44 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 IMPLEMENT_DYNAMIC_CLASS(wxChoicebook
, wxBookCtrlBase
)
52 wxDEFINE_EVENT( wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING
, wxBookCtrlEvent
);
53 wxDEFINE_EVENT( wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED
, wxBookCtrlEvent
);
55 BEGIN_EVENT_TABLE(wxChoicebook
, wxBookCtrlBase
)
56 EVT_CHOICE(wxID_ANY
, wxChoicebook::OnChoiceSelected
)
59 // ============================================================================
60 // wxChoicebook implementation
61 // ============================================================================
63 // ----------------------------------------------------------------------------
64 // wxChoicebook creation
65 // ----------------------------------------------------------------------------
67 void wxChoicebook::Init()
69 m_selection
= wxNOT_FOUND
;
73 wxChoicebook::Create(wxWindow
*parent
,
80 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
85 // no border for this control, it doesn't look nice together with
87 style
&= ~wxBORDER_MASK
;
88 style
|= wxBORDER_NONE
;
90 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
91 wxDefaultValidator
, name
) )
94 m_bookctrl
= new wxChoice
102 wxSizer
* mainSizer
= new wxBoxSizer(IsVertical() ? wxVERTICAL
: wxHORIZONTAL
);
104 if (style
& wxBK_RIGHT
|| style
& wxBK_BOTTOM
)
105 mainSizer
->Add(0, 0, 1, wxEXPAND
, 0);
107 m_controlSizer
= new wxBoxSizer(IsVertical() ? wxHORIZONTAL
: wxVERTICAL
);
108 m_controlSizer
->Add(m_bookctrl
, 1, (IsVertical() ? wxALIGN_CENTRE_VERTICAL
: wxALIGN_CENTRE
) |wxGROW
, 0);
109 mainSizer
->Add(m_controlSizer
, 0, (IsVertical() ? (int) wxGROW
: (int) wxALIGN_CENTRE_VERTICAL
)|wxALL
, m_controlMargin
);
114 // ----------------------------------------------------------------------------
115 // wxChoicebook geometry management
116 // ----------------------------------------------------------------------------
118 wxSize
wxChoicebook::GetControllerSize() const
120 const wxSize sizeClient
= GetClientSize(),
121 sizeChoice
= m_controlSizer
->CalcMin();
126 size
.x
= sizeClient
.x
;
127 size
.y
= sizeChoice
.y
;
129 else // left/right aligned
131 size
.x
= sizeChoice
.x
;
132 size
.y
= sizeClient
.y
;
138 wxSize
wxChoicebook::CalcSizeFromPage(const wxSize
& sizePage
) const
140 // we need to add the size of the choice control and the border between
141 const wxSize sizeChoice
= GetControllerSize();
143 wxSize size
= sizePage
;
146 if ( sizeChoice
.x
> sizePage
.x
)
147 size
.x
= sizeChoice
.x
;
148 size
.y
+= sizeChoice
.y
+ GetInternalBorder();
150 else // left/right aligned
152 size
.x
+= sizeChoice
.x
+ GetInternalBorder();
153 if ( sizeChoice
.y
> sizePage
.y
)
154 size
.y
= sizeChoice
.y
;
161 // ----------------------------------------------------------------------------
162 // accessing the pages
163 // ----------------------------------------------------------------------------
165 bool wxChoicebook::SetPageText(size_t n
, const wxString
& strText
)
167 GetChoiceCtrl()->SetString(n
, strText
);
172 wxString
wxChoicebook::GetPageText(size_t n
) const
174 return GetChoiceCtrl()->GetString(n
);
177 int wxChoicebook::GetPageImage(size_t WXUNUSED(n
)) const
179 wxFAIL_MSG( _T("wxChoicebook::GetPageImage() not implemented") );
184 bool wxChoicebook::SetPageImage(size_t WXUNUSED(n
), int WXUNUSED(imageId
))
186 wxFAIL_MSG( _T("wxChoicebook::SetPageImage() not implemented") );
191 // ----------------------------------------------------------------------------
192 // miscellaneous other stuff
193 // ----------------------------------------------------------------------------
195 void wxChoicebook::DoSetWindowVariant(wxWindowVariant variant
)
197 wxCHECK_RET( m_bookctrl
, "can't be called before control creation" );
199 m_bookctrl
->SetWindowVariant(variant
);
202 void wxChoicebook::SetImageList(wxImageList
*imageList
)
204 // TODO: can be implemented in form of static bitmap near choice control
206 wxBookCtrlBase::SetImageList(imageList
);
209 // ----------------------------------------------------------------------------
211 // ----------------------------------------------------------------------------
213 int wxChoicebook::GetSelection() const
218 wxBookCtrlEvent
* wxChoicebook::CreatePageChangingEvent() const
220 return new wxBookCtrlEvent(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING
, m_windowId
);
223 void wxChoicebook::MakeChangedEvent(wxBookCtrlEvent
&event
)
225 event
.SetEventType(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED
);
228 // ----------------------------------------------------------------------------
229 // adding/removing the pages
230 // ----------------------------------------------------------------------------
233 wxChoicebook::InsertPage(size_t n
,
235 const wxString
& text
,
239 if ( !wxBookCtrlBase::InsertPage(n
, page
, text
, bSelect
, imageId
) )
242 GetChoiceCtrl()->Insert(text
, n
);
244 // if the inserted page is before the selected one, we must update the
245 // index of the selected page
246 if ( int(n
) <= m_selection
)
248 // one extra page added
250 GetChoiceCtrl()->Select(m_selection
);
253 // some page should be selected: either this one or the first one if there
254 // is still no selection
255 int selNew
= wxNOT_FOUND
;
258 else if ( m_selection
== wxNOT_FOUND
)
261 if ( selNew
!= m_selection
)
264 if ( selNew
!= wxNOT_FOUND
)
265 SetSelection(selNew
);
270 wxWindow
*wxChoicebook::DoRemovePage(size_t page
)
272 wxWindow
*win
= wxBookCtrlBase::DoRemovePage(page
);
276 GetChoiceCtrl()->Delete(page
);
278 if ( m_selection
>= (int)page
)
280 // ensure that the selection is valid
282 if ( GetPageCount() == 0 )
285 sel
= m_selection
? m_selection
- 1 : 0;
287 // if deleting current page we shouldn't try to hide it
288 m_selection
= m_selection
== (int)page
? wxNOT_FOUND
291 if ( sel
!= wxNOT_FOUND
&& sel
!= m_selection
)
300 bool wxChoicebook::DeleteAllPages()
302 m_selection
= wxNOT_FOUND
;
303 GetChoiceCtrl()->Clear();
304 return wxBookCtrlBase::DeleteAllPages();
307 // ----------------------------------------------------------------------------
308 // wxChoicebook events
309 // ----------------------------------------------------------------------------
311 void wxChoicebook::OnChoiceSelected(wxCommandEvent
& eventChoice
)
313 if ( eventChoice
.GetEventObject() != m_bookctrl
)
319 const int selNew
= eventChoice
.GetSelection();
321 if ( selNew
== m_selection
)
323 // this event can only come from our own Select(m_selection) below
324 // which we call when the page change is vetoed, so we should simply
329 SetSelection(selNew
);
331 // change wasn't allowed, return to previous state
332 if (m_selection
!= selNew
)
333 GetChoiceCtrl()->Select(m_selection
);
336 #endif // wxUSE_CHOICEBOOK