]>
Commit | Line | Data |
---|---|---|
e9c0df38 | 1 | /////////////////////////////////////////////////////////////////////////////// |
2ddb4d13 | 2 | // Name: src/generic/listbkg.cpp |
e9c0df38 VZ |
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> | |
65571936 | 9 | // Licence: wxWindows licence |
e9c0df38 VZ |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
e9c0df38 VZ |
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 | ||
9eddec69 WS |
29 | #include "wx/listbook.h" |
30 | ||
31 | #ifndef WX_PRECOMP | |
32 | #include "wx/settings.h" | |
33 | #endif | |
34 | ||
e9c0df38 VZ |
35 | #include "wx/listctrl.h" |
36 | #include "wx/statline.h" | |
5b24f0d3 | 37 | #include "wx/imaglist.h" |
e9c0df38 | 38 | |
1a9a6eed VZ |
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 | ||
e9c0df38 | 45 | // ---------------------------------------------------------------------------- |
77ffb593 | 46 | // various wxWidgets macros |
e9c0df38 VZ |
47 | // ---------------------------------------------------------------------------- |
48 | ||
1f30c176 WS |
49 | // check that the page index is valid |
50 | #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount()) | |
51 | ||
52 | // ---------------------------------------------------------------------------- | |
53 | // event table | |
54 | // ---------------------------------------------------------------------------- | |
55 | ||
2ddb4d13 | 56 | IMPLEMENT_DYNAMIC_CLASS(wxListbook, wxBookCtrlBase) |
e9c0df38 VZ |
57 | IMPLEMENT_DYNAMIC_CLASS(wxListbookEvent, wxNotifyEvent) |
58 | ||
59 | const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING = wxNewEventType(); | |
60 | const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED = wxNewEventType(); | |
61 | ||
61c083e7 | 62 | BEGIN_EVENT_TABLE(wxListbook, wxBookCtrlBase) |
e9c0df38 | 63 | EVT_SIZE(wxListbook::OnSize) |
447325a4 | 64 | EVT_LIST_ITEM_SELECTED(wxID_ANY, wxListbook::OnListSelected) |
e9c0df38 VZ |
65 | END_EVENT_TABLE() |
66 | ||
67 | // ============================================================================ | |
68 | // wxListbook implementation | |
69 | // ============================================================================ | |
70 | ||
71 | // ---------------------------------------------------------------------------- | |
72 | // wxListbook creation | |
73 | // ---------------------------------------------------------------------------- | |
74 | ||
75 | void wxListbook::Init() | |
76 | { | |
e9c0df38 VZ |
77 | m_selection = wxNOT_FOUND; |
78 | } | |
79 | ||
80 | bool | |
81 | wxListbook::Create(wxWindow *parent, | |
82 | wxWindowID id, | |
83 | const wxPoint& pos, | |
84 | const wxSize& size, | |
85 | long style, | |
86 | const wxString& name) | |
87 | { | |
2ddb4d13 | 88 | if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT ) |
e9c0df38 VZ |
89 | { |
90 | #ifdef __WXMAC__ | |
2ddb4d13 | 91 | style |= wxBK_TOP; |
e9c0df38 | 92 | #else // !__WXMAC__ |
2ddb4d13 | 93 | style |= wxBK_LEFT; |
e9c0df38 VZ |
94 | #endif // __WXMAC__/!__WXMAC__ |
95 | } | |
96 | ||
ef0120c1 VZ |
97 | // no border for this control, it doesn't look nice together with |
98 | // wxListCtrl border | |
99 | style &= ~wxBORDER_MASK; | |
100 | style |= wxBORDER_NONE; | |
101 | ||
e9c0df38 VZ |
102 | if ( !wxControl::Create(parent, id, pos, size, style, |
103 | wxDefaultValidator, name) ) | |
104 | return false; | |
105 | ||
2ddb4d13 | 106 | m_bookctrl = new wxListView |
e9c0df38 VZ |
107 | ( |
108 | this, | |
447325a4 | 109 | wxID_ANY, |
e9c0df38 VZ |
110 | wxDefaultPosition, |
111 | wxDefaultSize, | |
1a9a6eed VZ |
112 | wxLC_SINGLE_SEL | |
113 | #ifdef CAN_USE_REPORT_VIEW | |
114 | GetListCtrlReportViewFlags() | |
115 | #else // !CAN_USE_REPORT_VIEW | |
116 | GetListCtrlIconViewFlags() | |
117 | #endif // CAN_USE_REPORT_VIEW/!CAN_USE_REPORT_VIEW | |
e9c0df38 VZ |
118 | ); |
119 | ||
1a9a6eed | 120 | #ifdef CAN_USE_REPORT_VIEW |
c1718122 | 121 | GetListView()->InsertColumn(0, wxT("Pages")); |
1a9a6eed | 122 | #endif // CAN_USE_REPORT_VIEW |
c1718122 | 123 | |
a9092ede | 124 | #ifdef __WXMSW__ |
2ddb4d13 | 125 | // On XP with themes enabled the GetViewRect used in GetControllerSize() to |
a9092ede | 126 | // determine the space needed for the list view will incorrectly return |
7b45094a | 127 | // (0,0,0,0) the first time. So send a pending event so OnSize will be |
a9092ede RD |
128 | // called again after the window is ready to go. Technically we don't |
129 | // need to do this on non-XP windows, but if things are already sized | |
130 | // correctly then nothing changes and so there is no harm. | |
131 | wxSizeEvent evt; | |
132 | GetEventHandler()->AddPendingEvent(evt); | |
133 | #endif | |
e9c0df38 VZ |
134 | return true; |
135 | } | |
136 | ||
1a9a6eed VZ |
137 | // ---------------------------------------------------------------------------- |
138 | // wxListCtrl flags | |
139 | // ---------------------------------------------------------------------------- | |
140 | ||
141 | long wxListbook::GetListCtrlIconViewFlags() const | |
142 | { | |
143 | return (IsVertical() ? wxLC_ALIGN_LEFT : wxLC_ALIGN_TOP) | wxLC_ICON; | |
144 | } | |
145 | ||
146 | #ifdef CAN_USE_REPORT_VIEW | |
147 | ||
148 | long wxListbook::GetListCtrlReportViewFlags() const | |
149 | { | |
150 | return wxLC_REPORT | wxLC_NO_HEADER; | |
151 | } | |
152 | ||
153 | #endif // CAN_USE_REPORT_VIEW | |
154 | ||
e9c0df38 VZ |
155 | // ---------------------------------------------------------------------------- |
156 | // wxListbook geometry management | |
157 | // ---------------------------------------------------------------------------- | |
158 | ||
2ddb4d13 | 159 | wxSize wxListbook::GetControllerSize() const |
e9c0df38 | 160 | { |
4a06b348 | 161 | const wxSize sizeClient = GetClientSize(), |
2ddb4d13 WS |
162 | sizeBorder = m_bookctrl->GetSize() - m_bookctrl->GetClientSize(), |
163 | sizeList = GetListView()->GetViewRect().GetSize() + sizeBorder; | |
e9c0df38 VZ |
164 | |
165 | wxSize size; | |
2ddb4d13 | 166 | |
e9c0df38 VZ |
167 | if ( IsVertical() ) |
168 | { | |
169 | size.x = sizeClient.x; | |
4a06b348 | 170 | size.y = sizeList.y; |
e9c0df38 VZ |
171 | } |
172 | else // left/right aligned | |
173 | { | |
4a06b348 | 174 | size.x = sizeList.x; |
e9c0df38 VZ |
175 | size.y = sizeClient.y; |
176 | } | |
177 | ||
178 | return size; | |
179 | } | |
180 | ||
e9c0df38 VZ |
181 | void wxListbook::OnSize(wxSizeEvent& event) |
182 | { | |
9f29226d WS |
183 | // arrange the icons before calling SetClientSize(), otherwise it wouldn't |
184 | // account for the scrollbars the list control might need and, at least | |
185 | // under MSW, we'd finish with an ugly looking list control with both | |
186 | // vertical and horizontal scrollbar (with one of them being added because | |
187 | // the other one is not accounted for in client size computations) | |
2ddb4d13 WS |
188 | wxListView *list = GetListView(); |
189 | if (list) list->Arrange(); | |
190 | wxBookCtrlBase::OnSize(event); | |
e9c0df38 VZ |
191 | } |
192 | ||
d0a84b63 | 193 | int wxListbook::HitTest(const wxPoint& pt, long *flags) const |
851b88c3 VZ |
194 | { |
195 | int pagePos = wxNOT_FOUND; | |
196 | ||
d0a84b63 | 197 | if ( flags ) |
9804d540 | 198 | *flags = wxBK_HITTEST_NOWHERE; |
851b88c3 | 199 | |
d0a84b63 VZ |
200 | // convert from listbook control coordinates to list control coordinates |
201 | const wxListView * const list = GetListView(); | |
202 | const wxPoint listPt = list->ScreenToClient(ClientToScreen(pt)); | |
203 | ||
204 | // is the point inside list control? | |
22a35096 | 205 | if ( wxRect(list->GetSize()).Contains(listPt) ) |
851b88c3 VZ |
206 | { |
207 | int flagsList; | |
d0a84b63 | 208 | pagePos = list->HitTest(listPt, flagsList); |
851b88c3 | 209 | |
d0a84b63 | 210 | if ( flags ) |
851b88c3 | 211 | { |
d0a84b63 VZ |
212 | if ( pagePos != wxNOT_FOUND ) |
213 | *flags = 0; | |
214 | ||
215 | if ( flagsList & (wxLIST_HITTEST_ONITEMICON | | |
216 | wxLIST_HITTEST_ONITEMSTATEICON ) ) | |
9804d540 | 217 | *flags |= wxBK_HITTEST_ONICON; |
d0a84b63 VZ |
218 | |
219 | if ( flagsList & wxLIST_HITTEST_ONITEMLABEL ) | |
9804d540 | 220 | *flags |= wxBK_HITTEST_ONLABEL; |
851b88c3 VZ |
221 | } |
222 | } | |
d0a84b63 VZ |
223 | else // not over list control at all |
224 | { | |
22a35096 | 225 | if ( flags && GetPageRect().Contains(pt) ) |
9804d540 | 226 | *flags |= wxBK_HITTEST_ONPAGE; |
d0a84b63 | 227 | } |
851b88c3 VZ |
228 | |
229 | return pagePos; | |
230 | } | |
231 | ||
e9c0df38 VZ |
232 | wxSize wxListbook::CalcSizeFromPage(const wxSize& sizePage) const |
233 | { | |
159e6235 | 234 | // we need to add the size of the list control and the border between |
2ddb4d13 | 235 | const wxSize sizeList = GetControllerSize(); |
e9c0df38 VZ |
236 | |
237 | wxSize size = sizePage; | |
238 | if ( IsVertical() ) | |
239 | { | |
159e6235 | 240 | size.y += sizeList.y + GetInternalBorder(); |
e9c0df38 VZ |
241 | } |
242 | else // left/right aligned | |
243 | { | |
159e6235 | 244 | size.x += sizeList.x + GetInternalBorder(); |
e9c0df38 VZ |
245 | } |
246 | ||
247 | return size; | |
248 | } | |
249 | ||
250 | ||
251 | // ---------------------------------------------------------------------------- | |
252 | // accessing the pages | |
253 | // ---------------------------------------------------------------------------- | |
254 | ||
255 | bool wxListbook::SetPageText(size_t n, const wxString& strText) | |
256 | { | |
2ddb4d13 | 257 | GetListView()->SetItemText(n, strText); |
e9c0df38 VZ |
258 | |
259 | return true; | |
260 | } | |
261 | ||
262 | wxString wxListbook::GetPageText(size_t n) const | |
263 | { | |
2ddb4d13 | 264 | return GetListView()->GetItemText(n); |
e9c0df38 VZ |
265 | } |
266 | ||
c1718122 | 267 | int wxListbook::GetPageImage(size_t n) const |
e9c0df38 | 268 | { |
c1718122 VZ |
269 | wxListItem item; |
270 | item.SetId(n); | |
e9c0df38 | 271 | |
c1718122 VZ |
272 | if (GetListView()->GetItem(item)) |
273 | { | |
274 | return item.GetImage(); | |
275 | } | |
276 | else | |
277 | { | |
278 | return wxNOT_FOUND; | |
279 | } | |
e9c0df38 VZ |
280 | } |
281 | ||
282 | bool wxListbook::SetPageImage(size_t n, int imageId) | |
283 | { | |
2ddb4d13 | 284 | return GetListView()->SetItemImage(n, imageId); |
e9c0df38 VZ |
285 | } |
286 | ||
287 | // ---------------------------------------------------------------------------- | |
288 | // image list stuff | |
289 | // ---------------------------------------------------------------------------- | |
290 | ||
291 | void wxListbook::SetImageList(wxImageList *imageList) | |
292 | { | |
c1718122 VZ |
293 | wxListView * const list = GetListView(); |
294 | ||
1a9a6eed | 295 | #ifdef CAN_USE_REPORT_VIEW |
c1718122 VZ |
296 | // If imageList presence has changed, we update the list control view |
297 | if ( (imageList != NULL) != (GetImageList() != NULL) ) | |
298 | { | |
299 | wxArrayString labels; | |
300 | labels.Alloc(GetPageCount()); | |
301 | ||
302 | wxArrayInt imageIds; | |
303 | imageIds.Alloc(GetPageCount()); | |
304 | ||
305 | const int oldSel = GetSelection(); | |
306 | size_t i; | |
307 | ||
308 | // Grab snapshot of all list control items before changing the window | |
309 | // style (which deletes the items) | |
310 | for ( i = 0; i < GetPageCount(); i++ ) | |
311 | { | |
312 | labels.Add(GetPageText(i)); | |
313 | imageIds.Add(GetPageImage(i)); | |
314 | } | |
315 | ||
316 | // Update the style to use icon view for images, report view otherwise | |
317 | long style = wxLC_SINGLE_SEL; | |
318 | if ( imageList ) | |
319 | { | |
1a9a6eed | 320 | style |= GetListCtrlIconViewFlags(); |
c1718122 VZ |
321 | } |
322 | else // no image list | |
323 | { | |
1a9a6eed | 324 | style |= GetListCtrlReportViewFlags(); |
c1718122 VZ |
325 | } |
326 | ||
1a9a6eed VZ |
327 | list->SetWindowStyleFlag(style); |
328 | if ( !imageList ) | |
329 | list->InsertColumn(0, wxT("Pages")); | |
330 | ||
c1718122 VZ |
331 | // Add back the list control items |
332 | for ( i = 0; i < GetPageCount(); i++ ) | |
333 | { | |
334 | list->InsertItem(i, labels[i], imageIds[i]); | |
335 | } | |
336 | ||
337 | // Restore selection | |
338 | if ( oldSel != wxNOT_FOUND ) | |
339 | SetSelection(oldSel); | |
340 | } | |
341 | ||
342 | list->SetImageList(imageList, wxIMAGE_LIST_NORMAL); | |
1a9a6eed | 343 | #endif // CAN_USE_REPORT_VIEW |
e9c0df38 | 344 | |
61c083e7 | 345 | wxBookCtrlBase::SetImageList(imageList); |
e9c0df38 VZ |
346 | } |
347 | ||
348 | // ---------------------------------------------------------------------------- | |
349 | // selection | |
350 | // ---------------------------------------------------------------------------- | |
351 | ||
1d6fcbcc VZ |
352 | void wxListbook::UpdateSelectedPage(size_t newsel) |
353 | { | |
354 | m_selection = newsel; | |
355 | GetListView()->Select(newsel); | |
356 | GetListView()->Focus(newsel); | |
357 | } | |
358 | ||
e9c0df38 VZ |
359 | int wxListbook::GetSelection() const |
360 | { | |
361 | return m_selection; | |
362 | } | |
363 | ||
deb325e3 | 364 | wxBookCtrlBaseEvent* wxListbook::CreatePageChangingEvent() const |
e9c0df38 | 365 | { |
deb325e3 | 366 | return new wxListbookEvent(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING, m_windowId); |
e9c0df38 VZ |
367 | } |
368 | ||
deb325e3 VZ |
369 | void wxListbook::MakeChangedEvent(wxBookCtrlBaseEvent &event) |
370 | { | |
371 | event.SetEventType(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED); | |
372 | } | |
373 | ||
374 | ||
e9c0df38 VZ |
375 | // ---------------------------------------------------------------------------- |
376 | // adding/removing the pages | |
377 | // ---------------------------------------------------------------------------- | |
378 | ||
379 | bool | |
380 | wxListbook::InsertPage(size_t n, | |
381 | wxWindow *page, | |
382 | const wxString& text, | |
383 | bool bSelect, | |
384 | int imageId) | |
385 | { | |
61c083e7 | 386 | if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) ) |
e9c0df38 VZ |
387 | return false; |
388 | ||
2ddb4d13 | 389 | GetListView()->InsertItem(n, text, imageId); |
e9c0df38 | 390 | |
716dc245 WS |
391 | // if the inserted page is before the selected one, we must update the |
392 | // index of the selected page | |
393 | if ( int(n) <= m_selection ) | |
e9c0df38 | 394 | { |
42841dfc | 395 | // one extra page added |
716dc245 | 396 | m_selection++; |
2ddb4d13 WS |
397 | GetListView()->Select(m_selection); |
398 | GetListView()->Focus(m_selection); | |
e9c0df38 | 399 | } |
716dc245 WS |
400 | |
401 | // some page should be selected: either this one or the first one if there | |
402 | // is still no selection | |
403 | int selNew = -1; | |
404 | if ( bSelect ) | |
405 | selNew = n; | |
406 | else if ( m_selection == -1 ) | |
407 | selNew = 0; | |
408 | ||
409 | if ( selNew != m_selection ) | |
e9c0df38 | 410 | page->Hide(); |
716dc245 WS |
411 | |
412 | if ( selNew != -1 ) | |
413 | SetSelection(selNew); | |
e9c0df38 | 414 | |
a9a56347 RR |
415 | wxSizeEvent sz(GetSize(), GetId()); |
416 | GetEventHandler()->ProcessEvent(sz); | |
417 | ||
e9c0df38 VZ |
418 | return true; |
419 | } | |
420 | ||
421 | wxWindow *wxListbook::DoRemovePage(size_t page) | |
422 | { | |
4a10ea8b | 423 | const size_t page_count = GetPageCount(); |
61c083e7 | 424 | wxWindow *win = wxBookCtrlBase::DoRemovePage(page); |
bb08a4a1 | 425 | |
e9c0df38 VZ |
426 | if ( win ) |
427 | { | |
2ddb4d13 | 428 | GetListView()->DeleteItem(page); |
33ebfc3b VZ |
429 | |
430 | if (m_selection >= (int)page) | |
431 | { | |
432 | // force new sel valid if possible | |
433 | int sel = m_selection - 1; | |
434 | if (page_count == 1) | |
bb08a4a1 | 435 | sel = wxNOT_FOUND; |
33ebfc3b VZ |
436 | else if ((page_count == 2) || (sel == -1)) |
437 | sel = 0; | |
bb08a4a1 | 438 | |
33ebfc3b | 439 | // force sel invalid if deleting current page - don't try to hide it |
bb08a4a1 WS |
440 | m_selection = (m_selection == (int)page) ? wxNOT_FOUND : m_selection - 1; |
441 | ||
442 | if ((sel != wxNOT_FOUND) && (sel != m_selection)) | |
33ebfc3b VZ |
443 | SetSelection(sel); |
444 | } | |
9f29226d | 445 | |
2ddb4d13 | 446 | GetListView()->Arrange(); |
6a82a0d0 JS |
447 | if (GetPageCount() == 0) |
448 | { | |
449 | wxSizeEvent sz(GetSize(), GetId()); | |
eafd76b0 | 450 | GetEventHandler()->ProcessEvent(sz); |
6a82a0d0 | 451 | } |
e9c0df38 VZ |
452 | } |
453 | ||
454 | return win; | |
455 | } | |
456 | ||
fbd11d30 RD |
457 | |
458 | bool wxListbook::DeleteAllPages() | |
459 | { | |
2ddb4d13 | 460 | GetListView()->DeleteAllItems(); |
6a82a0d0 JS |
461 | if (!wxBookCtrlBase::DeleteAllPages()) |
462 | return false; | |
9eddec69 | 463 | |
6a82a0d0 JS |
464 | m_selection = -1; |
465 | ||
466 | wxSizeEvent sz(GetSize(), GetId()); | |
eafd76b0 | 467 | GetEventHandler()->ProcessEvent(sz); |
9eddec69 | 468 | |
6a82a0d0 | 469 | return true; |
fbd11d30 RD |
470 | } |
471 | ||
e9c0df38 VZ |
472 | // ---------------------------------------------------------------------------- |
473 | // wxListbook events | |
474 | // ---------------------------------------------------------------------------- | |
475 | ||
476 | void wxListbook::OnListSelected(wxListEvent& eventList) | |
477 | { | |
447325a4 VZ |
478 | if ( eventList.GetEventObject() != m_bookctrl ) |
479 | { | |
480 | eventList.Skip(); | |
481 | return; | |
482 | } | |
483 | ||
e9c0df38 VZ |
484 | const int selNew = eventList.GetIndex(); |
485 | ||
486 | if ( selNew == m_selection ) | |
487 | { | |
488 | // this event can only come from our own Select(m_selection) below | |
489 | // which we call when the page change is vetoed, so we should simply | |
490 | // ignore it | |
491 | return; | |
492 | } | |
493 | ||
716dc245 | 494 | SetSelection(selNew); |
e9c0df38 | 495 | |
716dc245 WS |
496 | // change wasn't allowed, return to previous state |
497 | if (m_selection != selNew) | |
e9c0df38 | 498 | { |
2ddb4d13 WS |
499 | GetListView()->Select(m_selection); |
500 | GetListView()->Focus(m_selection); | |
e9c0df38 | 501 | } |
e9c0df38 VZ |
502 | } |
503 | ||
504 | #endif // wxUSE_LISTBOOK |