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
)
52 wxDEFINE_EVENT( wxEVT_LISTBOOK_PAGE_CHANGING
, wxBookCtrlEvent
);
53 wxDEFINE_EVENT( wxEVT_LISTBOOK_PAGE_CHANGED
, wxBookCtrlEvent
);
55 BEGIN_EVENT_TABLE(wxListbook
, wxBookCtrlBase
)
56 EVT_SIZE(wxListbook::OnSize
)
57 EVT_LIST_ITEM_SELECTED(wxID_ANY
, wxListbook::OnListSelected
)
60 // ============================================================================
61 // wxListbook implementation
62 // ============================================================================
64 // ----------------------------------------------------------------------------
65 // wxListbook creation
66 // ----------------------------------------------------------------------------
69 wxListbook::Create(wxWindow
*parent
,
76 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
82 #endif // __WXMAC__/!__WXMAC__
85 // no border for this control, it doesn't look nice together with
87 style
&= ~wxBORDER_MASK
;
88 style
|= wxBORDER_NONE
;
90 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
91 wxDefaultValidator
, name
) )
94 m_bookctrl
= new wxListView
103 if ( GetListView()->InReportView() )
104 GetListView()->InsertColumn(0, wxS("Pages"));
107 // On XP with themes enabled the GetViewRect used in GetControllerSize() to
108 // determine the space needed for the list view will incorrectly return
109 // (0,0,0,0) the first time. So send a pending event so OnSize will be
110 // called again after the window is ready to go. Technically we don't
111 // need to do this on non-XP windows, but if things are already sized
112 // correctly then nothing changes and so there is no harm.
114 GetEventHandler()->AddPendingEvent(evt
);
119 // ----------------------------------------------------------------------------
121 // ----------------------------------------------------------------------------
123 long wxListbook::GetListCtrlFlags() const
125 // We'd like to always use wxLC_ICON mode but it doesn't work with the
126 // native wxListCtrl under MSW unless we do have icons for all the items,
127 // so we can't use it if we have no image list. In this case we'd like to
128 // use wxLC_LIST mode because it works correctly for both horizontally and
129 // vertically laid out controls, but MSW native wxListCtrl insists on
130 // creating multiple columns if there are too many items and there doesn't
131 // seem anything to do about it, so we have to use wxLC_REPORT mode in this
134 long flags
= IsVertical() ? wxLC_ALIGN_LEFT
: wxLC_ALIGN_TOP
;
135 if ( GetImageList() )
144 // Notice that we intentionally overwrite the alignment flags here
145 // by not using "|=", alignment isn't used for report view.
146 flags
= wxLC_REPORT
| wxLC_NO_HEADER
;
155 // Use single selection in any case.
156 return flags
| wxLC_SINGLE_SEL
;
159 // ----------------------------------------------------------------------------
160 // wxListbook geometry management
161 // ----------------------------------------------------------------------------
163 void wxListbook::OnSize(wxSizeEvent
& event
)
165 // arrange the icons before calling SetClientSize(), otherwise it wouldn't
166 // account for the scrollbars the list control might need and, at least
167 // under MSW, we'd finish with an ugly looking list control with both
168 // vertical and horizontal scrollbar (with one of them being added because
169 // the other one is not accounted for in client size computations)
170 wxListView
* const list
= GetListView();
177 int wxListbook::HitTest(const wxPoint
& pt
, long *flags
) const
179 int pagePos
= wxNOT_FOUND
;
182 *flags
= wxBK_HITTEST_NOWHERE
;
184 // convert from listbook control coordinates to list control coordinates
185 const wxListView
* const list
= GetListView();
186 const wxPoint listPt
= list
->ScreenToClient(ClientToScreen(pt
));
188 // is the point inside list control?
189 if ( wxRect(list
->GetSize()).Contains(listPt
) )
192 pagePos
= list
->HitTest(listPt
, flagsList
);
196 if ( pagePos
!= wxNOT_FOUND
)
199 if ( flagsList
& (wxLIST_HITTEST_ONITEMICON
|
200 wxLIST_HITTEST_ONITEMSTATEICON
) )
201 *flags
|= wxBK_HITTEST_ONICON
;
203 if ( flagsList
& wxLIST_HITTEST_ONITEMLABEL
)
204 *flags
|= wxBK_HITTEST_ONLABEL
;
207 else // not over list control at all
209 if ( flags
&& GetPageRect().Contains(pt
) )
210 *flags
|= wxBK_HITTEST_ONPAGE
;
216 void wxListbook::UpdateSize()
218 // we should find a more elegant way to force a layout than generating this
220 wxSizeEvent
sz(GetSize(), GetId());
221 GetEventHandler()->ProcessEvent(sz
);
224 // ----------------------------------------------------------------------------
225 // accessing the pages
226 // ----------------------------------------------------------------------------
228 bool wxListbook::SetPageText(size_t n
, const wxString
& strText
)
230 GetListView()->SetItemText(n
, strText
);
235 wxString
wxListbook::GetPageText(size_t n
) const
237 return GetListView()->GetItemText(n
);
240 int wxListbook::GetPageImage(size_t n
) const
245 if (GetListView()->GetItem(item
))
247 return item
.GetImage();
255 bool wxListbook::SetPageImage(size_t n
, int imageId
)
257 return GetListView()->SetItemImage(n
, imageId
);
260 // ----------------------------------------------------------------------------
262 // ----------------------------------------------------------------------------
264 void wxListbook::SetImageList(wxImageList
*imageList
)
266 const long flagsOld
= GetListCtrlFlags();
268 wxBookCtrlBase::SetImageList(imageList
);
270 const long flagsNew
= GetListCtrlFlags();
272 wxListView
* const list
= GetListView();
274 // We may need to change the list control mode if the image list presence
276 if ( flagsNew
!= flagsOld
)
278 // Preserve the selection which is lost when changing the mode
279 const int oldSel
= GetSelection();
281 list
->SetWindowStyleFlag(flagsNew
);
282 if ( list
->InReportView() )
283 list
->InsertColumn(0, wxS("Pages"));
286 if ( oldSel
!= wxNOT_FOUND
)
287 SetSelection(oldSel
);
290 list
->SetImageList(imageList
, wxIMAGE_LIST_NORMAL
);
293 // ----------------------------------------------------------------------------
295 // ----------------------------------------------------------------------------
297 void wxListbook::UpdateSelectedPage(size_t newsel
)
299 m_selection
= newsel
;
300 GetListView()->Select(newsel
);
301 GetListView()->Focus(newsel
);
304 wxBookCtrlEvent
* wxListbook::CreatePageChangingEvent() const
306 return new wxBookCtrlEvent(wxEVT_LISTBOOK_PAGE_CHANGING
, m_windowId
);
309 void wxListbook::MakeChangedEvent(wxBookCtrlEvent
&event
)
311 event
.SetEventType(wxEVT_LISTBOOK_PAGE_CHANGED
);
315 // ----------------------------------------------------------------------------
316 // adding/removing the pages
317 // ----------------------------------------------------------------------------
320 wxListbook::InsertPage(size_t n
,
322 const wxString
& text
,
326 if ( !wxBookCtrlBase::InsertPage(n
, page
, text
, bSelect
, imageId
) )
329 GetListView()->InsertItem(n
, text
, imageId
);
331 // if the inserted page is before the selected one, we must update the
332 // index of the selected page
333 if ( int(n
) <= m_selection
)
335 // one extra page added
337 GetListView()->Select(m_selection
);
338 GetListView()->Focus(m_selection
);
341 if ( !DoSetSelectionAfterInsertion(n
, bSelect
) )
349 wxWindow
*wxListbook::DoRemovePage(size_t page
)
351 wxWindow
*win
= wxBookCtrlBase::DoRemovePage(page
);
355 GetListView()->DeleteItem(page
);
357 DoSetSelectionAfterRemoval(page
);
359 GetListView()->Arrange();
367 bool wxListbook::DeleteAllPages()
369 GetListView()->DeleteAllItems();
370 if (!wxBookCtrlBase::DeleteAllPages())
378 // ----------------------------------------------------------------------------
380 // ----------------------------------------------------------------------------
382 void wxListbook::OnListSelected(wxListEvent
& eventList
)
384 if ( eventList
.GetEventObject() != m_bookctrl
)
390 const int selNew
= eventList
.GetIndex();
392 if ( selNew
== m_selection
)
394 // this event can only come from our own Select(m_selection) below
395 // which we call when the page change is vetoed, so we should simply
400 SetSelection(selNew
);
402 // change wasn't allowed, return to previous state
403 if (m_selection
!= selNew
)
405 GetListView()->Select(m_selection
);
406 GetListView()->Focus(m_selection
);
410 #endif // wxUSE_LISTBOOK