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
)
51 IMPLEMENT_DYNAMIC_CLASS(wxListbookEvent
, wxNotifyEvent
)
53 const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING
= wxNewEventType();
54 const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED
= wxNewEventType();
56 BEGIN_EVENT_TABLE(wxListbook
, wxBookCtrlBase
)
57 EVT_SIZE(wxListbook::OnSize
)
58 EVT_LIST_ITEM_SELECTED(wxID_ANY
, wxListbook::OnListSelected
)
61 // ============================================================================
62 // wxListbook implementation
63 // ============================================================================
65 // ----------------------------------------------------------------------------
66 // wxListbook creation
67 // ----------------------------------------------------------------------------
69 void wxListbook::Init()
71 m_selection
= wxNOT_FOUND
;
75 wxListbook::Create(wxWindow
*parent
,
82 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
88 #endif // __WXMAC__/!__WXMAC__
91 // no border for this control, it doesn't look nice together with
93 style
&= ~wxBORDER_MASK
;
94 style
|= wxBORDER_NONE
;
96 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
97 wxDefaultValidator
, name
) )
100 m_bookctrl
= new wxListView
106 wxLC_SINGLE_SEL
| wxLC_REPORT
| wxLC_NO_HEADER
109 GetListView()->InsertColumn(0, wxT("Pages"));
112 // On XP with themes enabled the GetViewRect used in GetControllerSize() to
113 // determine the space needed for the list view will incorrectly return
114 // (0,0,0,0) the first time. So send a pending event so OnSize will be
115 // called again after the window is ready to go. Technically we don't
116 // need to do this on non-XP windows, but if things are already sized
117 // correctly then nothing changes and so there is no harm.
119 GetEventHandler()->AddPendingEvent(evt
);
124 // ----------------------------------------------------------------------------
125 // wxListbook geometry management
126 // ----------------------------------------------------------------------------
128 wxSize
wxListbook::GetControllerSize() const
130 const wxSize sizeClient
= GetClientSize(),
131 sizeBorder
= m_bookctrl
->GetSize() - m_bookctrl
->GetClientSize(),
132 sizeList
= GetListView()->GetViewRect().GetSize() + sizeBorder
;
138 size
.x
= sizeClient
.x
;
141 else // left/right aligned
144 size
.y
= sizeClient
.y
;
150 void wxListbook::OnSize(wxSizeEvent
& event
)
152 // arrange the icons before calling SetClientSize(), otherwise it wouldn't
153 // account for the scrollbars the list control might need and, at least
154 // under MSW, we'd finish with an ugly looking list control with both
155 // vertical and horizontal scrollbar (with one of them being added because
156 // the other one is not accounted for in client size computations)
157 wxListView
*list
= GetListView();
158 if (list
) list
->Arrange();
159 wxBookCtrlBase::OnSize(event
);
162 int wxListbook::HitTest(const wxPoint
& pt
, long *flags
) const
164 int pagePos
= wxNOT_FOUND
;
167 *flags
= wxBK_HITTEST_NOWHERE
;
169 // convert from listbook control coordinates to list control coordinates
170 const wxListView
* const list
= GetListView();
171 const wxPoint listPt
= list
->ScreenToClient(ClientToScreen(pt
));
173 // is the point inside list control?
174 if ( wxRect(list
->GetSize()).Contains(listPt
) )
177 pagePos
= list
->HitTest(listPt
, flagsList
);
181 if ( pagePos
!= wxNOT_FOUND
)
184 if ( flagsList
& (wxLIST_HITTEST_ONITEMICON
|
185 wxLIST_HITTEST_ONITEMSTATEICON
) )
186 *flags
|= wxBK_HITTEST_ONICON
;
188 if ( flagsList
& wxLIST_HITTEST_ONITEMLABEL
)
189 *flags
|= wxBK_HITTEST_ONLABEL
;
192 else // not over list control at all
194 if ( flags
&& GetPageRect().Contains(pt
) )
195 *flags
|= wxBK_HITTEST_ONPAGE
;
201 wxSize
wxListbook::CalcSizeFromPage(const wxSize
& sizePage
) const
203 // we need to add the size of the list control and the border between
204 const wxSize sizeList
= GetControllerSize();
206 wxSize size
= sizePage
;
209 size
.y
+= sizeList
.y
+ GetInternalBorder();
211 else // left/right aligned
213 size
.x
+= sizeList
.x
+ GetInternalBorder();
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 wxListView
* const list
= GetListView();
264 // If imageList presence has changed, we update the list control view
265 if ( (imageList
!= NULL
) != (GetImageList() != NULL
) )
267 wxArrayString labels
;
268 labels
.Alloc(GetPageCount());
271 imageIds
.Alloc(GetPageCount());
273 const int oldSel
= GetSelection();
276 // Grab snapshot of all list control items before changing the window
277 // style (which deletes the items)
278 for ( i
= 0; i
< GetPageCount(); i
++ )
280 labels
.Add(GetPageText(i
));
281 imageIds
.Add(GetPageImage(i
));
284 // Update the style to use icon view for images, report view otherwise
285 long style
= wxLC_SINGLE_SEL
;
288 list
->SetWindowStyleFlag(style
|
289 (IsVertical() ? wxLC_ALIGN_LEFT
293 else // no image list
295 list
->SetWindowStyleFlag(style
| wxLC_REPORT
| wxLC_NO_HEADER
);
296 list
->InsertColumn(0, wxT("Pages"));
299 // Add back the list control items
300 for ( i
= 0; i
< GetPageCount(); i
++ )
302 list
->InsertItem(i
, labels
[i
], imageIds
[i
]);
306 if ( oldSel
!= wxNOT_FOUND
)
307 SetSelection(oldSel
);
310 list
->SetImageList(imageList
, wxIMAGE_LIST_NORMAL
);
312 wxBookCtrlBase::SetImageList(imageList
);
315 // ----------------------------------------------------------------------------
317 // ----------------------------------------------------------------------------
319 void wxListbook::UpdateSelectedPage(size_t newsel
)
321 m_selection
= newsel
;
322 GetListView()->Select(newsel
);
323 GetListView()->Focus(newsel
);
326 int wxListbook::GetSelection() const
331 wxBookCtrlBaseEvent
* wxListbook::CreatePageChangingEvent() const
333 return new wxListbookEvent(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING
, m_windowId
);
336 void wxListbook::MakeChangedEvent(wxBookCtrlBaseEvent
&event
)
338 event
.SetEventType(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED
);
342 // ----------------------------------------------------------------------------
343 // adding/removing the pages
344 // ----------------------------------------------------------------------------
347 wxListbook::InsertPage(size_t n
,
349 const wxString
& text
,
353 if ( !wxBookCtrlBase::InsertPage(n
, page
, text
, bSelect
, imageId
) )
356 GetListView()->InsertItem(n
, text
, imageId
);
358 // if the inserted page is before the selected one, we must update the
359 // index of the selected page
360 if ( int(n
) <= m_selection
)
362 // one extra page added
364 GetListView()->Select(m_selection
);
365 GetListView()->Focus(m_selection
);
368 // some page should be selected: either this one or the first one if there
369 // is still no selection
373 else if ( m_selection
== -1 )
376 if ( selNew
!= m_selection
)
380 SetSelection(selNew
);
382 wxSizeEvent
sz(GetSize(), GetId());
383 GetEventHandler()->ProcessEvent(sz
);
388 wxWindow
*wxListbook::DoRemovePage(size_t page
)
390 const size_t page_count
= GetPageCount();
391 wxWindow
*win
= wxBookCtrlBase::DoRemovePage(page
);
395 GetListView()->DeleteItem(page
);
397 if (m_selection
>= (int)page
)
399 // force new sel valid if possible
400 int sel
= m_selection
- 1;
403 else if ((page_count
== 2) || (sel
== -1))
406 // force sel invalid if deleting current page - don't try to hide it
407 m_selection
= (m_selection
== (int)page
) ? wxNOT_FOUND
: m_selection
- 1;
409 if ((sel
!= wxNOT_FOUND
) && (sel
!= m_selection
))
413 GetListView()->Arrange();
414 if (GetPageCount() == 0)
416 wxSizeEvent
sz(GetSize(), GetId());
417 GetEventHandler()->ProcessEvent(sz
);
425 bool wxListbook::DeleteAllPages()
427 GetListView()->DeleteAllItems();
428 if (!wxBookCtrlBase::DeleteAllPages())
433 wxSizeEvent
sz(GetSize(), GetId());
434 GetEventHandler()->ProcessEvent(sz
);
439 // ----------------------------------------------------------------------------
441 // ----------------------------------------------------------------------------
443 void wxListbook::OnListSelected(wxListEvent
& eventList
)
445 if ( eventList
.GetEventObject() != m_bookctrl
)
451 const int selNew
= eventList
.GetIndex();
453 if ( selNew
== m_selection
)
455 // this event can only come from our own Select(m_selection) below
456 // which we call when the page change is vetoed, so we should simply
461 SetSelection(selNew
);
463 // change wasn't allowed, return to previous state
464 if (m_selection
!= selNew
)
466 GetListView()->Select(m_selection
);
467 GetListView()->Focus(m_selection
);
471 #endif // wxUSE_LISTBOOK