]> git.saurik.com Git - wxWidgets.git/blame - src/generic/listbkg.cpp
don't pass pointers to unaligned DWORDs to avoid 64 bit build problems (#9726)
[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
1a9a6eed
VZ
39// FIXME: native OS X wxListCtrl hangs if this code is used for it so disable
40// it for now
41#if !defined(__WXMAC__)
42 #define CAN_USE_REPORT_VIEW
43#endif
44
e9c0df38 45// ----------------------------------------------------------------------------
77ffb593 46// various wxWidgets macros
e9c0df38
VZ
47// ----------------------------------------------------------------------------
48
1f30c176
WS
49// check that the page index is valid
50#define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
51
52// ----------------------------------------------------------------------------
53// event table
54// ----------------------------------------------------------------------------
55
2ddb4d13 56IMPLEMENT_DYNAMIC_CLASS(wxListbook, wxBookCtrlBase)
e9c0df38
VZ
57IMPLEMENT_DYNAMIC_CLASS(wxListbookEvent, wxNotifyEvent)
58
59const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING = wxNewEventType();
60const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED = wxNewEventType();
61
61c083e7 62BEGIN_EVENT_TABLE(wxListbook, wxBookCtrlBase)
e9c0df38 63 EVT_SIZE(wxListbook::OnSize)
447325a4 64 EVT_LIST_ITEM_SELECTED(wxID_ANY, wxListbook::OnListSelected)
e9c0df38
VZ
65END_EVENT_TABLE()
66
67// ============================================================================
68// wxListbook implementation
69// ============================================================================
70
71// ----------------------------------------------------------------------------
72// wxListbook creation
73// ----------------------------------------------------------------------------
74
75void wxListbook::Init()
76{
e9c0df38
VZ
77 m_selection = wxNOT_FOUND;
78}
79
80bool
81wxListbook::Create(wxWindow *parent,
82 wxWindowID id,
83 const wxPoint& pos,
84 const wxSize& size,
85 long style,
86 const wxString& name)
87{
2ddb4d13 88 if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT )
e9c0df38
VZ
89 {
90#ifdef __WXMAC__
2ddb4d13 91 style |= wxBK_TOP;
e9c0df38 92#else // !__WXMAC__
2ddb4d13 93 style |= wxBK_LEFT;
e9c0df38
VZ
94#endif // __WXMAC__/!__WXMAC__
95 }
96
ef0120c1
VZ
97 // no border for this control, it doesn't look nice together with
98 // wxListCtrl border
99 style &= ~wxBORDER_MASK;
100 style |= wxBORDER_NONE;
101
e9c0df38
VZ
102 if ( !wxControl::Create(parent, id, pos, size, style,
103 wxDefaultValidator, name) )
104 return false;
105
2ddb4d13 106 m_bookctrl = new wxListView
e9c0df38
VZ
107 (
108 this,
447325a4 109 wxID_ANY,
e9c0df38
VZ
110 wxDefaultPosition,
111 wxDefaultSize,
1a9a6eed
VZ
112 wxLC_SINGLE_SEL |
113#ifdef CAN_USE_REPORT_VIEW
114 GetListCtrlReportViewFlags()
115#else // !CAN_USE_REPORT_VIEW
116 GetListCtrlIconViewFlags()
117#endif // CAN_USE_REPORT_VIEW/!CAN_USE_REPORT_VIEW
e9c0df38
VZ
118 );
119
1a9a6eed 120#ifdef CAN_USE_REPORT_VIEW
c1718122 121 GetListView()->InsertColumn(0, wxT("Pages"));
1a9a6eed 122#endif // CAN_USE_REPORT_VIEW
c1718122 123
a9092ede 124#ifdef __WXMSW__
2ddb4d13 125 // On XP with themes enabled the GetViewRect used in GetControllerSize() to
a9092ede 126 // determine the space needed for the list view will incorrectly return
7b45094a 127 // (0,0,0,0) the first time. So send a pending event so OnSize will be
a9092ede
RD
128 // called again after the window is ready to go. Technically we don't
129 // need to do this on non-XP windows, but if things are already sized
130 // correctly then nothing changes and so there is no harm.
131 wxSizeEvent evt;
132 GetEventHandler()->AddPendingEvent(evt);
133#endif
e9c0df38
VZ
134 return true;
135}
136
1a9a6eed
VZ
137// ----------------------------------------------------------------------------
138// wxListCtrl flags
139// ----------------------------------------------------------------------------
140
141long wxListbook::GetListCtrlIconViewFlags() const
142{
143 return (IsVertical() ? wxLC_ALIGN_LEFT : wxLC_ALIGN_TOP) | wxLC_ICON;
144}
145
146#ifdef CAN_USE_REPORT_VIEW
147
148long wxListbook::GetListCtrlReportViewFlags() const
149{
150 return wxLC_REPORT | wxLC_NO_HEADER;
151}
152
153#endif // CAN_USE_REPORT_VIEW
154
e9c0df38
VZ
155// ----------------------------------------------------------------------------
156// wxListbook geometry management
157// ----------------------------------------------------------------------------
158
2ddb4d13 159wxSize wxListbook::GetControllerSize() const
e9c0df38 160{
4a06b348 161 const wxSize sizeClient = GetClientSize(),
2ddb4d13
WS
162 sizeBorder = m_bookctrl->GetSize() - m_bookctrl->GetClientSize(),
163 sizeList = GetListView()->GetViewRect().GetSize() + sizeBorder;
e9c0df38
VZ
164
165 wxSize size;
2ddb4d13 166
e9c0df38
VZ
167 if ( IsVertical() )
168 {
169 size.x = sizeClient.x;
4a06b348 170 size.y = sizeList.y;
e9c0df38
VZ
171 }
172 else // left/right aligned
173 {
4a06b348 174 size.x = sizeList.x;
e9c0df38
VZ
175 size.y = sizeClient.y;
176 }
177
178 return size;
179}
180
e9c0df38
VZ
181void wxListbook::OnSize(wxSizeEvent& event)
182{
9f29226d
WS
183 // arrange the icons before calling SetClientSize(), otherwise it wouldn't
184 // account for the scrollbars the list control might need and, at least
185 // under MSW, we'd finish with an ugly looking list control with both
186 // vertical and horizontal scrollbar (with one of them being added because
187 // the other one is not accounted for in client size computations)
905069b1
VZ
188 wxListView * const list = GetListView();
189 if ( list )
190 list->Arrange();
191
192 event.Skip();
e9c0df38
VZ
193}
194
d0a84b63 195int wxListbook::HitTest(const wxPoint& pt, long *flags) const
851b88c3
VZ
196{
197 int pagePos = wxNOT_FOUND;
198
d0a84b63 199 if ( flags )
9804d540 200 *flags = wxBK_HITTEST_NOWHERE;
851b88c3 201
d0a84b63
VZ
202 // convert from listbook control coordinates to list control coordinates
203 const wxListView * const list = GetListView();
204 const wxPoint listPt = list->ScreenToClient(ClientToScreen(pt));
205
206 // is the point inside list control?
22a35096 207 if ( wxRect(list->GetSize()).Contains(listPt) )
851b88c3
VZ
208 {
209 int flagsList;
d0a84b63 210 pagePos = list->HitTest(listPt, flagsList);
851b88c3 211
d0a84b63 212 if ( flags )
851b88c3 213 {
d0a84b63
VZ
214 if ( pagePos != wxNOT_FOUND )
215 *flags = 0;
216
217 if ( flagsList & (wxLIST_HITTEST_ONITEMICON |
218 wxLIST_HITTEST_ONITEMSTATEICON ) )
9804d540 219 *flags |= wxBK_HITTEST_ONICON;
d0a84b63
VZ
220
221 if ( flagsList & wxLIST_HITTEST_ONITEMLABEL )
9804d540 222 *flags |= wxBK_HITTEST_ONLABEL;
851b88c3
VZ
223 }
224 }
d0a84b63
VZ
225 else // not over list control at all
226 {
22a35096 227 if ( flags && GetPageRect().Contains(pt) )
9804d540 228 *flags |= wxBK_HITTEST_ONPAGE;
d0a84b63 229 }
851b88c3
VZ
230
231 return pagePos;
232}
233
e9c0df38
VZ
234wxSize wxListbook::CalcSizeFromPage(const wxSize& sizePage) const
235{
159e6235 236 // we need to add the size of the list control and the border between
2ddb4d13 237 const wxSize sizeList = GetControllerSize();
e9c0df38
VZ
238
239 wxSize size = sizePage;
240 if ( IsVertical() )
241 {
159e6235 242 size.y += sizeList.y + GetInternalBorder();
e9c0df38
VZ
243 }
244 else // left/right aligned
245 {
159e6235 246 size.x += sizeList.x + GetInternalBorder();
e9c0df38
VZ
247 }
248
249 return size;
250}
251
905069b1
VZ
252void wxListbook::UpdateSize()
253{
254 // we should find a more elegant way to force a layout than generating this
255 // dummy event
256 wxSizeEvent sz(GetSize(), GetId());
257 GetEventHandler()->ProcessEvent(sz);
258}
e9c0df38
VZ
259
260// ----------------------------------------------------------------------------
261// accessing the pages
262// ----------------------------------------------------------------------------
263
264bool wxListbook::SetPageText(size_t n, const wxString& strText)
265{
2ddb4d13 266 GetListView()->SetItemText(n, strText);
e9c0df38
VZ
267
268 return true;
269}
270
271wxString wxListbook::GetPageText(size_t n) const
272{
2ddb4d13 273 return GetListView()->GetItemText(n);
e9c0df38
VZ
274}
275
c1718122 276int wxListbook::GetPageImage(size_t n) const
e9c0df38 277{
c1718122
VZ
278 wxListItem item;
279 item.SetId(n);
e9c0df38 280
c1718122
VZ
281 if (GetListView()->GetItem(item))
282 {
283 return item.GetImage();
284 }
285 else
286 {
287 return wxNOT_FOUND;
288 }
e9c0df38
VZ
289}
290
291bool wxListbook::SetPageImage(size_t n, int imageId)
292{
2ddb4d13 293 return GetListView()->SetItemImage(n, imageId);
e9c0df38
VZ
294}
295
296// ----------------------------------------------------------------------------
297// image list stuff
298// ----------------------------------------------------------------------------
299
300void wxListbook::SetImageList(wxImageList *imageList)
301{
c1718122
VZ
302 wxListView * const list = GetListView();
303
1a9a6eed 304#ifdef CAN_USE_REPORT_VIEW
c1718122
VZ
305 // If imageList presence has changed, we update the list control view
306 if ( (imageList != NULL) != (GetImageList() != NULL) )
307 {
308 wxArrayString labels;
309 labels.Alloc(GetPageCount());
310
311 wxArrayInt imageIds;
312 imageIds.Alloc(GetPageCount());
313
314 const int oldSel = GetSelection();
315 size_t i;
316
317 // Grab snapshot of all list control items before changing the window
318 // style (which deletes the items)
319 for ( i = 0; i < GetPageCount(); i++ )
320 {
321 labels.Add(GetPageText(i));
322 imageIds.Add(GetPageImage(i));
323 }
324
325 // Update the style to use icon view for images, report view otherwise
326 long style = wxLC_SINGLE_SEL;
327 if ( imageList )
328 {
1a9a6eed 329 style |= GetListCtrlIconViewFlags();
c1718122
VZ
330 }
331 else // no image list
332 {
1a9a6eed 333 style |= GetListCtrlReportViewFlags();
c1718122
VZ
334 }
335
1a9a6eed
VZ
336 list->SetWindowStyleFlag(style);
337 if ( !imageList )
338 list->InsertColumn(0, wxT("Pages"));
339
c1718122
VZ
340 // Add back the list control items
341 for ( i = 0; i < GetPageCount(); i++ )
342 {
343 list->InsertItem(i, labels[i], imageIds[i]);
344 }
345
346 // Restore selection
347 if ( oldSel != wxNOT_FOUND )
348 SetSelection(oldSel);
349 }
350
351 list->SetImageList(imageList, wxIMAGE_LIST_NORMAL);
1a9a6eed 352#endif // CAN_USE_REPORT_VIEW
e9c0df38 353
61c083e7 354 wxBookCtrlBase::SetImageList(imageList);
e9c0df38
VZ
355}
356
357// ----------------------------------------------------------------------------
358// selection
359// ----------------------------------------------------------------------------
360
1d6fcbcc
VZ
361void wxListbook::UpdateSelectedPage(size_t newsel)
362{
363 m_selection = newsel;
364 GetListView()->Select(newsel);
365 GetListView()->Focus(newsel);
366}
367
e9c0df38
VZ
368int wxListbook::GetSelection() const
369{
370 return m_selection;
371}
372
deb325e3 373wxBookCtrlBaseEvent* wxListbook::CreatePageChangingEvent() const
e9c0df38 374{
deb325e3 375 return new wxListbookEvent(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING, m_windowId);
e9c0df38
VZ
376}
377
deb325e3
VZ
378void wxListbook::MakeChangedEvent(wxBookCtrlBaseEvent &event)
379{
380 event.SetEventType(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED);
381}
382
383
e9c0df38
VZ
384// ----------------------------------------------------------------------------
385// adding/removing the pages
386// ----------------------------------------------------------------------------
387
388bool
389wxListbook::InsertPage(size_t n,
390 wxWindow *page,
391 const wxString& text,
392 bool bSelect,
393 int imageId)
394{
61c083e7 395 if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) )
e9c0df38
VZ
396 return false;
397
2ddb4d13 398 GetListView()->InsertItem(n, text, imageId);
e9c0df38 399
716dc245
WS
400 // if the inserted page is before the selected one, we must update the
401 // index of the selected page
402 if ( int(n) <= m_selection )
e9c0df38 403 {
42841dfc 404 // one extra page added
716dc245 405 m_selection++;
2ddb4d13
WS
406 GetListView()->Select(m_selection);
407 GetListView()->Focus(m_selection);
e9c0df38 408 }
716dc245
WS
409
410 // some page should be selected: either this one or the first one if there
411 // is still no selection
412 int selNew = -1;
413 if ( bSelect )
414 selNew = n;
415 else if ( m_selection == -1 )
416 selNew = 0;
417
418 if ( selNew != m_selection )
e9c0df38 419 page->Hide();
716dc245
WS
420
421 if ( selNew != -1 )
422 SetSelection(selNew);
e9c0df38 423
905069b1
VZ
424 UpdateSize();
425
e9c0df38
VZ
426 return true;
427}
428
429wxWindow *wxListbook::DoRemovePage(size_t page)
430{
4a10ea8b 431 const size_t page_count = GetPageCount();
61c083e7 432 wxWindow *win = wxBookCtrlBase::DoRemovePage(page);
bb08a4a1 433
e9c0df38
VZ
434 if ( win )
435 {
2ddb4d13 436 GetListView()->DeleteItem(page);
33ebfc3b
VZ
437
438 if (m_selection >= (int)page)
439 {
440 // force new sel valid if possible
441 int sel = m_selection - 1;
442 if (page_count == 1)
bb08a4a1 443 sel = wxNOT_FOUND;
33ebfc3b
VZ
444 else if ((page_count == 2) || (sel == -1))
445 sel = 0;
bb08a4a1 446
33ebfc3b 447 // force sel invalid if deleting current page - don't try to hide it
bb08a4a1
WS
448 m_selection = (m_selection == (int)page) ? wxNOT_FOUND : m_selection - 1;
449
450 if ((sel != wxNOT_FOUND) && (sel != m_selection))
33ebfc3b
VZ
451 SetSelection(sel);
452 }
9f29226d 453
2ddb4d13 454 GetListView()->Arrange();
905069b1 455 UpdateSize();
e9c0df38
VZ
456 }
457
458 return win;
459}
460
fbd11d30
RD
461
462bool wxListbook::DeleteAllPages()
463{
2ddb4d13 464 GetListView()->DeleteAllItems();
6a82a0d0
JS
465 if (!wxBookCtrlBase::DeleteAllPages())
466 return false;
9eddec69 467
6a82a0d0
JS
468 m_selection = -1;
469
905069b1 470 UpdateSize();
9eddec69 471
6a82a0d0 472 return true;
fbd11d30
RD
473}
474
e9c0df38
VZ
475// ----------------------------------------------------------------------------
476// wxListbook events
477// ----------------------------------------------------------------------------
478
479void wxListbook::OnListSelected(wxListEvent& eventList)
480{
447325a4
VZ
481 if ( eventList.GetEventObject() != m_bookctrl )
482 {
483 eventList.Skip();
484 return;
485 }
486
e9c0df38
VZ
487 const int selNew = eventList.GetIndex();
488
489 if ( selNew == m_selection )
490 {
491 // this event can only come from our own Select(m_selection) below
492 // which we call when the page change is vetoed, so we should simply
493 // ignore it
494 return;
495 }
496
716dc245 497 SetSelection(selNew);
e9c0df38 498
716dc245
WS
499 // change wasn't allowed, return to previous state
500 if (m_selection != selNew)
e9c0df38 501 {
2ddb4d13
WS
502 GetListView()->Select(m_selection);
503 GetListView()->Focus(m_selection);
e9c0df38 504 }
e9c0df38
VZ
505}
506
507#endif // wxUSE_LISTBOOK