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();
176 wxRect
rectPage(pt
, GetClientSize());
177 switch ( GetWindowStyle() & wxLB_ALIGN_MASK
)
180 wxFAIL_MSG( _T("unexpected wxListbook alignment") );
184 rectPage
.y
= sizeList
.y
+ MARGIN
;
188 rectPage
.height
-= sizeList
.y
+ MARGIN
;
192 rectPage
.x
= sizeList
.x
+ MARGIN
;
196 rectPage
.width
-= sizeList
.x
+ MARGIN
;
203 void wxListbook::OnSize(wxSizeEvent
& event
)
209 // we're not fully created yet
213 // resize the list control and the page area to fit inside our new size
214 const wxSize sizeClient
= GetClientSize(),
215 sizeList
= GetListSize();
218 switch ( GetWindowStyle() & wxLB_ALIGN_MASK
)
221 wxFAIL_MSG( _T("unexpected wxListbook alignment") );
226 // posList is already ok
230 posList
.y
= sizeClient
.y
- sizeList
.y
;
234 posList
.x
= sizeClient
.x
- sizeList
.x
;
238 m_list
->Move(posList
.x
, posList
.y
);
239 m_list
->SetClientSize(sizeList
.x
, sizeList
.y
);
241 #if wxUSE_LINE_IN_LISTBOOK
244 wxRect
rectLine(wxPoint(0, 0), sizeClient
);
246 switch ( GetWindowStyle() & wxLB_ALIGN_MASK
)
249 rectLine
.y
= sizeList
.y
+ 1;
250 rectLine
.height
= MARGIN
- 2;
254 rectLine
.height
= MARGIN
- 2;
255 rectLine
.y
= sizeClient
.y
- sizeList
.y
- rectLine
.height
;
259 rectLine
.x
= sizeList
.x
+ 1;
260 rectLine
.width
= MARGIN
- 2;
264 rectLine
.width
= MARGIN
- 2;
265 rectLine
.x
= sizeClient
.x
- sizeList
.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 margin
285 const wxSize sizeList
= GetListSize();
287 wxSize size
= sizePage
;
290 size
.y
+= sizeList
.y
+ MARGIN
;
292 else // left/right aligned
294 size
.x
+= sizeList
.x
+ MARGIN
;
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 wxBookCtrl::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 ( !wxBookCtrl::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();
429 wxWindow
*wxListbook::DoRemovePage(size_t page
)
431 const int page_count
= GetPageCount();
432 wxWindow
*win
= wxBookCtrl::DoRemovePage(page
);
436 m_list
->DeleteItem(page
);
438 if (m_selection
>= (int)page
)
440 // force new sel valid if possible
441 int sel
= m_selection
- 1;
444 else if ((page_count
== 2) || (sel
== -1))
447 // force sel invalid if deleting current page - don't try to hide it
448 m_selection
= (m_selection
== (int)page
) ? wxNOT_FOUND
: m_selection
- 1;
450 if ((sel
!= wxNOT_FOUND
) && (sel
!= m_selection
))
459 bool wxListbook::DeleteAllPages()
461 m_list
->DeleteAllItems();
462 return wxBookCtrl::DeleteAllPages();
465 // ----------------------------------------------------------------------------
467 // ----------------------------------------------------------------------------
469 void wxListbook::OnListSelected(wxListEvent
& eventList
)
471 const int selNew
= eventList
.GetIndex();
473 if ( selNew
== m_selection
)
475 // this event can only come from our own Select(m_selection) below
476 // which we call when the page change is vetoed, so we should simply
481 SetSelection(selNew
);
483 // change wasn't allowed, return to previous state
484 if (m_selection
!= selNew
)
486 m_list
->Select(m_selection
);
487 m_list
->Focus(m_selection
);
491 #endif // wxUSE_LISTBOOK