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