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
)
51 IMPLEMENT_DYNAMIC_CLASS(wxChoicebookEvent
, wxNotifyEvent
)
53 #if !WXWIN_COMPATIBILITY_EVENT_TYPES
54 const wxEventType wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING
= wxNewEventType();
55 const wxEventType wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED
= wxNewEventType();
58 BEGIN_EVENT_TABLE(wxChoicebook
, wxBookCtrlBase
)
59 EVT_CHOICE(wxID_ANY
, wxChoicebook::OnChoiceSelected
)
62 // ============================================================================
63 // wxChoicebook implementation
64 // ============================================================================
66 // ----------------------------------------------------------------------------
67 // wxChoicebook creation
68 // ----------------------------------------------------------------------------
70 void wxChoicebook::Init()
72 m_selection
= wxNOT_FOUND
;
76 wxChoicebook::Create(wxWindow
*parent
,
83 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
88 // no border for this control, it doesn't look nice together with
90 style
&= ~wxBORDER_MASK
;
91 style
|= wxBORDER_NONE
;
93 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
94 wxDefaultValidator
, name
) )
97 m_bookctrl
= new wxChoice
105 wxSizer
* mainSizer
= new wxBoxSizer(IsVertical() ? wxVERTICAL
: wxHORIZONTAL
);
107 if (style
& wxBK_RIGHT
|| style
& wxBK_BOTTOM
)
108 mainSizer
->Add(0, 0, 1, wxEXPAND
, 0);
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
);
117 // ----------------------------------------------------------------------------
118 // wxChoicebook geometry management
119 // ----------------------------------------------------------------------------
121 wxSize
wxChoicebook::GetControllerSize() const
123 const wxSize sizeClient
= GetClientSize(),
124 sizeChoice
= m_controlSizer
->CalcMin();
129 size
.x
= sizeClient
.x
;
130 size
.y
= sizeChoice
.y
;
132 else // left/right aligned
134 size
.x
= sizeChoice
.x
;
135 size
.y
= sizeClient
.y
;
141 wxSize
wxChoicebook::CalcSizeFromPage(const wxSize
& sizePage
) const
143 // we need to add the size of the choice control and the border between
144 const wxSize sizeChoice
= GetControllerSize();
146 wxSize size
= sizePage
;
149 size
.y
+= sizeChoice
.y
+ GetInternalBorder();
151 else // left/right aligned
153 size
.x
+= sizeChoice
.x
+ GetInternalBorder();
160 // ----------------------------------------------------------------------------
161 // accessing the pages
162 // ----------------------------------------------------------------------------
164 bool wxChoicebook::SetPageText(size_t n
, const wxString
& strText
)
166 GetChoiceCtrl()->SetString(n
, strText
);
171 wxString
wxChoicebook::GetPageText(size_t n
) const
173 return GetChoiceCtrl()->GetString(n
);
176 int wxChoicebook::GetPageImage(size_t WXUNUSED(n
)) const
178 wxFAIL_MSG( _T("wxChoicebook::GetPageImage() not implemented") );
183 bool wxChoicebook::SetPageImage(size_t WXUNUSED(n
), int WXUNUSED(imageId
))
185 wxFAIL_MSG( _T("wxChoicebook::SetPageImage() not implemented") );
190 // ----------------------------------------------------------------------------
192 // ----------------------------------------------------------------------------
194 void wxChoicebook::SetImageList(wxImageList
*imageList
)
196 // TODO: can be implemented in form of static bitmap near choice control
198 wxBookCtrlBase::SetImageList(imageList
);
201 // ----------------------------------------------------------------------------
203 // ----------------------------------------------------------------------------
205 int wxChoicebook::GetSelection() const
210 wxBookCtrlBaseEvent
* wxChoicebook::CreatePageChangingEvent() const
212 return new wxChoicebookEvent(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING
, m_windowId
);
215 void wxChoicebook::MakeChangedEvent(wxBookCtrlBaseEvent
&event
)
217 event
.SetEventType(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED
);
220 // ----------------------------------------------------------------------------
221 // adding/removing the pages
222 // ----------------------------------------------------------------------------
225 wxChoicebook::InsertPage(size_t n
,
227 const wxString
& text
,
231 if ( !wxBookCtrlBase::InsertPage(n
, page
, text
, bSelect
, imageId
) )
234 GetChoiceCtrl()->Insert(text
, n
);
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
)
240 // one extra page added
242 GetChoiceCtrl()->Select(m_selection
);
245 // some page should be selected: either this one or the first one if there
246 // is still no selection
247 int selNew
= wxNOT_FOUND
;
250 else if ( m_selection
== wxNOT_FOUND
)
253 if ( selNew
!= m_selection
)
256 if ( selNew
!= wxNOT_FOUND
)
257 SetSelection(selNew
);
262 wxWindow
*wxChoicebook::DoRemovePage(size_t page
)
264 const size_t page_count
= GetPageCount();
265 wxWindow
*win
= wxBookCtrlBase::DoRemovePage(page
);
269 GetChoiceCtrl()->Delete(page
);
271 if (m_selection
>= (int)page
)
273 // force new sel valid if possible
274 int sel
= m_selection
- 1;
277 else if ((page_count
== 2) || (sel
== -1))
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;
283 if ((sel
!= wxNOT_FOUND
) && (sel
!= m_selection
))
292 bool wxChoicebook::DeleteAllPages()
294 GetChoiceCtrl()->Clear();
295 return wxBookCtrlBase::DeleteAllPages();
298 // ----------------------------------------------------------------------------
299 // wxChoicebook events
300 // ----------------------------------------------------------------------------
302 void wxChoicebook::OnChoiceSelected(wxCommandEvent
& eventChoice
)
304 if ( eventChoice
.GetEventObject() != m_bookctrl
)
310 const int selNew
= eventChoice
.GetSelection();
312 if ( selNew
== m_selection
)
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
320 SetSelection(selNew
);
322 // change wasn't allowed, return to previous state
323 if (m_selection
!= selNew
)
324 GetChoiceCtrl()->Select(m_selection
);
327 #endif // wxUSE_CHOICEBOOK