Implement DoGetSizeFromTextSize() for wxMSW wx{Choice,Combobox,TextCtrl}.

Refactor and improve the existing DoGetBestSize() implementations to use
DoGetBestSize().

Closes #14816.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@72954 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2012-11-14 13:47:59 +00:00
parent df74e2d251
commit aa24f946c6
7 changed files with 88 additions and 16 deletions

View File

@@ -127,6 +127,7 @@ protected:
virtual void DoSetSize(int x, int y, virtual void DoSetSize(int x, int y,
int width, int height, int width, int height,
int sizeFlags = wxSIZE_AUTO); int sizeFlags = wxSIZE_AUTO);
virtual wxSize DoGetSizeFromTextSize(int xlen, int ylen = -1) const;
// Show or hide the popup part of the control. // Show or hide the popup part of the control.
void MSWDoPopupOrDismiss(bool show); void MSWDoPopupOrDismiss(bool show);

View File

@@ -132,6 +132,8 @@ protected:
virtual void DoSetToolTip(wxToolTip *tip); virtual void DoSetToolTip(wxToolTip *tip);
#endif #endif
virtual wxSize DoGetSizeFromTextSize(int xlen, int ylen = -1) const;
// this is the implementation of GetEditHWND() which can also be used when // this is the implementation of GetEditHWND() which can also be used when
// we don't have the edit control, it simply returns NULL then // we don't have the edit control, it simply returns NULL then
// //

View File

@@ -236,6 +236,7 @@ protected:
bool SendUpdateEvent(); bool SendUpdateEvent();
virtual wxSize DoGetBestSize() const; virtual wxSize DoGetBestSize() const;
virtual wxSize DoGetSizeFromTextSize(int xlen, int ylen = -1) const;
#if wxUSE_RICHEDIT #if wxUSE_RICHEDIT
// Apply the character-related parts of wxTextAttr to the given selection // Apply the character-related parts of wxTextAttr to the given selection

View File

@@ -172,7 +172,7 @@ public:
@endcode @endcode
Currently this method is only implemented for wxTextCtrl, wxComboBox Currently this method is only implemented for wxTextCtrl, wxComboBox
and wxChoice in wxGTK. and wxChoice in wxMSW and wxGTK.
@param xlen The horizontal extent of the area to leave for text, in @param xlen The horizontal extent of the area to leave for text, in
pixels. pixels.

View File

@@ -612,18 +612,7 @@ void wxChoice::DoSetSize(int x, int y,
wxSize wxChoice::DoGetBestSize() const wxSize wxChoice::DoGetBestSize() const
{ {
// The base version returns the size of the largest string // The base version returns the size of the largest string
wxSize best( wxChoiceBase::DoGetBestSize() ); return GetSizeFromTextSize(wxChoiceBase::DoGetBestSize().x);
// We just need to adjust it to account for the arrow width.
best.x += 5*GetCharWidth();
// set height on our own
if( HasFlag( wxCB_SIMPLE ) )
best.y = SetHeightSimpleComboBox(GetCount());
else
best.y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(GetCharHeight());
return best;
} }
int wxChoice::SetHeightSimpleComboBox(int nItems) const int wxChoice::SetHeightSimpleComboBox(int nItems) const
@@ -634,6 +623,38 @@ int wxChoice::SetHeightSimpleComboBox(int nItems) const
return EDIT_HEIGHT_FROM_CHAR_HEIGHT( cy ) * wxMin( wxMax( nItems, 3 ), 6 ) + hItem - 1; return EDIT_HEIGHT_FROM_CHAR_HEIGHT( cy ) * wxMin( wxMax( nItems, 3 ), 6 ) + hItem - 1;
} }
wxSize wxChoice::DoGetSizeFromTextSize(int xlen, int ylen) const
{
int cHeight = GetCharHeight();
// We are interested in the difference of sizes between the whole control
// and its child part. I.e. arrow, separators, etc.
wxSize tsize(xlen, 0);
WinStruct<COMBOBOXINFO> info;
if ( MSWGetComboBoxInfo(&info) )
{
tsize.x += info.rcItem.left + info.rcButton.right - info.rcItem.right
+ info.rcItem.left + 3; // right and extra margins
}
else // Just use some rough approximation.
{
tsize.x += 4*cHeight;
}
// set height on our own
if( HasFlag( wxCB_SIMPLE ) )
tsize.y = SetHeightSimpleComboBox(GetCount());
else
tsize.y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cHeight);
// Perhaps the user wants something different from CharHeight
if ( ylen > 0 )
tsize.IncBy(0, ylen - cHeight);
return tsize;
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Popup operations // Popup operations
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------

View File

@@ -661,4 +661,20 @@ bool wxComboBox::SetHint(const wxString& hintOrig)
#endif // wxUSE_UXTHEME #endif // wxUSE_UXTHEME
wxSize wxComboBox::DoGetSizeFromTextSize(int xlen, int ylen) const
{
wxSize tsize( wxChoice::DoGetSizeFromTextSize(xlen, ylen) );
if ( !HasFlag(wxCB_READONLY) )
{
// Add the margins we have previously set
wxPoint marg( GetMargins() );
marg.x = wxMax(0, marg.x);
marg.y = wxMax(0, marg.y);
tsize.IncBy( marg );
}
return tsize;
}
#endif // wxUSE_COMBOBOX #endif // wxUSE_COMBOBOX

View File

@@ -2106,18 +2106,42 @@ bool wxTextCtrl::AcceptsFocusFromKeyboard() const
} }
wxSize wxTextCtrl::DoGetBestSize() const wxSize wxTextCtrl::DoGetBestSize() const
{
return DoGetSizeFromTextSize( DEFAULT_ITEM_WIDTH );
}
wxSize wxTextCtrl::DoGetSizeFromTextSize(int xlen, int ylen) const
{ {
int cx, cy; int cx, cy;
wxGetCharSize(GetHWND(), &cx, &cy, GetFont()); wxGetCharSize(GetHWND(), &cx, &cy, GetFont());
int wText = DEFAULT_ITEM_WIDTH; DWORD wText = 1;
::SystemParametersInfo(SPI_GETCARETWIDTH, 0, &wText, 0);
wText += xlen;
int hText = cy; int hText = cy;
if ( m_windowStyle & wxTE_MULTILINE ) if ( m_windowStyle & wxTE_MULTILINE )
{ {
hText *= wxMax(wxMin(GetNumberOfLines(), 10), 2); // add space for vertical scrollbar
if ( !(m_windowStyle & wxTE_NO_VSCROLL) )
wText += ::GetSystemMetrics(SM_CXVSCROLL);
if ( ylen <= 0 )
{
hText *= wxMax(wxMin(GetNumberOfLines(), 10), 2);
// add space for horizontal scrollbar
if ( m_windowStyle & wxHSCROLL )
hText += ::GetSystemMetrics(SM_CYHSCROLL);
}
}
// for single line control cy (height + external leading) is ok
else
{
// Add the margins we have previously set
wxPoint marg( GetMargins() );
wText += wxMax(0, marg.x);
hText += wxMax(0, marg.y);
} }
//else: for single line control everything is ok
// Text controls without border are special and have the same height as // Text controls without border are special and have the same height as
// static labels (they also have the same appearance when they're disable // static labels (they also have the same appearance when they're disable
@@ -2126,11 +2150,18 @@ wxSize wxTextCtrl::DoGetBestSize() const
// stand out). // stand out).
if ( !HasFlag(wxBORDER_NONE) ) if ( !HasFlag(wxBORDER_NONE) )
{ {
wText += 9; // borders and inner margins
// we have to add the adjustments for the control height only once, not // we have to add the adjustments for the control height only once, not
// once per line, so do it after multiplication above // once per line, so do it after multiplication above
hText += EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy) - cy; hText += EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy) - cy;
} }
// Perhaps the user wants something different from CharHeight, or ylen
// is used as the height of a multiline text.
if ( ylen > 0 )
hText += ylen - GetCharHeight();
return wxSize(wText, hText); return wxSize(wText, hText);
} }