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 // we should always have some selection if possible
206 if ( m_selection
== wxNOT_FOUND
&& GetPageCount() )
211 if ( m_selection
!= wxNOT_FOUND
)
213 wxWindow
*page
= m_pages
[m_selection
];
214 wxCHECK_RET( page
, _T("NULL page in wxChoicebook?") );
216 page
->SetSize(GetPageRect());
217 if ( !page
->IsShown() )
224 wxSize
wxChoicebook::CalcSizeFromPage(const wxSize
& sizePage
) const
226 // we need to add the size of the choice control and the margin
227 const wxSize sizeChoice
= GetChoiceSize();
229 wxSize size
= sizePage
;
232 size
.y
+= sizeChoice
.y
+ MARGIN
;
234 else // left/right aligned
236 size
.x
+= sizeChoice
.x
+ MARGIN
;
243 // ----------------------------------------------------------------------------
244 // accessing the pages
245 // ----------------------------------------------------------------------------
247 bool wxChoicebook::SetPageText(size_t n
, const wxString
& strText
)
249 m_choice
->SetString(n
, strText
);
254 wxString
wxChoicebook::GetPageText(size_t n
) const
256 return m_choice
->GetString(n
);
259 int wxChoicebook::GetPageImage(size_t WXUNUSED(n
)) const
261 wxFAIL_MSG( _T("wxChoicebook::GetPageImage() not implemented") );
266 bool wxChoicebook::SetPageImage(size_t WXUNUSED(n
), int WXUNUSED(imageId
))
268 wxFAIL_MSG( _T("wxChoicebook::SetPageImage() not implemented") );
273 // ----------------------------------------------------------------------------
275 // ----------------------------------------------------------------------------
277 void wxChoicebook::SetImageList(wxImageList
*imageList
)
279 // TODO: can be implemented in form of static bitmap near choice control
281 wxBookCtrl::SetImageList(imageList
);
284 // ----------------------------------------------------------------------------
286 // ----------------------------------------------------------------------------
288 int wxChoicebook::GetSelection() const
293 int wxChoicebook::SetSelection(size_t n
)
295 wxCHECK_MSG( n
< GetPageCount(), wxNOT_FOUND
,
296 _T("invalid page index in wxChoicebook::SetSelection()") );
298 int selOld
= m_selection
;
300 if ( (int)n
!= m_selection
)
304 // change m_selection only now, otherwise OnChoiceSelected() would ignore
305 // the selection change event
307 if ( m_selection
!= wxNOT_FOUND
)
308 m_pages
[m_selection
]->Hide();
309 wxWindow
*page
= m_pages
[m_selection
= n
];
310 page
->SetSize(GetPageRect());
318 // ----------------------------------------------------------------------------
319 // adding/removing the pages
320 // ----------------------------------------------------------------------------
323 wxChoicebook::InsertPage(size_t n
,
325 const wxString
& text
,
329 if ( !wxBookCtrl::InsertPage(n
, page
, text
, bSelect
, imageId
) )
332 m_choice
->Insert(text
, n
);
338 else // don't select this page
340 // it will be shown only when selected
344 InvalidateBestSize();
348 wxWindow
*wxChoicebook::DoRemovePage(size_t page
)
350 wxWindow
*win
= wxBookCtrl::DoRemovePage(page
);
353 m_choice
->Delete(page
);
360 bool wxChoicebook::DeleteAllPages()
363 return wxBookCtrl::DeleteAllPages();
366 // ----------------------------------------------------------------------------
367 // wxChoicebook events
368 // ----------------------------------------------------------------------------
370 void wxChoicebook::OnChoiceSelected(wxCommandEvent
& eventChoice
)
372 const int selNew
= eventChoice
.GetSelection();
374 if ( selNew
== m_selection
)
376 // this event can only come from our own Select(m_selection) below
377 // which we call when the page change is vetoed, so we should simply
382 // first send "change in progress" event which may be vetoed by user
383 wxChoicebookEvent
eventIng(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING
, GetId());
385 eventIng
.SetEventObject(this);
386 eventIng
.SetSelection(selNew
);
387 eventIng
.SetOldSelection(m_selection
);
388 if ( GetEventHandler()->ProcessEvent(eventIng
) && !eventIng
.IsAllowed() )
390 m_choice
->Select(m_selection
);
394 // change allowed: do change the page and notify the user about it
395 if ( m_selection
!= wxNOT_FOUND
)
396 m_pages
[m_selection
]->Hide();
397 wxWindow
*page
= m_pages
[m_selection
= selNew
];
398 page
->SetSize(GetPageRect());
401 wxChoicebookEvent
eventEd(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED
, GetId());
403 eventEd
.SetEventObject(this);
404 eventEd
.SetSelection(selNew
);
405 eventEd
.SetOldSelection(m_selection
);
407 (void)GetEventHandler()->ProcessEvent(eventEd
);
410 #endif // wxUSE_CHOICEBOOK