]> git.saurik.com Git - wxWidgets.git/blob - src/generic/listbkg.cpp
fix for a crash due to using NULL inputConv in Unicode build introduced in rev 1.162
[wxWidgets.git] / src / generic / listbkg.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: 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 // constants
37 // ----------------------------------------------------------------------------
38
39 // margin between the list and the page, should be bigger than wxStaticLine
40 // size
41 const wxCoord MARGIN = 5;
42
43 // ----------------------------------------------------------------------------
44 // various wxWidgets macros
45 // ----------------------------------------------------------------------------
46
47 // check that the page index is valid
48 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
49
50 // ----------------------------------------------------------------------------
51 // event table
52 // ----------------------------------------------------------------------------
53
54 IMPLEMENT_DYNAMIC_CLASS(wxListbook, wxControl)
55 IMPLEMENT_DYNAMIC_CLASS(wxListbookEvent, wxNotifyEvent)
56
57 const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING = wxNewEventType();
58 const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED = wxNewEventType();
59 const int wxID_LISTBOOKLISTVIEW = wxNewId();
60
61 BEGIN_EVENT_TABLE(wxListbook, wxBookCtrlBase)
62 EVT_SIZE(wxListbook::OnSize)
63 EVT_LIST_ITEM_SELECTED(wxID_LISTBOOKLISTVIEW, wxListbook::OnListSelected)
64 END_EVENT_TABLE()
65
66 // ============================================================================
67 // wxListbook implementation
68 // ============================================================================
69
70 // ----------------------------------------------------------------------------
71 // wxListbook creation
72 // ----------------------------------------------------------------------------
73
74 void wxListbook::Init()
75 {
76 m_list = NULL;
77 #if wxUSE_LINE_IN_LISTBOOK
78 m_line = NULL;
79 #endif // wxUSE_LINE_IN_LISTBOOK
80 m_selection = wxNOT_FOUND;
81 }
82
83 bool
84 wxListbook::Create(wxWindow *parent,
85 wxWindowID id,
86 const wxPoint& pos,
87 const wxSize& size,
88 long style,
89 const wxString& name)
90 {
91 if ( (style & wxLB_ALIGN_MASK) == wxLB_DEFAULT )
92 {
93 #ifdef __WXMAC__
94 style |= wxLB_TOP;
95 #else // !__WXMAC__
96 style |= wxLB_LEFT;
97 #endif // __WXMAC__/!__WXMAC__
98 }
99
100 // no border for this control, it doesn't look nice together with
101 // wxListCtrl border
102 style &= ~wxBORDER_MASK;
103 style |= wxBORDER_NONE;
104
105 if ( !wxControl::Create(parent, id, pos, size, style,
106 wxDefaultValidator, name) )
107 return false;
108
109 m_list = new wxListView
110 (
111 this,
112 wxID_LISTBOOKLISTVIEW,
113 wxDefaultPosition,
114 wxDefaultSize,
115 wxLC_ICON | wxLC_SINGLE_SEL |
116 (IsVertical() ? wxLC_ALIGN_LEFT : wxLC_ALIGN_TOP)
117 );
118
119 #if wxUSE_LINE_IN_LISTBOOK
120 m_line = new wxStaticLine
121 (
122 this,
123 wxID_ANY,
124 wxDefaultPosition,
125 wxDefaultSize,
126 IsVertical() ? wxLI_HORIZONTAL : wxLI_VERTICAL
127 );
128 #endif // wxUSE_LINE_IN_LISTBOOK
129
130 #ifdef __WXMSW__
131 // On XP with themes enabled the GetViewRect used in GetListSize to
132 // determine the space needed for the list view will incorrectly return
133 // (0,0,0,0) the first time. So send a pending event so OnSize will be
134 // called again after the window is ready to go. Technically we don't
135 // need to do this on non-XP windows, but if things are already sized
136 // correctly then nothing changes and so there is no harm.
137 wxSizeEvent evt;
138 GetEventHandler()->AddPendingEvent(evt);
139 #endif
140 return true;
141 }
142
143 // ----------------------------------------------------------------------------
144 // wxListbook geometry management
145 // ----------------------------------------------------------------------------
146
147 wxSize wxListbook::GetListSize() const
148 {
149 const wxSize sizeClient = GetClientSize(),
150 sizeBorder = m_list->GetSize() - m_list->GetClientSize(),
151 sizeList = m_list->GetViewRect().GetSize() + sizeBorder;
152
153 wxSize size;
154 if ( IsVertical() )
155 {
156 size.x = sizeClient.x;
157 size.y = sizeList.y;
158 }
159 else // left/right aligned
160 {
161 size.x = sizeList.x;
162 size.y = sizeClient.y;
163 }
164
165 return size;
166 }
167
168 wxRect wxListbook::GetPageRect() const
169 {
170 const wxSize sizeList = m_list->GetSize();
171
172 wxPoint pt;
173 wxRect rectPage(pt, GetClientSize());
174 switch ( GetWindowStyle() & wxLB_ALIGN_MASK )
175 {
176 default:
177 wxFAIL_MSG( _T("unexpected wxListbook alignment") );
178 // fall through
179
180 case wxLB_TOP:
181 rectPage.y = sizeList.y + MARGIN;
182 // fall through
183
184 case wxLB_BOTTOM:
185 rectPage.height -= sizeList.y + MARGIN;
186 break;
187
188 case wxLB_LEFT:
189 rectPage.x = sizeList.x + MARGIN;
190 // fall through
191
192 case wxLB_RIGHT:
193 rectPage.width -= sizeList.x + MARGIN;
194 break;
195 }
196
197 return rectPage;
198 }
199
200 void wxListbook::OnSize(wxSizeEvent& event)
201 {
202 event.Skip();
203
204 if ( !m_list )
205 {
206 // we're not fully created yet
207 return;
208 }
209
210 // arrange the icons before calling SetClientSize(), otherwise it wouldn't
211 // account for the scrollbars the list control might need and, at least
212 // under MSW, we'd finish with an ugly looking list control with both
213 // vertical and horizontal scrollbar (with one of them being added because
214 // the other one is not accounted for in client size computations)
215 m_list->Arrange();
216
217 // resize the list control and the page area to fit inside our new size
218 const wxSize sizeClient = GetClientSize(),
219 sizeBorder = m_list->GetSize() - m_list->GetClientSize(),
220 sizeList = GetListSize();
221
222 m_list->SetClientSize( sizeList.x - sizeBorder.x, sizeList.y - sizeBorder.y );
223
224 const wxSize sizeNew = m_list->GetSize();
225 wxPoint posList;
226 switch ( GetWindowStyle() & wxLB_ALIGN_MASK )
227 {
228 default:
229 wxFAIL_MSG( _T("unexpected wxListbook alignment") );
230 // fall through
231
232 case wxLB_TOP:
233 case wxLB_LEFT:
234 // posList is already ok
235 break;
236
237 case wxLB_BOTTOM:
238 posList.y = sizeClient.y - sizeNew.y;
239 break;
240
241 case wxLB_RIGHT:
242 posList.x = sizeClient.x - sizeNew.x;
243 break;
244 }
245
246 if ( m_list->GetPosition() != posList )
247 m_list->Move(posList);
248
249 #if wxUSE_LINE_IN_LISTBOOK
250 if ( m_line )
251 {
252 wxRect rectLine(sizeClient);
253
254 switch ( GetWindowStyle() & wxLB_ALIGN_MASK )
255 {
256 case wxLB_TOP:
257 rectLine.y = sizeNew.y + 1;
258 rectLine.height = MARGIN - 2;
259 break;
260
261 case wxLB_BOTTOM:
262 rectLine.height = MARGIN - 2;
263 rectLine.y = sizeClient.y - sizeNew.y - rectLine.height;
264 break;
265
266 case wxLB_LEFT:
267 rectLine.x = sizeNew.x + 1;
268 rectLine.width = MARGIN - 2;
269 break;
270
271 case wxLB_RIGHT:
272 rectLine.width = MARGIN - 2;
273 rectLine.x = sizeClient.x - sizeNew.x - rectLine.width;
274 break;
275 }
276
277 m_line->SetSize(rectLine);
278 }
279 #endif // wxUSE_LINE_IN_LISTBOOK
280
281 // resize the currently shown page
282 if (m_selection != wxNOT_FOUND )
283 {
284 wxWindow *page = m_pages[m_selection];
285 wxCHECK_RET( page, _T("NULL page in wxListbook?") );
286 page->SetSize(GetPageRect());
287 }
288 }
289
290 wxSize wxListbook::CalcSizeFromPage(const wxSize& sizePage) const
291 {
292 // we need to add the size of the list control and the margin
293 const wxSize sizeList = GetListSize();
294
295 wxSize size = sizePage;
296 if ( IsVertical() )
297 {
298 size.y += sizeList.y + MARGIN;
299 }
300 else // left/right aligned
301 {
302 size.x += sizeList.x + MARGIN;
303 }
304
305 return size;
306 }
307
308
309 // ----------------------------------------------------------------------------
310 // accessing the pages
311 // ----------------------------------------------------------------------------
312
313 bool wxListbook::SetPageText(size_t n, const wxString& strText)
314 {
315 m_list->SetItemText(n, strText);
316
317 return true;
318 }
319
320 wxString wxListbook::GetPageText(size_t n) const
321 {
322 return m_list->GetItemText(n);
323 }
324
325 int wxListbook::GetPageImage(size_t WXUNUSED(n)) const
326 {
327 wxFAIL_MSG( _T("wxListbook::GetPageImage() not implemented") );
328
329 return wxNOT_FOUND;
330 }
331
332 bool wxListbook::SetPageImage(size_t n, int imageId)
333 {
334 return m_list->SetItemImage(n, imageId);
335 }
336
337 // ----------------------------------------------------------------------------
338 // image list stuff
339 // ----------------------------------------------------------------------------
340
341 void wxListbook::SetImageList(wxImageList *imageList)
342 {
343 m_list->SetImageList(imageList, wxIMAGE_LIST_NORMAL);
344
345 wxBookCtrlBase::SetImageList(imageList);
346 }
347
348 // ----------------------------------------------------------------------------
349 // selection
350 // ----------------------------------------------------------------------------
351
352 int wxListbook::GetSelection() const
353 {
354 return m_selection;
355 }
356
357 int wxListbook::SetSelection(size_t n)
358 {
359 wxCHECK_MSG( IS_VALID_PAGE(n), wxNOT_FOUND,
360 wxT("invalid page index in wxListbook::SetSelection()") );
361
362 const int oldSel = m_selection;
363
364 if ( int(n) != m_selection )
365 {
366 wxListbookEvent event(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING, m_windowId);
367 event.SetSelection(n);
368 event.SetOldSelection(m_selection);
369 event.SetEventObject(this);
370 if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
371 {
372 if ( m_selection != wxNOT_FOUND )
373 m_pages[m_selection]->Hide();
374
375 wxWindow *page = m_pages[n];
376 page->SetSize(GetPageRect());
377 page->Show();
378
379 // change m_selection now to ignore the selection change event
380 m_selection = n;
381 m_list->Select(n);
382 m_list->Focus(n);
383
384 // program allows the page change
385 event.SetEventType(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED);
386 (void)GetEventHandler()->ProcessEvent(event);
387 }
388 }
389
390 return oldSel;
391 }
392
393 // ----------------------------------------------------------------------------
394 // adding/removing the pages
395 // ----------------------------------------------------------------------------
396
397 bool
398 wxListbook::InsertPage(size_t n,
399 wxWindow *page,
400 const wxString& text,
401 bool bSelect,
402 int imageId)
403 {
404 if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) )
405 return false;
406
407 m_list->InsertItem(n, text, imageId);
408
409 // if the inserted page is before the selected one, we must update the
410 // index of the selected page
411 if ( int(n) <= m_selection )
412 {
413 // one extra page added
414 m_selection++;
415 m_list->Select(m_selection);
416 m_list->Focus(m_selection);
417 }
418
419 // some page should be selected: either this one or the first one if there
420 // is still no selection
421 int selNew = -1;
422 if ( bSelect )
423 selNew = n;
424 else if ( m_selection == -1 )
425 selNew = 0;
426
427 if ( selNew != m_selection )
428 page->Hide();
429
430 if ( selNew != -1 )
431 SetSelection(selNew);
432
433 InvalidateBestSize();
434 m_list->Arrange();
435 return true;
436 }
437
438 wxWindow *wxListbook::DoRemovePage(size_t page)
439 {
440 const int page_count = GetPageCount();
441 wxWindow *win = wxBookCtrlBase::DoRemovePage(page);
442
443 if ( win )
444 {
445 m_list->DeleteItem(page);
446
447 if (m_selection >= (int)page)
448 {
449 // force new sel valid if possible
450 int sel = m_selection - 1;
451 if (page_count == 1)
452 sel = wxNOT_FOUND;
453 else if ((page_count == 2) || (sel == -1))
454 sel = 0;
455
456 // force sel invalid if deleting current page - don't try to hide it
457 m_selection = (m_selection == (int)page) ? wxNOT_FOUND : m_selection - 1;
458
459 if ((sel != wxNOT_FOUND) && (sel != m_selection))
460 SetSelection(sel);
461 }
462
463 m_list->Arrange();
464 }
465
466 return win;
467 }
468
469
470 bool wxListbook::DeleteAllPages()
471 {
472 m_list->DeleteAllItems();
473 return wxBookCtrlBase::DeleteAllPages();
474 }
475
476 // ----------------------------------------------------------------------------
477 // wxListbook events
478 // ----------------------------------------------------------------------------
479
480 void wxListbook::OnListSelected(wxListEvent& eventList)
481 {
482 const int selNew = eventList.GetIndex();
483
484 if ( selNew == m_selection )
485 {
486 // this event can only come from our own Select(m_selection) below
487 // which we call when the page change is vetoed, so we should simply
488 // ignore it
489 return;
490 }
491
492 SetSelection(selNew);
493
494 // change wasn't allowed, return to previous state
495 if (m_selection != selNew)
496 {
497 m_list->Select(m_selection);
498 m_list->Focus(m_selection);
499 }
500 }
501
502 #endif // wxUSE_LISTBOOK
503