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 if ( !wxVScrolledWindow::Create(parent
, id
, pos
, size
, style
, name
) )
74 if ( style
& wxLB_MULTIPLE
)
75 m_selStore
= new wxSelectionStore
;
77 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
));
78 m_colBgSel
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
83 wxVListBox::~wxVListBox()
88 void wxVListBox::SetItemCount(size_t count
)
92 // tell the selection store that our number of items has changed
93 m_selStore
->SetItemCount(count
);
99 // ----------------------------------------------------------------------------
100 // selection handling
101 // ----------------------------------------------------------------------------
103 bool wxVListBox::IsSelected(size_t line
) const
105 return m_selStore
? m_selStore
->IsSelected(line
) : (int)line
== m_current
;
108 bool wxVListBox::Select(size_t item
, bool select
)
110 wxCHECK_MSG( m_selStore
, false,
111 _T("Select() may only be used with multiselection listbox") );
113 wxCHECK_MSG( item
< GetItemCount(), false,
114 _T("Select(): invalid item index") );
116 bool changed
= m_selStore
->SelectItem(item
, select
);
119 // selection really changed
128 bool wxVListBox::SelectRange(size_t from
, size_t to
)
130 wxCHECK_MSG( m_selStore
, false,
131 _T("SelectRange() may only be used with multiselection listbox") );
133 // make sure items are in correct order
141 wxCHECK_MSG( to
< GetItemCount(), false,
142 _T("SelectRange(): invalid item index") );
145 if ( !m_selStore
->SelectRange(from
, to
, true, &changed
) )
147 // too many items have changed, we didn't record them in changed array
148 // so we have no choice but to refresh everything between from and to
149 RefreshLines(from
, to
);
151 else // we've got the indices of the changed items
153 const size_t count
= changed
.GetCount();
160 // refresh just the lines which have really changed
161 for ( size_t n
= 0; n
< count
; n
++ )
163 RefreshLine(changed
[n
]);
171 bool wxVListBox::DoSelectAll(bool select
)
173 wxCHECK_MSG( m_selStore
, false,
174 _T("SelectAll may only be used with multiselection listbox") );
176 size_t count
= GetItemCount();
180 if ( !m_selStore
->SelectRange(0, count
- 1, select
) ||
193 bool wxVListBox::DoSetCurrent(int current
)
195 wxASSERT_MSG( current
== wxNOT_FOUND
||
196 (current
>= 0 && (size_t)current
< GetItemCount()),
197 _T("wxVListBox::DoSetCurrent(): invalid item index") );
199 if ( current
== m_current
)
205 if ( m_current
!= wxNOT_FOUND
)
206 RefreshLine(m_current
);
210 if ( m_current
!= wxNOT_FOUND
)
212 // if the line is not visible at all, we scroll it into view but we
213 // don't need to refresh it -- it will be redrawn anyhow
214 if ( !IsVisible(m_current
) )
216 ScrollToLine(m_current
);
218 else // line is at least partly visible
220 // it is, indeed, only partly visible, so scroll it into view to
221 // make it entirely visible
222 if ( (size_t)m_current
== GetLastVisibleLine() )
224 ScrollToLine(m_current
);
227 // but in any case refresh it as even if it was only partly visible
228 // before we need to redraw it entirely as its background changed
229 RefreshLine(m_current
);
236 void wxVListBox::SendSelectedEvent()
238 wxASSERT_MSG( m_current
!= wxNOT_FOUND
,
239 _T("SendSelectedEvent() shouldn't be called") );
241 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, GetId());
242 event
.SetEventObject(this);
243 event
.m_commandInt
= m_current
;
245 (void)GetEventHandler()->ProcessEvent(event
);
248 void wxVListBox::SetSelection(int selection
)
250 wxCHECK_RET( selection
== wxNOT_FOUND
||
251 (selection
>= 0 && (size_t)selection
< GetItemCount()),
252 _T("wxVListBox::SetSelection(): invalid item index") );
254 if ( HasMultipleSelection() )
257 m_anchor
= selection
;
260 DoSetCurrent(selection
);
263 size_t wxVListBox::GetSelectedCount() const
265 return m_selStore
? m_selStore
->GetSelectedCount()
266 : m_current
== wxNOT_FOUND
? 0 : 1;
269 int wxVListBox::GetFirstSelected(unsigned long& cookie
) const
273 return GetNextSelected(cookie
);
276 int wxVListBox::GetNextSelected(unsigned long& cookie
) const
278 wxCHECK_MSG( m_selStore
, wxNOT_FOUND
,
279 _T("GetFirst/NextSelected() may only be used with multiselection listboxes") );
281 while ( cookie
< GetItemCount() )
283 if ( IsSelected(cookie
++) )
290 // ----------------------------------------------------------------------------
291 // wxVListBox appearance parameters
292 // ----------------------------------------------------------------------------
294 void wxVListBox::SetMargins(const wxPoint
& pt
)
296 if ( pt
!= m_ptMargins
)
304 void wxVListBox::SetSelectionBackground(const wxColour
& col
)
309 // ----------------------------------------------------------------------------
310 // wxVListBox painting
311 // ----------------------------------------------------------------------------
313 wxCoord
wxVListBox::OnGetLineHeight(size_t line
) const
315 return OnMeasureItem(line
) + 2*m_ptMargins
.y
;
318 void wxVListBox::OnDrawSeparator(wxDC
& WXUNUSED(dc
),
319 wxRect
& WXUNUSED(rect
),
320 size_t WXUNUSED(n
)) const
324 void wxVListBox::OnDrawBackground(wxDC
& dc
, const wxRect
& rect
, size_t n
) const
326 // we need to render selected and current items differently
327 const bool isSelected
= IsSelected(n
),
328 isCurrent
= IsCurrent(n
);
329 if ( isSelected
|| isCurrent
)
333 dc
.SetBrush(wxBrush(m_colBgSel
, wxSOLID
));
337 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
340 dc
.SetPen(*(isCurrent
? wxBLACK_PEN
: wxTRANSPARENT_PEN
));
342 dc
.DrawRectangle(rect
);
344 //else: do nothing for the normal items
347 void wxVListBox::OnPaint(wxPaintEvent
& WXUNUSED(event
))
351 // the update rectangle
352 wxRect rectUpdate
= GetUpdateClientRect();
354 // the bounding rectangle of the current line
356 rectLine
.width
= GetClientSize().x
;
358 // iterate over all visible lines
359 const size_t lineMax
= GetLastVisibleLine();
360 for ( size_t line
= GetFirstVisibleLine(); line
<= lineMax
; line
++ )
362 const wxCoord hLine
= OnGetLineHeight(line
);
364 rectLine
.height
= hLine
;
366 // and draw the ones which intersect the update rect
367 if ( rectLine
.Intersects(rectUpdate
) )
369 // don't allow drawing outside of the lines rectangle
370 wxDCClipper
clip(dc
, rectLine
);
372 wxRect rect
= rectLine
;
373 OnDrawBackground(dc
, rect
, line
);
375 OnDrawSeparator(dc
, rect
, line
);
377 rect
.Deflate(m_ptMargins
.x
, m_ptMargins
.y
);
378 OnDrawItem(dc
, rect
, line
);
380 else // no intersection
382 if ( rectLine
.GetTop() > rectUpdate
.GetBottom() )
384 // we are already below the update rect, no need to continue
388 //else: the next line may intersect the update rect
395 // ============================================================================
396 // wxVListBox keyboard/mouse handling
397 // ============================================================================
399 void wxVListBox::DoHandleItemClick(int item
, int flags
)
401 // has anything worth telling the client code about happened?
404 if ( HasMultipleSelection() )
406 // select the iteem clicked?
409 // NB: the keyboard interface we implement here corresponds to
410 // wxLB_EXTENDED rather than wxLB_MULTIPLE but this one makes more
412 if ( flags
& ItemClick_Shift
)
414 if ( m_current
!= wxNOT_FOUND
)
416 if ( m_anchor
== wxNOT_FOUND
)
417 m_anchor
= m_current
;
421 // only the range from the selection anchor to new m_current
426 if ( SelectRange(m_anchor
, item
) )
429 //else: treat it as ordinary click/keypress
431 else // Shift not pressed
435 if ( flags
& ItemClick_Ctrl
)
439 if ( !(flags
& ItemClick_Kbd
) )
443 // the status of the item has definitely changed
446 //else: Ctrl-arrow pressed, don't change selection
448 //else: behave as in single selection case
453 // make the clicked item the only selection
462 // in any case the item should become the current one
463 if ( DoSetCurrent(item
) )
465 if ( !HasMultipleSelection() )
467 // this has also changed the selection for single selection case
474 // notify the user about the selection change
477 //else: nothing changed at all
480 // ----------------------------------------------------------------------------
482 // ----------------------------------------------------------------------------
484 void wxVListBox::OnKeyDown(wxKeyEvent
& event
)
486 // flags for DoHandleItemClick()
487 int flags
= ItemClick_Kbd
;
490 switch ( event
.GetKeyCode() )
497 current
= GetLineCount() - 1;
501 if ( m_current
== (int)GetLineCount() - 1 )
504 current
= m_current
+ 1;
508 if ( m_current
== wxNOT_FOUND
)
509 current
= GetLineCount() - 1;
510 else if ( m_current
!= 0 )
511 current
= m_current
- 1;
512 else // m_current == 0
519 current
= GetFirstVisibleLine();
524 if ( m_current
== (int)GetFirstVisibleLine() )
529 current
= GetFirstVisibleLine();
533 // hack: pressing space should work like a mouse click rather than
534 // like a keyboard arrow press, so trick DoHandleItemClick() in
535 // thinking we were clicked
536 flags
&= ~ItemClick_Kbd
;
542 current
= 0; // just to silent the stupid compiler warnings
543 wxUnusedVar(current
);
547 if ( event
.ShiftDown() )
548 flags
|= ItemClick_Shift
;
549 if ( event
.ControlDown() )
550 flags
|= ItemClick_Ctrl
;
552 DoHandleItemClick(current
, flags
);
555 // ----------------------------------------------------------------------------
556 // wxVListBox mouse handling
557 // ----------------------------------------------------------------------------
559 void wxVListBox::OnLeftDown(wxMouseEvent
& event
)
561 int item
= HitTest(event
.GetPosition());
563 if ( item
!= wxNOT_FOUND
)
566 if ( event
.ShiftDown() )
567 flags
|= ItemClick_Shift
;
569 // under Mac Apple-click is used in the same way as Ctrl-click
572 if ( event
.MetaDown() )
574 if ( event
.ControlDown() )
576 flags
|= ItemClick_Ctrl
;
578 DoHandleItemClick(item
, flags
);
582 void wxVListBox::OnLeftDClick(wxMouseEvent
& event
)
584 int item
= HitTest(event
.GetPosition());
585 if ( item
!= wxNOT_FOUND
)
587 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, GetId());
588 event
.SetEventObject(this);
589 event
.m_commandInt
= item
;
591 (void)GetEventHandler()->ProcessEvent(event
);