]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/stattextcmn.cpp
Use wxMenuBar::Attach/Detach() instead of SetInvokingWindow() in wxGTK1.
[wxWidgets.git] / src / common / stattextcmn.cpp
... / ...
CommitLineData
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/textwrapper.h"
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"
34 #include "wx/log.h"
35 #include "wx/settings.h"
36 #include "wx/stattext.h"
37 #include "wx/sizer.h"
38 #include "wx/containr.h"
39#endif
40
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
49#if wxUSE_STATTEXT
50
51// ----------------------------------------------------------------------------
52// wxTextWrapper
53// ----------------------------------------------------------------------------
54
55void 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 == wxT('\n') )
73 {
74 DoOutputLine(line);
75
76 if ( p == text.end() )
77 break;
78 }
79 else // not EOL
80 {
81 if ( *p == wxT(' ') )
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
112class wxLabelWrapper : public wxTextWrapper
113{
114public:
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
122protected:
123 virtual void OnOutputLine(const wxString& line)
124 {
125 m_text += line;
126 }
127
128 virtual void OnNewLine()
129 {
130 m_text += wxT('\n');
131 }
132
133private:
134 wxString m_text;
135};
136
137
138// ----------------------------------------------------------------------------
139// wxStaticTextBase
140// ----------------------------------------------------------------------------
141
142void wxStaticTextBase::Wrap(int width)
143{
144 wxLabelWrapper wrapper;
145 wrapper.WrapLabel(this, width);
146}
147
148wxString wxStaticTextBase::GetLabelText() const
149{
150 wxString ret(GetLabel());
151
152 if (HasFlag(wxST_MARKUP))
153 ret = RemoveMarkup(ret);
154 return RemoveMnemonics(ret);
155}
156
157void wxStaticTextBase::SetLabelText(const wxString& text)
158{
159 wxString str = text;
160
161 if (HasFlag(wxST_MARKUP))
162 str = EscapeMarkup(str); // escapes markup and the & characters (which are also mnemonics)
163 else
164 str = EscapeMnemonics(text); // escape only the mnemonics
165 SetLabel(str);
166}
167
168/* static */
169wxString wxStaticTextBase::GetLabelText(const wxString& label)
170{
171 wxString ret = RemoveMarkup(label);
172 // always remove the markup (this function is static
173 // and cannot check for wxST_MARKUP presence/absence)
174
175 return RemoveMnemonics(ret);
176}
177
178/* static */
179wxString wxStaticTextBase::RemoveMarkup(const wxString& text)
180{
181 // strip out of "text" the markup for platforms which don't support it natively
182 bool inside_tag = false;
183
184 wxString label;
185 for ( wxString::const_iterator source = text.begin();
186 source != text.end(); ++source )
187 {
188 switch ( (*source).GetValue() )
189 {
190 case wxT('<'):
191 if (inside_tag)
192 {
193 wxLogDebug(wxT("Invalid markup !"));
194 return wxEmptyString;
195 }
196 inside_tag = true;
197 break;
198
199 case wxT('>'):
200 if (!inside_tag)
201 {
202 wxLogDebug(wxT("Invalid markup !"));
203 return wxEmptyString;
204 }
205 inside_tag = false;
206 break;
207
208 case wxT('&'):
209 {
210 if ( source+1 == text.end() )
211 {
212 wxLogDebug(wxT("Cannot use & as last character of the string '%s'"),
213 text.c_str());
214 return wxEmptyString;
215 }
216
217 // is this ampersand introducing a mnemonic or rather an entity?
218 bool isMnemonic = true;
219 size_t distanceFromEnd = text.end() - source;
220 for (size_t j=0; j < wxMARKUP_ENTITY_MAX; j++)
221 {
222 const wxChar *entity = wxMarkupEntities[wxMARKUP_ELEMENT_NAME][j];
223 size_t entityLen = wxStrlen(entity);
224
225 if (distanceFromEnd >= entityLen &&
226 wxString(source, source + entityLen) == entity)
227 {
228 // replace the &entity; string with the entity reference
229 label << wxMarkupEntities[wxMARKUP_ELEMENT_VALUE][j];
230 // little exception: when the entity reference is
231 // "&" (i.e. when entity is "&amp;"), substitute it
232 // with && instead of a single ampersand:
233 if (*wxMarkupEntities[wxMARKUP_ELEMENT_VALUE][j] == wxT('&'))
234 label << wxT('&');
235 // the -1 is because main for() loop already
236 // increments i:
237 source += entityLen - 1;
238 isMnemonic = false;
239 break;
240 }
241 }
242
243 if (isMnemonic)
244 label << *source;
245 }
246 break;
247
248
249 default:
250 if (!inside_tag)
251 label << *source;
252 }
253 }
254
255 return label;
256}
257
258/* static */
259wxString wxStaticTextBase::EscapeMarkup(const wxString& text)
260{
261 wxString ret;
262
263 for (wxString::const_iterator source = text.begin();
264 source != text.end(); ++source)
265 {
266 bool isEntity = false;
267
268 // search in the list of the entities and eventually escape this character
269 for (size_t j=0; j < wxMARKUP_ENTITY_MAX; j++)
270 {
271 if (*source == *wxMarkupEntities[wxMARKUP_ELEMENT_VALUE][j])
272 {
273 ret << wxMarkupEntities[wxMARKUP_ELEMENT_NAME][j];
274 isEntity = true;
275 break;
276 }
277 }
278
279 if (!isEntity)
280 ret << *source; // this character does not need to be escaped
281 }
282
283 return ret;
284}
285
286
287// ----------------------------------------------------------------------------
288// wxStaticTextBase - generic implementation for wxST_ELLIPSIZE_* support
289// ----------------------------------------------------------------------------
290
291void wxStaticTextBase::UpdateLabel()
292{
293 if (!IsEllipsized())
294 return;
295
296 wxString newlabel = GetEllipsizedLabelWithoutMarkup();
297
298 // we need to touch the "real" label (i.e. the text set inside the control,
299 // using port-specific functions) instead of the string returned by GetLabel().
300 //
301 // In fact, we must be careful not to touch the original label passed to
302 // SetLabel() otherwise GetLabel() will behave in a strange way to the user
303 // (e.g. returning a "Ver...ing" instead of "Very long string") !
304 if (newlabel == DoGetLabel())
305 return;
306 DoSetLabel(newlabel);
307}
308
309wxString wxStaticTextBase::GetLabelWithoutMarkup() const
310{
311 wxString ret(m_labelOrig);
312
313 if (HasFlag(wxST_MARKUP))
314 ret = RemoveMarkup(ret);
315
316 // unlike GetLabelText() we don't remove the mnemonics here!
317 return ret;
318}
319
320wxString wxStaticTextBase::GetEllipsizedLabelWithoutMarkup() const
321{
322 // this function should be used only by ports which do not support
323 // ellipsis in static texts: we first remove markup (which cannot
324 // be handled safely by Ellipsize()) and then ellipsize the result.
325
326 wxString ret(m_labelOrig);
327
328 // the order of the following two blocks is important!
329
330 if (HasFlag(wxST_MARKUP))
331 ret = RemoveMarkup(ret);
332
333 if (IsEllipsized())
334 ret = Ellipsize(ret);
335
336 return ret;
337}
338
339wxString wxStaticTextBase::Ellipsize(const wxString& label) const
340{
341 wxSize sz(GetSize());
342 if (sz.GetWidth() < 2 || sz.GetHeight() < 2)
343 {
344 // the size of this window is not valid (yet)
345 return label;
346 }
347
348 wxClientDC dc(const_cast<wxStaticTextBase*>(this));
349 dc.SetFont(GetFont());
350
351 wxEllipsizeMode mode;
352 if ( HasFlag(wxST_ELLIPSIZE_START) )
353 mode = wxELLIPSIZE_START;
354 else if ( HasFlag(wxST_ELLIPSIZE_MIDDLE) )
355 mode = wxELLIPSIZE_MIDDLE;
356 else if ( HasFlag(wxST_ELLIPSIZE_END) )
357 mode = wxELLIPSIZE_END;
358 else
359 {
360 wxFAIL_MSG( "should only be called if have one of wxST_ELLIPSIZE_XXX" );
361
362 return label;
363 }
364
365 return wxControl::Ellipsize(label, dc, mode, sz.GetWidth());
366}
367
368#endif // wxUSE_STATTEXT