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