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 IMPLEMENT_DYNAMIC_CLASS(wxChoicebook
, wxControl
)
54 IMPLEMENT_DYNAMIC_CLASS(wxChoicebookEvent
, wxNotifyEvent
)
56 const wxEventType wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING
= wxNewEventType();
57 const wxEventType wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED
= wxNewEventType();
58 const int wxID_CHOICEBOOKCHOICE
= wxNewId();
60 BEGIN_EVENT_TABLE(wxChoicebook
, wxBookCtrl
)
61 EVT_SIZE(wxChoicebook::OnSize
)
62 EVT_CHOICE(wxID_CHOICEBOOKCHOICE
, wxChoicebook::OnChoiceSelected
)
65 // ============================================================================
66 // wxChoicebook implementation
67 // ============================================================================
69 // ----------------------------------------------------------------------------
70 // wxChoicebook creation
71 // ----------------------------------------------------------------------------
73 void wxChoicebook::Init()
76 m_selection
= wxNOT_FOUND
;
80 wxChoicebook::Create(wxWindow
*parent
,
87 if ( (style
& wxCHB_ALIGN_MASK
) == wxCHB_DEFAULT
)
92 // no border for this control, it doesn't look nice together with
94 style
&= ~wxBORDER_MASK
;
95 style
|= wxBORDER_NONE
;
97 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
98 wxDefaultValidator
, name
) )
101 m_choice
= new wxChoice
104 wxID_CHOICEBOOKCHOICE
,
112 // ----------------------------------------------------------------------------
113 // wxChoicebook geometry management
114 // ----------------------------------------------------------------------------
116 wxSize
wxChoicebook::GetChoiceSize() const
118 const wxSize sizeClient
= GetClientSize(),
119 sizeChoice
= m_choice
->GetBestSize();
124 size
.x
= sizeClient
.x
;
125 size
.y
= sizeChoice
.y
;
127 else // left/right aligned
129 size
.x
= sizeChoice
.x
;
130 size
.y
= sizeClient
.y
;
136 wxRect
wxChoicebook::GetPageRect() const
138 const wxSize sizeChoice
= m_choice
->GetSize();
140 wxRect
rectPage(wxPoint(0, 0), GetClientSize());
141 switch ( GetWindowStyle() & wxCHB_ALIGN_MASK
)
144 wxFAIL_MSG( _T("unexpected wxChoicebook alignment") );
148 rectPage
.y
= sizeChoice
.y
+ MARGIN
;
152 rectPage
.height
-= sizeChoice
.y
+ MARGIN
;
156 rectPage
.x
= sizeChoice
.x
+ MARGIN
;
160 rectPage
.width
-= sizeChoice
.x
+ MARGIN
;
167 void wxChoicebook::OnSize(wxSizeEvent
& event
)
173 // we're not fully created yet
177 // resize the choice control and the page area to fit inside our new size
178 const wxSize sizeClient
= GetClientSize(),
179 sizeChoice
= GetChoiceSize();
182 switch ( GetWindowStyle() & wxCHB_ALIGN_MASK
)
185 wxFAIL_MSG( _T("unexpected wxChoicebook alignment") );
190 // posChoice is already ok
194 posChoice
.y
= sizeClient
.y
- sizeChoice
.y
;
198 posChoice
.x
= sizeClient
.x
- sizeChoice
.x
;
202 m_choice
->Move(posChoice
.x
, posChoice
.y
);
203 m_choice
->SetSize(sizeChoice
.x
, sizeChoice
.y
);
205 // resize the currently shown page
206 if ( m_selection
!= wxNOT_FOUND
)
208 wxWindow
*page
= m_pages
[m_selection
];
209 wxCHECK_RET( page
, _T("NULL page in wxChoicebook?") );
210 page
->SetSize(GetPageRect());
214 wxSize
wxChoicebook::CalcSizeFromPage(const wxSize
& sizePage
) const
216 // we need to add the size of the choice control and the margin
217 const wxSize sizeChoice
= GetChoiceSize();
219 wxSize size
= sizePage
;
222 size
.y
+= sizeChoice
.y
+ MARGIN
;
224 else // left/right aligned
226 size
.x
+= sizeChoice
.x
+ MARGIN
;
233 // ----------------------------------------------------------------------------
234 // accessing the pages
235 // ----------------------------------------------------------------------------
237 bool wxChoicebook::SetPageText(size_t n
, const wxString
& strText
)
239 m_choice
->SetString(n
, strText
);
244 wxString
wxChoicebook::GetPageText(size_t n
) const
246 return m_choice
->GetString(n
);
249 int wxChoicebook::GetPageImage(size_t WXUNUSED(n
)) const
251 wxFAIL_MSG( _T("wxChoicebook::GetPageImage() not implemented") );
256 bool wxChoicebook::SetPageImage(size_t WXUNUSED(n
), int WXUNUSED(imageId
))
258 wxFAIL_MSG( _T("wxChoicebook::SetPageImage() not implemented") );
263 // ----------------------------------------------------------------------------
265 // ----------------------------------------------------------------------------
267 void wxChoicebook::SetImageList(wxImageList
*imageList
)
269 // TODO: can be implemented in form of static bitmap near choice control
271 wxBookCtrl::SetImageList(imageList
);
274 // ----------------------------------------------------------------------------
276 // ----------------------------------------------------------------------------
278 int wxChoicebook::GetSelection() const
283 int wxChoicebook::SetSelection(size_t n
)
285 wxCHECK_MSG( n
< GetPageCount(), wxNOT_FOUND
,
286 _T("invalid page index in wxChoicebook::SetSelection()") );
288 const int selOld
= m_selection
;
290 if ( (int)n
!= m_selection
)
292 if ( m_selection
!= wxNOT_FOUND
)
293 m_pages
[m_selection
]->Hide();
294 wxWindow
*page
= m_pages
[n
];
295 page
->SetSize(GetPageRect());
298 // change m_selection only now to ignore the selection change event
306 // ----------------------------------------------------------------------------
307 // adding/removing the pages
308 // ----------------------------------------------------------------------------
311 wxChoicebook::InsertPage(size_t n
,
313 const wxString
& text
,
317 if ( !wxBookCtrl::InsertPage(n
, page
, text
, bSelect
, imageId
) )
320 m_choice
->Insert(text
, n
);
322 // we should always have some selection if possible
323 if ( bSelect
|| (m_selection
== wxNOT_FOUND
) )
327 else // don't select this page
329 // it will be shown only when selected
333 InvalidateBestSize();
337 wxWindow
*wxChoicebook::DoRemovePage(size_t page
)
339 const int page_count
= GetPageCount();
340 wxWindow
*win
= wxBookCtrl::DoRemovePage(page
);
344 m_choice
->Delete(page
);
346 if (m_selection
>= (int)page
)
348 // force new sel valid if possible
349 int sel
= m_selection
- 1;
352 else if ((page_count
== 2) || (sel
== -1))
355 // force sel invalid if deleting current page - don't try to hide it
356 m_selection
= (m_selection
== (int)page
) ? wxNOT_FOUND
: m_selection
- 1;
358 if ((sel
!= wxNOT_FOUND
) && (sel
!= m_selection
))
367 bool wxChoicebook::DeleteAllPages()
370 return wxBookCtrl::DeleteAllPages();
373 // ----------------------------------------------------------------------------
374 // wxChoicebook events
375 // ----------------------------------------------------------------------------
377 void wxChoicebook::OnChoiceSelected(wxCommandEvent
& eventChoice
)
379 const int selNew
= eventChoice
.GetSelection();
380 const int selOld
= m_selection
;
382 if ( selNew
== m_selection
)
384 // this event can only come from our own Select(m_selection) below
385 // which we call when the page change is vetoed, so we should simply
390 // first send "change in progress" event which may be vetoed by user
391 wxChoicebookEvent
eventIng(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING
, GetId());
393 eventIng
.SetEventObject(this);
394 eventIng
.SetSelection(selNew
);
395 eventIng
.SetOldSelection(selOld
);
396 if ( GetEventHandler()->ProcessEvent(eventIng
) && !eventIng
.IsAllowed() )
398 m_choice
->Select(m_selection
);
402 // change allowed: do change the page and notify the user about it
403 SetSelection(selNew
);
405 wxChoicebookEvent
eventEd(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED
, GetId());
407 eventEd
.SetEventObject(this);
408 eventEd
.SetSelection(selNew
);
409 eventEd
.SetOldSelection(selOld
);
411 (void)GetEventHandler()->ProcessEvent(eventEd
);
414 #endif // wxUSE_CHOICEBOOK