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