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