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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/listctrl.h"
30 #include "wx/statline.h"
31 #include "wx/listbook.h"
32 #include "wx/imaglist.h"
33 #include "wx/settings.h"
35 // ----------------------------------------------------------------------------
36 // various wxWidgets macros
37 // ----------------------------------------------------------------------------
39 // check that the page index is valid
40 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 IMPLEMENT_DYNAMIC_CLASS(wxListbook
, wxControl
)
47 IMPLEMENT_DYNAMIC_CLASS(wxListbookEvent
, wxNotifyEvent
)
49 const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING
= wxNewEventType();
50 const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED
= wxNewEventType();
51 const int wxID_LISTBOOKLISTVIEW
= wxNewId();
53 BEGIN_EVENT_TABLE(wxListbook
, wxBookCtrlBase
)
54 EVT_SIZE(wxListbook::OnSize
)
55 EVT_LIST_ITEM_SELECTED(wxID_LISTBOOKLISTVIEW
, wxListbook::OnListSelected
)
58 // ============================================================================
59 // wxListbook implementation
60 // ============================================================================
62 // ----------------------------------------------------------------------------
63 // wxListbook creation
64 // ----------------------------------------------------------------------------
66 void wxListbook::Init()
69 #if wxUSE_LINE_IN_LISTBOOK
71 #endif // wxUSE_LINE_IN_LISTBOOK
72 m_selection
= wxNOT_FOUND
;
76 wxListbook::Create(wxWindow
*parent
,
83 if ( (style
& wxLB_ALIGN_MASK
) == wxLB_DEFAULT
)
89 #endif // __WXMAC__/!__WXMAC__
92 // no border for this control, it doesn't look nice together with
94 style
&= ~wxBORDER_MASK
;
95 style
|= wxBORDER_NONE
;
97 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
98 wxDefaultValidator
, name
) )
101 m_list
= new wxListView
104 wxID_LISTBOOKLISTVIEW
,
107 wxLC_ICON
| wxLC_SINGLE_SEL
|
108 (IsVertical() ? wxLC_ALIGN_LEFT
: wxLC_ALIGN_TOP
)
111 #if wxUSE_LINE_IN_LISTBOOK
112 m_line
= new wxStaticLine
118 IsVertical() ? wxLI_HORIZONTAL
: wxLI_VERTICAL
120 #endif // wxUSE_LINE_IN_LISTBOOK
123 // On XP with themes enabled the GetViewRect used in GetListSize to
124 // determine the space needed for the list view will incorrectly return
125 // (0,0,0,0) the first time. So send a pending event so OnSize will be
126 // called again after the window is ready to go. Technically we don't
127 // need to do this on non-XP windows, but if things are already sized
128 // correctly then nothing changes and so there is no harm.
130 GetEventHandler()->AddPendingEvent(evt
);
135 // ----------------------------------------------------------------------------
136 // wxListbook geometry management
137 // ----------------------------------------------------------------------------
139 wxSize
wxListbook::GetListSize() const
141 const wxSize sizeClient
= GetClientSize(),
142 sizeBorder
= m_list
->GetSize() - m_list
->GetClientSize(),
143 sizeList
= m_list
->GetViewRect().GetSize() + sizeBorder
;
148 size
.x
= sizeClient
.x
;
151 else // left/right aligned
154 size
.y
= sizeClient
.y
;
160 wxRect
wxListbook::GetPageRect() const
162 const wxSize sizeList
= m_list
->GetSize();
165 wxRect
rectPage(pt
, GetClientSize());
166 switch ( GetWindowStyle() & wxLB_ALIGN_MASK
)
169 wxFAIL_MSG( _T("unexpected wxListbook alignment") );
173 rectPage
.y
= sizeList
.y
+ GetInternalBorder();
177 rectPage
.height
-= sizeList
.y
+ GetInternalBorder();
181 rectPage
.x
= sizeList
.x
+ GetInternalBorder();
185 rectPage
.width
-= sizeList
.x
+ GetInternalBorder();
192 void wxListbook::OnSize(wxSizeEvent
& event
)
198 // we're not fully created yet
202 // arrange the icons before calling SetClientSize(), otherwise it wouldn't
203 // account for the scrollbars the list control might need and, at least
204 // under MSW, we'd finish with an ugly looking list control with both
205 // vertical and horizontal scrollbar (with one of them being added because
206 // the other one is not accounted for in client size computations)
209 // resize the list control and the page area to fit inside our new size
210 const wxSize sizeClient
= GetClientSize(),
211 sizeBorder
= m_list
->GetSize() - m_list
->GetClientSize(),
212 sizeList
= GetListSize();
214 m_list
->SetClientSize( sizeList
.x
- sizeBorder
.x
, sizeList
.y
- sizeBorder
.y
);
216 const wxSize sizeNew
= m_list
->GetSize();
218 switch ( GetWindowStyle() & wxLB_ALIGN_MASK
)
221 wxFAIL_MSG( _T("unexpected wxListbook alignment") );
226 // posList is already ok
230 posList
.y
= sizeClient
.y
- sizeNew
.y
;
234 posList
.x
= sizeClient
.x
- sizeNew
.x
;
238 if ( m_list
->GetPosition() != posList
)
239 m_list
->Move(posList
);
241 #if wxUSE_LINE_IN_LISTBOOK
244 wxRect
rectLine(sizeClient
);
246 switch ( GetWindowStyle() & wxLB_ALIGN_MASK
)
249 rectLine
.y
= sizeNew
.y
+ 1;
250 rectLine
.height
= GetInternalBorder() - 2;
254 rectLine
.height
= GetInternalBorder() - 2;
255 rectLine
.y
= sizeClient
.y
- sizeNew
.y
- rectLine
.height
;
259 rectLine
.x
= sizeNew
.x
+ 1;
260 rectLine
.width
= GetInternalBorder() - 2;
264 rectLine
.width
= GetInternalBorder() - 2;
265 rectLine
.x
= sizeClient
.x
- sizeNew
.x
- rectLine
.width
;
269 m_line
->SetSize(rectLine
);
271 #endif // wxUSE_LINE_IN_LISTBOOK
273 // resize the currently shown page
274 if (m_selection
!= wxNOT_FOUND
)
276 wxWindow
*page
= m_pages
[m_selection
];
277 wxCHECK_RET( page
, _T("NULL page in wxListbook?") );
278 page
->SetSize(GetPageRect());
282 wxSize
wxListbook::CalcSizeFromPage(const wxSize
& sizePage
) const
284 // we need to add the size of the list control and the border between
285 const wxSize sizeList
= GetListSize();
287 wxSize size
= sizePage
;
290 size
.y
+= sizeList
.y
+ GetInternalBorder();
292 else // left/right aligned
294 size
.x
+= sizeList
.x
+ GetInternalBorder();
301 // ----------------------------------------------------------------------------
302 // accessing the pages
303 // ----------------------------------------------------------------------------
305 bool wxListbook::SetPageText(size_t n
, const wxString
& strText
)
307 m_list
->SetItemText(n
, strText
);
312 wxString
wxListbook::GetPageText(size_t n
) const
314 return m_list
->GetItemText(n
);
317 int wxListbook::GetPageImage(size_t WXUNUSED(n
)) const
319 wxFAIL_MSG( _T("wxListbook::GetPageImage() not implemented") );
324 bool wxListbook::SetPageImage(size_t n
, int imageId
)
326 return m_list
->SetItemImage(n
, imageId
);
329 // ----------------------------------------------------------------------------
331 // ----------------------------------------------------------------------------
333 void wxListbook::SetImageList(wxImageList
*imageList
)
335 m_list
->SetImageList(imageList
, wxIMAGE_LIST_NORMAL
);
337 wxBookCtrlBase::SetImageList(imageList
);
340 // ----------------------------------------------------------------------------
342 // ----------------------------------------------------------------------------
344 int wxListbook::GetSelection() const
349 int wxListbook::SetSelection(size_t n
)
351 wxCHECK_MSG( IS_VALID_PAGE(n
), wxNOT_FOUND
,
352 wxT("invalid page index in wxListbook::SetSelection()") );
354 const int oldSel
= m_selection
;
356 if ( int(n
) != m_selection
)
358 wxListbookEvent
event(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING
, m_windowId
);
359 event
.SetSelection(n
);
360 event
.SetOldSelection(m_selection
);
361 event
.SetEventObject(this);
362 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
364 if ( m_selection
!= wxNOT_FOUND
)
365 m_pages
[m_selection
]->Hide();
367 wxWindow
*page
= m_pages
[n
];
368 page
->SetSize(GetPageRect());
371 // change m_selection now to ignore the selection change event
376 // program allows the page change
377 event
.SetEventType(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED
);
378 (void)GetEventHandler()->ProcessEvent(event
);
385 // ----------------------------------------------------------------------------
386 // adding/removing the pages
387 // ----------------------------------------------------------------------------
390 wxListbook::InsertPage(size_t n
,
392 const wxString
& text
,
396 if ( !wxBookCtrlBase::InsertPage(n
, page
, text
, bSelect
, imageId
) )
399 m_list
->InsertItem(n
, text
, imageId
);
401 // if the inserted page is before the selected one, we must update the
402 // index of the selected page
403 if ( int(n
) <= m_selection
)
405 // one extra page added
407 m_list
->Select(m_selection
);
408 m_list
->Focus(m_selection
);
411 // some page should be selected: either this one or the first one if there
412 // is still no selection
416 else if ( m_selection
== -1 )
419 if ( selNew
!= m_selection
)
423 SetSelection(selNew
);
425 InvalidateBestSize();
430 wxWindow
*wxListbook::DoRemovePage(size_t page
)
432 const size_t page_count
= GetPageCount();
433 wxWindow
*win
= wxBookCtrlBase::DoRemovePage(page
);
437 m_list
->DeleteItem(page
);
439 if (m_selection
>= (int)page
)
441 // force new sel valid if possible
442 int sel
= m_selection
- 1;
445 else if ((page_count
== 2) || (sel
== -1))
448 // force sel invalid if deleting current page - don't try to hide it
449 m_selection
= (m_selection
== (int)page
) ? wxNOT_FOUND
: m_selection
- 1;
451 if ((sel
!= wxNOT_FOUND
) && (sel
!= m_selection
))
462 bool wxListbook::DeleteAllPages()
464 m_list
->DeleteAllItems();
465 return wxBookCtrlBase::DeleteAllPages();
468 // ----------------------------------------------------------------------------
470 // ----------------------------------------------------------------------------
472 void wxListbook::OnListSelected(wxListEvent
& eventList
)
474 const int selNew
= eventList
.GetIndex();
476 if ( selNew
== m_selection
)
478 // this event can only come from our own Select(m_selection) below
479 // which we call when the page change is vetoed, so we should simply
484 SetSelection(selNew
);
486 // change wasn't allowed, return to previous state
487 if (m_selection
!= selNew
)
489 m_list
->Select(m_selection
);
490 m_list
->Focus(m_selection
);
494 #endif // wxUSE_LISTBOOK