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
)
55 EVT_SIZE(wxVListBox::OnSize
)
58 // ============================================================================
60 // ============================================================================
62 IMPLEMENT_ABSTRACT_CLASS(wxVListBox
, wxVScrolledWindow
)
64 // ----------------------------------------------------------------------------
65 // wxVListBox creation
66 // ----------------------------------------------------------------------------
68 void wxVListBox::Init()
71 m_anchor
= wxNOT_FOUND
;
75 bool wxVListBox::Create(wxWindow
*parent
,
83 if ( (style
& wxBORDER_MASK
) == wxDEFAULT
)
84 style
|= wxBORDER_THEME
;
87 style
|= wxWANTS_CHARS
| wxFULL_REPAINT_ON_RESIZE
;
88 if ( !wxVScrolledWindow::Create(parent
, id
, pos
, size
, style
, name
) )
91 if ( style
& wxLB_MULTIPLE
)
92 m_selStore
= new wxSelectionStore
;
94 // make sure the native widget has the right colour since we do
95 // transparent drawing by default
96 SetBackgroundColour(GetBackgroundColour());
98 // leave m_colBgSel in an invalid state: it means for OnDrawBackground()
99 // to use wxRendererNative instead of painting selection bg ourselves
100 m_colBgSel
= wxNullColour
;
102 // flicker-free drawing requires this
103 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
108 wxVListBox::~wxVListBox()
113 void wxVListBox::SetItemCount(size_t count
)
115 // don't leave the current index invalid
116 if ( m_current
!= wxNOT_FOUND
&& (size_t)m_current
>= count
)
117 m_current
= count
- 1; // also ok when count == 0 as wxNOT_FOUND == -1
121 // tell the selection store that our number of items has changed
122 m_selStore
->SetItemCount(count
);
128 // ----------------------------------------------------------------------------
129 // selection handling
130 // ----------------------------------------------------------------------------
132 bool wxVListBox::IsSelected(size_t line
) const
134 return m_selStore
? m_selStore
->IsSelected(line
) : (int)line
== m_current
;
137 bool wxVListBox::Select(size_t item
, bool select
)
139 wxCHECK_MSG( m_selStore
, false,
140 _T("Select() may only be used with multiselection listbox") );
142 wxCHECK_MSG( item
< GetItemCount(), false,
143 _T("Select(): invalid item index") );
145 bool changed
= m_selStore
->SelectItem(item
, select
);
148 // selection really changed
157 bool wxVListBox::SelectRange(size_t from
, size_t to
)
159 wxCHECK_MSG( m_selStore
, false,
160 _T("SelectRange() may only be used with multiselection listbox") );
162 // make sure items are in correct order
170 wxCHECK_MSG( to
< GetItemCount(), false,
171 _T("SelectRange(): invalid item index") );
174 if ( !m_selStore
->SelectRange(from
, to
, true, &changed
) )
176 // too many items have changed, we didn't record them in changed array
177 // so we have no choice but to refresh everything between from and to
178 RefreshRows(from
, to
);
180 else // we've got the indices of the changed items
182 const size_t count
= changed
.GetCount();
189 // refresh just the lines which have really changed
190 for ( size_t n
= 0; n
< count
; n
++ )
192 RefreshRow(changed
[n
]);
200 bool wxVListBox::DoSelectAll(bool select
)
202 wxCHECK_MSG( m_selStore
, false,
203 _T("SelectAll may only be used with multiselection listbox") );
205 size_t count
= GetItemCount();
209 if ( !m_selStore
->SelectRange(0, count
- 1, select
) ||
222 bool wxVListBox::DoSetCurrent(int current
)
224 wxASSERT_MSG( current
== wxNOT_FOUND
||
225 (current
>= 0 && (size_t)current
< GetItemCount()),
226 _T("wxVListBox::DoSetCurrent(): invalid item index") );
228 if ( current
== m_current
)
234 if ( m_current
!= wxNOT_FOUND
)
235 RefreshRow(m_current
);
239 if ( m_current
!= wxNOT_FOUND
)
241 // if the line is not visible at all, we scroll it into view but we
242 // don't need to refresh it -- it will be redrawn anyhow
243 if ( !IsVisible(m_current
) )
245 ScrollToRow(m_current
);
247 else // line is at least partly visible
249 // it is, indeed, only partly visible, so scroll it into view to
250 // make it entirely visible
251 while ( (size_t)m_current
+ 1 == GetVisibleRowsEnd() &&
252 ScrollToRow(GetVisibleBegin() + 1) ) ;
254 // but in any case refresh it as even if it was only partly visible
255 // before we need to redraw it entirely as its background changed
256 RefreshRow(m_current
);
263 void wxVListBox::SendSelectedEvent()
265 wxASSERT_MSG( m_current
!= wxNOT_FOUND
,
266 _T("SendSelectedEvent() shouldn't be called") );
268 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, GetId());
269 event
.SetEventObject(this);
270 event
.SetInt(m_current
);
272 (void)GetEventHandler()->ProcessEvent(event
);
275 void wxVListBox::SetSelection(int selection
)
277 wxCHECK_RET( selection
== wxNOT_FOUND
||
278 (selection
>= 0 && (size_t)selection
< GetItemCount()),
279 _T("wxVListBox::SetSelection(): invalid item index") );
281 if ( HasMultipleSelection() )
283 if (selection
!= wxNOT_FOUND
)
287 m_anchor
= selection
;
290 DoSetCurrent(selection
);
293 size_t wxVListBox::GetSelectedCount() const
295 return m_selStore
? m_selStore
->GetSelectedCount()
296 : m_current
== wxNOT_FOUND
? 0 : 1;
299 int wxVListBox::GetFirstSelected(unsigned long& cookie
) const
303 return GetNextSelected(cookie
);
306 int wxVListBox::GetNextSelected(unsigned long& cookie
) const
308 wxCHECK_MSG( m_selStore
, wxNOT_FOUND
,
309 _T("GetFirst/NextSelected() may only be used with multiselection listboxes") );
311 while ( cookie
< GetItemCount() )
313 if ( IsSelected(cookie
++) )
320 void wxVListBox::RefreshSelected()
322 // only refresh those items which are currently visible and selected:
323 for ( size_t n
= GetVisibleBegin(), end
= GetVisibleEnd(); n
< end
; n
++ )
330 wxRect
wxVListBox::GetItemRect(size_t n
) const
334 // check that this item is visible
335 const size_t lineMax
= GetVisibleEnd();
338 size_t line
= GetVisibleBegin();
344 itemrect
.y
+= itemrect
.height
;
345 itemrect
.height
= OnGetRowHeight(line
);
350 itemrect
.width
= GetClientSize().x
;
355 // ----------------------------------------------------------------------------
356 // wxVListBox appearance parameters
357 // ----------------------------------------------------------------------------
359 void wxVListBox::SetMargins(const wxPoint
& pt
)
361 if ( pt
!= m_ptMargins
)
369 void wxVListBox::SetSelectionBackground(const wxColour
& col
)
374 // ----------------------------------------------------------------------------
375 // wxVListBox painting
376 // ----------------------------------------------------------------------------
378 wxCoord
wxVListBox::OnGetRowHeight(size_t line
) const
380 return OnMeasureItem(line
) + 2*m_ptMargins
.y
;
383 void wxVListBox::OnDrawSeparator(wxDC
& WXUNUSED(dc
),
384 wxRect
& WXUNUSED(rect
),
385 size_t WXUNUSED(n
)) const
390 wxVListBox::DoDrawSolidBackground(const wxColour
& col
,
398 // we need to render selected and current items differently
399 const bool isSelected
= IsSelected(n
),
400 isCurrent
= IsCurrent(n
);
401 if ( isSelected
|| isCurrent
)
405 dc
.SetBrush(wxBrush(col
, wxBRUSHSTYLE_SOLID
));
409 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
411 dc
.SetPen(*(isCurrent
? wxBLACK_PEN
: wxTRANSPARENT_PEN
));
412 dc
.DrawRectangle(rect
);
414 //else: do nothing for the normal items
419 void wxVListBox::OnDrawBackground(wxDC
& dc
, const wxRect
& rect
, size_t n
) const
421 // use wxRendererNative for more native look unless we use custom bg colour
422 if ( !DoDrawSolidBackground(m_colBgSel
, dc
, rect
, n
) )
426 flags
|= wxCONTROL_SELECTED
;
428 flags
|= wxCONTROL_CURRENT
;
429 if ( wxWindow::FindFocus() == wx_const_cast(wxVListBox
*, this) )
430 flags
|= wxCONTROL_FOCUSED
;
432 wxRendererNative::Get().DrawItemSelectionRect(
433 wx_const_cast(wxVListBox
*, this), dc
, rect
, flags
);
437 void wxVListBox::OnPaint(wxPaintEvent
& WXUNUSED(event
))
439 wxSize clientSize
= GetClientSize();
441 wxAutoBufferedPaintDC
dc(this);
443 // the update rectangle
444 wxRect rectUpdate
= GetUpdateClientRect();
446 // fill it with background colour
447 dc
.SetBackground(GetBackgroundColour());
450 // the bounding rectangle of the current line
452 rectRow
.width
= clientSize
.x
;
454 // iterate over all visible lines
455 const size_t lineMax
= GetVisibleEnd();
456 for ( size_t line
= GetVisibleBegin(); line
< lineMax
; line
++ )
458 const wxCoord hRow
= OnGetRowHeight(line
);
460 rectRow
.height
= hRow
;
462 // and draw the ones which intersect the update rect
463 if ( rectRow
.Intersects(rectUpdate
) )
465 // don't allow drawing outside of the lines rectangle
466 wxDCClipper
clip(dc
, rectRow
);
468 wxRect rect
= rectRow
;
469 OnDrawBackground(dc
, rect
, line
);
471 OnDrawSeparator(dc
, rect
, line
);
473 rect
.Deflate(m_ptMargins
.x
, m_ptMargins
.y
);
474 OnDrawItem(dc
, rect
, line
);
476 else // no intersection
478 if ( rectRow
.GetTop() > rectUpdate
.GetBottom() )
480 // we are already below the update rect, no need to continue
484 //else: the next line may intersect the update rect
491 void wxVListBox::OnSetOrKillFocus(wxFocusEvent
& WXUNUSED(event
))
493 // we need to repaint the selection when we get the focus since
494 // wxRendererNative in general draws the focused selection differently
495 // from the unfocused selection (see OnDrawItem):
499 void wxVListBox::OnSize(wxSizeEvent
& event
)
505 // ============================================================================
506 // wxVListBox keyboard/mouse handling
507 // ============================================================================
509 void wxVListBox::DoHandleItemClick(int item
, int flags
)
511 // has anything worth telling the client code about happened?
514 if ( HasMultipleSelection() )
516 // select the iteem clicked?
519 // NB: the keyboard interface we implement here corresponds to
520 // wxLB_EXTENDED rather than wxLB_MULTIPLE but this one makes more
522 if ( flags
& ItemClick_Shift
)
524 if ( m_current
!= wxNOT_FOUND
)
526 if ( m_anchor
== wxNOT_FOUND
)
527 m_anchor
= m_current
;
531 // only the range from the selection anchor to new m_current
536 if ( SelectRange(m_anchor
, item
) )
539 //else: treat it as ordinary click/keypress
541 else // Shift not pressed
545 if ( flags
& ItemClick_Ctrl
)
549 if ( !(flags
& ItemClick_Kbd
) )
553 // the status of the item has definitely changed
556 //else: Ctrl-arrow pressed, don't change selection
558 //else: behave as in single selection case
563 // make the clicked item the only selection
572 // in any case the item should become the current one
573 if ( DoSetCurrent(item
) )
575 if ( !HasMultipleSelection() )
577 // this has also changed the selection for single selection case
584 // notify the user about the selection change
587 //else: nothing changed at all
590 // ----------------------------------------------------------------------------
592 // ----------------------------------------------------------------------------
594 void wxVListBox::OnKeyDown(wxKeyEvent
& event
)
596 // flags for DoHandleItemClick()
597 int flags
= ItemClick_Kbd
;
600 switch ( event
.GetKeyCode() )
607 current
= GetRowCount() - 1;
611 if ( m_current
== (int)GetRowCount() - 1 )
614 current
= m_current
+ 1;
618 if ( m_current
== wxNOT_FOUND
)
619 current
= GetRowCount() - 1;
620 else if ( m_current
!= 0 )
621 current
= m_current
- 1;
622 else // m_current == 0
628 current
= GetVisibleBegin();
632 if ( m_current
== (int)GetVisibleBegin() )
637 current
= GetVisibleBegin();
641 // hack: pressing space should work like a mouse click rather than
642 // like a keyboard arrow press, so trick DoHandleItemClick() in
643 // thinking we were clicked
644 flags
&= ~ItemClick_Kbd
;
650 // Since we are using wxWANTS_CHARS we need to send navigation
651 // events for the tabs on MSW
652 HandleAsNavigationKey(event
);
653 // fall through to default
657 current
= 0; // just to silent the stupid compiler warnings
658 wxUnusedVar(current
);
662 if ( event
.ShiftDown() )
663 flags
|= ItemClick_Shift
;
664 if ( event
.ControlDown() )
665 flags
|= ItemClick_Ctrl
;
667 DoHandleItemClick(current
, flags
);
670 // ----------------------------------------------------------------------------
671 // wxVListBox mouse handling
672 // ----------------------------------------------------------------------------
674 void wxVListBox::OnLeftDown(wxMouseEvent
& event
)
678 int item
= VirtualHitTest(event
.GetPosition().y
);
680 if ( item
!= wxNOT_FOUND
)
683 if ( event
.ShiftDown() )
684 flags
|= ItemClick_Shift
;
686 // under Mac Apple-click is used in the same way as Ctrl-click
689 if ( event
.MetaDown() )
691 if ( event
.ControlDown() )
693 flags
|= ItemClick_Ctrl
;
695 DoHandleItemClick(item
, flags
);
699 void wxVListBox::OnLeftDClick(wxMouseEvent
& eventMouse
)
701 int item
= VirtualHitTest(eventMouse
.GetPosition().y
);
702 if ( item
!= wxNOT_FOUND
)
705 // if item double-clicked was not yet selected, then treat
706 // this event as a left-click instead
707 if ( item
== m_current
)
709 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, GetId());
710 event
.SetEventObject(this);
713 (void)GetEventHandler()->ProcessEvent(event
);
717 OnLeftDown(eventMouse
);
724 // ----------------------------------------------------------------------------
725 // use the same default attributes as wxListBox
726 // ----------------------------------------------------------------------------
730 wxVListBox::GetClassDefaultAttributes(wxWindowVariant variant
)
732 return wxListBox::GetClassDefaultAttributes(variant
);