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