From: Todd D. P. <ci...@us...> - 2003-09-24 01:28:51
|
Log Message: ----------- Static text now understands "\n" in the string as a line-break Modified Files: -------------- /cvsroot/decaldev/source/DecalControls: Static.cpp Revision Data ------------- Index: Static.cpp =================================================================== RCS file: /cvsroot/decaldev/source/DecalControls/Static.cpp,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -r1.5 -r1.6 --- Static.cpp 22 Sep 2003 02:56:51 -0000 1.5 +++ Static.cpp 24 Sep 2003 01:28:48 -0000 1.6 @@ -153,21 +153,95 @@ pPlugin->CreateFont( bstrFontName /*_bstr_t( _T( "Times New Roman" ) )*/, 14, 0, &m_pFont ); } - SIZE Size; - char *token; - _bstr_t strText= m_strText.copy(); - _bstr_t temp=_bstr_t(""); - - POINT ptText = { 0, 0 }; + SIZE Size; + char *token; + POINT ptText = { 0, 0 }; - m_pFont->MeasureText(strText, &Size); - int height = Size.cy; + //_bstr_t strText= m_strText.copy(); + //_bstr_t temp=_bstr_t(""); + // Determine control size RECT rcPos; m_pSite->get_Position(&rcPos); int nWidth = rcPos.right - rcPos.left; int nHeight = rcPos.bottom - rcPos.top; + // Copy the string for parsing + std::string strText((const char *) m_strText); + + // Determine line height + m_pFont->MeasureText(_bstr_t(strText.data()), &Size); + int height = Size.cy; + + // Break it into lines (can't use strtok or we miss blank lines) + std::vector<_bstr_t> lines; + int nStart = 0, nEnd; + bool bDone = false; + while (!bDone) + { + // Get the next CR + nEnd = strText.find("\n", nStart); + if (nEnd == std::string::npos) + { + bDone = true; + nEnd = strText.length(); + } + + // Store the line + lines.push_back(strText.substr(nStart, nEnd - nStart).data()); + + // Move one character beyond + nStart = nEnd + 1; + } + + // Loop through the lines + _bstr_t bstrSpace(" "), bstrEmpty(""); + int nIndex, nLines = lines.size(); + for (nIndex = 0, bDone = false; !bDone && (nIndex < nLines); nIndex++) + { + // Copy the line for parsing + _bstr_t strLine((const char *) lines[nIndex]); + + // Line to be built + _bstr_t strOut = bstrEmpty; + + // Loop through the "words" on the line + for (token = ::strtok((char *) strLine, " "); !bDone && (token != NULL); token = ::strtok(NULL, " ")) + { + _bstr_t bstrToken(token); + + // Measure this token + m_pFont->MeasureText(strOut + bstrToken, &Size); + + // Is this line full??? + if (Size.cx > nWidth) + { + DrawText(&ptText, strOut, pCanvas); + ptText.y += height; + + // Does the next line put us over the top + bDone = ((ptText.y + height) > nHeight) ? true : false; + + // Clear the line + strOut = bstrEmpty; + } + + // Store the token + strOut += bstrToken + bstrSpace; + } + + // Draw as needed + if (!bDone) + { + DrawText(&ptText, strOut, pCanvas); + ptText.y += height; + } + + // Does the next line put us over the top + bDone = ((ptText.y + height) > nHeight) ? true : false; + } + + /* if(Size.cx > nWidth) { for( token = ::strtok( (char*)strText, " " ); token !=NULL; token = ::strtok( NULL, " " ) ) @@ -193,6 +267,7 @@ { DrawText( &ptText, strText, pCanvas ); } + */ return S_OK; } |