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