int wxListBox::GetSelection() const { wxCHECK_MSG( !HasMultipleSelection(), -1, _T("use wxListBox::GetSelections for ths listbox") ); return m_selections.IsEmpty() ? -1 : m_selections[0]; }
void wxListBoxBase::SetSelection(int n) { if ( !HasMultipleSelection() ) DoChangeSingleSelection(n); DoSetSelection(n, true); }
// Get single selection, for single choice list items int wxListBox::GetSelection() const { wxCHECK_MSG( !HasMultipleSelection(), -1, wxT("GetSelection() can't be used with multiple-selection listboxes, use GetSelections() instead.") ); return ListBox_GetCurSel(GetHwnd()); }
bool wxListBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) { wxEventType evtType; int n = wxNOT_FOUND; if ( param == LBN_SELCHANGE ) { if ( HasMultipleSelection() ) return CalcAndSendEvent(); evtType = wxEVT_COMMAND_LISTBOX_SELECTED; if ( m_selectedByKeyboard ) { // We shouldn't use the mouse position to find the item as mouse // can be anywhere, ask the listbox itself. Notice that this can't // be used when the item is selected using the mouse however as // LB_GETCARETINDEX will always return a valid item, even if the // mouse is clicked below all the items, which is why we find the // item ourselves below in this case. n = SendMessage(GetHwnd(), LB_GETCARETINDEX, 0, 0); } //else: n will be determined below from the mouse position } else if ( param == LBN_DBLCLK ) { evtType = wxEVT_COMMAND_LISTBOX_DOUBLECLICKED; } else { // some event we're not interested in return false; } // Find the item position if it was a mouse-generated selection event or a // double click event (which is always generated using the mouse) if ( n == wxNOT_FOUND ) { const DWORD pos = ::GetMessagePos(); const wxPoint pt(GET_X_LPARAM(pos), GET_Y_LPARAM(pos)); n = HitTest(ScreenToClient(wxPoint(pt))); } // We get events even when mouse is clicked outside of any valid item from // Windows, just ignore them. if ( n == wxNOT_FOUND ) return false; if ( param == LBN_SELCHANGE ) { if ( !DoChangeSingleSelection(n) ) return false; } // Do generate an event otherwise. return SendEvent(evtType, n, true /* selection */); }
void wxListBox::DoSetSelection(int n, bool select) { if ( select ) { if ( m_selections.Index(n) == wxNOT_FOUND ) { if ( !HasMultipleSelection() ) { // selecting an item in a single selection listbox deselects // all the others DeselectAll(); } m_selections.Add(n); RefreshItem(n); } //else: already selected } else // unselect { int index = m_selections.Index(n); if ( index != wxNOT_FOUND ) { m_selections.RemoveAt(index); RefreshItem(n); } //else: not selected } // sanity check: a single selection listbox can't have more than one item // selected wxASSERT_MSG( HasMultipleSelection() || (m_selections.GetCount() < 2), _T("multiple selected items in single selection lbox?") ); if ( select ) { // the newly selected item becomes the current one SetCurrentItem(n); } }
void wxListBox::DoSetSelection(int N, bool select) { wxCHECK_RET( N == wxNOT_FOUND || IsValid(N), wxT("invalid index in wxListBox::SetSelection") ); if ( HasMultipleSelection() ) { SendMessage(GetHwnd(), LB_SETSEL, select, N); } else { SendMessage(GetHwnd(), LB_SETCURSEL, select ? N : -1, 0); } }
int wxListBox::GetSelection() const { wxCHECK_MSG( !HasMultipleSelection(), -1, wxT("GetSelection() can't be used with multiple-selection " "listboxes, use GetSelections() instead.") ); return(LONGFROMMR(::WinSendMsg( GetHwnd() ,LM_QUERYSELECTION ,(MPARAM)LIT_FIRST ,(MPARAM)0 ) )); } // end of wxListBox::GetSelection
void wxListBox::MacSetSelection( int n , bool select ) { bool former = MacSuppressSelection( true ) ; UInt32 id = n + 1 ; if ( m_peer->IsItemSelected( id ) != select ) { if ( select ) verify_noerr(m_peer->SetSelectedItems( 1 , & id , HasMultipleSelection() ? kDataBrowserItemsAdd : kDataBrowserItemsAssign ) ) ; else verify_noerr(m_peer->SetSelectedItems( 1 , & id , kDataBrowserItemsRemove ) ) ; } MacScrollTo( n ) ; MacSuppressSelection( former ) ; }
void wxListBox::DoSetSelection(int n, bool select) { wxCHECK_RET( n == wxNOT_FOUND || IsValid(n), wxT("invalid index in wxListBox::SetSelection") ); m_blockEvents = true; if ( n == wxNOT_FOUND ) GetListPeer()->ListDeselectAll(); else GetListPeer()->ListSetSelection( n, select, HasMultipleSelection() ); m_blockEvents = false; UpdateOldSelections(); }
void wxVListBox::SetSelection(int selection) { wxCHECK_RET( selection == wxNOT_FOUND || (selection >= 0 && (size_t)selection < GetItemCount()), _T("wxVListBox::SetSelection(): invalid item index") ); if ( HasMultipleSelection() ) { if (selection != wxNOT_FOUND) Select(selection); else DeselectAll(); m_anchor = selection; } DoSetCurrent(selection); }
bool wxListBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) { if ((param == LBN_SELCHANGE) && HasMultipleSelection()) { CalcAndSendEvent(); return true; } wxEventType evtType; int n; if ( param == LBN_SELCHANGE ) { evtType = wxEVT_COMMAND_LISTBOX_SELECTED; n = SendMessage(GetHwnd(), LB_GETCARETINDEX, 0, 0); // NB: conveniently enough, LB_ERR is the same as wxNOT_FOUND } else if ( param == LBN_DBLCLK ) { evtType = wxEVT_COMMAND_LISTBOX_DOUBLECLICKED; n = HitTest(ScreenToClient(wxGetMousePosition())); } else { // some event we're not interested in return false; } // retrieve the affected item if ( n == wxNOT_FOUND ) return false; wxCommandEvent event(evtType, m_windowId); event.SetEventObject(this); if ( HasClientObjectData() ) event.SetClientObject( GetClientObject(n) ); else if ( HasClientUntypedData() ) event.SetClientData( GetClientData(n) ); event.SetString(GetString(n)); event.SetInt(n); return HandleWindowEvent(event); }
bool wxListBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) { wxEventType evtType; if ( param == LBN_SELCHANGE ) { if ( HasMultipleSelection() ) return CalcAndSendEvent(); evtType = wxEVT_LISTBOX; } else if ( param == LBN_DBLCLK ) { // Clicking under the last item in the listbox generates double click // event for the currently selected item which is rather surprising. // Avoid the surprise by checking that we do have an item under mouse. const DWORD pos = ::GetMessagePos(); const wxPoint pt(GET_X_LPARAM(pos), GET_Y_LPARAM(pos)); if ( HitTest(ScreenToClient(pt)) == wxNOT_FOUND ) return false; evtType = wxEVT_LISTBOX_DCLICK; } else { // some event we're not interested in return false; } const int n = ListBox_GetCurSel(GetHwnd()); // We get events even when mouse is clicked outside of any valid item from // Windows, just ignore them. if ( n == wxNOT_FOUND ) return false; if ( param == LBN_SELCHANGE ) { if ( !DoChangeSingleSelection(n) ) return false; } // Do generate an event otherwise. return SendEvent(evtType, n, true /* selection */); }
void wxListBox::DoSetSelection(int N, bool select) { wxCHECK_RET( N == wxNOT_FOUND || IsValid(N), wxT("invalid index in wxListBox::SetSelection") ); if ( HasMultipleSelection() ) { // Setting selection to -1 should deselect everything. const bool deselectAll = N == wxNOT_FOUND; SendMessage(GetHwnd(), LB_SETSEL, deselectAll ? FALSE : select, deselectAll ? -1 : N); } else { SendMessage(GetHwnd(), LB_SETCURSEL, select ? N : -1, 0); } UpdateOldSelections(); }
// Return number of selections and an array of selected integers int wxListBox::GetSelections(wxArrayInt& aSelections) const { aSelections.Empty(); if ( HasMultipleSelection() ) { int countSel = ListBox_GetSelCount(GetHwnd()); if ( countSel == LB_ERR ) { wxLogDebug(wxT("ListBox_GetSelCount failed")); } else if ( countSel != 0 ) { int *selections = new int[countSel]; if ( ListBox_GetSelItems(GetHwnd(), countSel, selections) == LB_ERR ) { wxLogDebug(wxT("ListBox_GetSelItems failed")); countSel = -1; } else { aSelections.Alloc(countSel); for ( int n = 0; n < countSel; n++ ) aSelections.Add(selections[n]); } delete [] selections; } return countSel; } else // single-selection listbox { if (ListBox_GetCurSel(GetHwnd()) > -1) aSelections.Add(ListBox_GetCurSel(GetHwnd())); return aSelections.Count(); } }
bool wxListBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) { wxEventType evtType; if ( param == LBN_SELCHANGE ) { evtType = wxEVT_COMMAND_LISTBOX_SELECTED; } else if ( param == LBN_DBLCLK ) { evtType = wxEVT_COMMAND_LISTBOX_DOUBLECLICKED; } else { // some event we're not interested in return false; } wxCommandEvent event(evtType, m_windowId); event.SetEventObject( this ); // retrieve the affected item int n = SendMessage(GetHwnd(), LB_GETCARETINDEX, 0, 0); if ( n != LB_ERR ) { if ( HasClientObjectData() ) event.SetClientObject( GetClientObject(n) ); else if ( HasClientUntypedData() ) event.SetClientData( GetClientData(n) ); event.SetString(GetString(n)); event.SetExtraLong( HasMultipleSelection() ? IsSelected(n) : true ); } event.SetInt(n); return GetEventHandler()->ProcessEvent(event); }
void wxListBoxBase::DeselectAll(int itemToLeaveSelected) { if ( HasMultipleSelection() ) { wxArrayInt selections; GetSelections(selections); size_t count = selections.GetCount(); for ( size_t n = 0; n < count; n++ ) { int item = selections[n]; if ( item != itemToLeaveSelected ) Deselect(item); } } else // single selection { int sel = GetSelection(); if ( sel != wxNOT_FOUND && sel != itemToLeaveSelected ) { Deselect(sel); } } }
void wxVListBox::DoHandleItemClick(int item, int flags) { // has anything worth telling the client code about happened? bool notify = false; if ( HasMultipleSelection() ) { // select the iteem clicked? bool select = true; // NB: the keyboard interface we implement here corresponds to // wxLB_EXTENDED rather than wxLB_MULTIPLE but this one makes more // sense IMHO if ( flags & ItemClick_Shift ) { if ( m_current != wxNOT_FOUND ) { if ( m_anchor == wxNOT_FOUND ) m_anchor = m_current; select = false; // only the range from the selection anchor to new m_current // must be selected if ( DeselectAll() ) notify = true; if ( SelectRange(m_anchor, item) ) notify = true; } //else: treat it as ordinary click/keypress } else // Shift not pressed { m_anchor = item; if ( flags & ItemClick_Ctrl ) { select = false; if ( !(flags & ItemClick_Kbd) ) { Toggle(item); // the status of the item has definitely changed notify = true; } //else: Ctrl-arrow pressed, don't change selection } //else: behave as in single selection case } if ( select ) { // make the clicked item the only selection if ( DeselectAll() ) notify = true; if ( Select(item) ) notify = true; } } // in any case the item should become the current one if ( DoSetCurrent(item) ) { if ( !HasMultipleSelection() ) { // this has also changed the selection for single selection case notify = true; } } if ( notify ) { // notify the user about the selection change SendSelectedEvent(); } //else: nothing changed at all }
void wxCheckListBox::OnKeyDown(wxKeyEvent& event) { // what do we do? enum { NONE, TOGGLE, SET, CLEAR } oper; switch ( event.GetKeyCode() ) { case WXK_SPACE: oper = TOGGLE; break; case WXK_NUMPAD_ADD: case '+': oper = SET; break; case WXK_NUMPAD_SUBTRACT: case '-': oper = CLEAR; break; default: oper = NONE; } if ( oper != NONE ) { wxArrayInt selections; int count = 0; if ( HasMultipleSelection() ) { count = GetSelections(selections); } else { int sel = GetSelection(); if (sel != -1) { count = 1; selections.Add(sel); } } for ( int i = 0; i < count; i++ ) { int nItem = selections[i]; switch ( oper ) { case TOGGLE: Toggle(nItem); break; case SET: case CLEAR: Check(nItem, oper == SET); break; default: wxFAIL_MSG( wxT("what should this key do?") ); } // we should send an event as this has been done by the user and // not by the program SendEvent(nItem); } } else // nothing to do { event.Skip(); } }
int wxListBox::GetSelections( wxArrayInt& raSelections ) const { int nCount = 0; LONG lItem; raSelections.Empty(); if (HasMultipleSelection()) { lItem = LONGFROMMR(::WinSendMsg( GetHwnd() ,LM_QUERYSELECTION ,(MPARAM)LIT_FIRST ,(MPARAM)0 ) ); if (lItem != LIT_NONE) { nCount++; while ((lItem = LONGFROMMR(::WinSendMsg( GetHwnd() ,LM_QUERYSELECTION ,(MPARAM)lItem ,(MPARAM)0 ) )) != LIT_NONE) { nCount++; } raSelections.Alloc(nCount); lItem = LONGFROMMR(::WinSendMsg( GetHwnd() ,LM_QUERYSELECTION ,(MPARAM)LIT_FIRST ,(MPARAM)0 ) ); raSelections.Add((int)lItem); while ((lItem = LONGFROMMR(::WinSendMsg( GetHwnd() ,LM_QUERYSELECTION ,(MPARAM)lItem ,(MPARAM)0 ) )) != LIT_NONE) { raSelections.Add((int)lItem); } return nCount; } } else // single-selection listbox { lItem = LONGFROMMR(::WinSendMsg( GetHwnd() ,LM_QUERYSELECTION ,(MPARAM)LIT_FIRST ,(MPARAM)0 ) ); raSelections.Add((int)lItem); return 1; } return 0; } // end of wxListBox::GetSelections