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()
75 #if wxUSE_LINE_IN_LISTBOOK
77 #endif // wxUSE_LINE_IN_LISTBOOK
78 m_selection
= wxNOT_FOUND
;
82 wxListbook::Create(wxWindow
*parent
,
89 if ( (style
& wxLB_ALIGN_MASK
) == wxLB_DEFAULT
)
95 #endif // __WXMAC__/!__WXMAC__
98 // no border for this control, it doesn't look nice together with
100 style
&= ~wxBORDER_MASK
;
101 style
|= wxBORDER_NONE
;
103 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
104 wxDefaultValidator
, name
) )
107 m_list
= new wxListView
110 wxID_LISTBOOKLISTVIEW
,
113 wxLC_ICON
| wxLC_SINGLE_SEL
|
114 (IsVertical() ? wxLC_ALIGN_LEFT
: wxLC_ALIGN_TOP
)
117 #if wxUSE_LINE_IN_LISTBOOK
118 m_line
= new wxStaticLine
124 IsVertical() ? wxLI_HORIZONTAL
: wxLI_VERTICAL
126 #endif // wxUSE_LINE_IN_LISTBOOK
131 // ----------------------------------------------------------------------------
132 // wxListbook geometry management
133 // ----------------------------------------------------------------------------
135 wxSize
wxListbook::GetListSize() const
137 const wxSize sizeClient
= GetClientSize(),
138 sizeList
= m_list
->GetViewRect().GetSize();
143 size
.x
= sizeClient
.x
;
146 else // left/right aligned
149 size
.y
= sizeClient
.y
;
155 wxRect
wxListbook::GetPageRect() const
157 const wxSize sizeList
= m_list
->GetSize();
159 wxRect
rectPage(wxPoint(0, 0), GetClientSize());
160 switch ( GetWindowStyle() & wxLB_ALIGN_MASK
)
163 wxFAIL_MSG( _T("unexpected wxListbook alignment") );
167 rectPage
.y
= sizeList
.y
+ MARGIN
;
171 rectPage
.height
-= sizeList
.y
+ MARGIN
;
175 rectPage
.x
= sizeList
.x
+ MARGIN
;
179 rectPage
.width
-= sizeList
.x
+ MARGIN
;
186 void wxListbook::OnSize(wxSizeEvent
& event
)
192 // we're not fully created yet
196 // resize the list control and the page area to fit inside our new size
197 const wxSize sizeClient
= GetClientSize(),
198 sizeList
= GetListSize();
201 switch ( GetWindowStyle() & wxLB_ALIGN_MASK
)
204 wxFAIL_MSG( _T("unexpected wxListbook alignment") );
209 // posList is already ok
213 posList
.y
= sizeClient
.y
- sizeList
.y
;
217 posList
.x
= sizeClient
.x
- sizeList
.x
;
221 m_list
->Move(posList
.x
, posList
.y
);
222 m_list
->SetClientSize(sizeList
.x
, sizeList
.y
);
224 #if wxUSE_LINE_IN_LISTBOOK
227 wxRect
rectLine(wxPoint(0, 0), sizeClient
);
229 switch ( GetWindowStyle() & wxLB_ALIGN_MASK
)
232 rectLine
.y
= sizeList
.y
+ 1;
233 rectLine
.height
= MARGIN
- 2;
237 rectLine
.height
= MARGIN
- 2;
238 rectLine
.y
= sizeClient
.y
- sizeList
.y
- rectLine
.height
;
242 rectLine
.x
= sizeList
.x
+ 1;
243 rectLine
.width
= MARGIN
- 2;
247 rectLine
.width
= MARGIN
- 2;
248 rectLine
.x
= sizeClient
.x
- sizeList
.x
- rectLine
.width
;
252 m_line
->SetSize(rectLine
);
254 #endif // wxUSE_LINE_IN_LISTBOOK
256 // we should always have some selection if possible
257 if ( m_selection
== wxNOT_FOUND
&& GetPageCount() )
262 if ( m_selection
!= wxNOT_FOUND
)
264 wxWindow
*page
= m_pages
[m_selection
];
265 wxCHECK_RET( page
, _T("NULL page in wxListbook?") );
267 page
->SetSize(GetPageRect());
268 if ( !page
->IsShown() )
275 wxSize
wxListbook::CalcSizeFromPage(const wxSize
& sizePage
) const
277 // we need to add the size of the list control and the margin
278 const wxSize sizeList
= GetListSize();
280 wxSize size
= sizePage
;
283 size
.y
+= sizeList
.y
+ MARGIN
;
285 else // left/right aligned
287 size
.x
+= sizeList
.x
+ MARGIN
;
294 // ----------------------------------------------------------------------------
295 // accessing the pages
296 // ----------------------------------------------------------------------------
298 bool wxListbook::SetPageText(size_t n
, const wxString
& strText
)
300 m_list
->SetItemText(n
, strText
);
305 wxString
wxListbook::GetPageText(size_t n
) const
307 return m_list
->GetItemText(n
);
310 int wxListbook::GetPageImage(size_t WXUNUSED(n
)) const
312 wxFAIL_MSG( _T("wxListbook::GetPageImage() not implemented") );
317 bool wxListbook::SetPageImage(size_t n
, int imageId
)
319 return m_list
->SetItemImage(n
, imageId
, imageId
);
322 // ----------------------------------------------------------------------------
324 // ----------------------------------------------------------------------------
326 void wxListbook::SetImageList(wxImageList
*imageList
)
328 m_list
->SetImageList(imageList
, wxIMAGE_LIST_NORMAL
);
330 wxBookCtrl::SetImageList(imageList
);
333 // ----------------------------------------------------------------------------
335 // ----------------------------------------------------------------------------
337 int wxListbook::GetSelection() const
342 int wxListbook::SetSelection(size_t n
)
344 wxCHECK_MSG( n
< GetPageCount(), wxNOT_FOUND
,
345 _T("invalid page index in wxListbook::SetSelection()") );
347 int selOld
= m_selection
;
349 if ( (int)n
!= m_selection
)
353 m_list
->Select(m_selection
);
354 m_list
->Focus(m_selection
);
361 // ----------------------------------------------------------------------------
362 // adding/removing the pages
363 // ----------------------------------------------------------------------------
366 wxListbook::InsertPage(size_t n
,
368 const wxString
& text
,
372 if ( !wxBookCtrl::InsertPage(n
, page
, text
, bSelect
, imageId
) )
375 m_list
->InsertItem(n
, text
, imageId
);
382 else // don't select this page
384 // it will be shown only when selected
391 wxWindow
*wxListbook::DoRemovePage(size_t page
)
393 wxWindow
*win
= wxBookCtrl::DoRemovePage(page
);
396 m_list
->DeleteItem(page
);
402 // ----------------------------------------------------------------------------
404 // ----------------------------------------------------------------------------
406 void wxListbook::OnListSelected(wxListEvent
& eventList
)
408 const int selNew
= eventList
.GetIndex();
410 if ( selNew
== m_selection
)
412 // this event can only come from our own Select(m_selection) below
413 // which we call when the page change is vetoed, so we should simply
418 // first send "change in progress" event which may be vetoed by user
419 wxListbookEvent
eventIng(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING
, GetId());
421 eventIng
.SetEventObject(this);
422 eventIng
.SetSelection(selNew
);
423 eventIng
.SetOldSelection(m_selection
);
424 if ( GetEventHandler()->ProcessEvent(eventIng
) && !eventIng
.IsAllowed() )
426 m_list
->Select(m_selection
);
430 // change allowed: do change the page and notify the user about it
431 if ( m_selection
!= wxNOT_FOUND
)
432 m_pages
[m_selection
]->Hide();
433 wxWindow
*page
= m_pages
[m_selection
= selNew
];
434 page
->SetSize(GetPageRect());
437 wxListbookEvent
eventEd(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED
, GetId());
439 eventEd
.SetEventObject(this);
440 eventEd
.SetSelection(selNew
);
441 eventEd
.SetOldSelection(m_selection
);
443 (void)GetEventHandler()->ProcessEvent(eventEd
);
446 #endif // wxUSE_LISTBOOK