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 #include "wx/sysopt.h"
44 // FIXME: This function exists because native OS X wxListCtrl seems to have
45 // problems with report view, either imperfect display or reported hanging, so
46 // disable it for now (but it should be fixed, and this function removed).
47 bool CanUseReportView()
49 #if defined(__WXMAC__) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON
50 if (wxSystemOptions::GetOptionInt(wxMAC_ALWAYS_USE_GENERIC_LISTCTRL
) == 0)
57 } // anonymous namespace
59 // ----------------------------------------------------------------------------
60 // various wxWidgets macros
61 // ----------------------------------------------------------------------------
63 // check that the page index is valid
64 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 IMPLEMENT_DYNAMIC_CLASS(wxListbook
, wxBookCtrlBase
)
72 wxDEFINE_EVENT( wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING
, wxBookCtrlEvent
);
73 wxDEFINE_EVENT( wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED
, wxBookCtrlEvent
);
75 BEGIN_EVENT_TABLE(wxListbook
, wxBookCtrlBase
)
76 EVT_SIZE(wxListbook::OnSize
)
77 EVT_LIST_ITEM_SELECTED(wxID_ANY
, wxListbook::OnListSelected
)
80 // ============================================================================
81 // wxListbook implementation
82 // ============================================================================
84 // ----------------------------------------------------------------------------
85 // wxListbook creation
86 // ----------------------------------------------------------------------------
88 void wxListbook::Init()
90 m_selection
= wxNOT_FOUND
;
94 wxListbook::Create(wxWindow
*parent
,
101 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
107 #endif // __WXMAC__/!__WXMAC__
110 // no border for this control, it doesn't look nice together with
112 style
&= ~wxBORDER_MASK
;
113 style
|= wxBORDER_NONE
;
115 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
116 wxDefaultValidator
, name
) )
119 m_bookctrl
= new wxListView
126 (CanUseReportView() ? GetListCtrlReportViewFlags()
127 : GetListCtrlIconViewFlags())
130 if ( CanUseReportView() )
131 GetListView()->InsertColumn(0, wxT("Pages"));
134 // On XP with themes enabled the GetViewRect used in GetControllerSize() to
135 // determine the space needed for the list view will incorrectly return
136 // (0,0,0,0) the first time. So send a pending event so OnSize will be
137 // called again after the window is ready to go. Technically we don't
138 // need to do this on non-XP windows, but if things are already sized
139 // correctly then nothing changes and so there is no harm.
141 GetEventHandler()->AddPendingEvent(evt
);
146 // ----------------------------------------------------------------------------
148 // ----------------------------------------------------------------------------
150 long wxListbook::GetListCtrlIconViewFlags() const
152 return (IsVertical() ? wxLC_ALIGN_LEFT
: wxLC_ALIGN_TOP
) | wxLC_ICON
;
155 long wxListbook::GetListCtrlReportViewFlags() const
157 return wxLC_REPORT
| wxLC_NO_HEADER
;
160 // ----------------------------------------------------------------------------
161 // wxListbook geometry management
162 // ----------------------------------------------------------------------------
164 void wxListbook::OnSize(wxSizeEvent
& event
)
166 // arrange the icons before calling SetClientSize(), otherwise it wouldn't
167 // account for the scrollbars the list control might need and, at least
168 // under MSW, we'd finish with an ugly looking list control with both
169 // vertical and horizontal scrollbar (with one of them being added because
170 // the other one is not accounted for in client size computations)
171 wxListView
* const list
= GetListView();
178 int wxListbook::HitTest(const wxPoint
& pt
, long *flags
) const
180 int pagePos
= wxNOT_FOUND
;
183 *flags
= wxBK_HITTEST_NOWHERE
;
185 // convert from listbook control coordinates to list control coordinates
186 const wxListView
* const list
= GetListView();
187 const wxPoint listPt
= list
->ScreenToClient(ClientToScreen(pt
));
189 // is the point inside list control?
190 if ( wxRect(list
->GetSize()).Contains(listPt
) )
193 pagePos
= list
->HitTest(listPt
, flagsList
);
197 if ( pagePos
!= wxNOT_FOUND
)
200 if ( flagsList
& (wxLIST_HITTEST_ONITEMICON
|
201 wxLIST_HITTEST_ONITEMSTATEICON
) )
202 *flags
|= wxBK_HITTEST_ONICON
;
204 if ( flagsList
& wxLIST_HITTEST_ONITEMLABEL
)
205 *flags
|= wxBK_HITTEST_ONLABEL
;
208 else // not over list control at all
210 if ( flags
&& GetPageRect().Contains(pt
) )
211 *flags
|= wxBK_HITTEST_ONPAGE
;
217 void wxListbook::UpdateSize()
219 // we should find a more elegant way to force a layout than generating this
221 wxSizeEvent
sz(GetSize(), GetId());
222 GetEventHandler()->ProcessEvent(sz
);
225 // ----------------------------------------------------------------------------
226 // accessing the pages
227 // ----------------------------------------------------------------------------
229 bool wxListbook::SetPageText(size_t n
, const wxString
& strText
)
231 GetListView()->SetItemText(n
, strText
);
236 wxString
wxListbook::GetPageText(size_t n
) const
238 return GetListView()->GetItemText(n
);
241 int wxListbook::GetPageImage(size_t n
) const
246 if (GetListView()->GetItem(item
))
248 return item
.GetImage();
256 bool wxListbook::SetPageImage(size_t n
, int imageId
)
258 return GetListView()->SetItemImage(n
, imageId
);
261 // ----------------------------------------------------------------------------
263 // ----------------------------------------------------------------------------
265 void wxListbook::SetImageList(wxImageList
*imageList
)
267 if ( CanUseReportView() )
269 wxListView
* const list
= GetListView();
271 // If imageList presence has changed, we update the list control view
272 if ( (imageList
!= NULL
) != (GetImageList() != NULL
) )
274 // Preserve the selection which is lost when changing the mode
275 const int oldSel
= GetSelection();
277 // Update the style to use icon view for images, report view otherwise
278 long style
= wxLC_SINGLE_SEL
;
281 style
|= GetListCtrlIconViewFlags();
283 else // no image list
285 style
|= GetListCtrlReportViewFlags();
288 list
->SetWindowStyleFlag(style
);
290 list
->InsertColumn(0, wxT("Pages"));
293 if ( oldSel
!= wxNOT_FOUND
)
294 SetSelection(oldSel
);
297 list
->SetImageList(imageList
, wxIMAGE_LIST_NORMAL
);
300 wxBookCtrlBase::SetImageList(imageList
);
303 // ----------------------------------------------------------------------------
305 // ----------------------------------------------------------------------------
307 void wxListbook::UpdateSelectedPage(size_t newsel
)
309 m_selection
= newsel
;
310 GetListView()->Select(newsel
);
311 GetListView()->Focus(newsel
);
314 int wxListbook::GetSelection() const
319 wxBookCtrlEvent
* wxListbook::CreatePageChangingEvent() const
321 return new wxBookCtrlEvent(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING
, m_windowId
);
324 void wxListbook::MakeChangedEvent(wxBookCtrlEvent
&event
)
326 event
.SetEventType(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED
);
330 // ----------------------------------------------------------------------------
331 // adding/removing the pages
332 // ----------------------------------------------------------------------------
335 wxListbook::InsertPage(size_t n
,
337 const wxString
& text
,
341 if ( !wxBookCtrlBase::InsertPage(n
, page
, text
, bSelect
, imageId
) )
344 GetListView()->InsertItem(n
, text
, imageId
);
346 // if the inserted page is before the selected one, we must update the
347 // index of the selected page
348 if ( int(n
) <= m_selection
)
350 // one extra page added
352 GetListView()->Select(m_selection
);
353 GetListView()->Focus(m_selection
);
356 // some page should be selected: either this one or the first one if there
357 // is still no selection
361 else if ( m_selection
== -1 )
364 if ( selNew
!= m_selection
)
368 SetSelection(selNew
);
375 wxWindow
*wxListbook::DoRemovePage(size_t page
)
377 const size_t page_count
= GetPageCount();
378 wxWindow
*win
= wxBookCtrlBase::DoRemovePage(page
);
382 GetListView()->DeleteItem(page
);
384 if (m_selection
>= (int)page
)
386 // force new sel valid if possible
387 int sel
= m_selection
- 1;
390 else if ((page_count
== 2) || (sel
== -1))
393 // force sel invalid if deleting current page - don't try to hide it
394 m_selection
= (m_selection
== (int)page
) ? wxNOT_FOUND
: m_selection
- 1;
396 if ((sel
!= wxNOT_FOUND
) && (sel
!= m_selection
))
400 GetListView()->Arrange();
408 bool wxListbook::DeleteAllPages()
410 GetListView()->DeleteAllItems();
411 if (!wxBookCtrlBase::DeleteAllPages())
421 // ----------------------------------------------------------------------------
423 // ----------------------------------------------------------------------------
425 void wxListbook::OnListSelected(wxListEvent
& eventList
)
427 if ( eventList
.GetEventObject() != m_bookctrl
)
433 const int selNew
= eventList
.GetIndex();
435 if ( selNew
== m_selection
)
437 // this event can only come from our own Select(m_selection) below
438 // which we call when the page change is vetoed, so we should simply
443 SetSelection(selNew
);
445 // change wasn't allowed, return to previous state
446 if (m_selection
!= selNew
)
448 GetListView()->Select(m_selection
);
449 GetListView()->Focus(m_selection
);
453 #endif // wxUSE_LISTBOOK