]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/vlbox.cpp
4ae7b7652c4cab8c62761ca7159686ea08858e04
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 // Licence: wxWindows licence
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"
39 #include "wx/renderer.h"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 BEGIN_EVENT_TABLE(wxVListBox
, wxVScrolledWindow
)
46 EVT_PAINT(wxVListBox::OnPaint
)
48 EVT_KEY_DOWN(wxVListBox::OnKeyDown
)
49 EVT_LEFT_DOWN(wxVListBox::OnLeftDown
)
50 EVT_LEFT_DCLICK(wxVListBox::OnLeftDClick
)
52 EVT_SET_FOCUS(wxVListBox::OnSetOrKillFocus
)
53 EVT_KILL_FOCUS(wxVListBox::OnSetOrKillFocus
)
55 EVT_SIZE(wxVListBox::OnSize
)
58 // ============================================================================
60 // ============================================================================
62 IMPLEMENT_ABSTRACT_CLASS(wxVListBox
, wxVScrolledWindow
)
63 const char wxVListBoxNameStr
[] = "wxVListBox";
65 // ----------------------------------------------------------------------------
66 // wxVListBox creation
67 // ----------------------------------------------------------------------------
69 void wxVListBox::Init()
72 m_anchor
= wxNOT_FOUND
;
76 bool wxVListBox::Create(wxWindow
*parent
,
84 if ( (style
& wxBORDER_MASK
) == wxDEFAULT
)
85 style
|= wxBORDER_THEME
;
88 style
|= wxWANTS_CHARS
| wxFULL_REPAINT_ON_RESIZE
;
89 if ( !wxVScrolledWindow::Create(parent
, id
, pos
, size
, style
, name
) )
92 if ( style
& wxLB_MULTIPLE
)
93 m_selStore
= new wxSelectionStore
;
95 // make sure the native widget has the right colour since we do
96 // transparent drawing by default
97 SetBackgroundColour(GetBackgroundColour());
99 // leave m_colBgSel in an invalid state: it means for OnDrawBackground()
100 // to use wxRendererNative instead of painting selection bg ourselves
101 m_colBgSel
= wxNullColour
;
103 // flicker-free drawing requires this
104 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
109 wxVListBox::~wxVListBox()
114 void wxVListBox::SetItemCount(size_t count
)
116 // don't leave the current index invalid
117 if ( m_current
!= wxNOT_FOUND
&& (size_t)m_current
>= count
)
118 m_current
= count
- 1; // also ok when count == 0 as wxNOT_FOUND == -1
122 // tell the selection store that our number of items has changed
123 m_selStore
->SetItemCount(count
);
129 // ----------------------------------------------------------------------------
130 // selection handling
131 // ----------------------------------------------------------------------------
133 bool wxVListBox::IsSelected(size_t line
) const
135 return m_selStore
? m_selStore
->IsSelected(line
) : (int)line
== m_current
;
138 bool wxVListBox::Select(size_t item
, bool select
)
140 wxCHECK_MSG( m_selStore
, false,
141 wxT("Select() may only be used with multiselection listbox") );
143 wxCHECK_MSG( item
< GetItemCount(), false,
144 wxT("Select(): invalid item index") );
146 bool changed
= m_selStore
->SelectItem(item
, select
);
149 // selection really changed
158 bool wxVListBox::SelectRange(size_t from
, size_t to
)
160 wxCHECK_MSG( m_selStore
, false,
161 wxT("SelectRange() may only be used with multiselection listbox") );
163 // make sure items are in correct order
171 wxCHECK_MSG( to
< GetItemCount(), false,
172 wxT("SelectRange(): invalid item index") );
175 if ( !m_selStore
->SelectRange(from
, to
, true, &changed
) )
177 // too many items have changed, we didn't record them in changed array
178 // so we have no choice but to refresh everything between from and to
179 RefreshRows(from
, to
);
181 else // we've got the indices of the changed items
183 const size_t count
= changed
.GetCount();
190 // refresh just the lines which have really changed
191 for ( size_t n
= 0; n
< count
; n
++ )
193 RefreshRow(changed
[n
]);
201 bool wxVListBox::DoSelectAll(bool select
)
203 wxCHECK_MSG( m_selStore
, false,
204 wxT("SelectAll may only be used with multiselection listbox") );
206 size_t count
= GetItemCount();
210 if ( !m_selStore
->SelectRange(0, count
- 1, select
) ||
223 bool wxVListBox::DoSetCurrent(int current
)
225 wxASSERT_MSG( current
== wxNOT_FOUND
||
226 (current
>= 0 && (size_t)current
< GetItemCount()),
227 wxT("wxVListBox::DoSetCurrent(): invalid item index") );
229 if ( current
== m_current
)
235 if ( m_current
!= wxNOT_FOUND
)
236 RefreshRow(m_current
);
240 if ( m_current
!= wxNOT_FOUND
)
242 // if the line is not visible at all, we scroll it into view but we
243 // don't need to refresh it -- it will be redrawn anyhow
244 if ( !IsVisible(m_current
) )
246 ScrollToRow(m_current
);
248 else // line is at least partly visible
250 // it is, indeed, only partly visible, so scroll it into view to
251 // make it entirely visible
252 // BUT scrolling down when m_current is first visible makes it
253 // completely hidden, so that is even worse
254 while ( (size_t)m_current
+ 1 == GetVisibleRowsEnd() &&
255 (size_t)m_current
!= GetVisibleRowsBegin() &&
256 ScrollToRow(GetVisibleBegin() + 1) ) ;
258 // but in any case refresh it as even if it was only partly visible
259 // before we need to redraw it entirely as its background changed
260 RefreshRow(m_current
);
267 void wxVListBox::InitEvent(wxCommandEvent
& event
, int n
)
269 event
.SetEventObject(this);
273 void wxVListBox::SendSelectedEvent()
275 wxASSERT_MSG( m_current
!= wxNOT_FOUND
,
276 wxT("SendSelectedEvent() shouldn't be called") );
278 wxCommandEvent
event(wxEVT_LISTBOX
, GetId());
279 InitEvent(event
, m_current
);
280 (void)GetEventHandler()->ProcessEvent(event
);
283 void wxVListBox::SetSelection(int selection
)
285 wxCHECK_RET( selection
== wxNOT_FOUND
||
286 (selection
>= 0 && (size_t)selection
< GetItemCount()),
287 wxT("wxVListBox::SetSelection(): invalid item index") );
289 if ( HasMultipleSelection() )
291 if (selection
!= wxNOT_FOUND
)
295 m_anchor
= selection
;
298 DoSetCurrent(selection
);
301 size_t wxVListBox::GetSelectedCount() const
303 return m_selStore
? m_selStore
->GetSelectedCount()
304 : m_current
== wxNOT_FOUND
? 0 : 1;
307 int wxVListBox::GetFirstSelected(unsigned long& cookie
) const
311 return GetNextSelected(cookie
);
314 int wxVListBox::GetNextSelected(unsigned long& cookie
) const
316 wxCHECK_MSG( m_selStore
, wxNOT_FOUND
,
317 wxT("GetFirst/NextSelected() may only be used with multiselection listboxes") );
319 while ( cookie
< GetItemCount() )
321 if ( IsSelected(cookie
++) )
328 void wxVListBox::RefreshSelected()
330 // only refresh those items which are currently visible and selected:
331 for ( size_t n
= GetVisibleBegin(), end
= GetVisibleEnd(); n
< end
; n
++ )
338 wxRect
wxVListBox::GetItemRect(size_t n
) const
342 // check that this item is visible
343 const size_t lineMax
= GetVisibleEnd();
346 size_t line
= GetVisibleBegin();
352 itemrect
.y
+= itemrect
.height
;
353 itemrect
.height
= OnGetRowHeight(line
);
358 itemrect
.width
= GetClientSize().x
;
363 // ----------------------------------------------------------------------------
364 // wxVListBox appearance parameters
365 // ----------------------------------------------------------------------------
367 void wxVListBox::SetMargins(const wxPoint
& pt
)
369 if ( pt
!= m_ptMargins
)
377 void wxVListBox::SetSelectionBackground(const wxColour
& col
)
382 // ----------------------------------------------------------------------------
383 // wxVListBox painting
384 // ----------------------------------------------------------------------------
386 wxCoord
wxVListBox::OnGetRowHeight(size_t line
) const
388 return OnMeasureItem(line
) + 2*m_ptMargins
.y
;
391 void wxVListBox::OnDrawSeparator(wxDC
& WXUNUSED(dc
),
392 wxRect
& WXUNUSED(rect
),
393 size_t WXUNUSED(n
)) const
398 wxVListBox::DoDrawSolidBackground(const wxColour
& col
,
406 // we need to render selected and current items differently
407 const bool isSelected
= IsSelected(n
),
408 isCurrent
= IsCurrent(n
);
409 if ( isSelected
|| isCurrent
)
413 dc
.SetBrush(wxBrush(col
, wxBRUSHSTYLE_SOLID
));
417 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
419 dc
.SetPen(*(isCurrent
? wxBLACK_PEN
: wxTRANSPARENT_PEN
));
420 dc
.DrawRectangle(rect
);
422 //else: do nothing for the normal items
427 void wxVListBox::OnDrawBackground(wxDC
& dc
, const wxRect
& rect
, size_t n
) const
429 // use wxRendererNative for more native look unless we use custom bg colour
430 if ( !DoDrawSolidBackground(m_colBgSel
, dc
, rect
, n
) )
434 flags
|= wxCONTROL_SELECTED
;
436 flags
|= wxCONTROL_CURRENT
;
437 if ( wxWindow::FindFocus() == const_cast<wxVListBox
*>(this) )
438 flags
|= wxCONTROL_FOCUSED
;
440 wxRendererNative::Get().DrawItemSelectionRect(
441 const_cast<wxVListBox
*>(this), dc
, rect
, flags
);
445 void wxVListBox::OnPaint(wxPaintEvent
& WXUNUSED(event
))
447 wxSize clientSize
= GetClientSize();
449 wxAutoBufferedPaintDC
dc(this);
451 // the update rectangle
452 wxRect rectUpdate
= GetUpdateClientRect();
454 // fill it with background colour
455 dc
.SetBackground(GetBackgroundColour());
458 // the bounding rectangle of the current line
460 rectRow
.width
= clientSize
.x
;
462 // iterate over all visible lines
463 const size_t lineMax
= GetVisibleEnd();
464 for ( size_t line
= GetVisibleBegin(); line
< lineMax
; line
++ )
466 const wxCoord hRow
= OnGetRowHeight(line
);
468 rectRow
.height
= hRow
;
470 // and draw the ones which intersect the update rect
471 if ( rectRow
.Intersects(rectUpdate
) )
473 // don't allow drawing outside of the lines rectangle
474 wxDCClipper
clip(dc
, rectRow
);
476 wxRect rect
= rectRow
;
477 OnDrawBackground(dc
, rect
, line
);
479 OnDrawSeparator(dc
, rect
, line
);
481 rect
.Deflate(m_ptMargins
.x
, m_ptMargins
.y
);
482 OnDrawItem(dc
, rect
, line
);
484 else // no intersection
486 if ( rectRow
.GetTop() > rectUpdate
.GetBottom() )
488 // we are already below the update rect, no need to continue
492 //else: the next line may intersect the update rect
499 void wxVListBox::OnSetOrKillFocus(wxFocusEvent
& WXUNUSED(event
))
501 // we need to repaint the selection when we get the focus since
502 // wxRendererNative in general draws the focused selection differently
503 // from the unfocused selection (see OnDrawItem):
507 void wxVListBox::OnSize(wxSizeEvent
& event
)
513 // ============================================================================
514 // wxVListBox keyboard/mouse handling
515 // ============================================================================
517 void wxVListBox::DoHandleItemClick(int item
, int flags
)
519 // has anything worth telling the client code about happened?
522 if ( HasMultipleSelection() )
524 // select the iteem clicked?
527 // NB: the keyboard interface we implement here corresponds to
528 // wxLB_EXTENDED rather than wxLB_MULTIPLE but this one makes more
530 if ( flags
& ItemClick_Shift
)
532 if ( m_current
!= wxNOT_FOUND
)
534 if ( m_anchor
== wxNOT_FOUND
)
535 m_anchor
= m_current
;
539 // only the range from the selection anchor to new m_current
544 if ( SelectRange(m_anchor
, item
) )
547 //else: treat it as ordinary click/keypress
549 else // Shift not pressed
553 if ( flags
& ItemClick_Ctrl
)
557 if ( !(flags
& ItemClick_Kbd
) )
561 // the status of the item has definitely changed
564 //else: Ctrl-arrow pressed, don't change selection
566 //else: behave as in single selection case
571 // make the clicked item the only selection
580 // in any case the item should become the current one
581 if ( DoSetCurrent(item
) )
583 if ( !HasMultipleSelection() )
585 // this has also changed the selection for single selection case
592 // notify the user about the selection change
595 //else: nothing changed at all
598 // ----------------------------------------------------------------------------
600 // ----------------------------------------------------------------------------
602 void wxVListBox::OnKeyDown(wxKeyEvent
& event
)
604 // flags for DoHandleItemClick()
605 int flags
= ItemClick_Kbd
;
608 switch ( event
.GetKeyCode() )
611 case WXK_NUMPAD_HOME
:
617 current
= GetRowCount() - 1;
621 case WXK_NUMPAD_DOWN
:
622 if ( m_current
== (int)GetRowCount() - 1 )
625 current
= m_current
+ 1;
630 if ( m_current
== wxNOT_FOUND
)
631 current
= GetRowCount() - 1;
632 else if ( m_current
!= 0 )
633 current
= m_current
- 1;
634 else // m_current == 0
639 case WXK_NUMPAD_PAGEDOWN
:
641 current
= GetVisibleBegin();
645 case WXK_NUMPAD_PAGEUP
:
646 if ( m_current
== (int)GetVisibleBegin() )
651 current
= GetVisibleBegin();
655 // hack: pressing space should work like a mouse click rather than
656 // like a keyboard arrow press, so trick DoHandleItemClick() in
657 // thinking we were clicked
658 flags
&= ~ItemClick_Kbd
;
664 // Since we are using wxWANTS_CHARS we need to send navigation
665 // events for the tabs on MSW
666 HandleAsNavigationKey(event
);
667 // fall through to default
671 current
= 0; // just to silent the stupid compiler warnings
672 wxUnusedVar(current
);
676 if ( event
.ShiftDown() )
677 flags
|= ItemClick_Shift
;
678 if ( event
.ControlDown() )
679 flags
|= ItemClick_Ctrl
;
681 DoHandleItemClick(current
, flags
);
684 // ----------------------------------------------------------------------------
685 // wxVListBox mouse handling
686 // ----------------------------------------------------------------------------
688 void wxVListBox::OnLeftDown(wxMouseEvent
& event
)
692 int item
= VirtualHitTest(event
.GetPosition().y
);
694 if ( item
!= wxNOT_FOUND
)
697 if ( event
.ShiftDown() )
698 flags
|= ItemClick_Shift
;
700 // under Mac Apple-click is used in the same way as Ctrl-click
703 if ( event
.MetaDown() )
705 if ( event
.ControlDown() )
707 flags
|= ItemClick_Ctrl
;
709 DoHandleItemClick(item
, flags
);
713 void wxVListBox::OnLeftDClick(wxMouseEvent
& eventMouse
)
715 int item
= VirtualHitTest(eventMouse
.GetPosition().y
);
716 if ( item
!= wxNOT_FOUND
)
719 // if item double-clicked was not yet selected, then treat
720 // this event as a left-click instead
721 if ( item
== m_current
)
723 wxCommandEvent
event(wxEVT_LISTBOX_DCLICK
, GetId());
724 InitEvent(event
, item
);
725 (void)GetEventHandler()->ProcessEvent(event
);
729 OnLeftDown(eventMouse
);
736 // ----------------------------------------------------------------------------
737 // use the same default attributes as wxListBox
738 // ----------------------------------------------------------------------------
742 wxVListBox::GetClassDefaultAttributes(wxWindowVariant variant
)
744 return wxListBox::GetClassDefaultAttributes(variant
);