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 wxRect
wxVListBox::GetItemRect(size_t n
) const
332 // check that this item is visible
333 const size_t lineMax
= GetVisibleEnd();
336 size_t line
= GetVisibleBegin();
342 itemrect
.y
+= itemrect
.height
;
343 itemrect
.height
= OnGetRowHeight(line
);
348 itemrect
.width
= GetClientSize().x
;
353 // ----------------------------------------------------------------------------
354 // wxVListBox appearance parameters
355 // ----------------------------------------------------------------------------
357 void wxVListBox::SetMargins(const wxPoint
& pt
)
359 if ( pt
!= m_ptMargins
)
367 void wxVListBox::SetSelectionBackground(const wxColour
& col
)
372 // ----------------------------------------------------------------------------
373 // wxVListBox painting
374 // ----------------------------------------------------------------------------
376 wxCoord
wxVListBox::OnGetRowHeight(size_t line
) const
378 return OnMeasureItem(line
) + 2*m_ptMargins
.y
;
381 void wxVListBox::OnDrawSeparator(wxDC
& WXUNUSED(dc
),
382 wxRect
& WXUNUSED(rect
),
383 size_t WXUNUSED(n
)) const
388 wxVListBox::DoDrawSolidBackground(const wxColour
& col
,
396 // we need to render selected and current items differently
397 const bool isSelected
= IsSelected(n
),
398 isCurrent
= IsCurrent(n
);
399 if ( isSelected
|| isCurrent
)
403 dc
.SetBrush(wxBrush(col
, wxBRUSHSTYLE_SOLID
));
407 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
409 dc
.SetPen(*(isCurrent
? wxBLACK_PEN
: wxTRANSPARENT_PEN
));
410 dc
.DrawRectangle(rect
);
412 //else: do nothing for the normal items
417 void wxVListBox::OnDrawBackground(wxDC
& dc
, const wxRect
& rect
, size_t n
) const
419 // use wxRendererNative for more native look unless we use custom bg colour
420 if ( !DoDrawSolidBackground(m_colBgSel
, dc
, rect
, n
) )
424 flags
|= wxCONTROL_SELECTED
;
426 flags
|= wxCONTROL_CURRENT
;
427 if ( wxWindow::FindFocus() == wx_const_cast(wxVListBox
*, this) )
428 flags
|= wxCONTROL_FOCUSED
;
430 wxRendererNative::Get().DrawItemSelectionRect(
431 wx_const_cast(wxVListBox
*, this), dc
, rect
, flags
);
435 void wxVListBox::OnPaint(wxPaintEvent
& WXUNUSED(event
))
437 wxSize clientSize
= GetClientSize();
439 wxAutoBufferedPaintDC
dc(this);
441 // the update rectangle
442 wxRect rectUpdate
= GetUpdateClientRect();
444 // fill it with background colour
445 dc
.SetBackground(GetBackgroundColour());
448 // the bounding rectangle of the current line
450 rectRow
.width
= clientSize
.x
;
452 // iterate over all visible lines
453 const size_t lineMax
= GetVisibleEnd();
454 for ( size_t line
= GetVisibleBegin(); line
< lineMax
; line
++ )
456 const wxCoord hRow
= OnGetRowHeight(line
);
458 rectRow
.height
= hRow
;
460 // and draw the ones which intersect the update rect
461 if ( rectRow
.Intersects(rectUpdate
) )
463 // don't allow drawing outside of the lines rectangle
464 wxDCClipper
clip(dc
, rectRow
);
466 wxRect rect
= rectRow
;
467 OnDrawBackground(dc
, rect
, line
);
469 OnDrawSeparator(dc
, rect
, line
);
471 rect
.Deflate(m_ptMargins
.x
, m_ptMargins
.y
);
472 OnDrawItem(dc
, rect
, line
);
474 else // no intersection
476 if ( rectRow
.GetTop() > rectUpdate
.GetBottom() )
478 // we are already below the update rect, no need to continue
482 //else: the next line may intersect the update rect
489 void wxVListBox::OnSetOrKillFocus(wxFocusEvent
& WXUNUSED(event
))
491 // we need to repaint the selection when we get the focus since
492 // wxRendererNative in general draws the focused selection differently
493 // from the unfocused selection (see OnDrawItem):
498 // ============================================================================
499 // wxVListBox keyboard/mouse handling
500 // ============================================================================
502 void wxVListBox::DoHandleItemClick(int item
, int flags
)
504 // has anything worth telling the client code about happened?
507 if ( HasMultipleSelection() )
509 // select the iteem clicked?
512 // NB: the keyboard interface we implement here corresponds to
513 // wxLB_EXTENDED rather than wxLB_MULTIPLE but this one makes more
515 if ( flags
& ItemClick_Shift
)
517 if ( m_current
!= wxNOT_FOUND
)
519 if ( m_anchor
== wxNOT_FOUND
)
520 m_anchor
= m_current
;
524 // only the range from the selection anchor to new m_current
529 if ( SelectRange(m_anchor
, item
) )
532 //else: treat it as ordinary click/keypress
534 else // Shift not pressed
538 if ( flags
& ItemClick_Ctrl
)
542 if ( !(flags
& ItemClick_Kbd
) )
546 // the status of the item has definitely changed
549 //else: Ctrl-arrow pressed, don't change selection
551 //else: behave as in single selection case
556 // make the clicked item the only selection
565 // in any case the item should become the current one
566 if ( DoSetCurrent(item
) )
568 if ( !HasMultipleSelection() )
570 // this has also changed the selection for single selection case
577 // notify the user about the selection change
580 //else: nothing changed at all
583 // ----------------------------------------------------------------------------
585 // ----------------------------------------------------------------------------
587 void wxVListBox::OnKeyDown(wxKeyEvent
& event
)
589 // flags for DoHandleItemClick()
590 int flags
= ItemClick_Kbd
;
593 switch ( event
.GetKeyCode() )
600 current
= GetRowCount() - 1;
604 if ( m_current
== (int)GetRowCount() - 1 )
607 current
= m_current
+ 1;
611 if ( m_current
== wxNOT_FOUND
)
612 current
= GetRowCount() - 1;
613 else if ( m_current
!= 0 )
614 current
= m_current
- 1;
615 else // m_current == 0
621 current
= GetVisibleBegin();
625 if ( m_current
== (int)GetVisibleBegin() )
630 current
= GetVisibleBegin();
634 // hack: pressing space should work like a mouse click rather than
635 // like a keyboard arrow press, so trick DoHandleItemClick() in
636 // thinking we were clicked
637 flags
&= ~ItemClick_Kbd
;
643 // Since we are using wxWANTS_CHARS we need to send navigation
644 // events for the tabs on MSW
645 HandleAsNavigationKey(event
);
646 // fall through to default
650 current
= 0; // just to silent the stupid compiler warnings
651 wxUnusedVar(current
);
655 if ( event
.ShiftDown() )
656 flags
|= ItemClick_Shift
;
657 if ( event
.ControlDown() )
658 flags
|= ItemClick_Ctrl
;
660 DoHandleItemClick(current
, flags
);
663 // ----------------------------------------------------------------------------
664 // wxVListBox mouse handling
665 // ----------------------------------------------------------------------------
667 void wxVListBox::OnLeftDown(wxMouseEvent
& event
)
671 int item
= VirtualHitTest(event
.GetPosition().y
);
673 if ( item
!= wxNOT_FOUND
)
676 if ( event
.ShiftDown() )
677 flags
|= ItemClick_Shift
;
679 // under Mac Apple-click is used in the same way as Ctrl-click
682 if ( event
.MetaDown() )
684 if ( event
.ControlDown() )
686 flags
|= ItemClick_Ctrl
;
688 DoHandleItemClick(item
, flags
);
692 void wxVListBox::OnLeftDClick(wxMouseEvent
& eventMouse
)
694 int item
= VirtualHitTest(eventMouse
.GetPosition().y
);
695 if ( item
!= wxNOT_FOUND
)
698 // if item double-clicked was not yet selected, then treat
699 // this event as a left-click instead
700 if ( item
== m_current
)
702 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, GetId());
703 event
.SetEventObject(this);
706 (void)GetEventHandler()->ProcessEvent(event
);
710 OnLeftDown(eventMouse
);
717 // ----------------------------------------------------------------------------
718 // use the same default attributes as wxListBox
719 // ----------------------------------------------------------------------------
723 wxVListBox::GetClassDefaultAttributes(wxWindowVariant variant
)
725 return wxListBox::GetClassDefaultAttributes(variant
);