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