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
;
67 m_doubleBuffer
= NULL
;
70 bool wxVListBox::Create(wxWindow
*parent
,
77 style
|= wxWANTS_CHARS
| wxFULL_REPAINT_ON_RESIZE
;
78 if ( !wxVScrolledWindow::Create(parent
, id
, pos
, size
, style
, name
) )
81 if ( style
& wxLB_MULTIPLE
)
82 m_selStore
= new wxSelectionStore
;
84 // make sure the native widget has the right colour since we do
85 // transparent drawing by default
86 SetBackgroundColour(GetBackgroundColour());
87 m_colBgSel
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
89 // flicker-free drawing requires this
90 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
95 wxVListBox::~wxVListBox()
97 delete m_doubleBuffer
;
101 void wxVListBox::SetItemCount(size_t count
)
105 // tell the selection store that our number of items has changed
106 m_selStore
->SetItemCount(count
);
112 // ----------------------------------------------------------------------------
113 // selection handling
114 // ----------------------------------------------------------------------------
116 bool wxVListBox::IsSelected(size_t line
) const
118 return m_selStore
? m_selStore
->IsSelected(line
) : (int)line
== m_current
;
121 bool wxVListBox::Select(size_t item
, bool select
)
123 wxCHECK_MSG( m_selStore
, false,
124 _T("Select() may only be used with multiselection listbox") );
126 wxCHECK_MSG( item
< GetItemCount(), false,
127 _T("Select(): invalid item index") );
129 bool changed
= m_selStore
->SelectItem(item
, select
);
132 // selection really changed
141 bool wxVListBox::SelectRange(size_t from
, size_t to
)
143 wxCHECK_MSG( m_selStore
, false,
144 _T("SelectRange() may only be used with multiselection listbox") );
146 // make sure items are in correct order
154 wxCHECK_MSG( to
< GetItemCount(), false,
155 _T("SelectRange(): invalid item index") );
158 if ( !m_selStore
->SelectRange(from
, to
, true, &changed
) )
160 // too many items have changed, we didn't record them in changed array
161 // so we have no choice but to refresh everything between from and to
162 RefreshLines(from
, to
);
164 else // we've got the indices of the changed items
166 const size_t count
= changed
.GetCount();
173 // refresh just the lines which have really changed
174 for ( size_t n
= 0; n
< count
; n
++ )
176 RefreshLine(changed
[n
]);
184 bool wxVListBox::DoSelectAll(bool select
)
186 wxCHECK_MSG( m_selStore
, false,
187 _T("SelectAll may only be used with multiselection listbox") );
189 size_t count
= GetItemCount();
193 if ( !m_selStore
->SelectRange(0, count
- 1, select
) ||
206 bool wxVListBox::DoSetCurrent(int current
)
208 wxASSERT_MSG( current
== wxNOT_FOUND
||
209 (current
>= 0 && (size_t)current
< GetItemCount()),
210 _T("wxVListBox::DoSetCurrent(): invalid item index") );
212 if ( current
== m_current
)
218 if ( m_current
!= wxNOT_FOUND
)
219 RefreshLine(m_current
);
223 if ( m_current
!= wxNOT_FOUND
)
225 // if the line is not visible at all, we scroll it into view but we
226 // don't need to refresh it -- it will be redrawn anyhow
227 if ( !IsVisible(m_current
) )
229 ScrollToLine(m_current
);
231 else // line is at least partly visible
233 // it is, indeed, only partly visible, so scroll it into view to
234 // make it entirely visible
235 while ( (size_t)m_current
== GetLastVisibleLine() &&
236 ScrollToLine(GetVisibleBegin()+1) ) ;
238 // but in any case refresh it as even if it was only partly visible
239 // before we need to redraw it entirely as its background changed
240 RefreshLine(m_current
);
247 void wxVListBox::SendSelectedEvent()
249 wxASSERT_MSG( m_current
!= wxNOT_FOUND
,
250 _T("SendSelectedEvent() shouldn't be called") );
252 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, GetId());
253 event
.SetEventObject(this);
254 event
.SetInt(m_current
);
256 (void)GetEventHandler()->ProcessEvent(event
);
259 void wxVListBox::SetSelection(int selection
)
261 wxCHECK_RET( selection
== wxNOT_FOUND
||
262 (selection
>= 0 && (size_t)selection
< GetItemCount()),
263 _T("wxVListBox::SetSelection(): invalid item index") );
265 if ( HasMultipleSelection() )
268 m_anchor
= selection
;
271 DoSetCurrent(selection
);
274 size_t wxVListBox::GetSelectedCount() const
276 return m_selStore
? m_selStore
->GetSelectedCount()
277 : m_current
== wxNOT_FOUND
? 0 : 1;
280 int wxVListBox::GetFirstSelected(unsigned long& cookie
) const
284 return GetNextSelected(cookie
);
287 int wxVListBox::GetNextSelected(unsigned long& cookie
) const
289 wxCHECK_MSG( m_selStore
, wxNOT_FOUND
,
290 _T("GetFirst/NextSelected() may only be used with multiselection listboxes") );
292 while ( cookie
< GetItemCount() )
294 if ( IsSelected(cookie
++) )
301 // ----------------------------------------------------------------------------
302 // wxVListBox appearance parameters
303 // ----------------------------------------------------------------------------
305 void wxVListBox::SetMargins(const wxPoint
& pt
)
307 if ( pt
!= m_ptMargins
)
315 void wxVListBox::SetSelectionBackground(const wxColour
& col
)
320 // ----------------------------------------------------------------------------
321 // wxVListBox painting
322 // ----------------------------------------------------------------------------
324 wxCoord
wxVListBox::OnGetLineHeight(size_t line
) const
326 return OnMeasureItem(line
) + 2*m_ptMargins
.y
;
329 void wxVListBox::OnDrawSeparator(wxDC
& WXUNUSED(dc
),
330 wxRect
& WXUNUSED(rect
),
331 size_t WXUNUSED(n
)) const
335 void wxVListBox::OnDrawBackground(wxDC
& dc
, const wxRect
& rect
, size_t n
) const
337 // we need to render selected and current items differently
338 const bool isSelected
= IsSelected(n
),
339 isCurrent
= IsCurrent(n
);
340 if ( isSelected
|| isCurrent
)
344 dc
.SetBrush(wxBrush(m_colBgSel
, wxSOLID
));
348 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
351 dc
.SetPen(*(isCurrent
? wxBLACK_PEN
: wxTRANSPARENT_PEN
));
353 dc
.DrawRectangle(rect
);
355 //else: do nothing for the normal items
358 void wxVListBox::OnPaint(wxPaintEvent
& WXUNUSED(event
))
360 // If size is larger, recalculate double buffer bitmap
361 wxSize clientSize
= GetClientSize();
363 if ( !m_doubleBuffer
||
364 clientSize
.x
> m_doubleBuffer
->GetWidth() ||
365 clientSize
.y
> m_doubleBuffer
->GetHeight() )
367 delete m_doubleBuffer
;
368 m_doubleBuffer
= new wxBitmap(clientSize
.x
+25,clientSize
.y
+25);
371 wxBufferedPaintDC
dc(this,*m_doubleBuffer
);
373 // the update rectangle
374 wxRect rectUpdate
= GetUpdateClientRect();
376 // fill it with background colour
377 dc
.SetBackground(GetBackgroundColour());
380 // the bounding rectangle of the current line
382 rectLine
.width
= clientSize
.x
;
384 // iterate over all visible lines
385 const size_t lineMax
= GetVisibleEnd();
386 for ( size_t line
= GetFirstVisibleLine(); line
< lineMax
; line
++ )
388 const wxCoord hLine
= OnGetLineHeight(line
);
390 rectLine
.height
= hLine
;
392 // and draw the ones which intersect the update rect
393 if ( rectLine
.Intersects(rectUpdate
) )
395 // don't allow drawing outside of the lines rectangle
396 wxDCClipper
clip(dc
, rectLine
);
398 wxRect rect
= rectLine
;
399 OnDrawBackground(dc
, rect
, line
);
401 OnDrawSeparator(dc
, rect
, line
);
403 rect
.Deflate(m_ptMargins
.x
, m_ptMargins
.y
);
404 OnDrawItem(dc
, rect
, line
);
406 else // no intersection
408 if ( rectLine
.GetTop() > rectUpdate
.GetBottom() )
410 // we are already below the update rect, no need to continue
414 //else: the next line may intersect the update rect
421 // ============================================================================
422 // wxVListBox keyboard/mouse handling
423 // ============================================================================
425 void wxVListBox::DoHandleItemClick(int item
, int flags
)
427 // has anything worth telling the client code about happened?
430 if ( HasMultipleSelection() )
432 // select the iteem clicked?
435 // NB: the keyboard interface we implement here corresponds to
436 // wxLB_EXTENDED rather than wxLB_MULTIPLE but this one makes more
438 if ( flags
& ItemClick_Shift
)
440 if ( m_current
!= wxNOT_FOUND
)
442 if ( m_anchor
== wxNOT_FOUND
)
443 m_anchor
= m_current
;
447 // only the range from the selection anchor to new m_current
452 if ( SelectRange(m_anchor
, item
) )
455 //else: treat it as ordinary click/keypress
457 else // Shift not pressed
461 if ( flags
& ItemClick_Ctrl
)
465 if ( !(flags
& ItemClick_Kbd
) )
469 // the status of the item has definitely changed
472 //else: Ctrl-arrow pressed, don't change selection
474 //else: behave as in single selection case
479 // make the clicked item the only selection
488 // in any case the item should become the current one
489 if ( DoSetCurrent(item
) )
491 if ( !HasMultipleSelection() )
493 // this has also changed the selection for single selection case
500 // notify the user about the selection change
503 //else: nothing changed at all
506 // ----------------------------------------------------------------------------
508 // ----------------------------------------------------------------------------
510 void wxVListBox::OnKeyDown(wxKeyEvent
& event
)
512 // flags for DoHandleItemClick()
513 int flags
= ItemClick_Kbd
;
516 switch ( event
.GetKeyCode() )
523 current
= GetLineCount() - 1;
527 if ( m_current
== (int)GetLineCount() - 1 )
530 current
= m_current
+ 1;
534 if ( m_current
== wxNOT_FOUND
)
535 current
= GetLineCount() - 1;
536 else if ( m_current
!= 0 )
537 current
= m_current
- 1;
538 else // m_current == 0
544 current
= GetFirstVisibleLine();
548 if ( m_current
== (int)GetFirstVisibleLine() )
553 current
= GetFirstVisibleLine();
557 // hack: pressing space should work like a mouse click rather than
558 // like a keyboard arrow press, so trick DoHandleItemClick() in
559 // thinking we were clicked
560 flags
&= ~ItemClick_Kbd
;
566 // Since we are using wxWANTS_CHARS we need to send navigation
567 // events for the tabs on MSW
569 wxNavigationKeyEvent ne
;
570 ne
.SetDirection(!event
.ShiftDown());
571 ne
.SetCurrentFocus(this);
572 ne
.SetEventObject(this);
573 GetParent()->GetEventHandler()->ProcessEvent(ne
);
575 // fall through to default
579 current
= 0; // just to silent the stupid compiler warnings
580 wxUnusedVar(current
);
584 if ( event
.ShiftDown() )
585 flags
|= ItemClick_Shift
;
586 if ( event
.ControlDown() )
587 flags
|= ItemClick_Ctrl
;
589 DoHandleItemClick(current
, flags
);
592 // ----------------------------------------------------------------------------
593 // wxVListBox mouse handling
594 // ----------------------------------------------------------------------------
596 void wxVListBox::OnLeftDown(wxMouseEvent
& event
)
600 int item
= HitTest(event
.GetPosition());
602 if ( item
!= wxNOT_FOUND
)
605 if ( event
.ShiftDown() )
606 flags
|= ItemClick_Shift
;
608 // under Mac Apple-click is used in the same way as Ctrl-click
611 if ( event
.MetaDown() )
613 if ( event
.ControlDown() )
615 flags
|= ItemClick_Ctrl
;
617 DoHandleItemClick(item
, flags
);
621 void wxVListBox::OnLeftDClick(wxMouseEvent
& eventMouse
)
623 int item
= HitTest(eventMouse
.GetPosition());
624 if ( item
!= wxNOT_FOUND
)
627 // if item double-clicked was not yet selected, then treat
628 // this event as a left-click instead
629 if ( item
== m_current
)
631 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, GetId());
632 event
.SetEventObject(this);
635 (void)GetEventHandler()->ProcessEvent(event
);
639 OnLeftDown(eventMouse
);
646 // ----------------------------------------------------------------------------
647 // use the same default attributes as wxListBox
648 // ----------------------------------------------------------------------------
652 wxVListBox::GetClassDefaultAttributes(wxWindowVariant variant
)
654 return wxListBox::GetClassDefaultAttributes(variant
);