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