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
,
99 wxSizer
* mainSizer
= new wxBoxSizer(IsVertical() ? wxVERTICAL
: wxHORIZONTAL
);
101 if (style
& wxCHB_RIGHT
|| style
& wxCHB_BOTTOM
)
102 mainSizer
->Add(0, 0, 1, wxEXPAND
, 0);
104 m_controlSizer
= new wxBoxSizer(IsVertical() ? wxHORIZONTAL
: wxVERTICAL
);
105 m_controlSizer
->Add(m_bookctrl
, 1, (IsVertical() ? wxALIGN_CENTRE_VERTICAL
: wxALIGN_CENTRE
) |wxGROW
, 0);
106 mainSizer
->Add(m_controlSizer
, 0, wxGROW
|wxALL
, m_controlMargin
);
111 // ----------------------------------------------------------------------------
112 // wxChoicebook geometry management
113 // ----------------------------------------------------------------------------
115 wxSize wxChoicebook
::GetControllerSize() const
117 const wxSize sizeClient
= GetClientSize(),
118 // sizeChoice = m_bookctrl->GetBestFittingSize();
119 sizeChoice
= m_controlSizer
->CalcMin();
124 size
.x
= sizeClient
.x
;
125 size
.y
= sizeChoice
.y
;
127 else // left/right aligned
129 size
.x
= sizeChoice
.x
;
130 size
.y
= sizeClient
.y
;
136 wxSize wxChoicebook
::CalcSizeFromPage(const wxSize
& sizePage
) const
138 // we need to add the size of the choice control and the border between
139 const wxSize sizeChoice
= GetControllerSize();
141 wxSize size
= sizePage
;
144 size
.y
+= sizeChoice
.y
+ GetInternalBorder();
146 else // left/right aligned
148 size
.x
+= sizeChoice
.x
+ GetInternalBorder();
155 // ----------------------------------------------------------------------------
156 // accessing the pages
157 // ----------------------------------------------------------------------------
159 bool wxChoicebook
::SetPageText(size_t n
, const wxString
& strText
)
161 GetChoiceCtrl()->SetString(n
, strText
);
166 wxString wxChoicebook
::GetPageText(size_t n
) const
168 return GetChoiceCtrl()->GetString(n
);
171 int wxChoicebook
::GetPageImage(size_t WXUNUSED(n
)) const
173 wxFAIL_MSG( _T("wxChoicebook::GetPageImage() not implemented") );
178 bool wxChoicebook
::SetPageImage(size_t WXUNUSED(n
), int WXUNUSED(imageId
))
180 wxFAIL_MSG( _T("wxChoicebook::SetPageImage() not implemented") );
185 // ----------------------------------------------------------------------------
187 // ----------------------------------------------------------------------------
189 void wxChoicebook
::SetImageList(wxImageList
*imageList
)
191 // TODO: can be implemented in form of static bitmap near choice control
193 wxBookCtrlBase
::SetImageList(imageList
);
196 // ----------------------------------------------------------------------------
198 // ----------------------------------------------------------------------------
200 int wxChoicebook
::GetSelection() const
205 int wxChoicebook
::SetSelection(size_t n
)
207 wxCHECK_MSG( IS_VALID_PAGE(n
), wxNOT_FOUND
,
208 wxT("invalid page index in wxChoicebook::SetSelection()") );
210 const int oldSel
= m_selection
;
212 if ( int(n
) != m_selection
)
214 wxChoicebookEvent
event(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING
, m_windowId
);
215 event
.SetSelection(n
);
216 event
.SetOldSelection(m_selection
);
217 event
.SetEventObject(this);
218 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
220 if ( m_selection
!= wxNOT_FOUND
)
221 m_pages
[m_selection
]->Hide();
223 wxWindow
*page
= m_pages
[n
];
224 page
->SetSize(GetPageRect());
227 // change m_selection now to ignore the selection change event
229 GetChoiceCtrl()->Select(n
);
231 // program allows the page change
232 event
.SetEventType(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED
);
233 (void)GetEventHandler()->ProcessEvent(event
);
240 // ----------------------------------------------------------------------------
241 // adding/removing the pages
242 // ----------------------------------------------------------------------------
245 wxChoicebook
::InsertPage(size_t n
,
247 const wxString
& text
,
251 if ( !wxBookCtrlBase
::InsertPage(n
, page
, text
, bSelect
, imageId
) )
254 GetChoiceCtrl()->Insert(text
, n
);
256 // if the inserted page is before the selected one, we must update the
257 // index of the selected page
258 if ( int(n
) <= m_selection
)
260 // one extra page added
262 GetChoiceCtrl()->Select(m_selection
);
265 // some page should be selected: either this one or the first one if there
266 // is still no selection
267 int selNew
= wxNOT_FOUND
;
270 else if ( m_selection
== wxNOT_FOUND
)
273 if ( selNew
!= m_selection
)
276 if ( selNew
!= wxNOT_FOUND
)
277 SetSelection(selNew
);
279 InvalidateBestSize();
283 wxWindow
*wxChoicebook
::DoRemovePage(size_t page
)
285 const size_t page_count
= GetPageCount();
286 wxWindow
*win
= wxBookCtrlBase
::DoRemovePage(page
);
290 GetChoiceCtrl()->Delete(page
);
292 if (m_selection
>= (int)page
)
294 // force new sel valid if possible
295 int sel
= m_selection
- 1;
298 else if ((page_count
== 2) || (sel
== -1))
301 // force sel invalid if deleting current page - don't try to hide it
302 m_selection
= (m_selection
== (int)page
) ? wxNOT_FOUND
: m_selection
- 1;
304 if ((sel
!= wxNOT_FOUND
) && (sel
!= m_selection
))
313 bool wxChoicebook
::DeleteAllPages()
315 GetChoiceCtrl()->Clear();
316 return wxBookCtrlBase
::DeleteAllPages();
319 // ----------------------------------------------------------------------------
320 // wxChoicebook events
321 // ----------------------------------------------------------------------------
323 void wxChoicebook
::OnChoiceSelected(wxCommandEvent
& eventChoice
)
325 const int selNew
= eventChoice
.GetSelection();
327 if ( selNew
== m_selection
)
329 // this event can only come from our own Select(m_selection) below
330 // which we call when the page change is vetoed, so we should simply
335 SetSelection(selNew
);
337 // change wasn't allowed, return to previous state
338 if (m_selection
!= selNew
)
339 GetChoiceCtrl()->Select(m_selection
);
342 #endif // wxUSE_CHOICEBOOK