X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/ad9835c97c10c0d7de345e3a7a7c0fc2776eb2cb..69659fd770f615210efac4b4fa741b3ad6223616:/src/msw/listbox.cpp diff --git a/src/msw/listbox.cpp b/src/msw/listbox.cpp index f5c67dba29..698d03d610 100644 --- a/src/msw/listbox.cpp +++ b/src/msw/listbox.cpp @@ -18,23 +18,23 @@ #if wxUSE_LISTBOX +#include "wx/listbox.h" + #ifndef WX_PRECOMP #include "wx/dynarray.h" - #include "wx/listbox.h" #include "wx/settings.h" #include "wx/brush.h" #include "wx/font.h" #include "wx/dc.h" #include "wx/utils.h" + #include "wx/log.h" + #include "wx/window.h" #endif -#include "wx/window.h" #include "wx/msw/private.h" #include -#include "wx/log.h" - #if wxUSE_OWNER_DRAWN #include "wx/ownerdrw.h" #endif @@ -80,7 +80,7 @@ wxBEGIN_FLAGS( wxListBoxStyle ) wxEND_FLAGS( wxListBoxStyle ) -IMPLEMENT_DYNAMIC_CLASS_XTI(wxListBox, wxControl,"wx/listbox.h") +IMPLEMENT_DYNAMIC_CLASS_XTI(wxListBox, wxControlWithItems,"wx/listbox.h") wxBEGIN_PROPERTIES_TABLE(wxListBox) wxEVENT_PROPERTY( Select , wxEVT_COMMAND_LISTBOX_SELECTED , wxCommandEvent ) @@ -97,7 +97,7 @@ wxEND_HANDLERS_TABLE() wxCONSTRUCTOR_4( wxListBox , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size ) #else -IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl) +IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControlWithItems) #endif /* @@ -177,7 +177,7 @@ bool wxListBox::Create(wxWindow *parent, } // now we can compute our best size correctly, so do it if necessary - SetBestSize(size); + SetInitialSize(size); return true; } @@ -275,13 +275,11 @@ void wxListBox::Delete(unsigned int n) m_noItems--; SetHorizontalExtent(wxEmptyString); - - InvalidateBestSize(); } int wxListBox::DoAppend(const wxString& item) { - int index = ListBox_AddString(GetHwnd(), item); + int index = ListBox_AddString(GetHwnd(), item.wx_str()); m_noItems++; #if wxUSE_OWNER_DRAWN @@ -296,7 +294,6 @@ int wxListBox::DoAppend(const wxString& item) SetHorizontalExtent(item); - InvalidateBestSize(); return index; } @@ -315,7 +312,7 @@ void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData) unsigned int i; for (i = 0; i < m_noItems; i++) { - ListBox_AddString(GetHwnd(), choices[i]); + ListBox_AddString(GetHwnd(), choices[i].wx_str()); if ( clientData ) { SetClientData(i, clientData[i]); @@ -344,8 +341,6 @@ void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData) // show the listbox back if we hid it ShowWindow(GetHwnd(), SW_SHOW); } - - InvalidateBestSize(); } int wxListBox::FindString(const wxString& s, bool bCase) const @@ -354,7 +349,7 @@ int wxListBox::FindString(const wxString& s, bool bCase) const if (bCase) return wxItemContainerImmutable::FindString( s, bCase ); - int pos = ListBox_FindStringExact(GetHwnd(), -1, s); + int pos = ListBox_FindStringExact(GetHwnd(), -1, s.wx_str()); if (pos == LB_ERR) return wxNOT_FOUND; else @@ -369,8 +364,6 @@ void wxListBox::Clear() m_noItems = 0; SetHorizontalExtent(); - - InvalidateBestSize(); } void wxListBox::Free() @@ -527,7 +520,7 @@ wxListBox::DoInsertItems(const wxArrayString& items, unsigned int pos) unsigned int nItems = items.GetCount(); for ( unsigned int i = 0; i < nItems; i++ ) { - int idx = ListBox_InsertString(GetHwnd(), i + pos, items[i]); + int idx = ListBox_InsertString(GetHwnd(), i + pos, items[i].wx_str()); #if wxUSE_OWNER_DRAWN if ( m_windowStyle & wxLB_OWNERDRAW ) @@ -547,8 +540,6 @@ wxListBox::DoInsertItems(const wxArrayString& items, unsigned int pos) m_noItems += nItems; SetHorizontalExtent(); - - InvalidateBestSize(); } int wxListBox::DoListHitTest(const wxPoint& point) const @@ -583,7 +574,7 @@ void wxListBox::SetString(unsigned int n, const wxString& s) if ( n == (m_noItems - 1) ) newN = -1; - ListBox_InsertString(GetHwnd(), newN, s); + ListBox_InsertString(GetHwnd(), newN, s.wx_str()); // restore the client data if ( oldData ) @@ -606,7 +597,7 @@ void wxListBox::SetString(unsigned int n, const wxString& s) if ( wasSelected ) Select(n); - InvalidateBestSize(); + SetHorizontalExtent(); } unsigned int wxListBox::GetCount() const @@ -615,64 +606,56 @@ unsigned int wxListBox::GetCount() const } // ---------------------------------------------------------------------------- -// helpers +// size-related stuff // ---------------------------------------------------------------------------- -// Windows-specific code to set the horizontal extent of the listbox, if -// necessary. If s is non-NULL, it's used to calculate the horizontal extent. -// Otherwise, all strings are used. void wxListBox::SetHorizontalExtent(const wxString& s) { - // Only necessary if we want a horizontal scrollbar - if (!(m_windowStyle & wxHSCROLL)) + // in any case, our best size could have changed + InvalidateBestSize(); + + // the rest is only necessary if we want a horizontal scrollbar + if ( !HasFlag(wxHSCROLL) ) return; - TEXTMETRIC lpTextMetric; - if ( !s.empty() ) - { - int existingExtent = (int)SendMessage(GetHwnd(), LB_GETHORIZONTALEXTENT, 0, 0L); - HDC dc = GetWindowDC(GetHwnd()); - HFONT oldFont = 0; - if (GetFont().Ok() && GetFont().GetResourceHandle() != 0) - oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle()); - - GetTextMetrics(dc, &lpTextMetric); - SIZE extentXY; - ::GetTextExtentPoint32(dc, (LPTSTR) (const wxChar *)s, s.length(), &extentXY); - int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth); - if (oldFont) - ::SelectObject(dc, oldFont); + WindowHDC dc(GetHwnd()); + SelectInHDC selFont(dc, GetHfontOf(GetFont())); - ReleaseDC(GetHwnd(), dc); - if (extentX > existingExtent) - SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(extentX), 0L); - } - else - { - int largestExtent = 0; - HDC dc = GetWindowDC(GetHwnd()); - HFONT oldFont = 0; - if (GetFont().Ok() && GetFont().GetResourceHandle() != 0) - oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle()); + TEXTMETRIC lpTextMetric; + ::GetTextMetrics(dc, &lpTextMetric); - GetTextMetrics(dc, &lpTextMetric); + int largestExtent = 0; + SIZE extentXY; - for (unsigned int i = 0; i < m_noItems; i++) + if ( s.empty() ) + { + // set extent to the max length of all strings + for ( unsigned int i = 0; i < m_noItems; i++ ) { - wxString str = GetString(i); - SIZE extentXY; + const wxString str = GetString(i); ::GetTextExtentPoint32(dc, str.c_str(), str.length(), &extentXY); + int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth); - if (extentX > largestExtent) + if ( extentX > largestExtent ) largestExtent = extentX; } - if (oldFont) - ::SelectObject(dc, oldFont); + } + else // just increase the extent to the length of this string + { + int existingExtent = (int)SendMessage(GetHwnd(), + LB_GETHORIZONTALEXTENT, 0, 0L); - ReleaseDC(GetHwnd(), dc); - SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(largestExtent), 0L); + ::GetTextExtentPoint32(dc, s.c_str(), s.length(), &extentXY); + + int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth); + if ( extentX > existingExtent ) + largestExtent = extentX; } + + if ( largestExtent ) + SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(largestExtent), 0L); + //else: it shouldn't change } wxSize wxListBox::DoGetBestSize() const @@ -815,7 +798,7 @@ bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT *item) if ( itemID == (UINT)-1 ) return false; - long data = ListBox_GetItemData(GetHwnd(), pStruct->itemID); + LRESULT data = ListBox_GetItemData(GetHwnd(), pStruct->itemID); wxCHECK( data && (data != LB_ERR), false );