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