void SplitWords(const Gwen::UnicodeString &s, wchar_t delim, std::vector<Gwen::UnicodeString> &elems) { Gwen::UnicodeString str; for ( int i=0; i<s.length(); i++ ) { if ( s[i] == L'\n' ) { if ( !str.empty() ) elems.push_back( str ); elems.push_back( L"\n" ); str.clear(); continue; } if ( s[i] == L' ' ) { str += s[i]; elems.push_back( str ); str.clear(); continue; } str += s[i]; } if ( !str.empty() ) elems.push_back( str ); }
void Text::SplitWords(const Gwen::UnicodeString &s, std::vector<Gwen::UnicodeString> & elems ) { Gwen::UnicodeString str; int w = GetParent()->Width() - GetParent()->GetPadding().left-GetParent()->GetPadding().right; for ( int i=0; i<(int)s.length(); i++ ) { if ( s[i] == L'\n' ) { if ( !str.empty() ) { elems.push_back( str ); } elems.push_back( L"\n" ); str.clear(); continue; } if ( s[i] == L' ' ) { str += s[i]; elems.push_back( str ); str.clear(); continue; } str += s[i]; //if adding character makes the word bigger than the textbox size Gwen::Point p = GetSkin()->GetRender()->MeasureText( GetFont(), str ); if ( p.x > w ) { int addSum = GetPadding().left+GetPadding().right; //split words str.pop_back(); elems.push_back( str ); str.clear(); --i; continue; } } if ( !str.empty() ) { elems.push_back( str ); } }
void Text::RefreshSizeWrap() { RemoveAllChildren(); for ( TextLines::iterator it = m_Lines.begin(); it != m_Lines.end(); ++it ) { delete *it; } m_Lines.clear(); std::vector<Gwen::UnicodeString> words; SplitWords( GetText().GetUnicode(), words ); // Adding a bullshit word to the end simplifies the code below // which is anything but simple. words.push_back( L"" ); if ( !GetFont() ) { Debug::AssertCheck( 0, "Text::RefreshSize() - No Font!!\n" ); return; } Point pFontSize = GetSkin()->GetRender()->MeasureText( GetFont(), L" " ); int w = GetParent()->Width() - GetParent()->GetPadding().left-GetParent()->GetPadding().right; int x = 0, y = 0; Gwen::UnicodeString strLine; for ( std::vector<Gwen::UnicodeString>::iterator it = words.begin(); it != words.end(); ++it ) { bool bFinishLine = false; bool bWrapped = false; // If this word is a newline - make a newline (we still add it to the text) if ( ( *it ).c_str() [0] == L'\n' ) { bFinishLine = true; } // Does adding this word drive us over the width? { strLine += ( *it ); Gwen::Point p = GetSkin()->GetRender()->MeasureText( GetFont(), strLine ); if ( p.x > w ) { bFinishLine = true; bWrapped = true; } } // If this is the last word then finish the line if ( --words.end() == it ) { bFinishLine = true; } if ( bFinishLine ) { Text* t = new Text( this ); t->SetFont( GetFont() ); if(bWrapped) { t->SetString( strLine.substr( 0, strLine.length() - (*it).length() ) ); // newline should start with the word that was too big strLine = *it; } else { t->SetString( strLine.substr( 0, strLine.length()) ); //new line is empty strLine.clear(); } t->RefreshSize(); t->SetPos( x, y ); m_Lines.push_back( t ); // newline should start with the word that was too big // strLine = *it; // Position the newline y += pFontSize.y; x = 0; //if ( strLine[0] == L' ' ) x -= pFontSize.x; } } // Size to children height and parent width { Point childsize = ChildrenSize(); SetSize( w, childsize.y ); } InvalidateParent(); Invalidate(); }