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 // ----------------------------------------------------------------------------
52 // wxVListBox creation
53 // ----------------------------------------------------------------------------
55 void wxVListBox::Init()
58 m_anchor
= wxNOT_FOUND
;
62 bool wxVListBox::Create(wxWindow
*parent
,
69 if ( !wxVScrolledWindow::Create(parent
, id
, pos
, size
, style
, name
) )
72 if ( style
& wxLB_MULTIPLE
)
73 m_selStore
= new wxSelectionStore
;
75 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
));
76 m_colBgSel
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
81 wxVListBox::~wxVListBox()
86 void wxVListBox::SetItemCount(size_t count
)
90 // tell the selection store that our number of items has changed
91 m_selStore
->SetItemCount(count
);
97 // ----------------------------------------------------------------------------
99 // ----------------------------------------------------------------------------
101 bool wxVListBox::IsSelected(size_t line
) const
103 return m_selStore
? m_selStore
->IsSelected(line
) : (int)line
== m_current
;
106 bool wxVListBox::Select(size_t item
, bool select
)
108 wxCHECK_MSG( m_selStore
, false,
109 _T("Select() may only be used with multiselection listbox") );
111 wxCHECK_MSG( item
< GetItemCount(), false,
112 _T("Select(): invalid item index") );
114 bool changed
= m_selStore
->SelectItem(item
, select
);
117 // selection really changed
126 bool wxVListBox::SelectRange(size_t from
, size_t to
)
128 wxCHECK_MSG( m_selStore
, false,
129 _T("SelectRange() may only be used with multiselection listbox") );
131 // make sure items are in correct order
139 wxCHECK_MSG( to
< GetItemCount(), false,
140 _T("SelectRange(): invalid item index") );
143 if ( !m_selStore
->SelectRange(from
, to
, true, &changed
) )
145 // too many items have changed, we didn't record them in changed array
146 // so we have no choice but to refresh everything between from and to
147 RefreshLines(from
, to
);
149 else // we've got the indices of the changed items
151 const size_t count
= changed
.GetCount();
158 // refresh just the lines which have really changed
159 for ( size_t n
= 0; n
< count
; n
++ )
161 RefreshLine(changed
[n
]);
169 bool wxVListBox::DoSelectAll(bool select
)
171 wxCHECK_MSG( m_selStore
, false,
172 _T("SelectAll may only be used with multiselection listbox") );
174 size_t count
= GetItemCount();
178 if ( !m_selStore
->SelectRange(0, count
- 1, select
) ||
191 bool wxVListBox::DoSetCurrent(int current
)
193 wxASSERT_MSG( current
== wxNOT_FOUND
||
194 (current
>= 0 && (size_t)current
< GetItemCount()),
195 _T("wxVListBox::DoSetCurrent(): invalid item index") );
197 if ( current
== m_current
)
203 if ( m_current
!= wxNOT_FOUND
)
204 RefreshLine(m_current
);
208 if ( m_current
!= wxNOT_FOUND
)
210 // if the line is not visible at all, we scroll it into view but we
211 // don't need to refresh it -- it will be redrawn anyhow
212 if ( !IsVisible(m_current
) )
214 ScrollToLine(m_current
);
216 else // line is at least partly visible
218 // it is, indeed, only partly visible, so scroll it into view to
219 // make it entirely visible
220 if ( (size_t)m_current
== GetLastVisibleLine() )
222 ScrollToLine(m_current
);
225 // but in any case refresh it as even if it was only partly visible
226 // before we need to redraw it entirely as its background changed
227 RefreshLine(m_current
);
234 void wxVListBox::SendSelectedEvent()
236 wxASSERT_MSG( m_current
!= wxNOT_FOUND
,
237 _T("SendSelectedEvent() shouldn't be called") );
239 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, GetId());
240 event
.SetEventObject(this);
241 event
.m_commandInt
= m_current
;
243 (void)GetEventHandler()->ProcessEvent(event
);
246 void wxVListBox::SetSelection(int selection
)
248 wxCHECK_RET( selection
== wxNOT_FOUND
||
249 (selection
>= 0 && (size_t)selection
< GetItemCount()),
250 _T("wxVListBox::SetSelection(): invalid item index") );
252 if ( HasMultipleSelection() )
255 m_anchor
= selection
;
258 DoSetCurrent(selection
);
261 size_t wxVListBox::GetSelectedCount() const
263 return m_selStore
? m_selStore
->GetSelectedCount()
264 : m_current
== wxNOT_FOUND
? 0 : 1;
267 int wxVListBox::GetFirstSelected(unsigned long& cookie
) const
271 return GetNextSelected(cookie
);
274 int wxVListBox::GetNextSelected(unsigned long& cookie
) const
276 wxCHECK_MSG( m_selStore
, wxNOT_FOUND
,
277 _T("GetFirst/NextSelected() may only be used with multiselection listboxes") );
279 while ( cookie
< GetItemCount() )
281 if ( IsSelected(cookie
++) )
288 // ----------------------------------------------------------------------------
289 // wxVListBox appearance parameters
290 // ----------------------------------------------------------------------------
292 void wxVListBox::SetMargins(const wxPoint
& pt
)
294 if ( pt
!= m_ptMargins
)
302 void wxVListBox::SetSelectionBackground(const wxColour
& col
)
307 // ----------------------------------------------------------------------------
308 // wxVListBox painting
309 // ----------------------------------------------------------------------------
311 wxCoord
wxVListBox::OnGetLineHeight(size_t line
) const
313 return OnMeasureItem(line
) + 2*m_ptMargins
.y
;
316 void wxVListBox::OnDrawSeparator(wxDC
& WXUNUSED(dc
),
317 wxRect
& WXUNUSED(rect
),
318 size_t WXUNUSED(n
)) const
322 void wxVListBox::OnPaint(wxPaintEvent
& event
)
326 // the update rectangle
327 wxRect rectUpdate
= GetUpdateClientRect();
329 // the bounding rectangle of the current line
331 rectLine
.width
= GetClientSize().x
;
333 // iterate over all visible lines
334 const size_t lineMax
= GetLastVisibleLine();
335 for ( size_t line
= GetFirstVisibleLine(); line
<= lineMax
; line
++ )
337 const wxCoord hLine
= OnGetLineHeight(line
);
339 rectLine
.height
= hLine
;
341 // and draw the ones which intersect the update rect
342 if ( rectLine
.Intersects(rectUpdate
) )
344 // don't allow drawing outside of the lines rectangle
345 wxDCClipper
clip(dc
, rectLine
);
347 // we need to render selected and current items differently
348 const bool isSelected
= IsSelected(line
);
349 if ( isSelected
|| IsCurrent(line
) )
353 dc
.SetBrush(wxBrush(m_colBgSel
, wxSOLID
));
357 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
360 dc
.SetPen(*(IsCurrent(line
) ? wxBLACK_PEN
: wxTRANSPARENT_PEN
));
362 dc
.DrawRectangle(rectLine
);
365 wxRect rect
= rectLine
;
366 OnDrawSeparator(dc
, rect
, line
);
368 rect
.Deflate(m_ptMargins
.x
, m_ptMargins
.y
);
369 OnDrawItem(dc
, rect
, line
);
371 else // no intersection
373 if ( rectLine
.GetTop() > rectUpdate
.GetBottom() )
375 // we are already below the update rect, no need to continue
379 //else: the next line may intersect the update rect
386 // ============================================================================
387 // wxVListBox keyboard/mouse handling
388 // ============================================================================
390 void wxVListBox::DoHandleItemClick(int item
, int flags
)
392 // has anything worth telling the client code about happened?
395 if ( HasMultipleSelection() )
397 // select the iteem clicked?
400 // NB: the keyboard interface we implement here corresponds to
401 // wxLB_EXTENDED rather than wxLB_MULTIPLE but this one makes more
403 if ( flags
& ItemClick_Shift
)
405 if ( m_current
!= wxNOT_FOUND
)
407 if ( m_anchor
== wxNOT_FOUND
)
408 m_anchor
= m_current
;
412 // only the range from the selection anchor to new m_current
417 if ( SelectRange(m_anchor
, item
) )
420 //else: treat it as ordinary click/keypress
422 else // Shift not pressed
426 if ( flags
& ItemClick_Ctrl
)
430 if ( !(flags
& ItemClick_Kbd
) )
434 // the status of the item has definitely changed
437 //else: Ctrl-arrow pressed, don't change selection
439 //else: behave as in single selection case
444 // make the clicked item the only selection
453 // in any case the item should become the current one
454 if ( DoSetCurrent(item
) )
456 if ( !HasMultipleSelection() )
458 // this has also changed the selection for single selection case
465 // notify the user about the selection change
468 //else: nothing changed at all
471 // ----------------------------------------------------------------------------
473 // ----------------------------------------------------------------------------
475 void wxVListBox::OnKeyDown(wxKeyEvent
& event
)
477 // flags for DoHandleItemClick()
478 int flags
= ItemClick_Kbd
;
480 int current
= 0; // just to silent the stupid compiler warnings
481 switch ( event
.GetKeyCode() )
488 current
= GetLineCount() - 1;
492 if ( m_current
== (int)GetLineCount() - 1 )
495 current
= m_current
+ 1;
499 if ( m_current
== wxNOT_FOUND
)
500 current
= GetLineCount() - 1;
501 else if ( m_current
!= 0 )
502 current
= m_current
- 1;
503 else // m_current == 0
510 current
= GetFirstVisibleLine();
515 if ( m_current
== (int)GetFirstVisibleLine() )
520 current
= GetFirstVisibleLine();
524 // hack: pressing space should work like a mouse click rather than
525 // like a keyboard arrow press, so trick DoHandleItemClick() in
526 // thinking we were clicked
527 flags
&= ~ItemClick_Kbd
;
536 if ( event
.ShiftDown() )
537 flags
|= ItemClick_Shift
;
538 if ( event
.ControlDown() )
539 flags
|= ItemClick_Ctrl
;
541 DoHandleItemClick(current
, flags
);
544 // ----------------------------------------------------------------------------
545 // wxVListBox mouse handling
546 // ----------------------------------------------------------------------------
548 void wxVListBox::OnLeftDown(wxMouseEvent
& event
)
550 int item
= HitTest(event
.GetPosition());
552 if ( item
!= wxNOT_FOUND
)
555 if ( event
.ShiftDown() )
556 flags
|= ItemClick_Shift
;
558 // under Mac Apple-click is used in the same way as Ctrl-click
561 if ( event
.MetaDown() )
563 if ( event
.ControlDown() )
565 flags
|= ItemClick_Ctrl
;
567 DoHandleItemClick(item
, flags
);
571 void wxVListBox::OnLeftDClick(wxMouseEvent
& event
)
573 int item
= HitTest(event
.GetPosition());
574 if ( item
!= wxNOT_FOUND
)
576 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, GetId());
577 event
.SetEventObject(this);
578 event
.m_commandInt
= item
;
580 (void)GetEventHandler()->ProcessEvent(event
);