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