]>
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 VZ |
51 | IMPLEMENT_DYNAMIC_CLASS(wxListbookEvent, wxNotifyEvent) |
52 | ||
9cf0f6ae | 53 | #if !WXWIN_COMPATIBILITY_EVENT_TYPES |
e9c0df38 VZ |
54 | const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING = wxNewEventType(); |
55 | const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED = wxNewEventType(); | |
9cf0f6ae | 56 | #endif |
f29395d0 | 57 | const int wxID_LISTBOOKLISTVIEW = wxNewId(); |
e9c0df38 | 58 | |
61c083e7 | 59 | BEGIN_EVENT_TABLE(wxListbook, wxBookCtrlBase) |
e9c0df38 | 60 | EVT_SIZE(wxListbook::OnSize) |
f29395d0 | 61 | EVT_LIST_ITEM_SELECTED(wxID_LISTBOOKLISTVIEW, wxListbook::OnListSelected) |
e9c0df38 VZ |
62 | END_EVENT_TABLE() |
63 | ||
64 | // ============================================================================ | |
65 | // wxListbook implementation | |
66 | // ============================================================================ | |
67 | ||
68 | // ---------------------------------------------------------------------------- | |
69 | // wxListbook creation | |
70 | // ---------------------------------------------------------------------------- | |
71 | ||
72 | void wxListbook::Init() | |
73 | { | |
e9c0df38 VZ |
74 | m_selection = wxNOT_FOUND; |
75 | } | |
76 | ||
77 | bool | |
78 | wxListbook::Create(wxWindow *parent, | |
79 | wxWindowID id, | |
80 | const wxPoint& pos, | |
81 | const wxSize& size, | |
82 | long style, | |
83 | const wxString& name) | |
84 | { | |
2ddb4d13 | 85 | if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT ) |
e9c0df38 VZ |
86 | { |
87 | #ifdef __WXMAC__ | |
2ddb4d13 | 88 | style |= wxBK_TOP; |
e9c0df38 | 89 | #else // !__WXMAC__ |
2ddb4d13 | 90 | style |= wxBK_LEFT; |
e9c0df38 VZ |
91 | #endif // __WXMAC__/!__WXMAC__ |
92 | } | |
93 | ||
ef0120c1 VZ |
94 | // no border for this control, it doesn't look nice together with |
95 | // wxListCtrl border | |
96 | style &= ~wxBORDER_MASK; | |
97 | style |= wxBORDER_NONE; | |
98 | ||
e9c0df38 VZ |
99 | if ( !wxControl::Create(parent, id, pos, size, style, |
100 | wxDefaultValidator, name) ) | |
101 | return false; | |
102 | ||
2ddb4d13 | 103 | m_bookctrl = new wxListView |
e9c0df38 VZ |
104 | ( |
105 | this, | |
f29395d0 | 106 | wxID_LISTBOOKLISTVIEW, |
e9c0df38 VZ |
107 | wxDefaultPosition, |
108 | wxDefaultSize, | |
ef0120c1 | 109 | wxLC_ICON | wxLC_SINGLE_SEL | |
4a06b348 | 110 | (IsVertical() ? wxLC_ALIGN_LEFT : wxLC_ALIGN_TOP) |
e9c0df38 VZ |
111 | ); |
112 | ||
a9092ede | 113 | #ifdef __WXMSW__ |
2ddb4d13 | 114 | // On XP with themes enabled the GetViewRect used in GetControllerSize() to |
a9092ede | 115 | // determine the space needed for the list view will incorrectly return |
7b45094a | 116 | // (0,0,0,0) the first time. So send a pending event so OnSize will be |
a9092ede RD |
117 | // called again after the window is ready to go. Technically we don't |
118 | // need to do this on non-XP windows, but if things are already sized | |
119 | // correctly then nothing changes and so there is no harm. | |
120 | wxSizeEvent evt; | |
121 | GetEventHandler()->AddPendingEvent(evt); | |
122 | #endif | |
e9c0df38 VZ |
123 | return true; |
124 | } | |
125 | ||
126 | // ---------------------------------------------------------------------------- | |
127 | // wxListbook geometry management | |
128 | // ---------------------------------------------------------------------------- | |
129 | ||
2ddb4d13 | 130 | wxSize wxListbook::GetControllerSize() const |
e9c0df38 | 131 | { |
4a06b348 | 132 | const wxSize sizeClient = GetClientSize(), |
2ddb4d13 WS |
133 | sizeBorder = m_bookctrl->GetSize() - m_bookctrl->GetClientSize(), |
134 | sizeList = GetListView()->GetViewRect().GetSize() + sizeBorder; | |
e9c0df38 VZ |
135 | |
136 | wxSize size; | |
2ddb4d13 | 137 | |
e9c0df38 VZ |
138 | if ( IsVertical() ) |
139 | { | |
140 | size.x = sizeClient.x; | |
4a06b348 | 141 | size.y = sizeList.y; |
e9c0df38 VZ |
142 | } |
143 | else // left/right aligned | |
144 | { | |
4a06b348 | 145 | size.x = sizeList.x; |
e9c0df38 VZ |
146 | size.y = sizeClient.y; |
147 | } | |
148 | ||
149 | return size; | |
150 | } | |
151 | ||
e9c0df38 VZ |
152 | void wxListbook::OnSize(wxSizeEvent& event) |
153 | { | |
9f29226d WS |
154 | // arrange the icons before calling SetClientSize(), otherwise it wouldn't |
155 | // account for the scrollbars the list control might need and, at least | |
156 | // under MSW, we'd finish with an ugly looking list control with both | |
157 | // vertical and horizontal scrollbar (with one of them being added because | |
158 | // the other one is not accounted for in client size computations) | |
2ddb4d13 WS |
159 | wxListView *list = GetListView(); |
160 | if (list) list->Arrange(); | |
161 | wxBookCtrlBase::OnSize(event); | |
e9c0df38 VZ |
162 | } |
163 | ||
851b88c3 VZ |
164 | int wxListbook::HitTest(const wxPoint& pt, long * WXUNUSED(flags)) const |
165 | { | |
166 | int pagePos = wxNOT_FOUND; | |
167 | ||
168 | const wxPoint clientPt = ClientToScreen(GetListView()->ScreenToClient(pt)); | |
169 | ||
170 | if ( wxRect(GetListView()->GetSize()).Inside(clientPt) ) | |
171 | { | |
172 | int flagsList; | |
173 | pagePos = GetListView()->HitTest(clientPt, flagsList); | |
174 | ||
175 | if ( !(flagsList & wxLIST_HITTEST_ONITEM) ) | |
176 | { | |
177 | pagePos = wxNOT_FOUND; | |
178 | } | |
179 | } | |
180 | ||
181 | return pagePos; | |
182 | } | |
183 | ||
e9c0df38 VZ |
184 | wxSize wxListbook::CalcSizeFromPage(const wxSize& sizePage) const |
185 | { | |
159e6235 | 186 | // we need to add the size of the list control and the border between |
2ddb4d13 | 187 | const wxSize sizeList = GetControllerSize(); |
e9c0df38 VZ |
188 | |
189 | wxSize size = sizePage; | |
190 | if ( IsVertical() ) | |
191 | { | |
159e6235 | 192 | size.y += sizeList.y + GetInternalBorder(); |
e9c0df38 VZ |
193 | } |
194 | else // left/right aligned | |
195 | { | |
159e6235 | 196 | size.x += sizeList.x + GetInternalBorder(); |
e9c0df38 VZ |
197 | } |
198 | ||
199 | return size; | |
200 | } | |
201 | ||
202 | ||
203 | // ---------------------------------------------------------------------------- | |
204 | // accessing the pages | |
205 | // ---------------------------------------------------------------------------- | |
206 | ||
207 | bool wxListbook::SetPageText(size_t n, const wxString& strText) | |
208 | { | |
2ddb4d13 | 209 | GetListView()->SetItemText(n, strText); |
e9c0df38 VZ |
210 | |
211 | return true; | |
212 | } | |
213 | ||
214 | wxString wxListbook::GetPageText(size_t n) const | |
215 | { | |
2ddb4d13 | 216 | return GetListView()->GetItemText(n); |
e9c0df38 VZ |
217 | } |
218 | ||
219 | int wxListbook::GetPageImage(size_t WXUNUSED(n)) const | |
220 | { | |
221 | wxFAIL_MSG( _T("wxListbook::GetPageImage() not implemented") ); | |
222 | ||
9f29226d | 223 | return wxNOT_FOUND; |
e9c0df38 VZ |
224 | } |
225 | ||
226 | bool wxListbook::SetPageImage(size_t n, int imageId) | |
227 | { | |
2ddb4d13 | 228 | return GetListView()->SetItemImage(n, imageId); |
e9c0df38 VZ |
229 | } |
230 | ||
231 | // ---------------------------------------------------------------------------- | |
232 | // image list stuff | |
233 | // ---------------------------------------------------------------------------- | |
234 | ||
235 | void wxListbook::SetImageList(wxImageList *imageList) | |
236 | { | |
2ddb4d13 | 237 | GetListView()->SetImageList(imageList, wxIMAGE_LIST_NORMAL); |
e9c0df38 | 238 | |
61c083e7 | 239 | wxBookCtrlBase::SetImageList(imageList); |
e9c0df38 VZ |
240 | } |
241 | ||
242 | // ---------------------------------------------------------------------------- | |
243 | // selection | |
244 | // ---------------------------------------------------------------------------- | |
245 | ||
246 | int wxListbook::GetSelection() const | |
247 | { | |
248 | return m_selection; | |
249 | } | |
250 | ||
251 | int wxListbook::SetSelection(size_t n) | |
252 | { | |
1f30c176 WS |
253 | wxCHECK_MSG( IS_VALID_PAGE(n), wxNOT_FOUND, |
254 | wxT("invalid page index in wxListbook::SetSelection()") ); | |
e9c0df38 | 255 | |
1f30c176 | 256 | const int oldSel = m_selection; |
e9c0df38 | 257 | |
1f30c176 | 258 | if ( int(n) != m_selection ) |
e9c0df38 | 259 | { |
1f30c176 WS |
260 | wxListbookEvent event(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING, m_windowId); |
261 | event.SetSelection(n); | |
262 | event.SetOldSelection(m_selection); | |
263 | event.SetEventObject(this); | |
264 | if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() ) | |
265 | { | |
266 | if ( m_selection != wxNOT_FOUND ) | |
267 | m_pages[m_selection]->Hide(); | |
e9c0df38 | 268 | |
1f30c176 WS |
269 | wxWindow *page = m_pages[n]; |
270 | page->SetSize(GetPageRect()); | |
271 | page->Show(); | |
bb08a4a1 | 272 | |
716dc245 | 273 | // change m_selection now to ignore the selection change event |
1f30c176 | 274 | m_selection = n; |
2ddb4d13 WS |
275 | GetListView()->Select(n); |
276 | GetListView()->Focus(n); | |
1f30c176 WS |
277 | |
278 | // program allows the page change | |
279 | event.SetEventType(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED); | |
280 | (void)GetEventHandler()->ProcessEvent(event); | |
281 | } | |
e9c0df38 VZ |
282 | } |
283 | ||
1f30c176 | 284 | return oldSel; |
e9c0df38 VZ |
285 | } |
286 | ||
e9c0df38 VZ |
287 | // ---------------------------------------------------------------------------- |
288 | // adding/removing the pages | |
289 | // ---------------------------------------------------------------------------- | |
290 | ||
291 | bool | |
292 | wxListbook::InsertPage(size_t n, | |
293 | wxWindow *page, | |
294 | const wxString& text, | |
295 | bool bSelect, | |
296 | int imageId) | |
297 | { | |
61c083e7 | 298 | if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) ) |
e9c0df38 VZ |
299 | return false; |
300 | ||
2ddb4d13 | 301 | GetListView()->InsertItem(n, text, imageId); |
e9c0df38 | 302 | |
716dc245 WS |
303 | // if the inserted page is before the selected one, we must update the |
304 | // index of the selected page | |
305 | if ( int(n) <= m_selection ) | |
e9c0df38 | 306 | { |
42841dfc | 307 | // one extra page added |
716dc245 | 308 | m_selection++; |
2ddb4d13 WS |
309 | GetListView()->Select(m_selection); |
310 | GetListView()->Focus(m_selection); | |
e9c0df38 | 311 | } |
716dc245 WS |
312 | |
313 | // some page should be selected: either this one or the first one if there | |
314 | // is still no selection | |
315 | int selNew = -1; | |
316 | if ( bSelect ) | |
317 | selNew = n; | |
318 | else if ( m_selection == -1 ) | |
319 | selNew = 0; | |
320 | ||
321 | if ( selNew != m_selection ) | |
e9c0df38 | 322 | page->Hide(); |
716dc245 WS |
323 | |
324 | if ( selNew != -1 ) | |
325 | SetSelection(selNew); | |
e9c0df38 | 326 | |
37144cf0 | 327 | InvalidateBestSize(); |
6a82a0d0 | 328 | // GetListView()->InvalidateBestSize(); |
2ddb4d13 | 329 | GetListView()->Arrange(); |
9eddec69 | 330 | |
6a82a0d0 JS |
331 | if (GetPageCount() == 1) |
332 | { | |
333 | wxSizeEvent sz(GetSize(), GetId()); | |
334 | ProcessEvent(sz); | |
335 | } | |
e9c0df38 VZ |
336 | return true; |
337 | } | |
338 | ||
339 | wxWindow *wxListbook::DoRemovePage(size_t page) | |
340 | { | |
4a10ea8b | 341 | const size_t page_count = GetPageCount(); |
61c083e7 | 342 | wxWindow *win = wxBookCtrlBase::DoRemovePage(page); |
bb08a4a1 | 343 | |
e9c0df38 VZ |
344 | if ( win ) |
345 | { | |
2ddb4d13 | 346 | GetListView()->DeleteItem(page); |
33ebfc3b VZ |
347 | |
348 | if (m_selection >= (int)page) | |
349 | { | |
350 | // force new sel valid if possible | |
351 | int sel = m_selection - 1; | |
352 | if (page_count == 1) | |
bb08a4a1 | 353 | sel = wxNOT_FOUND; |
33ebfc3b VZ |
354 | else if ((page_count == 2) || (sel == -1)) |
355 | sel = 0; | |
bb08a4a1 | 356 | |
33ebfc3b | 357 | // force sel invalid if deleting current page - don't try to hide it |
bb08a4a1 WS |
358 | m_selection = (m_selection == (int)page) ? wxNOT_FOUND : m_selection - 1; |
359 | ||
360 | if ((sel != wxNOT_FOUND) && (sel != m_selection)) | |
33ebfc3b VZ |
361 | SetSelection(sel); |
362 | } | |
9f29226d | 363 | |
2ddb4d13 | 364 | GetListView()->Arrange(); |
6a82a0d0 JS |
365 | if (GetPageCount() == 0) |
366 | { | |
367 | wxSizeEvent sz(GetSize(), GetId()); | |
368 | ProcessEvent(sz); | |
369 | } | |
e9c0df38 VZ |
370 | } |
371 | ||
372 | return win; | |
373 | } | |
374 | ||
fbd11d30 RD |
375 | |
376 | bool wxListbook::DeleteAllPages() | |
377 | { | |
2ddb4d13 | 378 | GetListView()->DeleteAllItems(); |
6a82a0d0 JS |
379 | if (!wxBookCtrlBase::DeleteAllPages()) |
380 | return false; | |
9eddec69 | 381 | |
6a82a0d0 JS |
382 | m_selection = -1; |
383 | ||
384 | wxSizeEvent sz(GetSize(), GetId()); | |
385 | ProcessEvent(sz); | |
9eddec69 | 386 | |
6a82a0d0 | 387 | return true; |
fbd11d30 RD |
388 | } |
389 | ||
e9c0df38 VZ |
390 | // ---------------------------------------------------------------------------- |
391 | // wxListbook events | |
392 | // ---------------------------------------------------------------------------- | |
393 | ||
394 | void wxListbook::OnListSelected(wxListEvent& eventList) | |
395 | { | |
396 | const int selNew = eventList.GetIndex(); | |
397 | ||
398 | if ( selNew == m_selection ) | |
399 | { | |
400 | // this event can only come from our own Select(m_selection) below | |
401 | // which we call when the page change is vetoed, so we should simply | |
402 | // ignore it | |
403 | return; | |
404 | } | |
405 | ||
716dc245 | 406 | SetSelection(selNew); |
e9c0df38 | 407 | |
716dc245 WS |
408 | // change wasn't allowed, return to previous state |
409 | if (m_selection != selNew) | |
e9c0df38 | 410 | { |
2ddb4d13 WS |
411 | GetListView()->Select(m_selection); |
412 | GetListView()->Focus(m_selection); | |
e9c0df38 | 413 | } |
e9c0df38 VZ |
414 | } |
415 | ||
416 | #endif // wxUSE_LISTBOOK |