1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: generic/vlbox.cpp
3 // Purpose: implementation of wxVListBox
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
9 // License: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
28 #include "wx/settings.h"
29 #include "wx/dcclient.h"
33 #include "wx/selstore.h"
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 BEGIN_EVENT_TABLE(wxVListBox
, wxVScrolledWindow
)
40 EVT_PAINT(wxVListBox::OnPaint
)
42 EVT_KEY_DOWN(wxVListBox::OnKeyDown
)
43 EVT_LEFT_DOWN(wxVListBox::OnLeftDown
)
44 EVT_LEFT_DCLICK(wxVListBox::OnLeftDClick
)
47 // ============================================================================
49 // ============================================================================
51 // ----------------------------------------------------------------------------
52 // wxVListBox creation
53 // ----------------------------------------------------------------------------
55 void wxVListBox::Init()
57 m_current
= wxNOT_FOUND
;
61 bool wxVListBox::Create(wxWindow
*parent
,
68 if ( !wxVScrolledWindow::Create(parent
, id
, pos
, size
, style
, name
) )
71 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
));
73 if ( style
& wxLB_MULTIPLE
)
74 m_selStore
= new wxSelectionStore
;
79 wxVListBox::~wxVListBox()
84 void wxVListBox::SetItemCount(size_t count
)
88 // tell the selection store that our number of items has changed
89 m_selStore
->SetItemCount(count
);
95 // ----------------------------------------------------------------------------
97 // ----------------------------------------------------------------------------
99 bool wxVListBox::IsSelected(size_t line
) const
101 return m_selStore
? m_selStore
->IsSelected(line
) : (int)line
== m_current
;
104 bool wxVListBox::Select(size_t item
, bool select
)
106 wxCHECK_MSG( m_selStore
, false,
107 _T("Select() may only be used with multiselection listbox") );
109 wxCHECK_MSG( item
< GetItemCount(), false,
110 _T("Select(): invalid item index") );
112 bool changed
= m_selStore
->SelectItem(item
, select
);
115 // selection really changed
124 bool wxVListBox::SelectRange(size_t from
, size_t to
)
126 wxCHECK_MSG( m_selStore
, false,
127 _T("SelectRange() may only be used with multiselection listbox") );
129 // make sure items are in correct order
137 wxCHECK_MSG( to
< GetItemCount(), false,
138 _T("SelectRange(): invalid item index") );
141 if ( !m_selStore
->SelectRange(from
, to
, true, &changed
) )
143 // too many items have changed, we didn't record them in changed array
144 // so we have no choice but to refresh everything between from and to
145 RefreshLines(from
, to
);
147 else // we've got the indices of the changed items
149 const size_t count
= changed
.GetCount();
156 // refresh just the lines which have really changed
157 for ( size_t n
= 0; n
< count
; n
++ )
159 RefreshLine(changed
[n
]);
167 bool wxVListBox::DoSelectAll(bool select
)
169 wxCHECK_MSG( m_selStore
, false,
170 _T("SelectAll may only be used with multiselection listbox") );
172 size_t count
= GetItemCount();
176 if ( !m_selStore
->SelectRange(0, count
- 1, select
) ||
189 bool wxVListBox::DoSetCurrent(int current
)
191 wxASSERT_MSG( current
== wxNOT_FOUND
||
192 (current
>= 0 && (size_t)current
< GetItemCount()),
193 _T("wxVListBox::DoSetCurrent(): invalid item index") );
195 if ( current
== m_current
)
201 if ( m_current
!= wxNOT_FOUND
)
202 RefreshLine(m_current
);
206 if ( m_current
!= wxNOT_FOUND
)
208 // if the line is not visible at all, we scroll it into view but we
209 // don't need to refresh it -- it will be redrawn anyhow
210 if ( !IsVisible(m_current
) )
212 ScrollToLine(m_current
);
214 else // line is at least partly visible
216 // it is, indeed, only partly visible, so scroll it into view to
217 // make it entirely visible
218 if ( (size_t)m_current
== GetLastVisibleLine() )
220 ScrollToLine(m_current
);
223 // but in any case refresh it as even if it was only partly visible
224 // before we need to redraw it entirely as its background changed
225 RefreshLine(m_current
);
232 void wxVListBox::SendSelectedEvent()
234 wxASSERT_MSG( m_current
!= wxNOT_FOUND
,
235 _T("SendSelectedEvent() shouldn't be called") );
237 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, GetId());
238 event
.SetEventObject(this);
239 event
.m_commandInt
= m_current
;
241 (void)GetEventHandler()->ProcessEvent(event
);
244 void wxVListBox::SetSelection(int selection
)
246 wxCHECK_RET( selection
== wxNOT_FOUND
||
247 (selection
>= 0 && (size_t)selection
< GetItemCount()),
248 _T("wxVListBox::SetSelection(): invalid item index") );
250 wxASSERT_MSG( !HasMultipleSelection(),
251 _T("SetSelection() is invalid with multiselection listbox") );
253 DoSetCurrent(selection
);
256 size_t wxVListBox::GetSelectedCount() const
258 return m_selStore
? m_selStore
->GetSelectedCount()
259 : m_current
== wxNOT_FOUND
? 0 : 1;
262 int wxVListBox::GetFirstSelected(unsigned long& cookie
) const
266 return GetNextSelected(cookie
);
269 int wxVListBox::GetNextSelected(unsigned long& cookie
) const
271 wxCHECK_MSG( m_selStore
, wxNOT_FOUND
,
272 _T("GetFirst/NextSelected() may only be used with multiselection listboxes") );
274 while ( cookie
< GetItemCount() )
276 if ( IsSelected(cookie
++) )
283 // ----------------------------------------------------------------------------
284 // wxVListBox painting
285 // ----------------------------------------------------------------------------
287 void wxVListBox::SetMargins(const wxPoint
& pt
)
289 if ( pt
!= m_ptMargins
)
297 wxCoord
wxVListBox::OnGetLineHeight(size_t line
) const
299 return OnMeasureItem(line
) + 2*m_ptMargins
.y
;
302 void wxVListBox::OnDrawSeparator(wxDC
& WXUNUSED(dc
),
303 wxRect
& WXUNUSED(rect
),
304 size_t WXUNUSED(n
)) const
308 void wxVListBox::OnPaint(wxPaintEvent
& event
)
312 // the update rectangle
313 wxRect rectUpdate
= GetUpdateClientRect();
315 // the bounding rectangle of the current line
317 rectLine
.width
= GetClientSize().x
;
319 // iterate over all visible lines
320 const size_t lineMax
= GetLastVisibleLine();
321 for ( size_t line
= GetFirstVisibleLine(); line
<= lineMax
; line
++ )
323 const wxCoord hLine
= OnGetLineHeight(line
);
325 rectLine
.height
= hLine
;
327 // and draw the ones which intersect the update rect
328 if ( rectLine
.Intersects(rectUpdate
) )
330 // don't allow drawing outside of the lines rectangle
331 wxDCClipper
clip(dc
, rectLine
);
333 // we need to render selected and current items differently
334 const bool isSelected
= IsSelected(line
);
335 if ( isSelected
|| IsCurrent(line
) )
339 wxBrush
brush(wxSystemSettings::
340 GetColour(wxSYS_COLOUR_HIGHLIGHT
),
346 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
349 dc
.SetPen(*(IsCurrent(line
) ? wxBLACK_PEN
: wxTRANSPARENT_PEN
));
351 dc
.DrawRectangle(rectLine
);
354 wxRect rect
= rectLine
;
355 OnDrawSeparator(dc
, rect
, line
);
357 rect
.Deflate(m_ptMargins
.x
, m_ptMargins
.y
);
358 OnDrawItem(dc
, rect
, line
);
360 else // no intersection
362 if ( rectLine
.GetTop() > rectUpdate
.GetBottom() )
364 // we are already below the update rect, no need to continue
368 //else: the next line may intersect the update rect
375 // ============================================================================
376 // wxVListBox keyboard/mouse handling
377 // ============================================================================
379 void wxVListBox::DoHandleItemClick(int item
, bool shiftDown
, bool ctrlDown
)
381 // has anything worth telling the client code about happened?
384 if ( HasMultipleSelection() )
386 // select the iteem clicked?
389 // NB: the keyboard interface we implement here corresponds to
390 // wxLB_EXTENDED rather than wxLB_MULTIPLE but this one makes more
394 if ( m_current
!= wxNOT_FOUND
)
398 // only the range from old m_current to new m_current must be
403 if ( SelectRange(m_current
, item
) )
406 //else: treat it as ordinary click/keypress
414 // the status of the item has definitely changed
417 //else: behave as in single selection case
421 // make the clicked item the only selection
430 // in any case the item should become the current one
431 if ( DoSetCurrent(item
) )
433 if ( !HasMultipleSelection() )
435 // this has also changed the selection for single selection case
442 // notify the user about the selection change
445 //else: nothing changed at all
448 // ----------------------------------------------------------------------------
450 // ----------------------------------------------------------------------------
452 void wxVListBox::OnKeyDown(wxKeyEvent
& event
)
454 int current
= 0; // just to silent the stupid compiler warnings
455 switch ( event
.GetKeyCode() )
462 current
= GetLineCount() - 1;
466 if ( m_current
== (int)GetLineCount() - 1 )
469 current
= m_current
+ 1;
473 if ( m_current
== wxNOT_FOUND
)
474 current
= GetLineCount() - 1;
475 else if ( m_current
!= 0 )
476 current
= m_current
- 1;
477 else // m_current == 0
484 current
= GetFirstVisibleLine();
489 if ( m_current
== (int)GetFirstVisibleLine() )
494 current
= GetFirstVisibleLine();
502 DoHandleItemClick(current
, event
.ShiftDown(), event
.ControlDown());
505 // ----------------------------------------------------------------------------
506 // wxVListBox mouse handling
507 // ----------------------------------------------------------------------------
509 void wxVListBox::OnLeftDown(wxMouseEvent
& event
)
511 int item
= HitTest(event
.GetPosition());
513 if ( item
!= wxNOT_FOUND
)
515 // under Mac Apple-click is used in the same way as Ctrl-click
517 DoHandleItemClick(item
, event
.ShiftDown(),
527 void wxVListBox::OnLeftDClick(wxMouseEvent
& event
)
529 int item
= HitTest(event
.GetPosition());
530 if ( item
!= wxNOT_FOUND
)
532 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, GetId());
533 event
.SetEventObject(this);
534 event
.m_commandInt
= item
;
536 (void)GetEventHandler()->ProcessEvent(event
);