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