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