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"
36 #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();
57 const int wxID_CHOICEBOOKCHOICE
= wxNewId();
59 BEGIN_EVENT_TABLE(wxChoicebook
, wxBookCtrlBase
)
60 EVT_CHOICE(wxID_CHOICEBOOKCHOICE
, wxChoicebook::OnChoiceSelected
)
63 // ============================================================================
64 // wxChoicebook implementation
65 // ============================================================================
67 // ----------------------------------------------------------------------------
68 // wxChoicebook creation
69 // ----------------------------------------------------------------------------
71 void wxChoicebook::Init()
73 m_selection
= wxNOT_FOUND
;
77 wxChoicebook::Create(wxWindow
*parent
,
84 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
89 // no border for this control, it doesn't look nice together with
91 style
&= ~wxBORDER_MASK
;
92 style
|= wxBORDER_NONE
;
94 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
95 wxDefaultValidator
, name
) )
98 m_bookctrl
= new wxChoice
101 wxID_CHOICEBOOKCHOICE
,
106 wxSizer
* mainSizer
= new wxBoxSizer(IsVertical() ? wxVERTICAL
: wxHORIZONTAL
);
108 if (style
& wxBK_RIGHT
|| style
& wxBK_BOTTOM
)
109 mainSizer
->Add(0, 0, 1, wxEXPAND
, 0);
111 m_controlSizer
= new wxBoxSizer(IsVertical() ? wxHORIZONTAL
: wxVERTICAL
);
112 m_controlSizer
->Add(m_bookctrl
, 1, (IsVertical() ? wxALIGN_CENTRE_VERTICAL
: wxALIGN_CENTRE
) |wxGROW
, 0);
113 mainSizer
->Add(m_controlSizer
, 0, wxGROW
|wxALL
, m_controlMargin
);
118 // ----------------------------------------------------------------------------
119 // wxChoicebook geometry management
120 // ----------------------------------------------------------------------------
122 wxSize
wxChoicebook::GetControllerSize() const
124 const wxSize sizeClient
= GetClientSize(),
125 // sizeChoice = m_bookctrl->GetBestFittingSize();
126 sizeChoice
= m_controlSizer
->CalcMin();
131 size
.x
= sizeClient
.x
;
132 size
.y
= sizeChoice
.y
;
134 else // left/right aligned
136 size
.x
= sizeChoice
.x
;
137 size
.y
= sizeClient
.y
;
143 wxSize
wxChoicebook::CalcSizeFromPage(const wxSize
& sizePage
) const
145 // we need to add the size of the choice control and the border between
146 const wxSize sizeChoice
= GetControllerSize();
148 wxSize size
= sizePage
;
151 size
.y
+= sizeChoice
.y
+ GetInternalBorder();
153 else // left/right aligned
155 size
.x
+= sizeChoice
.x
+ GetInternalBorder();
162 // ----------------------------------------------------------------------------
163 // accessing the pages
164 // ----------------------------------------------------------------------------
166 bool wxChoicebook::SetPageText(size_t n
, const wxString
& strText
)
168 GetChoiceCtrl()->SetString(n
, strText
);
173 wxString
wxChoicebook::GetPageText(size_t n
) const
175 return GetChoiceCtrl()->GetString(n
);
178 int wxChoicebook::GetPageImage(size_t WXUNUSED(n
)) const
180 wxFAIL_MSG( _T("wxChoicebook::GetPageImage() not implemented") );
185 bool wxChoicebook::SetPageImage(size_t WXUNUSED(n
), int WXUNUSED(imageId
))
187 wxFAIL_MSG( _T("wxChoicebook::SetPageImage() not implemented") );
192 // ----------------------------------------------------------------------------
194 // ----------------------------------------------------------------------------
196 void wxChoicebook::SetImageList(wxImageList
*imageList
)
198 // TODO: can be implemented in form of static bitmap near choice control
200 wxBookCtrlBase::SetImageList(imageList
);
203 // ----------------------------------------------------------------------------
205 // ----------------------------------------------------------------------------
207 int wxChoicebook::GetSelection() const
212 int wxChoicebook::SetSelection(size_t n
)
214 wxCHECK_MSG( IS_VALID_PAGE(n
), wxNOT_FOUND
,
215 wxT("invalid page index in wxChoicebook::SetSelection()") );
217 const int oldSel
= m_selection
;
219 if ( int(n
) != m_selection
)
221 wxChoicebookEvent
event(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING
, m_windowId
);
222 event
.SetSelection(n
);
223 event
.SetOldSelection(m_selection
);
224 event
.SetEventObject(this);
225 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
227 if ( m_selection
!= wxNOT_FOUND
)
228 m_pages
[m_selection
]->Hide();
230 wxWindow
*page
= m_pages
[n
];
231 page
->SetSize(GetPageRect());
234 // change m_selection now to ignore the selection change event
236 GetChoiceCtrl()->Select(n
);
238 // program allows the page change
239 event
.SetEventType(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED
);
240 (void)GetEventHandler()->ProcessEvent(event
);
247 // ----------------------------------------------------------------------------
248 // adding/removing the pages
249 // ----------------------------------------------------------------------------
252 wxChoicebook::InsertPage(size_t n
,
254 const wxString
& text
,
258 if ( !wxBookCtrlBase::InsertPage(n
, page
, text
, bSelect
, imageId
) )
261 GetChoiceCtrl()->Insert(text
, n
);
263 // if the inserted page is before the selected one, we must update the
264 // index of the selected page
265 if ( int(n
) <= m_selection
)
267 // one extra page added
269 GetChoiceCtrl()->Select(m_selection
);
272 // some page should be selected: either this one or the first one if there
273 // is still no selection
274 int selNew
= wxNOT_FOUND
;
277 else if ( m_selection
== wxNOT_FOUND
)
280 if ( selNew
!= m_selection
)
283 if ( selNew
!= wxNOT_FOUND
)
284 SetSelection(selNew
);
286 InvalidateBestSize();
290 wxWindow
*wxChoicebook::DoRemovePage(size_t page
)
292 const size_t page_count
= GetPageCount();
293 wxWindow
*win
= wxBookCtrlBase::DoRemovePage(page
);
297 GetChoiceCtrl()->Delete(page
);
299 if (m_selection
>= (int)page
)
301 // force new sel valid if possible
302 int sel
= m_selection
- 1;
305 else if ((page_count
== 2) || (sel
== -1))
308 // force sel invalid if deleting current page - don't try to hide it
309 m_selection
= (m_selection
== (int)page
) ? wxNOT_FOUND
: m_selection
- 1;
311 if ((sel
!= wxNOT_FOUND
) && (sel
!= m_selection
))
320 bool wxChoicebook::DeleteAllPages()
322 GetChoiceCtrl()->Clear();
323 return wxBookCtrlBase::DeleteAllPages();
326 // ----------------------------------------------------------------------------
327 // wxChoicebook events
328 // ----------------------------------------------------------------------------
330 void wxChoicebook::OnChoiceSelected(wxCommandEvent
& eventChoice
)
332 const int selNew
= eventChoice
.GetSelection();
334 if ( selNew
== m_selection
)
336 // this event can only come from our own Select(m_selection) below
337 // which we call when the page change is vetoed, so we should simply
342 SetSelection(selNew
);
344 // change wasn't allowed, return to previous state
345 if (m_selection
!= selNew
)
346 GetChoiceCtrl()->Select(m_selection
);
349 #endif // wxUSE_CHOICEBOOK