multi line static controls now calculate their width and height correctly

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@2898 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
1999-06-25 22:50:54 +00:00
parent b134516c10
commit 4b5d9823ec

View File

@@ -98,6 +98,7 @@ void wxStaticText::DoSetSize(int x, int y, int width, int height, int sizeFlags)
{ {
int currentX, currentY; int currentX, currentY;
GetPosition(&currentX, &currentY); GetPosition(&currentX, &currentY);
int x1 = x; int x1 = x;
int y1 = y; int y1 = y;
@@ -111,58 +112,63 @@ void wxStaticText::DoSetSize(int x, int y, int width, int height, int sizeFlags)
int actualWidth = width; int actualWidth = width;
int actualHeight = height; int actualHeight = height;
int current_width;
int cyf;
wxString text(wxGetWindowText(GetHWND())); wxString text(wxGetWindowText(GetHWND()));
GetTextExtent(text, &current_width, &cyf, NULL, NULL, & GetFont());
int widthTextMax = 0, widthLine,
heightTextTotal = 0, heightLine;
wxString curLine;
for ( const char *pc = text; ; pc++ ) {
if ( *pc == '\n' || *pc == '\0' ) {
GetTextExtent(curLine, &widthLine, &heightLine);
if ( width > widthTextMax )
widthTextMax = widthLine;
heightTextTotal += heightLine;
if ( *pc == '\n' ) {
curLine.Empty();
}
else {
// the end of string
break;
}
}
else {
curLine += *pc;
}
}
int ww, hh; int ww, hh;
GetSize(&ww, &hh); GetSize(&ww, &hh);
// If we're prepared to use the existing width, then... // If we're prepared to use the existing width, then...
if (width == -1 && ((sizeFlags & wxSIZE_AUTO_WIDTH) != wxSIZE_AUTO_WIDTH)) if (width == -1 && ((sizeFlags & wxSIZE_AUTO_WIDTH) != wxSIZE_AUTO_WIDTH))
{
actualWidth = ww; actualWidth = ww;
}
else if (width == -1) else if (width == -1)
{ {
int cx; actualWidth = widthTextMax;
int cy;
wxGetCharSize(GetHWND(), &cx, &cy, & GetFont());
actualWidth = (int)(current_width + cx) ;
} }
// If we're prepared to use the existing height, then... // If we're prepared to use the existing height, then...
if (height == -1 && ((sizeFlags & wxSIZE_AUTO_HEIGHT) != wxSIZE_AUTO_HEIGHT)) if (height == -1 && ((sizeFlags & wxSIZE_AUTO_HEIGHT) != wxSIZE_AUTO_HEIGHT))
{
actualHeight = hh; actualHeight = hh;
}
else if (height == -1) else if (height == -1)
{ {
actualHeight = (int)(cyf) ; actualHeight = heightTextTotal;
} }
MoveWindow((HWND) GetHWND(), x1, y1, actualWidth, actualHeight, TRUE); MoveWindow(GetHwnd(), x1, y1, actualWidth, actualHeight, TRUE);
} }
void wxStaticText::SetLabel(const wxString& label) void wxStaticText::SetLabel(const wxString& label)
{ {
wxWindow *parent = GetParent(); SetWindowText(GetHwnd(), label);
RECT rect; DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT);
GetWindowRect((HWND) GetHWND(), &rect);
// Since we now have the absolute screen coords, if there's a parent we must
// subtract its top left corner
POINT point;
point.x = rect.left;
point.y = rect.top;
if (parent)
{
::ScreenToClient((HWND) parent->GetHWND(), &point);
}
int w, h;
GetTextExtent(label, &w, &h, NULL, NULL, & GetFont());
MoveWindow((HWND) GetHWND(), point.x, point.y, (int)(w + 10), (int)h, TRUE);
SetWindowText((HWND) GetHWND(), (const wxChar *)label);
} }
WXHBRUSH wxStaticText::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor, WXHBRUSH wxStaticText::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,