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
)
115 // tell the selection store that our number of items has changed
116 m_selStore
->SetItemCount(count
);
122 // ----------------------------------------------------------------------------
123 // selection handling
124 // ----------------------------------------------------------------------------
126 bool wxVListBox::IsSelected(size_t line
) const
128 return m_selStore
? m_selStore
->IsSelected(line
) : (int)line
== m_current
;
131 bool wxVListBox::Select(size_t item
, bool select
)
133 wxCHECK_MSG( m_selStore
, false,
134 _T("Select() may only be used with multiselection listbox") );
136 wxCHECK_MSG( item
< GetItemCount(), false,
137 _T("Select(): invalid item index") );
139 bool changed
= m_selStore
->SelectItem(item
, select
);
142 // selection really changed
151 bool wxVListBox::SelectRange(size_t from
, size_t to
)
153 wxCHECK_MSG( m_selStore
, false,
154 _T("SelectRange() may only be used with multiselection listbox") );
156 // make sure items are in correct order
164 wxCHECK_MSG( to
< GetItemCount(), false,
165 _T("SelectRange(): invalid item index") );
168 if ( !m_selStore
->SelectRange(from
, to
, true, &changed
) )
170 // too many items have changed, we didn't record them in changed array
171 // so we have no choice but to refresh everything between from and to
172 RefreshRows(from
, to
);
174 else // we've got the indices of the changed items
176 const size_t count
= changed
.GetCount();
183 // refresh just the lines which have really changed
184 for ( size_t n
= 0; n
< count
; n
++ )
186 RefreshRow(changed
[n
]);
194 bool wxVListBox::DoSelectAll(bool select
)
196 wxCHECK_MSG( m_selStore
, false,
197 _T("SelectAll may only be used with multiselection listbox") );
199 size_t count
= GetItemCount();
203 if ( !m_selStore
->SelectRange(0, count
- 1, select
) ||
216 bool wxVListBox::DoSetCurrent(int current
)
218 wxASSERT_MSG( current
== wxNOT_FOUND
||
219 (current
>= 0 && (size_t)current
< GetItemCount()),
220 _T("wxVListBox::DoSetCurrent(): invalid item index") );
222 if ( current
== m_current
)
228 if ( m_current
!= wxNOT_FOUND
)
229 RefreshRow(m_current
);
233 if ( m_current
!= wxNOT_FOUND
)
235 // if the line is not visible at all, we scroll it into view but we
236 // don't need to refresh it -- it will be redrawn anyhow
237 if ( !IsVisible(m_current
) )
239 ScrollToRow(m_current
);
241 else // line is at least partly visible
243 // it is, indeed, only partly visible, so scroll it into view to
244 // make it entirely visible
245 while ( (size_t)m_current
+ 1 == GetVisibleRowsEnd() &&
246 ScrollToRow(GetVisibleBegin() + 1) ) ;
248 // but in any case refresh it as even if it was only partly visible
249 // before we need to redraw it entirely as its background changed
250 RefreshRow(m_current
);
257 void wxVListBox::SendSelectedEvent()
259 wxASSERT_MSG( m_current
!= wxNOT_FOUND
,
260 _T("SendSelectedEvent() shouldn't be called") );
262 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, GetId());
263 event
.SetEventObject(this);
264 event
.SetInt(m_current
);
266 (void)GetEventHandler()->ProcessEvent(event
);
269 void wxVListBox::SetSelection(int selection
)
271 wxCHECK_RET( selection
== wxNOT_FOUND
||
272 (selection
>= 0 && (size_t)selection
< GetItemCount()),
273 _T("wxVListBox::SetSelection(): invalid item index") );
275 if ( HasMultipleSelection() )
277 if (selection
!= wxNOT_FOUND
)
281 m_anchor
= selection
;
284 DoSetCurrent(selection
);
287 size_t wxVListBox::GetSelectedCount() const
289 return m_selStore
? m_selStore
->GetSelectedCount()
290 : m_current
== wxNOT_FOUND
? 0 : 1;
293 int wxVListBox::GetFirstSelected(unsigned long& cookie
) const
297 return GetNextSelected(cookie
);
300 int wxVListBox::GetNextSelected(unsigned long& cookie
) const
302 wxCHECK_MSG( m_selStore
, wxNOT_FOUND
,
303 _T("GetFirst/NextSelected() may only be used with multiselection listboxes") );
305 while ( cookie
< GetItemCount() )
307 if ( IsSelected(cookie
++) )
314 void wxVListBox::RefreshSelected()
316 // only refresh those items which are currently visible and selected:
317 for ( size_t n
= GetVisibleBegin(), end
= GetVisibleEnd(); n
< end
; n
++ )
324 // ----------------------------------------------------------------------------
325 // wxVListBox appearance parameters
326 // ----------------------------------------------------------------------------
328 void wxVListBox::SetMargins(const wxPoint
& pt
)
330 if ( pt
!= m_ptMargins
)
338 void wxVListBox::SetSelectionBackground(const wxColour
& col
)
343 // ----------------------------------------------------------------------------
344 // wxVListBox painting
345 // ----------------------------------------------------------------------------
347 wxCoord
wxVListBox::OnGetRowHeight(size_t line
) const
349 return OnMeasureItem(line
) + 2*m_ptMargins
.y
;
352 void wxVListBox::OnDrawSeparator(wxDC
& WXUNUSED(dc
),
353 wxRect
& WXUNUSED(rect
),
354 size_t WXUNUSED(n
)) const
359 wxVListBox::DoDrawSolidBackground(const wxColour
& col
,
367 // we need to render selected and current items differently
368 const bool isSelected
= IsSelected(n
),
369 isCurrent
= IsCurrent(n
);
370 if ( isSelected
|| isCurrent
)
374 dc
.SetBrush(wxBrush(col
, wxBRUSHSTYLE_SOLID
));
378 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
380 dc
.SetPen(*(isCurrent
? wxBLACK_PEN
: wxTRANSPARENT_PEN
));
381 dc
.DrawRectangle(rect
);
383 //else: do nothing for the normal items
388 void wxVListBox::OnDrawBackground(wxDC
& dc
, const wxRect
& rect
, size_t n
) const
390 // use wxRendererNative for more native look unless we use custom bg colour
391 if ( !DoDrawSolidBackground(m_colBgSel
, dc
, rect
, n
) )
395 flags
|= wxCONTROL_SELECTED
;
397 flags
|= wxCONTROL_CURRENT
;
398 if ( wxWindow::FindFocus() == wx_const_cast(wxVListBox
*, this) )
399 flags
|= wxCONTROL_FOCUSED
;
401 wxRendererNative::Get().DrawItemSelectionRect(
402 wx_const_cast(wxVListBox
*, this), dc
, rect
, flags
);
406 void wxVListBox::OnPaint(wxPaintEvent
& WXUNUSED(event
))
408 wxSize clientSize
= GetClientSize();
410 wxAutoBufferedPaintDC
dc(this);
412 // the update rectangle
413 wxRect rectUpdate
= GetUpdateClientRect();
415 // fill it with background colour
416 dc
.SetBackground(GetBackgroundColour());
419 // the bounding rectangle of the current line
421 rectRow
.width
= clientSize
.x
;
423 // iterate over all visible lines
424 const size_t lineMax
= GetVisibleEnd();
425 for ( size_t line
= GetVisibleBegin(); line
< lineMax
; line
++ )
427 const wxCoord hRow
= OnGetRowHeight(line
);
429 rectRow
.height
= hRow
;
431 // and draw the ones which intersect the update rect
432 if ( rectRow
.Intersects(rectUpdate
) )
434 // don't allow drawing outside of the lines rectangle
435 wxDCClipper
clip(dc
, rectRow
);
437 wxRect rect
= rectRow
;
438 OnDrawBackground(dc
, rect
, line
);
440 OnDrawSeparator(dc
, rect
, line
);
442 rect
.Deflate(m_ptMargins
.x
, m_ptMargins
.y
);
443 OnDrawItem(dc
, rect
, line
);
445 else // no intersection
447 if ( rectRow
.GetTop() > rectUpdate
.GetBottom() )
449 // we are already below the update rect, no need to continue
453 //else: the next line may intersect the update rect
460 void wxVListBox::OnSetOrKillFocus(wxFocusEvent
& WXUNUSED(event
))
462 // we need to repaint the selection when we get the focus since
463 // wxRendererNative in general draws the focused selection differently
464 // from the unfocused selection (see OnDrawItem):
469 // ============================================================================
470 // wxVListBox keyboard/mouse handling
471 // ============================================================================
473 void wxVListBox::DoHandleItemClick(int item
, int flags
)
475 // has anything worth telling the client code about happened?
478 if ( HasMultipleSelection() )
480 // select the iteem clicked?
483 // NB: the keyboard interface we implement here corresponds to
484 // wxLB_EXTENDED rather than wxLB_MULTIPLE but this one makes more
486 if ( flags
& ItemClick_Shift
)
488 if ( m_current
!= wxNOT_FOUND
)
490 if ( m_anchor
== wxNOT_FOUND
)
491 m_anchor
= m_current
;
495 // only the range from the selection anchor to new m_current
500 if ( SelectRange(m_anchor
, item
) )
503 //else: treat it as ordinary click/keypress
505 else // Shift not pressed
509 if ( flags
& ItemClick_Ctrl
)
513 if ( !(flags
& ItemClick_Kbd
) )
517 // the status of the item has definitely changed
520 //else: Ctrl-arrow pressed, don't change selection
522 //else: behave as in single selection case
527 // make the clicked item the only selection
536 // in any case the item should become the current one
537 if ( DoSetCurrent(item
) )
539 if ( !HasMultipleSelection() )
541 // this has also changed the selection for single selection case
548 // notify the user about the selection change
551 //else: nothing changed at all
554 // ----------------------------------------------------------------------------
556 // ----------------------------------------------------------------------------
558 void wxVListBox::OnKeyDown(wxKeyEvent
& event
)
560 // flags for DoHandleItemClick()
561 int flags
= ItemClick_Kbd
;
564 switch ( event
.GetKeyCode() )
571 current
= GetRowCount() - 1;
575 if ( m_current
== (int)GetRowCount() - 1 )
578 current
= m_current
+ 1;
582 if ( m_current
== wxNOT_FOUND
)
583 current
= GetRowCount() - 1;
584 else if ( m_current
!= 0 )
585 current
= m_current
- 1;
586 else // m_current == 0
592 current
= GetVisibleBegin();
596 if ( m_current
== (int)GetVisibleBegin() )
601 current
= GetVisibleBegin();
605 // hack: pressing space should work like a mouse click rather than
606 // like a keyboard arrow press, so trick DoHandleItemClick() in
607 // thinking we were clicked
608 flags
&= ~ItemClick_Kbd
;
614 // Since we are using wxWANTS_CHARS we need to send navigation
615 // events for the tabs on MSW
616 HandleAsNavigationKey(event
);
617 // fall through to default
621 current
= 0; // just to silent the stupid compiler warnings
622 wxUnusedVar(current
);
626 if ( event
.ShiftDown() )
627 flags
|= ItemClick_Shift
;
628 if ( event
.ControlDown() )
629 flags
|= ItemClick_Ctrl
;
631 DoHandleItemClick(current
, flags
);
634 // ----------------------------------------------------------------------------
635 // wxVListBox mouse handling
636 // ----------------------------------------------------------------------------
638 void wxVListBox::OnLeftDown(wxMouseEvent
& event
)
642 int item
= VirtualHitTest(event
.GetPosition().y
);
644 if ( item
!= wxNOT_FOUND
)
647 if ( event
.ShiftDown() )
648 flags
|= ItemClick_Shift
;
650 // under Mac Apple-click is used in the same way as Ctrl-click
653 if ( event
.MetaDown() )
655 if ( event
.ControlDown() )
657 flags
|= ItemClick_Ctrl
;
659 DoHandleItemClick(item
, flags
);
663 void wxVListBox::OnLeftDClick(wxMouseEvent
& eventMouse
)
665 int item
= VirtualHitTest(eventMouse
.GetPosition().y
);
666 if ( item
!= wxNOT_FOUND
)
669 // if item double-clicked was not yet selected, then treat
670 // this event as a left-click instead
671 if ( item
== m_current
)
673 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, GetId());
674 event
.SetEventObject(this);
677 (void)GetEventHandler()->ProcessEvent(event
);
681 OnLeftDown(eventMouse
);
688 // ----------------------------------------------------------------------------
689 // use the same default attributes as wxListBox
690 // ----------------------------------------------------------------------------
694 wxVListBox::GetClassDefaultAttributes(wxWindowVariant variant
)
696 return wxListBox::GetClassDefaultAttributes(variant
);