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 const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING
= wxNewEventType();
50 const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED
= wxNewEventType();
51 const int wxID_LISTBOOKLISTVIEW
= wxNewId();
53 BEGIN_EVENT_TABLE(wxListbook
, wxBookCtrlBase
)
54 EVT_SIZE(wxListbook::OnSize
)
55 EVT_LIST_ITEM_SELECTED(wxID_LISTBOOKLISTVIEW
, wxListbook::OnListSelected
)
58 // ============================================================================
59 // wxListbook implementation
60 // ============================================================================
62 // ----------------------------------------------------------------------------
63 // wxListbook creation
64 // ----------------------------------------------------------------------------
66 void wxListbook::Init()
68 m_selection
= wxNOT_FOUND
;
72 wxListbook::Create(wxWindow
*parent
,
79 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
85 #endif // __WXMAC__/!__WXMAC__
88 // no border for this control, it doesn't look nice together with
90 style
&= ~wxBORDER_MASK
;
91 style
|= wxBORDER_NONE
;
93 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
94 wxDefaultValidator
, name
) )
97 m_bookctrl
= new wxListView
100 wxID_LISTBOOKLISTVIEW
,
103 wxLC_ICON
| wxLC_SINGLE_SEL
|
104 (IsVertical() ? wxLC_ALIGN_LEFT
: wxLC_ALIGN_TOP
)
108 // On XP with themes enabled the GetViewRect used in GetControllerSize() to
109 // determine the space needed for the list view will incorrectly return
110 // (0,0,0,0) the first time. So send a pending event so OnSize will be
111 // called again after the window is ready to go. Technically we don't
112 // need to do this on non-XP windows, but if things are already sized
113 // correctly then nothing changes and so there is no harm.
115 GetEventHandler()->AddPendingEvent(evt
);
120 // ----------------------------------------------------------------------------
121 // wxListbook geometry management
122 // ----------------------------------------------------------------------------
124 wxSize
wxListbook::GetControllerSize() const
126 const wxSize sizeClient
= GetClientSize(),
127 sizeBorder
= m_bookctrl
->GetSize() - m_bookctrl
->GetClientSize(),
128 sizeList
= GetListView()->GetViewRect().GetSize() + sizeBorder
;
134 size
.x
= sizeClient
.x
;
137 else // left/right aligned
140 size
.y
= sizeClient
.y
;
146 void wxListbook::OnSize(wxSizeEvent
& event
)
148 // arrange the icons before calling SetClientSize(), otherwise it wouldn't
149 // account for the scrollbars the list control might need and, at least
150 // under MSW, we'd finish with an ugly looking list control with both
151 // vertical and horizontal scrollbar (with one of them being added because
152 // the other one is not accounted for in client size computations)
153 wxListView
*list
= GetListView();
154 if (list
) list
->Arrange();
155 wxBookCtrlBase::OnSize(event
);
158 wxSize
wxListbook::CalcSizeFromPage(const wxSize
& sizePage
) const
160 // we need to add the size of the list control and the border between
161 const wxSize sizeList
= GetControllerSize();
163 wxSize size
= sizePage
;
166 size
.y
+= sizeList
.y
+ GetInternalBorder();
168 else // left/right aligned
170 size
.x
+= sizeList
.x
+ GetInternalBorder();
177 // ----------------------------------------------------------------------------
178 // accessing the pages
179 // ----------------------------------------------------------------------------
181 bool wxListbook::SetPageText(size_t n
, const wxString
& strText
)
183 GetListView()->SetItemText(n
, strText
);
188 wxString
wxListbook::GetPageText(size_t n
) const
190 return GetListView()->GetItemText(n
);
193 int wxListbook::GetPageImage(size_t WXUNUSED(n
)) const
195 wxFAIL_MSG( _T("wxListbook::GetPageImage() not implemented") );
200 bool wxListbook::SetPageImage(size_t n
, int imageId
)
202 return GetListView()->SetItemImage(n
, imageId
);
205 // ----------------------------------------------------------------------------
207 // ----------------------------------------------------------------------------
209 void wxListbook::SetImageList(wxImageList
*imageList
)
211 GetListView()->SetImageList(imageList
, wxIMAGE_LIST_NORMAL
);
213 wxBookCtrlBase::SetImageList(imageList
);
216 // ----------------------------------------------------------------------------
218 // ----------------------------------------------------------------------------
220 int wxListbook::GetSelection() const
225 int wxListbook::SetSelection(size_t n
)
227 wxCHECK_MSG( IS_VALID_PAGE(n
), wxNOT_FOUND
,
228 wxT("invalid page index in wxListbook::SetSelection()") );
230 const int oldSel
= m_selection
;
232 if ( int(n
) != m_selection
)
234 wxListbookEvent
event(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING
, m_windowId
);
235 event
.SetSelection(n
);
236 event
.SetOldSelection(m_selection
);
237 event
.SetEventObject(this);
238 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
240 if ( m_selection
!= wxNOT_FOUND
)
241 m_pages
[m_selection
]->Hide();
243 wxWindow
*page
= m_pages
[n
];
244 page
->SetSize(GetPageRect());
247 // change m_selection now to ignore the selection change event
249 GetListView()->Select(n
);
250 GetListView()->Focus(n
);
252 // program allows the page change
253 event
.SetEventType(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED
);
254 (void)GetEventHandler()->ProcessEvent(event
);
261 // ----------------------------------------------------------------------------
262 // adding/removing the pages
263 // ----------------------------------------------------------------------------
266 wxListbook::InsertPage(size_t n
,
268 const wxString
& text
,
272 if ( !wxBookCtrlBase::InsertPage(n
, page
, text
, bSelect
, imageId
) )
275 GetListView()->InsertItem(n
, text
, imageId
);
277 // if the inserted page is before the selected one, we must update the
278 // index of the selected page
279 if ( int(n
) <= m_selection
)
281 // one extra page added
283 GetListView()->Select(m_selection
);
284 GetListView()->Focus(m_selection
);
287 // some page should be selected: either this one or the first one if there
288 // is still no selection
292 else if ( m_selection
== -1 )
295 if ( selNew
!= m_selection
)
299 SetSelection(selNew
);
301 InvalidateBestSize();
302 GetListView()->Arrange();
306 wxWindow
*wxListbook::DoRemovePage(size_t page
)
308 const size_t page_count
= GetPageCount();
309 wxWindow
*win
= wxBookCtrlBase::DoRemovePage(page
);
313 GetListView()->DeleteItem(page
);
315 if (m_selection
>= (int)page
)
317 // force new sel valid if possible
318 int sel
= m_selection
- 1;
321 else if ((page_count
== 2) || (sel
== -1))
324 // force sel invalid if deleting current page - don't try to hide it
325 m_selection
= (m_selection
== (int)page
) ? wxNOT_FOUND
: m_selection
- 1;
327 if ((sel
!= wxNOT_FOUND
) && (sel
!= m_selection
))
331 GetListView()->Arrange();
338 bool wxListbook::DeleteAllPages()
340 GetListView()->DeleteAllItems();
341 return wxBookCtrlBase::DeleteAllPages();
344 // ----------------------------------------------------------------------------
346 // ----------------------------------------------------------------------------
348 void wxListbook::OnListSelected(wxListEvent
& eventList
)
350 const int selNew
= eventList
.GetIndex();
352 if ( selNew
== m_selection
)
354 // this event can only come from our own Select(m_selection) below
355 // which we call when the page change is vetoed, so we should simply
360 SetSelection(selNew
);
362 // change wasn't allowed, return to previous state
363 if (m_selection
!= selNew
)
365 GetListView()->Select(m_selection
);
366 GetListView()->Focus(m_selection
);
370 #endif // wxUSE_LISTBOOK