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
* const list
= GetListView();
195 int wxListbook::HitTest(const wxPoint
& pt
, long *flags
) const
197 int pagePos
= wxNOT_FOUND
;
200 *flags
= wxBK_HITTEST_NOWHERE
;
202 // convert from listbook control coordinates to list control coordinates
203 const wxListView
* const list
= GetListView();
204 const wxPoint listPt
= list
->ScreenToClient(ClientToScreen(pt
));
206 // is the point inside list control?
207 if ( wxRect(list
->GetSize()).Contains(listPt
) )
210 pagePos
= list
->HitTest(listPt
, flagsList
);
214 if ( pagePos
!= wxNOT_FOUND
)
217 if ( flagsList
& (wxLIST_HITTEST_ONITEMICON
|
218 wxLIST_HITTEST_ONITEMSTATEICON
) )
219 *flags
|= wxBK_HITTEST_ONICON
;
221 if ( flagsList
& wxLIST_HITTEST_ONITEMLABEL
)
222 *flags
|= wxBK_HITTEST_ONLABEL
;
225 else // not over list control at all
227 if ( flags
&& GetPageRect().Contains(pt
) )
228 *flags
|= wxBK_HITTEST_ONPAGE
;
234 wxSize
wxListbook::CalcSizeFromPage(const wxSize
& sizePage
) const
236 // we need to add the size of the list control and the border between
237 const wxSize sizeList
= GetControllerSize();
239 wxSize size
= sizePage
;
242 size
.y
+= sizeList
.y
+ GetInternalBorder();
244 else // left/right aligned
246 size
.x
+= sizeList
.x
+ GetInternalBorder();
252 void wxListbook::UpdateSize()
254 // we should find a more elegant way to force a layout than generating this
256 wxSizeEvent
sz(GetSize(), GetId());
257 GetEventHandler()->ProcessEvent(sz
);
260 // ----------------------------------------------------------------------------
261 // accessing the pages
262 // ----------------------------------------------------------------------------
264 bool wxListbook::SetPageText(size_t n
, const wxString
& strText
)
266 GetListView()->SetItemText(n
, strText
);
271 wxString
wxListbook::GetPageText(size_t n
) const
273 return GetListView()->GetItemText(n
);
276 int wxListbook::GetPageImage(size_t n
) const
281 if (GetListView()->GetItem(item
))
283 return item
.GetImage();
291 bool wxListbook::SetPageImage(size_t n
, int imageId
)
293 return GetListView()->SetItemImage(n
, imageId
);
296 // ----------------------------------------------------------------------------
298 // ----------------------------------------------------------------------------
300 void wxListbook::SetImageList(wxImageList
*imageList
)
302 wxListView
* const list
= GetListView();
304 #ifdef CAN_USE_REPORT_VIEW
305 // If imageList presence has changed, we update the list control view
306 if ( (imageList
!= NULL
) != (GetImageList() != NULL
) )
308 wxArrayString labels
;
309 labels
.Alloc(GetPageCount());
312 imageIds
.Alloc(GetPageCount());
314 const int oldSel
= GetSelection();
317 // Grab snapshot of all list control items before changing the window
318 // style (which deletes the items)
319 for ( i
= 0; i
< GetPageCount(); i
++ )
321 labels
.Add(GetPageText(i
));
322 imageIds
.Add(GetPageImage(i
));
325 // Update the style to use icon view for images, report view otherwise
326 long style
= wxLC_SINGLE_SEL
;
329 style
|= GetListCtrlIconViewFlags();
331 else // no image list
333 style
|= GetListCtrlReportViewFlags();
336 list
->SetWindowStyleFlag(style
);
338 list
->InsertColumn(0, wxT("Pages"));
340 // Add back the list control items
341 for ( i
= 0; i
< GetPageCount(); i
++ )
343 list
->InsertItem(i
, labels
[i
], imageIds
[i
]);
347 if ( oldSel
!= wxNOT_FOUND
)
348 SetSelection(oldSel
);
351 list
->SetImageList(imageList
, wxIMAGE_LIST_NORMAL
);
352 #endif // CAN_USE_REPORT_VIEW
354 wxBookCtrlBase::SetImageList(imageList
);
357 // ----------------------------------------------------------------------------
359 // ----------------------------------------------------------------------------
361 void wxListbook::UpdateSelectedPage(size_t newsel
)
363 m_selection
= newsel
;
364 GetListView()->Select(newsel
);
365 GetListView()->Focus(newsel
);
368 int wxListbook::GetSelection() const
373 wxBookCtrlBaseEvent
* wxListbook::CreatePageChangingEvent() const
375 return new wxListbookEvent(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING
, m_windowId
);
378 void wxListbook::MakeChangedEvent(wxBookCtrlBaseEvent
&event
)
380 event
.SetEventType(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED
);
384 // ----------------------------------------------------------------------------
385 // adding/removing the pages
386 // ----------------------------------------------------------------------------
389 wxListbook::InsertPage(size_t n
,
391 const wxString
& text
,
395 if ( !wxBookCtrlBase::InsertPage(n
, page
, text
, bSelect
, imageId
) )
398 GetListView()->InsertItem(n
, text
, imageId
);
400 // if the inserted page is before the selected one, we must update the
401 // index of the selected page
402 if ( int(n
) <= m_selection
)
404 // one extra page added
406 GetListView()->Select(m_selection
);
407 GetListView()->Focus(m_selection
);
410 // some page should be selected: either this one or the first one if there
411 // is still no selection
415 else if ( m_selection
== -1 )
418 if ( selNew
!= m_selection
)
422 SetSelection(selNew
);
429 wxWindow
*wxListbook::DoRemovePage(size_t page
)
431 const size_t page_count
= GetPageCount();
432 wxWindow
*win
= wxBookCtrlBase::DoRemovePage(page
);
436 GetListView()->DeleteItem(page
);
438 if (m_selection
>= (int)page
)
440 // force new sel valid if possible
441 int sel
= m_selection
- 1;
444 else if ((page_count
== 2) || (sel
== -1))
447 // force sel invalid if deleting current page - don't try to hide it
448 m_selection
= (m_selection
== (int)page
) ? wxNOT_FOUND
: m_selection
- 1;
450 if ((sel
!= wxNOT_FOUND
) && (sel
!= m_selection
))
454 GetListView()->Arrange();
462 bool wxListbook::DeleteAllPages()
464 GetListView()->DeleteAllItems();
465 if (!wxBookCtrlBase::DeleteAllPages())
475 // ----------------------------------------------------------------------------
477 // ----------------------------------------------------------------------------
479 void wxListbook::OnListSelected(wxListEvent
& eventList
)
481 if ( eventList
.GetEventObject() != m_bookctrl
)
487 const int selNew
= eventList
.GetIndex();
489 if ( selNew
== m_selection
)
491 // this event can only come from our own Select(m_selection) below
492 // which we call when the page change is vetoed, so we should simply
497 SetSelection(selNew
);
499 // change wasn't allowed, return to previous state
500 if (m_selection
!= selNew
)
502 GetListView()->Select(m_selection
);
503 GetListView()->Focus(m_selection
);
507 #endif // wxUSE_LISTBOOK