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
)
52 wxDEFINE_EVENT( wxEVT_CHOICEBOOK_PAGE_CHANGING
, wxBookCtrlEvent
);
53 wxDEFINE_EVENT( wxEVT_CHOICEBOOK_PAGE_CHANGED
, wxBookCtrlEvent
);
55 BEGIN_EVENT_TABLE(wxChoicebook
, wxBookCtrlBase
)
56 EVT_CHOICE(wxID_ANY
, wxChoicebook::OnChoiceSelected
)
59 // ============================================================================
60 // wxChoicebook implementation
61 // ============================================================================
63 // ----------------------------------------------------------------------------
64 // wxChoicebook creation
65 // ----------------------------------------------------------------------------
68 wxChoicebook::Create(wxWindow
*parent
,
75 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
80 // no border for this control, it doesn't look nice together with
82 style
&= ~wxBORDER_MASK
;
83 style
|= wxBORDER_NONE
;
85 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
86 wxDefaultValidator
, name
) )
89 m_bookctrl
= new wxChoice
97 wxSizer
* mainSizer
= new wxBoxSizer(IsVertical() ? wxVERTICAL
: wxHORIZONTAL
);
99 if (style
& wxBK_RIGHT
|| style
& wxBK_BOTTOM
)
100 mainSizer
->Add(0, 0, 1, wxEXPAND
, 0);
102 m_controlSizer
= new wxBoxSizer(IsVertical() ? wxHORIZONTAL
: wxVERTICAL
);
103 m_controlSizer
->Add(m_bookctrl
, 1, (IsVertical() ? wxALIGN_CENTRE_VERTICAL
: wxALIGN_CENTRE
) |wxGROW
, 0);
104 mainSizer
->Add(m_controlSizer
, 0, (IsVertical() ? (int) wxGROW
: (int) wxALIGN_CENTRE_VERTICAL
)|wxALL
, m_controlMargin
);
109 // ----------------------------------------------------------------------------
110 // accessing the pages
111 // ----------------------------------------------------------------------------
113 bool wxChoicebook::SetPageText(size_t n
, const wxString
& strText
)
115 GetChoiceCtrl()->SetString(n
, strText
);
120 wxString
wxChoicebook::GetPageText(size_t n
) const
122 return GetChoiceCtrl()->GetString(n
);
125 int wxChoicebook::GetPageImage(size_t WXUNUSED(n
)) const
130 bool wxChoicebook::SetPageImage(size_t WXUNUSED(n
), int WXUNUSED(imageId
))
132 // fail silently, the code may be written to use one of several book
133 // classes and call SetPageImage() unconditionally, it's better to just
134 // ignore it (which is the best we can do short of rewriting this class to
135 // use wxBitmapComboBox anyhow) than complain loudly about a rather
141 // ----------------------------------------------------------------------------
142 // miscellaneous other stuff
143 // ----------------------------------------------------------------------------
145 void wxChoicebook::DoSetWindowVariant(wxWindowVariant variant
)
147 wxBookCtrlBase::DoSetWindowVariant(variant
);
149 m_bookctrl
->SetWindowVariant(variant
);
152 void wxChoicebook::SetImageList(wxImageList
*imageList
)
154 // TODO: can be implemented in form of static bitmap near choice control
156 wxBookCtrlBase::SetImageList(imageList
);
159 // ----------------------------------------------------------------------------
161 // ----------------------------------------------------------------------------
163 wxBookCtrlEvent
* wxChoicebook::CreatePageChangingEvent() const
165 return new wxBookCtrlEvent(wxEVT_CHOICEBOOK_PAGE_CHANGING
, m_windowId
);
168 void wxChoicebook::MakeChangedEvent(wxBookCtrlEvent
&event
)
170 event
.SetEventType(wxEVT_CHOICEBOOK_PAGE_CHANGED
);
173 // ----------------------------------------------------------------------------
174 // adding/removing the pages
175 // ----------------------------------------------------------------------------
178 wxChoicebook::InsertPage(size_t n
,
180 const wxString
& text
,
184 if ( !wxBookCtrlBase::InsertPage(n
, page
, text
, bSelect
, imageId
) )
187 GetChoiceCtrl()->Insert(text
, n
);
189 // if the inserted page is before the selected one, we must update the
190 // index of the selected page
191 if ( int(n
) <= m_selection
)
193 // one extra page added
195 GetChoiceCtrl()->Select(m_selection
);
198 if ( !DoSetSelectionAfterInsertion(n
, bSelect
) )
204 wxWindow
*wxChoicebook::DoRemovePage(size_t page
)
206 wxWindow
*win
= wxBookCtrlBase::DoRemovePage(page
);
210 GetChoiceCtrl()->Delete(page
);
212 if ( m_selection
>= (int)page
)
214 // ensure that the selection is valid
216 if ( GetPageCount() == 0 )
219 sel
= m_selection
? m_selection
- 1 : 0;
221 // if deleting current page we shouldn't try to hide it
222 m_selection
= m_selection
== (int)page
? wxNOT_FOUND
225 if ( sel
!= wxNOT_FOUND
&& sel
!= m_selection
)
234 bool wxChoicebook::DeleteAllPages()
236 GetChoiceCtrl()->Clear();
237 return wxBookCtrlBase::DeleteAllPages();
240 // ----------------------------------------------------------------------------
241 // wxChoicebook events
242 // ----------------------------------------------------------------------------
244 void wxChoicebook::OnChoiceSelected(wxCommandEvent
& eventChoice
)
246 if ( eventChoice
.GetEventObject() != m_bookctrl
)
252 const int selNew
= eventChoice
.GetSelection();
254 if ( selNew
== m_selection
)
256 // this event can only come from our own Select(m_selection) below
257 // which we call when the page change is vetoed, so we should simply
262 SetSelection(selNew
);
264 // change wasn't allowed, return to previous state
265 if (m_selection
!= selNew
)
266 GetChoiceCtrl()->Select(m_selection
);
269 #endif // wxUSE_CHOICEBOOK