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"
35 // ----------------------------------------------------------------------------
36 // various wxWidgets macros
37 // ----------------------------------------------------------------------------
39 // check that the page index is valid
40 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 IMPLEMENT_DYNAMIC_CLASS(wxChoicebook
, wxBookCtrlBase
)
47 IMPLEMENT_DYNAMIC_CLASS(wxChoicebookEvent
, wxNotifyEvent
)
49 #if !WXWIN_COMPATIBILITY_EVENT_TYPES
50 const wxEventType wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING
= wxNewEventType();
51 const wxEventType wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED
= wxNewEventType();
53 const int wxID_CHOICEBOOKCHOICE
= wxNewId();
55 BEGIN_EVENT_TABLE(wxChoicebook
, wxBookCtrlBase
)
56 EVT_CHOICE(wxID_CHOICEBOOKCHOICE
, wxChoicebook::OnChoiceSelected
)
59 // ============================================================================
60 // wxChoicebook implementation
61 // ============================================================================
63 // ----------------------------------------------------------------------------
64 // wxChoicebook creation
65 // ----------------------------------------------------------------------------
67 void wxChoicebook::Init()
69 m_selection
= wxNOT_FOUND
;
73 wxChoicebook::Create(wxWindow
*parent
,
80 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
85 // no border for this control, it doesn't look nice together with
87 style
&= ~wxBORDER_MASK
;
88 style
|= wxBORDER_NONE
;
90 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
91 wxDefaultValidator
, name
) )
94 m_bookctrl
= new wxChoice
97 wxID_CHOICEBOOKCHOICE
,
102 wxSizer
* mainSizer
= new wxBoxSizer(IsVertical() ? wxVERTICAL
: wxHORIZONTAL
);
104 if (style
& wxBK_RIGHT
|| style
& wxBK_BOTTOM
)
105 mainSizer
->Add(0, 0, 1, wxEXPAND
, 0);
107 m_controlSizer
= new wxBoxSizer(IsVertical() ? wxHORIZONTAL
: wxVERTICAL
);
108 m_controlSizer
->Add(m_bookctrl
, 1, (IsVertical() ? wxALIGN_CENTRE_VERTICAL
: wxALIGN_CENTRE
) |wxGROW
, 0);
109 mainSizer
->Add(m_controlSizer
, 0, wxGROW
|wxALL
, m_controlMargin
);
114 // ----------------------------------------------------------------------------
115 // wxChoicebook geometry management
116 // ----------------------------------------------------------------------------
118 wxSize
wxChoicebook::GetControllerSize() const
120 const wxSize sizeClient
= GetClientSize(),
121 // sizeChoice = m_bookctrl->GetBestFittingSize();
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 int wxChoicebook::SetSelection(size_t n
)
210 wxCHECK_MSG( IS_VALID_PAGE(n
), wxNOT_FOUND
,
211 wxT("invalid page index in wxChoicebook::SetSelection()") );
213 const int oldSel
= m_selection
;
215 if ( int(n
) != m_selection
)
217 wxChoicebookEvent
event(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING
, m_windowId
);
218 event
.SetSelection(n
);
219 event
.SetOldSelection(m_selection
);
220 event
.SetEventObject(this);
221 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
223 if ( m_selection
!= wxNOT_FOUND
)
224 m_pages
[m_selection
]->Hide();
226 wxWindow
*page
= m_pages
[n
];
227 page
->SetSize(GetPageRect());
230 // change m_selection now to ignore the selection change event
232 GetChoiceCtrl()->Select(n
);
234 // program allows the page change
235 event
.SetEventType(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED
);
236 (void)GetEventHandler()->ProcessEvent(event
);
243 // ----------------------------------------------------------------------------
244 // adding/removing the pages
245 // ----------------------------------------------------------------------------
248 wxChoicebook::InsertPage(size_t n
,
250 const wxString
& text
,
254 if ( !wxBookCtrlBase::InsertPage(n
, page
, text
, bSelect
, imageId
) )
257 GetChoiceCtrl()->Insert(text
, n
);
259 // if the inserted page is before the selected one, we must update the
260 // index of the selected page
261 if ( int(n
) <= m_selection
)
263 // one extra page added
265 GetChoiceCtrl()->Select(m_selection
);
268 // some page should be selected: either this one or the first one if there
269 // is still no selection
270 int selNew
= wxNOT_FOUND
;
273 else if ( m_selection
== wxNOT_FOUND
)
276 if ( selNew
!= m_selection
)
279 if ( selNew
!= wxNOT_FOUND
)
280 SetSelection(selNew
);
282 InvalidateBestSize();
286 wxWindow
*wxChoicebook::DoRemovePage(size_t page
)
288 const size_t page_count
= GetPageCount();
289 wxWindow
*win
= wxBookCtrlBase::DoRemovePage(page
);
293 GetChoiceCtrl()->Delete(page
);
295 if (m_selection
>= (int)page
)
297 // force new sel valid if possible
298 int sel
= m_selection
- 1;
301 else if ((page_count
== 2) || (sel
== -1))
304 // force sel invalid if deleting current page - don't try to hide it
305 m_selection
= (m_selection
== (int)page
) ? wxNOT_FOUND
: m_selection
- 1;
307 if ((sel
!= wxNOT_FOUND
) && (sel
!= m_selection
))
316 bool wxChoicebook::DeleteAllPages()
318 GetChoiceCtrl()->Clear();
319 return wxBookCtrlBase::DeleteAllPages();
322 // ----------------------------------------------------------------------------
323 // wxChoicebook events
324 // ----------------------------------------------------------------------------
326 void wxChoicebook::OnChoiceSelected(wxCommandEvent
& eventChoice
)
328 const int selNew
= eventChoice
.GetSelection();
330 if ( selNew
== m_selection
)
332 // this event can only come from our own Select(m_selection) below
333 // which we call when the page change is vetoed, so we should simply
338 SetSelection(selNew
);
340 // change wasn't allowed, return to previous state
341 if (m_selection
!= selNew
)
342 GetChoiceCtrl()->Select(m_selection
);
345 #endif // wxUSE_CHOICEBOOK