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