]> git.saurik.com Git - wxWidgets.git/blame - src/common/stattextcmn.cpp
fixing duplicate rti info
[wxWidgets.git] / src / common / stattextcmn.cpp
CommitLineData
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
255c07b4 27#include "wx/textwrapper.h"
39bc0347
VZ
28#include "wx/private/stattext.h"
29
30#ifndef WX_PRECOMP
31 #include "wx/button.h"
32 #include "wx/dcclient.h"
33 #include "wx/intl.h"
523b9ce4 34 #include "wx/log.h"
39bc0347
VZ
35 #include "wx/settings.h"
36 #include "wx/stattext.h"
37 #include "wx/sizer.h"
38 #include "wx/containr.h"
39#endif
40
39bc0347
VZ
41const 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
b1f17bf0 49#if wxUSE_STATTEXT
39bc0347 50
28953245
SC
51// ----------------------------------------------------------------------------
52// XTI
53// ----------------------------------------------------------------------------
54
55wxDEFINE_FLAGS( wxStaticTextStyle )
56wxBEGIN_FLAGS( wxStaticTextStyle )
57// new style border flags, we put them first to
58// use them for streaming out
59wxFLAGS_MEMBER(wxBORDER_SIMPLE)
60wxFLAGS_MEMBER(wxBORDER_SUNKEN)
61wxFLAGS_MEMBER(wxBORDER_DOUBLE)
62wxFLAGS_MEMBER(wxBORDER_RAISED)
63wxFLAGS_MEMBER(wxBORDER_STATIC)
64wxFLAGS_MEMBER(wxBORDER_NONE)
65
66// old style border flags
67wxFLAGS_MEMBER(wxSIMPLE_BORDER)
68wxFLAGS_MEMBER(wxSUNKEN_BORDER)
69wxFLAGS_MEMBER(wxDOUBLE_BORDER)
70wxFLAGS_MEMBER(wxRAISED_BORDER)
71wxFLAGS_MEMBER(wxSTATIC_BORDER)
72wxFLAGS_MEMBER(wxBORDER)
73
74// standard window styles
75wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
76wxFLAGS_MEMBER(wxCLIP_CHILDREN)
77wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
78wxFLAGS_MEMBER(wxWANTS_CHARS)
79wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
80wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
81wxFLAGS_MEMBER(wxVSCROLL)
82wxFLAGS_MEMBER(wxHSCROLL)
83
84wxFLAGS_MEMBER(wxST_NO_AUTORESIZE)
85wxFLAGS_MEMBER(wxALIGN_LEFT)
86wxFLAGS_MEMBER(wxALIGN_RIGHT)
87wxFLAGS_MEMBER(wxALIGN_CENTRE)
88wxEND_FLAGS( wxStaticTextStyle )
89
90wxIMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticText, wxControl, "wx/stattext.h")
91
92wxBEGIN_PROPERTIES_TABLE(wxStaticText)
93wxPROPERTY( Label,wxString, SetLabel, GetLabel, wxString(), 0 /*flags*/, \
94 wxT("Helpstring"), wxT("group"))
95wxPROPERTY_FLAGS( WindowStyle, wxStaticTextStyle, long, SetWindowStyleFlag, \
96 GetWindowStyleFlag, wxEMPTY_PARAMETER_VALUE, 0 /*flags*/, \
97 wxT("Helpstring"), wxT("group")) // style
98wxEND_PROPERTIES_TABLE()
99
100wxEMPTY_HANDLERS_TABLE(wxStaticText)
101
102wxCONSTRUCTOR_6( wxStaticText, wxWindow*, Parent, wxWindowID, Id, \
103 wxString, Label, wxPoint, Position, wxSize, Size, long, WindowStyle )
104
105
39bc0347
VZ
106// ----------------------------------------------------------------------------
107// wxTextWrapper
108// ----------------------------------------------------------------------------
109
110void wxTextWrapper::Wrap(wxWindow *win, const wxString& text, int widthMax)
111{
39bc0347
VZ
112 wxString line;
113
5b871700
VS
114 wxString::const_iterator lastSpace = text.end();
115 wxString::const_iterator lineStart = text.begin();
116 for ( wxString::const_iterator p = lineStart; ; ++p )
39bc0347
VZ
117 {
118 if ( IsStartOfNewLine() )
119 {
120 OnNewLine();
121
5b871700 122 lastSpace = text.end();
39bc0347
VZ
123 line.clear();
124 lineStart = p;
125 }
126
9a83f860 127 if ( p == text.end() || *p == wxT('\n') )
39bc0347
VZ
128 {
129 DoOutputLine(line);
130
5b871700 131 if ( p == text.end() )
39bc0347
VZ
132 break;
133 }
134 else // not EOL
135 {
9a83f860 136 if ( *p == wxT(' ') )
39bc0347
VZ
137 lastSpace = p;
138
139 line += *p;
140
5b871700 141 if ( widthMax >= 0 && lastSpace != text.end() )
39bc0347
VZ
142 {
143 int width;
144 win->GetTextExtent(line, &width, NULL);
145
146 if ( width > widthMax )
147 {
148 // remove the last word from this line
149 line.erase(lastSpace - lineStart, p + 1 - lineStart);
150 DoOutputLine(line);
151
152 // go back to the last word of this line which we didn't
153 // output yet
154 p = lastSpace;
155 }
156 }
157 //else: no wrapping at all or impossible to wrap
158 }
159 }
160}
161
162
163// ----------------------------------------------------------------------------
164// wxLabelWrapper: helper class for wxStaticTextBase::Wrap()
165// ----------------------------------------------------------------------------
166
167class wxLabelWrapper : public wxTextWrapper
168{
169public:
170 void WrapLabel(wxWindow *text, int widthMax)
171 {
172 m_text.clear();
173 Wrap(text, text->GetLabel(), widthMax);
174 text->SetLabel(m_text);
175 }
176
177protected:
178 virtual void OnOutputLine(const wxString& line)
179 {
180 m_text += line;
181 }
182
183 virtual void OnNewLine()
184 {
9a83f860 185 m_text += wxT('\n');
39bc0347
VZ
186 }
187
188private:
189 wxString m_text;
190};
191
192
193// ----------------------------------------------------------------------------
194// wxStaticTextBase
195// ----------------------------------------------------------------------------
196
197void wxStaticTextBase::Wrap(int width)
198{
199 wxLabelWrapper wrapper;
200 wrapper.WrapLabel(this, width);
201}
202
203wxString wxStaticTextBase::GetLabelText() const
204{
205 wxString ret(GetLabel());
206
207 if (HasFlag(wxST_MARKUP))
208 ret = RemoveMarkup(ret);
209 return RemoveMnemonics(ret);
210}
211
32ee98eb
FM
212void wxStaticTextBase::SetLabelText(const wxString& text)
213{
214 wxString str = text;
215
216 if (HasFlag(wxST_MARKUP))
217 str = EscapeMarkup(str); // escapes markup and the & characters (which are also mnemonics)
218 else
219 str = EscapeMnemonics(text); // escape only the mnemonics
220 SetLabel(str);
221}
222
223/* static */
19cf1ef3
FM
224wxString wxStaticTextBase::GetLabelText(const wxString& label)
225{
19cf1ef3 226 wxString ret = RemoveMarkup(label);
ce00f59b 227 // always remove the markup (this function is static
32ee98eb
FM
228 // and cannot check for wxST_MARKUP presence/absence)
229
19cf1ef3
FM
230 return RemoveMnemonics(ret);
231}
232
32ee98eb 233/* static */
39bc0347
VZ
234wxString wxStaticTextBase::RemoveMarkup(const wxString& text)
235{
236 // strip out of "text" the markup for platforms which don't support it natively
237 bool inside_tag = false;
238
239 wxString label;
5b871700
VS
240 for ( wxString::const_iterator source = text.begin();
241 source != text.end(); ++source )
39bc0347 242 {
5b871700 243 switch ( (*source).GetValue() )
39bc0347
VZ
244 {
245 case wxT('<'):
246 if (inside_tag)
247 {
248 wxLogDebug(wxT("Invalid markup !"));
249 return wxEmptyString;
250 }
251 inside_tag = true;
252 break;
253
254 case wxT('>'):
255 if (!inside_tag)
256 {
257 wxLogDebug(wxT("Invalid markup !"));
258 return wxEmptyString;
259 }
260 inside_tag = false;
261 break;
262
263 case wxT('&'):
264 {
5b871700 265 if ( source+1 == text.end() )
39bc0347
VZ
266 {
267 wxLogDebug(wxT("Cannot use & as last character of the string '%s'"),
268 text.c_str());
269 return wxEmptyString;
270 }
271
272 // is this ampersand introducing a mnemonic or rather an entity?
273 bool isMnemonic = true;
5b871700 274 size_t distanceFromEnd = text.end() - source;
39bc0347
VZ
275 for (size_t j=0; j < wxMARKUP_ENTITY_MAX; j++)
276 {
277 const wxChar *entity = wxMarkupEntities[wxMARKUP_ELEMENT_NAME][j];
278 size_t entityLen = wxStrlen(entity);
279
5b871700
VS
280 if (distanceFromEnd >= entityLen &&
281 wxString(source, source + entityLen) == entity)
39bc0347
VZ
282 {
283 // replace the &entity; string with the entity reference
284 label << wxMarkupEntities[wxMARKUP_ELEMENT_VALUE][j];
5b871700
VS
285 // little exception: when the entity reference is
286 // "&" (i.e. when entity is "&amp;"), substitute it
287 // with && instead of a single ampersand:
39bc0347
VZ
288 if (*wxMarkupEntities[wxMARKUP_ELEMENT_VALUE][j] == wxT('&'))
289 label << wxT('&');
5b871700
VS
290 // the -1 is because main for() loop already
291 // increments i:
292 source += entityLen - 1;
39bc0347
VZ
293 isMnemonic = false;
294 break;
295 }
296 }
297
298 if (isMnemonic)
5b871700 299 label << *source;
39bc0347
VZ
300 }
301 break;
302
303
304 default:
305 if (!inside_tag)
5b871700 306 label << *source;
39bc0347
VZ
307 }
308 }
309
310 return label;
311}
312
313/* static */
314wxString wxStaticTextBase::EscapeMarkup(const wxString& text)
315{
316 wxString ret;
317
5b871700
VS
318 for (wxString::const_iterator source = text.begin();
319 source != text.end(); ++source)
39bc0347
VZ
320 {
321 bool isEntity = false;
322
323 // search in the list of the entities and eventually escape this character
324 for (size_t j=0; j < wxMARKUP_ENTITY_MAX; j++)
325 {
326 if (*source == *wxMarkupEntities[wxMARKUP_ELEMENT_VALUE][j])
327 {
328 ret << wxMarkupEntities[wxMARKUP_ELEMENT_NAME][j];
329 isEntity = true;
330 break;
331 }
332 }
333
334 if (!isEntity)
335 ret << *source; // this character does not need to be escaped
336 }
337
338 return ret;
339}
340
4520d583
FM
341
342// ----------------------------------------------------------------------------
343// wxStaticTextBase - generic implementation for wxST_ELLIPSIZE_* support
344// ----------------------------------------------------------------------------
345
346void wxStaticTextBase::UpdateLabel()
347{
348 if (!IsEllipsized())
349 return;
350
351 wxString newlabel = GetEllipsizedLabelWithoutMarkup();
352
353 // we need to touch the "real" label (i.e. the text set inside the control,
354 // using port-specific functions) instead of the string returned by GetLabel().
355 //
356 // In fact, we must be careful not to touch the original label passed to
357 // SetLabel() otherwise GetLabel() will behave in a strange way to the user
358 // (e.g. returning a "Ver...ing" instead of "Very long string") !
359 if (newlabel == DoGetLabel())
360 return;
361 DoSetLabel(newlabel);
362}
363
32ee98eb
FM
364wxString wxStaticTextBase::GetLabelWithoutMarkup() const
365{
366 wxString ret(m_labelOrig);
367
368 if (HasFlag(wxST_MARKUP))
369 ret = RemoveMarkup(ret);
370
371 // unlike GetLabelText() we don't remove the mnemonics here!
372 return ret;
373}
374
4520d583
FM
375wxString wxStaticTextBase::GetEllipsizedLabelWithoutMarkup() const
376{
377 // this function should be used only by ports which do not support
378 // ellipsis in static texts: we first remove markup (which cannot
379 // be handled safely by Ellipsize()) and then ellipsize the result.
380
381 wxString ret(m_labelOrig);
382
383 // the order of the following two blocks is important!
384
385 if (HasFlag(wxST_MARKUP))
386 ret = RemoveMarkup(ret);
387
388 if (IsEllipsized())
389 ret = Ellipsize(ret);
390
391 return ret;
392}
393
394wxString wxStaticTextBase::Ellipsize(const wxString& label) const
395{
396 wxSize sz(GetSize());
397 if (sz.GetWidth() < 2 || sz.GetHeight() < 2)
398 {
399 // the size of this window is not valid (yet)
400 return label;
401 }
402
403 wxClientDC dc(const_cast<wxStaticTextBase*>(this));
404 dc.SetFont(GetFont());
405
406 wxEllipsizeMode mode;
41ce5eff
VZ
407 if ( HasFlag(wxST_ELLIPSIZE_START) )
408 mode = wxELLIPSIZE_START;
409 else if ( HasFlag(wxST_ELLIPSIZE_MIDDLE) )
410 mode = wxELLIPSIZE_MIDDLE;
411 else if ( HasFlag(wxST_ELLIPSIZE_END) )
412 mode = wxELLIPSIZE_END;
413 else
414 {
415 wxFAIL_MSG( "should only be called if have one of wxST_ELLIPSIZE_XXX" );
416
417 return label;
418 }
4520d583 419
5c87527c 420 return wxControl::Ellipsize(label, dc, mode, sz.GetWidth());
39bc0347
VZ
421}
422
423#endif // wxUSE_STATTEXT