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