1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/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"
32 #include "wx/settings.h"
33 #include "wx/dcclient.h"
34 #include "wx/listbox.h"
37 #include "wx/dcbuffer.h"
38 #include "wx/selstore.h"
39 #include "wx/renderer.h"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 BEGIN_EVENT_TABLE(wxVListBox
, wxVScrolledWindow
)
46 EVT_PAINT(wxVListBox::OnPaint
)
48 EVT_KEY_DOWN(wxVListBox::OnKeyDown
)
49 EVT_LEFT_DOWN(wxVListBox::OnLeftDown
)
50 EVT_LEFT_DCLICK(wxVListBox::OnLeftDClick
)
52 EVT_SET_FOCUS(wxVListBox::OnSetOrKillFocus
)
53 EVT_KILL_FOCUS(wxVListBox::OnSetOrKillFocus
)
56 // ============================================================================
58 // ============================================================================
60 IMPLEMENT_ABSTRACT_CLASS(wxVListBox
, wxVScrolledWindow
)
62 // ----------------------------------------------------------------------------
63 // wxVListBox creation
64 // ----------------------------------------------------------------------------
66 void wxVListBox::Init()
69 m_anchor
= wxNOT_FOUND
;
73 bool wxVListBox::Create(wxWindow
*parent
,
81 if ( (style
& wxBORDER_MASK
) == wxDEFAULT
)
82 style
|= wxBORDER_THEME
;
85 style
|= wxWANTS_CHARS
| wxFULL_REPAINT_ON_RESIZE
;
86 if ( !wxVScrolledWindow::Create(parent
, id
, pos
, size
, style
, name
) )
89 if ( style
& wxLB_MULTIPLE
)
90 m_selStore
= new wxSelectionStore
;
92 // make sure the native widget has the right colour since we do
93 // transparent drawing by default
94 SetBackgroundColour(GetBackgroundColour());
96 // leave m_colBgSel in an invalid state: it means for OnDrawBackground()
97 // to use wxRendererNative instead of painting selection bg ourselves
98 m_colBgSel
= wxNullColour
;
100 // flicker-free drawing requires this
101 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
106 wxVListBox::~wxVListBox()
111 void wxVListBox::SetItemCount(size_t count
)
113 // don't leave the current index invalid
114 if ( m_current
!= wxNOT_FOUND
&& (size_t)m_current
>= count
)
115 m_current
= count
- 1; // also ok when count == 0 as wxNOT_FOUND == -1
119 // tell the selection store that our number of items has changed
120 m_selStore
->SetItemCount(count
);
126 // ----------------------------------------------------------------------------
127 // selection handling
128 // ----------------------------------------------------------------------------
130 bool wxVListBox::IsSelected(size_t line
) const
132 return m_selStore
? m_selStore
->IsSelected(line
) : (int)line
== m_current
;
135 bool wxVListBox::Select(size_t item
, bool select
)
137 wxCHECK_MSG( m_selStore
, false,
138 _T("Select() may only be used with multiselection listbox") );
140 wxCHECK_MSG( item
< GetItemCount(), false,
141 _T("Select(): invalid item index") );
143 bool changed
= m_selStore
->SelectItem(item
, select
);
146 // selection really changed
155 bool wxVListBox::SelectRange(size_t from
, size_t to
)
157 wxCHECK_MSG( m_selStore
, false,
158 _T("SelectRange() may only be used with multiselection listbox") );
160 // make sure items are in correct order
168 wxCHECK_MSG( to
< GetItemCount(), false,
169 _T("SelectRange(): invalid item index") );
172 if ( !m_selStore
->SelectRange(from
, to
, true, &changed
) )
174 // too many items have changed, we didn't record them in changed array
175 // so we have no choice but to refresh everything between from and to
176 RefreshRows(from
, to
);
178 else // we've got the indices of the changed items
180 const size_t count
= changed
.GetCount();
187 // refresh just the lines which have really changed
188 for ( size_t n
= 0; n
< count
; n
++ )
190 RefreshRow(changed
[n
]);
198 bool wxVListBox::DoSelectAll(bool select
)
200 wxCHECK_MSG( m_selStore
, false,
201 _T("SelectAll may only be used with multiselection listbox") );
203 size_t count
= GetItemCount();
207 if ( !m_selStore
->SelectRange(0, count
- 1, select
) ||
220 bool wxVListBox::DoSetCurrent(int current
)
222 wxASSERT_MSG( current
== wxNOT_FOUND
||
223 (current
>= 0 && (size_t)current
< GetItemCount()),
224 _T("wxVListBox::DoSetCurrent(): invalid item index") );
226 if ( current
== m_current
)
232 if ( m_current
!= wxNOT_FOUND
)
233 RefreshRow(m_current
);
237 if ( m_current
!= wxNOT_FOUND
)
239 // if the line is not visible at all, we scroll it into view but we
240 // don't need to refresh it -- it will be redrawn anyhow
241 if ( !IsVisible(m_current
) )
243 ScrollToRow(m_current
);
245 else // line is at least partly visible
247 // it is, indeed, only partly visible, so scroll it into view to
248 // make it entirely visible
249 while ( (size_t)m_current
+ 1 == GetVisibleRowsEnd() &&
250 ScrollToRow(GetVisibleBegin() + 1) ) ;
252 // but in any case refresh it as even if it was only partly visible
253 // before we need to redraw it entirely as its background changed
254 RefreshRow(m_current
);
261 void wxVListBox::SendSelectedEvent()
263 wxASSERT_MSG( m_current
!= wxNOT_FOUND
,
264 _T("SendSelectedEvent() shouldn't be called") );
266 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, GetId());
267 event
.SetEventObject(this);
268 event
.SetInt(m_current
);
270 (void)GetEventHandler()->ProcessEvent(event
);
273 void wxVListBox::SetSelection(int selection
)
275 wxCHECK_RET( selection
== wxNOT_FOUND
||
276 (selection
>= 0 && (size_t)selection
< GetItemCount()),
277 _T("wxVListBox::SetSelection(): invalid item index") );
279 if ( HasMultipleSelection() )
281 if (selection
!= wxNOT_FOUND
)
285 m_anchor
= selection
;
288 DoSetCurrent(selection
);
291 size_t wxVListBox::GetSelectedCount() const
293 return m_selStore
? m_selStore
->GetSelectedCount()
294 : m_current
== wxNOT_FOUND
? 0 : 1;
297 int wxVListBox::GetFirstSelected(unsigned long& cookie
) const
301 return GetNextSelected(cookie
);
304 int wxVListBox::GetNextSelected(unsigned long& cookie
) const
306 wxCHECK_MSG( m_selStore
, wxNOT_FOUND
,
307 _T("GetFirst/NextSelected() may only be used with multiselection listboxes") );
309 while ( cookie
< GetItemCount() )
311 if ( IsSelected(cookie
++) )
318 void wxVListBox::RefreshSelected()
320 // only refresh those items which are currently visible and selected:
321 for ( size_t n
= GetVisibleBegin(), end
= GetVisibleEnd(); n
< end
; n
++ )
328 // ----------------------------------------------------------------------------
329 // wxVListBox appearance parameters
330 // ----------------------------------------------------------------------------
332 void wxVListBox::SetMargins(const wxPoint
& pt
)
334 if ( pt
!= m_ptMargins
)
342 void wxVListBox::SetSelectionBackground(const wxColour
& col
)
347 // ----------------------------------------------------------------------------
348 // wxVListBox painting
349 // ----------------------------------------------------------------------------
351 wxCoord
wxVListBox::OnGetRowHeight(size_t line
) const
353 return OnMeasureItem(line
) + 2*m_ptMargins
.y
;
356 void wxVListBox::OnDrawSeparator(wxDC
& WXUNUSED(dc
),
357 wxRect
& WXUNUSED(rect
),
358 size_t WXUNUSED(n
)) const
363 wxVListBox::DoDrawSolidBackground(const wxColour
& col
,
371 // we need to render selected and current items differently
372 const bool isSelected
= IsSelected(n
),
373 isCurrent
= IsCurrent(n
);
374 if ( isSelected
|| isCurrent
)
378 dc
.SetBrush(wxBrush(col
, wxBRUSHSTYLE_SOLID
));
382 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
384 dc
.SetPen(*(isCurrent
? wxBLACK_PEN
: wxTRANSPARENT_PEN
));
385 dc
.DrawRectangle(rect
);
387 //else: do nothing for the normal items
392 void wxVListBox::OnDrawBackground(wxDC
& dc
, const wxRect
& rect
, size_t n
) const
394 // use wxRendererNative for more native look unless we use custom bg colour
395 if ( !DoDrawSolidBackground(m_colBgSel
, dc
, rect
, n
) )
399 flags
|= wxCONTROL_SELECTED
;
401 flags
|= wxCONTROL_CURRENT
;
402 if ( wxWindow::FindFocus() == wx_const_cast(wxVListBox
*, this) )
403 flags
|= wxCONTROL_FOCUSED
;
405 wxRendererNative::Get().DrawItemSelectionRect(
406 wx_const_cast(wxVListBox
*, this), dc
, rect
, flags
);
410 void wxVListBox::OnPaint(wxPaintEvent
& WXUNUSED(event
))
412 wxSize clientSize
= GetClientSize();
414 wxAutoBufferedPaintDC
dc(this);
416 // the update rectangle
417 wxRect rectUpdate
= GetUpdateClientRect();
419 // fill it with background colour
420 dc
.SetBackground(GetBackgroundColour());
423 // the bounding rectangle of the current line
425 rectRow
.width
= clientSize
.x
;
427 // iterate over all visible lines
428 const size_t lineMax
= GetVisibleEnd();
429 for ( size_t line
= GetVisibleBegin(); line
< lineMax
; line
++ )
431 const wxCoord hRow
= OnGetRowHeight(line
);
433 rectRow
.height
= hRow
;
435 // and draw the ones which intersect the update rect
436 if ( rectRow
.Intersects(rectUpdate
) )
438 // don't allow drawing outside of the lines rectangle
439 wxDCClipper
clip(dc
, rectRow
);
441 wxRect rect
= rectRow
;
442 OnDrawBackground(dc
, rect
, line
);
444 OnDrawSeparator(dc
, rect
, line
);
446 rect
.Deflate(m_ptMargins
.x
, m_ptMargins
.y
);
447 OnDrawItem(dc
, rect
, line
);
449 else // no intersection
451 if ( rectRow
.GetTop() > rectUpdate
.GetBottom() )
453 // we are already below the update rect, no need to continue
457 //else: the next line may intersect the update rect
464 void wxVListBox::OnSetOrKillFocus(wxFocusEvent
& WXUNUSED(event
))
466 // we need to repaint the selection when we get the focus since
467 // wxRendererNative in general draws the focused selection differently
468 // from the unfocused selection (see OnDrawItem):
473 // ============================================================================
474 // wxVListBox keyboard/mouse handling
475 // ============================================================================
477 void wxVListBox::DoHandleItemClick(int item
, int flags
)
479 // has anything worth telling the client code about happened?
482 if ( HasMultipleSelection() )
484 // select the iteem clicked?
487 // NB: the keyboard interface we implement here corresponds to
488 // wxLB_EXTENDED rather than wxLB_MULTIPLE but this one makes more
490 if ( flags
& ItemClick_Shift
)
492 if ( m_current
!= wxNOT_FOUND
)
494 if ( m_anchor
== wxNOT_FOUND
)
495 m_anchor
= m_current
;
499 // only the range from the selection anchor to new m_current
504 if ( SelectRange(m_anchor
, item
) )
507 //else: treat it as ordinary click/keypress
509 else // Shift not pressed
513 if ( flags
& ItemClick_Ctrl
)
517 if ( !(flags
& ItemClick_Kbd
) )
521 // the status of the item has definitely changed
524 //else: Ctrl-arrow pressed, don't change selection
526 //else: behave as in single selection case
531 // make the clicked item the only selection
540 // in any case the item should become the current one
541 if ( DoSetCurrent(item
) )
543 if ( !HasMultipleSelection() )
545 // this has also changed the selection for single selection case
552 // notify the user about the selection change
555 //else: nothing changed at all
558 // ----------------------------------------------------------------------------
560 // ----------------------------------------------------------------------------
562 void wxVListBox::OnKeyDown(wxKeyEvent
& event
)
564 // flags for DoHandleItemClick()
565 int flags
= ItemClick_Kbd
;
568 switch ( event
.GetKeyCode() )
575 current
= GetRowCount() - 1;
579 if ( m_current
== (int)GetRowCount() - 1 )
582 current
= m_current
+ 1;
586 if ( m_current
== wxNOT_FOUND
)
587 current
= GetRowCount() - 1;
588 else if ( m_current
!= 0 )
589 current
= m_current
- 1;
590 else // m_current == 0
596 current
= GetVisibleBegin();
600 if ( m_current
== (int)GetVisibleBegin() )
605 current
= GetVisibleBegin();
609 // hack: pressing space should work like a mouse click rather than
610 // like a keyboard arrow press, so trick DoHandleItemClick() in
611 // thinking we were clicked
612 flags
&= ~ItemClick_Kbd
;
618 // Since we are using wxWANTS_CHARS we need to send navigation
619 // events for the tabs on MSW
620 HandleAsNavigationKey(event
);
621 // fall through to default
625 current
= 0; // just to silent the stupid compiler warnings
626 wxUnusedVar(current
);
630 if ( event
.ShiftDown() )
631 flags
|= ItemClick_Shift
;
632 if ( event
.ControlDown() )
633 flags
|= ItemClick_Ctrl
;
635 DoHandleItemClick(current
, flags
);
638 // ----------------------------------------------------------------------------
639 // wxVListBox mouse handling
640 // ----------------------------------------------------------------------------
642 void wxVListBox::OnLeftDown(wxMouseEvent
& event
)
646 int item
= VirtualHitTest(event
.GetPosition().y
);
648 if ( item
!= wxNOT_FOUND
)
651 if ( event
.ShiftDown() )
652 flags
|= ItemClick_Shift
;
654 // under Mac Apple-click is used in the same way as Ctrl-click
657 if ( event
.MetaDown() )
659 if ( event
.ControlDown() )
661 flags
|= ItemClick_Ctrl
;
663 DoHandleItemClick(item
, flags
);
667 void wxVListBox::OnLeftDClick(wxMouseEvent
& eventMouse
)
669 int item
= VirtualHitTest(eventMouse
.GetPosition().y
);
670 if ( item
!= wxNOT_FOUND
)
673 // if item double-clicked was not yet selected, then treat
674 // this event as a left-click instead
675 if ( item
== m_current
)
677 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, GetId());
678 event
.SetEventObject(this);
681 (void)GetEventHandler()->ProcessEvent(event
);
685 OnLeftDown(eventMouse
);
692 // ----------------------------------------------------------------------------
693 // use the same default attributes as wxListBox
694 // ----------------------------------------------------------------------------
698 wxVListBox::GetClassDefaultAttributes(wxWindowVariant variant
)
700 return wxListBox::GetClassDefaultAttributes(variant
);