]> git.saurik.com Git - wxWidgets.git/blame - src/generic/listbkg.cpp
avoid populating the array with NULL controls during creation (SetWindowVariant calli...
[wxWidgets.git] / src / generic / listbkg.cpp
CommitLineData
e9c0df38 1///////////////////////////////////////////////////////////////////////////////
2ddb4d13 2// Name: src/generic/listbkg.cpp
e9c0df38
VZ
3// Purpose: generic implementation of wxListbook
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 19.08.03
7// RCS-ID: $Id$
8// Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
65571936 9// Licence: wxWindows licence
e9c0df38
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
e9c0df38
VZ
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
27#if wxUSE_LISTBOOK
28
9eddec69
WS
29#include "wx/listbook.h"
30
31#ifndef WX_PRECOMP
32 #include "wx/settings.h"
33#endif
34
e9c0df38
VZ
35#include "wx/listctrl.h"
36#include "wx/statline.h"
5b24f0d3 37#include "wx/imaglist.h"
e9c0df38 38
0dc44daa
VZ
39#include "wx/sysopt.h"
40
41namespace
42{
43
44// FIXME: This function exists because native OS X wxListCtrl seems to have
45// problems with report view, either imperfect display or reported hanging, so
46// disable it for now (but it should be fixed, and this function removed).
47bool CanUseReportView()
48{
49#if defined(__WXMAC__) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON
50 if (wxSystemOptions::GetOptionInt(wxMAC_ALWAYS_USE_GENERIC_LISTCTRL) == 0)
51 return false;
52 else
1a9a6eed 53#endif
0dc44daa
VZ
54 return true;
55}
56
57} // anonymous namespace
1a9a6eed 58
e9c0df38 59// ----------------------------------------------------------------------------
77ffb593 60// various wxWidgets macros
e9c0df38
VZ
61// ----------------------------------------------------------------------------
62
1f30c176
WS
63// check that the page index is valid
64#define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
65
66// ----------------------------------------------------------------------------
67// event table
68// ----------------------------------------------------------------------------
69
2ddb4d13 70IMPLEMENT_DYNAMIC_CLASS(wxListbook, wxBookCtrlBase)
e9c0df38 71
9b11752c
VZ
72wxDEFINE_EVENT( wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING, wxBookCtrlEvent );
73wxDEFINE_EVENT( wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED, wxBookCtrlEvent );
e9c0df38 74
61c083e7 75BEGIN_EVENT_TABLE(wxListbook, wxBookCtrlBase)
e9c0df38 76 EVT_SIZE(wxListbook::OnSize)
447325a4 77 EVT_LIST_ITEM_SELECTED(wxID_ANY, wxListbook::OnListSelected)
e9c0df38
VZ
78END_EVENT_TABLE()
79
80// ============================================================================
81// wxListbook implementation
82// ============================================================================
83
84// ----------------------------------------------------------------------------
85// wxListbook creation
86// ----------------------------------------------------------------------------
87
e9c0df38
VZ
88bool
89wxListbook::Create(wxWindow *parent,
90 wxWindowID id,
91 const wxPoint& pos,
92 const wxSize& size,
93 long style,
94 const wxString& name)
95{
2ddb4d13 96 if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT )
e9c0df38
VZ
97 {
98#ifdef __WXMAC__
2ddb4d13 99 style |= wxBK_TOP;
e9c0df38 100#else // !__WXMAC__
2ddb4d13 101 style |= wxBK_LEFT;
e9c0df38
VZ
102#endif // __WXMAC__/!__WXMAC__
103 }
104
ef0120c1
VZ
105 // no border for this control, it doesn't look nice together with
106 // wxListCtrl border
107 style &= ~wxBORDER_MASK;
108 style |= wxBORDER_NONE;
109
e9c0df38
VZ
110 if ( !wxControl::Create(parent, id, pos, size, style,
111 wxDefaultValidator, name) )
112 return false;
113
2ddb4d13 114 m_bookctrl = new wxListView
e9c0df38
VZ
115 (
116 this,
447325a4 117 wxID_ANY,
e9c0df38
VZ
118 wxDefaultPosition,
119 wxDefaultSize,
1a9a6eed 120 wxLC_SINGLE_SEL |
0dc44daa
VZ
121 (CanUseReportView() ? GetListCtrlReportViewFlags()
122 : GetListCtrlIconViewFlags())
e9c0df38
VZ
123 );
124
0dc44daa
VZ
125 if ( CanUseReportView() )
126 GetListView()->InsertColumn(0, wxT("Pages"));
c1718122 127
a9092ede 128#ifdef __WXMSW__
2ddb4d13 129 // On XP with themes enabled the GetViewRect used in GetControllerSize() to
a9092ede 130 // determine the space needed for the list view will incorrectly return
7b45094a 131 // (0,0,0,0) the first time. So send a pending event so OnSize will be
a9092ede
RD
132 // called again after the window is ready to go. Technically we don't
133 // need to do this on non-XP windows, but if things are already sized
134 // correctly then nothing changes and so there is no harm.
135 wxSizeEvent evt;
136 GetEventHandler()->AddPendingEvent(evt);
137#endif
e9c0df38
VZ
138 return true;
139}
140
1a9a6eed
VZ
141// ----------------------------------------------------------------------------
142// wxListCtrl flags
143// ----------------------------------------------------------------------------
144
145long wxListbook::GetListCtrlIconViewFlags() const
146{
147 return (IsVertical() ? wxLC_ALIGN_LEFT : wxLC_ALIGN_TOP) | wxLC_ICON;
148}
149
1a9a6eed
VZ
150long wxListbook::GetListCtrlReportViewFlags() const
151{
152 return wxLC_REPORT | wxLC_NO_HEADER;
153}
154
e9c0df38
VZ
155// ----------------------------------------------------------------------------
156// wxListbook geometry management
157// ----------------------------------------------------------------------------
158
e9c0df38
VZ
159void wxListbook::OnSize(wxSizeEvent& event)
160{
9f29226d
WS
161 // arrange the icons before calling SetClientSize(), otherwise it wouldn't
162 // account for the scrollbars the list control might need and, at least
163 // under MSW, we'd finish with an ugly looking list control with both
164 // vertical and horizontal scrollbar (with one of them being added because
165 // the other one is not accounted for in client size computations)
905069b1
VZ
166 wxListView * const list = GetListView();
167 if ( list )
168 list->Arrange();
169
170 event.Skip();
e9c0df38
VZ
171}
172
d0a84b63 173int wxListbook::HitTest(const wxPoint& pt, long *flags) const
851b88c3
VZ
174{
175 int pagePos = wxNOT_FOUND;
176
d0a84b63 177 if ( flags )
9804d540 178 *flags = wxBK_HITTEST_NOWHERE;
851b88c3 179
d0a84b63
VZ
180 // convert from listbook control coordinates to list control coordinates
181 const wxListView * const list = GetListView();
182 const wxPoint listPt = list->ScreenToClient(ClientToScreen(pt));
183
184 // is the point inside list control?
22a35096 185 if ( wxRect(list->GetSize()).Contains(listPt) )
851b88c3
VZ
186 {
187 int flagsList;
d0a84b63 188 pagePos = list->HitTest(listPt, flagsList);
851b88c3 189
d0a84b63 190 if ( flags )
851b88c3 191 {
d0a84b63
VZ
192 if ( pagePos != wxNOT_FOUND )
193 *flags = 0;
194
195 if ( flagsList & (wxLIST_HITTEST_ONITEMICON |
196 wxLIST_HITTEST_ONITEMSTATEICON ) )
9804d540 197 *flags |= wxBK_HITTEST_ONICON;
d0a84b63
VZ
198
199 if ( flagsList & wxLIST_HITTEST_ONITEMLABEL )
9804d540 200 *flags |= wxBK_HITTEST_ONLABEL;
851b88c3
VZ
201 }
202 }
d0a84b63
VZ
203 else // not over list control at all
204 {
22a35096 205 if ( flags && GetPageRect().Contains(pt) )
9804d540 206 *flags |= wxBK_HITTEST_ONPAGE;
d0a84b63 207 }
851b88c3
VZ
208
209 return pagePos;
210}
211
905069b1
VZ
212void wxListbook::UpdateSize()
213{
214 // we should find a more elegant way to force a layout than generating this
215 // dummy event
216 wxSizeEvent sz(GetSize(), GetId());
217 GetEventHandler()->ProcessEvent(sz);
218}
e9c0df38
VZ
219
220// ----------------------------------------------------------------------------
221// accessing the pages
222// ----------------------------------------------------------------------------
223
224bool wxListbook::SetPageText(size_t n, const wxString& strText)
225{
2ddb4d13 226 GetListView()->SetItemText(n, strText);
e9c0df38
VZ
227
228 return true;
229}
230
231wxString wxListbook::GetPageText(size_t n) const
232{
2ddb4d13 233 return GetListView()->GetItemText(n);
e9c0df38
VZ
234}
235
c1718122 236int wxListbook::GetPageImage(size_t n) const
e9c0df38 237{
c1718122
VZ
238 wxListItem item;
239 item.SetId(n);
e9c0df38 240
c1718122
VZ
241 if (GetListView()->GetItem(item))
242 {
243 return item.GetImage();
244 }
245 else
246 {
247 return wxNOT_FOUND;
248 }
e9c0df38
VZ
249}
250
251bool wxListbook::SetPageImage(size_t n, int imageId)
252{
2ddb4d13 253 return GetListView()->SetItemImage(n, imageId);
e9c0df38
VZ
254}
255
256// ----------------------------------------------------------------------------
257// image list stuff
258// ----------------------------------------------------------------------------
259
260void wxListbook::SetImageList(wxImageList *imageList)
261{
0dc44daa 262 if ( CanUseReportView() )
c1718122 263 {
0dc44daa 264 wxListView * const list = GetListView();
c1718122 265
0dc44daa
VZ
266 // If imageList presence has changed, we update the list control view
267 if ( (imageList != NULL) != (GetImageList() != NULL) )
c1718122 268 {
0dc44daa
VZ
269 // Preserve the selection which is lost when changing the mode
270 const int oldSel = GetSelection();
271
272 // Update the style to use icon view for images, report view otherwise
273 long style = wxLC_SINGLE_SEL;
274 if ( imageList )
275 {
276 style |= GetListCtrlIconViewFlags();
277 }
278 else // no image list
279 {
280 style |= GetListCtrlReportViewFlags();
281 }
282
283 list->SetWindowStyleFlag(style);
284 if ( !imageList )
285 list->InsertColumn(0, wxT("Pages"));
286
287 // Restore selection
288 if ( oldSel != wxNOT_FOUND )
289 SetSelection(oldSel);
c1718122
VZ
290 }
291
0dc44daa 292 list->SetImageList(imageList, wxIMAGE_LIST_NORMAL);
c1718122
VZ
293 }
294
61c083e7 295 wxBookCtrlBase::SetImageList(imageList);
e9c0df38
VZ
296}
297
298// ----------------------------------------------------------------------------
299// selection
300// ----------------------------------------------------------------------------
301
1d6fcbcc
VZ
302void wxListbook::UpdateSelectedPage(size_t newsel)
303{
304 m_selection = newsel;
305 GetListView()->Select(newsel);
306 GetListView()->Focus(newsel);
307}
308
3e97a905 309wxBookCtrlEvent* wxListbook::CreatePageChangingEvent() const
e9c0df38 310{
3e97a905 311 return new wxBookCtrlEvent(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING, m_windowId);
e9c0df38
VZ
312}
313
3e97a905 314void wxListbook::MakeChangedEvent(wxBookCtrlEvent &event)
deb325e3
VZ
315{
316 event.SetEventType(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED);
317}
318
319
e9c0df38
VZ
320// ----------------------------------------------------------------------------
321// adding/removing the pages
322// ----------------------------------------------------------------------------
323
324bool
325wxListbook::InsertPage(size_t n,
326 wxWindow *page,
327 const wxString& text,
328 bool bSelect,
329 int imageId)
330{
61c083e7 331 if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) )
e9c0df38
VZ
332 return false;
333
2ddb4d13 334 GetListView()->InsertItem(n, text, imageId);
e9c0df38 335
716dc245
WS
336 // if the inserted page is before the selected one, we must update the
337 // index of the selected page
338 if ( int(n) <= m_selection )
e9c0df38 339 {
42841dfc 340 // one extra page added
716dc245 341 m_selection++;
2ddb4d13
WS
342 GetListView()->Select(m_selection);
343 GetListView()->Focus(m_selection);
e9c0df38 344 }
716dc245 345
60d5c563 346 if ( !DoSetSelectionAfterInsertion(n, bSelect) )
e9c0df38 347 page->Hide();
716dc245 348
905069b1
VZ
349 UpdateSize();
350
e9c0df38
VZ
351 return true;
352}
353
354wxWindow *wxListbook::DoRemovePage(size_t page)
355{
4a10ea8b 356 const size_t page_count = GetPageCount();
61c083e7 357 wxWindow *win = wxBookCtrlBase::DoRemovePage(page);
bb08a4a1 358
e9c0df38
VZ
359 if ( win )
360 {
2ddb4d13 361 GetListView()->DeleteItem(page);
33ebfc3b
VZ
362
363 if (m_selection >= (int)page)
364 {
365 // force new sel valid if possible
366 int sel = m_selection - 1;
367 if (page_count == 1)
bb08a4a1 368 sel = wxNOT_FOUND;
7e837615 369 else if ((page_count == 2) || (sel == wxNOT_FOUND))
33ebfc3b 370 sel = 0;
bb08a4a1 371
33ebfc3b 372 // force sel invalid if deleting current page - don't try to hide it
bb08a4a1
WS
373 m_selection = (m_selection == (int)page) ? wxNOT_FOUND : m_selection - 1;
374
375 if ((sel != wxNOT_FOUND) && (sel != m_selection))
33ebfc3b
VZ
376 SetSelection(sel);
377 }
9f29226d 378
2ddb4d13 379 GetListView()->Arrange();
905069b1 380 UpdateSize();
e9c0df38
VZ
381 }
382
383 return win;
384}
385
fbd11d30
RD
386
387bool wxListbook::DeleteAllPages()
388{
2ddb4d13 389 GetListView()->DeleteAllItems();
6a82a0d0
JS
390 if (!wxBookCtrlBase::DeleteAllPages())
391 return false;
9eddec69 392
905069b1 393 UpdateSize();
9eddec69 394
6a82a0d0 395 return true;
fbd11d30
RD
396}
397
e9c0df38
VZ
398// ----------------------------------------------------------------------------
399// wxListbook events
400// ----------------------------------------------------------------------------
401
402void wxListbook::OnListSelected(wxListEvent& eventList)
403{
447325a4
VZ
404 if ( eventList.GetEventObject() != m_bookctrl )
405 {
406 eventList.Skip();
407 return;
408 }
409
e9c0df38
VZ
410 const int selNew = eventList.GetIndex();
411
412 if ( selNew == m_selection )
413 {
414 // this event can only come from our own Select(m_selection) below
415 // which we call when the page change is vetoed, so we should simply
416 // ignore it
417 return;
418 }
419
716dc245 420 SetSelection(selNew);
e9c0df38 421
716dc245
WS
422 // change wasn't allowed, return to previous state
423 if (m_selection != selNew)
e9c0df38 424 {
2ddb4d13
WS
425 GetListView()->Select(m_selection);
426 GetListView()->Focus(m_selection);
e9c0df38 427 }
e9c0df38
VZ
428}
429
430#endif // wxUSE_LISTBOOK