]> git.saurik.com Git - wxWidgets.git/blame_incremental - 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
1///////////////////////////////////////////////////////////////////////////////
2// Name: src/generic/listbkg.cpp
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>
9// Licence: wxWindows licence
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
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
29#include "wx/listbook.h"
30
31#ifndef WX_PRECOMP
32 #include "wx/settings.h"
33#endif
34
35#include "wx/listctrl.h"
36#include "wx/statline.h"
37#include "wx/imaglist.h"
38
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
45// ----------------------------------------------------------------------------
46// various wxWidgets macros
47// ----------------------------------------------------------------------------
48
49// check that the page index is valid
50#define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
51
52// ----------------------------------------------------------------------------
53// event table
54// ----------------------------------------------------------------------------
55
56IMPLEMENT_DYNAMIC_CLASS(wxListbook, wxBookCtrlBase)
57IMPLEMENT_DYNAMIC_CLASS(wxListbookEvent, wxNotifyEvent)
58
59const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING = wxNewEventType();
60const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED = wxNewEventType();
61
62BEGIN_EVENT_TABLE(wxListbook, wxBookCtrlBase)
63 EVT_SIZE(wxListbook::OnSize)
64 EVT_LIST_ITEM_SELECTED(wxID_ANY, wxListbook::OnListSelected)
65END_EVENT_TABLE()
66
67// ============================================================================
68// wxListbook implementation
69// ============================================================================
70
71// ----------------------------------------------------------------------------
72// wxListbook creation
73// ----------------------------------------------------------------------------
74
75void wxListbook::Init()
76{
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{
88 if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT )
89 {
90#ifdef __WXMAC__
91 style |= wxBK_TOP;
92#else // !__WXMAC__
93 style |= wxBK_LEFT;
94#endif // __WXMAC__/!__WXMAC__
95 }
96
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
102 if ( !wxControl::Create(parent, id, pos, size, style,
103 wxDefaultValidator, name) )
104 return false;
105
106 m_bookctrl = new wxListView
107 (
108 this,
109 wxID_ANY,
110 wxDefaultPosition,
111 wxDefaultSize,
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
118 );
119
120#ifdef CAN_USE_REPORT_VIEW
121 GetListView()->InsertColumn(0, wxT("Pages"));
122#endif // CAN_USE_REPORT_VIEW
123
124#ifdef __WXMSW__
125 // On XP with themes enabled the GetViewRect used in GetControllerSize() to
126 // determine the space needed for the list view will incorrectly return
127 // (0,0,0,0) the first time. So send a pending event so OnSize will be
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
134 return true;
135}
136
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
155// ----------------------------------------------------------------------------
156// wxListbook geometry management
157// ----------------------------------------------------------------------------
158
159wxSize wxListbook::GetControllerSize() const
160{
161 const wxSize sizeClient = GetClientSize(),
162 sizeBorder = m_bookctrl->GetSize() - m_bookctrl->GetClientSize(),
163 sizeList = GetListView()->GetViewRect().GetSize() + sizeBorder;
164
165 wxSize size;
166
167 if ( IsVertical() )
168 {
169 size.x = sizeClient.x;
170 size.y = sizeList.y;
171 }
172 else // left/right aligned
173 {
174 size.x = sizeList.x;
175 size.y = sizeClient.y;
176 }
177
178 return size;
179}
180
181void wxListbook::OnSize(wxSizeEvent& event)
182{
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)
188 wxListView * const list = GetListView();
189 if ( list )
190 list->Arrange();
191
192 event.Skip();
193}
194
195int wxListbook::HitTest(const wxPoint& pt, long *flags) const
196{
197 int pagePos = wxNOT_FOUND;
198
199 if ( flags )
200 *flags = wxBK_HITTEST_NOWHERE;
201
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?
207 if ( wxRect(list->GetSize()).Contains(listPt) )
208 {
209 int flagsList;
210 pagePos = list->HitTest(listPt, flagsList);
211
212 if ( flags )
213 {
214 if ( pagePos != wxNOT_FOUND )
215 *flags = 0;
216
217 if ( flagsList & (wxLIST_HITTEST_ONITEMICON |
218 wxLIST_HITTEST_ONITEMSTATEICON ) )
219 *flags |= wxBK_HITTEST_ONICON;
220
221 if ( flagsList & wxLIST_HITTEST_ONITEMLABEL )
222 *flags |= wxBK_HITTEST_ONLABEL;
223 }
224 }
225 else // not over list control at all
226 {
227 if ( flags && GetPageRect().Contains(pt) )
228 *flags |= wxBK_HITTEST_ONPAGE;
229 }
230
231 return pagePos;
232}
233
234wxSize wxListbook::CalcSizeFromPage(const wxSize& sizePage) const
235{
236 // we need to add the size of the list control and the border between
237 const wxSize sizeList = GetControllerSize();
238
239 wxSize size = sizePage;
240 if ( IsVertical() )
241 {
242 size.y += sizeList.y + GetInternalBorder();
243 }
244 else // left/right aligned
245 {
246 size.x += sizeList.x + GetInternalBorder();
247 }
248
249 return size;
250}
251
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}
259
260// ----------------------------------------------------------------------------
261// accessing the pages
262// ----------------------------------------------------------------------------
263
264bool wxListbook::SetPageText(size_t n, const wxString& strText)
265{
266 GetListView()->SetItemText(n, strText);
267
268 return true;
269}
270
271wxString wxListbook::GetPageText(size_t n) const
272{
273 return GetListView()->GetItemText(n);
274}
275
276int wxListbook::GetPageImage(size_t n) const
277{
278 wxListItem item;
279 item.SetId(n);
280
281 if (GetListView()->GetItem(item))
282 {
283 return item.GetImage();
284 }
285 else
286 {
287 return wxNOT_FOUND;
288 }
289}
290
291bool wxListbook::SetPageImage(size_t n, int imageId)
292{
293 return GetListView()->SetItemImage(n, imageId);
294}
295
296// ----------------------------------------------------------------------------
297// image list stuff
298// ----------------------------------------------------------------------------
299
300void wxListbook::SetImageList(wxImageList *imageList)
301{
302 wxListView * const list = GetListView();
303
304#ifdef CAN_USE_REPORT_VIEW
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 {
329 style |= GetListCtrlIconViewFlags();
330 }
331 else // no image list
332 {
333 style |= GetListCtrlReportViewFlags();
334 }
335
336 list->SetWindowStyleFlag(style);
337 if ( !imageList )
338 list->InsertColumn(0, wxT("Pages"));
339
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);
352#endif // CAN_USE_REPORT_VIEW
353
354 wxBookCtrlBase::SetImageList(imageList);
355}
356
357// ----------------------------------------------------------------------------
358// selection
359// ----------------------------------------------------------------------------
360
361void wxListbook::UpdateSelectedPage(size_t newsel)
362{
363 m_selection = newsel;
364 GetListView()->Select(newsel);
365 GetListView()->Focus(newsel);
366}
367
368int wxListbook::GetSelection() const
369{
370 return m_selection;
371}
372
373wxBookCtrlBaseEvent* wxListbook::CreatePageChangingEvent() const
374{
375 return new wxListbookEvent(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING, m_windowId);
376}
377
378void wxListbook::MakeChangedEvent(wxBookCtrlBaseEvent &event)
379{
380 event.SetEventType(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED);
381}
382
383
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{
395 if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) )
396 return false;
397
398 GetListView()->InsertItem(n, text, imageId);
399
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 )
403 {
404 // one extra page added
405 m_selection++;
406 GetListView()->Select(m_selection);
407 GetListView()->Focus(m_selection);
408 }
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 )
419 page->Hide();
420
421 if ( selNew != -1 )
422 SetSelection(selNew);
423
424 UpdateSize();
425
426 return true;
427}
428
429wxWindow *wxListbook::DoRemovePage(size_t page)
430{
431 const size_t page_count = GetPageCount();
432 wxWindow *win = wxBookCtrlBase::DoRemovePage(page);
433
434 if ( win )
435 {
436 GetListView()->DeleteItem(page);
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)
443 sel = wxNOT_FOUND;
444 else if ((page_count == 2) || (sel == -1))
445 sel = 0;
446
447 // force sel invalid if deleting current page - don't try to hide it
448 m_selection = (m_selection == (int)page) ? wxNOT_FOUND : m_selection - 1;
449
450 if ((sel != wxNOT_FOUND) && (sel != m_selection))
451 SetSelection(sel);
452 }
453
454 GetListView()->Arrange();
455 UpdateSize();
456 }
457
458 return win;
459}
460
461
462bool wxListbook::DeleteAllPages()
463{
464 GetListView()->DeleteAllItems();
465 if (!wxBookCtrlBase::DeleteAllPages())
466 return false;
467
468 m_selection = -1;
469
470 UpdateSize();
471
472 return true;
473}
474
475// ----------------------------------------------------------------------------
476// wxListbook events
477// ----------------------------------------------------------------------------
478
479void wxListbook::OnListSelected(wxListEvent& eventList)
480{
481 if ( eventList.GetEventObject() != m_bookctrl )
482 {
483 eventList.Skip();
484 return;
485 }
486
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
497 SetSelection(selNew);
498
499 // change wasn't allowed, return to previous state
500 if (m_selection != selNew)
501 {
502 GetListView()->Select(m_selection);
503 GetListView()->Focus(m_selection);
504 }
505}
506
507#endif // wxUSE_LISTBOOK