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 const wxEventType wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING
= wxNewEventType();
54 const wxEventType wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED
= wxNewEventType();
56 BEGIN_EVENT_TABLE(wxChoicebook
, wxBookCtrlBase
)
57 EVT_CHOICE(wxID_ANY
, wxChoicebook::OnChoiceSelected
)
60 // ============================================================================
61 // wxChoicebook implementation
62 // ============================================================================
64 // ----------------------------------------------------------------------------
65 // wxChoicebook creation
66 // ----------------------------------------------------------------------------
68 void wxChoicebook::Init()
70 m_selection
= wxNOT_FOUND
;
74 wxChoicebook::Create(wxWindow
*parent
,
81 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
86 // no border for this control, it doesn't look nice together with
88 style
&= ~wxBORDER_MASK
;
89 style
|= wxBORDER_NONE
;
91 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
92 wxDefaultValidator
, name
) )
95 m_bookctrl
= new wxChoice
103 wxSizer
* mainSizer
= new wxBoxSizer(IsVertical() ? wxVERTICAL
: wxHORIZONTAL
);
105 if (style
& wxBK_RIGHT
|| style
& wxBK_BOTTOM
)
106 mainSizer
->Add(0, 0, 1, wxEXPAND
, 0);
108 m_controlSizer
= new wxBoxSizer(IsVertical() ? wxHORIZONTAL
: wxVERTICAL
);
109 m_controlSizer
->Add(m_bookctrl
, 1, (IsVertical() ? wxALIGN_CENTRE_VERTICAL
: wxALIGN_CENTRE
) |wxGROW
, 0);
110 mainSizer
->Add(m_controlSizer
, 0, (IsVertical() ? (int) wxGROW
: (int) wxALIGN_CENTRE_VERTICAL
)|wxALL
, m_controlMargin
);
115 // ----------------------------------------------------------------------------
116 // wxChoicebook geometry management
117 // ----------------------------------------------------------------------------
119 wxSize
wxChoicebook::GetControllerSize() const
121 const wxSize sizeClient
= GetClientSize(),
122 sizeChoice
= m_controlSizer
->CalcMin();
127 size
.x
= sizeClient
.x
;
128 size
.y
= sizeChoice
.y
;
130 else // left/right aligned
132 size
.x
= sizeChoice
.x
;
133 size
.y
= sizeClient
.y
;
139 wxSize
wxChoicebook::CalcSizeFromPage(const wxSize
& sizePage
) const
141 // we need to add the size of the choice control and the border between
142 const wxSize sizeChoice
= GetControllerSize();
144 wxSize size
= sizePage
;
147 size
.y
+= sizeChoice
.y
+ GetInternalBorder();
149 else // left/right aligned
151 size
.x
+= sizeChoice
.x
+ GetInternalBorder();
158 // ----------------------------------------------------------------------------
159 // accessing the pages
160 // ----------------------------------------------------------------------------
162 bool wxChoicebook::SetPageText(size_t n
, const wxString
& strText
)
164 GetChoiceCtrl()->SetString(n
, strText
);
169 wxString
wxChoicebook::GetPageText(size_t n
) const
171 return GetChoiceCtrl()->GetString(n
);
174 int wxChoicebook::GetPageImage(size_t WXUNUSED(n
)) const
176 wxFAIL_MSG( _T("wxChoicebook::GetPageImage() not implemented") );
181 bool wxChoicebook::SetPageImage(size_t WXUNUSED(n
), int WXUNUSED(imageId
))
183 wxFAIL_MSG( _T("wxChoicebook::SetPageImage() not implemented") );
188 // ----------------------------------------------------------------------------
190 // ----------------------------------------------------------------------------
192 void wxChoicebook::SetImageList(wxImageList
*imageList
)
194 // TODO: can be implemented in form of static bitmap near choice control
196 wxBookCtrlBase::SetImageList(imageList
);
199 // ----------------------------------------------------------------------------
201 // ----------------------------------------------------------------------------
203 int wxChoicebook::GetSelection() const
208 wxBookCtrlBaseEvent
* wxChoicebook::CreatePageChangingEvent() const
210 return new wxChoicebookEvent(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING
, m_windowId
);
213 void wxChoicebook::MakeChangedEvent(wxBookCtrlBaseEvent
&event
)
215 event
.SetEventType(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED
);
218 // ----------------------------------------------------------------------------
219 // adding/removing the pages
220 // ----------------------------------------------------------------------------
223 wxChoicebook::InsertPage(size_t n
,
225 const wxString
& text
,
229 if ( !wxBookCtrlBase::InsertPage(n
, page
, text
, bSelect
, imageId
) )
232 GetChoiceCtrl()->Insert(text
, n
);
234 // if the inserted page is before the selected one, we must update the
235 // index of the selected page
236 if ( int(n
) <= m_selection
)
238 // one extra page added
240 GetChoiceCtrl()->Select(m_selection
);
243 // some page should be selected: either this one or the first one if there
244 // is still no selection
245 int selNew
= wxNOT_FOUND
;
248 else if ( m_selection
== wxNOT_FOUND
)
251 if ( selNew
!= m_selection
)
254 if ( selNew
!= wxNOT_FOUND
)
255 SetSelection(selNew
);
260 wxWindow
*wxChoicebook::DoRemovePage(size_t page
)
262 const size_t page_count
= GetPageCount();
263 wxWindow
*win
= wxBookCtrlBase::DoRemovePage(page
);
267 GetChoiceCtrl()->Delete(page
);
269 if (m_selection
>= (int)page
)
271 // force new sel valid if possible
272 int sel
= m_selection
- 1;
275 else if ((page_count
== 2) || (sel
== -1))
278 // force sel invalid if deleting current page - don't try to hide it
279 m_selection
= (m_selection
== (int)page
) ? wxNOT_FOUND
: m_selection
- 1;
281 if ((sel
!= wxNOT_FOUND
) && (sel
!= m_selection
))
290 bool wxChoicebook::DeleteAllPages()
292 GetChoiceCtrl()->Clear();
293 return wxBookCtrlBase::DeleteAllPages();
296 // ----------------------------------------------------------------------------
297 // wxChoicebook events
298 // ----------------------------------------------------------------------------
300 void wxChoicebook::OnChoiceSelected(wxCommandEvent
& eventChoice
)
302 if ( eventChoice
.GetEventObject() != m_bookctrl
)
308 const int selNew
= eventChoice
.GetSelection();
310 if ( selNew
== m_selection
)
312 // this event can only come from our own Select(m_selection) below
313 // which we call when the page change is vetoed, so we should simply
318 SetSelection(selNew
);
320 // change wasn't allowed, return to previous state
321 if (m_selection
!= selNew
)
322 GetChoiceCtrl()->Select(m_selection
);
325 #endif // wxUSE_CHOICEBOOK