]>
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 | // 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 | wxSize wxListbook::GetControllerSize() const | |
159 | { | |
160 | const wxSize sizeClient = GetClientSize(), | |
161 | sizeBorder = m_bookctrl->GetSize() - m_bookctrl->GetClientSize(), | |
162 | sizeList = GetListView()->GetViewRect().GetSize() + sizeBorder; | |
163 | ||
164 | wxSize size; | |
165 | ||
166 | if ( IsVertical() ) | |
167 | { | |
168 | size.x = sizeClient.x; | |
169 | size.y = sizeList.y; | |
170 | } | |
171 | else // left/right aligned | |
172 | { | |
173 | size.x = sizeList.x; | |
174 | size.y = sizeClient.y; | |
175 | } | |
176 | ||
177 | return size; | |
178 | } | |
179 | ||
180 | void wxListbook::OnSize(wxSizeEvent& event) | |
181 | { | |
182 | // arrange the icons before calling SetClientSize(), otherwise it wouldn't | |
183 | // account for the scrollbars the list control might need and, at least | |
184 | // under MSW, we'd finish with an ugly looking list control with both | |
185 | // vertical and horizontal scrollbar (with one of them being added because | |
186 | // the other one is not accounted for in client size computations) | |
187 | wxListView * const list = GetListView(); | |
188 | if ( list ) | |
189 | list->Arrange(); | |
190 | ||
191 | event.Skip(); | |
192 | } | |
193 | ||
194 | int wxListbook::HitTest(const wxPoint& pt, long *flags) const | |
195 | { | |
196 | int pagePos = wxNOT_FOUND; | |
197 | ||
198 | if ( flags ) | |
199 | *flags = wxBK_HITTEST_NOWHERE; | |
200 | ||
201 | // convert from listbook control coordinates to list control coordinates | |
202 | const wxListView * const list = GetListView(); | |
203 | const wxPoint listPt = list->ScreenToClient(ClientToScreen(pt)); | |
204 | ||
205 | // is the point inside list control? | |
206 | if ( wxRect(list->GetSize()).Contains(listPt) ) | |
207 | { | |
208 | int flagsList; | |
209 | pagePos = list->HitTest(listPt, flagsList); | |
210 | ||
211 | if ( flags ) | |
212 | { | |
213 | if ( pagePos != wxNOT_FOUND ) | |
214 | *flags = 0; | |
215 | ||
216 | if ( flagsList & (wxLIST_HITTEST_ONITEMICON | | |
217 | wxLIST_HITTEST_ONITEMSTATEICON ) ) | |
218 | *flags |= wxBK_HITTEST_ONICON; | |
219 | ||
220 | if ( flagsList & wxLIST_HITTEST_ONITEMLABEL ) | |
221 | *flags |= wxBK_HITTEST_ONLABEL; | |
222 | } | |
223 | } | |
224 | else // not over list control at all | |
225 | { | |
226 | if ( flags && GetPageRect().Contains(pt) ) | |
227 | *flags |= wxBK_HITTEST_ONPAGE; | |
228 | } | |
229 | ||
230 | return pagePos; | |
231 | } | |
232 | ||
233 | wxSize wxListbook::CalcSizeFromPage(const wxSize& sizePage) const | |
234 | { | |
235 | // we need to add the size of the list control and the border between | |
236 | const wxSize sizeList = GetControllerSize(); | |
237 | ||
238 | wxSize size = sizePage; | |
239 | if ( IsVertical() ) | |
240 | { | |
241 | size.y += sizeList.y + GetInternalBorder(); | |
242 | } | |
243 | else // left/right aligned | |
244 | { | |
245 | size.x += sizeList.x + GetInternalBorder(); | |
246 | } | |
247 | ||
248 | return size; | |
249 | } | |
250 | ||
251 | void wxListbook::UpdateSize() | |
252 | { | |
253 | // we should find a more elegant way to force a layout than generating this | |
254 | // dummy event | |
255 | wxSizeEvent sz(GetSize(), GetId()); | |
256 | GetEventHandler()->ProcessEvent(sz); | |
257 | } | |
258 | ||
259 | // ---------------------------------------------------------------------------- | |
260 | // accessing the pages | |
261 | // ---------------------------------------------------------------------------- | |
262 | ||
263 | bool wxListbook::SetPageText(size_t n, const wxString& strText) | |
264 | { | |
265 | GetListView()->SetItemText(n, strText); | |
266 | ||
267 | return true; | |
268 | } | |
269 | ||
270 | wxString wxListbook::GetPageText(size_t n) const | |
271 | { | |
272 | return GetListView()->GetItemText(n); | |
273 | } | |
274 | ||
275 | int wxListbook::GetPageImage(size_t n) const | |
276 | { | |
277 | wxListItem item; | |
278 | item.SetId(n); | |
279 | ||
280 | if (GetListView()->GetItem(item)) | |
281 | { | |
282 | return item.GetImage(); | |
283 | } | |
284 | else | |
285 | { | |
286 | return wxNOT_FOUND; | |
287 | } | |
288 | } | |
289 | ||
290 | bool wxListbook::SetPageImage(size_t n, int imageId) | |
291 | { | |
292 | return GetListView()->SetItemImage(n, imageId); | |
293 | } | |
294 | ||
295 | // ---------------------------------------------------------------------------- | |
296 | // image list stuff | |
297 | // ---------------------------------------------------------------------------- | |
298 | ||
299 | void wxListbook::SetImageList(wxImageList *imageList) | |
300 | { | |
301 | wxListView * const list = GetListView(); | |
302 | ||
303 | #ifdef CAN_USE_REPORT_VIEW | |
304 | // If imageList presence has changed, we update the list control view | |
305 | if ( (imageList != NULL) != (GetImageList() != NULL) ) | |
306 | { | |
307 | // Preserve the selection which is lost when changing the mode | |
308 | const int oldSel = GetSelection(); | |
309 | ||
310 | // Update the style to use icon view for images, report view otherwise | |
311 | long style = wxLC_SINGLE_SEL; | |
312 | if ( imageList ) | |
313 | { | |
314 | style |= GetListCtrlIconViewFlags(); | |
315 | } | |
316 | else // no image list | |
317 | { | |
318 | style |= GetListCtrlReportViewFlags(); | |
319 | } | |
320 | ||
321 | list->SetWindowStyleFlag(style); | |
322 | if ( !imageList ) | |
323 | list->InsertColumn(0, wxT("Pages")); | |
324 | ||
325 | // Restore selection | |
326 | if ( oldSel != wxNOT_FOUND ) | |
327 | SetSelection(oldSel); | |
328 | } | |
329 | ||
330 | list->SetImageList(imageList, wxIMAGE_LIST_NORMAL); | |
331 | #endif // CAN_USE_REPORT_VIEW | |
332 | ||
333 | wxBookCtrlBase::SetImageList(imageList); | |
334 | } | |
335 | ||
336 | // ---------------------------------------------------------------------------- | |
337 | // selection | |
338 | // ---------------------------------------------------------------------------- | |
339 | ||
340 | void wxListbook::UpdateSelectedPage(size_t newsel) | |
341 | { | |
342 | m_selection = newsel; | |
343 | GetListView()->Select(newsel); | |
344 | GetListView()->Focus(newsel); | |
345 | } | |
346 | ||
347 | int wxListbook::GetSelection() const | |
348 | { | |
349 | return m_selection; | |
350 | } | |
351 | ||
352 | wxBookCtrlEvent* wxListbook::CreatePageChangingEvent() const | |
353 | { | |
354 | return new wxBookCtrlEvent(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING, m_windowId); | |
355 | } | |
356 | ||
357 | void wxListbook::MakeChangedEvent(wxBookCtrlEvent &event) | |
358 | { | |
359 | event.SetEventType(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED); | |
360 | } | |
361 | ||
362 | ||
363 | // ---------------------------------------------------------------------------- | |
364 | // adding/removing the pages | |
365 | // ---------------------------------------------------------------------------- | |
366 | ||
367 | bool | |
368 | wxListbook::InsertPage(size_t n, | |
369 | wxWindow *page, | |
370 | const wxString& text, | |
371 | bool bSelect, | |
372 | int imageId) | |
373 | { | |
374 | if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) ) | |
375 | return false; | |
376 | ||
377 | GetListView()->InsertItem(n, text, imageId); | |
378 | ||
379 | // if the inserted page is before the selected one, we must update the | |
380 | // index of the selected page | |
381 | if ( int(n) <= m_selection ) | |
382 | { | |
383 | // one extra page added | |
384 | m_selection++; | |
385 | GetListView()->Select(m_selection); | |
386 | GetListView()->Focus(m_selection); | |
387 | } | |
388 | ||
389 | // some page should be selected: either this one or the first one if there | |
390 | // is still no selection | |
391 | int selNew = -1; | |
392 | if ( bSelect ) | |
393 | selNew = n; | |
394 | else if ( m_selection == -1 ) | |
395 | selNew = 0; | |
396 | ||
397 | if ( selNew != m_selection ) | |
398 | page->Hide(); | |
399 | ||
400 | if ( selNew != -1 ) | |
401 | SetSelection(selNew); | |
402 | ||
403 | UpdateSize(); | |
404 | ||
405 | return true; | |
406 | } | |
407 | ||
408 | wxWindow *wxListbook::DoRemovePage(size_t page) | |
409 | { | |
410 | const size_t page_count = GetPageCount(); | |
411 | wxWindow *win = wxBookCtrlBase::DoRemovePage(page); | |
412 | ||
413 | if ( win ) | |
414 | { | |
415 | GetListView()->DeleteItem(page); | |
416 | ||
417 | if (m_selection >= (int)page) | |
418 | { | |
419 | // force new sel valid if possible | |
420 | int sel = m_selection - 1; | |
421 | if (page_count == 1) | |
422 | sel = wxNOT_FOUND; | |
423 | else if ((page_count == 2) || (sel == -1)) | |
424 | sel = 0; | |
425 | ||
426 | // force sel invalid if deleting current page - don't try to hide it | |
427 | m_selection = (m_selection == (int)page) ? wxNOT_FOUND : m_selection - 1; | |
428 | ||
429 | if ((sel != wxNOT_FOUND) && (sel != m_selection)) | |
430 | SetSelection(sel); | |
431 | } | |
432 | ||
433 | GetListView()->Arrange(); | |
434 | UpdateSize(); | |
435 | } | |
436 | ||
437 | return win; | |
438 | } | |
439 | ||
440 | ||
441 | bool wxListbook::DeleteAllPages() | |
442 | { | |
443 | GetListView()->DeleteAllItems(); | |
444 | if (!wxBookCtrlBase::DeleteAllPages()) | |
445 | return false; | |
446 | ||
447 | m_selection = -1; | |
448 | ||
449 | UpdateSize(); | |
450 | ||
451 | return true; | |
452 | } | |
453 | ||
454 | // ---------------------------------------------------------------------------- | |
455 | // wxListbook events | |
456 | // ---------------------------------------------------------------------------- | |
457 | ||
458 | void wxListbook::OnListSelected(wxListEvent& eventList) | |
459 | { | |
460 | if ( eventList.GetEventObject() != m_bookctrl ) | |
461 | { | |
462 | eventList.Skip(); | |
463 | return; | |
464 | } | |
465 | ||
466 | const int selNew = eventList.GetIndex(); | |
467 | ||
468 | if ( selNew == m_selection ) | |
469 | { | |
470 | // this event can only come from our own Select(m_selection) below | |
471 | // which we call when the page change is vetoed, so we should simply | |
472 | // ignore it | |
473 | return; | |
474 | } | |
475 | ||
476 | SetSelection(selNew); | |
477 | ||
478 | // change wasn't allowed, return to previous state | |
479 | if (m_selection != selNew) | |
480 | { | |
481 | GetListView()->Select(m_selection); | |
482 | GetListView()->Focus(m_selection); | |
483 | } | |
484 | } | |
485 | ||
486 | #endif // wxUSE_LISTBOOK |