From 4b5d9823ec8c849bcd51e3b15ba78f656e54493e Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 25 Jun 1999 22:50:54 +0000 Subject: [PATCH] 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 --- src/msw/stattext.cpp | 130 ++++++++++++++++++++++--------------------- 1 file changed, 68 insertions(+), 62 deletions(-) diff --git a/src/msw/stattext.cpp b/src/msw/stattext.cpp index 2d56002296..afbb5f2ff2 100644 --- a/src/msw/stattext.cpp +++ b/src/msw/stattext.cpp @@ -96,73 +96,79 @@ bool wxStaticText::Create(wxWindow *parent, wxWindowID id, void wxStaticText::DoSetSize(int x, int y, int width, int height, int sizeFlags) { - int currentX, currentY; - GetPosition(¤tX, ¤tY); - int x1 = x; - int y1 = y; - - if (x == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) - x1 = currentX; - if (y == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) - y1 = currentY; - - AdjustForParentClientOrigin(x1, y1, sizeFlags); - - int actualWidth = width; - int actualHeight = height; - - int current_width; - int cyf; - - wxString text(wxGetWindowText(GetHWND())); - GetTextExtent(text, ¤t_width, &cyf, NULL, NULL, & GetFont()); - - int ww, hh; - GetSize(&ww, &hh); - - // If we're prepared to use the existing width, then... - if (width == -1 && ((sizeFlags & wxSIZE_AUTO_WIDTH) != wxSIZE_AUTO_WIDTH)) - actualWidth = ww; - else if (width == -1) - { - int cx; - int cy; - wxGetCharSize(GetHWND(), &cx, &cy, & GetFont()); - actualWidth = (int)(current_width + cx) ; - } - - // If we're prepared to use the existing height, then... - if (height == -1 && ((sizeFlags & wxSIZE_AUTO_HEIGHT) != wxSIZE_AUTO_HEIGHT)) - actualHeight = hh; - else if (height == -1) - { - actualHeight = (int)(cyf) ; - } - - MoveWindow((HWND) GetHWND(), x1, y1, actualWidth, actualHeight, TRUE); + int currentX, currentY; + GetPosition(¤tX, ¤tY); + + int x1 = x; + int y1 = y; + + if (x == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) + x1 = currentX; + if (y == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) + y1 = currentY; + + AdjustForParentClientOrigin(x1, y1, sizeFlags); + + int actualWidth = width; + int actualHeight = height; + + wxString text(wxGetWindowText(GetHWND())); + + 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; + GetSize(&ww, &hh); + + // If we're prepared to use the existing width, then... + if (width == -1 && ((sizeFlags & wxSIZE_AUTO_WIDTH) != wxSIZE_AUTO_WIDTH)) + { + actualWidth = ww; + } + else if (width == -1) + { + actualWidth = widthTextMax; + } + + // If we're prepared to use the existing height, then... + if (height == -1 && ((sizeFlags & wxSIZE_AUTO_HEIGHT) != wxSIZE_AUTO_HEIGHT)) + { + actualHeight = hh; + } + else if (height == -1) + { + actualHeight = heightTextTotal; + } + + MoveWindow(GetHwnd(), x1, y1, actualWidth, actualHeight, TRUE); } void wxStaticText::SetLabel(const wxString& label) { - wxWindow *parent = GetParent(); - - RECT rect; - 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); - } + SetWindowText(GetHwnd(), label); - 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); + DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT); } WXHBRUSH wxStaticText::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor, -- 2.45.2