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