]> git.saurik.com Git - wxWidgets.git/blob - src/generic/listbkg.cpp
add wxMask::GetBitmap(), closes #9381
[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_COMMAND_LISTBOOK_PAGE_CHANGING, wxBookCtrlEvent );
53 wxDEFINE_EVENT( wxEVT_COMMAND_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_COMMAND_LISTBOOK_PAGE_CHANGING, m_windowId);
307 }
308
309 void wxListbook::MakeChangedEvent(wxBookCtrlEvent &event)
310 {
311 event.SetEventType(wxEVT_COMMAND_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 const size_t page_count = GetPageCount();
352 wxWindow *win = wxBookCtrlBase::DoRemovePage(page);
353
354 if ( win )
355 {
356 GetListView()->DeleteItem(page);
357
358 if (m_selection >= (int)page)
359 {
360 // force new sel valid if possible
361 int sel = m_selection - 1;
362 if (page_count == 1)
363 sel = wxNOT_FOUND;
364 else if ((page_count == 2) || (sel == wxNOT_FOUND))
365 sel = 0;
366
367 // force sel invalid if deleting current page - don't try to hide it
368 m_selection = (m_selection == (int)page) ? wxNOT_FOUND : m_selection - 1;
369
370 if ((sel != wxNOT_FOUND) && (sel != m_selection))
371 SetSelection(sel);
372 }
373
374 GetListView()->Arrange();
375 UpdateSize();
376 }
377
378 return win;
379 }
380
381
382 bool wxListbook::DeleteAllPages()
383 {
384 GetListView()->DeleteAllItems();
385 if (!wxBookCtrlBase::DeleteAllPages())
386 return false;
387
388 UpdateSize();
389
390 return true;
391 }
392
393 // ----------------------------------------------------------------------------
394 // wxListbook events
395 // ----------------------------------------------------------------------------
396
397 void wxListbook::OnListSelected(wxListEvent& eventList)
398 {
399 if ( eventList.GetEventObject() != m_bookctrl )
400 {
401 eventList.Skip();
402 return;
403 }
404
405 const int selNew = eventList.GetIndex();
406
407 if ( selNew == m_selection )
408 {
409 // this event can only come from our own Select(m_selection) below
410 // which we call when the page change is vetoed, so we should simply
411 // ignore it
412 return;
413 }
414
415 SetSelection(selNew);
416
417 // change wasn't allowed, return to previous state
418 if (m_selection != selNew)
419 {
420 GetListView()->Select(m_selection);
421 GetListView()->Focus(m_selection);
422 }
423 }
424
425 #endif // wxUSE_LISTBOOK