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