]>
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 | 57 | |
9b11752c VZ |
58 | wxDEFINE_EVENT( wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING, wxBookCtrlEvent ); |
59 | wxDEFINE_EVENT( wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED, wxBookCtrlEvent ); | |
e9c0df38 | 60 | |
61c083e7 | 61 | BEGIN_EVENT_TABLE(wxListbook, wxBookCtrlBase) |
e9c0df38 | 62 | EVT_SIZE(wxListbook::OnSize) |
447325a4 | 63 | EVT_LIST_ITEM_SELECTED(wxID_ANY, wxListbook::OnListSelected) |
e9c0df38 VZ |
64 | END_EVENT_TABLE() |
65 | ||
66 | // ============================================================================ | |
67 | // wxListbook implementation | |
68 | // ============================================================================ | |
69 | ||
70 | // ---------------------------------------------------------------------------- | |
71 | // wxListbook creation | |
72 | // ---------------------------------------------------------------------------- | |
73 | ||
74 | void wxListbook::Init() | |
75 | { | |
e9c0df38 VZ |
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 | { | |
2ddb4d13 | 87 | if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT ) |
e9c0df38 VZ |
88 | { |
89 | #ifdef __WXMAC__ | |
2ddb4d13 | 90 | style |= wxBK_TOP; |
e9c0df38 | 91 | #else // !__WXMAC__ |
2ddb4d13 | 92 | style |= wxBK_LEFT; |
e9c0df38 VZ |
93 | #endif // __WXMAC__/!__WXMAC__ |
94 | } | |
95 | ||
ef0120c1 VZ |
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 | ||
e9c0df38 VZ |
101 | if ( !wxControl::Create(parent, id, pos, size, style, |
102 | wxDefaultValidator, name) ) | |
103 | return false; | |
104 | ||
2ddb4d13 | 105 | m_bookctrl = new wxListView |
e9c0df38 VZ |
106 | ( |
107 | this, | |
447325a4 | 108 | wxID_ANY, |
e9c0df38 VZ |
109 | wxDefaultPosition, |
110 | wxDefaultSize, | |
1a9a6eed VZ |
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 | |
e9c0df38 VZ |
117 | ); |
118 | ||
1a9a6eed | 119 | #ifdef CAN_USE_REPORT_VIEW |
c1718122 | 120 | GetListView()->InsertColumn(0, wxT("Pages")); |
1a9a6eed | 121 | #endif // CAN_USE_REPORT_VIEW |
c1718122 | 122 | |
a9092ede | 123 | #ifdef __WXMSW__ |
2ddb4d13 | 124 | // On XP with themes enabled the GetViewRect used in GetControllerSize() to |
a9092ede | 125 | // determine the space needed for the list view will incorrectly return |
7b45094a | 126 | // (0,0,0,0) the first time. So send a pending event so OnSize will be |
a9092ede RD |
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 | |
e9c0df38 VZ |
133 | return true; |
134 | } | |
135 | ||
1a9a6eed VZ |
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 | ||
e9c0df38 VZ |
154 | // ---------------------------------------------------------------------------- |
155 | // wxListbook geometry management | |
156 | // ---------------------------------------------------------------------------- | |
157 | ||
e9c0df38 VZ |
158 | void wxListbook::OnSize(wxSizeEvent& event) |
159 | { | |
9f29226d WS |
160 | // arrange the icons before calling SetClientSize(), otherwise it wouldn't |
161 | // account for the scrollbars the list control might need and, at least | |
162 | // under MSW, we'd finish with an ugly looking list control with both | |
163 | // vertical and horizontal scrollbar (with one of them being added because | |
164 | // the other one is not accounted for in client size computations) | |
905069b1 VZ |
165 | wxListView * const list = GetListView(); |
166 | if ( list ) | |
167 | list->Arrange(); | |
168 | ||
169 | event.Skip(); | |
e9c0df38 VZ |
170 | } |
171 | ||
d0a84b63 | 172 | int wxListbook::HitTest(const wxPoint& pt, long *flags) const |
851b88c3 VZ |
173 | { |
174 | int pagePos = wxNOT_FOUND; | |
175 | ||
d0a84b63 | 176 | if ( flags ) |
9804d540 | 177 | *flags = wxBK_HITTEST_NOWHERE; |
851b88c3 | 178 | |
d0a84b63 VZ |
179 | // convert from listbook control coordinates to list control coordinates |
180 | const wxListView * const list = GetListView(); | |
181 | const wxPoint listPt = list->ScreenToClient(ClientToScreen(pt)); | |
182 | ||
183 | // is the point inside list control? | |
22a35096 | 184 | if ( wxRect(list->GetSize()).Contains(listPt) ) |
851b88c3 VZ |
185 | { |
186 | int flagsList; | |
d0a84b63 | 187 | pagePos = list->HitTest(listPt, flagsList); |
851b88c3 | 188 | |
d0a84b63 | 189 | if ( flags ) |
851b88c3 | 190 | { |
d0a84b63 VZ |
191 | if ( pagePos != wxNOT_FOUND ) |
192 | *flags = 0; | |
193 | ||
194 | if ( flagsList & (wxLIST_HITTEST_ONITEMICON | | |
195 | wxLIST_HITTEST_ONITEMSTATEICON ) ) | |
9804d540 | 196 | *flags |= wxBK_HITTEST_ONICON; |
d0a84b63 VZ |
197 | |
198 | if ( flagsList & wxLIST_HITTEST_ONITEMLABEL ) | |
9804d540 | 199 | *flags |= wxBK_HITTEST_ONLABEL; |
851b88c3 VZ |
200 | } |
201 | } | |
d0a84b63 VZ |
202 | else // not over list control at all |
203 | { | |
22a35096 | 204 | if ( flags && GetPageRect().Contains(pt) ) |
9804d540 | 205 | *flags |= wxBK_HITTEST_ONPAGE; |
d0a84b63 | 206 | } |
851b88c3 VZ |
207 | |
208 | return pagePos; | |
209 | } | |
210 | ||
905069b1 VZ |
211 | void wxListbook::UpdateSize() |
212 | { | |
213 | // we should find a more elegant way to force a layout than generating this | |
214 | // dummy event | |
215 | wxSizeEvent sz(GetSize(), GetId()); | |
216 | GetEventHandler()->ProcessEvent(sz); | |
217 | } | |
e9c0df38 VZ |
218 | |
219 | // ---------------------------------------------------------------------------- | |
220 | // accessing the pages | |
221 | // ---------------------------------------------------------------------------- | |
222 | ||
223 | bool wxListbook::SetPageText(size_t n, const wxString& strText) | |
224 | { | |
2ddb4d13 | 225 | GetListView()->SetItemText(n, strText); |
e9c0df38 VZ |
226 | |
227 | return true; | |
228 | } | |
229 | ||
230 | wxString wxListbook::GetPageText(size_t n) const | |
231 | { | |
2ddb4d13 | 232 | return GetListView()->GetItemText(n); |
e9c0df38 VZ |
233 | } |
234 | ||
c1718122 | 235 | int wxListbook::GetPageImage(size_t n) const |
e9c0df38 | 236 | { |
c1718122 VZ |
237 | wxListItem item; |
238 | item.SetId(n); | |
e9c0df38 | 239 | |
c1718122 VZ |
240 | if (GetListView()->GetItem(item)) |
241 | { | |
242 | return item.GetImage(); | |
243 | } | |
244 | else | |
245 | { | |
246 | return wxNOT_FOUND; | |
247 | } | |
e9c0df38 VZ |
248 | } |
249 | ||
250 | bool wxListbook::SetPageImage(size_t n, int imageId) | |
251 | { | |
2ddb4d13 | 252 | return GetListView()->SetItemImage(n, imageId); |
e9c0df38 VZ |
253 | } |
254 | ||
255 | // ---------------------------------------------------------------------------- | |
256 | // image list stuff | |
257 | // ---------------------------------------------------------------------------- | |
258 | ||
259 | void wxListbook::SetImageList(wxImageList *imageList) | |
260 | { | |
c22ace4d | 261 | #ifdef CAN_USE_REPORT_VIEW |
c1718122 VZ |
262 | wxListView * const list = GetListView(); |
263 | ||
264 | // If imageList presence has changed, we update the list control view | |
265 | if ( (imageList != NULL) != (GetImageList() != NULL) ) | |
266 | { | |
bd7bd09e | 267 | // Preserve the selection which is lost when changing the mode |
c1718122 | 268 | const int oldSel = GetSelection(); |
c1718122 VZ |
269 | |
270 | // Update the style to use icon view for images, report view otherwise | |
271 | long style = wxLC_SINGLE_SEL; | |
272 | if ( imageList ) | |
273 | { | |
1a9a6eed | 274 | style |= GetListCtrlIconViewFlags(); |
c1718122 VZ |
275 | } |
276 | else // no image list | |
277 | { | |
1a9a6eed | 278 | style |= GetListCtrlReportViewFlags(); |
c1718122 VZ |
279 | } |
280 | ||
1a9a6eed VZ |
281 | list->SetWindowStyleFlag(style); |
282 | if ( !imageList ) | |
283 | list->InsertColumn(0, wxT("Pages")); | |
284 | ||
c1718122 VZ |
285 | // Restore selection |
286 | if ( oldSel != wxNOT_FOUND ) | |
287 | SetSelection(oldSel); | |
288 | } | |
289 | ||
290 | list->SetImageList(imageList, wxIMAGE_LIST_NORMAL); | |
1a9a6eed | 291 | #endif // CAN_USE_REPORT_VIEW |
e9c0df38 | 292 | |
61c083e7 | 293 | wxBookCtrlBase::SetImageList(imageList); |
e9c0df38 VZ |
294 | } |
295 | ||
296 | // ---------------------------------------------------------------------------- | |
297 | // selection | |
298 | // ---------------------------------------------------------------------------- | |
299 | ||
1d6fcbcc VZ |
300 | void wxListbook::UpdateSelectedPage(size_t newsel) |
301 | { | |
302 | m_selection = newsel; | |
303 | GetListView()->Select(newsel); | |
304 | GetListView()->Focus(newsel); | |
305 | } | |
306 | ||
e9c0df38 VZ |
307 | int wxListbook::GetSelection() const |
308 | { | |
309 | return m_selection; | |
310 | } | |
311 | ||
3e97a905 | 312 | wxBookCtrlEvent* wxListbook::CreatePageChangingEvent() const |
e9c0df38 | 313 | { |
3e97a905 | 314 | return new wxBookCtrlEvent(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING, m_windowId); |
e9c0df38 VZ |
315 | } |
316 | ||
3e97a905 | 317 | void wxListbook::MakeChangedEvent(wxBookCtrlEvent &event) |
deb325e3 VZ |
318 | { |
319 | event.SetEventType(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED); | |
320 | } | |
321 | ||
322 | ||
e9c0df38 VZ |
323 | // ---------------------------------------------------------------------------- |
324 | // adding/removing the pages | |
325 | // ---------------------------------------------------------------------------- | |
326 | ||
327 | bool | |
328 | wxListbook::InsertPage(size_t n, | |
329 | wxWindow *page, | |
330 | const wxString& text, | |
331 | bool bSelect, | |
332 | int imageId) | |
333 | { | |
61c083e7 | 334 | if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) ) |
e9c0df38 VZ |
335 | return false; |
336 | ||
2ddb4d13 | 337 | GetListView()->InsertItem(n, text, imageId); |
e9c0df38 | 338 | |
716dc245 WS |
339 | // if the inserted page is before the selected one, we must update the |
340 | // index of the selected page | |
341 | if ( int(n) <= m_selection ) | |
e9c0df38 | 342 | { |
42841dfc | 343 | // one extra page added |
716dc245 | 344 | m_selection++; |
2ddb4d13 WS |
345 | GetListView()->Select(m_selection); |
346 | GetListView()->Focus(m_selection); | |
e9c0df38 | 347 | } |
716dc245 WS |
348 | |
349 | // some page should be selected: either this one or the first one if there | |
350 | // is still no selection | |
351 | int selNew = -1; | |
352 | if ( bSelect ) | |
353 | selNew = n; | |
354 | else if ( m_selection == -1 ) | |
355 | selNew = 0; | |
356 | ||
357 | if ( selNew != m_selection ) | |
e9c0df38 | 358 | page->Hide(); |
716dc245 WS |
359 | |
360 | if ( selNew != -1 ) | |
361 | SetSelection(selNew); | |
e9c0df38 | 362 | |
905069b1 VZ |
363 | UpdateSize(); |
364 | ||
e9c0df38 VZ |
365 | return true; |
366 | } | |
367 | ||
368 | wxWindow *wxListbook::DoRemovePage(size_t page) | |
369 | { | |
4a10ea8b | 370 | const size_t page_count = GetPageCount(); |
61c083e7 | 371 | wxWindow *win = wxBookCtrlBase::DoRemovePage(page); |
bb08a4a1 | 372 | |
e9c0df38 VZ |
373 | if ( win ) |
374 | { | |
2ddb4d13 | 375 | GetListView()->DeleteItem(page); |
33ebfc3b VZ |
376 | |
377 | if (m_selection >= (int)page) | |
378 | { | |
379 | // force new sel valid if possible | |
380 | int sel = m_selection - 1; | |
381 | if (page_count == 1) | |
bb08a4a1 | 382 | sel = wxNOT_FOUND; |
33ebfc3b VZ |
383 | else if ((page_count == 2) || (sel == -1)) |
384 | sel = 0; | |
bb08a4a1 | 385 | |
33ebfc3b | 386 | // force sel invalid if deleting current page - don't try to hide it |
bb08a4a1 WS |
387 | m_selection = (m_selection == (int)page) ? wxNOT_FOUND : m_selection - 1; |
388 | ||
389 | if ((sel != wxNOT_FOUND) && (sel != m_selection)) | |
33ebfc3b VZ |
390 | SetSelection(sel); |
391 | } | |
9f29226d | 392 | |
2ddb4d13 | 393 | GetListView()->Arrange(); |
905069b1 | 394 | UpdateSize(); |
e9c0df38 VZ |
395 | } |
396 | ||
397 | return win; | |
398 | } | |
399 | ||
fbd11d30 RD |
400 | |
401 | bool wxListbook::DeleteAllPages() | |
402 | { | |
2ddb4d13 | 403 | GetListView()->DeleteAllItems(); |
6a82a0d0 JS |
404 | if (!wxBookCtrlBase::DeleteAllPages()) |
405 | return false; | |
9eddec69 | 406 | |
6a82a0d0 JS |
407 | m_selection = -1; |
408 | ||
905069b1 | 409 | UpdateSize(); |
9eddec69 | 410 | |
6a82a0d0 | 411 | return true; |
fbd11d30 RD |
412 | } |
413 | ||
e9c0df38 VZ |
414 | // ---------------------------------------------------------------------------- |
415 | // wxListbook events | |
416 | // ---------------------------------------------------------------------------- | |
417 | ||
418 | void wxListbook::OnListSelected(wxListEvent& eventList) | |
419 | { | |
447325a4 VZ |
420 | if ( eventList.GetEventObject() != m_bookctrl ) |
421 | { | |
422 | eventList.Skip(); | |
423 | return; | |
424 | } | |
425 | ||
e9c0df38 VZ |
426 | const int selNew = eventList.GetIndex(); |
427 | ||
428 | if ( selNew == m_selection ) | |
429 | { | |
430 | // this event can only come from our own Select(m_selection) below | |
431 | // which we call when the page change is vetoed, so we should simply | |
432 | // ignore it | |
433 | return; | |
434 | } | |
435 | ||
716dc245 | 436 | SetSelection(selNew); |
e9c0df38 | 437 | |
716dc245 WS |
438 | // change wasn't allowed, return to previous state |
439 | if (m_selection != selNew) | |
e9c0df38 | 440 | { |
2ddb4d13 WS |
441 | GetListView()->Select(m_selection); |
442 | GetListView()->Focus(m_selection); | |
e9c0df38 | 443 | } |
e9c0df38 VZ |
444 | } |
445 | ||
446 | #endif // wxUSE_LISTBOOK |