]> git.saurik.com Git - wxWidgets.git/blame - src/generic/listbkg.cpp
Compile fix.
[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
e9c0df38 39// ----------------------------------------------------------------------------
77ffb593 40// various wxWidgets macros
e9c0df38
VZ
41// ----------------------------------------------------------------------------
42
1f30c176
WS
43// check that the page index is valid
44#define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
45
46// ----------------------------------------------------------------------------
47// event table
48// ----------------------------------------------------------------------------
49
2ddb4d13 50IMPLEMENT_DYNAMIC_CLASS(wxListbook, wxBookCtrlBase)
e9c0df38
VZ
51IMPLEMENT_DYNAMIC_CLASS(wxListbookEvent, wxNotifyEvent)
52
9cf0f6ae 53#if !WXWIN_COMPATIBILITY_EVENT_TYPES
e9c0df38
VZ
54const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING = wxNewEventType();
55const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED = wxNewEventType();
9cf0f6ae 56#endif
f29395d0 57const int wxID_LISTBOOKLISTVIEW = wxNewId();
e9c0df38 58
61c083e7 59BEGIN_EVENT_TABLE(wxListbook, wxBookCtrlBase)
e9c0df38 60 EVT_SIZE(wxListbook::OnSize)
f29395d0 61 EVT_LIST_ITEM_SELECTED(wxID_LISTBOOKLISTVIEW, wxListbook::OnListSelected)
e9c0df38
VZ
62END_EVENT_TABLE()
63
64// ============================================================================
65// wxListbook implementation
66// ============================================================================
67
68// ----------------------------------------------------------------------------
69// wxListbook creation
70// ----------------------------------------------------------------------------
71
72void wxListbook::Init()
73{
e9c0df38
VZ
74 m_selection = wxNOT_FOUND;
75}
76
77bool
78wxListbook::Create(wxWindow *parent,
79 wxWindowID id,
80 const wxPoint& pos,
81 const wxSize& size,
82 long style,
83 const wxString& name)
84{
2ddb4d13 85 if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT )
e9c0df38
VZ
86 {
87#ifdef __WXMAC__
2ddb4d13 88 style |= wxBK_TOP;
e9c0df38 89#else // !__WXMAC__
2ddb4d13 90 style |= wxBK_LEFT;
e9c0df38
VZ
91#endif // __WXMAC__/!__WXMAC__
92 }
93
ef0120c1
VZ
94 // no border for this control, it doesn't look nice together with
95 // wxListCtrl border
96 style &= ~wxBORDER_MASK;
97 style |= wxBORDER_NONE;
98
e9c0df38
VZ
99 if ( !wxControl::Create(parent, id, pos, size, style,
100 wxDefaultValidator, name) )
101 return false;
102
2ddb4d13 103 m_bookctrl = new wxListView
e9c0df38
VZ
104 (
105 this,
f29395d0 106 wxID_LISTBOOKLISTVIEW,
e9c0df38
VZ
107 wxDefaultPosition,
108 wxDefaultSize,
ef0120c1 109 wxLC_ICON | wxLC_SINGLE_SEL |
4a06b348 110 (IsVertical() ? wxLC_ALIGN_LEFT : wxLC_ALIGN_TOP)
e9c0df38
VZ
111 );
112
a9092ede 113#ifdef __WXMSW__
2ddb4d13 114 // On XP with themes enabled the GetViewRect used in GetControllerSize() to
a9092ede 115 // determine the space needed for the list view will incorrectly return
7b45094a 116 // (0,0,0,0) the first time. So send a pending event so OnSize will be
a9092ede
RD
117 // called again after the window is ready to go. Technically we don't
118 // need to do this on non-XP windows, but if things are already sized
119 // correctly then nothing changes and so there is no harm.
120 wxSizeEvent evt;
121 GetEventHandler()->AddPendingEvent(evt);
122#endif
e9c0df38
VZ
123 return true;
124}
125
126// ----------------------------------------------------------------------------
127// wxListbook geometry management
128// ----------------------------------------------------------------------------
129
2ddb4d13 130wxSize wxListbook::GetControllerSize() const
e9c0df38 131{
4a06b348 132 const wxSize sizeClient = GetClientSize(),
2ddb4d13
WS
133 sizeBorder = m_bookctrl->GetSize() - m_bookctrl->GetClientSize(),
134 sizeList = GetListView()->GetViewRect().GetSize() + sizeBorder;
e9c0df38
VZ
135
136 wxSize size;
2ddb4d13 137
e9c0df38
VZ
138 if ( IsVertical() )
139 {
140 size.x = sizeClient.x;
4a06b348 141 size.y = sizeList.y;
e9c0df38
VZ
142 }
143 else // left/right aligned
144 {
4a06b348 145 size.x = sizeList.x;
e9c0df38
VZ
146 size.y = sizeClient.y;
147 }
148
149 return size;
150}
151
e9c0df38
VZ
152void wxListbook::OnSize(wxSizeEvent& event)
153{
9f29226d
WS
154 // arrange the icons before calling SetClientSize(), otherwise it wouldn't
155 // account for the scrollbars the list control might need and, at least
156 // under MSW, we'd finish with an ugly looking list control with both
157 // vertical and horizontal scrollbar (with one of them being added because
158 // the other one is not accounted for in client size computations)
2ddb4d13
WS
159 wxListView *list = GetListView();
160 if (list) list->Arrange();
161 wxBookCtrlBase::OnSize(event);
e9c0df38
VZ
162}
163
d0a84b63 164int wxListbook::HitTest(const wxPoint& pt, long *flags) const
851b88c3
VZ
165{
166 int pagePos = wxNOT_FOUND;
167
d0a84b63 168 if ( flags )
9804d540 169 *flags = wxBK_HITTEST_NOWHERE;
851b88c3 170
d0a84b63
VZ
171 // convert from listbook control coordinates to list control coordinates
172 const wxListView * const list = GetListView();
173 const wxPoint listPt = list->ScreenToClient(ClientToScreen(pt));
174
175 // is the point inside list control?
22a35096 176 if ( wxRect(list->GetSize()).Contains(listPt) )
851b88c3
VZ
177 {
178 int flagsList;
d0a84b63 179 pagePos = list->HitTest(listPt, flagsList);
851b88c3 180
d0a84b63 181 if ( flags )
851b88c3 182 {
d0a84b63
VZ
183 if ( pagePos != wxNOT_FOUND )
184 *flags = 0;
185
186 if ( flagsList & (wxLIST_HITTEST_ONITEMICON |
187 wxLIST_HITTEST_ONITEMSTATEICON ) )
9804d540 188 *flags |= wxBK_HITTEST_ONICON;
d0a84b63
VZ
189
190 if ( flagsList & wxLIST_HITTEST_ONITEMLABEL )
9804d540 191 *flags |= wxBK_HITTEST_ONLABEL;
851b88c3
VZ
192 }
193 }
d0a84b63
VZ
194 else // not over list control at all
195 {
22a35096 196 if ( flags && GetPageRect().Contains(pt) )
9804d540 197 *flags |= wxBK_HITTEST_ONPAGE;
d0a84b63 198 }
851b88c3
VZ
199
200 return pagePos;
201}
202
e9c0df38
VZ
203wxSize wxListbook::CalcSizeFromPage(const wxSize& sizePage) const
204{
159e6235 205 // we need to add the size of the list control and the border between
2ddb4d13 206 const wxSize sizeList = GetControllerSize();
e9c0df38
VZ
207
208 wxSize size = sizePage;
209 if ( IsVertical() )
210 {
159e6235 211 size.y += sizeList.y + GetInternalBorder();
e9c0df38
VZ
212 }
213 else // left/right aligned
214 {
159e6235 215 size.x += sizeList.x + GetInternalBorder();
e9c0df38
VZ
216 }
217
218 return size;
219}
220
221
222// ----------------------------------------------------------------------------
223// accessing the pages
224// ----------------------------------------------------------------------------
225
226bool wxListbook::SetPageText(size_t n, const wxString& strText)
227{
2ddb4d13 228 GetListView()->SetItemText(n, strText);
e9c0df38
VZ
229
230 return true;
231}
232
233wxString wxListbook::GetPageText(size_t n) const
234{
2ddb4d13 235 return GetListView()->GetItemText(n);
e9c0df38
VZ
236}
237
238int wxListbook::GetPageImage(size_t WXUNUSED(n)) const
239{
240 wxFAIL_MSG( _T("wxListbook::GetPageImage() not implemented") );
241
9f29226d 242 return wxNOT_FOUND;
e9c0df38
VZ
243}
244
245bool wxListbook::SetPageImage(size_t n, int imageId)
246{
2ddb4d13 247 return GetListView()->SetItemImage(n, imageId);
e9c0df38
VZ
248}
249
250// ----------------------------------------------------------------------------
251// image list stuff
252// ----------------------------------------------------------------------------
253
254void wxListbook::SetImageList(wxImageList *imageList)
255{
2ddb4d13 256 GetListView()->SetImageList(imageList, wxIMAGE_LIST_NORMAL);
e9c0df38 257
61c083e7 258 wxBookCtrlBase::SetImageList(imageList);
e9c0df38
VZ
259}
260
261// ----------------------------------------------------------------------------
262// selection
263// ----------------------------------------------------------------------------
264
1d6fcbcc
VZ
265void wxListbook::UpdateSelectedPage(size_t newsel)
266{
267 m_selection = newsel;
268 GetListView()->Select(newsel);
269 GetListView()->Focus(newsel);
270}
271
e9c0df38
VZ
272int wxListbook::GetSelection() const
273{
274 return m_selection;
275}
276
1d6fcbcc 277int wxListbook::DoSetSelection(size_t n, int flags)
e9c0df38 278{
1d6fcbcc
VZ
279 wxListbookEvent event(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING, m_windowId);
280 return wxBookCtrlBase::DoSetSelection(n, flags, event);
e9c0df38
VZ
281}
282
e9c0df38
VZ
283// ----------------------------------------------------------------------------
284// adding/removing the pages
285// ----------------------------------------------------------------------------
286
287bool
288wxListbook::InsertPage(size_t n,
289 wxWindow *page,
290 const wxString& text,
291 bool bSelect,
292 int imageId)
293{
61c083e7 294 if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) )
e9c0df38
VZ
295 return false;
296
2ddb4d13 297 GetListView()->InsertItem(n, text, imageId);
e9c0df38 298
716dc245
WS
299 // if the inserted page is before the selected one, we must update the
300 // index of the selected page
301 if ( int(n) <= m_selection )
e9c0df38 302 {
42841dfc 303 // one extra page added
716dc245 304 m_selection++;
2ddb4d13
WS
305 GetListView()->Select(m_selection);
306 GetListView()->Focus(m_selection);
e9c0df38 307 }
716dc245
WS
308
309 // some page should be selected: either this one or the first one if there
310 // is still no selection
311 int selNew = -1;
312 if ( bSelect )
313 selNew = n;
314 else if ( m_selection == -1 )
315 selNew = 0;
316
317 if ( selNew != m_selection )
e9c0df38 318 page->Hide();
716dc245
WS
319
320 if ( selNew != -1 )
321 SetSelection(selNew);
e9c0df38 322
37144cf0 323 InvalidateBestSize();
6a82a0d0 324 // GetListView()->InvalidateBestSize();
2ddb4d13 325 GetListView()->Arrange();
9eddec69 326
6a82a0d0
JS
327 if (GetPageCount() == 1)
328 {
329 wxSizeEvent sz(GetSize(), GetId());
330 ProcessEvent(sz);
331 }
e9c0df38
VZ
332 return true;
333}
334
335wxWindow *wxListbook::DoRemovePage(size_t page)
336{
4a10ea8b 337 const size_t page_count = GetPageCount();
61c083e7 338 wxWindow *win = wxBookCtrlBase::DoRemovePage(page);
bb08a4a1 339
e9c0df38
VZ
340 if ( win )
341 {
2ddb4d13 342 GetListView()->DeleteItem(page);
33ebfc3b
VZ
343
344 if (m_selection >= (int)page)
345 {
346 // force new sel valid if possible
347 int sel = m_selection - 1;
348 if (page_count == 1)
bb08a4a1 349 sel = wxNOT_FOUND;
33ebfc3b
VZ
350 else if ((page_count == 2) || (sel == -1))
351 sel = 0;
bb08a4a1 352
33ebfc3b 353 // force sel invalid if deleting current page - don't try to hide it
bb08a4a1
WS
354 m_selection = (m_selection == (int)page) ? wxNOT_FOUND : m_selection - 1;
355
356 if ((sel != wxNOT_FOUND) && (sel != m_selection))
33ebfc3b
VZ
357 SetSelection(sel);
358 }
9f29226d 359
2ddb4d13 360 GetListView()->Arrange();
6a82a0d0
JS
361 if (GetPageCount() == 0)
362 {
363 wxSizeEvent sz(GetSize(), GetId());
364 ProcessEvent(sz);
365 }
e9c0df38
VZ
366 }
367
368 return win;
369}
370
fbd11d30
RD
371
372bool wxListbook::DeleteAllPages()
373{
2ddb4d13 374 GetListView()->DeleteAllItems();
6a82a0d0
JS
375 if (!wxBookCtrlBase::DeleteAllPages())
376 return false;
9eddec69 377
6a82a0d0
JS
378 m_selection = -1;
379
380 wxSizeEvent sz(GetSize(), GetId());
381 ProcessEvent(sz);
9eddec69 382
6a82a0d0 383 return true;
fbd11d30
RD
384}
385
e9c0df38
VZ
386// ----------------------------------------------------------------------------
387// wxListbook events
388// ----------------------------------------------------------------------------
389
390void wxListbook::OnListSelected(wxListEvent& eventList)
391{
392 const int selNew = eventList.GetIndex();
393
394 if ( selNew == m_selection )
395 {
396 // this event can only come from our own Select(m_selection) below
397 // which we call when the page change is vetoed, so we should simply
398 // ignore it
399 return;
400 }
401
716dc245 402 SetSelection(selNew);
e9c0df38 403
716dc245
WS
404 // change wasn't allowed, return to previous state
405 if (m_selection != selNew)
e9c0df38 406 {
2ddb4d13
WS
407 GetListView()->Select(m_selection);
408 GetListView()->Focus(m_selection);
e9c0df38 409 }
e9c0df38
VZ
410}
411
412#endif // wxUSE_LISTBOOK