Add #if !WXWIN_COMPATIBILITY_EVENT_TYPES around wxNewEventType calls
[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/listctrl.h"
30 #include "wx/statline.h"
31 #include "wx/listbook.h"
32 #include "wx/imaglist.h"
33 #include "wx/settings.h"
34
35 // ----------------------------------------------------------------------------
36 // various wxWidgets macros
37 // ----------------------------------------------------------------------------
38
39 // check that the page index is valid
40 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
41
42 // ----------------------------------------------------------------------------
43 // event table
44 // ----------------------------------------------------------------------------
45
46 IMPLEMENT_DYNAMIC_CLASS(wxListbook, wxBookCtrlBase)
47 IMPLEMENT_DYNAMIC_CLASS(wxListbookEvent, wxNotifyEvent)
48
49 #if !WXWIN_COMPATIBILITY_EVENT_TYPES
50 const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING = wxNewEventType();
51 const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED = wxNewEventType();
52 #endif
53 const int wxID_LISTBOOKLISTVIEW = wxNewId();
54
55 BEGIN_EVENT_TABLE(wxListbook, wxBookCtrlBase)
56 EVT_SIZE(wxListbook::OnSize)
57 EVT_LIST_ITEM_SELECTED(wxID_LISTBOOKLISTVIEW, wxListbook::OnListSelected)
58 END_EVENT_TABLE()
59
60 // ============================================================================
61 // wxListbook implementation
62 // ============================================================================
63
64 // ----------------------------------------------------------------------------
65 // wxListbook creation
66 // ----------------------------------------------------------------------------
67
68 void wxListbook::Init()
69 {
70 m_selection = wxNOT_FOUND;
71 }
72
73 bool
74 wxListbook::Create(wxWindow *parent,
75 wxWindowID id,
76 const wxPoint& pos,
77 const wxSize& size,
78 long style,
79 const wxString& name)
80 {
81 if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT )
82 {
83 #ifdef __WXMAC__
84 style |= wxBK_TOP;
85 #else // !__WXMAC__
86 style |= wxBK_LEFT;
87 #endif // __WXMAC__/!__WXMAC__
88 }
89
90 // no border for this control, it doesn't look nice together with
91 // wxListCtrl border
92 style &= ~wxBORDER_MASK;
93 style |= wxBORDER_NONE;
94
95 if ( !wxControl::Create(parent, id, pos, size, style,
96 wxDefaultValidator, name) )
97 return false;
98
99 m_bookctrl = new wxListView
100 (
101 this,
102 wxID_LISTBOOKLISTVIEW,
103 wxDefaultPosition,
104 wxDefaultSize,
105 wxLC_ICON | wxLC_SINGLE_SEL |
106 (IsVertical() ? wxLC_ALIGN_LEFT : wxLC_ALIGN_TOP)
107 );
108
109 #ifdef __WXMSW__
110 // On XP with themes enabled the GetViewRect used in GetControllerSize() to
111 // determine the space needed for the list view will incorrectly return
112 // (0,0,0,0) the first time. So send a pending event so OnSize will be
113 // called again after the window is ready to go. Technically we don't
114 // need to do this on non-XP windows, but if things are already sized
115 // correctly then nothing changes and so there is no harm.
116 wxSizeEvent evt;
117 GetEventHandler()->AddPendingEvent(evt);
118 #endif
119 return true;
120 }
121
122 // ----------------------------------------------------------------------------
123 // wxListbook geometry management
124 // ----------------------------------------------------------------------------
125
126 wxSize wxListbook::GetControllerSize() const
127 {
128 const wxSize sizeClient = GetClientSize(),
129 sizeBorder = m_bookctrl->GetSize() - m_bookctrl->GetClientSize(),
130 sizeList = GetListView()->GetViewRect().GetSize() + sizeBorder;
131
132 wxSize size;
133
134 if ( IsVertical() )
135 {
136 size.x = sizeClient.x;
137 size.y = sizeList.y;
138 }
139 else // left/right aligned
140 {
141 size.x = sizeList.x;
142 size.y = sizeClient.y;
143 }
144
145 return size;
146 }
147
148 void wxListbook::OnSize(wxSizeEvent& event)
149 {
150 // arrange the icons before calling SetClientSize(), otherwise it wouldn't
151 // account for the scrollbars the list control might need and, at least
152 // under MSW, we'd finish with an ugly looking list control with both
153 // vertical and horizontal scrollbar (with one of them being added because
154 // the other one is not accounted for in client size computations)
155 wxListView *list = GetListView();
156 if (list) list->Arrange();
157 wxBookCtrlBase::OnSize(event);
158 }
159
160 wxSize wxListbook::CalcSizeFromPage(const wxSize& sizePage) const
161 {
162 // we need to add the size of the list control and the border between
163 const wxSize sizeList = GetControllerSize();
164
165 wxSize size = sizePage;
166 if ( IsVertical() )
167 {
168 size.y += sizeList.y + GetInternalBorder();
169 }
170 else // left/right aligned
171 {
172 size.x += sizeList.x + GetInternalBorder();
173 }
174
175 return size;
176 }
177
178
179 // ----------------------------------------------------------------------------
180 // accessing the pages
181 // ----------------------------------------------------------------------------
182
183 bool wxListbook::SetPageText(size_t n, const wxString& strText)
184 {
185 GetListView()->SetItemText(n, strText);
186
187 return true;
188 }
189
190 wxString wxListbook::GetPageText(size_t n) const
191 {
192 return GetListView()->GetItemText(n);
193 }
194
195 int wxListbook::GetPageImage(size_t WXUNUSED(n)) const
196 {
197 wxFAIL_MSG( _T("wxListbook::GetPageImage() not implemented") );
198
199 return wxNOT_FOUND;
200 }
201
202 bool wxListbook::SetPageImage(size_t n, int imageId)
203 {
204 return GetListView()->SetItemImage(n, imageId);
205 }
206
207 // ----------------------------------------------------------------------------
208 // image list stuff
209 // ----------------------------------------------------------------------------
210
211 void wxListbook::SetImageList(wxImageList *imageList)
212 {
213 GetListView()->SetImageList(imageList, wxIMAGE_LIST_NORMAL);
214
215 wxBookCtrlBase::SetImageList(imageList);
216 }
217
218 // ----------------------------------------------------------------------------
219 // selection
220 // ----------------------------------------------------------------------------
221
222 int wxListbook::GetSelection() const
223 {
224 return m_selection;
225 }
226
227 int wxListbook::SetSelection(size_t n)
228 {
229 wxCHECK_MSG( IS_VALID_PAGE(n), wxNOT_FOUND,
230 wxT("invalid page index in wxListbook::SetSelection()") );
231
232 const int oldSel = m_selection;
233
234 if ( int(n) != m_selection )
235 {
236 wxListbookEvent event(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING, m_windowId);
237 event.SetSelection(n);
238 event.SetOldSelection(m_selection);
239 event.SetEventObject(this);
240 if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
241 {
242 if ( m_selection != wxNOT_FOUND )
243 m_pages[m_selection]->Hide();
244
245 wxWindow *page = m_pages[n];
246 page->SetSize(GetPageRect());
247 page->Show();
248
249 // change m_selection now to ignore the selection change event
250 m_selection = n;
251 GetListView()->Select(n);
252 GetListView()->Focus(n);
253
254 // program allows the page change
255 event.SetEventType(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED);
256 (void)GetEventHandler()->ProcessEvent(event);
257 }
258 }
259
260 return oldSel;
261 }
262
263 // ----------------------------------------------------------------------------
264 // adding/removing the pages
265 // ----------------------------------------------------------------------------
266
267 bool
268 wxListbook::InsertPage(size_t n,
269 wxWindow *page,
270 const wxString& text,
271 bool bSelect,
272 int imageId)
273 {
274 if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) )
275 return false;
276
277 GetListView()->InsertItem(n, text, imageId);
278
279 // if the inserted page is before the selected one, we must update the
280 // index of the selected page
281 if ( int(n) <= m_selection )
282 {
283 // one extra page added
284 m_selection++;
285 GetListView()->Select(m_selection);
286 GetListView()->Focus(m_selection);
287 }
288
289 // some page should be selected: either this one or the first one if there
290 // is still no selection
291 int selNew = -1;
292 if ( bSelect )
293 selNew = n;
294 else if ( m_selection == -1 )
295 selNew = 0;
296
297 if ( selNew != m_selection )
298 page->Hide();
299
300 if ( selNew != -1 )
301 SetSelection(selNew);
302
303 InvalidateBestSize();
304 GetListView()->Arrange();
305 return true;
306 }
307
308 wxWindow *wxListbook::DoRemovePage(size_t page)
309 {
310 const size_t page_count = GetPageCount();
311 wxWindow *win = wxBookCtrlBase::DoRemovePage(page);
312
313 if ( win )
314 {
315 GetListView()->DeleteItem(page);
316
317 if (m_selection >= (int)page)
318 {
319 // force new sel valid if possible
320 int sel = m_selection - 1;
321 if (page_count == 1)
322 sel = wxNOT_FOUND;
323 else if ((page_count == 2) || (sel == -1))
324 sel = 0;
325
326 // force sel invalid if deleting current page - don't try to hide it
327 m_selection = (m_selection == (int)page) ? wxNOT_FOUND : m_selection - 1;
328
329 if ((sel != wxNOT_FOUND) && (sel != m_selection))
330 SetSelection(sel);
331 }
332
333 GetListView()->Arrange();
334 }
335
336 return win;
337 }
338
339
340 bool wxListbook::DeleteAllPages()
341 {
342 GetListView()->DeleteAllItems();
343 return wxBookCtrlBase::DeleteAllPages();
344 }
345
346 // ----------------------------------------------------------------------------
347 // wxListbook events
348 // ----------------------------------------------------------------------------
349
350 void wxListbook::OnListSelected(wxListEvent& eventList)
351 {
352 const int selNew = eventList.GetIndex();
353
354 if ( selNew == m_selection )
355 {
356 // this event can only come from our own Select(m_selection) below
357 // which we call when the page change is vetoed, so we should simply
358 // ignore it
359 return;
360 }
361
362 SetSelection(selNew);
363
364 // change wasn't allowed, return to previous state
365 if (m_selection != selNew)
366 {
367 GetListView()->Select(m_selection);
368 GetListView()->Focus(m_selection);
369 }
370 }
371
372 #endif // wxUSE_LISTBOOK