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 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
));
74 if ( style
& wxLB_MULTIPLE
)
75 m_selStore
= new wxSelectionStore
;
80 wxVListBox::~wxVListBox()
85 void wxVListBox::SetItemCount(size_t count
)
89 // tell the selection store that our number of items has changed
90 m_selStore
->SetItemCount(count
);
96 // ----------------------------------------------------------------------------
98 // ----------------------------------------------------------------------------
100 bool wxVListBox::IsSelected(size_t line
) const
102 return m_selStore
? m_selStore
->IsSelected(line
) : (int)line
== m_current
;
105 bool wxVListBox::Select(size_t item
, bool select
)
107 wxCHECK_MSG( m_selStore
, false,
108 _T("Select() may only be used with multiselection listbox") );
110 wxCHECK_MSG( item
< GetItemCount(), false,
111 _T("Select(): invalid item index") );
113 bool changed
= m_selStore
->SelectItem(item
, select
);
116 // selection really changed
125 bool wxVListBox::SelectRange(size_t from
, size_t to
)
127 wxCHECK_MSG( m_selStore
, false,
128 _T("SelectRange() may only be used with multiselection listbox") );
130 // make sure items are in correct order
138 wxCHECK_MSG( to
< GetItemCount(), false,
139 _T("SelectRange(): invalid item index") );
142 if ( !m_selStore
->SelectRange(from
, to
, true, &changed
) )
144 // too many items have changed, we didn't record them in changed array
145 // so we have no choice but to refresh everything between from and to
146 RefreshLines(from
, to
);
148 else // we've got the indices of the changed items
150 const size_t count
= changed
.GetCount();
157 // refresh just the lines which have really changed
158 for ( size_t n
= 0; n
< count
; n
++ )
160 RefreshLine(changed
[n
]);
168 bool wxVListBox::DoSelectAll(bool select
)
170 wxCHECK_MSG( m_selStore
, false,
171 _T("SelectAll may only be used with multiselection listbox") );
173 size_t count
= GetItemCount();
177 if ( !m_selStore
->SelectRange(0, count
- 1, select
) ||
190 bool wxVListBox::DoSetCurrent(int current
)
192 wxASSERT_MSG( current
== wxNOT_FOUND
||
193 (current
>= 0 && (size_t)current
< GetItemCount()),
194 _T("wxVListBox::DoSetCurrent(): invalid item index") );
196 if ( current
== m_current
)
202 if ( m_current
!= wxNOT_FOUND
)
203 RefreshLine(m_current
);
207 if ( m_current
!= wxNOT_FOUND
)
209 // if the line is not visible at all, we scroll it into view but we
210 // don't need to refresh it -- it will be redrawn anyhow
211 if ( !IsVisible(m_current
) )
213 ScrollToLine(m_current
);
215 else // line is at least partly visible
217 // it is, indeed, only partly visible, so scroll it into view to
218 // make it entirely visible
219 if ( (size_t)m_current
== GetLastVisibleLine() )
221 ScrollToLine(m_current
);
224 // but in any case refresh it as even if it was only partly visible
225 // before we need to redraw it entirely as its background changed
226 RefreshLine(m_current
);
233 void wxVListBox::SendSelectedEvent()
235 wxASSERT_MSG( m_current
!= wxNOT_FOUND
,
236 _T("SendSelectedEvent() shouldn't be called") );
238 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, GetId());
239 event
.SetEventObject(this);
240 event
.m_commandInt
= m_current
;
242 (void)GetEventHandler()->ProcessEvent(event
);
245 void wxVListBox::SetSelection(int selection
)
247 wxCHECK_RET( selection
== wxNOT_FOUND
||
248 (selection
>= 0 && (size_t)selection
< GetItemCount()),
249 _T("wxVListBox::SetSelection(): invalid item index") );
251 wxASSERT_MSG( !HasMultipleSelection(),
252 _T("SetSelection() is invalid with multiselection listbox") );
254 DoSetCurrent(selection
);
257 size_t wxVListBox::GetSelectedCount() const
259 return m_selStore
? m_selStore
->GetSelectedCount()
260 : m_current
== wxNOT_FOUND
? 0 : 1;
263 int wxVListBox::GetFirstSelected(unsigned long& cookie
) const
267 return GetNextSelected(cookie
);
270 int wxVListBox::GetNextSelected(unsigned long& cookie
) const
272 wxCHECK_MSG( m_selStore
, wxNOT_FOUND
,
273 _T("GetFirst/NextSelected() may only be used with multiselection listboxes") );
275 while ( cookie
< GetItemCount() )
277 if ( IsSelected(cookie
++) )
284 // ----------------------------------------------------------------------------
285 // wxVListBox painting
286 // ----------------------------------------------------------------------------
288 void wxVListBox::SetMargins(const wxPoint
& pt
)
290 if ( pt
!= m_ptMargins
)
298 wxCoord
wxVListBox::OnGetLineHeight(size_t line
) const
300 return OnMeasureItem(line
) + 2*m_ptMargins
.y
;
303 void wxVListBox::OnDrawSeparator(wxDC
& WXUNUSED(dc
),
304 wxRect
& WXUNUSED(rect
),
305 size_t WXUNUSED(n
)) const
309 void wxVListBox::OnPaint(wxPaintEvent
& event
)
313 // the update rectangle
314 wxRect rectUpdate
= GetUpdateClientRect();
316 // the bounding rectangle of the current line
318 rectLine
.width
= GetClientSize().x
;
320 // iterate over all visible lines
321 const size_t lineMax
= GetLastVisibleLine();
322 for ( size_t line
= GetFirstVisibleLine(); line
<= lineMax
; line
++ )
324 const wxCoord hLine
= OnGetLineHeight(line
);
326 rectLine
.height
= hLine
;
328 // and draw the ones which intersect the update rect
329 if ( rectLine
.Intersects(rectUpdate
) )
331 // don't allow drawing outside of the lines rectangle
332 wxDCClipper
clip(dc
, rectLine
);
334 // we need to render selected and current items differently
335 const bool isSelected
= IsSelected(line
);
336 if ( isSelected
|| IsCurrent(line
) )
340 wxBrush
brush(wxSystemSettings::
341 GetColour(wxSYS_COLOUR_HIGHLIGHT
),
347 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
350 dc
.SetPen(*(IsCurrent(line
) ? wxBLACK_PEN
: wxTRANSPARENT_PEN
));
352 dc
.DrawRectangle(rectLine
);
355 wxRect rect
= rectLine
;
356 OnDrawSeparator(dc
, rect
, line
);
358 rect
.Deflate(m_ptMargins
.x
, m_ptMargins
.y
);
359 OnDrawItem(dc
, rect
, line
);
361 else // no intersection
363 if ( rectLine
.GetTop() > rectUpdate
.GetBottom() )
365 // we are already below the update rect, no need to continue
369 //else: the next line may intersect the update rect
376 // ============================================================================
377 // wxVListBox keyboard/mouse handling
378 // ============================================================================
380 void wxVListBox::DoHandleItemClick(int item
, int flags
)
382 // has anything worth telling the client code about happened?
385 if ( HasMultipleSelection() )
387 // select the iteem clicked?
390 // NB: the keyboard interface we implement here corresponds to
391 // wxLB_EXTENDED rather than wxLB_MULTIPLE but this one makes more
393 if ( flags
& ItemClick_Shift
)
395 if ( m_current
!= wxNOT_FOUND
)
397 if ( m_anchor
== wxNOT_FOUND
)
398 m_anchor
= m_current
;
402 // only the range from the selection anchor to new m_current
407 if ( SelectRange(m_anchor
, item
) )
410 //else: treat it as ordinary click/keypress
412 else // Shift not pressed
416 if ( flags
& ItemClick_Ctrl
)
420 if ( !(flags
& ItemClick_Kbd
) )
424 // the status of the item has definitely changed
427 //else: Ctrl-arrow pressed, don't change selection
429 //else: behave as in single selection case
434 // make the clicked item the only selection
443 // in any case the item should become the current one
444 if ( DoSetCurrent(item
) )
446 if ( !HasMultipleSelection() )
448 // this has also changed the selection for single selection case
455 // notify the user about the selection change
458 //else: nothing changed at all
461 // ----------------------------------------------------------------------------
463 // ----------------------------------------------------------------------------
465 void wxVListBox::OnKeyDown(wxKeyEvent
& event
)
467 // flags for DoHandleItemClick()
468 int flags
= ItemClick_Kbd
;
470 int current
= 0; // just to silent the stupid compiler warnings
471 switch ( event
.GetKeyCode() )
478 current
= GetLineCount() - 1;
482 if ( m_current
== (int)GetLineCount() - 1 )
485 current
= m_current
+ 1;
489 if ( m_current
== wxNOT_FOUND
)
490 current
= GetLineCount() - 1;
491 else if ( m_current
!= 0 )
492 current
= m_current
- 1;
493 else // m_current == 0
500 current
= GetFirstVisibleLine();
505 if ( m_current
== (int)GetFirstVisibleLine() )
510 current
= GetFirstVisibleLine();
514 // hack: pressing space should work like a mouse click rather than
515 // like a keyboard arrow press, so trick DoHandleItemClick() in
516 // thinking we were clicked
517 flags
&= ~ItemClick_Kbd
;
526 if ( event
.ShiftDown() )
527 flags
|= ItemClick_Shift
;
528 if ( event
.ControlDown() )
529 flags
|= ItemClick_Ctrl
;
531 DoHandleItemClick(current
, flags
);
534 // ----------------------------------------------------------------------------
535 // wxVListBox mouse handling
536 // ----------------------------------------------------------------------------
538 void wxVListBox::OnLeftDown(wxMouseEvent
& event
)
540 int item
= HitTest(event
.GetPosition());
542 if ( item
!= wxNOT_FOUND
)
545 if ( event
.ShiftDown() )
546 flags
|= ItemClick_Shift
;
548 // under Mac Apple-click is used in the same way as Ctrl-click
551 if ( event
.MetaDown() )
553 if ( event
.ControlDown() )
555 flags
|= ItemClick_Ctrl
;
557 DoHandleItemClick(item
, flags
);
561 void wxVListBox::OnLeftDClick(wxMouseEvent
& event
)
563 int item
= HitTest(event
.GetPosition());
564 if ( item
!= wxNOT_FOUND
)
566 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, GetId());
567 event
.SetEventObject(this);
568 event
.m_commandInt
= item
;
570 (void)GetEventHandler()->ProcessEvent(event
);