1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/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/listbook.h"
32 #include "wx/settings.h"
35 #include "wx/listctrl.h"
36 #include "wx/statline.h"
37 #include "wx/imaglist.h"
39 #include "wx/sysopt.h"
41 // ----------------------------------------------------------------------------
42 // various wxWidgets macros
43 // ----------------------------------------------------------------------------
45 // check that the page index is valid
46 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 IMPLEMENT_DYNAMIC_CLASS(wxListbook
, wxBookCtrlBase
)
54 wxDEFINE_EVENT( wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING
, wxBookCtrlEvent
);
55 wxDEFINE_EVENT( wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED
, wxBookCtrlEvent
);
57 BEGIN_EVENT_TABLE(wxListbook
, wxBookCtrlBase
)
58 EVT_SIZE(wxListbook::OnSize
)
59 EVT_LIST_ITEM_SELECTED(wxID_ANY
, wxListbook::OnListSelected
)
62 // ============================================================================
63 // wxListbook implementation
64 // ============================================================================
66 // ----------------------------------------------------------------------------
67 // wxListbook creation
68 // ----------------------------------------------------------------------------
71 wxListbook::Create(wxWindow
*parent
,
78 if ( (style
& wxBK_ALIGN_MASK
) == wxBK_DEFAULT
)
84 #endif // __WXMAC__/!__WXMAC__
87 // no border for this control, it doesn't look nice together with
89 style
&= ~wxBORDER_MASK
;
90 style
|= wxBORDER_NONE
;
92 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
93 wxDefaultValidator
, name
) )
96 m_bookctrl
= new wxListView
103 (IsVertical() ? wxLC_ALIGN_LEFT
: wxLC_ALIGN_TOP
) |
108 // On XP with themes enabled the GetViewRect used in GetControllerSize() to
109 // determine the space needed for the list view will incorrectly return
110 // (0,0,0,0) the first time. So send a pending event so OnSize will be
111 // called again after the window is ready to go. Technically we don't
112 // need to do this on non-XP windows, but if things are already sized
113 // correctly then nothing changes and so there is no harm.
115 GetEventHandler()->AddPendingEvent(evt
);
120 // ----------------------------------------------------------------------------
121 // wxListbook geometry management
122 // ----------------------------------------------------------------------------
124 void wxListbook::OnSize(wxSizeEvent
& event
)
126 // arrange the icons before calling SetClientSize(), otherwise it wouldn't
127 // account for the scrollbars the list control might need and, at least
128 // under MSW, we'd finish with an ugly looking list control with both
129 // vertical and horizontal scrollbar (with one of them being added because
130 // the other one is not accounted for in client size computations)
131 wxListView
* const list
= GetListView();
138 int wxListbook::HitTest(const wxPoint
& pt
, long *flags
) const
140 int pagePos
= wxNOT_FOUND
;
143 *flags
= wxBK_HITTEST_NOWHERE
;
145 // convert from listbook control coordinates to list control coordinates
146 const wxListView
* const list
= GetListView();
147 const wxPoint listPt
= list
->ScreenToClient(ClientToScreen(pt
));
149 // is the point inside list control?
150 if ( wxRect(list
->GetSize()).Contains(listPt
) )
153 pagePos
= list
->HitTest(listPt
, flagsList
);
157 if ( pagePos
!= wxNOT_FOUND
)
160 if ( flagsList
& (wxLIST_HITTEST_ONITEMICON
|
161 wxLIST_HITTEST_ONITEMSTATEICON
) )
162 *flags
|= wxBK_HITTEST_ONICON
;
164 if ( flagsList
& wxLIST_HITTEST_ONITEMLABEL
)
165 *flags
|= wxBK_HITTEST_ONLABEL
;
168 else // not over list control at all
170 if ( flags
&& GetPageRect().Contains(pt
) )
171 *flags
|= wxBK_HITTEST_ONPAGE
;
177 void wxListbook::UpdateSize()
179 // we should find a more elegant way to force a layout than generating this
181 wxSizeEvent
sz(GetSize(), GetId());
182 GetEventHandler()->ProcessEvent(sz
);
185 // ----------------------------------------------------------------------------
186 // accessing the pages
187 // ----------------------------------------------------------------------------
189 bool wxListbook::SetPageText(size_t n
, const wxString
& strText
)
191 GetListView()->SetItemText(n
, strText
);
196 wxString
wxListbook::GetPageText(size_t n
) const
198 return GetListView()->GetItemText(n
);
201 int wxListbook::GetPageImage(size_t n
) const
206 if (GetListView()->GetItem(item
))
208 return item
.GetImage();
216 bool wxListbook::SetPageImage(size_t n
, int imageId
)
218 return GetListView()->SetItemImage(n
, imageId
);
221 // ----------------------------------------------------------------------------
223 // ----------------------------------------------------------------------------
225 void wxListbook::SetImageList(wxImageList
*imageList
)
227 wxListView
* const list
= GetListView();
229 // If imageList presence has changed, we update the list control style
230 if ( (imageList
!= NULL
) != (GetImageList() != NULL
) )
232 // Preserve the selection which is lost when changing the mode
233 const int oldSel
= GetSelection();
235 // Update the style to use icon view for images, list view otherwise
236 long style
= list
->GetWindowStyle() & ~wxLC_MASK_TYPE
;
241 else // no image list
246 list
->SetWindowStyleFlag(style
);
249 if ( oldSel
!= wxNOT_FOUND
)
250 SetSelection(oldSel
);
253 list
->SetImageList(imageList
, wxIMAGE_LIST_NORMAL
);
255 wxBookCtrlBase::SetImageList(imageList
);
258 // ----------------------------------------------------------------------------
260 // ----------------------------------------------------------------------------
262 void wxListbook::UpdateSelectedPage(size_t newsel
)
264 m_selection
= newsel
;
265 GetListView()->Select(newsel
);
266 GetListView()->Focus(newsel
);
269 wxBookCtrlEvent
* wxListbook::CreatePageChangingEvent() const
271 return new wxBookCtrlEvent(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING
, m_windowId
);
274 void wxListbook::MakeChangedEvent(wxBookCtrlEvent
&event
)
276 event
.SetEventType(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED
);
280 // ----------------------------------------------------------------------------
281 // adding/removing the pages
282 // ----------------------------------------------------------------------------
285 wxListbook::InsertPage(size_t n
,
287 const wxString
& text
,
291 if ( !wxBookCtrlBase::InsertPage(n
, page
, text
, bSelect
, imageId
) )
294 GetListView()->InsertItem(n
, text
, imageId
);
296 // if the inserted page is before the selected one, we must update the
297 // index of the selected page
298 if ( int(n
) <= m_selection
)
300 // one extra page added
302 GetListView()->Select(m_selection
);
303 GetListView()->Focus(m_selection
);
306 if ( !DoSetSelectionAfterInsertion(n
, bSelect
) )
314 wxWindow
*wxListbook::DoRemovePage(size_t page
)
316 const size_t page_count
= GetPageCount();
317 wxWindow
*win
= wxBookCtrlBase::DoRemovePage(page
);
321 GetListView()->DeleteItem(page
);
323 if (m_selection
>= (int)page
)
325 // force new sel valid if possible
326 int sel
= m_selection
- 1;
329 else if ((page_count
== 2) || (sel
== wxNOT_FOUND
))
332 // force sel invalid if deleting current page - don't try to hide it
333 m_selection
= (m_selection
== (int)page
) ? wxNOT_FOUND
: m_selection
- 1;
335 if ((sel
!= wxNOT_FOUND
) && (sel
!= m_selection
))
339 GetListView()->Arrange();
347 bool wxListbook::DeleteAllPages()
349 GetListView()->DeleteAllItems();
350 if (!wxBookCtrlBase::DeleteAllPages())
358 // ----------------------------------------------------------------------------
360 // ----------------------------------------------------------------------------
362 void wxListbook::OnListSelected(wxListEvent
& eventList
)
364 if ( eventList
.GetEventObject() != m_bookctrl
)
370 const int selNew
= eventList
.GetIndex();
372 if ( selNew
== m_selection
)
374 // this event can only come from our own Select(m_selection) below
375 // which we call when the page change is vetoed, so we should simply
380 SetSelection(selNew
);
382 // change wasn't allowed, return to previous state
383 if (m_selection
!= selNew
)
385 GetListView()->Select(m_selection
);
386 GetListView()->Focus(m_selection
);
390 #endif // wxUSE_LISTBOOK