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 wxWidgets macros
49 // ----------------------------------------------------------------------------
51 // check that the page index is valid
52 #define IS_VALID_PAGE(nPage) ((nPage) < GetPageCount())
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 IMPLEMENT_DYNAMIC_CLASS(wxListbook
, wxControl
)
59 IMPLEMENT_DYNAMIC_CLASS(wxListbookEvent
, wxNotifyEvent
)
61 const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING
= wxNewEventType();
62 const wxEventType wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED
= wxNewEventType();
63 const int wxID_LISTBOOKLISTVIEW
= wxNewId();
65 BEGIN_EVENT_TABLE(wxListbook
, wxBookCtrl
)
66 EVT_SIZE(wxListbook::OnSize
)
67 EVT_LIST_ITEM_SELECTED(wxID_LISTBOOKLISTVIEW
, wxListbook::OnListSelected
)
70 // ============================================================================
71 // wxListbook implementation
72 // ============================================================================
74 // ----------------------------------------------------------------------------
75 // wxListbook creation
76 // ----------------------------------------------------------------------------
78 void wxListbook::Init()
81 #if wxUSE_LINE_IN_LISTBOOK
83 #endif // wxUSE_LINE_IN_LISTBOOK
84 m_selection
= wxNOT_FOUND
;
88 wxListbook::Create(wxWindow
*parent
,
95 if ( (style
& wxLB_ALIGN_MASK
) == wxLB_DEFAULT
)
101 #endif // __WXMAC__/!__WXMAC__
104 // no border for this control, it doesn't look nice together with
106 style
&= ~wxBORDER_MASK
;
107 style
|= wxBORDER_NONE
;
109 if ( !wxControl::Create(parent
, id
, pos
, size
, style
,
110 wxDefaultValidator
, name
) )
113 m_list
= new wxListView
116 wxID_LISTBOOKLISTVIEW
,
119 wxLC_ICON
| wxLC_SINGLE_SEL
|
120 (IsVertical() ? wxLC_ALIGN_LEFT
: wxLC_ALIGN_TOP
)
123 #if wxUSE_LINE_IN_LISTBOOK
124 m_line
= new wxStaticLine
130 IsVertical() ? wxLI_HORIZONTAL
: wxLI_VERTICAL
132 #endif // wxUSE_LINE_IN_LISTBOOK
135 // On XP with themes enabled the GetViewRect used in GetListSize to
136 // determine the space needed for the list view will incorrectly return
137 // (0,0,0,0) the first time. So send a pending event so OnSize wiull be
138 // called again after the window is ready to go. Technically we don't
139 // need to do this on non-XP windows, but if things are already sized
140 // correctly then nothing changes and so there is no harm.
142 GetEventHandler()->AddPendingEvent(evt
);
147 // ----------------------------------------------------------------------------
148 // wxListbook geometry management
149 // ----------------------------------------------------------------------------
151 wxSize
wxListbook::GetListSize() const
153 const wxSize sizeClient
= GetClientSize(),
154 sizeList
= m_list
->GetViewRect().GetSize();
159 size
.x
= sizeClient
.x
;
162 else // left/right aligned
165 size
.y
= sizeClient
.y
;
171 wxRect
wxListbook::GetPageRect() const
173 const wxSize sizeList
= m_list
->GetSize();
175 wxRect
rectPage(wxPoint(0, 0), GetClientSize());
176 switch ( GetWindowStyle() & wxLB_ALIGN_MASK
)
179 wxFAIL_MSG( _T("unexpected wxListbook alignment") );
183 rectPage
.y
= sizeList
.y
+ MARGIN
;
187 rectPage
.height
-= sizeList
.y
+ MARGIN
;
191 rectPage
.x
= sizeList
.x
+ MARGIN
;
195 rectPage
.width
-= sizeList
.x
+ MARGIN
;
202 void wxListbook::OnSize(wxSizeEvent
& event
)
208 // we're not fully created yet
212 // resize the list control and the page area to fit inside our new size
213 const wxSize sizeClient
= GetClientSize(),
214 sizeList
= GetListSize();
217 switch ( GetWindowStyle() & wxLB_ALIGN_MASK
)
220 wxFAIL_MSG( _T("unexpected wxListbook alignment") );
225 // posList is already ok
229 posList
.y
= sizeClient
.y
- sizeList
.y
;
233 posList
.x
= sizeClient
.x
- sizeList
.x
;
237 m_list
->Move(posList
.x
, posList
.y
);
238 m_list
->SetClientSize(sizeList
.x
, sizeList
.y
);
240 #if wxUSE_LINE_IN_LISTBOOK
243 wxRect
rectLine(wxPoint(0, 0), sizeClient
);
245 switch ( GetWindowStyle() & wxLB_ALIGN_MASK
)
248 rectLine
.y
= sizeList
.y
+ 1;
249 rectLine
.height
= MARGIN
- 2;
253 rectLine
.height
= MARGIN
- 2;
254 rectLine
.y
= sizeClient
.y
- sizeList
.y
- rectLine
.height
;
258 rectLine
.x
= sizeList
.x
+ 1;
259 rectLine
.width
= MARGIN
- 2;
263 rectLine
.width
= MARGIN
- 2;
264 rectLine
.x
= sizeClient
.x
- sizeList
.x
- rectLine
.width
;
268 m_line
->SetSize(rectLine
);
270 #endif // wxUSE_LINE_IN_LISTBOOK
272 // resize the currently shown page
273 if (m_selection
!= wxNOT_FOUND
)
275 wxWindow
*page
= m_pages
[m_selection
];
276 wxCHECK_RET( page
, _T("NULL page in wxListbook?") );
277 page
->SetSize(GetPageRect());
281 wxSize
wxListbook::CalcSizeFromPage(const wxSize
& sizePage
) const
283 // we need to add the size of the list control and the margin
284 const wxSize sizeList
= GetListSize();
286 wxSize size
= sizePage
;
289 size
.y
+= sizeList
.y
+ MARGIN
;
291 else // left/right aligned
293 size
.x
+= sizeList
.x
+ MARGIN
;
300 // ----------------------------------------------------------------------------
301 // accessing the pages
302 // ----------------------------------------------------------------------------
304 bool wxListbook::SetPageText(size_t n
, const wxString
& strText
)
306 m_list
->SetItemText(n
, strText
);
311 wxString
wxListbook::GetPageText(size_t n
) const
313 return m_list
->GetItemText(n
);
316 int wxListbook::GetPageImage(size_t WXUNUSED(n
)) const
318 wxFAIL_MSG( _T("wxListbook::GetPageImage() not implemented") );
323 bool wxListbook::SetPageImage(size_t n
, int imageId
)
325 return m_list
->SetItemImage(n
, imageId
, imageId
);
328 // ----------------------------------------------------------------------------
330 // ----------------------------------------------------------------------------
332 void wxListbook::SetImageList(wxImageList
*imageList
)
334 m_list
->SetImageList(imageList
, wxIMAGE_LIST_NORMAL
);
336 wxBookCtrl::SetImageList(imageList
);
339 // ----------------------------------------------------------------------------
341 // ----------------------------------------------------------------------------
343 int wxListbook::GetSelection() const
348 int wxListbook::SetSelection(size_t n
)
350 wxCHECK_MSG( IS_VALID_PAGE(n
), wxNOT_FOUND
,
351 wxT("invalid page index in wxListbook::SetSelection()") );
353 const int oldSel
= m_selection
;
355 if ( int(n
) != m_selection
)
357 wxListbookEvent
event(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING
, m_windowId
);
358 event
.SetSelection(n
);
359 event
.SetOldSelection(m_selection
);
360 event
.SetEventObject(this);
361 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
363 if ( m_selection
!= wxNOT_FOUND
)
364 m_pages
[m_selection
]->Hide();
366 wxWindow
*page
= m_pages
[n
];
367 page
->SetSize(GetPageRect());
374 // program allows the page change
375 event
.SetEventType(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED
);
376 (void)GetEventHandler()->ProcessEvent(event
);
383 // ----------------------------------------------------------------------------
384 // adding/removing the pages
385 // ----------------------------------------------------------------------------
388 wxListbook::InsertPage(size_t n
,
390 const wxString
& text
,
394 if ( !wxBookCtrl::InsertPage(n
, page
, text
, bSelect
, imageId
) )
397 m_list
->InsertItem(n
, text
, imageId
);
399 // we should always have some selection if possible
400 if ( bSelect
|| (m_selection
== wxNOT_FOUND
) )
404 else // don't select this page
406 // it will be shown only when selected
410 InvalidateBestSize();
414 wxWindow
*wxListbook::DoRemovePage(size_t page
)
416 const int page_count
= GetPageCount();
417 wxWindow
*win
= wxBookCtrl::DoRemovePage(page
);
421 m_list
->DeleteItem(page
);
423 if (m_selection
>= (int)page
)
425 // force new sel valid if possible
426 int sel
= m_selection
- 1;
429 else if ((page_count
== 2) || (sel
== -1))
432 // force sel invalid if deleting current page - don't try to hide it
433 m_selection
= (m_selection
== (int)page
) ? wxNOT_FOUND
: m_selection
- 1;
435 if ((sel
!= wxNOT_FOUND
) && (sel
!= m_selection
))
444 bool wxListbook::DeleteAllPages()
446 m_list
->DeleteAllItems();
447 return wxBookCtrl::DeleteAllPages();
450 // ----------------------------------------------------------------------------
452 // ----------------------------------------------------------------------------
454 void wxListbook::OnListSelected(wxListEvent
& eventList
)
456 const int selNew
= eventList
.GetIndex();
457 const int selOld
= m_selection
;
459 if ( selNew
== m_selection
)
461 // this event can only come from our own Select(m_selection) below
462 // which we call when the page change is vetoed, so we should simply
467 // first send "change in progress" event which may be vetoed by user
468 wxListbookEvent
eventIng(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING
, GetId());
470 eventIng
.SetEventObject(this);
471 eventIng
.SetSelection(selNew
);
472 eventIng
.SetOldSelection(selOld
);
473 if ( GetEventHandler()->ProcessEvent(eventIng
) && !eventIng
.IsAllowed() )
475 m_list
->Select(m_selection
);
479 // change allowed: do change the page and notify the user about it
480 SetSelection(selNew
);
482 wxListbookEvent
eventEd(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED
, GetId());
484 eventEd
.SetEventObject(this);
485 eventEd
.SetSelection(selNew
);
486 eventEd
.SetOldSelection(selOld
);
488 (void)GetEventHandler()->ProcessEvent(eventEd
);
491 #endif // wxUSE_LISTBOOK