]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/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> | |
9 | // Licence: wxWindows licence | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
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/listbook.h" | |
30 | ||
31 | #ifndef WX_PRECOMP | |
32 | #include "wx/settings.h" | |
33 | #endif | |
34 | ||
35 | #include "wx/listctrl.h" | |
36 | #include "wx/statline.h" | |
37 | #include "wx/imaglist.h" | |
38 | ||
39 | // ---------------------------------------------------------------------------- | |
40 | // various wxWidgets macros | |
41 | // ---------------------------------------------------------------------------- | |
42 | ||
43 | // check that the page index is valid | |
44 | #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount()) | |
45 | ||
46 | // ---------------------------------------------------------------------------- | |
47 | // event table | |
48 | // ---------------------------------------------------------------------------- | |
49 | ||
50 | IMPLEMENT_DYNAMIC_CLASS(wxListbook, wxBookCtrlBase) | |
51 | IMPLEMENT_DYNAMIC_CLASS(wxListbookEvent, wxNotifyEvent) | |
52 | ||
53 | const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING = wxNewEventType(); | |
54 | const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED = wxNewEventType(); | |
55 | ||
56 | BEGIN_EVENT_TABLE(wxListbook, wxBookCtrlBase) | |
57 | EVT_SIZE(wxListbook::OnSize) | |
58 | EVT_LIST_ITEM_SELECTED(wxID_ANY, wxListbook::OnListSelected) | |
59 | END_EVENT_TABLE() | |
60 | ||
61 | // ============================================================================ | |
62 | // wxListbook implementation | |
63 | // ============================================================================ | |
64 | ||
65 | // ---------------------------------------------------------------------------- | |
66 | // wxListbook creation | |
67 | // ---------------------------------------------------------------------------- | |
68 | ||
69 | void wxListbook::Init() | |
70 | { | |
71 | m_selection = wxNOT_FOUND; | |
72 | } | |
73 | ||
74 | bool | |
75 | wxListbook::Create(wxWindow *parent, | |
76 | wxWindowID id, | |
77 | const wxPoint& pos, | |
78 | const wxSize& size, | |
79 | long style, | |
80 | const wxString& name) | |
81 | { | |
82 | if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT ) | |
83 | { | |
84 | #ifdef __WXMAC__ | |
85 | style |= wxBK_TOP; | |
86 | #else // !__WXMAC__ | |
87 | style |= wxBK_LEFT; | |
88 | #endif // __WXMAC__/!__WXMAC__ | |
89 | } | |
90 | ||
91 | // no border for this control, it doesn't look nice together with | |
92 | // wxListCtrl border | |
93 | style &= ~wxBORDER_MASK; | |
94 | style |= wxBORDER_NONE; | |
95 | ||
96 | if ( !wxControl::Create(parent, id, pos, size, style, | |
97 | wxDefaultValidator, name) ) | |
98 | return false; | |
99 | ||
100 | m_bookctrl = new wxListView | |
101 | ( | |
102 | this, | |
103 | wxID_ANY, | |
104 | wxDefaultPosition, | |
105 | wxDefaultSize, | |
106 | wxLC_SINGLE_SEL | wxLC_REPORT | wxLC_NO_HEADER | |
107 | ); | |
108 | ||
109 | GetListView()->InsertColumn(0, wxT("Pages")); | |
110 | ||
111 | #ifdef __WXMSW__ | |
112 | // On XP with themes enabled the GetViewRect used in GetControllerSize() to | |
113 | // determine the space needed for the list view will incorrectly return | |
114 | // (0,0,0,0) the first time. So send a pending event so OnSize will be | |
115 | // called again after the window is ready to go. Technically we don't | |
116 | // need to do this on non-XP windows, but if things are already sized | |
117 | // correctly then nothing changes and so there is no harm. | |
118 | wxSizeEvent evt; | |
119 | GetEventHandler()->AddPendingEvent(evt); | |
120 | #endif | |
121 | return true; | |
122 | } | |
123 | ||
124 | // ---------------------------------------------------------------------------- | |
125 | // wxListbook geometry management | |
126 | // ---------------------------------------------------------------------------- | |
127 | ||
128 | wxSize wxListbook::GetControllerSize() const | |
129 | { | |
130 | const wxSize sizeClient = GetClientSize(), | |
131 | sizeBorder = m_bookctrl->GetSize() - m_bookctrl->GetClientSize(), | |
132 | sizeList = GetListView()->GetViewRect().GetSize() + sizeBorder; | |
133 | ||
134 | wxSize size; | |
135 | ||
136 | if ( IsVertical() ) | |
137 | { | |
138 | size.x = sizeClient.x; | |
139 | size.y = sizeList.y; | |
140 | } | |
141 | else // left/right aligned | |
142 | { | |
143 | size.x = sizeList.x; | |
144 | size.y = sizeClient.y; | |
145 | } | |
146 | ||
147 | return size; | |
148 | } | |
149 | ||
150 | void wxListbook::OnSize(wxSizeEvent& event) | |
151 | { | |
152 | // arrange the icons before calling SetClientSize(), otherwise it wouldn't | |
153 | // account for the scrollbars the list control might need and, at least | |
154 | // under MSW, we'd finish with an ugly looking list control with both | |
155 | // vertical and horizontal scrollbar (with one of them being added because | |
156 | // the other one is not accounted for in client size computations) | |
157 | wxListView *list = GetListView(); | |
158 | if (list) list->Arrange(); | |
159 | wxBookCtrlBase::OnSize(event); | |
160 | } | |
161 | ||
162 | int wxListbook::HitTest(const wxPoint& pt, long *flags) const | |
163 | { | |
164 | int pagePos = wxNOT_FOUND; | |
165 | ||
166 | if ( flags ) | |
167 | *flags = wxBK_HITTEST_NOWHERE; | |
168 | ||
169 | // convert from listbook control coordinates to list control coordinates | |
170 | const wxListView * const list = GetListView(); | |
171 | const wxPoint listPt = list->ScreenToClient(ClientToScreen(pt)); | |
172 | ||
173 | // is the point inside list control? | |
174 | if ( wxRect(list->GetSize()).Contains(listPt) ) | |
175 | { | |
176 | int flagsList; | |
177 | pagePos = list->HitTest(listPt, flagsList); | |
178 | ||
179 | if ( flags ) | |
180 | { | |
181 | if ( pagePos != wxNOT_FOUND ) | |
182 | *flags = 0; | |
183 | ||
184 | if ( flagsList & (wxLIST_HITTEST_ONITEMICON | | |
185 | wxLIST_HITTEST_ONITEMSTATEICON ) ) | |
186 | *flags |= wxBK_HITTEST_ONICON; | |
187 | ||
188 | if ( flagsList & wxLIST_HITTEST_ONITEMLABEL ) | |
189 | *flags |= wxBK_HITTEST_ONLABEL; | |
190 | } | |
191 | } | |
192 | else // not over list control at all | |
193 | { | |
194 | if ( flags && GetPageRect().Contains(pt) ) | |
195 | *flags |= wxBK_HITTEST_ONPAGE; | |
196 | } | |
197 | ||
198 | return pagePos; | |
199 | } | |
200 | ||
201 | wxSize wxListbook::CalcSizeFromPage(const wxSize& sizePage) const | |
202 | { | |
203 | // we need to add the size of the list control and the border between | |
204 | const wxSize sizeList = GetControllerSize(); | |
205 | ||
206 | wxSize size = sizePage; | |
207 | if ( IsVertical() ) | |
208 | { | |
209 | size.y += sizeList.y + GetInternalBorder(); | |
210 | } | |
211 | else // left/right aligned | |
212 | { | |
213 | size.x += sizeList.x + GetInternalBorder(); | |
214 | } | |
215 | ||
216 | return size; | |
217 | } | |
218 | ||
219 | ||
220 | // ---------------------------------------------------------------------------- | |
221 | // accessing the pages | |
222 | // ---------------------------------------------------------------------------- | |
223 | ||
224 | bool wxListbook::SetPageText(size_t n, const wxString& strText) | |
225 | { | |
226 | GetListView()->SetItemText(n, strText); | |
227 | ||
228 | return true; | |
229 | } | |
230 | ||
231 | wxString wxListbook::GetPageText(size_t n) const | |
232 | { | |
233 | return GetListView()->GetItemText(n); | |
234 | } | |
235 | ||
236 | int wxListbook::GetPageImage(size_t n) const | |
237 | { | |
238 | wxListItem item; | |
239 | item.SetId(n); | |
240 | ||
241 | if (GetListView()->GetItem(item)) | |
242 | { | |
243 | return item.GetImage(); | |
244 | } | |
245 | else | |
246 | { | |
247 | return wxNOT_FOUND; | |
248 | } | |
249 | } | |
250 | ||
251 | bool wxListbook::SetPageImage(size_t n, int imageId) | |
252 | { | |
253 | return GetListView()->SetItemImage(n, imageId); | |
254 | } | |
255 | ||
256 | // ---------------------------------------------------------------------------- | |
257 | // image list stuff | |
258 | // ---------------------------------------------------------------------------- | |
259 | ||
260 | void wxListbook::SetImageList(wxImageList *imageList) | |
261 | { | |
262 | wxListView * const list = GetListView(); | |
263 | ||
264 | // If imageList presence has changed, we update the list control view | |
265 | if ( (imageList != NULL) != (GetImageList() != NULL) ) | |
266 | { | |
267 | wxArrayString labels; | |
268 | labels.Alloc(GetPageCount()); | |
269 | ||
270 | wxArrayInt imageIds; | |
271 | imageIds.Alloc(GetPageCount()); | |
272 | ||
273 | const int oldSel = GetSelection(); | |
274 | size_t i; | |
275 | ||
276 | // Grab snapshot of all list control items before changing the window | |
277 | // style (which deletes the items) | |
278 | for ( i = 0; i < GetPageCount(); i++ ) | |
279 | { | |
280 | labels.Add(GetPageText(i)); | |
281 | imageIds.Add(GetPageImage(i)); | |
282 | } | |
283 | ||
284 | // Update the style to use icon view for images, report view otherwise | |
285 | long style = wxLC_SINGLE_SEL; | |
286 | if ( imageList ) | |
287 | { | |
288 | list->SetWindowStyleFlag(style | | |
289 | (IsVertical() ? wxLC_ALIGN_LEFT | |
290 | : wxLC_ALIGN_TOP) | | |
291 | wxLC_ICON); | |
292 | } | |
293 | else // no image list | |
294 | { | |
295 | list->SetWindowStyleFlag(style | wxLC_REPORT | wxLC_NO_HEADER); | |
296 | list->InsertColumn(0, wxT("Pages")); | |
297 | } | |
298 | ||
299 | // Add back the list control items | |
300 | for ( i = 0; i < GetPageCount(); i++ ) | |
301 | { | |
302 | list->InsertItem(i, labels[i], imageIds[i]); | |
303 | } | |
304 | ||
305 | // Restore selection | |
306 | if ( oldSel != wxNOT_FOUND ) | |
307 | SetSelection(oldSel); | |
308 | } | |
309 | ||
310 | list->SetImageList(imageList, wxIMAGE_LIST_NORMAL); | |
311 | ||
312 | wxBookCtrlBase::SetImageList(imageList); | |
313 | } | |
314 | ||
315 | // ---------------------------------------------------------------------------- | |
316 | // selection | |
317 | // ---------------------------------------------------------------------------- | |
318 | ||
319 | void wxListbook::UpdateSelectedPage(size_t newsel) | |
320 | { | |
321 | m_selection = newsel; | |
322 | GetListView()->Select(newsel); | |
323 | GetListView()->Focus(newsel); | |
324 | } | |
325 | ||
326 | int wxListbook::GetSelection() const | |
327 | { | |
328 | return m_selection; | |
329 | } | |
330 | ||
331 | wxBookCtrlBaseEvent* wxListbook::CreatePageChangingEvent() const | |
332 | { | |
333 | return new wxListbookEvent(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING, m_windowId); | |
334 | } | |
335 | ||
336 | void wxListbook::MakeChangedEvent(wxBookCtrlBaseEvent &event) | |
337 | { | |
338 | event.SetEventType(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED); | |
339 | } | |
340 | ||
341 | ||
342 | // ---------------------------------------------------------------------------- | |
343 | // adding/removing the pages | |
344 | // ---------------------------------------------------------------------------- | |
345 | ||
346 | bool | |
347 | wxListbook::InsertPage(size_t n, | |
348 | wxWindow *page, | |
349 | const wxString& text, | |
350 | bool bSelect, | |
351 | int imageId) | |
352 | { | |
353 | if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) ) | |
354 | return false; | |
355 | ||
356 | GetListView()->InsertItem(n, text, imageId); | |
357 | ||
358 | // if the inserted page is before the selected one, we must update the | |
359 | // index of the selected page | |
360 | if ( int(n) <= m_selection ) | |
361 | { | |
362 | // one extra page added | |
363 | m_selection++; | |
364 | GetListView()->Select(m_selection); | |
365 | GetListView()->Focus(m_selection); | |
366 | } | |
367 | ||
368 | // some page should be selected: either this one or the first one if there | |
369 | // is still no selection | |
370 | int selNew = -1; | |
371 | if ( bSelect ) | |
372 | selNew = n; | |
373 | else if ( m_selection == -1 ) | |
374 | selNew = 0; | |
375 | ||
376 | if ( selNew != m_selection ) | |
377 | page->Hide(); | |
378 | ||
379 | if ( selNew != -1 ) | |
380 | SetSelection(selNew); | |
381 | ||
382 | wxSizeEvent sz(GetSize(), GetId()); | |
383 | GetEventHandler()->ProcessEvent(sz); | |
384 | ||
385 | return true; | |
386 | } | |
387 | ||
388 | wxWindow *wxListbook::DoRemovePage(size_t page) | |
389 | { | |
390 | const size_t page_count = GetPageCount(); | |
391 | wxWindow *win = wxBookCtrlBase::DoRemovePage(page); | |
392 | ||
393 | if ( win ) | |
394 | { | |
395 | GetListView()->DeleteItem(page); | |
396 | ||
397 | if (m_selection >= (int)page) | |
398 | { | |
399 | // force new sel valid if possible | |
400 | int sel = m_selection - 1; | |
401 | if (page_count == 1) | |
402 | sel = wxNOT_FOUND; | |
403 | else if ((page_count == 2) || (sel == -1)) | |
404 | sel = 0; | |
405 | ||
406 | // force sel invalid if deleting current page - don't try to hide it | |
407 | m_selection = (m_selection == (int)page) ? wxNOT_FOUND : m_selection - 1; | |
408 | ||
409 | if ((sel != wxNOT_FOUND) && (sel != m_selection)) | |
410 | SetSelection(sel); | |
411 | } | |
412 | ||
413 | GetListView()->Arrange(); | |
414 | if (GetPageCount() == 0) | |
415 | { | |
416 | wxSizeEvent sz(GetSize(), GetId()); | |
417 | GetEventHandler()->ProcessEvent(sz); | |
418 | } | |
419 | } | |
420 | ||
421 | return win; | |
422 | } | |
423 | ||
424 | ||
425 | bool wxListbook::DeleteAllPages() | |
426 | { | |
427 | GetListView()->DeleteAllItems(); | |
428 | if (!wxBookCtrlBase::DeleteAllPages()) | |
429 | return false; | |
430 | ||
431 | m_selection = -1; | |
432 | ||
433 | wxSizeEvent sz(GetSize(), GetId()); | |
434 | GetEventHandler()->ProcessEvent(sz); | |
435 | ||
436 | return true; | |
437 | } | |
438 | ||
439 | // ---------------------------------------------------------------------------- | |
440 | // wxListbook events | |
441 | // ---------------------------------------------------------------------------- | |
442 | ||
443 | void wxListbook::OnListSelected(wxListEvent& eventList) | |
444 | { | |
445 | if ( eventList.GetEventObject() != m_bookctrl ) | |
446 | { | |
447 | eventList.Skip(); | |
448 | return; | |
449 | } | |
450 | ||
451 | const int selNew = eventList.GetIndex(); | |
452 | ||
453 | if ( selNew == m_selection ) | |
454 | { | |
455 | // this event can only come from our own Select(m_selection) below | |
456 | // which we call when the page change is vetoed, so we should simply | |
457 | // ignore it | |
458 | return; | |
459 | } | |
460 | ||
461 | SetSelection(selNew); | |
462 | ||
463 | // change wasn't allowed, return to previous state | |
464 | if (m_selection != selNew) | |
465 | { | |
466 | GetListView()->Select(m_selection); | |
467 | GetListView()->Focus(m_selection); | |
468 | } | |
469 | } | |
470 | ||
471 | #endif // wxUSE_LISTBOOK |