1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/listbkg.cpp
3 // Purpose: generic implementation of wxListbook
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
28 #include "wx/listbook.h"
31 #include "wx/settings.h"
34 #include "wx/listctrl.h"
35 #include "wx/statline.h"
36 #include "wx/imaglist.h"
38 // ----------------------------------------------------------------------------
39 // various wxWidgets macros
40 // ----------------------------------------------------------------------------
42 // check that the page index is valid
43 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 IMPLEMENT_DYNAMIC_CLASS(wxListbook
, wxBookCtrlBase
)
51 wxDEFINE_EVENT( wxEVT_LISTBOOK_PAGE_CHANGING
, wxBookCtrlEvent
);
52 wxDEFINE_EVENT( wxEVT_LISTBOOK_PAGE_CHANGED
, wxBookCtrlEvent
);
54 BEGIN_EVENT_TABLE(wxListbook
, wxBookCtrlBase
)
55 EVT_SIZE(wxListbook::OnSize
)
56 EVT_LIST_ITEM_SELECTED(wxID_ANY
, wxListbook::OnListSelected
)
59 // ============================================================================
60 // wxListbook implementation
61 // ============================================================================
63 // ----------------------------------------------------------------------------
64 // wxListbook creation
65 // ----------------------------------------------------------------------------
68 wxListbook::Create(wxWindow
*parent
,
75 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
81 #endif // __WXMAC__/!__WXMAC__
84 // no border for this control, it doesn't look nice together with
86 style
&= ~wxBORDER_MASK
;
87 style
|= wxBORDER_NONE
;
89 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
90 wxDefaultValidator
, name
) )
93 m_bookctrl
= new wxListView
102 if ( GetListView()->InReportView() )
103 GetListView()->InsertColumn(0, wxS("Pages"));
106 // On XP with themes enabled the GetViewRect used in GetControllerSize() to
107 // determine the space needed for the list view will incorrectly return
108 // (0,0,0,0) the first time. So send a pending event so OnSize will be
109 // called again after the window is ready to go. Technically we don't
110 // need to do this on non-XP windows, but if things are already sized
111 // correctly then nothing changes and so there is no harm.
113 GetEventHandler()->AddPendingEvent(evt
);
118 // ----------------------------------------------------------------------------
120 // ----------------------------------------------------------------------------
122 long wxListbook::GetListCtrlFlags() const
124 // We'd like to always use wxLC_ICON mode but it doesn't work with the
125 // native wxListCtrl under MSW unless we do have icons for all the items,
126 // so we can't use it if we have no image list. In this case we'd like to
127 // use wxLC_LIST mode because it works correctly for both horizontally and
128 // vertically laid out controls, but MSW native wxListCtrl insists on
129 // creating multiple columns if there are too many items and there doesn't
130 // seem anything to do about it, so we have to use wxLC_REPORT mode in this
133 long flags
= IsVertical() ? wxLC_ALIGN_LEFT
: wxLC_ALIGN_TOP
;
134 if ( GetImageList() )
143 // Notice that we intentionally overwrite the alignment flags here
144 // by not using "|=", alignment isn't used for report view.
145 flags
= wxLC_REPORT
| wxLC_NO_HEADER
;
154 // Use single selection in any case.
155 return flags
| wxLC_SINGLE_SEL
;
158 // ----------------------------------------------------------------------------
159 // wxListbook geometry management
160 // ----------------------------------------------------------------------------
162 void wxListbook::OnSize(wxSizeEvent
& event
)
164 // arrange the icons before calling SetClientSize(), otherwise it wouldn't
165 // account for the scrollbars the list control might need and, at least
166 // under MSW, we'd finish with an ugly looking list control with both
167 // vertical and horizontal scrollbar (with one of them being added because
168 // the other one is not accounted for in client size computations)
169 wxListView
* const list
= GetListView();
176 int wxListbook::HitTest(const wxPoint
& pt
, long *flags
) const
178 int pagePos
= wxNOT_FOUND
;
181 *flags
= wxBK_HITTEST_NOWHERE
;
183 // convert from listbook control coordinates to list control coordinates
184 const wxListView
* const list
= GetListView();
185 const wxPoint listPt
= list
->ScreenToClient(ClientToScreen(pt
));
187 // is the point inside list control?
188 if ( wxRect(list
->GetSize()).Contains(listPt
) )
191 pagePos
= list
->HitTest(listPt
, flagsList
);
195 if ( pagePos
!= wxNOT_FOUND
)
198 if ( flagsList
& (wxLIST_HITTEST_ONITEMICON
|
199 wxLIST_HITTEST_ONITEMSTATEICON
) )
200 *flags
|= wxBK_HITTEST_ONICON
;
202 if ( flagsList
& wxLIST_HITTEST_ONITEMLABEL
)
203 *flags
|= wxBK_HITTEST_ONLABEL
;
206 else // not over list control at all
208 if ( flags
&& GetPageRect().Contains(pt
) )
209 *flags
|= wxBK_HITTEST_ONPAGE
;
215 void wxListbook::UpdateSize()
217 // we should find a more elegant way to force a layout than generating this
219 wxSizeEvent
sz(GetSize(), GetId());
220 GetEventHandler()->ProcessEvent(sz
);
223 // ----------------------------------------------------------------------------
224 // accessing the pages
225 // ----------------------------------------------------------------------------
227 bool wxListbook::SetPageText(size_t n
, const wxString
& strText
)
229 GetListView()->SetItemText(n
, strText
);
234 wxString
wxListbook::GetPageText(size_t n
) const
236 return GetListView()->GetItemText(n
);
239 int wxListbook::GetPageImage(size_t n
) const
244 if (GetListView()->GetItem(item
))
246 return item
.GetImage();
254 bool wxListbook::SetPageImage(size_t n
, int imageId
)
256 return GetListView()->SetItemImage(n
, imageId
);
259 // ----------------------------------------------------------------------------
261 // ----------------------------------------------------------------------------
263 void wxListbook::SetImageList(wxImageList
*imageList
)
265 const long flagsOld
= GetListCtrlFlags();
267 wxBookCtrlBase::SetImageList(imageList
);
269 const long flagsNew
= GetListCtrlFlags();
271 wxListView
* const list
= GetListView();
273 // We may need to change the list control mode if the image list presence
275 if ( flagsNew
!= flagsOld
)
277 // Preserve the selection which is lost when changing the mode
278 const int oldSel
= GetSelection();
280 list
->SetWindowStyleFlag(flagsNew
);
281 if ( list
->InReportView() )
282 list
->InsertColumn(0, wxS("Pages"));
285 if ( oldSel
!= wxNOT_FOUND
)
286 SetSelection(oldSel
);
289 list
->SetImageList(imageList
, wxIMAGE_LIST_NORMAL
);
292 // ----------------------------------------------------------------------------
294 // ----------------------------------------------------------------------------
296 void wxListbook::UpdateSelectedPage(size_t newsel
)
298 m_selection
= newsel
;
299 GetListView()->Select(newsel
);
300 GetListView()->Focus(newsel
);
303 wxBookCtrlEvent
* wxListbook::CreatePageChangingEvent() const
305 return new wxBookCtrlEvent(wxEVT_LISTBOOK_PAGE_CHANGING
, m_windowId
);
308 void wxListbook::MakeChangedEvent(wxBookCtrlEvent
&event
)
310 event
.SetEventType(wxEVT_LISTBOOK_PAGE_CHANGED
);
314 // ----------------------------------------------------------------------------
315 // adding/removing the pages
316 // ----------------------------------------------------------------------------
319 wxListbook::InsertPage(size_t n
,
321 const wxString
& text
,
325 if ( !wxBookCtrlBase::InsertPage(n
, page
, text
, bSelect
, imageId
) )
328 GetListView()->InsertItem(n
, text
, imageId
);
330 // if the inserted page is before the selected one, we must update the
331 // index of the selected page
332 if ( int(n
) <= m_selection
)
334 // one extra page added
336 GetListView()->Select(m_selection
);
337 GetListView()->Focus(m_selection
);
340 if ( !DoSetSelectionAfterInsertion(n
, bSelect
) )
348 wxWindow
*wxListbook::DoRemovePage(size_t page
)
350 wxWindow
*win
= wxBookCtrlBase::DoRemovePage(page
);
354 GetListView()->DeleteItem(page
);
356 DoSetSelectionAfterRemoval(page
);
358 GetListView()->Arrange();
366 bool wxListbook::DeleteAllPages()
368 GetListView()->DeleteAllItems();
369 if (!wxBookCtrlBase::DeleteAllPages())
377 // ----------------------------------------------------------------------------
379 // ----------------------------------------------------------------------------
381 void wxListbook::OnListSelected(wxListEvent
& eventList
)
383 if ( eventList
.GetEventObject() != m_bookctrl
)
389 const int selNew
= eventList
.GetIndex();
391 if ( selNew
== m_selection
)
393 // this event can only come from our own Select(m_selection) below
394 // which we call when the page change is vetoed, so we should simply
399 SetSelection(selNew
);
401 // change wasn't allowed, return to previous state
402 if (m_selection
!= selNew
)
404 GetListView()->Select(m_selection
);
405 GetListView()->Focus(m_selection
);
409 #endif // wxUSE_LISTBOOK