]> git.saurik.com Git - wxWidgets.git/blob - src/generic/listbkg.cpp
Fix crash in docview code if view initialization failed.
[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 // ----------------------------------------------------------------------------
40 // various wxWidgets macros
41 // ----------------------------------------------------------------------------
42
43 // check that the page index is valid
44 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
45
46 // ----------------------------------------------------------------------------
47 // event table
48 // ----------------------------------------------------------------------------
49
50 IMPLEMENT_DYNAMIC_CLASS(wxListbook, wxBookCtrlBase)
51
52 wxDEFINE_EVENT( wxEVT_LISTBOOK_PAGE_CHANGING, wxBookCtrlEvent );
53 wxDEFINE_EVENT( wxEVT_LISTBOOK_PAGE_CHANGED, wxBookCtrlEvent );
54
55 BEGIN_EVENT_TABLE(wxListbook, wxBookCtrlBase)
56 EVT_SIZE(wxListbook::OnSize)
57 EVT_LIST_ITEM_SELECTED(wxID_ANY, wxListbook::OnListSelected)
58 END_EVENT_TABLE()
59
60 // ============================================================================
61 // wxListbook implementation
62 // ============================================================================
63
64 // ----------------------------------------------------------------------------
65 // wxListbook creation
66 // ----------------------------------------------------------------------------
67
68 bool
69 wxListbook::Create(wxWindow *parent,
70 wxWindowID id,
71 const wxPoint& pos,
72 const wxSize& size,
73 long style,
74 const wxString& name)
75 {
76 if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT )
77 {
78 #ifdef __WXMAC__
79 style |= wxBK_TOP;
80 #else // !__WXMAC__
81 style |= wxBK_LEFT;
82 #endif // __WXMAC__/!__WXMAC__
83 }
84
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
90 if ( !wxControl::Create(parent, id, pos, size, style,
91 wxDefaultValidator, name) )
92 return false;
93
94 m_bookctrl = new wxListView
95 (
96 this,
97 wxID_ANY,
98 wxDefaultPosition,
99 wxDefaultSize,
100 GetListCtrlFlags()
101 );
102
103 if ( GetListView()->InReportView() )
104 GetListView()->InsertColumn(0, wxS("Pages"));
105
106 #ifdef __WXMSW__
107 // On XP with themes enabled the GetViewRect used in GetControllerSize() to
108 // determine the space needed for the list view will incorrectly return
109 // (0,0,0,0) the first time. So send a pending event so OnSize will be
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
116 return true;
117 }
118
119 // ----------------------------------------------------------------------------
120 // wxListCtrl flags
121 // ----------------------------------------------------------------------------
122
123 long 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
159 // ----------------------------------------------------------------------------
160 // wxListbook geometry management
161 // ----------------------------------------------------------------------------
162
163 void wxListbook::OnSize(wxSizeEvent& event)
164 {
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)
170 wxListView * const list = GetListView();
171 if ( list )
172 list->Arrange();
173
174 event.Skip();
175 }
176
177 int wxListbook::HitTest(const wxPoint& pt, long *flags) const
178 {
179 int pagePos = wxNOT_FOUND;
180
181 if ( flags )
182 *flags = wxBK_HITTEST_NOWHERE;
183
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?
189 if ( wxRect(list->GetSize()).Contains(listPt) )
190 {
191 int flagsList;
192 pagePos = list->HitTest(listPt, flagsList);
193
194 if ( flags )
195 {
196 if ( pagePos != wxNOT_FOUND )
197 *flags = 0;
198
199 if ( flagsList & (wxLIST_HITTEST_ONITEMICON |
200 wxLIST_HITTEST_ONITEMSTATEICON ) )
201 *flags |= wxBK_HITTEST_ONICON;
202
203 if ( flagsList & wxLIST_HITTEST_ONITEMLABEL )
204 *flags |= wxBK_HITTEST_ONLABEL;
205 }
206 }
207 else // not over list control at all
208 {
209 if ( flags && GetPageRect().Contains(pt) )
210 *flags |= wxBK_HITTEST_ONPAGE;
211 }
212
213 return pagePos;
214 }
215
216 void 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 }
223
224 // ----------------------------------------------------------------------------
225 // accessing the pages
226 // ----------------------------------------------------------------------------
227
228 bool wxListbook::SetPageText(size_t n, const wxString& strText)
229 {
230 GetListView()->SetItemText(n, strText);
231
232 return true;
233 }
234
235 wxString wxListbook::GetPageText(size_t n) const
236 {
237 return GetListView()->GetItemText(n);
238 }
239
240 int wxListbook::GetPageImage(size_t n) const
241 {
242 wxListItem item;
243 item.SetId(n);
244
245 if (GetListView()->GetItem(item))
246 {
247 return item.GetImage();
248 }
249 else
250 {
251 return wxNOT_FOUND;
252 }
253 }
254
255 bool wxListbook::SetPageImage(size_t n, int imageId)
256 {
257 return GetListView()->SetItemImage(n, imageId);
258 }
259
260 // ----------------------------------------------------------------------------
261 // image list stuff
262 // ----------------------------------------------------------------------------
263
264 void wxListbook::SetImageList(wxImageList *imageList)
265 {
266 const long flagsOld = GetListCtrlFlags();
267
268 wxBookCtrlBase::SetImageList(imageList);
269
270 const long flagsNew = GetListCtrlFlags();
271
272 wxListView * const list = GetListView();
273
274 // We may need to change the list control mode if the image list presence
275 // has changed.
276 if ( flagsNew != flagsOld )
277 {
278 // Preserve the selection which is lost when changing the mode
279 const int oldSel = GetSelection();
280
281 list->SetWindowStyleFlag(flagsNew);
282 if ( list->InReportView() )
283 list->InsertColumn(0, wxS("Pages"));
284
285 // Restore selection
286 if ( oldSel != wxNOT_FOUND )
287 SetSelection(oldSel);
288 }
289
290 list->SetImageList(imageList, wxIMAGE_LIST_NORMAL);
291 }
292
293 // ----------------------------------------------------------------------------
294 // selection
295 // ----------------------------------------------------------------------------
296
297 void wxListbook::UpdateSelectedPage(size_t newsel)
298 {
299 m_selection = newsel;
300 GetListView()->Select(newsel);
301 GetListView()->Focus(newsel);
302 }
303
304 wxBookCtrlEvent* wxListbook::CreatePageChangingEvent() const
305 {
306 return new wxBookCtrlEvent(wxEVT_LISTBOOK_PAGE_CHANGING, m_windowId);
307 }
308
309 void wxListbook::MakeChangedEvent(wxBookCtrlEvent &event)
310 {
311 event.SetEventType(wxEVT_LISTBOOK_PAGE_CHANGED);
312 }
313
314
315 // ----------------------------------------------------------------------------
316 // adding/removing the pages
317 // ----------------------------------------------------------------------------
318
319 bool
320 wxListbook::InsertPage(size_t n,
321 wxWindow *page,
322 const wxString& text,
323 bool bSelect,
324 int imageId)
325 {
326 if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) )
327 return false;
328
329 GetListView()->InsertItem(n, text, imageId);
330
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 )
334 {
335 // one extra page added
336 m_selection++;
337 GetListView()->Select(m_selection);
338 GetListView()->Focus(m_selection);
339 }
340
341 if ( !DoSetSelectionAfterInsertion(n, bSelect) )
342 page->Hide();
343
344 UpdateSize();
345
346 return true;
347 }
348
349 wxWindow *wxListbook::DoRemovePage(size_t page)
350 {
351 wxWindow *win = wxBookCtrlBase::DoRemovePage(page);
352
353 if ( win )
354 {
355 GetListView()->DeleteItem(page);
356
357 DoSetSelectionAfterRemoval(page);
358
359 GetListView()->Arrange();
360 UpdateSize();
361 }
362
363 return win;
364 }
365
366
367 bool wxListbook::DeleteAllPages()
368 {
369 GetListView()->DeleteAllItems();
370 if (!wxBookCtrlBase::DeleteAllPages())
371 return false;
372
373 UpdateSize();
374
375 return true;
376 }
377
378 // ----------------------------------------------------------------------------
379 // wxListbook events
380 // ----------------------------------------------------------------------------
381
382 void wxListbook::OnListSelected(wxListEvent& eventList)
383 {
384 if ( eventList.GetEventObject() != m_bookctrl )
385 {
386 eventList.Skip();
387 return;
388 }
389
390 const int selNew = eventList.GetIndex();
391
392 if ( selNew == m_selection )
393 {
394 // this event can only come from our own Select(m_selection) below
395 // which we call when the page change is vetoed, so we should simply
396 // ignore it
397 return;
398 }
399
400 SetSelection(selNew);
401
402 // change wasn't allowed, return to previous state
403 if (m_selection != selNew)
404 {
405 GetListView()->Select(m_selection);
406 GetListView()->Focus(m_selection);
407 }
408 }
409
410 #endif // wxUSE_LISTBOOK