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();
57 const int wxID_LISTBOOKLISTVIEW
= wxNewId();
59 BEGIN_EVENT_TABLE(wxListbook
, wxBookCtrlBase
)
60 EVT_SIZE(wxListbook::OnSize
)
61 EVT_LIST_ITEM_SELECTED(wxID_LISTBOOKLISTVIEW
, wxListbook::OnListSelected
)
64 // ============================================================================
65 // wxListbook implementation
66 // ============================================================================
68 // ----------------------------------------------------------------------------
69 // wxListbook creation
70 // ----------------------------------------------------------------------------
72 void wxListbook::Init()
74 m_selection
= wxNOT_FOUND
;
78 wxListbook::Create(wxWindow
*parent
,
85 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
91 #endif // __WXMAC__/!__WXMAC__
94 // no border for this control, it doesn't look nice together with
96 style
&= ~wxBORDER_MASK
;
97 style
|= wxBORDER_NONE
;
99 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
100 wxDefaultValidator
, name
) )
103 m_bookctrl
= new wxListView
106 wxID_LISTBOOKLISTVIEW
,
109 wxLC_ICON
| wxLC_SINGLE_SEL
|
110 (IsVertical() ? wxLC_ALIGN_LEFT
: wxLC_ALIGN_TOP
)
114 // On XP with themes enabled the GetViewRect used in GetControllerSize() to
115 // determine the space needed for the list view will incorrectly return
116 // (0,0,0,0) the first time. So send a pending event so OnSize will be
117 // called again after the window is ready to go. Technically we don't
118 // need to do this on non-XP windows, but if things are already sized
119 // correctly then nothing changes and so there is no harm.
121 GetEventHandler()->AddPendingEvent(evt
);
126 // ----------------------------------------------------------------------------
127 // wxListbook geometry management
128 // ----------------------------------------------------------------------------
130 wxSize
wxListbook::GetControllerSize() const
132 const wxSize sizeClient
= GetClientSize(),
133 sizeBorder
= m_bookctrl
->GetSize() - m_bookctrl
->GetClientSize(),
134 sizeList
= GetListView()->GetViewRect().GetSize() + sizeBorder
;
140 size
.x
= sizeClient
.x
;
143 else // left/right aligned
146 size
.y
= sizeClient
.y
;
152 void wxListbook::OnSize(wxSizeEvent
& event
)
154 // arrange the icons before calling SetClientSize(), otherwise it wouldn't
155 // account for the scrollbars the list control might need and, at least
156 // under MSW, we'd finish with an ugly looking list control with both
157 // vertical and horizontal scrollbar (with one of them being added because
158 // the other one is not accounted for in client size computations)
159 wxListView
*list
= GetListView();
160 if (list
) list
->Arrange();
161 wxBookCtrlBase::OnSize(event
);
164 int wxListbook::HitTest(const wxPoint
& pt
, long *flags
) const
166 int pagePos
= wxNOT_FOUND
;
169 *flags
= wxBK_HITTEST_NOWHERE
;
171 // convert from listbook control coordinates to list control coordinates
172 const wxListView
* const list
= GetListView();
173 const wxPoint listPt
= list
->ScreenToClient(ClientToScreen(pt
));
175 // is the point inside list control?
176 if ( wxRect(list
->GetSize()).Contains(listPt
) )
179 pagePos
= list
->HitTest(listPt
, flagsList
);
183 if ( pagePos
!= wxNOT_FOUND
)
186 if ( flagsList
& (wxLIST_HITTEST_ONITEMICON
|
187 wxLIST_HITTEST_ONITEMSTATEICON
) )
188 *flags
|= wxBK_HITTEST_ONICON
;
190 if ( flagsList
& wxLIST_HITTEST_ONITEMLABEL
)
191 *flags
|= wxBK_HITTEST_ONLABEL
;
194 else // not over list control at all
196 if ( flags
&& GetPageRect().Contains(pt
) )
197 *flags
|= wxBK_HITTEST_ONPAGE
;
203 wxSize
wxListbook::CalcSizeFromPage(const wxSize
& sizePage
) const
205 // we need to add the size of the list control and the border between
206 const wxSize sizeList
= GetControllerSize();
208 wxSize size
= sizePage
;
211 size
.y
+= sizeList
.y
+ GetInternalBorder();
213 else // left/right aligned
215 size
.x
+= sizeList
.x
+ GetInternalBorder();
222 // ----------------------------------------------------------------------------
223 // accessing the pages
224 // ----------------------------------------------------------------------------
226 bool wxListbook::SetPageText(size_t n
, const wxString
& strText
)
228 GetListView()->SetItemText(n
, strText
);
233 wxString
wxListbook::GetPageText(size_t n
) const
235 return GetListView()->GetItemText(n
);
238 int wxListbook::GetPageImage(size_t WXUNUSED(n
)) const
240 wxFAIL_MSG( _T("wxListbook::GetPageImage() not implemented") );
245 bool wxListbook::SetPageImage(size_t n
, int imageId
)
247 return GetListView()->SetItemImage(n
, imageId
);
250 // ----------------------------------------------------------------------------
252 // ----------------------------------------------------------------------------
254 void wxListbook::SetImageList(wxImageList
*imageList
)
256 GetListView()->SetImageList(imageList
, wxIMAGE_LIST_NORMAL
);
258 wxBookCtrlBase::SetImageList(imageList
);
261 // ----------------------------------------------------------------------------
263 // ----------------------------------------------------------------------------
265 void wxListbook::UpdateSelectedPage(size_t newsel
)
267 m_selection
= newsel
;
268 GetListView()->Select(newsel
);
269 GetListView()->Focus(newsel
);
272 int wxListbook::GetSelection() const
277 wxBookCtrlBaseEvent
* wxListbook::CreatePageChangingEvent() const
279 return new wxListbookEvent(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING
, m_windowId
);
282 void wxListbook::MakeChangedEvent(wxBookCtrlBaseEvent
&event
)
284 event
.SetEventType(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED
);
288 // ----------------------------------------------------------------------------
289 // adding/removing the pages
290 // ----------------------------------------------------------------------------
293 wxListbook::InsertPage(size_t n
,
295 const wxString
& text
,
299 if ( !wxBookCtrlBase::InsertPage(n
, page
, text
, bSelect
, imageId
) )
302 GetListView()->InsertItem(n
, text
, imageId
);
304 // if the inserted page is before the selected one, we must update the
305 // index of the selected page
306 if ( int(n
) <= m_selection
)
308 // one extra page added
310 GetListView()->Select(m_selection
);
311 GetListView()->Focus(m_selection
);
314 // some page should be selected: either this one or the first one if there
315 // is still no selection
319 else if ( m_selection
== -1 )
322 if ( selNew
!= m_selection
)
326 SetSelection(selNew
);
328 InvalidateBestSize();
329 // GetListView()->InvalidateBestSize();
330 GetListView()->Arrange();
332 if (GetPageCount() == 1)
334 wxSizeEvent
sz(GetSize(), GetId());
340 wxWindow
*wxListbook::DoRemovePage(size_t page
)
342 const size_t page_count
= GetPageCount();
343 wxWindow
*win
= wxBookCtrlBase::DoRemovePage(page
);
347 GetListView()->DeleteItem(page
);
349 if (m_selection
>= (int)page
)
351 // force new sel valid if possible
352 int sel
= m_selection
- 1;
355 else if ((page_count
== 2) || (sel
== -1))
358 // force sel invalid if deleting current page - don't try to hide it
359 m_selection
= (m_selection
== (int)page
) ? wxNOT_FOUND
: m_selection
- 1;
361 if ((sel
!= wxNOT_FOUND
) && (sel
!= m_selection
))
365 GetListView()->Arrange();
366 if (GetPageCount() == 0)
368 wxSizeEvent
sz(GetSize(), GetId());
377 bool wxListbook::DeleteAllPages()
379 GetListView()->DeleteAllItems();
380 if (!wxBookCtrlBase::DeleteAllPages())
385 wxSizeEvent
sz(GetSize(), GetId());
391 // ----------------------------------------------------------------------------
393 // ----------------------------------------------------------------------------
395 void wxListbook::OnListSelected(wxListEvent
& eventList
)
397 const int selNew
= eventList
.GetIndex();
399 if ( selNew
== m_selection
)
401 // this event can only come from our own Select(m_selection) below
402 // which we call when the page change is vetoed, so we should simply
407 SetSelection(selNew
);
409 // change wasn't allowed, return to previous state
410 if (m_selection
!= selNew
)
412 GetListView()->Select(m_selection
);
413 GetListView()->Focus(m_selection
);
417 #endif // wxUSE_LISTBOOK