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