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
= m_list
->GetSize();
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
->Move(posList
.x
, posList
.y
);
213 m_list
->SetClientSize(sizeList
.x
, sizeList
.y
);
217 wxRect
rectLine(wxPoint(0, 0), sizeClient
);
219 switch ( GetWindowStyle() & wxLB_ALIGN_MASK
)
222 rectLine
.y
= sizeList
.y
+ 1;
223 rectLine
.height
= MARGIN
- 2;
227 rectLine
.height
= MARGIN
- 2;
228 rectLine
.y
= sizeClient
.y
- sizeList
.y
- rectLine
.height
;
232 rectLine
.x
= sizeList
.x
+ 1;
233 rectLine
.width
= MARGIN
- 2;
237 rectLine
.width
= MARGIN
- 2;
238 rectLine
.x
= sizeClient
.x
- sizeList
.x
- rectLine
.width
;
242 m_line
->SetSize(rectLine
);
245 // we should always have some selection if possible
246 if ( m_selection
== wxNOT_FOUND
&& GetPageCount() )
251 if ( m_selection
!= wxNOT_FOUND
)
253 wxWindow
*page
= m_pages
[m_selection
];
254 wxCHECK_RET( page
, _T("NULL page in wxListbook?") );
256 page
->SetSize(GetPageRect());
257 if ( !page
->IsShown() )
264 wxSize
wxListbook::CalcSizeFromPage(const wxSize
& sizePage
) const
266 // we need to add the size of the list control and the margin
267 const wxSize sizeList
= GetListSize();
269 wxSize size
= sizePage
;
272 size
.y
+= sizeList
.y
+ MARGIN
;
274 else // left/right aligned
276 size
.x
+= sizeList
.x
+ MARGIN
;
283 // ----------------------------------------------------------------------------
284 // accessing the pages
285 // ----------------------------------------------------------------------------
287 bool wxListbook::SetPageText(size_t n
, const wxString
& strText
)
289 m_list
->SetItemText(n
, strText
);
294 wxString
wxListbook::GetPageText(size_t n
) const
296 return m_list
->GetItemText(n
);
299 int wxListbook::GetPageImage(size_t WXUNUSED(n
)) const
301 wxFAIL_MSG( _T("wxListbook::GetPageImage() not implemented") );
306 bool wxListbook::SetPageImage(size_t n
, int imageId
)
308 return m_list
->SetItemImage(n
, imageId
, imageId
);
311 // ----------------------------------------------------------------------------
313 // ----------------------------------------------------------------------------
315 void wxListbook::SetImageList(wxImageList
*imageList
)
317 m_list
->SetImageList(imageList
, wxIMAGE_LIST_NORMAL
);
319 wxBookCtrl::SetImageList(imageList
);
322 // ----------------------------------------------------------------------------
324 // ----------------------------------------------------------------------------
326 int wxListbook::GetSelection() const
331 int wxListbook::SetSelection(size_t n
)
333 wxCHECK_MSG( n
< GetPageCount(), wxNOT_FOUND
,
334 _T("invalid page index in wxListbook::SetSelection()") );
336 int selOld
= m_selection
;
338 if ( (int)n
!= m_selection
)
342 m_list
->Select(m_selection
);
343 m_list
->Focus(m_selection
);
350 // ----------------------------------------------------------------------------
351 // adding/removing the pages
352 // ----------------------------------------------------------------------------
355 wxListbook::InsertPage(size_t n
,
357 const wxString
& text
,
361 if ( !wxBookCtrl::InsertPage(n
, page
, text
, bSelect
, imageId
) )
364 m_list
->InsertItem(n
, text
, imageId
);
371 else // don't select this page
373 // it will be shown only when selected
380 wxWindow
*wxListbook::DoRemovePage(size_t page
)
382 wxWindow
*win
= wxBookCtrl::DoRemovePage(page
);
385 m_list
->DeleteItem(page
);
391 // ----------------------------------------------------------------------------
393 // ----------------------------------------------------------------------------
395 void wxListbook::OnListSelected(wxListEvent
& eventList
)
397 const int selNew
= eventList
.GetIndex();
399 if ( selNew
== m_selection
)
401 // this event can only come from our own Select(m_selection) below
402 // which we call when the page change is vetoed, so we should simply
407 // first send "change in progress" event which may be vetoed by user
408 wxListbookEvent
eventIng(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING
, GetId());
410 eventIng
.SetEventObject(this);
411 eventIng
.SetSelection(selNew
);
412 eventIng
.SetOldSelection(m_selection
);
413 if ( GetEventHandler()->ProcessEvent(eventIng
) && !eventIng
.IsAllowed() )
415 m_list
->Select(m_selection
);
419 // change allowed: do change the page and notify the user about it
420 if ( m_selection
!= wxNOT_FOUND
)
421 m_pages
[m_selection
]->Hide();
422 wxWindow
*page
= m_pages
[m_selection
= selNew
];
423 page
->SetSize(GetPageRect());
426 wxListbookEvent
eventEd(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED
, GetId());
428 eventEd
.SetEventObject(this);
429 eventEd
.SetSelection(selNew
);
430 eventEd
.SetOldSelection(m_selection
);
432 (void)GetEventHandler()->ProcessEvent(eventEd
);
435 #endif // wxUSE_LISTBOOK