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 wxBORDER_NONE
| wxLC_ICON
| wxLC_SINGLE_SEL
|
107 (IsVertical() ? wxLC_ALIGN_LEFT
: wxLC_ALIGN_TOP
)
110 m_line
= new wxStaticLine
116 IsVertical() ? wxLI_HORIZONTAL
: wxLI_VERTICAL
122 // ----------------------------------------------------------------------------
123 // wxListbook geometry management
124 // ----------------------------------------------------------------------------
126 wxSize
wxListbook::GetListSize() const
128 const wxSize sizeClient
= GetClientSize(),
129 sizeList
= m_list
->GetViewRect().GetSize();
134 size
.x
= sizeClient
.x
;
137 else // left/right aligned
140 size
.y
= sizeClient
.y
;
146 wxRect
wxListbook::GetPageRect() const
148 const wxSize sizeList
= GetListSize();
150 wxRect
rectPage(wxPoint(0, 0), GetClientSize());
151 switch ( GetWindowStyle() & wxLB_ALIGN_MASK
)
154 wxFAIL_MSG( _T("unexpected wxListbook alignment") );
158 rectPage
.y
= sizeList
.y
+ MARGIN
;
162 rectPage
.height
-= sizeList
.y
+ MARGIN
;
166 rectPage
.x
= sizeList
.x
+ MARGIN
;
170 rectPage
.width
-= sizeList
.x
+ MARGIN
;
177 void wxListbook::OnSize(wxSizeEvent
& event
)
183 // we're not fully created yet
187 // resize the list control and the page area to fit inside our new size
188 const wxSize sizeClient
= GetClientSize(),
189 sizeList
= GetListSize();
192 switch ( GetWindowStyle() & wxLB_ALIGN_MASK
)
195 wxFAIL_MSG( _T("unexpected wxListbook alignment") );
200 // posList is already ok
204 posList
.y
= sizeClient
.y
- sizeList
.y
;
208 posList
.x
= sizeClient
.x
- sizeList
.x
;
212 m_list
->SetSize(posList
.x
, posList
.y
, sizeList
.x
, sizeList
.y
);
216 wxRect
rectLine(wxPoint(0, 0), sizeClient
);
218 switch ( GetWindowStyle() & wxLB_ALIGN_MASK
)
221 rectLine
.y
= sizeList
.y
+ 1;
222 rectLine
.height
= MARGIN
- 2;
226 rectLine
.height
= MARGIN
- 2;
227 rectLine
.y
= sizeClient
.y
- sizeList
.y
- rectLine
.height
;
231 rectLine
.x
= sizeList
.x
+ 1;
232 rectLine
.width
= MARGIN
- 2;
236 rectLine
.width
= MARGIN
- 2;
237 rectLine
.x
= sizeClient
.x
- sizeList
.x
- rectLine
.width
;
241 m_line
->SetSize(rectLine
);
244 // we should always have some selection if possible
245 if ( m_selection
== wxNOT_FOUND
&& GetPageCount() )
250 if ( m_selection
!= wxNOT_FOUND
)
252 wxWindow
*page
= m_pages
[m_selection
];
253 wxCHECK_RET( page
, _T("NULL page in wxListbook?") );
255 page
->SetSize(GetPageRect());
256 if ( !page
->IsShown() )
263 wxSize
wxListbook::CalcSizeFromPage(const wxSize
& sizePage
) const
265 // we need to add the size of the list control and the margin
266 const wxSize sizeList
= GetListSize();
268 wxSize size
= sizePage
;
271 size
.y
+= sizeList
.y
+ MARGIN
;
273 else // left/right aligned
275 size
.x
+= sizeList
.x
+ MARGIN
;
282 // ----------------------------------------------------------------------------
283 // accessing the pages
284 // ----------------------------------------------------------------------------
286 bool wxListbook::SetPageText(size_t n
, const wxString
& strText
)
288 m_list
->SetItemText(n
, strText
);
293 wxString
wxListbook::GetPageText(size_t n
) const
295 return m_list
->GetItemText(n
);
298 int wxListbook::GetPageImage(size_t WXUNUSED(n
)) const
300 wxFAIL_MSG( _T("wxListbook::GetPageImage() not implemented") );
305 bool wxListbook::SetPageImage(size_t n
, int imageId
)
307 return m_list
->SetItemImage(n
, imageId
, imageId
);
310 // ----------------------------------------------------------------------------
312 // ----------------------------------------------------------------------------
314 void wxListbook::SetImageList(wxImageList
*imageList
)
316 m_list
->SetImageList(imageList
, wxIMAGE_LIST_NORMAL
);
318 wxBookCtrl::SetImageList(imageList
);
321 // ----------------------------------------------------------------------------
323 // ----------------------------------------------------------------------------
325 int wxListbook::GetSelection() const
330 int wxListbook::SetSelection(size_t n
)
332 wxCHECK_MSG( n
< GetPageCount(), wxNOT_FOUND
,
333 _T("invalid page index in wxListbook::SetSelection()") );
335 int selOld
= m_selection
;
337 if ( (int)n
!= m_selection
)
341 m_list
->Select(m_selection
);
342 m_list
->Focus(m_selection
);
349 // ----------------------------------------------------------------------------
350 // adding/removing the pages
351 // ----------------------------------------------------------------------------
354 wxListbook::InsertPage(size_t n
,
356 const wxString
& text
,
360 if ( !wxBookCtrl::InsertPage(n
, page
, text
, bSelect
, imageId
) )
363 m_list
->InsertItem(n
, text
, imageId
);
370 else // don't select this page
372 // it will be shown only when selected
379 wxWindow
*wxListbook::DoRemovePage(size_t page
)
381 wxWindow
*win
= wxBookCtrl::DoRemovePage(page
);
384 m_list
->DeleteItem(page
);
390 // ----------------------------------------------------------------------------
392 // ----------------------------------------------------------------------------
394 void wxListbook::OnListSelected(wxListEvent
& eventList
)
396 const int selNew
= eventList
.GetIndex();
398 if ( selNew
== m_selection
)
400 // this event can only come from our own Select(m_selection) below
401 // which we call when the page change is vetoed, so we should simply
406 // first send "change in progress" event which may be vetoed by user
407 wxListbookEvent
eventIng(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING
, GetId());
409 eventIng
.SetEventObject(this);
410 eventIng
.SetSelection(selNew
);
411 eventIng
.SetOldSelection(m_selection
);
412 if ( GetEventHandler()->ProcessEvent(eventIng
) && !eventIng
.IsAllowed() )
414 m_list
->Select(m_selection
);
418 // change allowed: do change the page and notify the user about it
419 if ( m_selection
!= wxNOT_FOUND
)
420 m_pages
[m_selection
]->Hide();
421 wxWindow
*page
= m_pages
[m_selection
= selNew
];
422 page
->SetSize(GetPageRect());
425 wxListbookEvent
eventEd(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED
, GetId());
427 eventEd
.SetEventObject(this);
428 eventEd
.SetSelection(selNew
);
429 eventEd
.SetOldSelection(m_selection
);
431 (void)GetEventHandler()->ProcessEvent(eventEd
);
434 #endif // wxUSE_LISTBOOK