X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/e1d6e01c88876f9ff57056e2847eaaa91b074abb..1ecc323f2609c8afea31948b9eac2d965cc84f5e:/src/univ/listbox.cpp diff --git a/src/univ/listbox.cpp b/src/univ/listbox.cpp index c7e00cee0c..147ebfb148 100644 --- a/src/univ/listbox.cpp +++ b/src/univ/listbox.cpp @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: univ/listbox.cpp +// Name: src/univ/listbox.cpp // Purpose: wxListBox implementation // Author: Vadim Zeitlin // Modified by: @@ -17,10 +17,6 @@ // headers // ---------------------------------------------------------------------------- -#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) - #pragma implementation "univlistbox.h" -#endif - #include "wx/wxprec.h" #ifdef __BORLANDC__ @@ -41,12 +37,66 @@ #include "wx/univ/inphand.h" #include "wx/univ/theme.h" +// ---------------------------------------------------------------------------- +// wxStdListboxInputHandler: handles mouse and kbd in a single or multi +// selection listbox +// ---------------------------------------------------------------------------- + +class WXDLLEXPORT wxStdListboxInputHandler : public wxStdInputHandler +{ +public: + // if pressing the mouse button in a multiselection listbox should toggle + // the item under mouse immediately, then specify true as the second + // parameter (this is the standard behaviour, under GTK the item is toggled + // only when the mouse is released in the multi selection listbox) + wxStdListboxInputHandler(wxInputHandler *inphand, + bool toggleOnPressAlways = true); + + // base class methods + virtual bool HandleKey(wxInputConsumer *consumer, + const wxKeyEvent& event, + bool pressed); + virtual bool HandleMouse(wxInputConsumer *consumer, + const wxMouseEvent& event); + virtual bool HandleMouseMove(wxInputConsumer *consumer, + const wxMouseEvent& event); + +protected: + // return the item under mouse, 0 if the mouse is above the listbox or + // GetCount() if it is below it + int HitTest(const wxListBox *listbox, const wxMouseEvent& event); + + // parts of HitTest(): first finds the pseudo (because not in range) index + // of the item and the second one adjusts it if necessary - that is if the + // third one returns false + int HitTestUnsafe(const wxListBox *listbox, const wxMouseEvent& event); + int FixItemIndex(const wxListBox *listbox, int item); + bool IsValidIndex(const wxListBox *listbox, int item); + + // init m_btnCapture and m_actionMouse + wxControlAction SetupCapture(wxListBox *lbox, + const wxMouseEvent& event, + int item); + + wxRenderer *m_renderer; + + // the button which initiated the mouse capture (currently 0 or 1) + int m_btnCapture; + + // the action to perform when the mouse moves while we capture it + wxControlAction m_actionMouse; + + // the ctor parameter toggleOnPressAlways (see comments near it) + bool m_toggleOnPressAlways; + + // do we track the mouse outside the window when it is captured? + bool m_trackMouseOutside; +}; + // ============================================================================ // implementation of wxListBox // ============================================================================ -IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl) - BEGIN_EVENT_TABLE(wxListBox, wxListBoxBase) EVT_SIZE(wxListBox::OnSize) END_EVENT_TABLE() @@ -63,7 +113,7 @@ void wxListBox::Init() m_maxWidth = 0; m_scrollRangeY = 0; m_maxWidthItem = -1; - m_strings = NULL; + m_strings.unsorted = NULL; // no items hence no current item m_current = -1; @@ -88,6 +138,7 @@ wxListBox::wxListBox(wxWindow *parent, long style, const wxValidator& validator, const wxString &name) + :wxScrollHelper(this) { Init(); @@ -139,13 +190,14 @@ bool wxListBox::Create(wxWindow *parent, validator, name) ) return false; - SetWindow(this); - - m_strings = new wxArrayString; + if ( IsSorted() ) + m_strings.sorted = new wxSortedArrayString; + else + m_strings.unsorted = new wxArrayString; Set(n, choices); - SetBestSize(size); + SetInitialSize(size); CreateInputHandler(wxINP_HANDLER_LISTBOX); @@ -157,76 +209,59 @@ wxListBox::~wxListBox() // call this just to free the client data -- and avoid leaking memory DoClear(); - delete m_strings; + if ( IsSorted() ) + delete m_strings.sorted; + else + delete m_strings.unsorted; - m_strings = NULL; + m_strings.sorted = NULL; } // ---------------------------------------------------------------------------- -// adding/inserting strings +// accessing strings // ---------------------------------------------------------------------------- -int wxCMPFUNC_CONV wxListBoxSortNoCase(wxString* s1, wxString* s2) +unsigned int wxListBox::GetCount() const { - return s1->CmpNoCase(*s2); + return IsSorted() ? m_strings.sorted->size() + : m_strings.unsorted->size(); } -int wxListBox::DoAppendOnly(const wxString& item) +wxString wxListBox::GetString(unsigned int n) const { - size_t index; - - if ( IsSorted() ) - { - m_strings->Add(item); - m_strings->Sort(wxListBoxSortNoCase); - index = m_strings->Index(item); - } - else - { - index = m_strings->GetCount(); - m_strings->Add(item); - } - - return index; + return IsSorted() ? m_strings.sorted->Item(n) + : m_strings.unsorted->Item(n); } -int wxListBox::DoAppend(const wxString& item) +int wxListBox::FindString(const wxString& s, bool bCase) const { - size_t index = DoAppendOnly( item ); + return IsSorted() ? m_strings.sorted->Index(s, bCase) + : m_strings.unsorted->Index(s, bCase); +} - m_itemsClientData.Insert(NULL, index); +// ---------------------------------------------------------------------------- +// adding/inserting strings +// ---------------------------------------------------------------------------- - m_updateScrollbarY = true; +int wxListBox::DoInsertItems(const wxArrayStringsAdapter& items, + unsigned int pos, + void **clientData, + wxClientDataType type) +{ + int idx = wxNOT_FOUND; - if ( HasHorzScrollbar() ) + const unsigned int numItems = items.GetCount(); + for ( unsigned int i = 0; i < numItems; ++i ) { - // has the max width increased? - wxCoord width; - GetTextExtent(item, &width, NULL); - if ( width > m_maxWidth ) - { - m_maxWidth = width; - m_maxWidthItem = index; - m_updateScrollbarX = true; - } - } - - RefreshFromItemToEnd(index); - - return index; -} + const wxString& item = items[i]; + idx = IsSorted() ? m_strings.sorted->Add(item) + : (m_strings.unsorted->Insert(item, pos), pos++); -void wxListBox::DoInsertItems(const wxArrayString& items, int pos) -{ - // the position of the item being added to a sorted listbox can't be - // specified - wxCHECK_RET( !IsSorted(), _T("can't insert items into sorted listbox") ); + m_itemsClientData.Insert(NULL, idx); + AssignNewItemClientData(idx, clientData, i, type); - size_t count = items.GetCount(); - for ( size_t n = 0; n < count; n++ ) - { - m_strings->Insert(items[n], pos + n); - m_itemsClientData.Insert(NULL, pos + n); + // call the wxCheckListBox hook + OnItemInserted(idx); } // the number of items has changed so we might have to show the scrollbar @@ -240,36 +275,18 @@ void wxListBox::DoInsertItems(const wxArrayString& items, int pos) // note that we have to refresh all the items after the ones we inserted, // not just these items RefreshFromItemToEnd(pos); -} - -void wxListBox::DoSetItems(const wxArrayString& items, void **clientData) -{ - DoClear(); - - size_t count = items.GetCount(); - if ( !count ) - return; - - m_strings->Alloc(count); - - m_itemsClientData.Alloc(count); - for ( size_t n = 0; n < count; n++ ) - { - size_t index = DoAppendOnly(items[n]); - - m_itemsClientData.Insert(clientData ? clientData[n] : NULL, index); - } - m_updateScrollbarY = true; - - RefreshAll(); + return idx; } -void wxListBox::SetString(int n, const wxString& s) +void wxListBox::SetString(unsigned int n, const wxString& s) { - wxCHECK_RET( !IsSorted(), _T("can't set string in sorted listbox") ); + wxCHECK_RET( !IsSorted(), wxT("can't set string in sorted listbox") ); - (*m_strings)[n] = s; + if ( IsSorted() ) + (*m_strings.sorted)[n] = s; + else + (*m_strings.unsorted)[n] = s; if ( HasHorzScrollbar() ) { @@ -287,7 +304,7 @@ void wxListBox::SetString(int n, const wxString& s) m_updateScrollbarX = true; } // or also decreased if the old string was the longest one - else if ( n == m_maxWidthItem ) + else if ( n == (unsigned int)m_maxWidthItem ) { RefreshHorzScrollbar(); } @@ -302,26 +319,15 @@ void wxListBox::SetString(int n, const wxString& s) void wxListBox::DoClear() { - m_strings->Clear(); - - if ( HasClientObjectData() ) - { - size_t count = m_itemsClientData.GetCount(); - for ( size_t n = 0; n < count; n++ ) - { - delete (wxClientData *) m_itemsClientData[n]; - } - } + if ( IsSorted() ) + m_strings.sorted->Clear(); + else + m_strings.unsorted->Clear(); m_itemsClientData.Clear(); m_selections.Clear(); m_current = -1; -} - -void wxListBox::Clear() -{ - DoClear(); m_updateScrollbarY = true; @@ -330,30 +336,28 @@ void wxListBox::Clear() RefreshAll(); } -void wxListBox::Delete(int n) +void wxListBox::DoDeleteOneItem(unsigned int n) { - wxCHECK_RET( n >= 0 && n < GetCount(), - _T("invalid index in wxListBox::Delete") ); + wxCHECK_RET( IsValid(n), + wxT("invalid index in wxListBox::Delete") ); // do it before removing the index as otherwise the last item will not be // refreshed (as GetCount() will be decremented) RefreshFromItemToEnd(n); - m_strings->RemoveAt(n); - - if ( HasClientObjectData() ) - { - delete (wxClientData *)m_itemsClientData[n]; - } + if ( IsSorted() ) + m_strings.sorted->RemoveAt(n); + else + m_strings.unsorted->RemoveAt(n); m_itemsClientData.RemoveAt(n); // when the item disappears we must not keep using its index - if ( n == m_current ) + if ( (int)n == m_current ) { m_current = -1; } - else if ( n < m_current ) + else if ( (int)n < m_current ) { m_current--; } @@ -362,15 +366,15 @@ void wxListBox::Delete(int n) // update the selections array: the indices of all seletected items after // the one being deleted must change and the item itselfm ust be removed int index = wxNOT_FOUND; - size_t count = m_selections.GetCount(); - for ( size_t item = 0; item < count; item++ ) + unsigned int count = m_selections.GetCount(); + for ( unsigned int item = 0; item < count; item++ ) { - if ( m_selections[item] == n ) + if ( m_selections[item] == (int)n ) { // remember to delete it later index = item; } - else if ( m_selections[item] > n ) + else if ( m_selections[item] > (int)n ) { // to account for the index shift m_selections[item]--; @@ -387,7 +391,7 @@ void wxListBox::Delete(int n) m_updateScrollbarY = true; // finally, if the longest item was deleted the scrollbar may disappear - if ( n == m_maxWidthItem ) + if ( (int)n == m_maxWidthItem ) { RefreshHorzScrollbar(); } @@ -397,35 +401,34 @@ void wxListBox::Delete(int n) // client data handling // ---------------------------------------------------------------------------- -void wxListBox::DoSetItemClientData(int n, void* clientData) +void wxListBox::DoSetItemClientData(unsigned int n, void* clientData) { m_itemsClientData[n] = clientData; } -void *wxListBox::DoGetItemClientData(int n) const +void *wxListBox::DoGetItemClientData(unsigned int n) const { return m_itemsClientData[n]; } -void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData) -{ - m_itemsClientData[n] = clientData; -} - -wxClientData* wxListBox::DoGetItemClientObject(int n) const -{ - return (wxClientData *)m_itemsClientData[n]; -} - // ---------------------------------------------------------------------------- // selection // ---------------------------------------------------------------------------- -void wxListBox::SetSelection(int n, bool select) +void wxListBox::DoSetSelection(int n, bool select) { if ( select ) { - if ( m_selections.Index(n) == wxNOT_FOUND ) + if ( n == wxNOT_FOUND ) + { + if ( !HasMultipleSelection() ) + { + // selecting wxNOT_FOUND is documented to deselect all items + DeselectAll(); + return; + } + } + else if ( m_selections.Index(n) == wxNOT_FOUND ) { if ( !HasMultipleSelection() ) { @@ -455,7 +458,7 @@ void wxListBox::SetSelection(int n, bool select) // 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?") ); + wxT("multiple selected items in single selection lbox?") ); if ( select ) { @@ -466,13 +469,13 @@ void wxListBox::SetSelection(int n, bool select) int wxListBox::GetSelection() const { - wxCHECK_MSG( !HasMultipleSelection(), -1, - _T("use wxListBox::GetSelections for ths listbox") ); + wxCHECK_MSG( !HasMultipleSelection(), wxNOT_FOUND, + wxT("use wxListBox::GetSelections for ths listbox") ); - return m_selections.IsEmpty() ? -1 : m_selections[0]; + return m_selections.IsEmpty() ? wxNOT_FOUND : m_selections[0]; } -int wxCMPFUNC_CONV wxCompareInts(int *n, int *m) +static int wxCMPFUNC_CONV wxCompareInts(int *n, int *m) { return *n - *m; } @@ -481,7 +484,7 @@ int wxListBox::GetSelections(wxArrayInt& selections) const { // always return sorted array to the user selections = m_selections; - size_t count = m_selections.GetCount(); + unsigned int count = m_selections.GetCount(); // don't call sort on an empty array if ( count ) @@ -579,9 +582,9 @@ void wxListBox::UpdateScrollbars() wxSize size = GetClientSize(); // is our height enough to show all items? - int nLines = GetCount(); + unsigned int nLines = GetCount(); wxCoord lineHeight = GetLineHeight(); - bool showScrollbarY = nLines*lineHeight > size.y; + bool showScrollbarY = (int)nLines*lineHeight > size.y; // check the width too if required wxCoord charWidth, maxWidth; @@ -634,7 +637,7 @@ void wxListBox::UpdateItems() if ( m_updateCount == -1 ) { // refresh all - wxLogTrace(_T("listbox"), _T("Refreshing all")); + wxLogTrace(wxT("listbox"), wxT("Refreshing all")); Refresh(); } @@ -651,7 +654,7 @@ void wxListBox::UpdateItems() // entire line(s) CalcScrolledPosition(0, rect.y, NULL, &rect.y); - wxLogTrace(_T("listbox"), _T("Refreshing items %d..%d (%d-%d)"), + wxLogTrace(wxT("listbox"), wxT("Refreshing items %d..%d (%d-%d)"), m_updateFrom, m_updateFrom + m_updateCount - 1, rect.GetTop(), rect.GetBottom()); @@ -710,9 +713,9 @@ void wxListBox::DoDraw(wxControlRenderer *renderer) // get the items which must be redrawn wxCoord lineHeight = GetLineHeight(); - size_t itemFirst = yTop / lineHeight, - itemLast = (yBottom + lineHeight - 1) / lineHeight, - itemMax = m_strings->GetCount(); + unsigned int itemFirst = yTop / lineHeight, + itemLast = (yBottom + lineHeight - 1) / lineHeight, + itemMax = GetCount(); if ( itemFirst >= itemMax ) return; @@ -721,7 +724,7 @@ void wxListBox::DoDraw(wxControlRenderer *renderer) itemLast = itemMax; // do draw them - wxLogTrace(_T("listbox"), _T("Repainting items %d..%d"), + wxLogTrace(wxT("listbox"), wxT("Repainting items %d..%d"), itemFirst, itemLast); DoDrawRange(renderer, itemFirst, itemLast); @@ -781,8 +784,8 @@ wxCoord wxListBox::GetMaxWidth() const { wxListBox *self = wxConstCast(this, wxListBox); wxCoord width; - size_t count = m_strings->GetCount(); - for ( size_t n = 0; n < count; n++ ) + unsigned int count = GetCount(); + for ( unsigned int n = 0; n < count; n++ ) { GetTextExtent(this->GetString(n), &width, NULL); if ( width > m_maxWidth ) @@ -838,8 +841,8 @@ wxSize wxListBox::DoGetBestClientSize() const wxCoord width = 0, height = 0; - size_t count = m_strings->GetCount(); - for ( size_t n = 0; n < count; n++ ) + unsigned int count = GetCount(); + for ( unsigned int n = 0; n < count; n++ ) { wxCoord w,h; GetTextExtent(this->GetString(n), &w, &h); @@ -892,7 +895,7 @@ bool wxListBox::SendEvent(wxEventType type, int item) event.SetString(GetString(item)); } - event.m_commandInt = item; + event.SetInt(item); return GetEventHandler()->ProcessEvent(event); } @@ -918,8 +921,8 @@ void wxListBox::SetCurrentItem(int n) bool wxListBox::FindItem(const wxString& prefix, bool strictlyAfter) { - int count = GetCount(); - if ( !count ) + unsigned int count = GetCount(); + if ( count==0 ) { // empty listbox, we can't find anything in it return false; @@ -932,7 +935,7 @@ bool wxListBox::FindItem(const wxString& prefix, bool strictlyAfter) { // the following line will set first correctly to 0 if there is no // selection (m_current == -1) - first = m_current == count - 1 ? 0 : m_current + 1; + first = m_current == (int)(count - 1) ? 0 : m_current + 1; } else // start with the current { @@ -942,13 +945,13 @@ bool wxListBox::FindItem(const wxString& prefix, bool strictlyAfter) int last = first == 0 ? count - 1 : first - 1; // if this is not true we'd never exit from the loop below! - wxASSERT_MSG( first < count && last < count, _T("logic error") ); + wxASSERT_MSG( first < (int)count && last < (int)count, wxT("logic error") ); // precompute it outside the loop size_t len = prefix.length(); // loop over all items in the listbox - for ( int item = first; item != last; item < count - 1 ? item++ : item = 0 ) + for ( int item = first; item != (int)last; item < (int)(count - 1) ? item++ : item = 0 ) { if ( wxStrnicmp(this->GetString(item).c_str(), prefix, len) == 0 ) { @@ -1060,8 +1063,8 @@ void wxListBox::ExtendSelection(int itemTo) SetSelection(n); } - int count = GetCount(); - for ( ; n < count; n++ ) + unsigned int count = GetCount(); + for ( ; n < (int)count; n++ ) { Deselect(n); } @@ -1188,13 +1191,21 @@ bool wxListBox::PerformAction(const wxControlAction& action, AnchorSelection(item == -1 ? m_current : item); else if ( action == wxACTION_LISTBOX_SELECTALL || action == wxACTION_LISTBOX_SELTOGGLE ) - wxFAIL_MSG(_T("unimplemented yet")); + wxFAIL_MSG(wxT("unimplemented yet")); else return wxControl::PerformAction(action, numArg, strArg); return true; } +/* static */ +wxInputHandler *wxListBox::GetStdInputHandler(wxInputHandler *handlerDef) +{ + static wxStdListboxInputHandler s_handler(handlerDef); + + return &s_handler; +} + // ============================================================================ // implementation of wxStdListboxInputHandler // ============================================================================ @@ -1235,7 +1246,7 @@ int wxStdListboxInputHandler::FixItemIndex(const wxListBox *lbox, // mouse is above the first item item = 0; } - else if ( item >= lbox->GetCount() ) + else if ( (unsigned int)item >= lbox->GetCount() ) { // mouse is below the last item item = lbox->GetCount() - 1; @@ -1246,7 +1257,7 @@ int wxStdListboxInputHandler::FixItemIndex(const wxListBox *lbox, bool wxStdListboxInputHandler::IsValidIndex(const wxListBox *lbox, int item) { - return item >= 0 && item < lbox->GetCount(); + return item >= 0 && (unsigned int)item < lbox->GetCount(); } wxControlAction @@ -1345,14 +1356,10 @@ bool wxStdListboxInputHandler::HandleKey(wxInputConsumer *consumer, break; case WXK_PAGEUP: - - case WXK_PRIOR: action = wxACTION_LISTBOX_PAGEUP; break; case WXK_PAGEDOWN: - - case WXK_NEXT: action = wxACTION_LISTBOX_PAGEDOWN; break; @@ -1495,7 +1502,7 @@ bool wxStdListboxInputHandler::HandleMouseMove(wxInputConsumer *consumer, { // pass something into strArg to tell the listbox that it shouldn't // send the notification message: see PerformAction() above - lbox->PerformAction(m_actionMouse, item, _T("no")); + lbox->PerformAction(m_actionMouse, item, wxT("no")); } // else: don't pass invalid index to the listbox }