]>
git.saurik.com Git - wxWidgets.git/blob - 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)
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/private/stattext.h"
30 #include "wx/button.h"
31 #include "wx/dcclient.h"
34 #include "wx/settings.h"
35 #include "wx/stattext.h"
37 #include "wx/containr.h"
42 const wxChar
*wxMarkupEntities
[][wxMARKUP_ENTITY_MAX
] =
44 // the entities handled by SetLabel() when wxST_MARKUP is used and their referenced string
46 { wxT("&"), wxT("<"), wxT(">"), wxT("'"), wxT(""") },
47 { 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
== _T('\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::RemoveMarkup(const wxString
& text
)
160 // strip out of "text" the markup for platforms which don't support it natively
161 bool inside_tag
= false;
164 for ( wxString::const_iterator source
= text
.begin();
165 source
!= text
.end(); ++source
)
167 switch ( (*source
).GetValue() )
172 wxLogDebug(wxT("Invalid markup !"));
173 return wxEmptyString
;
181 wxLogDebug(wxT("Invalid markup !"));
182 return wxEmptyString
;
189 if ( source
+1 == text
.end() )
191 wxLogDebug(wxT("Cannot use & as last character of the string '%s'"),
193 return wxEmptyString
;
196 // is this ampersand introducing a mnemonic or rather an entity?
197 bool isMnemonic
= true;
198 size_t distanceFromEnd
= text
.end() - source
;
199 for (size_t j
=0; j
< wxMARKUP_ENTITY_MAX
; j
++)
201 const wxChar
*entity
= wxMarkupEntities
[wxMARKUP_ELEMENT_NAME
][j
];
202 size_t entityLen
= wxStrlen(entity
);
204 if (distanceFromEnd
>= entityLen
&&
205 wxString(source
, source
+ entityLen
) == entity
)
207 // replace the &entity; string with the entity reference
208 label
<< wxMarkupEntities
[wxMARKUP_ELEMENT_VALUE
][j
];
209 // little exception: when the entity reference is
210 // "&" (i.e. when entity is "&"), substitute it
211 // with && instead of a single ampersand:
212 if (*wxMarkupEntities
[wxMARKUP_ELEMENT_VALUE
][j
] == wxT('&'))
214 // the -1 is because main for() loop already
216 source
+= entityLen
- 1;
238 wxString
wxStaticTextBase::EscapeMarkup(const wxString
& text
)
242 for (wxString::const_iterator source
= text
.begin();
243 source
!= text
.end(); ++source
)
245 bool isEntity
= false;
247 // search in the list of the entities and eventually escape this character
248 for (size_t j
=0; j
< wxMARKUP_ENTITY_MAX
; j
++)
250 if (*source
== *wxMarkupEntities
[wxMARKUP_ELEMENT_VALUE
][j
])
252 ret
<< wxMarkupEntities
[wxMARKUP_ELEMENT_NAME
][j
];
259 ret
<< *source
; // this character does not need to be escaped
267 // ----------------------------------------------------------------------------
268 // wxStaticTextBase - generic implementation for wxST_ELLIPSIZE_* support
269 // ----------------------------------------------------------------------------
271 void wxStaticTextBase::UpdateLabel()
276 wxString newlabel
= GetEllipsizedLabelWithoutMarkup();
278 // we need to touch the "real" label (i.e. the text set inside the control,
279 // using port-specific functions) instead of the string returned by GetLabel().
281 // In fact, we must be careful not to touch the original label passed to
282 // SetLabel() otherwise GetLabel() will behave in a strange way to the user
283 // (e.g. returning a "Ver...ing" instead of "Very long string") !
284 if (newlabel
== DoGetLabel())
286 DoSetLabel(newlabel
);
289 wxString
wxStaticTextBase::GetEllipsizedLabelWithoutMarkup() const
291 // this function should be used only by ports which do not support
292 // ellipsis in static texts: we first remove markup (which cannot
293 // be handled safely by Ellipsize()) and then ellipsize the result.
295 wxString
ret(m_labelOrig
);
297 // the order of the following two blocks is important!
299 if (HasFlag(wxST_MARKUP
))
300 ret
= RemoveMarkup(ret
);
303 ret
= Ellipsize(ret
);
308 #define wxELLIPSE_REPLACEMENT wxT("...")
310 wxString
wxStaticTextBase::Ellipsize(const wxString
& label
) const
312 wxSize
sz(GetSize());
313 if (sz
.GetWidth() < 2 || sz
.GetHeight() < 2)
315 // the size of this window is not valid (yet)
319 wxClientDC
dc(wx_const_cast(wxStaticTextBase
*, this));
320 dc
.SetFont(GetFont());
322 wxArrayInt charOffsets
;
325 // these cannot be cached as they can change because of e.g. a font change
326 int replacementWidth
= dc
.GetTextExtent(wxELLIPSE_REPLACEMENT
).GetWidth();
327 int marginWidth
= dc
.GetCharWidth()*2;
329 // handle correctly labels with newlines
333 for ( wxString::const_iterator pc
= label
.begin(); ; ++pc
)
335 if ( pc
== label
.end() || *pc
== _T('\n') )
337 len
= curLine
.length();
339 dc
.GetPartialTextExtents(curLine
, charOffsets
))
341 wxASSERT(charOffsets
.GetCount() == len
);
343 size_t totalWidth
= charOffsets
.Last();
344 if ( totalWidth
> (size_t)sz
.GetWidth() )
346 // we need to ellipsize this row
347 int excessPixels
= totalWidth
- sz
.GetWidth() +
349 marginWidth
; // security margin (NEEDED!)
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
))
358 nChars
< len
&& charOffsets
[nChars
] < excessPixels
;
362 else if (HasFlag(wxST_ELLIPSIZE_MIDDLE
))
364 // the start & end of the removed span of chars
366 size_t endChar
= len
/2;
369 for ( ; removed
< excessPixels
; )
373 // width of the initialChar-th character
374 int width
= charOffsets
[initialChar
] -
375 charOffsets
[initialChar
-1];
377 // remove the initialChar-th character
382 if (endChar
< len
- 1 &&
383 removed
< excessPixels
)
385 // width of the (endChar+1)-th character
386 int width
= charOffsets
[endChar
+1] -
387 charOffsets
[endChar
];
389 // remove the endChar-th character
394 if (initialChar
== 0 && endChar
== len
-1)
402 nChars
= endChar
- initialChar
+ 1;
406 wxASSERT(HasFlag(wxST_ELLIPSIZE_END
));
409 int maxWidth
= totalWidth
- excessPixels
;
412 charOffsets
[initialChar
] < maxWidth
;
416 if (initialChar
== 0)
422 initialChar
--; // go back one character
423 nChars
= len
- initialChar
;
429 // need to remove the entire row!
434 // erase nChars characters after initialChar (included):
435 curLine
.erase(initialChar
, nChars
+1);
437 // if there is space for the replacement dots, add them
438 if (sz
.GetWidth() > replacementWidth
)
439 curLine
.insert(initialChar
, wxELLIPSE_REPLACEMENT
);
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());
448 // add this (ellipsized) row to the rest of the label
450 if ( pc
== label
.end() )
460 // we need to remove mnemonics from the label for correct calculations
461 else if ( *pc
== _T('&') )
463 // pc+1 is safe: at worst we'll be at end()
464 wxString::const_iterator next
= pc
+ 1;
465 if ( next
!= label
.end() && *next
== _T('&') )
466 curLine
+= _T('&'); // && becomes &
467 //else: remove this ampersand
469 // we need also to expand tabs to properly calc their size
470 else if ( *pc
== _T('\t') )
472 // Windows natively expands the TABs to 6 spaces. Do the same:
484 #endif // wxUSE_STATTEXT