1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: 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
, wxControl
)
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_SIZE(wxChoicebook::OnSize
)
54 EVT_CHOICE(wxID_CHOICEBOOKCHOICE
, wxChoicebook::OnChoiceSelected
)
57 // ============================================================================
58 // wxChoicebook implementation
59 // ============================================================================
61 // ----------------------------------------------------------------------------
62 // wxChoicebook creation
63 // ----------------------------------------------------------------------------
65 void wxChoicebook::Init()
68 m_selection
= wxNOT_FOUND
;
72 wxChoicebook::Create(wxWindow
*parent
,
79 if ( (style
& wxCHB_ALIGN_MASK
) == wxCHB_DEFAULT
)
84 // no border for this control, it doesn't look nice together with
86 style
&= ~wxBORDER_MASK
;
87 style
|= wxBORDER_NONE
;
89 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
90 wxDefaultValidator
, name
) )
93 m_choice
= new wxChoice
96 wxID_CHOICEBOOKCHOICE
,
104 // ----------------------------------------------------------------------------
105 // wxChoicebook geometry management
106 // ----------------------------------------------------------------------------
108 wxSize
wxChoicebook::GetChoiceSize() const
110 const wxSize sizeClient
= GetClientSize(),
111 sizeChoice
= m_choice
->GetBestFittingSize();
116 size
.x
= sizeClient
.x
;
117 size
.y
= sizeChoice
.y
;
119 else // left/right aligned
121 size
.x
= sizeChoice
.x
;
122 size
.y
= sizeClient
.y
;
128 wxRect
wxChoicebook::GetPageRect() const
130 const wxSize sizeChoice
= m_choice
->GetBestFittingSize();
133 wxRect
rectPage(pt
, GetClientSize());
134 switch ( GetWindowStyle() & wxCHB_ALIGN_MASK
)
137 wxFAIL_MSG( _T("unexpected wxChoicebook alignment") );
141 rectPage
.y
= sizeChoice
.y
+ GetInternalBorder();
145 rectPage
.height
-= sizeChoice
.y
+ GetInternalBorder();
149 rectPage
.x
= sizeChoice
.x
+ GetInternalBorder();
153 rectPage
.width
-= sizeChoice
.x
+ GetInternalBorder();
160 void wxChoicebook::OnSize(wxSizeEvent
& event
)
166 // we're not fully created yet
170 // resize the choice control and the page area to fit inside our new size
171 const wxSize sizeClient
= GetClientSize(),
172 sizeChoice
= GetChoiceSize();
175 switch ( GetWindowStyle() & wxCHB_ALIGN_MASK
)
178 wxFAIL_MSG( _T("unexpected wxChoicebook alignment") );
183 // posChoice is already ok
187 posChoice
.y
= sizeClient
.y
- sizeChoice
.y
;
191 posChoice
.x
= sizeClient
.x
- sizeChoice
.x
;
195 m_choice
->Move(posChoice
);
196 m_choice
->SetSize(sizeChoice
);
198 // resize the currently shown page
199 if ( m_selection
!= wxNOT_FOUND
)
201 wxWindow
*page
= m_pages
[m_selection
];
202 wxCHECK_RET( page
, _T("NULL page in wxChoicebook?") );
203 page
->SetSize(GetPageRect());
207 wxSize
wxChoicebook::CalcSizeFromPage(const wxSize
& sizePage
) const
209 // we need to add the size of the choice control and the border between
210 const wxSize sizeChoice
= GetChoiceSize();
212 wxSize size
= sizePage
;
215 size
.y
+= sizeChoice
.y
+ GetInternalBorder();
217 else // left/right aligned
219 size
.x
+= sizeChoice
.x
+ GetInternalBorder();
226 // ----------------------------------------------------------------------------
227 // accessing the pages
228 // ----------------------------------------------------------------------------
230 bool wxChoicebook::SetPageText(size_t n
, const wxString
& strText
)
232 m_choice
->SetString(n
, strText
);
237 wxString
wxChoicebook::GetPageText(size_t n
) const
239 return m_choice
->GetString(n
);
242 int wxChoicebook::GetPageImage(size_t WXUNUSED(n
)) const
244 wxFAIL_MSG( _T("wxChoicebook::GetPageImage() not implemented") );
249 bool wxChoicebook::SetPageImage(size_t WXUNUSED(n
), int WXUNUSED(imageId
))
251 wxFAIL_MSG( _T("wxChoicebook::SetPageImage() not implemented") );
256 // ----------------------------------------------------------------------------
258 // ----------------------------------------------------------------------------
260 void wxChoicebook::SetImageList(wxImageList
*imageList
)
262 // TODO: can be implemented in form of static bitmap near choice control
264 wxBookCtrlBase::SetImageList(imageList
);
267 // ----------------------------------------------------------------------------
269 // ----------------------------------------------------------------------------
271 int wxChoicebook::GetSelection() const
276 int wxChoicebook::SetSelection(size_t n
)
278 wxCHECK_MSG( IS_VALID_PAGE(n
), wxNOT_FOUND
,
279 wxT("invalid page index in wxChoicebook::SetSelection()") );
281 const int oldSel
= m_selection
;
283 if ( int(n
) != m_selection
)
285 wxChoicebookEvent
event(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING
, m_windowId
);
286 event
.SetSelection(n
);
287 event
.SetOldSelection(m_selection
);
288 event
.SetEventObject(this);
289 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
291 if ( m_selection
!= wxNOT_FOUND
)
292 m_pages
[m_selection
]->Hide();
294 wxWindow
*page
= m_pages
[n
];
295 page
->SetSize(GetPageRect());
298 // change m_selection now to ignore the selection change event
302 // program allows the page change
303 event
.SetEventType(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED
);
304 (void)GetEventHandler()->ProcessEvent(event
);
311 // ----------------------------------------------------------------------------
312 // adding/removing the pages
313 // ----------------------------------------------------------------------------
316 wxChoicebook::InsertPage(size_t n
,
318 const wxString
& text
,
322 if ( !wxBookCtrlBase::InsertPage(n
, page
, text
, bSelect
, imageId
) )
325 m_choice
->Insert(text
, n
);
327 // if the inserted page is before the selected one, we must update the
328 // index of the selected page
329 if ( int(n
) <= m_selection
)
331 // one extra page added
333 m_choice
->Select(m_selection
);
336 // some page should be selected: either this one or the first one if there
337 // is still no selection
338 int selNew
= wxNOT_FOUND
;
341 else if ( m_selection
== wxNOT_FOUND
)
344 if ( selNew
!= m_selection
)
347 if ( selNew
!= wxNOT_FOUND
)
348 SetSelection(selNew
);
350 InvalidateBestSize();
354 wxWindow
*wxChoicebook::DoRemovePage(size_t page
)
356 const size_t page_count
= GetPageCount();
357 wxWindow
*win
= wxBookCtrlBase::DoRemovePage(page
);
361 m_choice
->Delete(page
);
363 if (m_selection
>= (int)page
)
365 // force new sel valid if possible
366 int sel
= m_selection
- 1;
369 else if ((page_count
== 2) || (sel
== -1))
372 // force sel invalid if deleting current page - don't try to hide it
373 m_selection
= (m_selection
== (int)page
) ? wxNOT_FOUND
: m_selection
- 1;
375 if ((sel
!= wxNOT_FOUND
) && (sel
!= m_selection
))
384 bool wxChoicebook::DeleteAllPages()
387 return wxBookCtrlBase::DeleteAllPages();
390 // ----------------------------------------------------------------------------
391 // wxChoicebook events
392 // ----------------------------------------------------------------------------
394 void wxChoicebook::OnChoiceSelected(wxCommandEvent
& eventChoice
)
396 const int selNew
= eventChoice
.GetSelection();
398 if ( selNew
== m_selection
)
400 // this event can only come from our own Select(m_selection) below
401 // which we call when the page change is vetoed, so we should simply
406 SetSelection(selNew
);
408 // change wasn't allowed, return to previous state
409 if (m_selection
!= selNew
)
410 m_choice
->Select(m_selection
);
413 #endif // wxUSE_CHOICEBOOK