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();
148 wxRect
rectPage(pt
, GetClientSize());
149 switch ( GetWindowStyle() & wxCHB_ALIGN_MASK
)
152 wxFAIL_MSG( _T("unexpected wxChoicebook alignment") );
156 rectPage
.y
= sizeChoice
.y
+ MARGIN
;
160 rectPage
.height
-= sizeChoice
.y
+ MARGIN
;
164 rectPage
.x
= sizeChoice
.x
+ MARGIN
;
168 rectPage
.width
-= sizeChoice
.x
+ MARGIN
;
175 void wxChoicebook::OnSize(wxSizeEvent
& event
)
181 // we're not fully created yet
185 // resize the choice control and the page area to fit inside our new size
186 const wxSize sizeClient
= GetClientSize(),
187 sizeChoice
= GetChoiceSize();
190 switch ( GetWindowStyle() & wxCHB_ALIGN_MASK
)
193 wxFAIL_MSG( _T("unexpected wxChoicebook alignment") );
198 // posChoice is already ok
202 posChoice
.y
= sizeClient
.y
- sizeChoice
.y
;
206 posChoice
.x
= sizeClient
.x
- sizeChoice
.x
;
210 m_choice
->Move(posChoice
.x
, posChoice
.y
);
211 m_choice
->SetSize(sizeChoice
.x
, sizeChoice
.y
);
213 // resize the currently shown page
214 if ( m_selection
!= wxNOT_FOUND
)
216 wxWindow
*page
= m_pages
[m_selection
];
217 wxCHECK_RET( page
, _T("NULL page in wxChoicebook?") );
218 page
->SetSize(GetPageRect());
222 wxSize
wxChoicebook::CalcSizeFromPage(const wxSize
& sizePage
) const
224 // we need to add the size of the choice control and the margin
225 const wxSize sizeChoice
= GetChoiceSize();
227 wxSize size
= sizePage
;
230 size
.y
+= sizeChoice
.y
+ MARGIN
;
232 else // left/right aligned
234 size
.x
+= sizeChoice
.x
+ MARGIN
;
241 // ----------------------------------------------------------------------------
242 // accessing the pages
243 // ----------------------------------------------------------------------------
245 bool wxChoicebook::SetPageText(size_t n
, const wxString
& strText
)
247 m_choice
->SetString(n
, strText
);
252 wxString
wxChoicebook::GetPageText(size_t n
) const
254 return m_choice
->GetString(n
);
257 int wxChoicebook::GetPageImage(size_t WXUNUSED(n
)) const
259 wxFAIL_MSG( _T("wxChoicebook::GetPageImage() not implemented") );
264 bool wxChoicebook::SetPageImage(size_t WXUNUSED(n
), int WXUNUSED(imageId
))
266 wxFAIL_MSG( _T("wxChoicebook::SetPageImage() not implemented") );
271 // ----------------------------------------------------------------------------
273 // ----------------------------------------------------------------------------
275 void wxChoicebook::SetImageList(wxImageList
*imageList
)
277 // TODO: can be implemented in form of static bitmap near choice control
279 wxBookCtrl::SetImageList(imageList
);
282 // ----------------------------------------------------------------------------
284 // ----------------------------------------------------------------------------
286 int wxChoicebook::GetSelection() const
291 int wxChoicebook::SetSelection(size_t n
)
293 wxCHECK_MSG( IS_VALID_PAGE(n
), wxNOT_FOUND
,
294 wxT("invalid page index in wxChoicebook::SetSelection()") );
296 const int oldSel
= m_selection
;
298 if ( int(n
) != m_selection
)
300 wxChoicebookEvent
event(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGING
, m_windowId
);
301 event
.SetSelection(n
);
302 event
.SetOldSelection(m_selection
);
303 event
.SetEventObject(this);
304 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
306 if ( m_selection
!= wxNOT_FOUND
)
307 m_pages
[m_selection
]->Hide();
309 wxWindow
*page
= m_pages
[n
];
310 page
->SetSize(GetPageRect());
313 // change m_selection now to ignore the selection change event
317 // program allows the page change
318 event
.SetEventType(wxEVT_COMMAND_CHOICEBOOK_PAGE_CHANGED
);
319 (void)GetEventHandler()->ProcessEvent(event
);
326 // ----------------------------------------------------------------------------
327 // adding/removing the pages
328 // ----------------------------------------------------------------------------
331 wxChoicebook::InsertPage(size_t n
,
333 const wxString
& text
,
337 if ( !wxBookCtrl::InsertPage(n
, page
, text
, bSelect
, imageId
) )
340 m_choice
->Insert(text
, n
);
342 // if the inserted page is before the selected one, we must update the
343 // index of the selected page
344 if ( int(n
) <= m_selection
)
346 // one extra page added
348 m_choice
->Select(m_selection
);
351 // some page should be selected: either this one or the first one if there
352 // is still no selection
356 else if ( m_selection
== -1 )
359 if ( selNew
!= m_selection
)
363 SetSelection(selNew
);
365 InvalidateBestSize();
369 wxWindow
*wxChoicebook::DoRemovePage(size_t page
)
371 const int page_count
= GetPageCount();
372 wxWindow
*win
= wxBookCtrl::DoRemovePage(page
);
376 m_choice
->Delete(page
);
378 if (m_selection
>= (int)page
)
380 // force new sel valid if possible
381 int sel
= m_selection
- 1;
384 else if ((page_count
== 2) || (sel
== -1))
387 // force sel invalid if deleting current page - don't try to hide it
388 m_selection
= (m_selection
== (int)page
) ? wxNOT_FOUND
: m_selection
- 1;
390 if ((sel
!= wxNOT_FOUND
) && (sel
!= m_selection
))
399 bool wxChoicebook::DeleteAllPages()
402 return wxBookCtrl::DeleteAllPages();
405 // ----------------------------------------------------------------------------
406 // wxChoicebook events
407 // ----------------------------------------------------------------------------
409 void wxChoicebook::OnChoiceSelected(wxCommandEvent
& eventChoice
)
411 const int selNew
= eventChoice
.GetSelection();
413 if ( selNew
== m_selection
)
415 // this event can only come from our own Select(m_selection) below
416 // which we call when the page change is vetoed, so we should simply
421 SetSelection(selNew
);
423 // change wasn't allowed, return to previous state
424 if (m_selection
!= selNew
)
425 m_choice
->Select(m_selection
);
428 #endif // wxUSE_CHOICEBOOK