]> git.saurik.com Git - wxWidgets.git/blob - src/common/stattextcmn.cpp
refactor wxStaticText::Ellipsize so it's a static public utility function; document...
[wxWidgets.git] / src / common / stattextcmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/stattextcmn.cpp
3 // Purpose: common (to all ports) wxStaticText functions
4 // Author: Vadim Zeitlin, Francesco Montorsi
5 // Created: 2007-01-07 (extracted from dlgcmn.cpp)
6 // RCS-ID: $Id$
7 // Copyright: (c) 1999-2006 Vadim Zeitlin
8 // (c) 2007 Francesco Montorsi
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #include "wx/private/stattext.h"
28
29 #ifndef WX_PRECOMP
30 #include "wx/button.h"
31 #include "wx/dcclient.h"
32 #include "wx/intl.h"
33 #include "wx/log.h"
34 #include "wx/settings.h"
35 #include "wx/stattext.h"
36 #include "wx/sizer.h"
37 #include "wx/containr.h"
38 #endif
39
40 const wxChar *wxMarkupEntities[][wxMARKUP_ENTITY_MAX] =
41 {
42 // the entities handled by SetLabel() when wxST_MARKUP is used and their referenced string
43
44 { wxT("&"), wxT("<"), wxT(">"), wxT("'"), wxT(""") },
45 { wxT("&"), wxT("<"), wxT(">"), wxT("'"), wxT("\"") }
46 };
47
48 #if wxUSE_STATTEXT
49
50 // ----------------------------------------------------------------------------
51 // wxTextWrapper
52 // ----------------------------------------------------------------------------
53
54 void wxTextWrapper::Wrap(wxWindow *win, const wxString& text, int widthMax)
55 {
56 wxString line;
57
58 wxString::const_iterator lastSpace = text.end();
59 wxString::const_iterator lineStart = text.begin();
60 for ( wxString::const_iterator p = lineStart; ; ++p )
61 {
62 if ( IsStartOfNewLine() )
63 {
64 OnNewLine();
65
66 lastSpace = text.end();
67 line.clear();
68 lineStart = p;
69 }
70
71 if ( p == text.end() || *p == _T('\n') )
72 {
73 DoOutputLine(line);
74
75 if ( p == text.end() )
76 break;
77 }
78 else // not EOL
79 {
80 if ( *p == _T(' ') )
81 lastSpace = p;
82
83 line += *p;
84
85 if ( widthMax >= 0 && lastSpace != text.end() )
86 {
87 int width;
88 win->GetTextExtent(line, &width, NULL);
89
90 if ( width > widthMax )
91 {
92 // remove the last word from this line
93 line.erase(lastSpace - lineStart, p + 1 - lineStart);
94 DoOutputLine(line);
95
96 // go back to the last word of this line which we didn't
97 // output yet
98 p = lastSpace;
99 }
100 }
101 //else: no wrapping at all or impossible to wrap
102 }
103 }
104 }
105
106
107 // ----------------------------------------------------------------------------
108 // wxLabelWrapper: helper class for wxStaticTextBase::Wrap()
109 // ----------------------------------------------------------------------------
110
111 class wxLabelWrapper : public wxTextWrapper
112 {
113 public:
114 void WrapLabel(wxWindow *text, int widthMax)
115 {
116 m_text.clear();
117 Wrap(text, text->GetLabel(), widthMax);
118 text->SetLabel(m_text);
119 }
120
121 protected:
122 virtual void OnOutputLine(const wxString& line)
123 {
124 m_text += line;
125 }
126
127 virtual void OnNewLine()
128 {
129 m_text += _T('\n');
130 }
131
132 private:
133 wxString m_text;
134 };
135
136
137 // ----------------------------------------------------------------------------
138 // wxStaticTextBase
139 // ----------------------------------------------------------------------------
140
141 void wxStaticTextBase::Wrap(int width)
142 {
143 wxLabelWrapper wrapper;
144 wrapper.WrapLabel(this, width);
145 }
146
147 wxString wxStaticTextBase::GetLabelText() const
148 {
149 wxString ret(GetLabel());
150
151 if (HasFlag(wxST_MARKUP))
152 ret = RemoveMarkup(ret);
153 return RemoveMnemonics(ret);
154 }
155
156 /*static*/
157 wxString wxStaticTextBase::GetLabelText(const wxString& label)
158 {
159 // remove markup
160 wxString ret = RemoveMarkup(label);
161 return RemoveMnemonics(ret);
162 }
163
164 /*static*/
165 wxString wxStaticTextBase::RemoveMarkup(const wxString& text)
166 {
167 // strip out of "text" the markup for platforms which don't support it natively
168 bool inside_tag = false;
169
170 wxString label;
171 for ( wxString::const_iterator source = text.begin();
172 source != text.end(); ++source )
173 {
174 switch ( (*source).GetValue() )
175 {
176 case wxT('<'):
177 if (inside_tag)
178 {
179 wxLogDebug(wxT("Invalid markup !"));
180 return wxEmptyString;
181 }
182 inside_tag = true;
183 break;
184
185 case wxT('>'):
186 if (!inside_tag)
187 {
188 wxLogDebug(wxT("Invalid markup !"));
189 return wxEmptyString;
190 }
191 inside_tag = false;
192 break;
193
194 case wxT('&'):
195 {
196 if ( source+1 == text.end() )
197 {
198 wxLogDebug(wxT("Cannot use & as last character of the string '%s'"),
199 text.c_str());
200 return wxEmptyString;
201 }
202
203 // is this ampersand introducing a mnemonic or rather an entity?
204 bool isMnemonic = true;
205 size_t distanceFromEnd = text.end() - source;
206 for (size_t j=0; j < wxMARKUP_ENTITY_MAX; j++)
207 {
208 const wxChar *entity = wxMarkupEntities[wxMARKUP_ELEMENT_NAME][j];
209 size_t entityLen = wxStrlen(entity);
210
211 if (distanceFromEnd >= entityLen &&
212 wxString(source, source + entityLen) == entity)
213 {
214 // replace the &entity; string with the entity reference
215 label << wxMarkupEntities[wxMARKUP_ELEMENT_VALUE][j];
216 // little exception: when the entity reference is
217 // "&" (i.e. when entity is "&amp;"), substitute it
218 // with && instead of a single ampersand:
219 if (*wxMarkupEntities[wxMARKUP_ELEMENT_VALUE][j] == wxT('&'))
220 label << wxT('&');
221 // the -1 is because main for() loop already
222 // increments i:
223 source += entityLen - 1;
224 isMnemonic = false;
225 break;
226 }
227 }
228
229 if (isMnemonic)
230 label << *source;
231 }
232 break;
233
234
235 default:
236 if (!inside_tag)
237 label << *source;
238 }
239 }
240
241 return label;
242 }
243
244 /* static */
245 wxString wxStaticTextBase::EscapeMarkup(const wxString& text)
246 {
247 wxString ret;
248
249 for (wxString::const_iterator source = text.begin();
250 source != text.end(); ++source)
251 {
252 bool isEntity = false;
253
254 // search in the list of the entities and eventually escape this character
255 for (size_t j=0; j < wxMARKUP_ENTITY_MAX; j++)
256 {
257 if (*source == *wxMarkupEntities[wxMARKUP_ELEMENT_VALUE][j])
258 {
259 ret << wxMarkupEntities[wxMARKUP_ELEMENT_NAME][j];
260 isEntity = true;
261 break;
262 }
263 }
264
265 if (!isEntity)
266 ret << *source; // this character does not need to be escaped
267 }
268
269 return ret;
270 }
271
272 #define wxELLIPSE_REPLACEMENT wxT("...")
273
274 /* static */
275 wxString wxStaticTextBase::Ellipsize(const wxString& label, const wxDC& dc,
276 wxEllipsizeMode mode, int maxFinalWidth)
277 {
278 wxArrayInt charOffsets;
279 wxString ret;
280
281 // these cannot be cached as they can change because of e.g. a font change
282 int replacementWidth = dc.GetTextExtent(wxELLIPSE_REPLACEMENT).GetWidth();
283 int marginWidth = dc.GetCharWidth()*2;
284
285 // NB: we must handle correctly labels with newlines:
286 wxString curLine;
287 wxSize reqsize;
288 size_t len;
289 for ( wxString::const_iterator pc = label.begin(); ; ++pc )
290 {
291 if ( pc == label.end() || *pc == _T('\n') )
292 {
293 len = curLine.length();
294 if (len > 0 &&
295 dc.GetPartialTextExtents(curLine, charOffsets))
296 {
297 wxASSERT(charOffsets.GetCount() == len);
298
299 size_t totalWidth = charOffsets.Last();
300 if ( totalWidth > (size_t)maxFinalWidth )
301 {
302 // we need to ellipsize this row
303 int excessPixels = totalWidth - maxFinalWidth +
304 replacementWidth +
305 marginWidth; // security margin (NEEDED!)
306
307 // remove characters in excess
308 size_t initialChar, // index of first char to erase
309 nChars; // how many chars do we need to erase?
310 if (mode == wxST_ELLIPSIZE_START)
311 {
312 initialChar = 0;
313 for (nChars=0;
314 nChars < len && charOffsets[nChars] < excessPixels;
315 nChars++)
316 ;
317 }
318 else if (mode == wxST_ELLIPSIZE_MIDDLE)
319 {
320 // the start & end of the removed span of chars
321 initialChar = len/2;
322 size_t endChar = len/2;
323
324 int removed = 0;
325 for ( ; removed < excessPixels; )
326 {
327 if (initialChar > 0)
328 {
329 // width of the initialChar-th character
330 int width = charOffsets[initialChar] -
331 charOffsets[initialChar-1];
332
333 // remove the initialChar-th character
334 removed += width;
335 initialChar--;
336 }
337
338 if (endChar < len - 1 &&
339 removed < excessPixels)
340 {
341 // width of the (endChar+1)-th character
342 int width = charOffsets[endChar+1] -
343 charOffsets[endChar];
344
345 // remove the endChar-th character
346 removed += width;
347 endChar++;
348 }
349
350 if (initialChar == 0 && endChar == len-1)
351 {
352 nChars = len+1;
353 break;
354 }
355 }
356
357 initialChar++;
358 nChars = endChar - initialChar + 1;
359 }
360 else
361 {
362 wxASSERT(mode == wxST_ELLIPSIZE_END);
363 wxASSERT(len > 0);
364
365 int maxWidth = totalWidth - excessPixels;
366 for (initialChar=0;
367 initialChar < len &&
368 charOffsets[initialChar] < maxWidth;
369 initialChar++)
370 ;
371
372 if (initialChar == 0)
373 {
374 nChars = len;
375 }
376 else
377 {
378 initialChar--; // go back one character
379 nChars = len - initialChar;
380 }
381 }
382
383 if (nChars > len)
384 {
385 // need to remove the entire row!
386 curLine.clear();
387 }
388 else
389 {
390 // erase nChars characters after initialChar (included):
391 curLine.erase(initialChar, nChars+1);
392
393 // if there is space for the replacement dots, add them
394 if (maxFinalWidth > replacementWidth)
395 curLine.insert(initialChar, wxELLIPSE_REPLACEMENT);
396 }
397
398 // if everything was ok, we should have shortened this line
399 // enough to make it fit in sz.maxFinalWidth:
400 wxASSERT(dc.GetTextExtent(curLine).GetWidth() < maxFinalWidth);
401 }
402 }
403
404 // add this (ellipsized) row to the rest of the label
405 ret << curLine;
406 if ( pc == label.end() )
407 {
408 return ret;
409 }
410 else
411 {
412 ret << *pc;
413 curLine.clear();
414 }
415 }
416 // we need to remove mnemonics from the label for correct calculations
417 else if ( *pc == _T('&') )
418 {
419 // pc+1 is safe: at worst we'll be at end()
420 wxString::const_iterator next = pc + 1;
421 if ( next != label.end() && *next == _T('&') )
422 curLine += _T('&'); // && becomes &
423 //else: remove this ampersand
424 }
425 // we need also to expand tabs to properly calc their size
426 else if ( *pc == _T('\t') )
427 {
428 // Windows natively expands the TABs to 6 spaces. Do the same:
429 curLine += wxT(" ");
430 }
431 else
432 {
433 curLine += *pc;
434 }
435 }
436
437 return ret;
438 }
439
440
441
442 // ----------------------------------------------------------------------------
443 // wxStaticTextBase - generic implementation for wxST_ELLIPSIZE_* support
444 // ----------------------------------------------------------------------------
445
446 void wxStaticTextBase::UpdateLabel()
447 {
448 if (!IsEllipsized())
449 return;
450
451 wxString newlabel = GetEllipsizedLabelWithoutMarkup();
452
453 // we need to touch the "real" label (i.e. the text set inside the control,
454 // using port-specific functions) instead of the string returned by GetLabel().
455 //
456 // In fact, we must be careful not to touch the original label passed to
457 // SetLabel() otherwise GetLabel() will behave in a strange way to the user
458 // (e.g. returning a "Ver...ing" instead of "Very long string") !
459 if (newlabel == DoGetLabel())
460 return;
461 DoSetLabel(newlabel);
462 }
463
464 wxString wxStaticTextBase::GetEllipsizedLabelWithoutMarkup() const
465 {
466 // this function should be used only by ports which do not support
467 // ellipsis in static texts: we first remove markup (which cannot
468 // be handled safely by Ellipsize()) and then ellipsize the result.
469
470 wxString ret(m_labelOrig);
471
472 // the order of the following two blocks is important!
473
474 if (HasFlag(wxST_MARKUP))
475 ret = RemoveMarkup(ret);
476
477 if (IsEllipsized())
478 ret = Ellipsize(ret);
479
480 return ret;
481 }
482
483 wxString wxStaticTextBase::Ellipsize(const wxString& label) const
484 {
485 wxSize sz(GetSize());
486 if (sz.GetWidth() < 2 || sz.GetHeight() < 2)
487 {
488 // the size of this window is not valid (yet)
489 return label;
490 }
491
492 wxClientDC dc(const_cast<wxStaticTextBase*>(this));
493 dc.SetFont(GetFont());
494
495 wxEllipsizeMode mode;
496 if (HasFlag(wxST_ELLIPSIZE_START)) mode = wxST_ELLIPSIZE_START;
497 else if (HasFlag(wxST_ELLIPSIZE_MIDDLE)) mode = wxST_ELLIPSIZE_MIDDLE;
498 else if (HasFlag(wxST_ELLIPSIZE_END)) mode = wxST_ELLIPSIZE_END;
499
500 return Ellipsize(label, dc, mode, sz.GetWidth());
501 }
502
503 #endif // wxUSE_STATTEXT