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/choice.h"
30 #include "wx/choicebk.h"
31 #include "wx/imaglist.h"
32 #include "wx/settings.h"
34 // ----------------------------------------------------------------------------
35 // various wxWidgets macros
36 // ----------------------------------------------------------------------------
38 // check that the page index is valid
39 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 IMPLEMENT_DYNAMIC_CLASS(wxChoicebook
, wxBookCtrlBase
)
46 IMPLEMENT_DYNAMIC_CLASS(wxChoicebookEvent
, wxNotifyEvent
)
48 const wxEventType wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING
= wxNewEventType();
49 const wxEventType wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED
= wxNewEventType();
50 const int wxID_CHOICEBOOKCHOICE
= wxNewId();
52 BEGIN_EVENT_TABLE(wxChoicebook
, wxBookCtrlBase
)
53 EVT_CHOICE(wxID_CHOICEBOOKCHOICE
, wxChoicebook::OnChoiceSelected
)
56 // ============================================================================
57 // wxChoicebook implementation
58 // ============================================================================
60 // ----------------------------------------------------------------------------
61 // wxChoicebook creation
62 // ----------------------------------------------------------------------------
64 void wxChoicebook::Init()
66 m_selection
= wxNOT_FOUND
;
70 wxChoicebook::Create(wxWindow
*parent
,
77 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
82 // no border for this control, it doesn't look nice together with
84 style
&= ~wxBORDER_MASK
;
85 style
|= wxBORDER_NONE
;
87 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
88 wxDefaultValidator
, name
) )
91 m_bookctrl
= new wxChoice
94 wxID_CHOICEBOOKCHOICE
,
102 // ----------------------------------------------------------------------------
103 // wxChoicebook geometry management
104 // ----------------------------------------------------------------------------
106 wxSize
wxChoicebook::GetControllerSize() const
108 const wxSize sizeClient
= GetClientSize(),
109 sizeChoice
= m_bookctrl
->GetBestFittingSize();
114 size
.x
= sizeClient
.x
;
115 size
.y
= sizeChoice
.y
;
117 else // left/right aligned
119 size
.x
= sizeChoice
.x
;
120 size
.y
= sizeClient
.y
;
126 wxSize
wxChoicebook::CalcSizeFromPage(const wxSize
& sizePage
) const
128 // we need to add the size of the choice control and the border between
129 const wxSize sizeChoice
= GetControllerSize();
131 wxSize size
= sizePage
;
134 size
.y
+= sizeChoice
.y
+ GetInternalBorder();
136 else // left/right aligned
138 size
.x
+= sizeChoice
.x
+ GetInternalBorder();
145 // ----------------------------------------------------------------------------
146 // accessing the pages
147 // ----------------------------------------------------------------------------
149 bool wxChoicebook::SetPageText(size_t n
, const wxString
& strText
)
151 GetChoiceCtrl()->SetString(n
, strText
);
156 wxString
wxChoicebook::GetPageText(size_t n
) const
158 return GetChoiceCtrl()->GetString(n
);
161 int wxChoicebook::GetPageImage(size_t WXUNUSED(n
)) const
163 wxFAIL_MSG( _T("wxChoicebook::GetPageImage() not implemented") );
168 bool wxChoicebook::SetPageImage(size_t WXUNUSED(n
), int WXUNUSED(imageId
))
170 wxFAIL_MSG( _T("wxChoicebook::SetPageImage() not implemented") );
175 // ----------------------------------------------------------------------------
177 // ----------------------------------------------------------------------------
179 void wxChoicebook::SetImageList(wxImageList
*imageList
)
181 // TODO: can be implemented in form of static bitmap near choice control
183 wxBookCtrlBase::SetImageList(imageList
);
186 // ----------------------------------------------------------------------------
188 // ----------------------------------------------------------------------------
190 int wxChoicebook::GetSelection() const
195 int wxChoicebook::SetSelection(size_t n
)
197 wxCHECK_MSG( IS_VALID_PAGE(n
), wxNOT_FOUND
,
198 wxT("invalid page index in wxChoicebook::SetSelection()") );
200 const int oldSel
= m_selection
;
202 if ( int(n
) != m_selection
)
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() )
210 if ( m_selection
!= wxNOT_FOUND
)
211 m_pages
[m_selection
]->Hide();
213 wxWindow
*page
= m_pages
[n
];
214 page
->SetSize(GetPageRect());
217 // change m_selection now to ignore the selection change event
219 GetChoiceCtrl()->Select(n
);
221 // program allows the page change
222 event
.SetEventType(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED
);
223 (void)GetEventHandler()->ProcessEvent(event
);
230 // ----------------------------------------------------------------------------
231 // adding/removing the pages
232 // ----------------------------------------------------------------------------
235 wxChoicebook::InsertPage(size_t n
,
237 const wxString
& text
,
241 if ( !wxBookCtrlBase::InsertPage(n
, page
, text
, bSelect
, imageId
) )
244 GetChoiceCtrl()->Insert(text
, n
);
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
)
250 // one extra page added
252 GetChoiceCtrl()->Select(m_selection
);
255 // some page should be selected: either this one or the first one if there
256 // is still no selection
257 int selNew
= wxNOT_FOUND
;
260 else if ( m_selection
== wxNOT_FOUND
)
263 if ( selNew
!= m_selection
)
266 if ( selNew
!= wxNOT_FOUND
)
267 SetSelection(selNew
);
269 InvalidateBestSize();
273 wxWindow
*wxChoicebook::DoRemovePage(size_t page
)
275 const size_t page_count
= GetPageCount();
276 wxWindow
*win
= wxBookCtrlBase::DoRemovePage(page
);
280 GetChoiceCtrl()->Delete(page
);
282 if (m_selection
>= (int)page
)
284 // force new sel valid if possible
285 int sel
= m_selection
- 1;
288 else if ((page_count
== 2) || (sel
== -1))
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;
294 if ((sel
!= wxNOT_FOUND
) && (sel
!= m_selection
))
303 bool wxChoicebook::DeleteAllPages()
305 GetChoiceCtrl()->Clear();
306 return wxBookCtrlBase::DeleteAllPages();
309 // ----------------------------------------------------------------------------
310 // wxChoicebook events
311 // ----------------------------------------------------------------------------
313 void wxChoicebook::OnChoiceSelected(wxCommandEvent
& eventChoice
)
315 const int selNew
= eventChoice
.GetSelection();
317 if ( selNew
== m_selection
)
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
325 SetSelection(selNew
);
327 // change wasn't allowed, return to previous state
328 if (m_selection
!= selNew
)
329 GetChoiceCtrl()->Select(m_selection
);
332 #endif // wxUSE_CHOICEBOOK