// win is used for getting the font, text is the text to wrap, width is the // max line width or -1 to disable wrapping void Wrap(wxWindow *win, const wxString& text, int widthMax) { const wxChar *lastSpace = NULL; wxString line; const wxChar *lineStart = text.c_str(); for ( const wxChar *p = lineStart; ; p++ ) { if ( IsStartOfNewLine() ) { OnNewLine(); lastSpace = NULL; line.clear(); lineStart = p; } if ( *p == _T('\n') || *p == _T('\0') ) { DoOutputLine(line); if ( *p == _T('\0') ) break; } else // not EOL { if ( *p == _T(' ') ) lastSpace = p; line += *p; if ( widthMax >= 0 && lastSpace ) { int width; win->GetTextExtent(line, &width, NULL); if ( width > widthMax ) { // remove the last word from this line line.erase(lastSpace - lineStart, p + 1 - lineStart); DoOutputLine(line); // go back to the last word of this line which we didn't // output yet p = lastSpace; } } //else: no wrapping at all or impossible to wrap } } }
void wxTextWrapper::Wrap(wxWindow *win, const wxString& text, int widthMax) { wxString line; wxString::const_iterator lastSpace = text.end(); wxString::const_iterator lineStart = text.begin(); for ( wxString::const_iterator p = lineStart; ; ++p ) { if ( IsStartOfNewLine() ) { OnNewLine(); lastSpace = text.end(); line.clear(); lineStart = p; } if ( p == text.end() || *p == wxT('\n') ) { DoOutputLine(line); if ( p == text.end() ) break; } else // not EOL { if ( *p == wxT(' ') ) lastSpace = p; line += *p; if ( widthMax >= 0 && lastSpace != text.end() ) { int width; win->GetTextExtent(line, &width, NULL); if ( width > widthMax ) { // remove the last word from this line line.erase(lastSpace - lineStart, p + 1 - lineStart); DoOutputLine(line); // go back to the last word of this line which we didn't // output yet p = lastSpace; } } //else: no wrapping at all or impossible to wrap } } }
pxTextWrapperBase& pxTextWrapperBase::Wrap( const wxWindow& win, const wxString& text, int widthMax ) { if( text.IsEmpty() ) return *this; const wxChar *lastSpace = NULL; bool wasWrapped = false; wxString line; line.Alloc( text.Length()+12 ); const wxChar* lineStart = text.wc_str(); for ( const wxChar *p = lineStart; ; p++ ) { if ( IsStartOfNewLine() ) { OnNewLine(); lastSpace = NULL; lineStart = p; if(wasWrapped) line = m_indent; else line.clear(); } if ( *p == L'\n' || *p == L'\0' ) { wasWrapped = false; DoOutputLine(line); if ( *p == L'\0' ) break; } else // not EOL { if (is_cjk_char(*p)) { if (!no_break_before(*p)) { if (p == lineStart || !no_break_after(*(p-1))) lastSpace = p; } } else if ( *p == L' ' || *p == L',' || *p == L'/' ) lastSpace = p; line += *p; if ( widthMax >= 0 && lastSpace ) { int width; win.GetTextExtent(line, &width, NULL); if ( width > widthMax ) { wasWrapped = true; // remove the last word from this line line.erase(lastSpace - lineStart, p + 1 - lineStart); DoOutputLine(line); // go back to the last word of this line which we didn't // output yet p = lastSpace; if ( *p != L' ' ) p--; } } //else: no wrapping at all or impossible to wrap } } return *this; }