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 IMPLEMENT_ABSTRACT_CLASS(wxVListBox
, wxVScrolledWindow
)
53 // ----------------------------------------------------------------------------
54 // wxVListBox creation
55 // ----------------------------------------------------------------------------
57 void wxVListBox::Init()
60 m_anchor
= wxNOT_FOUND
;
64 bool wxVListBox::Create(wxWindow
*parent
,
71 style
|= wxWANTS_CHARS
| wxFULL_REPAINT_ON_RESIZE
;
72 if ( !wxVScrolledWindow::Create(parent
, id
, pos
, size
, style
, name
) )
75 if ( style
& wxLB_MULTIPLE
)
76 m_selStore
= new wxSelectionStore
;
78 // make sure the native widget has the right colour since we do
79 // transparent drawing by default
80 SetBackgroundColour(GetBackgroundColour());
81 m_colBgSel
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
86 wxVListBox::~wxVListBox()
91 void wxVListBox::SetItemCount(size_t count
)
95 // tell the selection store that our number of items has changed
96 m_selStore
->SetItemCount(count
);
102 // ----------------------------------------------------------------------------
103 // selection handling
104 // ----------------------------------------------------------------------------
106 bool wxVListBox::IsSelected(size_t line
) const
108 return m_selStore
? m_selStore
->IsSelected(line
) : (int)line
== m_current
;
111 bool wxVListBox::Select(size_t item
, bool select
)
113 wxCHECK_MSG( m_selStore
, false,
114 _T("Select() may only be used with multiselection listbox") );
116 wxCHECK_MSG( item
< GetItemCount(), false,
117 _T("Select(): invalid item index") );
119 bool changed
= m_selStore
->SelectItem(item
, select
);
122 // selection really changed
131 bool wxVListBox::SelectRange(size_t from
, size_t to
)
133 wxCHECK_MSG( m_selStore
, false,
134 _T("SelectRange() may only be used with multiselection listbox") );
136 // make sure items are in correct order
144 wxCHECK_MSG( to
< GetItemCount(), false,
145 _T("SelectRange(): invalid item index") );
148 if ( !m_selStore
->SelectRange(from
, to
, true, &changed
) )
150 // too many items have changed, we didn't record them in changed array
151 // so we have no choice but to refresh everything between from and to
152 RefreshLines(from
, to
);
154 else // we've got the indices of the changed items
156 const size_t count
= changed
.GetCount();
163 // refresh just the lines which have really changed
164 for ( size_t n
= 0; n
< count
; n
++ )
166 RefreshLine(changed
[n
]);
174 bool wxVListBox::DoSelectAll(bool select
)
176 wxCHECK_MSG( m_selStore
, false,
177 _T("SelectAll may only be used with multiselection listbox") );
179 size_t count
= GetItemCount();
183 if ( !m_selStore
->SelectRange(0, count
- 1, select
) ||
196 bool wxVListBox::DoSetCurrent(int current
)
198 wxASSERT_MSG( current
== wxNOT_FOUND
||
199 (current
>= 0 && (size_t)current
< GetItemCount()),
200 _T("wxVListBox::DoSetCurrent(): invalid item index") );
202 if ( current
== m_current
)
208 if ( m_current
!= wxNOT_FOUND
)
209 RefreshLine(m_current
);
213 if ( m_current
!= wxNOT_FOUND
)
215 // if the line is not visible at all, we scroll it into view but we
216 // don't need to refresh it -- it will be redrawn anyhow
217 if ( !IsVisible(m_current
) )
219 ScrollToLine(m_current
);
221 else // line is at least partly visible
223 // it is, indeed, only partly visible, so scroll it into view to
224 // make it entirely visible
225 if ( (size_t)m_current
== GetLastVisibleLine() )
227 ScrollToLine(m_current
);
230 // but in any case refresh it as even if it was only partly visible
231 // before we need to redraw it entirely as its background changed
232 RefreshLine(m_current
);
239 void wxVListBox::SendSelectedEvent()
241 wxASSERT_MSG( m_current
!= wxNOT_FOUND
,
242 _T("SendSelectedEvent() shouldn't be called") );
244 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, GetId());
245 event
.SetEventObject(this);
246 event
.m_commandInt
= m_current
;
248 (void)GetEventHandler()->ProcessEvent(event
);
251 void wxVListBox::SetSelection(int selection
)
253 wxCHECK_RET( selection
== wxNOT_FOUND
||
254 (selection
>= 0 && (size_t)selection
< GetItemCount()),
255 _T("wxVListBox::SetSelection(): invalid item index") );
257 if ( HasMultipleSelection() )
260 m_anchor
= selection
;
263 DoSetCurrent(selection
);
266 size_t wxVListBox::GetSelectedCount() const
268 return m_selStore
? m_selStore
->GetSelectedCount()
269 : m_current
== wxNOT_FOUND
? 0 : 1;
272 int wxVListBox::GetFirstSelected(unsigned long& cookie
) const
276 return GetNextSelected(cookie
);
279 int wxVListBox::GetNextSelected(unsigned long& cookie
) const
281 wxCHECK_MSG( m_selStore
, wxNOT_FOUND
,
282 _T("GetFirst/NextSelected() may only be used with multiselection listboxes") );
284 while ( cookie
< GetItemCount() )
286 if ( IsSelected(cookie
++) )
293 // ----------------------------------------------------------------------------
294 // wxVListBox appearance parameters
295 // ----------------------------------------------------------------------------
297 void wxVListBox::SetMargins(const wxPoint
& pt
)
299 if ( pt
!= m_ptMargins
)
307 void wxVListBox::SetSelectionBackground(const wxColour
& col
)
312 // ----------------------------------------------------------------------------
313 // wxVListBox painting
314 // ----------------------------------------------------------------------------
316 wxCoord
wxVListBox::OnGetLineHeight(size_t line
) const
318 return OnMeasureItem(line
) + 2*m_ptMargins
.y
;
321 void wxVListBox::OnDrawSeparator(wxDC
& WXUNUSED(dc
),
322 wxRect
& WXUNUSED(rect
),
323 size_t WXUNUSED(n
)) const
327 void wxVListBox::OnDrawBackground(wxDC
& dc
, const wxRect
& rect
, size_t n
) const
329 // we need to render selected and current items differently
330 const bool isSelected
= IsSelected(n
),
331 isCurrent
= IsCurrent(n
);
332 if ( isSelected
|| isCurrent
)
336 dc
.SetBrush(wxBrush(m_colBgSel
, wxSOLID
));
340 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
343 dc
.SetPen(*(isCurrent
? wxBLACK_PEN
: wxTRANSPARENT_PEN
));
345 dc
.DrawRectangle(rect
);
347 //else: do nothing for the normal items
350 void wxVListBox::OnPaint(wxPaintEvent
& WXUNUSED(event
))
354 // the update rectangle
355 wxRect rectUpdate
= GetUpdateClientRect();
357 // the bounding rectangle of the current line
359 rectLine
.width
= GetClientSize().x
;
361 // iterate over all visible lines
362 const size_t lineMax
= GetLastVisibleLine();
363 for ( size_t line
= GetFirstVisibleLine(); line
<= lineMax
; line
++ )
365 const wxCoord hLine
= OnGetLineHeight(line
);
367 rectLine
.height
= hLine
;
369 // and draw the ones which intersect the update rect
370 if ( rectLine
.Intersects(rectUpdate
) )
372 // don't allow drawing outside of the lines rectangle
373 wxDCClipper
clip(dc
, rectLine
);
375 wxRect rect
= rectLine
;
376 OnDrawBackground(dc
, rect
, line
);
378 OnDrawSeparator(dc
, rect
, line
);
380 rect
.Deflate(m_ptMargins
.x
, m_ptMargins
.y
);
381 OnDrawItem(dc
, rect
, line
);
383 else // no intersection
385 if ( rectLine
.GetTop() > rectUpdate
.GetBottom() )
387 // we are already below the update rect, no need to continue
391 //else: the next line may intersect the update rect
398 // ============================================================================
399 // wxVListBox keyboard/mouse handling
400 // ============================================================================
402 void wxVListBox::DoHandleItemClick(int item
, int flags
)
404 // has anything worth telling the client code about happened?
407 if ( HasMultipleSelection() )
409 // select the iteem clicked?
412 // NB: the keyboard interface we implement here corresponds to
413 // wxLB_EXTENDED rather than wxLB_MULTIPLE but this one makes more
415 if ( flags
& ItemClick_Shift
)
417 if ( m_current
!= wxNOT_FOUND
)
419 if ( m_anchor
== wxNOT_FOUND
)
420 m_anchor
= m_current
;
424 // only the range from the selection anchor to new m_current
429 if ( SelectRange(m_anchor
, item
) )
432 //else: treat it as ordinary click/keypress
434 else // Shift not pressed
438 if ( flags
& ItemClick_Ctrl
)
442 if ( !(flags
& ItemClick_Kbd
) )
446 // the status of the item has definitely changed
449 //else: Ctrl-arrow pressed, don't change selection
451 //else: behave as in single selection case
456 // make the clicked item the only selection
465 // in any case the item should become the current one
466 if ( DoSetCurrent(item
) )
468 if ( !HasMultipleSelection() )
470 // this has also changed the selection for single selection case
477 // notify the user about the selection change
480 //else: nothing changed at all
483 // ----------------------------------------------------------------------------
485 // ----------------------------------------------------------------------------
487 void wxVListBox::OnKeyDown(wxKeyEvent
& event
)
489 // flags for DoHandleItemClick()
490 int flags
= ItemClick_Kbd
;
493 switch ( event
.GetKeyCode() )
500 current
= GetLineCount() - 1;
504 if ( m_current
== (int)GetLineCount() - 1 )
507 current
= m_current
+ 1;
511 if ( m_current
== wxNOT_FOUND
)
512 current
= GetLineCount() - 1;
513 else if ( m_current
!= 0 )
514 current
= m_current
- 1;
515 else // m_current == 0
522 current
= GetFirstVisibleLine();
527 if ( m_current
== (int)GetFirstVisibleLine() )
532 current
= GetFirstVisibleLine();
536 // hack: pressing space should work like a mouse click rather than
537 // like a keyboard arrow press, so trick DoHandleItemClick() in
538 // thinking we were clicked
539 flags
&= ~ItemClick_Kbd
;
545 // Since we are using wxWANTS_CHARS we need to send navigation
546 // events for the tabs on MSW
548 wxNavigationKeyEvent ne
;
549 ne
.SetDirection(!event
.ShiftDown());
550 ne
.SetCurrentFocus(this);
551 ne
.SetEventObject(this);
552 GetParent()->GetEventHandler()->ProcessEvent(ne
);
554 // fall through to default
558 current
= 0; // just to silent the stupid compiler warnings
559 wxUnusedVar(current
);
563 if ( event
.ShiftDown() )
564 flags
|= ItemClick_Shift
;
565 if ( event
.ControlDown() )
566 flags
|= ItemClick_Ctrl
;
568 DoHandleItemClick(current
, flags
);
571 // ----------------------------------------------------------------------------
572 // wxVListBox mouse handling
573 // ----------------------------------------------------------------------------
575 void wxVListBox::OnLeftDown(wxMouseEvent
& event
)
577 int item
= HitTest(event
.GetPosition());
579 if ( item
!= wxNOT_FOUND
)
582 if ( event
.ShiftDown() )
583 flags
|= ItemClick_Shift
;
585 // under Mac Apple-click is used in the same way as Ctrl-click
588 if ( event
.MetaDown() )
590 if ( event
.ControlDown() )
592 flags
|= ItemClick_Ctrl
;
594 DoHandleItemClick(item
, flags
);
598 void wxVListBox::OnLeftDClick(wxMouseEvent
& event
)
600 int item
= HitTest(event
.GetPosition());
601 if ( item
!= wxNOT_FOUND
)
603 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, GetId());
604 event
.SetEventObject(this);
605 event
.m_commandInt
= item
;
607 (void)GetEventHandler()->ProcessEvent(event
);
612 // ----------------------------------------------------------------------------
613 // use the same default attributes as wxListBox
614 // ----------------------------------------------------------------------------
616 #include "wx/listbox.h"
620 wxVListBox::GetClassDefaultAttributes(wxWindowVariant variant
)
622 return wxListBox::GetClassDefaultAttributes(variant
);