]>
Commit | Line | Data |
---|---|---|
e9c0df38 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: generic/listbkg.cpp | |
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 | ||
29 | #include "wx/listctrl.h" | |
30 | #include "wx/statline.h" | |
31 | #include "wx/listbook.h" | |
5b24f0d3 | 32 | #include "wx/imaglist.h" |
c59da60a | 33 | #include "wx/settings.h" |
e9c0df38 VZ |
34 | |
35 | // ---------------------------------------------------------------------------- | |
36 | // constants | |
37 | // ---------------------------------------------------------------------------- | |
38 | ||
39 | // margin between the list and the page, should be bigger than wxStaticLine | |
40 | // size | |
41 | const wxCoord MARGIN = 5; | |
42 | ||
43 | // ---------------------------------------------------------------------------- | |
77ffb593 | 44 | // various wxWidgets macros |
e9c0df38 VZ |
45 | // ---------------------------------------------------------------------------- |
46 | ||
1f30c176 WS |
47 | // check that the page index is valid |
48 | #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount()) | |
49 | ||
50 | // ---------------------------------------------------------------------------- | |
51 | // event table | |
52 | // ---------------------------------------------------------------------------- | |
53 | ||
e9c0df38 VZ |
54 | IMPLEMENT_DYNAMIC_CLASS(wxListbook, wxControl) |
55 | IMPLEMENT_DYNAMIC_CLASS(wxListbookEvent, wxNotifyEvent) | |
56 | ||
57 | const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING = wxNewEventType(); | |
58 | const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED = wxNewEventType(); | |
f29395d0 | 59 | const int wxID_LISTBOOKLISTVIEW = wxNewId(); |
e9c0df38 | 60 | |
61c083e7 | 61 | BEGIN_EVENT_TABLE(wxListbook, wxBookCtrlBase) |
e9c0df38 | 62 | EVT_SIZE(wxListbook::OnSize) |
f29395d0 | 63 | EVT_LIST_ITEM_SELECTED(wxID_LISTBOOKLISTVIEW, 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 | { | |
76 | m_list = NULL; | |
ef0120c1 | 77 | #if wxUSE_LINE_IN_LISTBOOK |
e9c0df38 | 78 | m_line = NULL; |
ef0120c1 | 79 | #endif // wxUSE_LINE_IN_LISTBOOK |
e9c0df38 VZ |
80 | m_selection = wxNOT_FOUND; |
81 | } | |
82 | ||
83 | bool | |
84 | wxListbook::Create(wxWindow *parent, | |
85 | wxWindowID id, | |
86 | const wxPoint& pos, | |
87 | const wxSize& size, | |
88 | long style, | |
89 | const wxString& name) | |
90 | { | |
91 | if ( (style & wxLB_ALIGN_MASK) == wxLB_DEFAULT ) | |
92 | { | |
93 | #ifdef __WXMAC__ | |
94 | style |= wxLB_TOP; | |
95 | #else // !__WXMAC__ | |
96 | style |= wxLB_LEFT; | |
97 | #endif // __WXMAC__/!__WXMAC__ | |
98 | } | |
99 | ||
ef0120c1 VZ |
100 | // no border for this control, it doesn't look nice together with |
101 | // wxListCtrl border | |
102 | style &= ~wxBORDER_MASK; | |
103 | style |= wxBORDER_NONE; | |
104 | ||
e9c0df38 VZ |
105 | if ( !wxControl::Create(parent, id, pos, size, style, |
106 | wxDefaultValidator, name) ) | |
107 | return false; | |
108 | ||
109 | m_list = new wxListView | |
110 | ( | |
111 | this, | |
f29395d0 | 112 | wxID_LISTBOOKLISTVIEW, |
e9c0df38 VZ |
113 | wxDefaultPosition, |
114 | wxDefaultSize, | |
ef0120c1 | 115 | wxLC_ICON | wxLC_SINGLE_SEL | |
4a06b348 | 116 | (IsVertical() ? wxLC_ALIGN_LEFT : wxLC_ALIGN_TOP) |
e9c0df38 VZ |
117 | ); |
118 | ||
ef0120c1 | 119 | #if wxUSE_LINE_IN_LISTBOOK |
e9c0df38 VZ |
120 | m_line = new wxStaticLine |
121 | ( | |
122 | this, | |
ca65c044 | 123 | wxID_ANY, |
e9c0df38 VZ |
124 | wxDefaultPosition, |
125 | wxDefaultSize, | |
126 | IsVertical() ? wxLI_HORIZONTAL : wxLI_VERTICAL | |
127 | ); | |
ef0120c1 | 128 | #endif // wxUSE_LINE_IN_LISTBOOK |
e9c0df38 | 129 | |
a9092ede RD |
130 | #ifdef __WXMSW__ |
131 | // On XP with themes enabled the GetViewRect used in GetListSize to | |
132 | // determine the space needed for the list view will incorrectly return | |
7b45094a | 133 | // (0,0,0,0) the first time. So send a pending event so OnSize will be |
a9092ede RD |
134 | // called again after the window is ready to go. Technically we don't |
135 | // need to do this on non-XP windows, but if things are already sized | |
136 | // correctly then nothing changes and so there is no harm. | |
137 | wxSizeEvent evt; | |
138 | GetEventHandler()->AddPendingEvent(evt); | |
139 | #endif | |
e9c0df38 VZ |
140 | return true; |
141 | } | |
142 | ||
143 | // ---------------------------------------------------------------------------- | |
144 | // wxListbook geometry management | |
145 | // ---------------------------------------------------------------------------- | |
146 | ||
147 | wxSize wxListbook::GetListSize() const | |
148 | { | |
4a06b348 | 149 | const wxSize sizeClient = GetClientSize(), |
9f29226d WS |
150 | sizeBorder = m_list->GetSize() - m_list->GetClientSize(), |
151 | sizeList = m_list->GetViewRect().GetSize() + sizeBorder; | |
e9c0df38 VZ |
152 | |
153 | wxSize size; | |
154 | if ( IsVertical() ) | |
155 | { | |
156 | size.x = sizeClient.x; | |
4a06b348 | 157 | size.y = sizeList.y; |
e9c0df38 VZ |
158 | } |
159 | else // left/right aligned | |
160 | { | |
4a06b348 | 161 | size.x = sizeList.x; |
e9c0df38 VZ |
162 | size.y = sizeClient.y; |
163 | } | |
164 | ||
165 | return size; | |
166 | } | |
167 | ||
168 | wxRect wxListbook::GetPageRect() const | |
169 | { | |
ae03fa2c | 170 | const wxSize sizeList = m_list->GetSize(); |
e9c0df38 | 171 | |
2997ca30 | 172 | wxPoint pt; |
42841dfc | 173 | wxRect rectPage(pt, GetClientSize()); |
e9c0df38 VZ |
174 | switch ( GetWindowStyle() & wxLB_ALIGN_MASK ) |
175 | { | |
176 | default: | |
177 | wxFAIL_MSG( _T("unexpected wxListbook alignment") ); | |
178 | // fall through | |
179 | ||
180 | case wxLB_TOP: | |
181 | rectPage.y = sizeList.y + MARGIN; | |
182 | // fall through | |
183 | ||
184 | case wxLB_BOTTOM: | |
185 | rectPage.height -= sizeList.y + MARGIN; | |
186 | break; | |
187 | ||
188 | case wxLB_LEFT: | |
189 | rectPage.x = sizeList.x + MARGIN; | |
190 | // fall through | |
191 | ||
192 | case wxLB_RIGHT: | |
193 | rectPage.width -= sizeList.x + MARGIN; | |
194 | break; | |
195 | } | |
196 | ||
197 | return rectPage; | |
198 | } | |
199 | ||
200 | void wxListbook::OnSize(wxSizeEvent& event) | |
201 | { | |
f1160963 VZ |
202 | event.Skip(); |
203 | ||
204 | if ( !m_list ) | |
205 | { | |
206 | // we're not fully created yet | |
207 | return; | |
208 | } | |
209 | ||
9f29226d WS |
210 | // arrange the icons before calling SetClientSize(), otherwise it wouldn't |
211 | // account for the scrollbars the list control might need and, at least | |
212 | // under MSW, we'd finish with an ugly looking list control with both | |
213 | // vertical and horizontal scrollbar (with one of them being added because | |
214 | // the other one is not accounted for in client size computations) | |
215 | m_list->Arrange(); | |
216 | ||
e9c0df38 VZ |
217 | // resize the list control and the page area to fit inside our new size |
218 | const wxSize sizeClient = GetClientSize(), | |
9f29226d | 219 | sizeBorder = m_list->GetSize() - m_list->GetClientSize(), |
e9c0df38 VZ |
220 | sizeList = GetListSize(); |
221 | ||
9f29226d WS |
222 | m_list->SetClientSize( sizeList.x - sizeBorder.x, sizeList.y - sizeBorder.y ); |
223 | ||
224 | const wxSize sizeNew = m_list->GetSize(); | |
e9c0df38 VZ |
225 | wxPoint posList; |
226 | switch ( GetWindowStyle() & wxLB_ALIGN_MASK ) | |
227 | { | |
228 | default: | |
229 | wxFAIL_MSG( _T("unexpected wxListbook alignment") ); | |
230 | // fall through | |
231 | ||
232 | case wxLB_TOP: | |
233 | case wxLB_LEFT: | |
234 | // posList is already ok | |
235 | break; | |
236 | ||
237 | case wxLB_BOTTOM: | |
9f29226d | 238 | posList.y = sizeClient.y - sizeNew.y; |
e9c0df38 VZ |
239 | break; |
240 | ||
241 | case wxLB_RIGHT: | |
9f29226d | 242 | posList.x = sizeClient.x - sizeNew.x; |
e9c0df38 VZ |
243 | break; |
244 | } | |
245 | ||
a1f9a880 | 246 | if ( m_list->GetPosition() != posList ) |
9f29226d | 247 | m_list->Move(posList); |
e9c0df38 | 248 | |
ef0120c1 | 249 | #if wxUSE_LINE_IN_LISTBOOK |
e9c0df38 VZ |
250 | if ( m_line ) |
251 | { | |
3f0785de | 252 | wxRect rectLine(sizeClient); |
e9c0df38 VZ |
253 | |
254 | switch ( GetWindowStyle() & wxLB_ALIGN_MASK ) | |
255 | { | |
256 | case wxLB_TOP: | |
9f29226d | 257 | rectLine.y = sizeNew.y + 1; |
e9c0df38 VZ |
258 | rectLine.height = MARGIN - 2; |
259 | break; | |
260 | ||
261 | case wxLB_BOTTOM: | |
262 | rectLine.height = MARGIN - 2; | |
9f29226d | 263 | rectLine.y = sizeClient.y - sizeNew.y - rectLine.height; |
e9c0df38 VZ |
264 | break; |
265 | ||
266 | case wxLB_LEFT: | |
9f29226d | 267 | rectLine.x = sizeNew.x + 1; |
e9c0df38 VZ |
268 | rectLine.width = MARGIN - 2; |
269 | break; | |
270 | ||
271 | case wxLB_RIGHT: | |
272 | rectLine.width = MARGIN - 2; | |
9f29226d | 273 | rectLine.x = sizeClient.x - sizeNew.x - rectLine.width; |
e9c0df38 VZ |
274 | break; |
275 | } | |
276 | ||
277 | m_line->SetSize(rectLine); | |
278 | } | |
ef0120c1 | 279 | #endif // wxUSE_LINE_IN_LISTBOOK |
e9c0df38 | 280 | |
33ebfc3b VZ |
281 | // resize the currently shown page |
282 | if (m_selection != wxNOT_FOUND ) | |
e9c0df38 VZ |
283 | { |
284 | wxWindow *page = m_pages[m_selection]; | |
285 | wxCHECK_RET( page, _T("NULL page in wxListbook?") ); | |
e9c0df38 | 286 | page->SetSize(GetPageRect()); |
7a19fb6e | 287 | } |
e9c0df38 VZ |
288 | } |
289 | ||
290 | wxSize wxListbook::CalcSizeFromPage(const wxSize& sizePage) const | |
291 | { | |
292 | // we need to add the size of the list control and the margin | |
293 | const wxSize sizeList = GetListSize(); | |
294 | ||
295 | wxSize size = sizePage; | |
296 | if ( IsVertical() ) | |
297 | { | |
298 | size.y += sizeList.y + MARGIN; | |
299 | } | |
300 | else // left/right aligned | |
301 | { | |
302 | size.x += sizeList.x + MARGIN; | |
303 | } | |
304 | ||
305 | return size; | |
306 | } | |
307 | ||
308 | ||
309 | // ---------------------------------------------------------------------------- | |
310 | // accessing the pages | |
311 | // ---------------------------------------------------------------------------- | |
312 | ||
313 | bool wxListbook::SetPageText(size_t n, const wxString& strText) | |
314 | { | |
315 | m_list->SetItemText(n, strText); | |
316 | ||
317 | return true; | |
318 | } | |
319 | ||
320 | wxString wxListbook::GetPageText(size_t n) const | |
321 | { | |
322 | return m_list->GetItemText(n); | |
323 | } | |
324 | ||
325 | int wxListbook::GetPageImage(size_t WXUNUSED(n)) const | |
326 | { | |
327 | wxFAIL_MSG( _T("wxListbook::GetPageImage() not implemented") ); | |
328 | ||
9f29226d | 329 | return wxNOT_FOUND; |
e9c0df38 VZ |
330 | } |
331 | ||
332 | bool wxListbook::SetPageImage(size_t n, int imageId) | |
333 | { | |
c3627a00 | 334 | return m_list->SetItemImage(n, imageId); |
e9c0df38 VZ |
335 | } |
336 | ||
337 | // ---------------------------------------------------------------------------- | |
338 | // image list stuff | |
339 | // ---------------------------------------------------------------------------- | |
340 | ||
341 | void wxListbook::SetImageList(wxImageList *imageList) | |
342 | { | |
343 | m_list->SetImageList(imageList, wxIMAGE_LIST_NORMAL); | |
344 | ||
61c083e7 | 345 | wxBookCtrlBase::SetImageList(imageList); |
e9c0df38 VZ |
346 | } |
347 | ||
348 | // ---------------------------------------------------------------------------- | |
349 | // selection | |
350 | // ---------------------------------------------------------------------------- | |
351 | ||
352 | int wxListbook::GetSelection() const | |
353 | { | |
354 | return m_selection; | |
355 | } | |
356 | ||
357 | int wxListbook::SetSelection(size_t n) | |
358 | { | |
1f30c176 WS |
359 | wxCHECK_MSG( IS_VALID_PAGE(n), wxNOT_FOUND, |
360 | wxT("invalid page index in wxListbook::SetSelection()") ); | |
e9c0df38 | 361 | |
1f30c176 | 362 | const int oldSel = m_selection; |
e9c0df38 | 363 | |
1f30c176 | 364 | if ( int(n) != m_selection ) |
e9c0df38 | 365 | { |
1f30c176 WS |
366 | wxListbookEvent event(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING, m_windowId); |
367 | event.SetSelection(n); | |
368 | event.SetOldSelection(m_selection); | |
369 | event.SetEventObject(this); | |
370 | if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() ) | |
371 | { | |
372 | if ( m_selection != wxNOT_FOUND ) | |
373 | m_pages[m_selection]->Hide(); | |
e9c0df38 | 374 | |
1f30c176 WS |
375 | wxWindow *page = m_pages[n]; |
376 | page->SetSize(GetPageRect()); | |
377 | page->Show(); | |
bb08a4a1 | 378 | |
716dc245 | 379 | // change m_selection now to ignore the selection change event |
1f30c176 WS |
380 | m_selection = n; |
381 | m_list->Select(n); | |
382 | m_list->Focus(n); | |
383 | ||
384 | // program allows the page change | |
385 | event.SetEventType(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED); | |
386 | (void)GetEventHandler()->ProcessEvent(event); | |
387 | } | |
e9c0df38 VZ |
388 | } |
389 | ||
1f30c176 | 390 | return oldSel; |
e9c0df38 VZ |
391 | } |
392 | ||
e9c0df38 VZ |
393 | // ---------------------------------------------------------------------------- |
394 | // adding/removing the pages | |
395 | // ---------------------------------------------------------------------------- | |
396 | ||
397 | bool | |
398 | wxListbook::InsertPage(size_t n, | |
399 | wxWindow *page, | |
400 | const wxString& text, | |
401 | bool bSelect, | |
402 | int imageId) | |
403 | { | |
61c083e7 | 404 | if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) ) |
e9c0df38 VZ |
405 | return false; |
406 | ||
407 | m_list->InsertItem(n, text, imageId); | |
408 | ||
716dc245 WS |
409 | // if the inserted page is before the selected one, we must update the |
410 | // index of the selected page | |
411 | if ( int(n) <= m_selection ) | |
e9c0df38 | 412 | { |
42841dfc | 413 | // one extra page added |
716dc245 WS |
414 | m_selection++; |
415 | m_list->Select(m_selection); | |
416 | m_list->Focus(m_selection); | |
e9c0df38 | 417 | } |
716dc245 WS |
418 | |
419 | // some page should be selected: either this one or the first one if there | |
420 | // is still no selection | |
421 | int selNew = -1; | |
422 | if ( bSelect ) | |
423 | selNew = n; | |
424 | else if ( m_selection == -1 ) | |
425 | selNew = 0; | |
426 | ||
427 | if ( selNew != m_selection ) | |
e9c0df38 | 428 | page->Hide(); |
716dc245 WS |
429 | |
430 | if ( selNew != -1 ) | |
431 | SetSelection(selNew); | |
e9c0df38 | 432 | |
37144cf0 | 433 | InvalidateBestSize(); |
9f29226d | 434 | m_list->Arrange(); |
e9c0df38 VZ |
435 | return true; |
436 | } | |
437 | ||
438 | wxWindow *wxListbook::DoRemovePage(size_t page) | |
439 | { | |
33ebfc3b | 440 | const int page_count = GetPageCount(); |
61c083e7 | 441 | wxWindow *win = wxBookCtrlBase::DoRemovePage(page); |
bb08a4a1 | 442 | |
e9c0df38 VZ |
443 | if ( win ) |
444 | { | |
445 | m_list->DeleteItem(page); | |
33ebfc3b VZ |
446 | |
447 | if (m_selection >= (int)page) | |
448 | { | |
449 | // force new sel valid if possible | |
450 | int sel = m_selection - 1; | |
451 | if (page_count == 1) | |
bb08a4a1 | 452 | sel = wxNOT_FOUND; |
33ebfc3b VZ |
453 | else if ((page_count == 2) || (sel == -1)) |
454 | sel = 0; | |
bb08a4a1 | 455 | |
33ebfc3b | 456 | // force sel invalid if deleting current page - don't try to hide it |
bb08a4a1 WS |
457 | m_selection = (m_selection == (int)page) ? wxNOT_FOUND : m_selection - 1; |
458 | ||
459 | if ((sel != wxNOT_FOUND) && (sel != m_selection)) | |
33ebfc3b VZ |
460 | SetSelection(sel); |
461 | } | |
9f29226d WS |
462 | |
463 | m_list->Arrange(); | |
e9c0df38 VZ |
464 | } |
465 | ||
466 | return win; | |
467 | } | |
468 | ||
fbd11d30 RD |
469 | |
470 | bool wxListbook::DeleteAllPages() | |
471 | { | |
472 | m_list->DeleteAllItems(); | |
61c083e7 | 473 | return wxBookCtrlBase::DeleteAllPages(); |
fbd11d30 RD |
474 | } |
475 | ||
e9c0df38 VZ |
476 | // ---------------------------------------------------------------------------- |
477 | // wxListbook events | |
478 | // ---------------------------------------------------------------------------- | |
479 | ||
480 | void wxListbook::OnListSelected(wxListEvent& eventList) | |
481 | { | |
482 | const int selNew = eventList.GetIndex(); | |
483 | ||
484 | if ( selNew == m_selection ) | |
485 | { | |
486 | // this event can only come from our own Select(m_selection) below | |
487 | // which we call when the page change is vetoed, so we should simply | |
488 | // ignore it | |
489 | return; | |
490 | } | |
491 | ||
716dc245 | 492 | SetSelection(selNew); |
e9c0df38 | 493 | |
716dc245 WS |
494 | // change wasn't allowed, return to previous state |
495 | if (m_selection != selNew) | |
e9c0df38 VZ |
496 | { |
497 | m_list->Select(m_selection); | |
716dc245 | 498 | m_list->Focus(m_selection); |
e9c0df38 | 499 | } |
e9c0df38 VZ |
500 | } |
501 | ||
502 | #endif // wxUSE_LISTBOOK | |
503 |