]> git.saurik.com Git - wxWidgets.git/blob - src/generic/listbkg.cpp
Fixed wxRichTextCtrl caret test case
[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 #include "wx/sysopt.h"
40
41 // ----------------------------------------------------------------------------
42 // various wxWidgets macros
43 // ----------------------------------------------------------------------------
44
45 // check that the page index is valid
46 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
47
48 // ----------------------------------------------------------------------------
49 // event table
50 // ----------------------------------------------------------------------------
51
52 IMPLEMENT_DYNAMIC_CLASS(wxListbook, wxBookCtrlBase)
53
54 wxDEFINE_EVENT( wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING, wxBookCtrlEvent );
55 wxDEFINE_EVENT( wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED, wxBookCtrlEvent );
56
57 BEGIN_EVENT_TABLE(wxListbook, wxBookCtrlBase)
58 EVT_SIZE(wxListbook::OnSize)
59 EVT_LIST_ITEM_SELECTED(wxID_ANY, wxListbook::OnListSelected)
60 END_EVENT_TABLE()
61
62 // ============================================================================
63 // wxListbook implementation
64 // ============================================================================
65
66 // ----------------------------------------------------------------------------
67 // wxListbook creation
68 // ----------------------------------------------------------------------------
69
70 bool
71 wxListbook::Create(wxWindow *parent,
72 wxWindowID id,
73 const wxPoint& pos,
74 const wxSize& size,
75 long style,
76 const wxString& name)
77 {
78 if ( (style & wxBK_ALIGN_MASK) == wxBK_DEFAULT )
79 {
80 #ifdef __WXMAC__
81 style |= wxBK_TOP;
82 #else // !__WXMAC__
83 style |= wxBK_LEFT;
84 #endif // __WXMAC__/!__WXMAC__
85 }
86
87 // no border for this control, it doesn't look nice together with
88 // wxListCtrl border
89 style &= ~wxBORDER_MASK;
90 style |= wxBORDER_NONE;
91
92 if ( !wxControl::Create(parent, id, pos, size, style,
93 wxDefaultValidator, name) )
94 return false;
95
96 m_bookctrl = new wxListView
97 (
98 this,
99 wxID_ANY,
100 wxDefaultPosition,
101 wxDefaultSize,
102 wxLC_SINGLE_SEL |
103 (IsVertical() ? wxLC_ALIGN_LEFT : wxLC_ALIGN_TOP) |
104 wxLC_LIST
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 void wxListbook::OnSize(wxSizeEvent& event)
125 {
126 // arrange the icons before calling SetClientSize(), otherwise it wouldn't
127 // account for the scrollbars the list control might need and, at least
128 // under MSW, we'd finish with an ugly looking list control with both
129 // vertical and horizontal scrollbar (with one of them being added because
130 // the other one is not accounted for in client size computations)
131 wxListView * const list = GetListView();
132 if ( list )
133 list->Arrange();
134
135 event.Skip();
136 }
137
138 int wxListbook::HitTest(const wxPoint& pt, long *flags) const
139 {
140 int pagePos = wxNOT_FOUND;
141
142 if ( flags )
143 *flags = wxBK_HITTEST_NOWHERE;
144
145 // convert from listbook control coordinates to list control coordinates
146 const wxListView * const list = GetListView();
147 const wxPoint listPt = list->ScreenToClient(ClientToScreen(pt));
148
149 // is the point inside list control?
150 if ( wxRect(list->GetSize()).Contains(listPt) )
151 {
152 int flagsList;
153 pagePos = list->HitTest(listPt, flagsList);
154
155 if ( flags )
156 {
157 if ( pagePos != wxNOT_FOUND )
158 *flags = 0;
159
160 if ( flagsList & (wxLIST_HITTEST_ONITEMICON |
161 wxLIST_HITTEST_ONITEMSTATEICON ) )
162 *flags |= wxBK_HITTEST_ONICON;
163
164 if ( flagsList & wxLIST_HITTEST_ONITEMLABEL )
165 *flags |= wxBK_HITTEST_ONLABEL;
166 }
167 }
168 else // not over list control at all
169 {
170 if ( flags && GetPageRect().Contains(pt) )
171 *flags |= wxBK_HITTEST_ONPAGE;
172 }
173
174 return pagePos;
175 }
176
177 void wxListbook::UpdateSize()
178 {
179 // we should find a more elegant way to force a layout than generating this
180 // dummy event
181 wxSizeEvent sz(GetSize(), GetId());
182 GetEventHandler()->ProcessEvent(sz);
183 }
184
185 // ----------------------------------------------------------------------------
186 // accessing the pages
187 // ----------------------------------------------------------------------------
188
189 bool wxListbook::SetPageText(size_t n, const wxString& strText)
190 {
191 GetListView()->SetItemText(n, strText);
192
193 return true;
194 }
195
196 wxString wxListbook::GetPageText(size_t n) const
197 {
198 return GetListView()->GetItemText(n);
199 }
200
201 int wxListbook::GetPageImage(size_t n) const
202 {
203 wxListItem item;
204 item.SetId(n);
205
206 if (GetListView()->GetItem(item))
207 {
208 return item.GetImage();
209 }
210 else
211 {
212 return wxNOT_FOUND;
213 }
214 }
215
216 bool wxListbook::SetPageImage(size_t n, int imageId)
217 {
218 return GetListView()->SetItemImage(n, imageId);
219 }
220
221 // ----------------------------------------------------------------------------
222 // image list stuff
223 // ----------------------------------------------------------------------------
224
225 void wxListbook::SetImageList(wxImageList *imageList)
226 {
227 wxListView * const list = GetListView();
228
229 // If imageList presence has changed, we update the list control style
230 if ( (imageList != NULL) != (GetImageList() != NULL) )
231 {
232 // Preserve the selection which is lost when changing the mode
233 const int oldSel = GetSelection();
234
235 // Update the style to use icon view for images, list view otherwise
236 long style = list->GetWindowStyle() & ~wxLC_MASK_TYPE;
237 if ( imageList )
238 {
239 style |= wxLC_ICON;
240 }
241 else // no image list
242 {
243 style |= wxLC_LIST;
244 }
245
246 list->SetWindowStyleFlag(style);
247
248 // Restore selection
249 if ( oldSel != wxNOT_FOUND )
250 SetSelection(oldSel);
251 }
252
253 list->SetImageList(imageList, wxIMAGE_LIST_NORMAL);
254
255 wxBookCtrlBase::SetImageList(imageList);
256 }
257
258 // ----------------------------------------------------------------------------
259 // selection
260 // ----------------------------------------------------------------------------
261
262 void wxListbook::UpdateSelectedPage(size_t newsel)
263 {
264 m_selection = newsel;
265 GetListView()->Select(newsel);
266 GetListView()->Focus(newsel);
267 }
268
269 wxBookCtrlEvent* wxListbook::CreatePageChangingEvent() const
270 {
271 return new wxBookCtrlEvent(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING, m_windowId);
272 }
273
274 void wxListbook::MakeChangedEvent(wxBookCtrlEvent &event)
275 {
276 event.SetEventType(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED);
277 }
278
279
280 // ----------------------------------------------------------------------------
281 // adding/removing the pages
282 // ----------------------------------------------------------------------------
283
284 bool
285 wxListbook::InsertPage(size_t n,
286 wxWindow *page,
287 const wxString& text,
288 bool bSelect,
289 int imageId)
290 {
291 if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) )
292 return false;
293
294 GetListView()->InsertItem(n, text, imageId);
295
296 // if the inserted page is before the selected one, we must update the
297 // index of the selected page
298 if ( int(n) <= m_selection )
299 {
300 // one extra page added
301 m_selection++;
302 GetListView()->Select(m_selection);
303 GetListView()->Focus(m_selection);
304 }
305
306 if ( !DoSetSelectionAfterInsertion(n, bSelect) )
307 page->Hide();
308
309 UpdateSize();
310
311 return true;
312 }
313
314 wxWindow *wxListbook::DoRemovePage(size_t page)
315 {
316 const size_t page_count = GetPageCount();
317 wxWindow *win = wxBookCtrlBase::DoRemovePage(page);
318
319 if ( win )
320 {
321 GetListView()->DeleteItem(page);
322
323 if (m_selection >= (int)page)
324 {
325 // force new sel valid if possible
326 int sel = m_selection - 1;
327 if (page_count == 1)
328 sel = wxNOT_FOUND;
329 else if ((page_count == 2) || (sel == wxNOT_FOUND))
330 sel = 0;
331
332 // force sel invalid if deleting current page - don't try to hide it
333 m_selection = (m_selection == (int)page) ? wxNOT_FOUND : m_selection - 1;
334
335 if ((sel != wxNOT_FOUND) && (sel != m_selection))
336 SetSelection(sel);
337 }
338
339 GetListView()->Arrange();
340 UpdateSize();
341 }
342
343 return win;
344 }
345
346
347 bool wxListbook::DeleteAllPages()
348 {
349 GetListView()->DeleteAllItems();
350 if (!wxBookCtrlBase::DeleteAllPages())
351 return false;
352
353 UpdateSize();
354
355 return true;
356 }
357
358 // ----------------------------------------------------------------------------
359 // wxListbook events
360 // ----------------------------------------------------------------------------
361
362 void wxListbook::OnListSelected(wxListEvent& eventList)
363 {
364 if ( eventList.GetEventObject() != m_bookctrl )
365 {
366 eventList.Skip();
367 return;
368 }
369
370 const int selNew = eventList.GetIndex();
371
372 if ( selNew == m_selection )
373 {
374 // this event can only come from our own Select(m_selection) below
375 // which we call when the page change is vetoed, so we should simply
376 // ignore it
377 return;
378 }
379
380 SetSelection(selNew);
381
382 // change wasn't allowed, return to previous state
383 if (m_selection != selNew)
384 {
385 GetListView()->Select(m_selection);
386 GetListView()->Focus(m_selection);
387 }
388 }
389
390 #endif // wxUSE_LISTBOOK