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
, wxBookCtrlBase
)
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 will 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 sizeBorder
= m_list
->GetSize() - m_list
->GetClientSize(),
155 sizeList
= m_list
->GetViewRect().GetSize() + sizeBorder
;
160 size
.x
= sizeClient
.x
;
163 else // left/right aligned
166 size
.y
= sizeClient
.y
;
172 wxRect
wxListbook::GetPageRect() const
174 const wxSize sizeList
= m_list
->GetSize();
177 wxRect
rectPage(pt
, GetClientSize());
178 switch ( GetWindowStyle() & wxLB_ALIGN_MASK
)
181 wxFAIL_MSG( _T("unexpected wxListbook alignment") );
185 rectPage
.y
= sizeList
.y
+ MARGIN
;
189 rectPage
.height
-= sizeList
.y
+ MARGIN
;
193 rectPage
.x
= sizeList
.x
+ MARGIN
;
197 rectPage
.width
-= sizeList
.x
+ MARGIN
;
204 void wxListbook::OnSize(wxSizeEvent
& event
)
210 // we're not fully created yet
214 // arrange the icons before calling SetClientSize(), otherwise it wouldn't
215 // account for the scrollbars the list control might need and, at least
216 // under MSW, we'd finish with an ugly looking list control with both
217 // vertical and horizontal scrollbar (with one of them being added because
218 // the other one is not accounted for in client size computations)
221 // resize the list control and the page area to fit inside our new size
222 const wxSize sizeClient
= GetClientSize(),
223 sizeBorder
= m_list
->GetSize() - m_list
->GetClientSize(),
224 sizeList
= GetListSize();
226 m_list
->SetClientSize( sizeList
.x
- sizeBorder
.x
, sizeList
.y
- sizeBorder
.y
);
228 const wxSize sizeNew
= m_list
->GetSize();
230 switch ( GetWindowStyle() & wxLB_ALIGN_MASK
)
233 wxFAIL_MSG( _T("unexpected wxListbook alignment") );
238 // posList is already ok
242 posList
.y
= sizeClient
.y
- sizeNew
.y
;
246 posList
.x
= sizeClient
.x
- sizeNew
.x
;
250 if ( m_list
->GetPosition() != posList
)
251 m_list
->Move(posList
);
253 #if wxUSE_LINE_IN_LISTBOOK
256 wxRect
rectLine(sizeClient
);
258 switch ( GetWindowStyle() & wxLB_ALIGN_MASK
)
261 rectLine
.y
= sizeNew
.y
+ 1;
262 rectLine
.height
= MARGIN
- 2;
266 rectLine
.height
= MARGIN
- 2;
267 rectLine
.y
= sizeClient
.y
- sizeNew
.y
- rectLine
.height
;
271 rectLine
.x
= sizeNew
.x
+ 1;
272 rectLine
.width
= MARGIN
- 2;
276 rectLine
.width
= MARGIN
- 2;
277 rectLine
.x
= sizeClient
.x
- sizeNew
.x
- rectLine
.width
;
281 m_line
->SetSize(rectLine
);
283 #endif // wxUSE_LINE_IN_LISTBOOK
285 // resize the currently shown page
286 if (m_selection
!= wxNOT_FOUND
)
288 wxWindow
*page
= m_pages
[m_selection
];
289 wxCHECK_RET( page
, _T("NULL page in wxListbook?") );
290 page
->SetSize(GetPageRect());
294 wxSize
wxListbook::CalcSizeFromPage(const wxSize
& sizePage
) const
296 // we need to add the size of the list control and the margin
297 const wxSize sizeList
= GetListSize();
299 wxSize size
= sizePage
;
302 size
.y
+= sizeList
.y
+ MARGIN
;
304 else // left/right aligned
306 size
.x
+= sizeList
.x
+ MARGIN
;
313 // ----------------------------------------------------------------------------
314 // accessing the pages
315 // ----------------------------------------------------------------------------
317 bool wxListbook::SetPageText(size_t n
, const wxString
& strText
)
319 m_list
->SetItemText(n
, strText
);
324 wxString
wxListbook::GetPageText(size_t n
) const
326 return m_list
->GetItemText(n
);
329 int wxListbook::GetPageImage(size_t WXUNUSED(n
)) const
331 wxFAIL_MSG( _T("wxListbook::GetPageImage() not implemented") );
336 bool wxListbook::SetPageImage(size_t n
, int imageId
)
338 return m_list
->SetItemImage(n
, imageId
);
341 // ----------------------------------------------------------------------------
343 // ----------------------------------------------------------------------------
345 void wxListbook::SetImageList(wxImageList
*imageList
)
347 m_list
->SetImageList(imageList
, wxIMAGE_LIST_NORMAL
);
349 wxBookCtrlBase::SetImageList(imageList
);
352 // ----------------------------------------------------------------------------
354 // ----------------------------------------------------------------------------
356 int wxListbook::GetSelection() const
361 int wxListbook::SetSelection(size_t n
)
363 wxCHECK_MSG( IS_VALID_PAGE(n
), wxNOT_FOUND
,
364 wxT("invalid page index in wxListbook::SetSelection()") );
366 const int oldSel
= m_selection
;
368 if ( int(n
) != m_selection
)
370 wxListbookEvent
event(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING
, m_windowId
);
371 event
.SetSelection(n
);
372 event
.SetOldSelection(m_selection
);
373 event
.SetEventObject(this);
374 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
376 if ( m_selection
!= wxNOT_FOUND
)
377 m_pages
[m_selection
]->Hide();
379 wxWindow
*page
= m_pages
[n
];
380 page
->SetSize(GetPageRect());
383 // change m_selection now to ignore the selection change event
388 // program allows the page change
389 event
.SetEventType(wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED
);
390 (void)GetEventHandler()->ProcessEvent(event
);
397 // ----------------------------------------------------------------------------
398 // adding/removing the pages
399 // ----------------------------------------------------------------------------
402 wxListbook::InsertPage(size_t n
,
404 const wxString
& text
,
408 if ( !wxBookCtrlBase::InsertPage(n
, page
, text
, bSelect
, imageId
) )
411 m_list
->InsertItem(n
, text
, imageId
);
413 // if the inserted page is before the selected one, we must update the
414 // index of the selected page
415 if ( int(n
) <= m_selection
)
417 // one extra page added
419 m_list
->Select(m_selection
);
420 m_list
->Focus(m_selection
);
423 // some page should be selected: either this one or the first one if there
424 // is still no selection
428 else if ( m_selection
== -1 )
431 if ( selNew
!= m_selection
)
435 SetSelection(selNew
);
437 InvalidateBestSize();
442 wxWindow
*wxListbook::DoRemovePage(size_t page
)
444 const int page_count
= GetPageCount();
445 wxWindow
*win
= wxBookCtrlBase::DoRemovePage(page
);
449 m_list
->DeleteItem(page
);
451 if (m_selection
>= (int)page
)
453 // force new sel valid if possible
454 int sel
= m_selection
- 1;
457 else if ((page_count
== 2) || (sel
== -1))
460 // force sel invalid if deleting current page - don't try to hide it
461 m_selection
= (m_selection
== (int)page
) ? wxNOT_FOUND
: m_selection
- 1;
463 if ((sel
!= wxNOT_FOUND
) && (sel
!= m_selection
))
474 bool wxListbook::DeleteAllPages()
476 m_list
->DeleteAllItems();
477 return wxBookCtrlBase::DeleteAllPages();
480 // ----------------------------------------------------------------------------
482 // ----------------------------------------------------------------------------
484 void wxListbook::OnListSelected(wxListEvent
& eventList
)
486 const int selNew
= eventList
.GetIndex();
488 if ( selNew
== m_selection
)
490 // this event can only come from our own Select(m_selection) below
491 // which we call when the page change is vetoed, so we should simply
496 SetSelection(selNew
);
498 // change wasn't allowed, return to previous state
499 if (m_selection
!= selNew
)
501 m_list
->Select(m_selection
);
502 m_list
->Focus(m_selection
);
506 #endif // wxUSE_LISTBOOK