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 const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING
= wxNewEventType();
54 const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED
= wxNewEventType();
56 BEGIN_EVENT_TABLE(wxListbook
, wxBookCtrlBase
)
57 EVT_SIZE(wxListbook::OnSize
)
58 EVT_LIST_ITEM_SELECTED(wxID_ANY
, wxListbook::OnListSelected
)
61 // ============================================================================
62 // wxListbook implementation
63 // ============================================================================
65 // ----------------------------------------------------------------------------
66 // wxListbook creation
67 // ----------------------------------------------------------------------------
69 void wxListbook::Init()
71 m_selection
= wxNOT_FOUND
;
75 wxListbook::Create(wxWindow
*parent
,
82 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
88 #endif // __WXMAC__/!__WXMAC__
91 // no border for this control, it doesn't look nice together with
93 style
&= ~wxBORDER_MASK
;
94 style
|= wxBORDER_NONE
;
96 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
97 wxDefaultValidator
, name
) )
100 m_bookctrl
= new wxListView
106 wxLC_ICON
| wxLC_SINGLE_SEL
|
107 (IsVertical() ? wxLC_ALIGN_LEFT
: wxLC_ALIGN_TOP
)
111 // On XP with themes enabled the GetViewRect used in GetControllerSize() to
112 // determine the space needed for the list view will incorrectly return
113 // (0,0,0,0) the first time. So send a pending event so OnSize will be
114 // called again after the window is ready to go. Technically we don't
115 // need to do this on non-XP windows, but if things are already sized
116 // correctly then nothing changes and so there is no harm.
118 GetEventHandler()->AddPendingEvent(evt
);
123 // ----------------------------------------------------------------------------
124 // wxListbook geometry management
125 // ----------------------------------------------------------------------------
127 wxSize
wxListbook::GetControllerSize() const
129 const wxSize sizeClient
= GetClientSize(),
130 sizeBorder
= m_bookctrl
->GetSize() - m_bookctrl
->GetClientSize(),
131 sizeList
= GetListView()->GetViewRect().GetSize() + sizeBorder
;
137 size
.x
= sizeClient
.x
;
140 else // left/right aligned
143 size
.y
= sizeClient
.y
;
149 void wxListbook::OnSize(wxSizeEvent
& event
)
151 // arrange the icons before calling SetClientSize(), otherwise it wouldn't
152 // account for the scrollbars the list control might need and, at least
153 // under MSW, we'd finish with an ugly looking list control with both
154 // vertical and horizontal scrollbar (with one of them being added because
155 // the other one is not accounted for in client size computations)
156 wxListView
*list
= GetListView();
157 if (list
) list
->Arrange();
158 wxBookCtrlBase::OnSize(event
);
161 int wxListbook::HitTest(const wxPoint
& pt
, long *flags
) const
163 int pagePos
= wxNOT_FOUND
;
166 *flags
= wxBK_HITTEST_NOWHERE
;
168 // convert from listbook control coordinates to list control coordinates
169 const wxListView
* const list
= GetListView();
170 const wxPoint listPt
= list
->ScreenToClient(ClientToScreen(pt
));
172 // is the point inside list control?
173 if ( wxRect(list
->GetSize()).Contains(listPt
) )
176 pagePos
= list
->HitTest(listPt
, flagsList
);
180 if ( pagePos
!= wxNOT_FOUND
)
183 if ( flagsList
& (wxLIST_HITTEST_ONITEMICON
|
184 wxLIST_HITTEST_ONITEMSTATEICON
) )
185 *flags
|= wxBK_HITTEST_ONICON
;
187 if ( flagsList
& wxLIST_HITTEST_ONITEMLABEL
)
188 *flags
|= wxBK_HITTEST_ONLABEL
;
191 else // not over list control at all
193 if ( flags
&& GetPageRect().Contains(pt
) )
194 *flags
|= wxBK_HITTEST_ONPAGE
;
200 wxSize
wxListbook::CalcSizeFromPage(const wxSize
& sizePage
) const
202 // we need to add the size of the list control and the border between
203 const wxSize sizeList
= GetControllerSize();
205 wxSize size
= sizePage
;
208 size
.y
+= sizeList
.y
+ GetInternalBorder();
210 else // left/right aligned
212 size
.x
+= sizeList
.x
+ GetInternalBorder();
219 // ----------------------------------------------------------------------------
220 // accessing the pages
221 // ----------------------------------------------------------------------------
223 bool wxListbook::SetPageText(size_t n
, const wxString
& strText
)
225 GetListView()->SetItemText(n
, strText
);
230 wxString
wxListbook::GetPageText(size_t n
) const
232 return GetListView()->GetItemText(n
);
235 int wxListbook::GetPageImage(size_t WXUNUSED(n
)) const
237 wxFAIL_MSG( _T("wxListbook::GetPageImage() not implemented") );
242 bool wxListbook::SetPageImage(size_t n
, int imageId
)
244 return GetListView()->SetItemImage(n
, imageId
);
247 // ----------------------------------------------------------------------------
249 // ----------------------------------------------------------------------------
251 void wxListbook::SetImageList(wxImageList
*imageList
)
253 GetListView()->SetImageList(imageList
, wxIMAGE_LIST_NORMAL
);
255 wxBookCtrlBase::SetImageList(imageList
);
258 // ----------------------------------------------------------------------------
260 // ----------------------------------------------------------------------------
262 void wxListbook::UpdateSelectedPage(size_t newsel
)
264 m_selection
= newsel
;
265 GetListView()->Select(newsel
);
266 GetListView()->Focus(newsel
);
269 int wxListbook::GetSelection() const
274 wxBookCtrlBaseEvent
* wxListbook::CreatePageChangingEvent() const
276 return new wxListbookEvent(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING
, m_windowId
);
279 void wxListbook::MakeChangedEvent(wxBookCtrlBaseEvent
&event
)
281 event
.SetEventType(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED
);
285 // ----------------------------------------------------------------------------
286 // adding/removing the pages
287 // ----------------------------------------------------------------------------
290 wxListbook::InsertPage(size_t n
,
292 const wxString
& text
,
296 if ( !wxBookCtrlBase::InsertPage(n
, page
, text
, bSelect
, imageId
) )
299 GetListView()->InsertItem(n
, text
, imageId
);
301 // if the inserted page is before the selected one, we must update the
302 // index of the selected page
303 if ( int(n
) <= m_selection
)
305 // one extra page added
307 GetListView()->Select(m_selection
);
308 GetListView()->Focus(m_selection
);
311 // some page should be selected: either this one or the first one if there
312 // is still no selection
316 else if ( m_selection
== -1 )
319 if ( selNew
!= m_selection
)
323 SetSelection(selNew
);
325 wxSizeEvent
sz(GetSize(), GetId());
326 GetEventHandler()->ProcessEvent(sz
);
331 wxWindow
*wxListbook::DoRemovePage(size_t page
)
333 const size_t page_count
= GetPageCount();
334 wxWindow
*win
= wxBookCtrlBase::DoRemovePage(page
);
338 GetListView()->DeleteItem(page
);
340 if (m_selection
>= (int)page
)
342 // force new sel valid if possible
343 int sel
= m_selection
- 1;
346 else if ((page_count
== 2) || (sel
== -1))
349 // force sel invalid if deleting current page - don't try to hide it
350 m_selection
= (m_selection
== (int)page
) ? wxNOT_FOUND
: m_selection
- 1;
352 if ((sel
!= wxNOT_FOUND
) && (sel
!= m_selection
))
356 GetListView()->Arrange();
357 if (GetPageCount() == 0)
359 wxSizeEvent
sz(GetSize(), GetId());
360 GetEventHandler()->ProcessEvent(sz
);
368 bool wxListbook::DeleteAllPages()
370 GetListView()->DeleteAllItems();
371 if (!wxBookCtrlBase::DeleteAllPages())
376 wxSizeEvent
sz(GetSize(), GetId());
377 GetEventHandler()->ProcessEvent(sz
);
382 // ----------------------------------------------------------------------------
384 // ----------------------------------------------------------------------------
386 void wxListbook::OnListSelected(wxListEvent
& eventList
)
388 if ( eventList
.GetEventObject() != m_bookctrl
)
394 const int selNew
= eventList
.GetIndex();
396 if ( selNew
== m_selection
)
398 // this event can only come from our own Select(m_selection) below
399 // which we call when the page change is vetoed, so we should simply
404 SetSelection(selNew
);
406 // change wasn't allowed, return to previous state
407 if (m_selection
!= selNew
)
409 GetListView()->Select(m_selection
);
410 GetListView()->Focus(m_selection
);
414 #endif // wxUSE_LISTBOOK