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 // ----------------------------------------------------------------------------
89 wxListbook::Create(wxWindow
*parent
,
96 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
102 #endif // __WXMAC__/!__WXMAC__
105 // no border for this control, it doesn't look nice together with
107 style
&= ~wxBORDER_MASK
;
108 style
|= wxBORDER_NONE
;
110 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
111 wxDefaultValidator
, name
) )
114 m_bookctrl
= new wxListView
121 (CanUseReportView() ? GetListCtrlReportViewFlags()
122 : GetListCtrlIconViewFlags())
125 if ( CanUseReportView() )
126 GetListView()->InsertColumn(0, wxT("Pages"));
129 // On XP with themes enabled the GetViewRect used in GetControllerSize() to
130 // determine the space needed for the list view will incorrectly return
131 // (0,0,0,0) the first time. So send a pending event so OnSize will be
132 // called again after the window is ready to go. Technically we don't
133 // need to do this on non-XP windows, but if things are already sized
134 // correctly then nothing changes and so there is no harm.
136 GetEventHandler()->AddPendingEvent(evt
);
141 // ----------------------------------------------------------------------------
143 // ----------------------------------------------------------------------------
145 long wxListbook::GetListCtrlIconViewFlags() const
147 return (IsVertical() ? wxLC_ALIGN_LEFT
: wxLC_ALIGN_TOP
) | wxLC_ICON
;
150 long wxListbook::GetListCtrlReportViewFlags() const
152 return wxLC_REPORT
| wxLC_NO_HEADER
;
155 // ----------------------------------------------------------------------------
156 // wxListbook geometry management
157 // ----------------------------------------------------------------------------
159 void wxListbook::OnSize(wxSizeEvent
& event
)
161 // arrange the icons before calling SetClientSize(), otherwise it wouldn't
162 // account for the scrollbars the list control might need and, at least
163 // under MSW, we'd finish with an ugly looking list control with both
164 // vertical and horizontal scrollbar (with one of them being added because
165 // the other one is not accounted for in client size computations)
166 wxListView
* const list
= GetListView();
173 int wxListbook::HitTest(const wxPoint
& pt
, long *flags
) const
175 int pagePos
= wxNOT_FOUND
;
178 *flags
= wxBK_HITTEST_NOWHERE
;
180 // convert from listbook control coordinates to list control coordinates
181 const wxListView
* const list
= GetListView();
182 const wxPoint listPt
= list
->ScreenToClient(ClientToScreen(pt
));
184 // is the point inside list control?
185 if ( wxRect(list
->GetSize()).Contains(listPt
) )
188 pagePos
= list
->HitTest(listPt
, flagsList
);
192 if ( pagePos
!= wxNOT_FOUND
)
195 if ( flagsList
& (wxLIST_HITTEST_ONITEMICON
|
196 wxLIST_HITTEST_ONITEMSTATEICON
) )
197 *flags
|= wxBK_HITTEST_ONICON
;
199 if ( flagsList
& wxLIST_HITTEST_ONITEMLABEL
)
200 *flags
|= wxBK_HITTEST_ONLABEL
;
203 else // not over list control at all
205 if ( flags
&& GetPageRect().Contains(pt
) )
206 *flags
|= wxBK_HITTEST_ONPAGE
;
212 void wxListbook::UpdateSize()
214 // we should find a more elegant way to force a layout than generating this
216 wxSizeEvent
sz(GetSize(), GetId());
217 GetEventHandler()->ProcessEvent(sz
);
220 // ----------------------------------------------------------------------------
221 // accessing the pages
222 // ----------------------------------------------------------------------------
224 bool wxListbook::SetPageText(size_t n
, const wxString
& strText
)
226 GetListView()->SetItemText(n
, strText
);
231 wxString
wxListbook::GetPageText(size_t n
) const
233 return GetListView()->GetItemText(n
);
236 int wxListbook::GetPageImage(size_t n
) const
241 if (GetListView()->GetItem(item
))
243 return item
.GetImage();
251 bool wxListbook::SetPageImage(size_t n
, int imageId
)
253 return GetListView()->SetItemImage(n
, imageId
);
256 // ----------------------------------------------------------------------------
258 // ----------------------------------------------------------------------------
260 void wxListbook::SetImageList(wxImageList
*imageList
)
262 if ( CanUseReportView() )
264 wxListView
* const list
= GetListView();
266 // If imageList presence has changed, we update the list control view
267 if ( (imageList
!= NULL
) != (GetImageList() != NULL
) )
269 // Preserve the selection which is lost when changing the mode
270 const int oldSel
= GetSelection();
272 // Update the style to use icon view for images, report view otherwise
273 long style
= wxLC_SINGLE_SEL
;
276 style
|= GetListCtrlIconViewFlags();
278 else // no image list
280 style
|= GetListCtrlReportViewFlags();
283 list
->SetWindowStyleFlag(style
);
285 list
->InsertColumn(0, wxT("Pages"));
288 if ( oldSel
!= wxNOT_FOUND
)
289 SetSelection(oldSel
);
292 list
->SetImageList(imageList
, wxIMAGE_LIST_NORMAL
);
295 wxBookCtrlBase::SetImageList(imageList
);
298 // ----------------------------------------------------------------------------
300 // ----------------------------------------------------------------------------
302 void wxListbook::UpdateSelectedPage(size_t newsel
)
304 m_selection
= newsel
;
305 GetListView()->Select(newsel
);
306 GetListView()->Focus(newsel
);
309 wxBookCtrlEvent
* wxListbook::CreatePageChangingEvent() const
311 return new wxBookCtrlEvent(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING
, m_windowId
);
314 void wxListbook::MakeChangedEvent(wxBookCtrlEvent
&event
)
316 event
.SetEventType(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED
);
320 // ----------------------------------------------------------------------------
321 // adding/removing the pages
322 // ----------------------------------------------------------------------------
325 wxListbook::InsertPage(size_t n
,
327 const wxString
& text
,
331 if ( !wxBookCtrlBase::InsertPage(n
, page
, text
, bSelect
, imageId
) )
334 GetListView()->InsertItem(n
, text
, imageId
);
336 // if the inserted page is before the selected one, we must update the
337 // index of the selected page
338 if ( int(n
) <= m_selection
)
340 // one extra page added
342 GetListView()->Select(m_selection
);
343 GetListView()->Focus(m_selection
);
346 if ( !DoSetSelectionAfterInsertion(n
, bSelect
) )
354 wxWindow
*wxListbook::DoRemovePage(size_t page
)
356 const size_t page_count
= GetPageCount();
357 wxWindow
*win
= wxBookCtrlBase::DoRemovePage(page
);
361 GetListView()->DeleteItem(page
);
363 if (m_selection
>= (int)page
)
365 // force new sel valid if possible
366 int sel
= m_selection
- 1;
369 else if ((page_count
== 2) || (sel
== wxNOT_FOUND
))
372 // force sel invalid if deleting current page - don't try to hide it
373 m_selection
= (m_selection
== (int)page
) ? wxNOT_FOUND
: m_selection
- 1;
375 if ((sel
!= wxNOT_FOUND
) && (sel
!= m_selection
))
379 GetListView()->Arrange();
387 bool wxListbook::DeleteAllPages()
389 GetListView()->DeleteAllItems();
390 if (!wxBookCtrlBase::DeleteAllPages())
398 // ----------------------------------------------------------------------------
400 // ----------------------------------------------------------------------------
402 void wxListbook::OnListSelected(wxListEvent
& eventList
)
404 if ( eventList
.GetEventObject() != m_bookctrl
)
410 const int selNew
= eventList
.GetIndex();
412 if ( selNew
== m_selection
)
414 // this event can only come from our own Select(m_selection) below
415 // which we call when the page change is vetoed, so we should simply
420 SetSelection(selNew
);
422 // change wasn't allowed, return to previous state
423 if (m_selection
!= selNew
)
425 GetListView()->Select(m_selection
);
426 GetListView()->Focus(m_selection
);
430 #endif // wxUSE_LISTBOOK