1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: generic/listbkg.cpp
3 // Purpose: generic implementation of wxListbook
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "listbook.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
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"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 // margin between the list and the page, should be bigger than wxStaticLine
45 const wxCoord MARGIN
= 5;
47 // ----------------------------------------------------------------------------
48 // various wxWindows macros
49 // ----------------------------------------------------------------------------
51 IMPLEMENT_DYNAMIC_CLASS(wxListbook
, wxControl
)
52 IMPLEMENT_DYNAMIC_CLASS(wxListbookEvent
, wxNotifyEvent
)
54 const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING
= wxNewEventType();
55 const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED
= wxNewEventType();
56 const int wxID_LISTBOOKLISTVIEW
= wxNewId();
58 BEGIN_EVENT_TABLE(wxListbook
, wxBookCtrl
)
59 EVT_SIZE(wxListbook::OnSize
)
61 EVT_LIST_ITEM_SELECTED(wxID_LISTBOOKLISTVIEW
, wxListbook::OnListSelected
)
64 // ============================================================================
65 // wxListbook implementation
66 // ============================================================================
68 // ----------------------------------------------------------------------------
69 // wxListbook creation
70 // ----------------------------------------------------------------------------
72 void wxListbook::Init()
76 m_selection
= wxNOT_FOUND
;
80 wxListbook::Create(wxWindow
*parent
,
87 if ( (style
& wxLB_ALIGN_MASK
) == wxLB_DEFAULT
)
93 #endif // __WXMAC__/!__WXMAC__
96 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
97 wxDefaultValidator
, name
) )
100 m_list
= new wxListView
103 wxID_LISTBOOKLISTVIEW
,
106 wxLC_ICON
| wxLC_SINGLE_SEL
109 m_line
= new wxStaticLine
115 IsVertical() ? wxLI_HORIZONTAL
: wxLI_VERTICAL
121 // ----------------------------------------------------------------------------
122 // wxListbook geometry management
123 // ----------------------------------------------------------------------------
125 wxSize
wxListbook::GetListSize() const
127 const wxSize sizeClient
= GetClientSize();
129 // we need to find the longest/tallest label
130 wxCoord widthMax
= 0,
132 const int count
= m_list
->GetItemCount();
135 for ( int i
= 0; i
< count
; i
++ )
138 m_list
->GetItemRect(i
, r
);
153 size
.x
= sizeClient
.x
;
156 if ( widthMax
>= sizeClient
.x
)
158 // account for the scrollbar
159 size
.y
+= wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
162 else // left/right aligned
164 // +20 is due to an apparent bug in wxListCtrl::GetItemRect() but I
165 // can't fix it there right now so just add a fudge here...
166 size
.x
= widthMax
+ 20;
167 size
.y
= sizeClient
.y
;
169 if ( heightMax
>= sizeClient
.y
)
171 // account for the scrollbar
172 size
.x
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
179 wxRect
wxListbook::GetPageRect() const
181 const wxSize sizeList
= GetListSize();
183 wxRect
rectPage(wxPoint(0, 0), GetClientSize());
184 switch ( GetWindowStyle() & wxLB_ALIGN_MASK
)
187 wxFAIL_MSG( _T("unexpected wxListbook alignment") );
191 rectPage
.y
= sizeList
.y
+ MARGIN
;
195 rectPage
.height
-= sizeList
.y
+ MARGIN
;
199 rectPage
.x
= sizeList
.x
+ MARGIN
;
203 rectPage
.width
-= sizeList
.x
+ MARGIN
;
210 void wxListbook::OnSize(wxSizeEvent
& event
)
216 // we're not fully created yet
220 // resize the list control and the page area to fit inside our new size
221 const wxSize sizeClient
= GetClientSize(),
222 sizeList
= GetListSize();
225 switch ( GetWindowStyle() & wxLB_ALIGN_MASK
)
228 wxFAIL_MSG( _T("unexpected wxListbook alignment") );
233 // posList is already ok
237 posList
.y
= sizeClient
.y
- sizeList
.y
;
241 posList
.x
= sizeClient
.x
- sizeList
.x
;
245 m_list
->SetSize(posList
.x
, posList
.y
, sizeList
.x
, sizeList
.y
);
249 wxRect
rectLine(wxPoint(0, 0), sizeClient
);
251 switch ( GetWindowStyle() & wxLB_ALIGN_MASK
)
254 rectLine
.y
= sizeList
.y
+ 1;
255 rectLine
.height
= MARGIN
- 2;
259 rectLine
.height
= MARGIN
- 2;
260 rectLine
.y
= sizeClient
.y
- sizeList
.y
- rectLine
.height
;
264 rectLine
.x
= sizeList
.x
+ 1;
265 rectLine
.width
= MARGIN
- 2;
269 rectLine
.width
= MARGIN
- 2;
270 rectLine
.x
= sizeClient
.x
- sizeList
.x
- rectLine
.width
;
274 m_line
->SetSize(rectLine
);
277 // we should always have some selection if possible
278 if ( m_selection
== wxNOT_FOUND
&& GetPageCount() )
283 if ( m_selection
!= wxNOT_FOUND
)
285 wxWindow
*page
= m_pages
[m_selection
];
286 wxCHECK_RET( page
, _T("NULL page in wxListbook?") );
288 page
->SetSize(GetPageRect());
289 if ( !page
->IsShown() )
296 wxSize
wxListbook::CalcSizeFromPage(const wxSize
& sizePage
) const
298 // we need to add the size of the list control and the margin
299 const wxSize sizeList
= GetListSize();
301 wxSize size
= sizePage
;
304 size
.y
+= sizeList
.y
+ MARGIN
;
306 else // left/right aligned
308 size
.x
+= sizeList
.x
+ MARGIN
;
315 // ----------------------------------------------------------------------------
316 // accessing the pages
317 // ----------------------------------------------------------------------------
319 bool wxListbook::SetPageText(size_t n
, const wxString
& strText
)
321 m_list
->SetItemText(n
, strText
);
326 wxString
wxListbook::GetPageText(size_t n
) const
328 return m_list
->GetItemText(n
);
331 int wxListbook::GetPageImage(size_t WXUNUSED(n
)) const
333 wxFAIL_MSG( _T("wxListbook::GetPageImage() not implemented") );
338 bool wxListbook::SetPageImage(size_t n
, int imageId
)
340 return m_list
->SetItemImage(n
, imageId
, imageId
);
343 // ----------------------------------------------------------------------------
345 // ----------------------------------------------------------------------------
347 void wxListbook::SetImageList(wxImageList
*imageList
)
349 m_list
->SetImageList(imageList
, wxIMAGE_LIST_NORMAL
);
351 wxBookCtrl::SetImageList(imageList
);
354 // ----------------------------------------------------------------------------
356 // ----------------------------------------------------------------------------
358 int wxListbook::GetSelection() const
363 int wxListbook::SetSelection(size_t n
)
365 wxCHECK_MSG( n
< GetPageCount(), wxNOT_FOUND
,
366 _T("invalid page index in wxListbook::SetSelection()") );
368 int selOld
= m_selection
;
370 if ( (int)n
!= m_selection
)
374 m_list
->Select(m_selection
);
375 m_list
->Focus(m_selection
);
382 // ----------------------------------------------------------------------------
383 // adding/removing the pages
384 // ----------------------------------------------------------------------------
387 wxListbook::InsertPage(size_t n
,
389 const wxString
& text
,
393 if ( !wxBookCtrl::InsertPage(n
, page
, text
, bSelect
, imageId
) )
396 m_list
->InsertItem(n
, text
, imageId
);
403 else // don't select this page
405 // it will be shown only when selected
412 wxWindow
*wxListbook::DoRemovePage(size_t page
)
414 wxWindow
*win
= wxBookCtrl::DoRemovePage(page
);
417 m_list
->DeleteItem(page
);
423 // ----------------------------------------------------------------------------
425 // ----------------------------------------------------------------------------
427 void wxListbook::OnListSelected(wxListEvent
& eventList
)
429 const int selNew
= eventList
.GetIndex();
431 if ( selNew
== m_selection
)
433 // this event can only come from our own Select(m_selection) below
434 // which we call when the page change is vetoed, so we should simply
439 // first send "change in progress" event which may be vetoed by user
440 wxListbookEvent
eventIng(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING
, GetId());
442 eventIng
.SetEventObject(this);
443 eventIng
.SetSelection(selNew
);
444 eventIng
.SetOldSelection(m_selection
);
445 if ( GetEventHandler()->ProcessEvent(eventIng
) && !eventIng
.IsAllowed() )
447 m_list
->Select(m_selection
);
451 // change allowed: do change the page and notify the user about it
452 if ( m_selection
!= wxNOT_FOUND
)
453 m_pages
[m_selection
]->Hide();
454 wxWindow
*page
= m_pages
[m_selection
= selNew
];
455 page
->SetSize(GetPageRect());
458 wxListbookEvent
eventEd(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED
, GetId());
460 eventEd
.SetEventObject(this);
461 eventEd
.SetSelection(selNew
);
462 eventEd
.SetOldSelection(m_selection
);
464 (void)GetEventHandler()->ProcessEvent(eventEd
);
467 #endif // wxUSE_LISTBOOK