]> git.saurik.com Git - wxWidgets.git/blame - src/generic/listbkg.cpp
Improve sizer support in generic wxNotebook. Fix the widgets sample
[wxWidgets.git] / src / generic / listbkg.cpp
CommitLineData
e9c0df38
VZ
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>
65571936 9// Licence: wxWindows licence
e9c0df38
VZ
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"
5b24f0d3 36#include "wx/imaglist.h"
c59da60a 37#include "wx/settings.h"
e9c0df38
VZ
38
39// ----------------------------------------------------------------------------
40// constants
41// ----------------------------------------------------------------------------
42
43// margin between the list and the page, should be bigger than wxStaticLine
44// size
45const wxCoord MARGIN = 5;
46
47// ----------------------------------------------------------------------------
77ffb593 48// various wxWidgets macros
e9c0df38
VZ
49// ----------------------------------------------------------------------------
50
1f30c176
WS
51// check that the page index is valid
52#define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
53
54// ----------------------------------------------------------------------------
55// event table
56// ----------------------------------------------------------------------------
57
e9c0df38
VZ
58IMPLEMENT_DYNAMIC_CLASS(wxListbook, wxControl)
59IMPLEMENT_DYNAMIC_CLASS(wxListbookEvent, wxNotifyEvent)
60
61const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING = wxNewEventType();
62const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED = wxNewEventType();
f29395d0 63const int wxID_LISTBOOKLISTVIEW = wxNewId();
e9c0df38 64
61c083e7 65BEGIN_EVENT_TABLE(wxListbook, wxBookCtrlBase)
e9c0df38 66 EVT_SIZE(wxListbook::OnSize)
f29395d0 67 EVT_LIST_ITEM_SELECTED(wxID_LISTBOOKLISTVIEW, wxListbook::OnListSelected)
e9c0df38
VZ
68END_EVENT_TABLE()
69
70// ============================================================================
71// wxListbook implementation
72// ============================================================================
73
74// ----------------------------------------------------------------------------
75// wxListbook creation
76// ----------------------------------------------------------------------------
77
78void wxListbook::Init()
79{
80 m_list = NULL;
ef0120c1 81#if wxUSE_LINE_IN_LISTBOOK
e9c0df38 82 m_line = NULL;
ef0120c1 83#endif // wxUSE_LINE_IN_LISTBOOK
e9c0df38
VZ
84 m_selection = wxNOT_FOUND;
85}
86
87bool
88wxListbook::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
ef0120c1
VZ
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
e9c0df38
VZ
109 if ( !wxControl::Create(parent, id, pos, size, style,
110 wxDefaultValidator, name) )
111 return false;
112
113 m_list = new wxListView
114 (
115 this,
f29395d0 116 wxID_LISTBOOKLISTVIEW,
e9c0df38
VZ
117 wxDefaultPosition,
118 wxDefaultSize,
ef0120c1 119 wxLC_ICON | wxLC_SINGLE_SEL |
4a06b348 120 (IsVertical() ? wxLC_ALIGN_LEFT : wxLC_ALIGN_TOP)
e9c0df38
VZ
121 );
122
ef0120c1 123#if wxUSE_LINE_IN_LISTBOOK
e9c0df38
VZ
124 m_line = new wxStaticLine
125 (
126 this,
ca65c044 127 wxID_ANY,
e9c0df38
VZ
128 wxDefaultPosition,
129 wxDefaultSize,
130 IsVertical() ? wxLI_HORIZONTAL : wxLI_VERTICAL
131 );
ef0120c1 132#endif // wxUSE_LINE_IN_LISTBOOK
e9c0df38 133
a9092ede
RD
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
7b45094a 137 // (0,0,0,0) the first time. So send a pending event so OnSize will be
a9092ede
RD
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
e9c0df38
VZ
144 return true;
145}
146
147// ----------------------------------------------------------------------------
148// wxListbook geometry management
149// ----------------------------------------------------------------------------
150
151wxSize wxListbook::GetListSize() const
152{
4a06b348
VZ
153 const wxSize sizeClient = GetClientSize(),
154 sizeList = m_list->GetViewRect().GetSize();
e9c0df38
VZ
155
156 wxSize size;
157 if ( IsVertical() )
158 {
159 size.x = sizeClient.x;
4a06b348 160 size.y = sizeList.y;
e9c0df38
VZ
161 }
162 else // left/right aligned
163 {
4a06b348 164 size.x = sizeList.x;
e9c0df38
VZ
165 size.y = sizeClient.y;
166 }
167
168 return size;
169}
170
171wxRect wxListbook::GetPageRect() const
172{
ae03fa2c 173 const wxSize sizeList = m_list->GetSize();
e9c0df38 174
2997ca30 175 wxPoint pt;
42841dfc 176 wxRect rectPage(pt, GetClientSize());
e9c0df38
VZ
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
203void wxListbook::OnSize(wxSizeEvent& event)
204{
f1160963
VZ
205 event.Skip();
206
207 if ( !m_list )
208 {
209 // we're not fully created yet
210 return;
211 }
212
e9c0df38
VZ
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
7a19fb6e
VZ
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();
a1f9a880
VZ
244 if ( m_list->GetPosition() != posList )
245 m_list->Move(posList.x, posList.y);
ae03fa2c 246 m_list->SetClientSize(sizeList.x, sizeList.y);
e9c0df38 247
ef0120c1 248#if wxUSE_LINE_IN_LISTBOOK
e9c0df38
VZ
249 if ( m_line )
250 {
3f0785de 251 wxRect rectLine(sizeClient);
e9c0df38
VZ
252
253 switch ( GetWindowStyle() & wxLB_ALIGN_MASK )
254 {
255 case wxLB_TOP:
256 rectLine.y = sizeList.y + 1;
257 rectLine.height = MARGIN - 2;
258 break;
259
260 case wxLB_BOTTOM:
261 rectLine.height = MARGIN - 2;
262 rectLine.y = sizeClient.y - sizeList.y - rectLine.height;
263 break;
264
265 case wxLB_LEFT:
266 rectLine.x = sizeList.x + 1;
267 rectLine.width = MARGIN - 2;
268 break;
269
270 case wxLB_RIGHT:
271 rectLine.width = MARGIN - 2;
272 rectLine.x = sizeClient.x - sizeList.x - rectLine.width;
273 break;
274 }
275
276 m_line->SetSize(rectLine);
277 }
ef0120c1 278#endif // wxUSE_LINE_IN_LISTBOOK
e9c0df38 279
33ebfc3b
VZ
280 // resize the currently shown page
281 if (m_selection != wxNOT_FOUND )
e9c0df38
VZ
282 {
283 wxWindow *page = m_pages[m_selection];
284 wxCHECK_RET( page, _T("NULL page in wxListbook?") );
e9c0df38 285 page->SetSize(GetPageRect());
7a19fb6e 286 }
e9c0df38
VZ
287}
288
289wxSize wxListbook::CalcSizeFromPage(const wxSize& sizePage) const
290{
291 // we need to add the size of the list control and the margin
292 const wxSize sizeList = GetListSize();
293
294 wxSize size = sizePage;
295 if ( IsVertical() )
296 {
297 size.y += sizeList.y + MARGIN;
298 }
299 else // left/right aligned
300 {
301 size.x += sizeList.x + MARGIN;
302 }
303
304 return size;
305}
306
307
308// ----------------------------------------------------------------------------
309// accessing the pages
310// ----------------------------------------------------------------------------
311
312bool wxListbook::SetPageText(size_t n, const wxString& strText)
313{
314 m_list->SetItemText(n, strText);
315
316 return true;
317}
318
319wxString wxListbook::GetPageText(size_t n) const
320{
321 return m_list->GetItemText(n);
322}
323
324int wxListbook::GetPageImage(size_t WXUNUSED(n)) const
325{
326 wxFAIL_MSG( _T("wxListbook::GetPageImage() not implemented") );
327
328 return -1;
329}
330
331bool wxListbook::SetPageImage(size_t n, int imageId)
332{
c3627a00 333 return m_list->SetItemImage(n, imageId);
e9c0df38
VZ
334}
335
336// ----------------------------------------------------------------------------
337// image list stuff
338// ----------------------------------------------------------------------------
339
340void wxListbook::SetImageList(wxImageList *imageList)
341{
342 m_list->SetImageList(imageList, wxIMAGE_LIST_NORMAL);
343
61c083e7 344 wxBookCtrlBase::SetImageList(imageList);
e9c0df38
VZ
345}
346
347// ----------------------------------------------------------------------------
348// selection
349// ----------------------------------------------------------------------------
350
351int wxListbook::GetSelection() const
352{
353 return m_selection;
354}
355
356int wxListbook::SetSelection(size_t n)
357{
1f30c176
WS
358 wxCHECK_MSG( IS_VALID_PAGE(n), wxNOT_FOUND,
359 wxT("invalid page index in wxListbook::SetSelection()") );
e9c0df38 360
1f30c176 361 const int oldSel = m_selection;
e9c0df38 362
1f30c176 363 if ( int(n) != m_selection )
e9c0df38 364 {
1f30c176
WS
365 wxListbookEvent event(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING, m_windowId);
366 event.SetSelection(n);
367 event.SetOldSelection(m_selection);
368 event.SetEventObject(this);
369 if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
370 {
371 if ( m_selection != wxNOT_FOUND )
372 m_pages[m_selection]->Hide();
e9c0df38 373
1f30c176
WS
374 wxWindow *page = m_pages[n];
375 page->SetSize(GetPageRect());
376 page->Show();
bb08a4a1 377
716dc245 378 // change m_selection now to ignore the selection change event
1f30c176
WS
379 m_selection = n;
380 m_list->Select(n);
381 m_list->Focus(n);
382
383 // program allows the page change
384 event.SetEventType(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED);
385 (void)GetEventHandler()->ProcessEvent(event);
386 }
e9c0df38
VZ
387 }
388
1f30c176 389 return oldSel;
e9c0df38
VZ
390}
391
e9c0df38
VZ
392// ----------------------------------------------------------------------------
393// adding/removing the pages
394// ----------------------------------------------------------------------------
395
396bool
397wxListbook::InsertPage(size_t n,
398 wxWindow *page,
399 const wxString& text,
400 bool bSelect,
401 int imageId)
402{
61c083e7 403 if ( !wxBookCtrlBase::InsertPage(n, page, text, bSelect, imageId) )
e9c0df38
VZ
404 return false;
405
406 m_list->InsertItem(n, text, imageId);
407
716dc245
WS
408 // if the inserted page is before the selected one, we must update the
409 // index of the selected page
410 if ( int(n) <= m_selection )
e9c0df38 411 {
42841dfc 412 // one extra page added
716dc245
WS
413 m_selection++;
414 m_list->Select(m_selection);
415 m_list->Focus(m_selection);
e9c0df38 416 }
716dc245
WS
417
418 // some page should be selected: either this one or the first one if there
419 // is still no selection
420 int selNew = -1;
421 if ( bSelect )
422 selNew = n;
423 else if ( m_selection == -1 )
424 selNew = 0;
425
426 if ( selNew != m_selection )
e9c0df38 427 page->Hide();
716dc245
WS
428
429 if ( selNew != -1 )
430 SetSelection(selNew);
e9c0df38 431
37144cf0 432 InvalidateBestSize();
e9c0df38
VZ
433 return true;
434}
435
436wxWindow *wxListbook::DoRemovePage(size_t page)
437{
33ebfc3b 438 const int page_count = GetPageCount();
61c083e7 439 wxWindow *win = wxBookCtrlBase::DoRemovePage(page);
bb08a4a1 440
e9c0df38
VZ
441 if ( win )
442 {
443 m_list->DeleteItem(page);
33ebfc3b
VZ
444
445 if (m_selection >= (int)page)
446 {
447 // force new sel valid if possible
448 int sel = m_selection - 1;
449 if (page_count == 1)
bb08a4a1 450 sel = wxNOT_FOUND;
33ebfc3b
VZ
451 else if ((page_count == 2) || (sel == -1))
452 sel = 0;
bb08a4a1 453
33ebfc3b 454 // force sel invalid if deleting current page - don't try to hide it
bb08a4a1
WS
455 m_selection = (m_selection == (int)page) ? wxNOT_FOUND : m_selection - 1;
456
457 if ((sel != wxNOT_FOUND) && (sel != m_selection))
33ebfc3b
VZ
458 SetSelection(sel);
459 }
e9c0df38
VZ
460 }
461
462 return win;
463}
464
fbd11d30
RD
465
466bool wxListbook::DeleteAllPages()
467{
468 m_list->DeleteAllItems();
61c083e7 469 return wxBookCtrlBase::DeleteAllPages();
fbd11d30
RD
470}
471
e9c0df38
VZ
472// ----------------------------------------------------------------------------
473// wxListbook events
474// ----------------------------------------------------------------------------
475
476void wxListbook::OnListSelected(wxListEvent& eventList)
477{
478 const int selNew = eventList.GetIndex();
479
480 if ( selNew == m_selection )
481 {
482 // this event can only come from our own Select(m_selection) below
483 // which we call when the page change is vetoed, so we should simply
484 // ignore it
485 return;
486 }
487
716dc245 488 SetSelection(selNew);
e9c0df38 489
716dc245
WS
490 // change wasn't allowed, return to previous state
491 if (m_selection != selNew)
e9c0df38
VZ
492 {
493 m_list->Select(m_selection);
716dc245 494 m_list->Focus(m_selection);
e9c0df38 495 }
e9c0df38
VZ
496}
497
498#endif // wxUSE_LISTBOOK
499