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"
30 #include "wx/settings.h"
31 #include "wx/dcclient.h"
35 #include "wx/dcbuffer.h"
36 #include "wx/selstore.h"
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 BEGIN_EVENT_TABLE(wxVListBox
, wxVScrolledWindow
)
43 EVT_PAINT(wxVListBox::OnPaint
)
45 EVT_KEY_DOWN(wxVListBox::OnKeyDown
)
46 EVT_LEFT_DOWN(wxVListBox::OnLeftDown
)
47 EVT_LEFT_DCLICK(wxVListBox::OnLeftDClick
)
50 // ============================================================================
52 // ============================================================================
54 IMPLEMENT_ABSTRACT_CLASS(wxVListBox
, wxVScrolledWindow
)
56 // ----------------------------------------------------------------------------
57 // wxVListBox creation
58 // ----------------------------------------------------------------------------
60 void wxVListBox::Init()
63 m_anchor
= wxNOT_FOUND
;
65 m_doubleBuffer
= NULL
;
68 bool wxVListBox::Create(wxWindow
*parent
,
75 style
|= wxWANTS_CHARS
| wxFULL_REPAINT_ON_RESIZE
;
76 if ( !wxVScrolledWindow::Create(parent
, id
, pos
, size
, style
, name
) )
79 if ( style
& wxLB_MULTIPLE
)
80 m_selStore
= new wxSelectionStore
;
82 // make sure the native widget has the right colour since we do
83 // transparent drawing by default
84 SetBackgroundColour(GetBackgroundColour());
85 m_colBgSel
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
87 // flicker-free drawing requires this
88 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
93 wxVListBox::~wxVListBox()
95 delete m_doubleBuffer
;
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() )
266 m_anchor
= selection
;
269 DoSetCurrent(selection
);
272 size_t wxVListBox::GetSelectedCount() const
274 return m_selStore
? m_selStore
->GetSelectedCount()
275 : m_current
== wxNOT_FOUND
? 0 : 1;
278 int wxVListBox::GetFirstSelected(unsigned long& cookie
) const
282 return GetNextSelected(cookie
);
285 int wxVListBox::GetNextSelected(unsigned long& cookie
) const
287 wxCHECK_MSG( m_selStore
, wxNOT_FOUND
,
288 _T("GetFirst/NextSelected() may only be used with multiselection listboxes") );
290 while ( cookie
< GetItemCount() )
292 if ( IsSelected(cookie
++) )
299 // ----------------------------------------------------------------------------
300 // wxVListBox appearance parameters
301 // ----------------------------------------------------------------------------
303 void wxVListBox::SetMargins(const wxPoint
& pt
)
305 if ( pt
!= m_ptMargins
)
313 void wxVListBox::SetSelectionBackground(const wxColour
& col
)
318 // ----------------------------------------------------------------------------
319 // wxVListBox painting
320 // ----------------------------------------------------------------------------
322 wxCoord
wxVListBox::OnGetLineHeight(size_t line
) const
324 return OnMeasureItem(line
) + 2*m_ptMargins
.y
;
327 void wxVListBox::OnDrawSeparator(wxDC
& WXUNUSED(dc
),
328 wxRect
& WXUNUSED(rect
),
329 size_t WXUNUSED(n
)) const
333 void wxVListBox::OnDrawBackground(wxDC
& dc
, const wxRect
& rect
, size_t n
) const
335 // we need to render selected and current items differently
336 const bool isSelected
= IsSelected(n
),
337 isCurrent
= IsCurrent(n
);
338 if ( isSelected
|| isCurrent
)
342 dc
.SetBrush(wxBrush(m_colBgSel
, wxSOLID
));
346 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
349 dc
.SetPen(*(isCurrent
? wxBLACK_PEN
: wxTRANSPARENT_PEN
));
351 dc
.DrawRectangle(rect
);
353 //else: do nothing for the normal items
356 void wxVListBox::OnPaint(wxPaintEvent
& WXUNUSED(event
))
358 // If size is larger, recalculate double buffer bitmap
359 wxSize clientSize
= GetClientSize();
361 if ( !m_doubleBuffer
||
362 clientSize
.x
> m_doubleBuffer
->GetWidth() ||
363 clientSize
.y
> m_doubleBuffer
->GetHeight() )
365 delete m_doubleBuffer
;
366 m_doubleBuffer
= new wxBitmap(clientSize
.x
+25,clientSize
.y
+25);
369 wxBufferedPaintDC
dc(this,*m_doubleBuffer
);
371 // the update rectangle
372 wxRect rectUpdate
= GetUpdateClientRect();
374 // fill it with background colour
375 dc
.SetBackground(GetBackgroundColour());
378 // the bounding rectangle of the current line
380 rectLine
.width
= clientSize
.x
;
382 // iterate over all visible lines
383 const size_t lineMax
= GetVisibleEnd();
384 for ( size_t line
= GetFirstVisibleLine(); line
< lineMax
; line
++ )
386 const wxCoord hLine
= OnGetLineHeight(line
);
388 rectLine
.height
= hLine
;
390 // and draw the ones which intersect the update rect
391 if ( rectLine
.Intersects(rectUpdate
) )
393 // don't allow drawing outside of the lines rectangle
394 wxDCClipper
clip(dc
, rectLine
);
396 wxRect rect
= rectLine
;
397 OnDrawBackground(dc
, rect
, line
);
399 OnDrawSeparator(dc
, rect
, line
);
401 rect
.Deflate(m_ptMargins
.x
, m_ptMargins
.y
);
402 OnDrawItem(dc
, rect
, line
);
404 else // no intersection
406 if ( rectLine
.GetTop() > rectUpdate
.GetBottom() )
408 // we are already below the update rect, no need to continue
412 //else: the next line may intersect the update rect
419 // ============================================================================
420 // wxVListBox keyboard/mouse handling
421 // ============================================================================
423 void wxVListBox::DoHandleItemClick(int item
, int flags
)
425 // has anything worth telling the client code about happened?
428 if ( HasMultipleSelection() )
430 // select the iteem clicked?
433 // NB: the keyboard interface we implement here corresponds to
434 // wxLB_EXTENDED rather than wxLB_MULTIPLE but this one makes more
436 if ( flags
& ItemClick_Shift
)
438 if ( m_current
!= wxNOT_FOUND
)
440 if ( m_anchor
== wxNOT_FOUND
)
441 m_anchor
= m_current
;
445 // only the range from the selection anchor to new m_current
450 if ( SelectRange(m_anchor
, item
) )
453 //else: treat it as ordinary click/keypress
455 else // Shift not pressed
459 if ( flags
& ItemClick_Ctrl
)
463 if ( !(flags
& ItemClick_Kbd
) )
467 // the status of the item has definitely changed
470 //else: Ctrl-arrow pressed, don't change selection
472 //else: behave as in single selection case
477 // make the clicked item the only selection
486 // in any case the item should become the current one
487 if ( DoSetCurrent(item
) )
489 if ( !HasMultipleSelection() )
491 // this has also changed the selection for single selection case
498 // notify the user about the selection change
501 //else: nothing changed at all
504 // ----------------------------------------------------------------------------
506 // ----------------------------------------------------------------------------
508 void wxVListBox::OnKeyDown(wxKeyEvent
& event
)
510 // flags for DoHandleItemClick()
511 int flags
= ItemClick_Kbd
;
514 switch ( event
.GetKeyCode() )
521 current
= GetLineCount() - 1;
525 if ( m_current
== (int)GetLineCount() - 1 )
528 current
= m_current
+ 1;
532 if ( m_current
== wxNOT_FOUND
)
533 current
= GetLineCount() - 1;
534 else if ( m_current
!= 0 )
535 current
= m_current
- 1;
536 else // m_current == 0
542 current
= GetFirstVisibleLine();
546 if ( m_current
== (int)GetFirstVisibleLine() )
551 current
= GetFirstVisibleLine();
555 // hack: pressing space should work like a mouse click rather than
556 // like a keyboard arrow press, so trick DoHandleItemClick() in
557 // thinking we were clicked
558 flags
&= ~ItemClick_Kbd
;
564 // Since we are using wxWANTS_CHARS we need to send navigation
565 // events for the tabs on MSW
567 wxNavigationKeyEvent ne
;
568 ne
.SetDirection(!event
.ShiftDown());
569 ne
.SetCurrentFocus(this);
570 ne
.SetEventObject(this);
571 GetParent()->GetEventHandler()->ProcessEvent(ne
);
573 // fall through to default
577 current
= 0; // just to silent the stupid compiler warnings
578 wxUnusedVar(current
);
582 if ( event
.ShiftDown() )
583 flags
|= ItemClick_Shift
;
584 if ( event
.ControlDown() )
585 flags
|= ItemClick_Ctrl
;
587 DoHandleItemClick(current
, flags
);
590 // ----------------------------------------------------------------------------
591 // wxVListBox mouse handling
592 // ----------------------------------------------------------------------------
594 void wxVListBox::OnLeftDown(wxMouseEvent
& event
)
598 int item
= HitTest(event
.GetPosition());
600 if ( item
!= wxNOT_FOUND
)
603 if ( event
.ShiftDown() )
604 flags
|= ItemClick_Shift
;
606 // under Mac Apple-click is used in the same way as Ctrl-click
609 if ( event
.MetaDown() )
611 if ( event
.ControlDown() )
613 flags
|= ItemClick_Ctrl
;
615 DoHandleItemClick(item
, flags
);
619 void wxVListBox::OnLeftDClick(wxMouseEvent
& eventMouse
)
621 int item
= HitTest(eventMouse
.GetPosition());
622 if ( item
!= wxNOT_FOUND
)
625 // if item double-clicked was not yet selected, then treat
626 // this event as a left-click instead
627 if ( item
== m_current
)
629 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, GetId());
630 event
.SetEventObject(this);
633 (void)GetEventHandler()->ProcessEvent(event
);
637 OnLeftDown(eventMouse
);
644 // ----------------------------------------------------------------------------
645 // use the same default attributes as wxListBox
646 // ----------------------------------------------------------------------------
648 #include "wx/listbox.h"
652 wxVListBox::GetClassDefaultAttributes(wxWindowVariant variant
)
654 return wxListBox::GetClassDefaultAttributes(variant
);