]>
git.saurik.com Git - wxWidgets.git/blob - src/common/stattextcmn.cpp
e9a22fd8f505823c9088aabdd99fed18451d26b9
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)
7 // Copyright: (c) 1999-2006 Vadim Zeitlin
8 // (c) 2007 Francesco Montorsi
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #include "wx/textwrapper.h"
28 #include "wx/private/stattext.h"
31 #include "wx/button.h"
32 #include "wx/dcclient.h"
35 #include "wx/settings.h"
36 #include "wx/stattext.h"
38 #include "wx/containr.h"
41 const wxChar
*wxMarkupEntities
[][wxMARKUP_ENTITY_MAX
] =
43 // the entities handled by SetLabel() when wxST_MARKUP is used and their referenced string
45 { wxT("&"), wxT("<"), wxT(">"), wxT("'"), wxT(""") },
46 { wxT("&"), wxT("<"), wxT(">"), wxT("'"), wxT("\"") }
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 void wxTextWrapper::Wrap(wxWindow
*win
, const wxString
& text
, int widthMax
)
59 wxString::const_iterator lastSpace
= text
.end();
60 wxString::const_iterator lineStart
= text
.begin();
61 for ( wxString::const_iterator p
= lineStart
; ; ++p
)
63 if ( IsStartOfNewLine() )
67 lastSpace
= text
.end();
72 if ( p
== text
.end() || *p
== wxT('\n') )
76 if ( p
== text
.end() )
86 if ( widthMax
>= 0 && lastSpace
!= text
.end() )
89 win
->GetTextExtent(line
, &width
, NULL
);
91 if ( width
> widthMax
)
93 // remove the last word from this line
94 line
.erase(lastSpace
- lineStart
, p
+ 1 - lineStart
);
97 // go back to the last word of this line which we didn't
102 //else: no wrapping at all or impossible to wrap
108 // ----------------------------------------------------------------------------
109 // wxLabelWrapper: helper class for wxStaticTextBase::Wrap()
110 // ----------------------------------------------------------------------------
112 class wxLabelWrapper
: public wxTextWrapper
115 void WrapLabel(wxWindow
*text
, int widthMax
)
118 Wrap(text
, text
->GetLabel(), widthMax
);
119 text
->SetLabel(m_text
);
123 virtual void OnOutputLine(const wxString
& line
)
128 virtual void OnNewLine()
138 // ----------------------------------------------------------------------------
140 // ----------------------------------------------------------------------------
142 void wxStaticTextBase::Wrap(int width
)
144 wxLabelWrapper wrapper
;
145 wrapper
.WrapLabel(this, width
);
148 wxString
wxStaticTextBase::GetLabelText() const
150 wxString
ret(GetLabel());
152 if (HasFlag(wxST_MARKUP
))
153 ret
= RemoveMarkup(ret
);
154 return RemoveMnemonics(ret
);
158 wxString
wxStaticTextBase::GetLabelText(const wxString
& label
)
161 wxString ret
= RemoveMarkup(label
);
162 return RemoveMnemonics(ret
);
166 wxString
wxStaticTextBase::RemoveMarkup(const wxString
& text
)
168 // strip out of "text" the markup for platforms which don't support it natively
169 bool inside_tag
= false;
172 for ( wxString::const_iterator source
= text
.begin();
173 source
!= text
.end(); ++source
)
175 switch ( (*source
).GetValue() )
180 wxLogDebug(wxT("Invalid markup !"));
181 return wxEmptyString
;
189 wxLogDebug(wxT("Invalid markup !"));
190 return wxEmptyString
;
197 if ( source
+1 == text
.end() )
199 wxLogDebug(wxT("Cannot use & as last character of the string '%s'"),
201 return wxEmptyString
;
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
++)
209 const wxChar
*entity
= wxMarkupEntities
[wxMARKUP_ELEMENT_NAME
][j
];
210 size_t entityLen
= wxStrlen(entity
);
212 if (distanceFromEnd
>= entityLen
&&
213 wxString(source
, source
+ entityLen
) == entity
)
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 "&"), substitute it
219 // with && instead of a single ampersand:
220 if (*wxMarkupEntities
[wxMARKUP_ELEMENT_VALUE
][j
] == wxT('&'))
222 // the -1 is because main for() loop already
224 source
+= entityLen
- 1;
246 wxString
wxStaticTextBase::EscapeMarkup(const wxString
& text
)
250 for (wxString::const_iterator source
= text
.begin();
251 source
!= text
.end(); ++source
)
253 bool isEntity
= false;
255 // search in the list of the entities and eventually escape this character
256 for (size_t j
=0; j
< wxMARKUP_ENTITY_MAX
; j
++)
258 if (*source
== *wxMarkupEntities
[wxMARKUP_ELEMENT_VALUE
][j
])
260 ret
<< wxMarkupEntities
[wxMARKUP_ELEMENT_NAME
][j
];
267 ret
<< *source
; // this character does not need to be escaped
274 // ----------------------------------------------------------------------------
275 // wxStaticTextBase - generic implementation for wxST_ELLIPSIZE_* support
276 // ----------------------------------------------------------------------------
278 void wxStaticTextBase::UpdateLabel()
283 wxString newlabel
= GetEllipsizedLabelWithoutMarkup();
285 // we need to touch the "real" label (i.e. the text set inside the control,
286 // using port-specific functions) instead of the string returned by GetLabel().
288 // In fact, we must be careful not to touch the original label passed to
289 // SetLabel() otherwise GetLabel() will behave in a strange way to the user
290 // (e.g. returning a "Ver...ing" instead of "Very long string") !
291 if (newlabel
== DoGetLabel())
293 DoSetLabel(newlabel
);
296 wxString
wxStaticTextBase::GetEllipsizedLabelWithoutMarkup() const
298 // this function should be used only by ports which do not support
299 // ellipsis in static texts: we first remove markup (which cannot
300 // be handled safely by Ellipsize()) and then ellipsize the result.
302 wxString
ret(m_labelOrig
);
304 // the order of the following two blocks is important!
306 if (HasFlag(wxST_MARKUP
))
307 ret
= RemoveMarkup(ret
);
310 ret
= Ellipsize(ret
);
315 wxString
wxStaticTextBase::Ellipsize(const wxString
& label
) const
317 wxSize
sz(GetSize());
318 if (sz
.GetWidth() < 2 || sz
.GetHeight() < 2)
320 // the size of this window is not valid (yet)
324 wxClientDC
dc(const_cast<wxStaticTextBase
*>(this));
325 dc
.SetFont(GetFont());
327 wxEllipsizeMode mode
;
328 if ( HasFlag(wxST_ELLIPSIZE_START
) )
329 mode
= wxELLIPSIZE_START
;
330 else if ( HasFlag(wxST_ELLIPSIZE_MIDDLE
) )
331 mode
= wxELLIPSIZE_MIDDLE
;
332 else if ( HasFlag(wxST_ELLIPSIZE_END
) )
333 mode
= wxELLIPSIZE_END
;
336 wxFAIL_MSG( "should only be called if have one of wxST_ELLIPSIZE_XXX" );
341 return wxControl::Ellipsize(label
, dc
, mode
, sz
.GetWidth());
344 #endif // wxUSE_STATTEXT