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 IMPLEMENT_ABSTRACT_CLASS(wxVListBox
, wxVScrolledWindow
)
53 // ----------------------------------------------------------------------------
54 // wxVListBox creation
55 // ----------------------------------------------------------------------------
57 void wxVListBox::Init()
60 m_anchor
= wxNOT_FOUND
;
64 bool wxVListBox::Create(wxWindow
*parent
,
71 style
|= wxWANTS_CHARS
;
72 if ( !wxVScrolledWindow::Create(parent
, id
, pos
, size
, style
, name
) )
75 if ( style
& wxLB_MULTIPLE
)
76 m_selStore
= new wxSelectionStore
;
78 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
));
79 SetForegroundColour(parent
->GetForegroundColour());
81 // ensure that the font actually changes and is set.
83 SetFont(parent
->GetFont());
85 m_colBgSel
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
90 wxVListBox::~wxVListBox()
95 void wxVListBox::SetItemCount(size_t count
)
99 // tell the selection store that our number of items has changed
100 m_selStore
->SetItemCount(count
);
106 // ----------------------------------------------------------------------------
107 // selection handling
108 // ----------------------------------------------------------------------------
110 bool wxVListBox::IsSelected(size_t line
) const
112 return m_selStore
? m_selStore
->IsSelected(line
) : (int)line
== m_current
;
115 bool wxVListBox::Select(size_t item
, bool select
)
117 wxCHECK_MSG( m_selStore
, false,
118 _T("Select() may only be used with multiselection listbox") );
120 wxCHECK_MSG( item
< GetItemCount(), false,
121 _T("Select(): invalid item index") );
123 bool changed
= m_selStore
->SelectItem(item
, select
);
126 // selection really changed
135 bool wxVListBox::SelectRange(size_t from
, size_t to
)
137 wxCHECK_MSG( m_selStore
, false,
138 _T("SelectRange() may only be used with multiselection listbox") );
140 // make sure items are in correct order
148 wxCHECK_MSG( to
< GetItemCount(), false,
149 _T("SelectRange(): invalid item index") );
152 if ( !m_selStore
->SelectRange(from
, to
, true, &changed
) )
154 // too many items have changed, we didn't record them in changed array
155 // so we have no choice but to refresh everything between from and to
156 RefreshLines(from
, to
);
158 else // we've got the indices of the changed items
160 const size_t count
= changed
.GetCount();
167 // refresh just the lines which have really changed
168 for ( size_t n
= 0; n
< count
; n
++ )
170 RefreshLine(changed
[n
]);
178 bool wxVListBox::DoSelectAll(bool select
)
180 wxCHECK_MSG( m_selStore
, false,
181 _T("SelectAll may only be used with multiselection listbox") );
183 size_t count
= GetItemCount();
187 if ( !m_selStore
->SelectRange(0, count
- 1, select
) ||
200 bool wxVListBox::DoSetCurrent(int current
)
202 wxASSERT_MSG( current
== wxNOT_FOUND
||
203 (current
>= 0 && (size_t)current
< GetItemCount()),
204 _T("wxVListBox::DoSetCurrent(): invalid item index") );
206 if ( current
== m_current
)
212 if ( m_current
!= wxNOT_FOUND
)
213 RefreshLine(m_current
);
217 if ( m_current
!= wxNOT_FOUND
)
219 // if the line is not visible at all, we scroll it into view but we
220 // don't need to refresh it -- it will be redrawn anyhow
221 if ( !IsVisible(m_current
) )
223 ScrollToLine(m_current
);
225 else // line is at least partly visible
227 // it is, indeed, only partly visible, so scroll it into view to
228 // make it entirely visible
229 if ( (size_t)m_current
== GetLastVisibleLine() )
231 ScrollToLine(m_current
);
234 // but in any case refresh it as even if it was only partly visible
235 // before we need to redraw it entirely as its background changed
236 RefreshLine(m_current
);
243 void wxVListBox::SendSelectedEvent()
245 wxASSERT_MSG( m_current
!= wxNOT_FOUND
,
246 _T("SendSelectedEvent() shouldn't be called") );
248 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, GetId());
249 event
.SetEventObject(this);
250 event
.m_commandInt
= m_current
;
252 (void)GetEventHandler()->ProcessEvent(event
);
255 void wxVListBox::SetSelection(int selection
)
257 wxCHECK_RET( selection
== wxNOT_FOUND
||
258 (selection
>= 0 && (size_t)selection
< GetItemCount()),
259 _T("wxVListBox::SetSelection(): invalid item index") );
261 if ( HasMultipleSelection() )
264 m_anchor
= selection
;
267 DoSetCurrent(selection
);
270 size_t wxVListBox::GetSelectedCount() const
272 return m_selStore
? m_selStore
->GetSelectedCount()
273 : m_current
== wxNOT_FOUND
? 0 : 1;
276 int wxVListBox::GetFirstSelected(unsigned long& cookie
) const
280 return GetNextSelected(cookie
);
283 int wxVListBox::GetNextSelected(unsigned long& cookie
) const
285 wxCHECK_MSG( m_selStore
, wxNOT_FOUND
,
286 _T("GetFirst/NextSelected() may only be used with multiselection listboxes") );
288 while ( cookie
< GetItemCount() )
290 if ( IsSelected(cookie
++) )
297 // ----------------------------------------------------------------------------
298 // wxVListBox appearance parameters
299 // ----------------------------------------------------------------------------
301 void wxVListBox::SetMargins(const wxPoint
& pt
)
303 if ( pt
!= m_ptMargins
)
311 void wxVListBox::SetSelectionBackground(const wxColour
& col
)
316 // ----------------------------------------------------------------------------
317 // wxVListBox painting
318 // ----------------------------------------------------------------------------
320 wxCoord
wxVListBox::OnGetLineHeight(size_t line
) const
322 return OnMeasureItem(line
) + 2*m_ptMargins
.y
;
325 void wxVListBox::OnDrawSeparator(wxDC
& WXUNUSED(dc
),
326 wxRect
& WXUNUSED(rect
),
327 size_t WXUNUSED(n
)) const
331 void wxVListBox::OnDrawBackground(wxDC
& dc
, const wxRect
& rect
, size_t n
) const
333 // we need to render selected and current items differently
334 const bool isSelected
= IsSelected(n
),
335 isCurrent
= IsCurrent(n
);
336 if ( isSelected
|| isCurrent
)
340 dc
.SetBrush(wxBrush(m_colBgSel
, wxSOLID
));
344 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
347 dc
.SetPen(*(isCurrent
? wxBLACK_PEN
: wxTRANSPARENT_PEN
));
349 dc
.DrawRectangle(rect
);
351 //else: do nothing for the normal items
354 void wxVListBox::OnPaint(wxPaintEvent
& WXUNUSED(event
))
358 // the update rectangle
359 wxRect rectUpdate
= GetUpdateClientRect();
361 // the bounding rectangle of the current line
363 rectLine
.width
= GetClientSize().x
;
365 // iterate over all visible lines
366 const size_t lineMax
= GetLastVisibleLine();
367 for ( size_t line
= GetFirstVisibleLine(); line
<= lineMax
; line
++ )
369 const wxCoord hLine
= OnGetLineHeight(line
);
371 rectLine
.height
= hLine
;
373 // and draw the ones which intersect the update rect
374 if ( rectLine
.Intersects(rectUpdate
) )
376 // don't allow drawing outside of the lines rectangle
377 wxDCClipper
clip(dc
, rectLine
);
379 wxRect rect
= rectLine
;
380 OnDrawBackground(dc
, rect
, line
);
382 OnDrawSeparator(dc
, rect
, line
);
384 rect
.Deflate(m_ptMargins
.x
, m_ptMargins
.y
);
385 OnDrawItem(dc
, rect
, line
);
387 else // no intersection
389 if ( rectLine
.GetTop() > rectUpdate
.GetBottom() )
391 // we are already below the update rect, no need to continue
395 //else: the next line may intersect the update rect
402 // ============================================================================
403 // wxVListBox keyboard/mouse handling
404 // ============================================================================
406 void wxVListBox::DoHandleItemClick(int item
, int flags
)
408 // has anything worth telling the client code about happened?
411 if ( HasMultipleSelection() )
413 // select the iteem clicked?
416 // NB: the keyboard interface we implement here corresponds to
417 // wxLB_EXTENDED rather than wxLB_MULTIPLE but this one makes more
419 if ( flags
& ItemClick_Shift
)
421 if ( m_current
!= wxNOT_FOUND
)
423 if ( m_anchor
== wxNOT_FOUND
)
424 m_anchor
= m_current
;
428 // only the range from the selection anchor to new m_current
433 if ( SelectRange(m_anchor
, item
) )
436 //else: treat it as ordinary click/keypress
438 else // Shift not pressed
442 if ( flags
& ItemClick_Ctrl
)
446 if ( !(flags
& ItemClick_Kbd
) )
450 // the status of the item has definitely changed
453 //else: Ctrl-arrow pressed, don't change selection
455 //else: behave as in single selection case
460 // make the clicked item the only selection
469 // in any case the item should become the current one
470 if ( DoSetCurrent(item
) )
472 if ( !HasMultipleSelection() )
474 // this has also changed the selection for single selection case
481 // notify the user about the selection change
484 //else: nothing changed at all
487 // ----------------------------------------------------------------------------
489 // ----------------------------------------------------------------------------
491 void wxVListBox::OnKeyDown(wxKeyEvent
& event
)
493 // flags for DoHandleItemClick()
494 int flags
= ItemClick_Kbd
;
497 switch ( event
.GetKeyCode() )
504 current
= GetLineCount() - 1;
508 if ( m_current
== (int)GetLineCount() - 1 )
511 current
= m_current
+ 1;
515 if ( m_current
== wxNOT_FOUND
)
516 current
= GetLineCount() - 1;
517 else if ( m_current
!= 0 )
518 current
= m_current
- 1;
519 else // m_current == 0
526 current
= GetFirstVisibleLine();
531 if ( m_current
== (int)GetFirstVisibleLine() )
536 current
= GetFirstVisibleLine();
540 // hack: pressing space should work like a mouse click rather than
541 // like a keyboard arrow press, so trick DoHandleItemClick() in
542 // thinking we were clicked
543 flags
&= ~ItemClick_Kbd
;
549 // Since we are using wxWANTS_CHARS we need to send navigation
550 // events for the tabs on MSW
552 wxNavigationKeyEvent ne
;
553 ne
.SetDirection(!event
.ShiftDown());
554 ne
.SetCurrentFocus(this);
555 ne
.SetEventObject(this);
556 GetParent()->GetEventHandler()->ProcessEvent(ne
);
558 // fall through to default
562 current
= 0; // just to silent the stupid compiler warnings
563 wxUnusedVar(current
);
567 if ( event
.ShiftDown() )
568 flags
|= ItemClick_Shift
;
569 if ( event
.ControlDown() )
570 flags
|= ItemClick_Ctrl
;
572 DoHandleItemClick(current
, flags
);
575 // ----------------------------------------------------------------------------
576 // wxVListBox mouse handling
577 // ----------------------------------------------------------------------------
579 void wxVListBox::OnLeftDown(wxMouseEvent
& event
)
581 int item
= HitTest(event
.GetPosition());
583 if ( item
!= wxNOT_FOUND
)
586 if ( event
.ShiftDown() )
587 flags
|= ItemClick_Shift
;
589 // under Mac Apple-click is used in the same way as Ctrl-click
592 if ( event
.MetaDown() )
594 if ( event
.ControlDown() )
596 flags
|= ItemClick_Ctrl
;
598 DoHandleItemClick(item
, flags
);
602 void wxVListBox::OnLeftDClick(wxMouseEvent
& event
)
604 int item
= HitTest(event
.GetPosition());
605 if ( item
!= wxNOT_FOUND
)
607 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, GetId());
608 event
.SetEventObject(this);
609 event
.m_commandInt
= item
;
611 (void)GetEventHandler()->ProcessEvent(event
);