wxCoord wxGridCellAutoWrapStringRenderer::BreakWord(wxDC& dc, const wxString& word, wxCoord maxWidth, wxArrayString& lines, wxString& line) { wxArrayInt widths; dc.GetPartialTextExtents(word, widths); // TODO: Use binary search to find the first element > maxWidth. const unsigned count = widths.size(); unsigned n; for ( n = 0; n < count; n++ ) { if ( widths[n] > maxWidth ) break; } if ( n == 0 ) { // This is a degenerate case: the first character of the word is // already wider than the available space, so we just can't show it // completely and have to put the first character in this line. n = 1; } lines.push_back(word.substr(0, n)); // Check if the remainder of the string fits in one line. // // Unfortunately we can't use the existing partial text extents as the // extent of the remainder may be different when it's rendered in a // separate line instead of as part of the same one, so we have to // recompute it. const wxString rest = word.substr(n); const wxCoord restWidth = dc.GetTextExtent(rest).x; if ( restWidth <= maxWidth ) { line = rest; return restWidth; } // Break the rest of the word into lines. // // TODO: Perhaps avoid recursion? The code is simpler like this but using a // loop in this function would probably be more efficient. return BreakWord(dc, rest, maxWidth, lines, line); }
WLIST *GetWord(void) { WLIST *a; WordPtr=BreakWord(WordPtr,WordBuffer,WordList,WD_NOISE); if(!WordPtr) return((WLIST *)-1); a=FindInList(WordList,WordBuffer,0); return(a); }
int GetNumber(void) { int a; WordPtr=BreakWord(WordPtr,WordBuffer,WordList,WD_NOISE); if(!WordPtr) return(-1); if(sscanf(WordBuffer,"%d",&a)==0) return(-1); return(a); }
void wxGridCellAutoWrapStringRenderer::BreakLine(wxDC& dc, const wxString& logicalLine, wxCoord maxWidth, wxArrayString& lines) { wxCoord lineWidth = 0; wxString line; // For each word wxStringTokenizer wordTokenizer(logicalLine, wxS(" \t"), wxTOKEN_RET_DELIMS); while ( wordTokenizer.HasMoreTokens() ) { const wxString word = wordTokenizer.GetNextToken(); const wxCoord wordWidth = dc.GetTextExtent(word).x; if ( lineWidth + wordWidth < maxWidth ) { // Word fits, just add it to this line. line += word; lineWidth += wordWidth; } else { // Word does not fit, check whether the word is itself wider that // available width if ( wordWidth < maxWidth ) { // Word can fit in a new line, put it at the beginning // of the new line. lines.push_back(line); line = word; lineWidth = wordWidth; } else // Word cannot fit in available width at all. { if ( !line.empty() ) { lines.push_back(line); line.clear(); lineWidth = 0; } // Break it up in several lines. lineWidth = BreakWord(dc, word, maxWidth, lines, line); } } } if ( !line.empty() ) lines.push_back(line); }
char *GetParsedWord(void) { return(WordPtr=BreakWord(WordPtr,WordBuffer,WordList,WD_NOISE)); }