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