]>
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 | |
0dc44daa VZ |
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 | |
1a9a6eed | 53 | #endif |
0dc44daa VZ |
54 | return true; |
55 | } | |
56 | ||
57 | } // anonymous namespace | |
1a9a6eed | 58 | |
e9c0df38 | 59 | // ---------------------------------------------------------------------------- |
77ffb593 | 60 | // various wxWidgets macros |
e9c0df38 VZ |
61 | // ---------------------------------------------------------------------------- |
62 | ||
1f30c176 WS |
63 | // check that the page index is valid |
64 | #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount()) | |
65 | ||
66 | // ---------------------------------------------------------------------------- | |
67 | // event table | |
68 | // ---------------------------------------------------------------------------- | |
69 | ||
2ddb4d13 | 70 | IMPLEMENT_DYNAMIC_CLASS(wxListbook, wxBookCtrlBase) |
e9c0df38 | 71 | |
9b11752c VZ |
72 | wxDEFINE_EVENT( wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING, wxBookCtrlEvent ); |
73 | wxDEFINE_EVENT( wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED, wxBookCtrlEvent ); | |
e9c0df38 | 74 | |
61c083e7 | 75 | BEGIN_EVENT_TABLE(wxListbook, wxBookCtrlBase) |
e9c0df38 | 76 | EVT_SIZE(wxListbook::OnSize) |
447325a4 | 77 | EVT_LIST_ITEM_SELECTED(wxID_ANY, wxListbook::OnListSelected) |
e9c0df38 VZ |
78 | END_EVENT_TABLE() |
79 | ||
80 | // ============================================================================ | |
81 | // wxListbook implementation | |
82 | // ============================================================================ | |
83 | ||
84 | // ---------------------------------------------------------------------------- | |
85 | // wxListbook creation | |
86 | // ---------------------------------------------------------------------------- | |
87 | ||
88 | void wxListbook::Init() | |
89 | { | |
e9c0df38 VZ |
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 | { | |
2ddb4d13 | 101 | if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT ) |
e9c0df38 VZ |
102 | { |
103 | #ifdef __WXMAC__ | |
2ddb4d13 | 104 | style |= wxBK_TOP; |
e9c0df38 | 105 | #else // !__WXMAC__ |
2ddb4d13 | 106 | style |= wxBK_LEFT; |
e9c0df38 VZ |
107 | #endif // __WXMAC__/!__WXMAC__ |
108 | } | |
109 | ||
ef0120c1 VZ |
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 | ||
e9c0df38 VZ |
115 | if ( !wxControl::Create(parent, id, pos, size, style, |
116 | wxDefaultValidator, name) ) | |
117 | return false; | |
118 | ||
2ddb4d13 | 119 | m_bookctrl = new wxListView |
e9c0df38 VZ |
120 | ( |
121 | this, | |
447325a4 | 122 | wxID_ANY, |
e9c0df38 VZ |
123 | wxDefaultPosition, |
124 | wxDefaultSize, | |
1a9a6eed | 125 | wxLC_SINGLE_SEL | |
0dc44daa VZ |
126 | (CanUseReportView() ? GetListCtrlReportViewFlags() |
127 | : GetListCtrlIconViewFlags()) | |
e9c0df38 VZ |
128 | ); |
129 | ||
0dc44daa VZ |
130 | if ( CanUseReportView() ) |
131 | GetListView()->InsertColumn(0, wxT("Pages")); | |
c1718122 | 132 | |
a9092ede | 133 | #ifdef __WXMSW__ |
2ddb4d13 | 134 | // On XP with themes enabled the GetViewRect used in GetControllerSize() to |
a9092ede | 135 | // determine the space needed for the list view will incorrectly return |
7b45094a | 136 | // (0,0,0,0) the first time. So send a pending event so OnSize will be |
a9092ede RD |
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 | |
e9c0df38 VZ |
143 | return true; |
144 | } | |
145 | ||
1a9a6eed VZ |
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 | ||
1a9a6eed VZ |
155 | long wxListbook::GetListCtrlReportViewFlags() const |
156 | { | |
157 | return wxLC_REPORT | wxLC_NO_HEADER; | |
158 | } | |
159 | ||
e9c0df38 VZ |
160 | // ---------------------------------------------------------------------------- |
161 | // wxListbook geometry management | |
162 | // ---------------------------------------------------------------------------- | |
163 | ||
e9c0df38 VZ |
164 | void wxListbook::OnSize(wxSizeEvent& event) |
165 | { | |
9f29226d WS |
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) | |
905069b1 VZ |
171 | wxListView * const list = GetListView(); |
172 | if ( list ) | |
173 | list->Arrange(); | |
174 | ||
175 | event.Skip(); | |
e9c0df38 VZ |
176 | } |
177 | ||
d0a84b63 | 178 | int wxListbook::HitTest(const wxPoint& pt, long *flags) const |
851b88c3 VZ |
179 | { |
180 | int pagePos = wxNOT_FOUND; | |
181 | ||
d0a84b63 | 182 | if ( flags ) |
9804d540 | 183 | *flags = wxBK_HITTEST_NOWHERE; |
851b88c3 | 184 | |
d0a84b63 VZ |
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? | |
22a35096 | 190 | if ( wxRect(list->GetSize()).Contains(listPt) ) |
851b88c3 VZ |
191 | { |
192 | int flagsList; | |
d0a84b63 | 193 | pagePos = list->HitTest(listPt, flagsList); |
851b88c3 | 194 | |
d0a84b63 | 195 | if ( flags ) |
851b88c3 | 196 | { |
d0a84b63 VZ |
197 | if ( pagePos != wxNOT_FOUND ) |
198 | *flags = 0; | |
199 | ||
200 | if ( flagsList & (wxLIST_HITTEST_ONITEMICON | | |
201 | wxLIST_HITTEST_ONITEMSTATEICON ) ) | |
9804d540 | 202 | *flags |= wxBK_HITTEST_ONICON; |
d0a84b63 VZ |
203 | |
204 | if ( flagsList & wxLIST_HITTEST_ONITEMLABEL ) | |
9804d540 | 205 | *flags |= wxBK_HITTEST_ONLABEL; |
851b88c3 VZ |
206 | } |
207 | } | |
d0a84b63 VZ |
208 | else // not over list control at all |
209 | { | |
22a35096 | 210 | if ( flags && GetPageRect().Contains(pt) ) |
9804d540 | 211 | *flags |= wxBK_HITTEST_ONPAGE; |
d0a84b63 | 212 | } |
851b88c3 VZ |
213 | |
214 | return pagePos; | |
215 | } | |
216 | ||
905069b1 VZ |
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 | } | |
e9c0df38 VZ |
224 | |
225 | // ---------------------------------------------------------------------------- | |
226 | // accessing the pages | |
227 | // ---------------------------------------------------------------------------- | |
228 | ||
229 | bool wxListbook::SetPageText(size_t n, const wxString& strText) | |
230 | { | |
2ddb4d13 | 231 | GetListView()->SetItemText(n, strText); |
e9c0df38 VZ |
232 | |
233 | return true; | |
234 | } | |
235 | ||
236 | wxString wxListbook::GetPageText(size_t n) const | |
237 | { | |
2ddb4d13 | 238 | return GetListView()->GetItemText(n); |
e9c0df38 VZ |
239 | } |
240 | ||
c1718122 | 241 | int wxListbook::GetPageImage(size_t n) const |
e9c0df38 | 242 | { |
c1718122 VZ |
243 | wxListItem item; |
244 | item.SetId(n); | |
e9c0df38 | 245 | |
c1718122 VZ |
246 | if (GetListView()->GetItem(item)) |
247 | { | |
248 | return item.GetImage(); | |
249 | } | |
250 | else | |
251 | { | |
252 | return wxNOT_FOUND; | |
253 | } | |
e9c0df38 VZ |
254 | } |
255 | ||
256 | bool wxListbook::SetPageImage(size_t n, int imageId) | |
257 | { | |
2ddb4d13 | 258 | return GetListView()->SetItemImage(n, imageId); |
e9c0df38 VZ |
259 | } |
260 | ||
261 | // ---------------------------------------------------------------------------- | |
262 | // image list stuff | |
263 | // ---------------------------------------------------------------------------- | |
264 | ||
265 | void wxListbook::SetImageList(wxImageList *imageList) | |
266 | { | |
0dc44daa | 267 | if ( CanUseReportView() ) |
c1718122 | 268 | { |
0dc44daa | 269 | wxListView * const list = GetListView(); |
c1718122 | 270 | |
0dc44daa VZ |
271 | // If imageList presence has changed, we update the list control view |
272 | if ( (imageList != NULL) != (GetImageList() != NULL) ) | |
c1718122 | 273 | { |
0dc44daa VZ |
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); | |
c1718122 VZ |
295 | } |
296 | ||
0dc44daa | 297 | list->SetImageList(imageList, wxIMAGE_LIST_NORMAL); |
c1718122 VZ |
298 | } |
299 | ||
61c083e7 | 300 | wxBookCtrlBase::SetImageList(imageList); |
e9c0df38 VZ |
301 | } |
302 | ||
303 | // ---------------------------------------------------------------------------- | |
304 | // selection | |
305 | // ---------------------------------------------------------------------------- | |
306 | ||
1d6fcbcc VZ |
307 | void wxListbook::UpdateSelectedPage(size_t newsel) |
308 | { | |
309 | m_selection = newsel; | |
310 | GetListView()->Select(newsel); | |
311 | GetListView()->Focus(newsel); | |
312 | } | |
313 | ||
e9c0df38 VZ |
314 | int wxListbook::GetSelection() const |
315 | { | |
316 | return m_selection; | |
317 | } | |
318 | ||
3e97a905 | 319 | wxBookCtrlEvent* wxListbook::CreatePageChangingEvent() const |
e9c0df38 | 320 | { |
3e97a905 | 321 | return new wxBookCtrlEvent(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING, m_windowId); |
e9c0df38 VZ |
322 | } |
323 | ||
3e97a905 | 324 | void wxListbook::MakeChangedEvent(wxBookCtrlEvent &event) |
deb325e3 VZ |
325 | { |
326 | event.SetEventType(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED); | |
327 | } | |
328 | ||
329 | ||
e9c0df38 VZ |
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 | { | |
61c083e7 | 341 | if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) ) |
e9c0df38 VZ |
342 | return false; |
343 | ||
2ddb4d13 | 344 | GetListView()->InsertItem(n, text, imageId); |
e9c0df38 | 345 | |
716dc245 WS |
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 ) | |
e9c0df38 | 349 | { |
42841dfc | 350 | // one extra page added |
716dc245 | 351 | m_selection++; |
2ddb4d13 WS |
352 | GetListView()->Select(m_selection); |
353 | GetListView()->Focus(m_selection); | |
e9c0df38 | 354 | } |
716dc245 WS |
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 ) | |
e9c0df38 | 365 | page->Hide(); |
716dc245 WS |
366 | |
367 | if ( selNew != -1 ) | |
368 | SetSelection(selNew); | |
e9c0df38 | 369 | |
905069b1 VZ |
370 | UpdateSize(); |
371 | ||
e9c0df38 VZ |
372 | return true; |
373 | } | |
374 | ||
375 | wxWindow *wxListbook::DoRemovePage(size_t page) | |
376 | { | |
4a10ea8b | 377 | const size_t page_count = GetPageCount(); |
61c083e7 | 378 | wxWindow *win = wxBookCtrlBase::DoRemovePage(page); |
bb08a4a1 | 379 | |
e9c0df38 VZ |
380 | if ( win ) |
381 | { | |
2ddb4d13 | 382 | GetListView()->DeleteItem(page); |
33ebfc3b VZ |
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) | |
bb08a4a1 | 389 | sel = wxNOT_FOUND; |
33ebfc3b VZ |
390 | else if ((page_count == 2) || (sel == -1)) |
391 | sel = 0; | |
bb08a4a1 | 392 | |
33ebfc3b | 393 | // force sel invalid if deleting current page - don't try to hide it |
bb08a4a1 WS |
394 | m_selection = (m_selection == (int)page) ? wxNOT_FOUND : m_selection - 1; |
395 | ||
396 | if ((sel != wxNOT_FOUND) && (sel != m_selection)) | |
33ebfc3b VZ |
397 | SetSelection(sel); |
398 | } | |
9f29226d | 399 | |
2ddb4d13 | 400 | GetListView()->Arrange(); |
905069b1 | 401 | UpdateSize(); |
e9c0df38 VZ |
402 | } |
403 | ||
404 | return win; | |
405 | } | |
406 | ||
fbd11d30 RD |
407 | |
408 | bool wxListbook::DeleteAllPages() | |
409 | { | |
2ddb4d13 | 410 | GetListView()->DeleteAllItems(); |
6a82a0d0 JS |
411 | if (!wxBookCtrlBase::DeleteAllPages()) |
412 | return false; | |
9eddec69 | 413 | |
6a82a0d0 JS |
414 | m_selection = -1; |
415 | ||
905069b1 | 416 | UpdateSize(); |
9eddec69 | 417 | |
6a82a0d0 | 418 | return true; |
fbd11d30 RD |
419 | } |
420 | ||
e9c0df38 VZ |
421 | // ---------------------------------------------------------------------------- |
422 | // wxListbook events | |
423 | // ---------------------------------------------------------------------------- | |
424 | ||
425 | void wxListbook::OnListSelected(wxListEvent& eventList) | |
426 | { | |
447325a4 VZ |
427 | if ( eventList.GetEventObject() != m_bookctrl ) |
428 | { | |
429 | eventList.Skip(); | |
430 | return; | |
431 | } | |
432 | ||
e9c0df38 VZ |
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 | ||
716dc245 | 443 | SetSelection(selNew); |
e9c0df38 | 444 | |
716dc245 WS |
445 | // change wasn't allowed, return to previous state |
446 | if (m_selection != selNew) | |
e9c0df38 | 447 | { |
2ddb4d13 WS |
448 | GetListView()->Select(m_selection); |
449 | GetListView()->Focus(m_selection); | |
e9c0df38 | 450 | } |
e9c0df38 VZ |
451 | } |
452 | ||
453 | #endif // wxUSE_LISTBOOK |