]>
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 | |
e9c0df38 | 7 | // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org> |
65571936 | 8 | // Licence: wxWindows licence |
e9c0df38 VZ |
9 | /////////////////////////////////////////////////////////////////////////////// |
10 | ||
11 | // ============================================================================ | |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // ---------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
e9c0df38 VZ |
19 | // For compilers that support precompilation, includes "wx.h". |
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
26 | #if wxUSE_LISTBOOK | |
27 | ||
9eddec69 WS |
28 | #include "wx/listbook.h" |
29 | ||
30 | #ifndef WX_PRECOMP | |
31 | #include "wx/settings.h" | |
32 | #endif | |
33 | ||
e9c0df38 VZ |
34 | #include "wx/listctrl.h" |
35 | #include "wx/statline.h" | |
5b24f0d3 | 36 | #include "wx/imaglist.h" |
e9c0df38 | 37 | |
e9c0df38 | 38 | // ---------------------------------------------------------------------------- |
77ffb593 | 39 | // various wxWidgets macros |
e9c0df38 VZ |
40 | // ---------------------------------------------------------------------------- |
41 | ||
1f30c176 WS |
42 | // check that the page index is valid |
43 | #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount()) | |
44 | ||
45 | // ---------------------------------------------------------------------------- | |
46 | // event table | |
47 | // ---------------------------------------------------------------------------- | |
48 | ||
2ddb4d13 | 49 | IMPLEMENT_DYNAMIC_CLASS(wxListbook, wxBookCtrlBase) |
e9c0df38 | 50 | |
ce7fe42e VZ |
51 | wxDEFINE_EVENT( wxEVT_LISTBOOK_PAGE_CHANGING, wxBookCtrlEvent ); |
52 | wxDEFINE_EVENT( wxEVT_LISTBOOK_PAGE_CHANGED, wxBookCtrlEvent ); | |
e9c0df38 | 53 | |
61c083e7 | 54 | BEGIN_EVENT_TABLE(wxListbook, wxBookCtrlBase) |
e9c0df38 | 55 | EVT_SIZE(wxListbook::OnSize) |
447325a4 | 56 | EVT_LIST_ITEM_SELECTED(wxID_ANY, wxListbook::OnListSelected) |
e9c0df38 VZ |
57 | END_EVENT_TABLE() |
58 | ||
59 | // ============================================================================ | |
60 | // wxListbook implementation | |
61 | // ============================================================================ | |
62 | ||
63 | // ---------------------------------------------------------------------------- | |
64 | // wxListbook creation | |
65 | // ---------------------------------------------------------------------------- | |
66 | ||
e9c0df38 VZ |
67 | bool |
68 | wxListbook::Create(wxWindow *parent, | |
69 | wxWindowID id, | |
70 | const wxPoint& pos, | |
71 | const wxSize& size, | |
72 | long style, | |
73 | const wxString& name) | |
74 | { | |
2ddb4d13 | 75 | if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT ) |
e9c0df38 VZ |
76 | { |
77 | #ifdef __WXMAC__ | |
2ddb4d13 | 78 | style |= wxBK_TOP; |
e9c0df38 | 79 | #else // !__WXMAC__ |
2ddb4d13 | 80 | style |= wxBK_LEFT; |
e9c0df38 VZ |
81 | #endif // __WXMAC__/!__WXMAC__ |
82 | } | |
83 | ||
ef0120c1 VZ |
84 | // no border for this control, it doesn't look nice together with |
85 | // wxListCtrl border | |
86 | style &= ~wxBORDER_MASK; | |
87 | style |= wxBORDER_NONE; | |
88 | ||
e9c0df38 VZ |
89 | if ( !wxControl::Create(parent, id, pos, size, style, |
90 | wxDefaultValidator, name) ) | |
91 | return false; | |
92 | ||
2ddb4d13 | 93 | m_bookctrl = new wxListView |
e9c0df38 VZ |
94 | ( |
95 | this, | |
447325a4 | 96 | wxID_ANY, |
e9c0df38 VZ |
97 | wxDefaultPosition, |
98 | wxDefaultSize, | |
947873e2 | 99 | GetListCtrlFlags() |
e9c0df38 VZ |
100 | ); |
101 | ||
947873e2 VZ |
102 | if ( GetListView()->InReportView() ) |
103 | GetListView()->InsertColumn(0, wxS("Pages")); | |
104 | ||
a9092ede | 105 | #ifdef __WXMSW__ |
2ddb4d13 | 106 | // On XP with themes enabled the GetViewRect used in GetControllerSize() to |
a9092ede | 107 | // determine the space needed for the list view will incorrectly return |
7b45094a | 108 | // (0,0,0,0) the first time. So send a pending event so OnSize will be |
a9092ede RD |
109 | // called again after the window is ready to go. Technically we don't |
110 | // need to do this on non-XP windows, but if things are already sized | |
111 | // correctly then nothing changes and so there is no harm. | |
112 | wxSizeEvent evt; | |
113 | GetEventHandler()->AddPendingEvent(evt); | |
114 | #endif | |
e9c0df38 VZ |
115 | return true; |
116 | } | |
117 | ||
947873e2 VZ |
118 | // ---------------------------------------------------------------------------- |
119 | // wxListCtrl flags | |
120 | // ---------------------------------------------------------------------------- | |
121 | ||
122 | long wxListbook::GetListCtrlFlags() const | |
123 | { | |
124 | // We'd like to always use wxLC_ICON mode but it doesn't work with the | |
125 | // native wxListCtrl under MSW unless we do have icons for all the items, | |
126 | // so we can't use it if we have no image list. In this case we'd like to | |
127 | // use wxLC_LIST mode because it works correctly for both horizontally and | |
128 | // vertically laid out controls, but MSW native wxListCtrl insists on | |
129 | // creating multiple columns if there are too many items and there doesn't | |
130 | // seem anything to do about it, so we have to use wxLC_REPORT mode in this | |
131 | // case there. | |
132 | ||
133 | long flags = IsVertical() ? wxLC_ALIGN_LEFT : wxLC_ALIGN_TOP; | |
134 | if ( GetImageList() ) | |
135 | { | |
136 | flags |= wxLC_ICON; | |
137 | } | |
138 | else // No images. | |
139 | { | |
140 | #ifdef __WXMSW__ | |
141 | if ( !IsVertical() ) | |
142 | { | |
143 | // Notice that we intentionally overwrite the alignment flags here | |
144 | // by not using "|=", alignment isn't used for report view. | |
145 | flags = wxLC_REPORT | wxLC_NO_HEADER; | |
146 | } | |
147 | else | |
148 | #endif // __WXMSW__ | |
149 | { | |
150 | flags |= wxLC_LIST; | |
151 | } | |
152 | } | |
153 | ||
154 | // Use single selection in any case. | |
155 | return flags | wxLC_SINGLE_SEL; | |
156 | } | |
157 | ||
e9c0df38 VZ |
158 | // ---------------------------------------------------------------------------- |
159 | // wxListbook geometry management | |
160 | // ---------------------------------------------------------------------------- | |
161 | ||
e9c0df38 VZ |
162 | void wxListbook::OnSize(wxSizeEvent& event) |
163 | { | |
9f29226d WS |
164 | // arrange the icons before calling SetClientSize(), otherwise it wouldn't |
165 | // account for the scrollbars the list control might need and, at least | |
166 | // under MSW, we'd finish with an ugly looking list control with both | |
167 | // vertical and horizontal scrollbar (with one of them being added because | |
168 | // the other one is not accounted for in client size computations) | |
905069b1 VZ |
169 | wxListView * const list = GetListView(); |
170 | if ( list ) | |
171 | list->Arrange(); | |
172 | ||
173 | event.Skip(); | |
e9c0df38 VZ |
174 | } |
175 | ||
d0a84b63 | 176 | int wxListbook::HitTest(const wxPoint& pt, long *flags) const |
851b88c3 VZ |
177 | { |
178 | int pagePos = wxNOT_FOUND; | |
179 | ||
d0a84b63 | 180 | if ( flags ) |
9804d540 | 181 | *flags = wxBK_HITTEST_NOWHERE; |
851b88c3 | 182 | |
d0a84b63 VZ |
183 | // convert from listbook control coordinates to list control coordinates |
184 | const wxListView * const list = GetListView(); | |
185 | const wxPoint listPt = list->ScreenToClient(ClientToScreen(pt)); | |
186 | ||
187 | // is the point inside list control? | |
22a35096 | 188 | if ( wxRect(list->GetSize()).Contains(listPt) ) |
851b88c3 VZ |
189 | { |
190 | int flagsList; | |
d0a84b63 | 191 | pagePos = list->HitTest(listPt, flagsList); |
851b88c3 | 192 | |
d0a84b63 | 193 | if ( flags ) |
851b88c3 | 194 | { |
d0a84b63 VZ |
195 | if ( pagePos != wxNOT_FOUND ) |
196 | *flags = 0; | |
197 | ||
198 | if ( flagsList & (wxLIST_HITTEST_ONITEMICON | | |
199 | wxLIST_HITTEST_ONITEMSTATEICON ) ) | |
9804d540 | 200 | *flags |= wxBK_HITTEST_ONICON; |
d0a84b63 VZ |
201 | |
202 | if ( flagsList & wxLIST_HITTEST_ONITEMLABEL ) | |
9804d540 | 203 | *flags |= wxBK_HITTEST_ONLABEL; |
851b88c3 VZ |
204 | } |
205 | } | |
d0a84b63 VZ |
206 | else // not over list control at all |
207 | { | |
22a35096 | 208 | if ( flags && GetPageRect().Contains(pt) ) |
9804d540 | 209 | *flags |= wxBK_HITTEST_ONPAGE; |
d0a84b63 | 210 | } |
851b88c3 VZ |
211 | |
212 | return pagePos; | |
213 | } | |
214 | ||
905069b1 VZ |
215 | void wxListbook::UpdateSize() |
216 | { | |
217 | // we should find a more elegant way to force a layout than generating this | |
218 | // dummy event | |
219 | wxSizeEvent sz(GetSize(), GetId()); | |
220 | GetEventHandler()->ProcessEvent(sz); | |
221 | } | |
e9c0df38 VZ |
222 | |
223 | // ---------------------------------------------------------------------------- | |
224 | // accessing the pages | |
225 | // ---------------------------------------------------------------------------- | |
226 | ||
227 | bool wxListbook::SetPageText(size_t n, const wxString& strText) | |
228 | { | |
2ddb4d13 | 229 | GetListView()->SetItemText(n, strText); |
e9c0df38 VZ |
230 | |
231 | return true; | |
232 | } | |
233 | ||
234 | wxString wxListbook::GetPageText(size_t n) const | |
235 | { | |
2ddb4d13 | 236 | return GetListView()->GetItemText(n); |
e9c0df38 VZ |
237 | } |
238 | ||
c1718122 | 239 | int wxListbook::GetPageImage(size_t n) const |
e9c0df38 | 240 | { |
c1718122 VZ |
241 | wxListItem item; |
242 | item.SetId(n); | |
e9c0df38 | 243 | |
c1718122 VZ |
244 | if (GetListView()->GetItem(item)) |
245 | { | |
246 | return item.GetImage(); | |
247 | } | |
248 | else | |
249 | { | |
250 | return wxNOT_FOUND; | |
251 | } | |
e9c0df38 VZ |
252 | } |
253 | ||
254 | bool wxListbook::SetPageImage(size_t n, int imageId) | |
255 | { | |
2ddb4d13 | 256 | return GetListView()->SetItemImage(n, imageId); |
e9c0df38 VZ |
257 | } |
258 | ||
259 | // ---------------------------------------------------------------------------- | |
260 | // image list stuff | |
261 | // ---------------------------------------------------------------------------- | |
262 | ||
263 | void wxListbook::SetImageList(wxImageList *imageList) | |
264 | { | |
947873e2 VZ |
265 | const long flagsOld = GetListCtrlFlags(); |
266 | ||
267 | wxBookCtrlBase::SetImageList(imageList); | |
268 | ||
269 | const long flagsNew = GetListCtrlFlags(); | |
270 | ||
0f79c83f VZ |
271 | wxListView * const list = GetListView(); |
272 | ||
947873e2 VZ |
273 | // We may need to change the list control mode if the image list presence |
274 | // has changed. | |
275 | if ( flagsNew != flagsOld ) | |
c1718122 | 276 | { |
0f79c83f VZ |
277 | // Preserve the selection which is lost when changing the mode |
278 | const int oldSel = GetSelection(); | |
c1718122 | 279 | |
947873e2 VZ |
280 | list->SetWindowStyleFlag(flagsNew); |
281 | if ( list->InReportView() ) | |
282 | list->InsertColumn(0, wxS("Pages")); | |
0f79c83f VZ |
283 | |
284 | // Restore selection | |
285 | if ( oldSel != wxNOT_FOUND ) | |
286 | SetSelection(oldSel); | |
c1718122 VZ |
287 | } |
288 | ||
0f79c83f | 289 | list->SetImageList(imageList, wxIMAGE_LIST_NORMAL); |
e9c0df38 VZ |
290 | } |
291 | ||
292 | // ---------------------------------------------------------------------------- | |
293 | // selection | |
294 | // ---------------------------------------------------------------------------- | |
295 | ||
1d6fcbcc VZ |
296 | void wxListbook::UpdateSelectedPage(size_t newsel) |
297 | { | |
298 | m_selection = newsel; | |
299 | GetListView()->Select(newsel); | |
300 | GetListView()->Focus(newsel); | |
301 | } | |
302 | ||
3e97a905 | 303 | wxBookCtrlEvent* wxListbook::CreatePageChangingEvent() const |
e9c0df38 | 304 | { |
ce7fe42e | 305 | return new wxBookCtrlEvent(wxEVT_LISTBOOK_PAGE_CHANGING, m_windowId); |
e9c0df38 VZ |
306 | } |
307 | ||
3e97a905 | 308 | void wxListbook::MakeChangedEvent(wxBookCtrlEvent &event) |
deb325e3 | 309 | { |
ce7fe42e | 310 | event.SetEventType(wxEVT_LISTBOOK_PAGE_CHANGED); |
deb325e3 VZ |
311 | } |
312 | ||
313 | ||
e9c0df38 VZ |
314 | // ---------------------------------------------------------------------------- |
315 | // adding/removing the pages | |
316 | // ---------------------------------------------------------------------------- | |
317 | ||
318 | bool | |
319 | wxListbook::InsertPage(size_t n, | |
320 | wxWindow *page, | |
321 | const wxString& text, | |
322 | bool bSelect, | |
323 | int imageId) | |
324 | { | |
61c083e7 | 325 | if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) ) |
e9c0df38 VZ |
326 | return false; |
327 | ||
2ddb4d13 | 328 | GetListView()->InsertItem(n, text, imageId); |
e9c0df38 | 329 | |
716dc245 WS |
330 | // if the inserted page is before the selected one, we must update the |
331 | // index of the selected page | |
332 | if ( int(n) <= m_selection ) | |
e9c0df38 | 333 | { |
42841dfc | 334 | // one extra page added |
716dc245 | 335 | m_selection++; |
2ddb4d13 WS |
336 | GetListView()->Select(m_selection); |
337 | GetListView()->Focus(m_selection); | |
e9c0df38 | 338 | } |
716dc245 | 339 | |
60d5c563 | 340 | if ( !DoSetSelectionAfterInsertion(n, bSelect) ) |
e9c0df38 | 341 | page->Hide(); |
716dc245 | 342 | |
905069b1 VZ |
343 | UpdateSize(); |
344 | ||
e9c0df38 VZ |
345 | return true; |
346 | } | |
347 | ||
348 | wxWindow *wxListbook::DoRemovePage(size_t page) | |
349 | { | |
61c083e7 | 350 | wxWindow *win = wxBookCtrlBase::DoRemovePage(page); |
bb08a4a1 | 351 | |
e9c0df38 VZ |
352 | if ( win ) |
353 | { | |
2ddb4d13 | 354 | GetListView()->DeleteItem(page); |
33ebfc3b | 355 | |
4f9ccec5 | 356 | DoSetSelectionAfterRemoval(page); |
9f29226d | 357 | |
2ddb4d13 | 358 | GetListView()->Arrange(); |
905069b1 | 359 | UpdateSize(); |
e9c0df38 VZ |
360 | } |
361 | ||
362 | return win; | |
363 | } | |
364 | ||
fbd11d30 RD |
365 | |
366 | bool wxListbook::DeleteAllPages() | |
367 | { | |
2ddb4d13 | 368 | GetListView()->DeleteAllItems(); |
6a82a0d0 JS |
369 | if (!wxBookCtrlBase::DeleteAllPages()) |
370 | return false; | |
9eddec69 | 371 | |
905069b1 | 372 | UpdateSize(); |
9eddec69 | 373 | |
6a82a0d0 | 374 | return true; |
fbd11d30 RD |
375 | } |
376 | ||
e9c0df38 VZ |
377 | // ---------------------------------------------------------------------------- |
378 | // wxListbook events | |
379 | // ---------------------------------------------------------------------------- | |
380 | ||
381 | void wxListbook::OnListSelected(wxListEvent& eventList) | |
382 | { | |
447325a4 VZ |
383 | if ( eventList.GetEventObject() != m_bookctrl ) |
384 | { | |
385 | eventList.Skip(); | |
386 | return; | |
387 | } | |
388 | ||
e9c0df38 VZ |
389 | const int selNew = eventList.GetIndex(); |
390 | ||
391 | if ( selNew == m_selection ) | |
392 | { | |
393 | // this event can only come from our own Select(m_selection) below | |
394 | // which we call when the page change is vetoed, so we should simply | |
395 | // ignore it | |
396 | return; | |
397 | } | |
398 | ||
716dc245 | 399 | SetSelection(selNew); |
e9c0df38 | 400 | |
716dc245 WS |
401 | // change wasn't allowed, return to previous state |
402 | if (m_selection != selNew) | |
e9c0df38 | 403 | { |
2ddb4d13 WS |
404 | GetListView()->Select(m_selection); |
405 | GetListView()->Focus(m_selection); | |
e9c0df38 | 406 | } |
e9c0df38 VZ |
407 | } |
408 | ||
409 | #endif // wxUSE_LISTBOOK |