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 // FIXME: native OS X wxListCtrl hangs if this code is used for it so disable
41 #if !defined(__WXMAC__)
42 #define CAN_USE_REPORT_VIEW
45 // ----------------------------------------------------------------------------
46 // various wxWidgets macros
47 // ----------------------------------------------------------------------------
49 // check that the page index is valid
50 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 IMPLEMENT_DYNAMIC_CLASS(wxListbook
, wxBookCtrlBase
)
57 IMPLEMENT_DYNAMIC_CLASS(wxListbookEvent
, wxNotifyEvent
)
59 const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING
= wxNewEventType();
60 const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED
= wxNewEventType();
62 BEGIN_EVENT_TABLE(wxListbook
, wxBookCtrlBase
)
63 EVT_SIZE(wxListbook::OnSize
)
64 EVT_LIST_ITEM_SELECTED(wxID_ANY
, wxListbook::OnListSelected
)
67 // ============================================================================
68 // wxListbook implementation
69 // ============================================================================
71 // ----------------------------------------------------------------------------
72 // wxListbook creation
73 // ----------------------------------------------------------------------------
75 void wxListbook::Init()
77 m_selection
= wxNOT_FOUND
;
81 wxListbook::Create(wxWindow
*parent
,
88 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
94 #endif // __WXMAC__/!__WXMAC__
97 // no border for this control, it doesn't look nice together with
99 style
&= ~wxBORDER_MASK
;
100 style
|= wxBORDER_NONE
;
102 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
103 wxDefaultValidator
, name
) )
106 m_bookctrl
= new wxListView
113 #ifdef CAN_USE_REPORT_VIEW
114 GetListCtrlReportViewFlags()
115 #else // !CAN_USE_REPORT_VIEW
116 GetListCtrlIconViewFlags()
117 #endif // CAN_USE_REPORT_VIEW/!CAN_USE_REPORT_VIEW
120 #ifdef CAN_USE_REPORT_VIEW
121 GetListView()->InsertColumn(0, wxT("Pages"));
122 #endif // CAN_USE_REPORT_VIEW
125 // On XP with themes enabled the GetViewRect used in GetControllerSize() to
126 // determine the space needed for the list view will incorrectly return
127 // (0,0,0,0) the first time. So send a pending event so OnSize will be
128 // called again after the window is ready to go. Technically we don't
129 // need to do this on non-XP windows, but if things are already sized
130 // correctly then nothing changes and so there is no harm.
132 GetEventHandler()->AddPendingEvent(evt
);
137 // ----------------------------------------------------------------------------
139 // ----------------------------------------------------------------------------
141 long wxListbook::GetListCtrlIconViewFlags() const
143 return (IsVertical() ? wxLC_ALIGN_LEFT
: wxLC_ALIGN_TOP
) | wxLC_ICON
;
146 #ifdef CAN_USE_REPORT_VIEW
148 long wxListbook::GetListCtrlReportViewFlags() const
150 return wxLC_REPORT
| wxLC_NO_HEADER
;
153 #endif // CAN_USE_REPORT_VIEW
155 // ----------------------------------------------------------------------------
156 // wxListbook geometry management
157 // ----------------------------------------------------------------------------
159 wxSize
wxListbook::GetControllerSize() const
161 const wxSize sizeClient
= GetClientSize(),
162 sizeBorder
= m_bookctrl
->GetSize() - m_bookctrl
->GetClientSize(),
163 sizeList
= GetListView()->GetViewRect().GetSize() + sizeBorder
;
169 size
.x
= sizeClient
.x
;
172 else // left/right aligned
175 size
.y
= sizeClient
.y
;
181 void wxListbook::OnSize(wxSizeEvent
& event
)
183 // arrange the icons before calling SetClientSize(), otherwise it wouldn't
184 // account for the scrollbars the list control might need and, at least
185 // under MSW, we'd finish with an ugly looking list control with both
186 // vertical and horizontal scrollbar (with one of them being added because
187 // the other one is not accounted for in client size computations)
188 wxListView
*list
= GetListView();
189 if (list
) list
->Arrange();
190 wxBookCtrlBase::OnSize(event
);
193 int wxListbook::HitTest(const wxPoint
& pt
, long *flags
) const
195 int pagePos
= wxNOT_FOUND
;
198 *flags
= wxBK_HITTEST_NOWHERE
;
200 // convert from listbook control coordinates to list control coordinates
201 const wxListView
* const list
= GetListView();
202 const wxPoint listPt
= list
->ScreenToClient(ClientToScreen(pt
));
204 // is the point inside list control?
205 if ( wxRect(list
->GetSize()).Contains(listPt
) )
208 pagePos
= list
->HitTest(listPt
, flagsList
);
212 if ( pagePos
!= wxNOT_FOUND
)
215 if ( flagsList
& (wxLIST_HITTEST_ONITEMICON
|
216 wxLIST_HITTEST_ONITEMSTATEICON
) )
217 *flags
|= wxBK_HITTEST_ONICON
;
219 if ( flagsList
& wxLIST_HITTEST_ONITEMLABEL
)
220 *flags
|= wxBK_HITTEST_ONLABEL
;
223 else // not over list control at all
225 if ( flags
&& GetPageRect().Contains(pt
) )
226 *flags
|= wxBK_HITTEST_ONPAGE
;
232 wxSize
wxListbook::CalcSizeFromPage(const wxSize
& sizePage
) const
234 // we need to add the size of the list control and the border between
235 const wxSize sizeList
= GetControllerSize();
237 wxSize size
= sizePage
;
240 size
.y
+= sizeList
.y
+ GetInternalBorder();
242 else // left/right aligned
244 size
.x
+= sizeList
.x
+ GetInternalBorder();
251 // ----------------------------------------------------------------------------
252 // accessing the pages
253 // ----------------------------------------------------------------------------
255 bool wxListbook::SetPageText(size_t n
, const wxString
& strText
)
257 GetListView()->SetItemText(n
, strText
);
262 wxString
wxListbook::GetPageText(size_t n
) const
264 return GetListView()->GetItemText(n
);
267 int wxListbook::GetPageImage(size_t n
) const
272 if (GetListView()->GetItem(item
))
274 return item
.GetImage();
282 bool wxListbook::SetPageImage(size_t n
, int imageId
)
284 return GetListView()->SetItemImage(n
, imageId
);
287 // ----------------------------------------------------------------------------
289 // ----------------------------------------------------------------------------
291 void wxListbook::SetImageList(wxImageList
*imageList
)
293 wxListView
* const list
= GetListView();
295 #ifdef CAN_USE_REPORT_VIEW
296 // If imageList presence has changed, we update the list control view
297 if ( (imageList
!= NULL
) != (GetImageList() != NULL
) )
299 wxArrayString labels
;
300 labels
.Alloc(GetPageCount());
303 imageIds
.Alloc(GetPageCount());
305 const int oldSel
= GetSelection();
308 // Grab snapshot of all list control items before changing the window
309 // style (which deletes the items)
310 for ( i
= 0; i
< GetPageCount(); i
++ )
312 labels
.Add(GetPageText(i
));
313 imageIds
.Add(GetPageImage(i
));
316 // Update the style to use icon view for images, report view otherwise
317 long style
= wxLC_SINGLE_SEL
;
320 style
|= GetListCtrlIconViewFlags();
322 else // no image list
324 style
|= GetListCtrlReportViewFlags();
327 list
->SetWindowStyleFlag(style
);
329 list
->InsertColumn(0, wxT("Pages"));
331 // Add back the list control items
332 for ( i
= 0; i
< GetPageCount(); i
++ )
334 list
->InsertItem(i
, labels
[i
], imageIds
[i
]);
338 if ( oldSel
!= wxNOT_FOUND
)
339 SetSelection(oldSel
);
342 list
->SetImageList(imageList
, wxIMAGE_LIST_NORMAL
);
343 #endif // CAN_USE_REPORT_VIEW
345 wxBookCtrlBase::SetImageList(imageList
);
348 // ----------------------------------------------------------------------------
350 // ----------------------------------------------------------------------------
352 void wxListbook::UpdateSelectedPage(size_t newsel
)
354 m_selection
= newsel
;
355 GetListView()->Select(newsel
);
356 GetListView()->Focus(newsel
);
359 int wxListbook::GetSelection() const
364 wxBookCtrlBaseEvent
* wxListbook::CreatePageChangingEvent() const
366 return new wxListbookEvent(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING
, m_windowId
);
369 void wxListbook::MakeChangedEvent(wxBookCtrlBaseEvent
&event
)
371 event
.SetEventType(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED
);
375 // ----------------------------------------------------------------------------
376 // adding/removing the pages
377 // ----------------------------------------------------------------------------
380 wxListbook::InsertPage(size_t n
,
382 const wxString
& text
,
386 if ( !wxBookCtrlBase::InsertPage(n
, page
, text
, bSelect
, imageId
) )
389 GetListView()->InsertItem(n
, text
, imageId
);
391 // if the inserted page is before the selected one, we must update the
392 // index of the selected page
393 if ( int(n
) <= m_selection
)
395 // one extra page added
397 GetListView()->Select(m_selection
);
398 GetListView()->Focus(m_selection
);
401 // some page should be selected: either this one or the first one if there
402 // is still no selection
406 else if ( m_selection
== -1 )
409 if ( selNew
!= m_selection
)
413 SetSelection(selNew
);
415 wxSizeEvent
sz(GetSize(), GetId());
416 GetEventHandler()->ProcessEvent(sz
);
421 wxWindow
*wxListbook::DoRemovePage(size_t page
)
423 const size_t page_count
= GetPageCount();
424 wxWindow
*win
= wxBookCtrlBase::DoRemovePage(page
);
428 GetListView()->DeleteItem(page
);
430 if (m_selection
>= (int)page
)
432 // force new sel valid if possible
433 int sel
= m_selection
- 1;
436 else if ((page_count
== 2) || (sel
== -1))
439 // force sel invalid if deleting current page - don't try to hide it
440 m_selection
= (m_selection
== (int)page
) ? wxNOT_FOUND
: m_selection
- 1;
442 if ((sel
!= wxNOT_FOUND
) && (sel
!= m_selection
))
446 GetListView()->Arrange();
447 if (GetPageCount() == 0)
449 wxSizeEvent
sz(GetSize(), GetId());
450 GetEventHandler()->ProcessEvent(sz
);
458 bool wxListbook::DeleteAllPages()
460 GetListView()->DeleteAllItems();
461 if (!wxBookCtrlBase::DeleteAllPages())
466 wxSizeEvent
sz(GetSize(), GetId());
467 GetEventHandler()->ProcessEvent(sz
);
472 // ----------------------------------------------------------------------------
474 // ----------------------------------------------------------------------------
476 void wxListbook::OnListSelected(wxListEvent
& eventList
)
478 if ( eventList
.GetEventObject() != m_bookctrl
)
484 const int selNew
= eventList
.GetIndex();
486 if ( selNew
== m_selection
)
488 // this event can only come from our own Select(m_selection) below
489 // which we call when the page change is vetoed, so we should simply
494 SetSelection(selNew
);
496 // change wasn't allowed, return to previous state
497 if (m_selection
!= selNew
)
499 GetListView()->Select(m_selection
);
500 GetListView()->Focus(m_selection
);
504 #endif // wxUSE_LISTBOOK