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"
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 BEGIN_EVENT_TABLE(wxVListBox
, wxVScrolledWindow
)
45 EVT_PAINT(wxVListBox::OnPaint
)
47 EVT_KEY_DOWN(wxVListBox::OnKeyDown
)
48 EVT_LEFT_DOWN(wxVListBox::OnLeftDown
)
49 EVT_LEFT_DCLICK(wxVListBox::OnLeftDClick
)
52 // ============================================================================
54 // ============================================================================
56 IMPLEMENT_ABSTRACT_CLASS(wxVListBox
, wxVScrolledWindow
)
58 // ----------------------------------------------------------------------------
59 // wxVListBox creation
60 // ----------------------------------------------------------------------------
62 void wxVListBox::Init()
65 m_anchor
= wxNOT_FOUND
;
69 bool wxVListBox::Create(wxWindow
*parent
,
76 style
|= wxWANTS_CHARS
| wxFULL_REPAINT_ON_RESIZE
;
77 if ( !wxVScrolledWindow::Create(parent
, id
, pos
, size
, style
, name
) )
80 if ( style
& wxLB_MULTIPLE
)
81 m_selStore
= new wxSelectionStore
;
83 // make sure the native widget has the right colour since we do
84 // transparent drawing by default
85 SetBackgroundColour(GetBackgroundColour());
86 m_colBgSel
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
88 // flicker-free drawing requires this
89 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
94 wxVListBox::~wxVListBox()
99 void wxVListBox::SetItemCount(size_t count
)
103 // tell the selection store that our number of items has changed
104 m_selStore
->SetItemCount(count
);
110 // ----------------------------------------------------------------------------
111 // selection handling
112 // ----------------------------------------------------------------------------
114 bool wxVListBox::IsSelected(size_t line
) const
116 return m_selStore
? m_selStore
->IsSelected(line
) : (int)line
== m_current
;
119 bool wxVListBox::Select(size_t item
, bool select
)
121 wxCHECK_MSG( m_selStore
, false,
122 _T("Select() may only be used with multiselection listbox") );
124 wxCHECK_MSG( item
< GetItemCount(), false,
125 _T("Select(): invalid item index") );
127 bool changed
= m_selStore
->SelectItem(item
, select
);
130 // selection really changed
139 bool wxVListBox::SelectRange(size_t from
, size_t to
)
141 wxCHECK_MSG( m_selStore
, false,
142 _T("SelectRange() may only be used with multiselection listbox") );
144 // make sure items are in correct order
152 wxCHECK_MSG( to
< GetItemCount(), false,
153 _T("SelectRange(): invalid item index") );
156 if ( !m_selStore
->SelectRange(from
, to
, true, &changed
) )
158 // too many items have changed, we didn't record them in changed array
159 // so we have no choice but to refresh everything between from and to
160 RefreshLines(from
, to
);
162 else // we've got the indices of the changed items
164 const size_t count
= changed
.GetCount();
171 // refresh just the lines which have really changed
172 for ( size_t n
= 0; n
< count
; n
++ )
174 RefreshLine(changed
[n
]);
182 bool wxVListBox::DoSelectAll(bool select
)
184 wxCHECK_MSG( m_selStore
, false,
185 _T("SelectAll may only be used with multiselection listbox") );
187 size_t count
= GetItemCount();
191 if ( !m_selStore
->SelectRange(0, count
- 1, select
) ||
204 bool wxVListBox::DoSetCurrent(int current
)
206 wxASSERT_MSG( current
== wxNOT_FOUND
||
207 (current
>= 0 && (size_t)current
< GetItemCount()),
208 _T("wxVListBox::DoSetCurrent(): invalid item index") );
210 if ( current
== m_current
)
216 if ( m_current
!= wxNOT_FOUND
)
217 RefreshLine(m_current
);
221 if ( m_current
!= wxNOT_FOUND
)
223 // if the line is not visible at all, we scroll it into view but we
224 // don't need to refresh it -- it will be redrawn anyhow
225 if ( !IsVisible(m_current
) )
227 ScrollToLine(m_current
);
229 else // line is at least partly visible
231 // it is, indeed, only partly visible, so scroll it into view to
232 // make it entirely visible
233 while ( (size_t)m_current
== GetLastVisibleLine() &&
234 ScrollToLine(GetVisibleBegin()+1) ) ;
236 // but in any case refresh it as even if it was only partly visible
237 // before we need to redraw it entirely as its background changed
238 RefreshLine(m_current
);
245 void wxVListBox::SendSelectedEvent()
247 wxASSERT_MSG( m_current
!= wxNOT_FOUND
,
248 _T("SendSelectedEvent() shouldn't be called") );
250 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, GetId());
251 event
.SetEventObject(this);
252 event
.SetInt(m_current
);
254 (void)GetEventHandler()->ProcessEvent(event
);
257 void wxVListBox::SetSelection(int selection
)
259 wxCHECK_RET( selection
== wxNOT_FOUND
||
260 (selection
>= 0 && (size_t)selection
< GetItemCount()),
261 _T("wxVListBox::SetSelection(): invalid item index") );
263 if ( HasMultipleSelection() )
265 if (selection
!= wxNOT_FOUND
)
269 m_anchor
= selection
;
272 DoSetCurrent(selection
);
275 size_t wxVListBox::GetSelectedCount() const
277 return m_selStore
? m_selStore
->GetSelectedCount()
278 : m_current
== wxNOT_FOUND
? 0 : 1;
281 int wxVListBox::GetFirstSelected(unsigned long& cookie
) const
285 return GetNextSelected(cookie
);
288 int wxVListBox::GetNextSelected(unsigned long& cookie
) const
290 wxCHECK_MSG( m_selStore
, wxNOT_FOUND
,
291 _T("GetFirst/NextSelected() may only be used with multiselection listboxes") );
293 while ( cookie
< GetItemCount() )
295 if ( IsSelected(cookie
++) )
302 // ----------------------------------------------------------------------------
303 // wxVListBox appearance parameters
304 // ----------------------------------------------------------------------------
306 void wxVListBox::SetMargins(const wxPoint
& pt
)
308 if ( pt
!= m_ptMargins
)
316 void wxVListBox::SetSelectionBackground(const wxColour
& col
)
321 // ----------------------------------------------------------------------------
322 // wxVListBox painting
323 // ----------------------------------------------------------------------------
325 wxCoord
wxVListBox::OnGetLineHeight(size_t line
) const
327 return OnMeasureItem(line
) + 2*m_ptMargins
.y
;
330 void wxVListBox::OnDrawSeparator(wxDC
& WXUNUSED(dc
),
331 wxRect
& WXUNUSED(rect
),
332 size_t WXUNUSED(n
)) const
336 void wxVListBox::OnDrawBackground(wxDC
& dc
, const wxRect
& rect
, size_t n
) const
338 // we need to render selected and current items differently
339 const bool isSelected
= IsSelected(n
),
340 isCurrent
= IsCurrent(n
);
341 if ( isSelected
|| isCurrent
)
345 dc
.SetBrush(wxBrush(m_colBgSel
, wxSOLID
));
349 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
352 dc
.SetPen(*(isCurrent
? wxBLACK_PEN
: wxTRANSPARENT_PEN
));
354 dc
.DrawRectangle(rect
);
356 //else: do nothing for the normal items
359 void wxVListBox::OnPaint(wxPaintEvent
& WXUNUSED(event
))
361 wxSize clientSize
= GetClientSize();
363 wxAutoBufferedPaintDC
dc(this);
365 // the update rectangle
366 wxRect rectUpdate
= GetUpdateClientRect();
368 // fill it with background colour
369 dc
.SetBackground(GetBackgroundColour());
372 // the bounding rectangle of the current line
374 rectLine
.width
= clientSize
.x
;
376 // iterate over all visible lines
377 const size_t lineMax
= GetVisibleEnd();
378 for ( size_t line
= GetFirstVisibleLine(); line
< lineMax
; line
++ )
380 const wxCoord hLine
= OnGetLineHeight(line
);
382 rectLine
.height
= hLine
;
384 // and draw the ones which intersect the update rect
385 if ( rectLine
.Intersects(rectUpdate
) )
387 // don't allow drawing outside of the lines rectangle
388 wxDCClipper
clip(dc
, rectLine
);
390 wxRect rect
= rectLine
;
391 OnDrawBackground(dc
, rect
, line
);
393 OnDrawSeparator(dc
, rect
, line
);
395 rect
.Deflate(m_ptMargins
.x
, m_ptMargins
.y
);
396 OnDrawItem(dc
, rect
, line
);
398 else // no intersection
400 if ( rectLine
.GetTop() > rectUpdate
.GetBottom() )
402 // we are already below the update rect, no need to continue
406 //else: the next line may intersect the update rect
413 // ============================================================================
414 // wxVListBox keyboard/mouse handling
415 // ============================================================================
417 void wxVListBox::DoHandleItemClick(int item
, int flags
)
419 // has anything worth telling the client code about happened?
422 if ( HasMultipleSelection() )
424 // select the iteem clicked?
427 // NB: the keyboard interface we implement here corresponds to
428 // wxLB_EXTENDED rather than wxLB_MULTIPLE but this one makes more
430 if ( flags
& ItemClick_Shift
)
432 if ( m_current
!= wxNOT_FOUND
)
434 if ( m_anchor
== wxNOT_FOUND
)
435 m_anchor
= m_current
;
439 // only the range from the selection anchor to new m_current
444 if ( SelectRange(m_anchor
, item
) )
447 //else: treat it as ordinary click/keypress
449 else // Shift not pressed
453 if ( flags
& ItemClick_Ctrl
)
457 if ( !(flags
& ItemClick_Kbd
) )
461 // the status of the item has definitely changed
464 //else: Ctrl-arrow pressed, don't change selection
466 //else: behave as in single selection case
471 // make the clicked item the only selection
480 // in any case the item should become the current one
481 if ( DoSetCurrent(item
) )
483 if ( !HasMultipleSelection() )
485 // this has also changed the selection for single selection case
492 // notify the user about the selection change
495 //else: nothing changed at all
498 // ----------------------------------------------------------------------------
500 // ----------------------------------------------------------------------------
502 void wxVListBox::OnKeyDown(wxKeyEvent
& event
)
504 // flags for DoHandleItemClick()
505 int flags
= ItemClick_Kbd
;
508 switch ( event
.GetKeyCode() )
515 current
= GetLineCount() - 1;
519 if ( m_current
== (int)GetLineCount() - 1 )
522 current
= m_current
+ 1;
526 if ( m_current
== wxNOT_FOUND
)
527 current
= GetLineCount() - 1;
528 else if ( m_current
!= 0 )
529 current
= m_current
- 1;
530 else // m_current == 0
536 current
= GetFirstVisibleLine();
540 if ( m_current
== (int)GetFirstVisibleLine() )
545 current
= GetFirstVisibleLine();
549 // hack: pressing space should work like a mouse click rather than
550 // like a keyboard arrow press, so trick DoHandleItemClick() in
551 // thinking we were clicked
552 flags
&= ~ItemClick_Kbd
;
558 // Since we are using wxWANTS_CHARS we need to send navigation
559 // events for the tabs on MSW
561 wxNavigationKeyEvent ne
;
562 ne
.SetDirection(!event
.ShiftDown());
563 ne
.SetCurrentFocus(this);
564 ne
.SetEventObject(this);
565 GetParent()->GetEventHandler()->ProcessEvent(ne
);
567 // fall through to default
571 current
= 0; // just to silent the stupid compiler warnings
572 wxUnusedVar(current
);
576 if ( event
.ShiftDown() )
577 flags
|= ItemClick_Shift
;
578 if ( event
.ControlDown() )
579 flags
|= ItemClick_Ctrl
;
581 DoHandleItemClick(current
, flags
);
584 // ----------------------------------------------------------------------------
585 // wxVListBox mouse handling
586 // ----------------------------------------------------------------------------
588 void wxVListBox::OnLeftDown(wxMouseEvent
& event
)
592 int item
= HitTest(event
.GetPosition());
594 if ( item
!= wxNOT_FOUND
)
597 if ( event
.ShiftDown() )
598 flags
|= ItemClick_Shift
;
600 // under Mac Apple-click is used in the same way as Ctrl-click
603 if ( event
.MetaDown() )
605 if ( event
.ControlDown() )
607 flags
|= ItemClick_Ctrl
;
609 DoHandleItemClick(item
, flags
);
613 void wxVListBox::OnLeftDClick(wxMouseEvent
& eventMouse
)
615 int item
= HitTest(eventMouse
.GetPosition());
616 if ( item
!= wxNOT_FOUND
)
619 // if item double-clicked was not yet selected, then treat
620 // this event as a left-click instead
621 if ( item
== m_current
)
623 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, GetId());
624 event
.SetEventObject(this);
627 (void)GetEventHandler()->ProcessEvent(event
);
631 OnLeftDown(eventMouse
);
638 // ----------------------------------------------------------------------------
639 // use the same default attributes as wxListBox
640 // ----------------------------------------------------------------------------
644 wxVListBox::GetClassDefaultAttributes(wxWindowVariant variant
)
646 return wxListBox::GetClassDefaultAttributes(variant
);