1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/listbkg.cpp
3 // Purpose: generic implementation of wxListbook
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
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/listbook.h"
32 #include "wx/settings.h"
35 #include "wx/listctrl.h"
36 #include "wx/statline.h"
37 #include "wx/imaglist.h"
39 // ----------------------------------------------------------------------------
40 // various wxWidgets macros
41 // ----------------------------------------------------------------------------
43 // check that the page index is valid
44 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 IMPLEMENT_DYNAMIC_CLASS(wxListbook
, wxBookCtrlBase
)
51 IMPLEMENT_DYNAMIC_CLASS(wxListbookEvent
, wxNotifyEvent
)
53 #if !WXWIN_COMPATIBILITY_EVENT_TYPES
54 const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING
= wxNewEventType();
55 const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED
= wxNewEventType();
58 BEGIN_EVENT_TABLE(wxListbook
, wxBookCtrlBase
)
59 EVT_SIZE(wxListbook::OnSize
)
60 EVT_LIST_ITEM_SELECTED(wxID_ANY
, wxListbook::OnListSelected
)
63 // ============================================================================
64 // wxListbook implementation
65 // ============================================================================
67 // ----------------------------------------------------------------------------
68 // wxListbook creation
69 // ----------------------------------------------------------------------------
71 void wxListbook::Init()
73 m_selection
= wxNOT_FOUND
;
77 wxListbook::Create(wxWindow
*parent
,
84 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
90 #endif // __WXMAC__/!__WXMAC__
93 // no border for this control, it doesn't look nice together with
95 style
&= ~wxBORDER_MASK
;
96 style
|= wxBORDER_NONE
;
98 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
99 wxDefaultValidator
, name
) )
102 m_bookctrl
= new wxListView
108 wxLC_ICON
| wxLC_SINGLE_SEL
|
109 (IsVertical() ? wxLC_ALIGN_LEFT
: wxLC_ALIGN_TOP
)
113 // On XP with themes enabled the GetViewRect used in GetControllerSize() to
114 // determine the space needed for the list view will incorrectly return
115 // (0,0,0,0) the first time. So send a pending event so OnSize will be
116 // called again after the window is ready to go. Technically we don't
117 // need to do this on non-XP windows, but if things are already sized
118 // correctly then nothing changes and so there is no harm.
120 GetEventHandler()->AddPendingEvent(evt
);
125 // ----------------------------------------------------------------------------
126 // wxListbook geometry management
127 // ----------------------------------------------------------------------------
129 wxSize
wxListbook::GetControllerSize() const
131 const wxSize sizeClient
= GetClientSize(),
132 sizeBorder
= m_bookctrl
->GetSize() - m_bookctrl
->GetClientSize(),
133 sizeList
= GetListView()->GetViewRect().GetSize() + sizeBorder
;
139 size
.x
= sizeClient
.x
;
142 else // left/right aligned
145 size
.y
= sizeClient
.y
;
151 void wxListbook::OnSize(wxSizeEvent
& event
)
153 // arrange the icons before calling SetClientSize(), otherwise it wouldn't
154 // account for the scrollbars the list control might need and, at least
155 // under MSW, we'd finish with an ugly looking list control with both
156 // vertical and horizontal scrollbar (with one of them being added because
157 // the other one is not accounted for in client size computations)
158 wxListView
*list
= GetListView();
159 if (list
) list
->Arrange();
160 wxBookCtrlBase::OnSize(event
);
163 int wxListbook::HitTest(const wxPoint
& pt
, long *flags
) const
165 int pagePos
= wxNOT_FOUND
;
168 *flags
= wxBK_HITTEST_NOWHERE
;
170 // convert from listbook control coordinates to list control coordinates
171 const wxListView
* const list
= GetListView();
172 const wxPoint listPt
= list
->ScreenToClient(ClientToScreen(pt
));
174 // is the point inside list control?
175 if ( wxRect(list
->GetSize()).Contains(listPt
) )
178 pagePos
= list
->HitTest(listPt
, flagsList
);
182 if ( pagePos
!= wxNOT_FOUND
)
185 if ( flagsList
& (wxLIST_HITTEST_ONITEMICON
|
186 wxLIST_HITTEST_ONITEMSTATEICON
) )
187 *flags
|= wxBK_HITTEST_ONICON
;
189 if ( flagsList
& wxLIST_HITTEST_ONITEMLABEL
)
190 *flags
|= wxBK_HITTEST_ONLABEL
;
193 else // not over list control at all
195 if ( flags
&& GetPageRect().Contains(pt
) )
196 *flags
|= wxBK_HITTEST_ONPAGE
;
202 wxSize
wxListbook::CalcSizeFromPage(const wxSize
& sizePage
) const
204 // we need to add the size of the list control and the border between
205 const wxSize sizeList
= GetControllerSize();
207 wxSize size
= sizePage
;
210 size
.y
+= sizeList
.y
+ GetInternalBorder();
212 else // left/right aligned
214 size
.x
+= sizeList
.x
+ GetInternalBorder();
221 // ----------------------------------------------------------------------------
222 // accessing the pages
223 // ----------------------------------------------------------------------------
225 bool wxListbook::SetPageText(size_t n
, const wxString
& strText
)
227 GetListView()->SetItemText(n
, strText
);
232 wxString
wxListbook::GetPageText(size_t n
) const
234 return GetListView()->GetItemText(n
);
237 int wxListbook::GetPageImage(size_t WXUNUSED(n
)) const
239 wxFAIL_MSG( _T("wxListbook::GetPageImage() not implemented") );
244 bool wxListbook::SetPageImage(size_t n
, int imageId
)
246 return GetListView()->SetItemImage(n
, imageId
);
249 // ----------------------------------------------------------------------------
251 // ----------------------------------------------------------------------------
253 void wxListbook::SetImageList(wxImageList
*imageList
)
255 GetListView()->SetImageList(imageList
, wxIMAGE_LIST_NORMAL
);
257 wxBookCtrlBase::SetImageList(imageList
);
260 // ----------------------------------------------------------------------------
262 // ----------------------------------------------------------------------------
264 void wxListbook::UpdateSelectedPage(size_t newsel
)
266 m_selection
= newsel
;
267 GetListView()->Select(newsel
);
268 GetListView()->Focus(newsel
);
271 int wxListbook::GetSelection() const
276 wxBookCtrlBaseEvent
* wxListbook::CreatePageChangingEvent() const
278 return new wxListbookEvent(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING
, m_windowId
);
281 void wxListbook::MakeChangedEvent(wxBookCtrlBaseEvent
&event
)
283 event
.SetEventType(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED
);
287 // ----------------------------------------------------------------------------
288 // adding/removing the pages
289 // ----------------------------------------------------------------------------
292 wxListbook::InsertPage(size_t n
,
294 const wxString
& text
,
298 if ( !wxBookCtrlBase::InsertPage(n
, page
, text
, bSelect
, imageId
) )
301 GetListView()->InsertItem(n
, text
, imageId
);
303 // if the inserted page is before the selected one, we must update the
304 // index of the selected page
305 if ( int(n
) <= m_selection
)
307 // one extra page added
309 GetListView()->Select(m_selection
);
310 GetListView()->Focus(m_selection
);
313 // some page should be selected: either this one or the first one if there
314 // is still no selection
318 else if ( m_selection
== -1 )
321 if ( selNew
!= m_selection
)
325 SetSelection(selNew
);
327 wxSizeEvent
sz(GetSize(), GetId());
328 GetEventHandler()->ProcessEvent(sz
);
333 wxWindow
*wxListbook::DoRemovePage(size_t page
)
335 const size_t page_count
= GetPageCount();
336 wxWindow
*win
= wxBookCtrlBase::DoRemovePage(page
);
340 GetListView()->DeleteItem(page
);
342 if (m_selection
>= (int)page
)
344 // force new sel valid if possible
345 int sel
= m_selection
- 1;
348 else if ((page_count
== 2) || (sel
== -1))
351 // force sel invalid if deleting current page - don't try to hide it
352 m_selection
= (m_selection
== (int)page
) ? wxNOT_FOUND
: m_selection
- 1;
354 if ((sel
!= wxNOT_FOUND
) && (sel
!= m_selection
))
358 GetListView()->Arrange();
359 if (GetPageCount() == 0)
361 wxSizeEvent
sz(GetSize(), GetId());
362 GetEventHandler()->ProcessEvent(sz
);
370 bool wxListbook::DeleteAllPages()
372 GetListView()->DeleteAllItems();
373 if (!wxBookCtrlBase::DeleteAllPages())
378 wxSizeEvent
sz(GetSize(), GetId());
379 GetEventHandler()->ProcessEvent(sz
);
384 // ----------------------------------------------------------------------------
386 // ----------------------------------------------------------------------------
388 void wxListbook::OnListSelected(wxListEvent
& eventList
)
390 if ( eventList
.GetEventObject() != m_bookctrl
)
396 const int selNew
= eventList
.GetIndex();
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
)
411 GetListView()->Select(m_selection
);
412 GetListView()->Focus(m_selection
);
416 #endif // wxUSE_LISTBOOK