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