KiCad PCB EDA Suite
GRIDCELL_AUTOWRAP_STRINGRENDERER Class Reference
Inheritance diagram for GRIDCELL_AUTOWRAP_STRINGRENDERER:

Public Member Functions

int GetHeight (wxDC &aDC, wxGrid *aGrid, int aRow, int aCol)
 
wxGridCellRenderer * Clone () const override
 

Private Member Functions

wxArrayString GetTextLines (wxGrid &grid, wxDC &dc, const wxGridCellAttr &attr, const wxRect &rect, int row, int col)
 
void BreakLine (wxDC &dc, const wxString &logicalLine, wxCoord maxWidth, wxArrayString &lines)
 
wxCoord BreakWord (wxDC &dc, const wxString &word, wxCoord maxWidth, wxArrayString &lines, wxString &line)
 

Detailed Description

Definition at line 55 of file dialog_edit_symbols_libid.cpp.

Member Function Documentation

◆ BreakLine()

void GRIDCELL_AUTOWRAP_STRINGRENDERER::BreakLine ( wxDC &  dc,
const wxString &  logicalLine,
wxCoord  maxWidth,
wxArrayString &  lines 
)
private

Definition at line 122 of file dialog_edit_symbols_libid.cpp.

124{
125 wxCoord lineWidth = 0;
126 wxString line;
127
128 // For each word
129 wxStringTokenizer wordTokenizer( logicalLine, wxS( " \t" ), wxTOKEN_RET_DELIMS );
130
131 while( wordTokenizer.HasMoreTokens() )
132 {
133 const wxString word = wordTokenizer.GetNextToken();
134 const wxCoord wordWidth = dc.GetTextExtent( word ).x;
135
136 if( lineWidth + wordWidth < maxWidth )
137 {
138 // Word fits, just add it to this line.
139 line += word;
140 lineWidth += wordWidth;
141 }
142 else
143 {
144 // Word does not fit, check whether the word is itself wider that
145 // available width
146 if( wordWidth < maxWidth )
147 {
148 // Word can fit in a new line, put it at the beginning
149 // of the new line.
150 lines.push_back( line );
151 line = word;
152 lineWidth = wordWidth;
153 }
154 else // Word cannot fit in available width at all.
155 {
156 if( !line.empty() )
157 {
158 lines.push_back( line );
159 line.clear();
160 lineWidth = 0;
161 }
162
163 // Break it up in several lines.
164 lineWidth = BreakWord( dc, word, maxWidth, lines, line );
165 }
166 }
167 }
168
169 if( !line.empty() )
170 lines.push_back( line );
171}
wxCoord BreakWord(wxDC &dc, const wxString &word, wxCoord maxWidth, wxArrayString &lines, wxString &line)

References BreakWord().

Referenced by GetTextLines().

◆ BreakWord()

wxCoord GRIDCELL_AUTOWRAP_STRINGRENDERER::BreakWord ( wxDC &  dc,
const wxString &  word,
wxCoord  maxWidth,
wxArrayString &  lines,
wxString &  line 
)
private

Definition at line 175 of file dialog_edit_symbols_libid.cpp.

178{
179 wxArrayInt widths;
180 dc.GetPartialTextExtents( word, widths );
181
182 // TODO: Use binary search to find the first element > maxWidth.
183 const unsigned count = widths.size();
184 unsigned n;
185
186 for( n = 0; n < count; n++ )
187 {
188 if( widths[n] > maxWidth )
189 break;
190 }
191
192 if( n == 0 )
193 {
194 // This is a degenerate case: the first character of the word is
195 // already wider than the available space, so we just can't show it
196 // completely and have to put the first character in this line.
197 n = 1;
198 }
199
200 lines.push_back( word.substr( 0, n ) );
201
202 // Check if the remainder of the string fits in one line.
203 //
204 // Unfortunately we can't use the existing partial text extents as the
205 // extent of the remainder may be different when it's rendered in a
206 // separate line instead of as part of the same one, so we have to
207 // recompute it.
208 const wxString rest = word.substr( n );
209 const wxCoord restWidth = dc.GetTextExtent( rest ).x;
210
211 if( restWidth <= maxWidth )
212 {
213 line = rest;
214 return restWidth;
215 }
216
217 // Break the rest of the word into lines.
218 //
219 // TODO: Perhaps avoid recursion? The code is simpler like this but using a
220 // loop in this function would probably be more efficient.
221 return BreakWord( dc, rest, maxWidth, lines, line );
222}

References BreakWord().

Referenced by BreakLine(), and BreakWord().

◆ Clone()

wxGridCellRenderer * GRIDCELL_AUTOWRAP_STRINGRENDERER::Clone ( ) const
inlineoverride

◆ GetHeight()

int GRIDCELL_AUTOWRAP_STRINGRENDERER::GetHeight ( wxDC &  aDC,
wxGrid *  aGrid,
int  aRow,
int  aCol 
)

Definition at line 227 of file dialog_edit_symbols_libid.cpp.

228{
229 wxGridCellAttr* attr = aGrid->GetOrCreateCellAttr( aRow, aCol );
230 wxRect rect;
231
232 aDC.SetFont( attr->GetFont() );
233 rect.SetWidth( aGrid->GetColSize( aCol ) - ( 2 * GRID_CELL_MARGIN ) );
234
235 const size_t numLines = GetTextLines( *aGrid, aDC, *attr, rect, aRow, aCol ).size();
236 const int textHeight = numLines * aDC.GetCharHeight();
237
238 attr->DecRef();
239
240 return textHeight + ( 2 * GRID_CELL_MARGIN );
241}
wxArrayString GetTextLines(wxGrid &grid, wxDC &dc, const wxGridCellAttr &attr, const wxRect &rect, int row, int col)
#define GRID_CELL_MARGIN

References GetTextLines(), and GRID_CELL_MARGIN.

Referenced by DIALOG_EDIT_SYMBOLS_LIBID::AddRowToGrid(), and DIALOG_EDIT_SYMBOLS_LIBID::OnSizeGrid().

◆ GetTextLines()

wxArrayString GRIDCELL_AUTOWRAP_STRINGRENDERER::GetTextLines ( wxGrid &  grid,
wxDC &  dc,
const wxGridCellAttr &  attr,
const wxRect &  rect,
int  row,
int  col 
)
private

Definition at line 87 of file dialog_edit_symbols_libid.cpp.

90{
91 dc.SetFont( attr.GetFont() );
92 const wxCoord maxWidth = rect.GetWidth();
93
94 // Transform logical lines into physical ones, wrapping the longer ones.
95 const wxArrayString logicalLines = wxSplit( grid.GetCellValue( row, col ), '\n', '\0' );
96
97 // Trying to do anything if the column is hidden anyhow doesn't make sense
98 // and we run into problems in BreakLine() in this case.
99 if( maxWidth <= 0 )
100 return logicalLines;
101
102 wxArrayString physicalLines;
103
104 for( const wxString& line : logicalLines )
105 {
106 if( dc.GetTextExtent( line ).x > maxWidth )
107 {
108 // Line does not fit, break it up.
109 BreakLine( dc, line, maxWidth, physicalLines );
110 }
111 else // The entire line fits as is
112 {
113 physicalLines.push_back( line );
114 }
115 }
116
117 return physicalLines;
118}
void BreakLine(wxDC &dc, const wxString &logicalLine, wxCoord maxWidth, wxArrayString &lines)

References BreakLine(), and grid.

Referenced by GetHeight().


The documentation for this class was generated from the following file: