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