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 const wxEventType wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING
= wxNewEventType();
50 const wxEventType wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED
= wxNewEventType();
51 const int wxID_CHOICEBOOKCHOICE
= wxNewId();
53 BEGIN_EVENT_TABLE(wxChoicebook
, wxBookCtrlBase
)
54 EVT_CHOICE(wxID_CHOICEBOOKCHOICE
, wxChoicebook::OnChoiceSelected
)
57 // ============================================================================
58 // wxChoicebook implementation
59 // ============================================================================
61 // ----------------------------------------------------------------------------
62 // wxChoicebook creation
63 // ----------------------------------------------------------------------------
65 void wxChoicebook::Init()
67 m_selection
= wxNOT_FOUND
;
71 wxChoicebook::Create(wxWindow
*parent
,
78 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
83 // no border for this control, it doesn't look nice together with
85 style
&= ~wxBORDER_MASK
;
86 style
|= wxBORDER_NONE
;
88 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
89 wxDefaultValidator
, name
) )
92 m_bookctrl
= new wxChoice
95 wxID_CHOICEBOOKCHOICE
,
100 wxSizer
* mainSizer
= new wxBoxSizer(IsVertical() ? wxVERTICAL
: wxHORIZONTAL
);
102 if (style
& wxCHB_RIGHT
|| style
& wxCHB_BOTTOM
)
103 mainSizer
->Add(0, 0, 1, wxEXPAND
, 0);
105 m_controlSizer
= new wxBoxSizer(IsVertical() ? wxHORIZONTAL
: wxVERTICAL
);
106 m_controlSizer
->Add(m_bookctrl
, 1, (IsVertical() ? wxALIGN_CENTRE_VERTICAL
: wxALIGN_CENTRE
) |wxGROW
, 0);
107 mainSizer
->Add(m_controlSizer
, 0, wxGROW
|wxALL
, m_controlMargin
);
112 // ----------------------------------------------------------------------------
113 // wxChoicebook geometry management
114 // ----------------------------------------------------------------------------
116 wxSize
wxChoicebook::GetControllerSize() const
118 const wxSize sizeClient
= GetClientSize(),
119 // sizeChoice = m_bookctrl->GetBestFittingSize();
120 sizeChoice
= m_controlSizer
->CalcMin();
125 size
.x
= sizeClient
.x
;
126 size
.y
= sizeChoice
.y
;
128 else // left/right aligned
130 size
.x
= sizeChoice
.x
;
131 size
.y
= sizeClient
.y
;
137 wxSize
wxChoicebook::CalcSizeFromPage(const wxSize
& sizePage
) const
139 // we need to add the size of the choice control and the border between
140 const wxSize sizeChoice
= GetControllerSize();
142 wxSize size
= sizePage
;
145 size
.y
+= sizeChoice
.y
+ GetInternalBorder();
147 else // left/right aligned
149 size
.x
+= sizeChoice
.x
+ GetInternalBorder();
156 // ----------------------------------------------------------------------------
157 // accessing the pages
158 // ----------------------------------------------------------------------------
160 bool wxChoicebook::SetPageText(size_t n
, const wxString
& strText
)
162 GetChoiceCtrl()->SetString(n
, strText
);
167 wxString
wxChoicebook::GetPageText(size_t n
) const
169 return GetChoiceCtrl()->GetString(n
);
172 int wxChoicebook::GetPageImage(size_t WXUNUSED(n
)) const
174 wxFAIL_MSG( _T("wxChoicebook::GetPageImage() not implemented") );
179 bool wxChoicebook::SetPageImage(size_t WXUNUSED(n
), int WXUNUSED(imageId
))
181 wxFAIL_MSG( _T("wxChoicebook::SetPageImage() not implemented") );
186 // ----------------------------------------------------------------------------
188 // ----------------------------------------------------------------------------
190 void wxChoicebook::SetImageList(wxImageList
*imageList
)
192 // TODO: can be implemented in form of static bitmap near choice control
194 wxBookCtrlBase::SetImageList(imageList
);
197 // ----------------------------------------------------------------------------
199 // ----------------------------------------------------------------------------
201 int wxChoicebook::GetSelection() const
206 int wxChoicebook::SetSelection(size_t n
)
208 wxCHECK_MSG( IS_VALID_PAGE(n
), wxNOT_FOUND
,
209 wxT("invalid page index in wxChoicebook::SetSelection()") );
211 const int oldSel
= m_selection
;
213 if ( int(n
) != m_selection
)
215 wxChoicebookEvent
event(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING
, m_windowId
);
216 event
.SetSelection(n
);
217 event
.SetOldSelection(m_selection
);
218 event
.SetEventObject(this);
219 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
221 if ( m_selection
!= wxNOT_FOUND
)
222 m_pages
[m_selection
]->Hide();
224 wxWindow
*page
= m_pages
[n
];
225 page
->SetSize(GetPageRect());
228 // change m_selection now to ignore the selection change event
230 GetChoiceCtrl()->Select(n
);
232 // program allows the page change
233 event
.SetEventType(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED
);
234 (void)GetEventHandler()->ProcessEvent(event
);
241 // ----------------------------------------------------------------------------
242 // adding/removing the pages
243 // ----------------------------------------------------------------------------
246 wxChoicebook::InsertPage(size_t n
,
248 const wxString
& text
,
252 if ( !wxBookCtrlBase::InsertPage(n
, page
, text
, bSelect
, imageId
) )
255 GetChoiceCtrl()->Insert(text
, n
);
257 // if the inserted page is before the selected one, we must update the
258 // index of the selected page
259 if ( int(n
) <= m_selection
)
261 // one extra page added
263 GetChoiceCtrl()->Select(m_selection
);
266 // some page should be selected: either this one or the first one if there
267 // is still no selection
268 int selNew
= wxNOT_FOUND
;
271 else if ( m_selection
== wxNOT_FOUND
)
274 if ( selNew
!= m_selection
)
277 if ( selNew
!= wxNOT_FOUND
)
278 SetSelection(selNew
);
280 InvalidateBestSize();
284 wxWindow
*wxChoicebook::DoRemovePage(size_t page
)
286 const size_t page_count
= GetPageCount();
287 wxWindow
*win
= wxBookCtrlBase::DoRemovePage(page
);
291 GetChoiceCtrl()->Delete(page
);
293 if (m_selection
>= (int)page
)
295 // force new sel valid if possible
296 int sel
= m_selection
- 1;
299 else if ((page_count
== 2) || (sel
== -1))
302 // force sel invalid if deleting current page - don't try to hide it
303 m_selection
= (m_selection
== (int)page
) ? wxNOT_FOUND
: m_selection
- 1;
305 if ((sel
!= wxNOT_FOUND
) && (sel
!= m_selection
))
314 bool wxChoicebook::DeleteAllPages()
316 GetChoiceCtrl()->Clear();
317 return wxBookCtrlBase::DeleteAllPages();
320 // ----------------------------------------------------------------------------
321 // wxChoicebook events
322 // ----------------------------------------------------------------------------
324 void wxChoicebook::OnChoiceSelected(wxCommandEvent
& eventChoice
)
326 const int selNew
= eventChoice
.GetSelection();
328 if ( selNew
== m_selection
)
330 // this event can only come from our own Select(m_selection) below
331 // which we call when the page change is vetoed, so we should simply
336 SetSelection(selNew
);
338 // change wasn't allowed, return to previous state
339 if (m_selection
!= selNew
)
340 GetChoiceCtrl()->Select(m_selection
);
343 #endif // wxUSE_CHOICEBOOK