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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "choicebook.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/choice.h"
34 #include "wx/choicebk.h"
35 #include "wx/imaglist.h"
36 #include "wx/settings.h"
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 // margin between the choice and the page
43 #if defined(__WXWINCE__)
44 const wxCoord MARGIN
= 1;
46 const wxCoord MARGIN
= 5;
49 // ----------------------------------------------------------------------------
50 // various wxWidgets macros
51 // ----------------------------------------------------------------------------
53 // check that the page index is valid
54 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 IMPLEMENT_DYNAMIC_CLASS(wxChoicebook
, wxControl
)
61 IMPLEMENT_DYNAMIC_CLASS(wxChoicebookEvent
, wxNotifyEvent
)
63 const wxEventType wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING
= wxNewEventType();
64 const wxEventType wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED
= wxNewEventType();
65 const int wxID_CHOICEBOOKCHOICE
= wxNewId();
67 BEGIN_EVENT_TABLE(wxChoicebook
, wxBookCtrl
)
68 EVT_SIZE(wxChoicebook::OnSize
)
69 EVT_CHOICE(wxID_CHOICEBOOKCHOICE
, wxChoicebook::OnChoiceSelected
)
72 // ============================================================================
73 // wxChoicebook implementation
74 // ============================================================================
76 // ----------------------------------------------------------------------------
77 // wxChoicebook creation
78 // ----------------------------------------------------------------------------
80 void wxChoicebook::Init()
83 m_selection
= wxNOT_FOUND
;
87 wxChoicebook::Create(wxWindow
*parent
,
94 if ( (style
& wxCHB_ALIGN_MASK
) == wxCHB_DEFAULT
)
99 // no border for this control, it doesn't look nice together with
101 style
&= ~wxBORDER_MASK
;
102 style
|= wxBORDER_NONE
;
104 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
105 wxDefaultValidator
, name
) )
108 m_choice
= new wxChoice
111 wxID_CHOICEBOOKCHOICE
,
119 // ----------------------------------------------------------------------------
120 // wxChoicebook geometry management
121 // ----------------------------------------------------------------------------
123 wxSize
wxChoicebook::GetChoiceSize() const
125 const wxSize sizeClient
= GetClientSize(),
126 sizeChoice
= m_choice
->GetBestSize();
131 size
.x
= sizeClient
.x
;
132 size
.y
= sizeChoice
.y
;
134 else // left/right aligned
136 size
.x
= sizeChoice
.x
;
137 size
.y
= sizeClient
.y
;
143 wxRect
wxChoicebook::GetPageRect() const
145 const wxSize sizeChoice
= m_choice
->GetSize();
147 wxRect
rectPage(wxPoint(0, 0), GetClientSize());
148 switch ( GetWindowStyle() & wxCHB_ALIGN_MASK
)
151 wxFAIL_MSG( _T("unexpected wxChoicebook alignment") );
155 rectPage
.y
= sizeChoice
.y
+ MARGIN
;
159 rectPage
.height
-= sizeChoice
.y
+ MARGIN
;
163 rectPage
.x
= sizeChoice
.x
+ MARGIN
;
167 rectPage
.width
-= sizeChoice
.x
+ MARGIN
;
174 void wxChoicebook::OnSize(wxSizeEvent
& event
)
180 // we're not fully created yet
184 // resize the choice control and the page area to fit inside our new size
185 const wxSize sizeClient
= GetClientSize(),
186 sizeChoice
= GetChoiceSize();
189 switch ( GetWindowStyle() & wxCHB_ALIGN_MASK
)
192 wxFAIL_MSG( _T("unexpected wxChoicebook alignment") );
197 // posChoice is already ok
201 posChoice
.y
= sizeClient
.y
- sizeChoice
.y
;
205 posChoice
.x
= sizeClient
.x
- sizeChoice
.x
;
209 m_choice
->Move(posChoice
.x
, posChoice
.y
);
210 m_choice
->SetSize(sizeChoice
.x
, sizeChoice
.y
);
212 // resize the currently shown page
213 if ( m_selection
!= wxNOT_FOUND
)
215 wxWindow
*page
= m_pages
[m_selection
];
216 wxCHECK_RET( page
, _T("NULL page in wxChoicebook?") );
217 page
->SetSize(GetPageRect());
221 wxSize
wxChoicebook::CalcSizeFromPage(const wxSize
& sizePage
) const
223 // we need to add the size of the choice control and the margin
224 const wxSize sizeChoice
= GetChoiceSize();
226 wxSize size
= sizePage
;
229 size
.y
+= sizeChoice
.y
+ MARGIN
;
231 else // left/right aligned
233 size
.x
+= sizeChoice
.x
+ MARGIN
;
240 // ----------------------------------------------------------------------------
241 // accessing the pages
242 // ----------------------------------------------------------------------------
244 bool wxChoicebook::SetPageText(size_t n
, const wxString
& strText
)
246 m_choice
->SetString(n
, strText
);
251 wxString
wxChoicebook::GetPageText(size_t n
) const
253 return m_choice
->GetString(n
);
256 int wxChoicebook::GetPageImage(size_t WXUNUSED(n
)) const
258 wxFAIL_MSG( _T("wxChoicebook::GetPageImage() not implemented") );
263 bool wxChoicebook::SetPageImage(size_t WXUNUSED(n
), int WXUNUSED(imageId
))
265 wxFAIL_MSG( _T("wxChoicebook::SetPageImage() not implemented") );
270 // ----------------------------------------------------------------------------
272 // ----------------------------------------------------------------------------
274 void wxChoicebook::SetImageList(wxImageList
*imageList
)
276 // TODO: can be implemented in form of static bitmap near choice control
278 wxBookCtrl::SetImageList(imageList
);
281 // ----------------------------------------------------------------------------
283 // ----------------------------------------------------------------------------
285 int wxChoicebook::GetSelection() const
290 int wxChoicebook::SetSelection(size_t n
)
292 wxCHECK_MSG( IS_VALID_PAGE(n
), wxNOT_FOUND
,
293 wxT("invalid page index in wxChoicebook::SetSelection()") );
295 const int oldSel
= m_selection
;
297 if ( int(n
) != m_selection
)
299 wxChoicebookEvent
event(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING
, m_windowId
);
300 event
.SetSelection(n
);
301 event
.SetOldSelection(m_selection
);
302 event
.SetEventObject(this);
303 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
305 if ( m_selection
!= wxNOT_FOUND
)
306 m_pages
[m_selection
]->Hide();
308 wxWindow
*page
= m_pages
[n
];
309 page
->SetSize(GetPageRect());
315 // program allows the page change
316 event
.SetEventType(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED
);
317 (void)GetEventHandler()->ProcessEvent(event
);
324 // ----------------------------------------------------------------------------
325 // adding/removing the pages
326 // ----------------------------------------------------------------------------
329 wxChoicebook::InsertPage(size_t n
,
331 const wxString
& text
,
335 if ( !wxBookCtrl::InsertPage(n
, page
, text
, bSelect
, imageId
) )
338 m_choice
->Insert(text
, n
);
340 // we should always have some selection if possible
341 if ( bSelect
|| (m_selection
== wxNOT_FOUND
) )
345 else // don't select this page
347 // it will be shown only when selected
351 InvalidateBestSize();
355 wxWindow
*wxChoicebook::DoRemovePage(size_t page
)
357 const int page_count
= GetPageCount();
358 wxWindow
*win
= wxBookCtrl::DoRemovePage(page
);
362 m_choice
->Delete(page
);
364 if (m_selection
>= (int)page
)
366 // force new sel valid if possible
367 int sel
= m_selection
- 1;
370 else if ((page_count
== 2) || (sel
== -1))
373 // force sel invalid if deleting current page - don't try to hide it
374 m_selection
= (m_selection
== (int)page
) ? wxNOT_FOUND
: m_selection
- 1;
376 if ((sel
!= wxNOT_FOUND
) && (sel
!= m_selection
))
385 bool wxChoicebook::DeleteAllPages()
388 return wxBookCtrl::DeleteAllPages();
391 // ----------------------------------------------------------------------------
392 // wxChoicebook events
393 // ----------------------------------------------------------------------------
395 void wxChoicebook::OnChoiceSelected(wxCommandEvent
& eventChoice
)
397 const int selNew
= eventChoice
.GetSelection();
398 const int selOld
= m_selection
;
400 if ( selNew
== m_selection
)
402 // this event can only come from our own Select(m_selection) below
403 // which we call when the page change is vetoed, so we should simply
408 // first send "change in progress" event which may be vetoed by user
409 wxChoicebookEvent
eventIng(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING
, GetId());
411 eventIng
.SetEventObject(this);
412 eventIng
.SetSelection(selNew
);
413 eventIng
.SetOldSelection(selOld
);
414 if ( GetEventHandler()->ProcessEvent(eventIng
) && !eventIng
.IsAllowed() )
416 m_choice
->Select(m_selection
);
420 // change allowed: do change the page and notify the user about it
421 SetSelection(selNew
);
423 wxChoicebookEvent
eventEd(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED
, GetId());
425 eventEd
.SetEventObject(this);
426 eventEd
.SetSelection(selNew
);
427 eventEd
.SetOldSelection(selOld
);
429 (void)GetEventHandler()->ProcessEvent(eventEd
);
432 #endif // wxUSE_CHOICEBOOK