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