call SetHorizontalExtent() from SetString(); also simplified/cleaned up the former function and call InvalidateBestSize() from it automatically now
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@40077 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -105,9 +105,10 @@ public:
|
|||||||
int GetItemIndex(wxOwnerDrawn *item) const { return m_aItems.Index(item); }
|
int GetItemIndex(wxOwnerDrawn *item) const { return m_aItems.Index(item); }
|
||||||
#endif // wxUSE_OWNER_DRAWN
|
#endif // wxUSE_OWNER_DRAWN
|
||||||
|
|
||||||
// Windows-specific code to set the horizontal extent of the listbox, if
|
// Windows-specific code to update the horizontal extent of the listbox, if
|
||||||
// necessary. If s is non-NULL, it's used to calculate the horizontal
|
// necessary. If s is non-empty, the horizontal extent is increased to the
|
||||||
// extent. Otherwise, all strings are used.
|
// length of this string if it's currently too short, otherwise the maximum
|
||||||
|
// extent of all strings is used. In any case calls InvalidateBestSize()
|
||||||
virtual void SetHorizontalExtent(const wxString& s = wxEmptyString);
|
virtual void SetHorizontalExtent(const wxString& s = wxEmptyString);
|
||||||
|
|
||||||
// Windows callbacks
|
// Windows callbacks
|
||||||
|
@@ -275,8 +275,6 @@ void wxListBox::Delete(unsigned int n)
|
|||||||
m_noItems--;
|
m_noItems--;
|
||||||
|
|
||||||
SetHorizontalExtent(wxEmptyString);
|
SetHorizontalExtent(wxEmptyString);
|
||||||
|
|
||||||
InvalidateBestSize();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int wxListBox::DoAppend(const wxString& item)
|
int wxListBox::DoAppend(const wxString& item)
|
||||||
@@ -296,7 +294,6 @@ int wxListBox::DoAppend(const wxString& item)
|
|||||||
|
|
||||||
SetHorizontalExtent(item);
|
SetHorizontalExtent(item);
|
||||||
|
|
||||||
InvalidateBestSize();
|
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -344,8 +341,6 @@ void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData)
|
|||||||
// show the listbox back if we hid it
|
// show the listbox back if we hid it
|
||||||
ShowWindow(GetHwnd(), SW_SHOW);
|
ShowWindow(GetHwnd(), SW_SHOW);
|
||||||
}
|
}
|
||||||
|
|
||||||
InvalidateBestSize();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int wxListBox::FindString(const wxString& s, bool bCase) const
|
int wxListBox::FindString(const wxString& s, bool bCase) const
|
||||||
@@ -369,8 +364,6 @@ void wxListBox::Clear()
|
|||||||
|
|
||||||
m_noItems = 0;
|
m_noItems = 0;
|
||||||
SetHorizontalExtent();
|
SetHorizontalExtent();
|
||||||
|
|
||||||
InvalidateBestSize();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void wxListBox::Free()
|
void wxListBox::Free()
|
||||||
@@ -547,8 +540,6 @@ wxListBox::DoInsertItems(const wxArrayString& items, unsigned int pos)
|
|||||||
m_noItems += nItems;
|
m_noItems += nItems;
|
||||||
|
|
||||||
SetHorizontalExtent();
|
SetHorizontalExtent();
|
||||||
|
|
||||||
InvalidateBestSize();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int wxListBox::DoListHitTest(const wxPoint& point) const
|
int wxListBox::DoListHitTest(const wxPoint& point) const
|
||||||
@@ -606,7 +597,7 @@ void wxListBox::SetString(unsigned int n, const wxString& s)
|
|||||||
if ( wasSelected )
|
if ( wasSelected )
|
||||||
Select(n);
|
Select(n);
|
||||||
|
|
||||||
InvalidateBestSize();
|
SetHorizontalExtent();
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int wxListBox::GetCount() const
|
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)
|
void wxListBox::SetHorizontalExtent(const wxString& s)
|
||||||
{
|
{
|
||||||
// Only necessary if we want a horizontal scrollbar
|
// in any case, our best size could have changed
|
||||||
if (!(m_windowStyle & wxHSCROLL))
|
InvalidateBestSize();
|
||||||
|
|
||||||
|
// the rest is only necessary if we want a horizontal scrollbar
|
||||||
|
if ( !HasFlag(wxHSCROLL) )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
|
||||||
|
WindowHDC dc(GetHwnd());
|
||||||
|
SelectInHDC selFont(dc, GetHfontOf(GetFont()));
|
||||||
|
|
||||||
TEXTMETRIC lpTextMetric;
|
TEXTMETRIC lpTextMetric;
|
||||||
|
::GetTextMetrics(dc, &lpTextMetric);
|
||||||
|
|
||||||
if ( !s.empty() )
|
int largestExtent = 0;
|
||||||
|
SIZE extentXY;
|
||||||
|
|
||||||
|
if ( s.empty() )
|
||||||
{
|
{
|
||||||
int existingExtent = (int)SendMessage(GetHwnd(), LB_GETHORIZONTALEXTENT, 0, 0L);
|
// set extent to the max length of all strings
|
||||||
HDC dc = GetWindowDC(GetHwnd());
|
for ( unsigned int i = 0; i < m_noItems; i++ )
|
||||||
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);
|
|
||||||
|
|
||||||
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());
|
|
||||||
|
|
||||||
GetTextMetrics(dc, &lpTextMetric);
|
|
||||||
|
|
||||||
for (unsigned int i = 0; i < m_noItems; i++)
|
|
||||||
{
|
{
|
||||||
wxString str = GetString(i);
|
const wxString str = GetString(i);
|
||||||
SIZE extentXY;
|
|
||||||
::GetTextExtentPoint32(dc, str.c_str(), str.length(), &extentXY);
|
::GetTextExtentPoint32(dc, str.c_str(), str.length(), &extentXY);
|
||||||
|
|
||||||
int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
|
int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
|
||||||
if (extentX > largestExtent)
|
if ( extentX > largestExtent )
|
||||||
largestExtent = extentX;
|
largestExtent = extentX;
|
||||||
}
|
}
|
||||||
if (oldFont)
|
|
||||||
::SelectObject(dc, oldFont);
|
|
||||||
|
|
||||||
ReleaseDC(GetHwnd(), dc);
|
|
||||||
SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(largestExtent), 0L);
|
|
||||||
}
|
}
|
||||||
|
else // just increase the extent to the length of this string
|
||||||
|
{
|
||||||
|
int existingExtent = (int)SendMessage(GetHwnd(),
|
||||||
|
LB_GETHORIZONTALEXTENT, 0, 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
|
wxSize wxListBox::DoGetBestSize() const
|
||||||
|
Reference in New Issue
Block a user