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