disable report view mode under Mac as it hangs the native wxListCtrl implementation...
[wxWidgets.git] / src / generic / listbkg.cpp
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
56 IMPLEMENT_DYNAMIC_CLASS(wxListbook, wxBookCtrlBase)
57 IMPLEMENT_DYNAMIC_CLASS(wxListbookEvent, wxNotifyEvent)
58
59 const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING = wxNewEventType();
60 const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED = wxNewEventType();
61
62 BEGIN_EVENT_TABLE(wxListbook, wxBookCtrlBase)
63 EVT_SIZE(wxListbook::OnSize)
64 EVT_LIST_ITEM_SELECTED(wxID_ANY, wxListbook::OnListSelected)
65 END_EVENT_TABLE()
66
67 // ============================================================================
68 // wxListbook implementation
69 // ============================================================================
70
71 // ----------------------------------------------------------------------------
72 // wxListbook creation
73 // ----------------------------------------------------------------------------
74
75 void wxListbook::Init()
76 {
77 m_selection = wxNOT_FOUND;
78 }
79
80 bool
81 wxListbook::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
141 long wxListbook::GetListCtrlIconViewFlags() const
142 {
143 return (IsVertical() ? wxLC_ALIGN_LEFT : wxLC_ALIGN_TOP) | wxLC_ICON;
144 }
145
146 #ifdef CAN_USE_REPORT_VIEW
147
148 long 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
159 wxSize 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
181 void 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 *list = GetListView();
189 if (list) list->Arrange();
190 wxBookCtrlBase::OnSize(event);
191 }
192
193 int wxListbook::HitTest(const wxPoint& pt, long *flags) const
194 {
195 int pagePos = wxNOT_FOUND;
196
197 if ( flags )
198 *flags = wxBK_HITTEST_NOWHERE;
199
200 // convert from listbook control coordinates to list control coordinates
201 const wxListView * const list = GetListView();
202 const wxPoint listPt = list->ScreenToClient(ClientToScreen(pt));
203
204 // is the point inside list control?
205 if ( wxRect(list->GetSize()).Contains(listPt) )
206 {
207 int flagsList;
208 pagePos = list->HitTest(listPt, flagsList);
209
210 if ( flags )
211 {
212 if ( pagePos != wxNOT_FOUND )
213 *flags = 0;
214
215 if ( flagsList & (wxLIST_HITTEST_ONITEMICON |
216 wxLIST_HITTEST_ONITEMSTATEICON ) )
217 *flags |= wxBK_HITTEST_ONICON;
218
219 if ( flagsList & wxLIST_HITTEST_ONITEMLABEL )
220 *flags |= wxBK_HITTEST_ONLABEL;
221 }
222 }
223 else // not over list control at all
224 {
225 if ( flags && GetPageRect().Contains(pt) )
226 *flags |= wxBK_HITTEST_ONPAGE;
227 }
228
229 return pagePos;
230 }
231
232 wxSize wxListbook::CalcSizeFromPage(const wxSize& sizePage) const
233 {
234 // we need to add the size of the list control and the border between
235 const wxSize sizeList = GetControllerSize();
236
237 wxSize size = sizePage;
238 if ( IsVertical() )
239 {
240 size.y += sizeList.y + GetInternalBorder();
241 }
242 else // left/right aligned
243 {
244 size.x += sizeList.x + GetInternalBorder();
245 }
246
247 return size;
248 }
249
250
251 // ----------------------------------------------------------------------------
252 // accessing the pages
253 // ----------------------------------------------------------------------------
254
255 bool wxListbook::SetPageText(size_t n, const wxString& strText)
256 {
257 GetListView()->SetItemText(n, strText);
258
259 return true;
260 }
261
262 wxString wxListbook::GetPageText(size_t n) const
263 {
264 return GetListView()->GetItemText(n);
265 }
266
267 int wxListbook::GetPageImage(size_t n) const
268 {
269 wxListItem item;
270 item.SetId(n);
271
272 if (GetListView()->GetItem(item))
273 {
274 return item.GetImage();
275 }
276 else
277 {
278 return wxNOT_FOUND;
279 }
280 }
281
282 bool wxListbook::SetPageImage(size_t n, int imageId)
283 {
284 return GetListView()->SetItemImage(n, imageId);
285 }
286
287 // ----------------------------------------------------------------------------
288 // image list stuff
289 // ----------------------------------------------------------------------------
290
291 void wxListbook::SetImageList(wxImageList *imageList)
292 {
293 wxListView * const list = GetListView();
294
295 #ifdef CAN_USE_REPORT_VIEW
296 // If imageList presence has changed, we update the list control view
297 if ( (imageList != NULL) != (GetImageList() != NULL) )
298 {
299 wxArrayString labels;
300 labels.Alloc(GetPageCount());
301
302 wxArrayInt imageIds;
303 imageIds.Alloc(GetPageCount());
304
305 const int oldSel = GetSelection();
306 size_t i;
307
308 // Grab snapshot of all list control items before changing the window
309 // style (which deletes the items)
310 for ( i = 0; i < GetPageCount(); i++ )
311 {
312 labels.Add(GetPageText(i));
313 imageIds.Add(GetPageImage(i));
314 }
315
316 // Update the style to use icon view for images, report view otherwise
317 long style = wxLC_SINGLE_SEL;
318 if ( imageList )
319 {
320 style |= GetListCtrlIconViewFlags();
321 }
322 else // no image list
323 {
324 style |= GetListCtrlReportViewFlags();
325 }
326
327 list->SetWindowStyleFlag(style);
328 if ( !imageList )
329 list->InsertColumn(0, wxT("Pages"));
330
331 // Add back the list control items
332 for ( i = 0; i < GetPageCount(); i++ )
333 {
334 list->InsertItem(i, labels[i], imageIds[i]);
335 }
336
337 // Restore selection
338 if ( oldSel != wxNOT_FOUND )
339 SetSelection(oldSel);
340 }
341
342 list->SetImageList(imageList, wxIMAGE_LIST_NORMAL);
343 #endif // CAN_USE_REPORT_VIEW
344
345 wxBookCtrlBase::SetImageList(imageList);
346 }
347
348 // ----------------------------------------------------------------------------
349 // selection
350 // ----------------------------------------------------------------------------
351
352 void wxListbook::UpdateSelectedPage(size_t newsel)
353 {
354 m_selection = newsel;
355 GetListView()->Select(newsel);
356 GetListView()->Focus(newsel);
357 }
358
359 int wxListbook::GetSelection() const
360 {
361 return m_selection;
362 }
363
364 wxBookCtrlBaseEvent* wxListbook::CreatePageChangingEvent() const
365 {
366 return new wxListbookEvent(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING, m_windowId);
367 }
368
369 void wxListbook::MakeChangedEvent(wxBookCtrlBaseEvent &event)
370 {
371 event.SetEventType(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED);
372 }
373
374
375 // ----------------------------------------------------------------------------
376 // adding/removing the pages
377 // ----------------------------------------------------------------------------
378
379 bool
380 wxListbook::InsertPage(size_t n,
381 wxWindow *page,
382 const wxString& text,
383 bool bSelect,
384 int imageId)
385 {
386 if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) )
387 return false;
388
389 GetListView()->InsertItem(n, text, imageId);
390
391 // if the inserted page is before the selected one, we must update the
392 // index of the selected page
393 if ( int(n) <= m_selection )
394 {
395 // one extra page added
396 m_selection++;
397 GetListView()->Select(m_selection);
398 GetListView()->Focus(m_selection);
399 }
400
401 // some page should be selected: either this one or the first one if there
402 // is still no selection
403 int selNew = -1;
404 if ( bSelect )
405 selNew = n;
406 else if ( m_selection == -1 )
407 selNew = 0;
408
409 if ( selNew != m_selection )
410 page->Hide();
411
412 if ( selNew != -1 )
413 SetSelection(selNew);
414
415 wxSizeEvent sz(GetSize(), GetId());
416 GetEventHandler()->ProcessEvent(sz);
417
418 return true;
419 }
420
421 wxWindow *wxListbook::DoRemovePage(size_t page)
422 {
423 const size_t page_count = GetPageCount();
424 wxWindow *win = wxBookCtrlBase::DoRemovePage(page);
425
426 if ( win )
427 {
428 GetListView()->DeleteItem(page);
429
430 if (m_selection >= (int)page)
431 {
432 // force new sel valid if possible
433 int sel = m_selection - 1;
434 if (page_count == 1)
435 sel = wxNOT_FOUND;
436 else if ((page_count == 2) || (sel == -1))
437 sel = 0;
438
439 // force sel invalid if deleting current page - don't try to hide it
440 m_selection = (m_selection == (int)page) ? wxNOT_FOUND : m_selection - 1;
441
442 if ((sel != wxNOT_FOUND) && (sel != m_selection))
443 SetSelection(sel);
444 }
445
446 GetListView()->Arrange();
447 if (GetPageCount() == 0)
448 {
449 wxSizeEvent sz(GetSize(), GetId());
450 GetEventHandler()->ProcessEvent(sz);
451 }
452 }
453
454 return win;
455 }
456
457
458 bool wxListbook::DeleteAllPages()
459 {
460 GetListView()->DeleteAllItems();
461 if (!wxBookCtrlBase::DeleteAllPages())
462 return false;
463
464 m_selection = -1;
465
466 wxSizeEvent sz(GetSize(), GetId());
467 GetEventHandler()->ProcessEvent(sz);
468
469 return true;
470 }
471
472 // ----------------------------------------------------------------------------
473 // wxListbook events
474 // ----------------------------------------------------------------------------
475
476 void wxListbook::OnListSelected(wxListEvent& eventList)
477 {
478 if ( eventList.GetEventObject() != m_bookctrl )
479 {
480 eventList.Skip();
481 return;
482 }
483
484 const int selNew = eventList.GetIndex();
485
486 if ( selNew == m_selection )
487 {
488 // this event can only come from our own Select(m_selection) below
489 // which we call when the page change is vetoed, so we should simply
490 // ignore it
491 return;
492 }
493
494 SetSelection(selNew);
495
496 // change wasn't allowed, return to previous state
497 if (m_selection != selNew)
498 {
499 GetListView()->Select(m_selection);
500 GetListView()->Focus(m_selection);
501 }
502 }
503
504 #endif // wxUSE_LISTBOOK