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