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
)
58 wxDEFINE_EVENT( wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING
, wxBookCtrlEvent
);
59 wxDEFINE_EVENT( wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED
, wxBookCtrlEvent
);
61 BEGIN_EVENT_TABLE(wxListbook
, wxBookCtrlBase
)
62 EVT_SIZE(wxListbook::OnSize
)
63 EVT_LIST_ITEM_SELECTED(wxID_ANY
, wxListbook::OnListSelected
)
66 // ============================================================================
67 // wxListbook implementation
68 // ============================================================================
70 // ----------------------------------------------------------------------------
71 // wxListbook creation
72 // ----------------------------------------------------------------------------
74 void wxListbook::Init()
76 m_selection
= wxNOT_FOUND
;
80 wxListbook::Create(wxWindow
*parent
,
87 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
93 #endif // __WXMAC__/!__WXMAC__
96 // no border for this control, it doesn't look nice together with
98 style
&= ~wxBORDER_MASK
;
99 style
|= wxBORDER_NONE
;
101 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
102 wxDefaultValidator
, name
) )
105 m_bookctrl
= new wxListView
112 #ifdef CAN_USE_REPORT_VIEW
113 GetListCtrlReportViewFlags()
114 #else // !CAN_USE_REPORT_VIEW
115 GetListCtrlIconViewFlags()
116 #endif // CAN_USE_REPORT_VIEW/!CAN_USE_REPORT_VIEW
119 #ifdef CAN_USE_REPORT_VIEW
120 GetListView()->InsertColumn(0, wxT("Pages"));
121 #endif // CAN_USE_REPORT_VIEW
124 // On XP with themes enabled the GetViewRect used in GetControllerSize() to
125 // determine the space needed for the list view will incorrectly return
126 // (0,0,0,0) the first time. So send a pending event so OnSize will be
127 // called again after the window is ready to go. Technically we don't
128 // need to do this on non-XP windows, but if things are already sized
129 // correctly then nothing changes and so there is no harm.
131 GetEventHandler()->AddPendingEvent(evt
);
136 // ----------------------------------------------------------------------------
138 // ----------------------------------------------------------------------------
140 long wxListbook::GetListCtrlIconViewFlags() const
142 return (IsVertical() ? wxLC_ALIGN_LEFT
: wxLC_ALIGN_TOP
) | wxLC_ICON
;
145 #ifdef CAN_USE_REPORT_VIEW
147 long wxListbook::GetListCtrlReportViewFlags() const
149 return wxLC_REPORT
| wxLC_NO_HEADER
;
152 #endif // CAN_USE_REPORT_VIEW
154 // ----------------------------------------------------------------------------
155 // wxListbook geometry management
156 // ----------------------------------------------------------------------------
158 wxSize
wxListbook::GetControllerSize() const
160 const wxSize sizeClient
= GetClientSize(),
161 sizeBorder
= m_bookctrl
->GetSize() - m_bookctrl
->GetClientSize(),
162 sizeList
= GetListView()->GetViewRect().GetSize() + sizeBorder
;
168 size
.x
= sizeClient
.x
;
171 else // left/right aligned
174 size
.y
= sizeClient
.y
;
180 void wxListbook::OnSize(wxSizeEvent
& event
)
182 // arrange the icons before calling SetClientSize(), otherwise it wouldn't
183 // account for the scrollbars the list control might need and, at least
184 // under MSW, we'd finish with an ugly looking list control with both
185 // vertical and horizontal scrollbar (with one of them being added because
186 // the other one is not accounted for in client size computations)
187 wxListView
* const list
= GetListView();
194 int wxListbook::HitTest(const wxPoint
& pt
, long *flags
) const
196 int pagePos
= wxNOT_FOUND
;
199 *flags
= wxBK_HITTEST_NOWHERE
;
201 // convert from listbook control coordinates to list control coordinates
202 const wxListView
* const list
= GetListView();
203 const wxPoint listPt
= list
->ScreenToClient(ClientToScreen(pt
));
205 // is the point inside list control?
206 if ( wxRect(list
->GetSize()).Contains(listPt
) )
209 pagePos
= list
->HitTest(listPt
, flagsList
);
213 if ( pagePos
!= wxNOT_FOUND
)
216 if ( flagsList
& (wxLIST_HITTEST_ONITEMICON
|
217 wxLIST_HITTEST_ONITEMSTATEICON
) )
218 *flags
|= wxBK_HITTEST_ONICON
;
220 if ( flagsList
& wxLIST_HITTEST_ONITEMLABEL
)
221 *flags
|= wxBK_HITTEST_ONLABEL
;
224 else // not over list control at all
226 if ( flags
&& GetPageRect().Contains(pt
) )
227 *flags
|= wxBK_HITTEST_ONPAGE
;
233 wxSize
wxListbook::CalcSizeFromPage(const wxSize
& sizePage
) const
235 // we need to add the size of the list control and the border between
236 const wxSize sizeList
= GetControllerSize();
238 wxSize size
= sizePage
;
241 size
.y
+= sizeList
.y
+ GetInternalBorder();
243 else // left/right aligned
245 size
.x
+= sizeList
.x
+ GetInternalBorder();
251 void wxListbook::UpdateSize()
253 // we should find a more elegant way to force a layout than generating this
255 wxSizeEvent
sz(GetSize(), GetId());
256 GetEventHandler()->ProcessEvent(sz
);
259 // ----------------------------------------------------------------------------
260 // accessing the pages
261 // ----------------------------------------------------------------------------
263 bool wxListbook::SetPageText(size_t n
, const wxString
& strText
)
265 GetListView()->SetItemText(n
, strText
);
270 wxString
wxListbook::GetPageText(size_t n
) const
272 return GetListView()->GetItemText(n
);
275 int wxListbook::GetPageImage(size_t n
) const
280 if (GetListView()->GetItem(item
))
282 return item
.GetImage();
290 bool wxListbook::SetPageImage(size_t n
, int imageId
)
292 return GetListView()->SetItemImage(n
, imageId
);
295 // ----------------------------------------------------------------------------
297 // ----------------------------------------------------------------------------
299 void wxListbook::SetImageList(wxImageList
*imageList
)
301 wxListView
* const list
= GetListView();
303 #ifdef CAN_USE_REPORT_VIEW
304 // If imageList presence has changed, we update the list control view
305 if ( (imageList
!= NULL
) != (GetImageList() != NULL
) )
307 wxArrayString labels
;
308 labels
.Alloc(GetPageCount());
311 imageIds
.Alloc(GetPageCount());
313 const int oldSel
= GetSelection();
316 // Grab snapshot of all list control items before changing the window
317 // style (which deletes the items)
318 for ( i
= 0; i
< GetPageCount(); i
++ )
320 labels
.Add(GetPageText(i
));
321 imageIds
.Add(GetPageImage(i
));
324 // Update the style to use icon view for images, report view otherwise
325 long style
= wxLC_SINGLE_SEL
;
328 style
|= GetListCtrlIconViewFlags();
330 else // no image list
332 style
|= GetListCtrlReportViewFlags();
335 list
->SetWindowStyleFlag(style
);
337 list
->InsertColumn(0, wxT("Pages"));
339 // Add back the list control items
340 for ( i
= 0; i
< GetPageCount(); i
++ )
342 list
->InsertItem(i
, labels
[i
], imageIds
[i
]);
346 if ( oldSel
!= wxNOT_FOUND
)
347 SetSelection(oldSel
);
350 list
->SetImageList(imageList
, wxIMAGE_LIST_NORMAL
);
351 #endif // CAN_USE_REPORT_VIEW
353 wxBookCtrlBase::SetImageList(imageList
);
356 // ----------------------------------------------------------------------------
358 // ----------------------------------------------------------------------------
360 void wxListbook::UpdateSelectedPage(size_t newsel
)
362 m_selection
= newsel
;
363 GetListView()->Select(newsel
);
364 GetListView()->Focus(newsel
);
367 int wxListbook::GetSelection() const
372 wxBookCtrlEvent
* wxListbook::CreatePageChangingEvent() const
374 return new wxBookCtrlEvent(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING
, m_windowId
);
377 void wxListbook::MakeChangedEvent(wxBookCtrlEvent
&event
)
379 event
.SetEventType(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED
);
383 // ----------------------------------------------------------------------------
384 // adding/removing the pages
385 // ----------------------------------------------------------------------------
388 wxListbook::InsertPage(size_t n
,
390 const wxString
& text
,
394 if ( !wxBookCtrlBase::InsertPage(n
, page
, text
, bSelect
, imageId
) )
397 GetListView()->InsertItem(n
, text
, imageId
);
399 // if the inserted page is before the selected one, we must update the
400 // index of the selected page
401 if ( int(n
) <= m_selection
)
403 // one extra page added
405 GetListView()->Select(m_selection
);
406 GetListView()->Focus(m_selection
);
409 // some page should be selected: either this one or the first one if there
410 // is still no selection
414 else if ( m_selection
== -1 )
417 if ( selNew
!= m_selection
)
421 SetSelection(selNew
);
428 wxWindow
*wxListbook::DoRemovePage(size_t page
)
430 const size_t page_count
= GetPageCount();
431 wxWindow
*win
= wxBookCtrlBase::DoRemovePage(page
);
435 GetListView()->DeleteItem(page
);
437 if (m_selection
>= (int)page
)
439 // force new sel valid if possible
440 int sel
= m_selection
- 1;
443 else if ((page_count
== 2) || (sel
== -1))
446 // force sel invalid if deleting current page - don't try to hide it
447 m_selection
= (m_selection
== (int)page
) ? wxNOT_FOUND
: m_selection
- 1;
449 if ((sel
!= wxNOT_FOUND
) && (sel
!= m_selection
))
453 GetListView()->Arrange();
461 bool wxListbook::DeleteAllPages()
463 GetListView()->DeleteAllItems();
464 if (!wxBookCtrlBase::DeleteAllPages())
474 // ----------------------------------------------------------------------------
476 // ----------------------------------------------------------------------------
478 void wxListbook::OnListSelected(wxListEvent
& eventList
)
480 if ( eventList
.GetEventObject() != m_bookctrl
)
486 const int selNew
= eventList
.GetIndex();
488 if ( selNew
== m_selection
)
490 // this event can only come from our own Select(m_selection) below
491 // which we call when the page change is vetoed, so we should simply
496 SetSelection(selNew
);
498 // change wasn't allowed, return to previous state
499 if (m_selection
!= selNew
)
501 GetListView()->Select(m_selection
);
502 GetListView()->Focus(m_selection
);
506 #endif // wxUSE_LISTBOOK