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/listctrl.h"
30 #include "wx/statline.h"
31 #include "wx/listbook.h"
32 #include "wx/imaglist.h"
33 #include "wx/settings.h"
35 // ----------------------------------------------------------------------------
36 // various wxWidgets macros
37 // ----------------------------------------------------------------------------
39 // check that the page index is valid
40 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 IMPLEMENT_DYNAMIC_CLASS(wxListbook
, wxBookCtrlBase
)
47 IMPLEMENT_DYNAMIC_CLASS(wxListbookEvent
, wxNotifyEvent
)
49 #if !WXWIN_COMPATIBILITY_EVENT_TYPES
50 const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING
= wxNewEventType();
51 const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED
= wxNewEventType();
53 const int wxID_LISTBOOKLISTVIEW
= wxNewId();
55 BEGIN_EVENT_TABLE(wxListbook
, wxBookCtrlBase
)
56 EVT_SIZE(wxListbook::OnSize
)
57 EVT_LIST_ITEM_SELECTED(wxID_LISTBOOKLISTVIEW
, wxListbook::OnListSelected
)
60 // ============================================================================
61 // wxListbook implementation
62 // ============================================================================
64 // ----------------------------------------------------------------------------
65 // wxListbook creation
66 // ----------------------------------------------------------------------------
68 void wxListbook::Init()
70 m_selection
= wxNOT_FOUND
;
74 wxListbook::Create(wxWindow
*parent
,
81 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
87 #endif // __WXMAC__/!__WXMAC__
90 // no border for this control, it doesn't look nice together with
92 style
&= ~wxBORDER_MASK
;
93 style
|= wxBORDER_NONE
;
95 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
96 wxDefaultValidator
, name
) )
99 m_bookctrl
= new wxListView
102 wxID_LISTBOOKLISTVIEW
,
105 wxLC_ICON
| wxLC_SINGLE_SEL
|
106 (IsVertical() ? wxLC_ALIGN_LEFT
: wxLC_ALIGN_TOP
)
110 // On XP with themes enabled the GetViewRect used in GetControllerSize() to
111 // determine the space needed for the list view will incorrectly return
112 // (0,0,0,0) the first time. So send a pending event so OnSize will be
113 // called again after the window is ready to go. Technically we don't
114 // need to do this on non-XP windows, but if things are already sized
115 // correctly then nothing changes and so there is no harm.
117 GetEventHandler()->AddPendingEvent(evt
);
122 // ----------------------------------------------------------------------------
123 // wxListbook geometry management
124 // ----------------------------------------------------------------------------
126 wxSize
wxListbook::GetControllerSize() const
128 const wxSize sizeClient
= GetClientSize(),
129 sizeBorder
= m_bookctrl
->GetSize() - m_bookctrl
->GetClientSize(),
130 sizeList
= GetListView()->GetViewRect().GetSize() + sizeBorder
;
136 size
.x
= sizeClient
.x
;
139 else // left/right aligned
142 size
.y
= sizeClient
.y
;
148 void wxListbook::OnSize(wxSizeEvent
& event
)
150 // arrange the icons before calling SetClientSize(), otherwise it wouldn't
151 // account for the scrollbars the list control might need and, at least
152 // under MSW, we'd finish with an ugly looking list control with both
153 // vertical and horizontal scrollbar (with one of them being added because
154 // the other one is not accounted for in client size computations)
155 wxListView
*list
= GetListView();
156 if (list
) list
->Arrange();
157 wxBookCtrlBase::OnSize(event
);
160 wxSize
wxListbook::CalcSizeFromPage(const wxSize
& sizePage
) const
162 // we need to add the size of the list control and the border between
163 const wxSize sizeList
= GetControllerSize();
165 wxSize size
= sizePage
;
168 size
.y
+= sizeList
.y
+ GetInternalBorder();
170 else // left/right aligned
172 size
.x
+= sizeList
.x
+ GetInternalBorder();
179 // ----------------------------------------------------------------------------
180 // accessing the pages
181 // ----------------------------------------------------------------------------
183 bool wxListbook::SetPageText(size_t n
, const wxString
& strText
)
185 GetListView()->SetItemText(n
, strText
);
190 wxString
wxListbook::GetPageText(size_t n
) const
192 return GetListView()->GetItemText(n
);
195 int wxListbook::GetPageImage(size_t WXUNUSED(n
)) const
197 wxFAIL_MSG( _T("wxListbook::GetPageImage() not implemented") );
202 bool wxListbook::SetPageImage(size_t n
, int imageId
)
204 return GetListView()->SetItemImage(n
, imageId
);
207 // ----------------------------------------------------------------------------
209 // ----------------------------------------------------------------------------
211 void wxListbook::SetImageList(wxImageList
*imageList
)
213 GetListView()->SetImageList(imageList
, wxIMAGE_LIST_NORMAL
);
215 wxBookCtrlBase::SetImageList(imageList
);
218 // ----------------------------------------------------------------------------
220 // ----------------------------------------------------------------------------
222 int wxListbook::GetSelection() const
227 int wxListbook::SetSelection(size_t n
)
229 wxCHECK_MSG( IS_VALID_PAGE(n
), wxNOT_FOUND
,
230 wxT("invalid page index in wxListbook::SetSelection()") );
232 const int oldSel
= m_selection
;
234 if ( int(n
) != m_selection
)
236 wxListbookEvent
event(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING
, m_windowId
);
237 event
.SetSelection(n
);
238 event
.SetOldSelection(m_selection
);
239 event
.SetEventObject(this);
240 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
242 if ( m_selection
!= wxNOT_FOUND
)
243 m_pages
[m_selection
]->Hide();
245 wxWindow
*page
= m_pages
[n
];
246 page
->SetSize(GetPageRect());
249 // change m_selection now to ignore the selection change event
251 GetListView()->Select(n
);
252 GetListView()->Focus(n
);
254 // program allows the page change
255 event
.SetEventType(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED
);
256 (void)GetEventHandler()->ProcessEvent(event
);
263 // ----------------------------------------------------------------------------
264 // adding/removing the pages
265 // ----------------------------------------------------------------------------
268 wxListbook::InsertPage(size_t n
,
270 const wxString
& text
,
274 if ( !wxBookCtrlBase::InsertPage(n
, page
, text
, bSelect
, imageId
) )
277 GetListView()->InsertItem(n
, text
, imageId
);
279 // if the inserted page is before the selected one, we must update the
280 // index of the selected page
281 if ( int(n
) <= m_selection
)
283 // one extra page added
285 GetListView()->Select(m_selection
);
286 GetListView()->Focus(m_selection
);
289 // some page should be selected: either this one or the first one if there
290 // is still no selection
294 else if ( m_selection
== -1 )
297 if ( selNew
!= m_selection
)
301 SetSelection(selNew
);
303 InvalidateBestSize();
304 GetListView()->Arrange();
308 wxWindow
*wxListbook::DoRemovePage(size_t page
)
310 const size_t page_count
= GetPageCount();
311 wxWindow
*win
= wxBookCtrlBase::DoRemovePage(page
);
315 GetListView()->DeleteItem(page
);
317 if (m_selection
>= (int)page
)
319 // force new sel valid if possible
320 int sel
= m_selection
- 1;
323 else if ((page_count
== 2) || (sel
== -1))
326 // force sel invalid if deleting current page - don't try to hide it
327 m_selection
= (m_selection
== (int)page
) ? wxNOT_FOUND
: m_selection
- 1;
329 if ((sel
!= wxNOT_FOUND
) && (sel
!= m_selection
))
333 GetListView()->Arrange();
340 bool wxListbook::DeleteAllPages()
342 GetListView()->DeleteAllItems();
343 return wxBookCtrlBase::DeleteAllPages();
346 // ----------------------------------------------------------------------------
348 // ----------------------------------------------------------------------------
350 void wxListbook::OnListSelected(wxListEvent
& eventList
)
352 const int selNew
= eventList
.GetIndex();
354 if ( selNew
== m_selection
)
356 // this event can only come from our own Select(m_selection) below
357 // which we call when the page change is vetoed, so we should simply
362 SetSelection(selNew
);
364 // change wasn't allowed, return to previous state
365 if (m_selection
!= selNew
)
367 GetListView()->Select(m_selection
);
368 GetListView()->Focus(m_selection
);
372 #endif // wxUSE_LISTBOOK