]>
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"
40 const wxChar
*wxMarkupEntities
[][wxMARKUP_ENTITY_MAX
] =
42 // the entities handled by SetLabel() when wxST_MARKUP is used and their referenced string
44 { wxT("&"), wxT("<"), wxT(">"), wxT("'"), wxT(""") },
45 { wxT("&"), wxT("<"), wxT(">"), wxT("'"), wxT("\"") }
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 void wxTextWrapper::Wrap(wxWindow
*win
, const wxString
& text
, int widthMax
)
58 wxString::const_iterator lastSpace
= text
.end();
59 wxString::const_iterator lineStart
= text
.begin();
60 for ( wxString::const_iterator p
= lineStart
; ; ++p
)
62 if ( IsStartOfNewLine() )
66 lastSpace
= text
.end();
71 if ( p
== text
.end() || *p
== _T('\n') )
75 if ( p
== text
.end() )
85 if ( widthMax
>= 0 && lastSpace
!= text
.end() )
88 win
->GetTextExtent(line
, &width
, NULL
);
90 if ( width
> widthMax
)
92 // remove the last word from this line
93 line
.erase(lastSpace
- lineStart
, p
+ 1 - lineStart
);
96 // go back to the last word of this line which we didn't
101 //else: no wrapping at all or impossible to wrap
107 // ----------------------------------------------------------------------------
108 // wxLabelWrapper: helper class for wxStaticTextBase::Wrap()
109 // ----------------------------------------------------------------------------
111 class wxLabelWrapper
: public wxTextWrapper
114 void WrapLabel(wxWindow
*text
, int widthMax
)
117 Wrap(text
, text
->GetLabel(), widthMax
);
118 text
->SetLabel(m_text
);
122 virtual void OnOutputLine(const wxString
& line
)
127 virtual void OnNewLine()
137 // ----------------------------------------------------------------------------
139 // ----------------------------------------------------------------------------
141 void wxStaticTextBase::Wrap(int width
)
143 wxLabelWrapper wrapper
;
144 wrapper
.WrapLabel(this, width
);
147 wxString
wxStaticTextBase::GetLabelText() const
149 wxString
ret(GetLabel());
151 if (HasFlag(wxST_MARKUP
))
152 ret
= RemoveMarkup(ret
);
153 return RemoveMnemonics(ret
);
157 wxString
wxStaticTextBase::GetLabelText(const wxString
& label
)
160 wxString ret
= RemoveMarkup(label
);
161 return RemoveMnemonics(ret
);
165 wxString
wxStaticTextBase::RemoveMarkup(const wxString
& text
)
167 // strip out of "text" the markup for platforms which don't support it natively
168 bool inside_tag
= false;
171 for ( wxString::const_iterator source
= text
.begin();
172 source
!= text
.end(); ++source
)
174 switch ( (*source
).GetValue() )
179 wxLogDebug(wxT("Invalid markup !"));
180 return wxEmptyString
;
188 wxLogDebug(wxT("Invalid markup !"));
189 return wxEmptyString
;
196 if ( source
+1 == text
.end() )
198 wxLogDebug(wxT("Cannot use & as last character of the string '%s'"),
200 return wxEmptyString
;
203 // is this ampersand introducing a mnemonic or rather an entity?
204 bool isMnemonic
= true;
205 size_t distanceFromEnd
= text
.end() - source
;
206 for (size_t j
=0; j
< wxMARKUP_ENTITY_MAX
; j
++)
208 const wxChar
*entity
= wxMarkupEntities
[wxMARKUP_ELEMENT_NAME
][j
];
209 size_t entityLen
= wxStrlen(entity
);
211 if (distanceFromEnd
>= entityLen
&&
212 wxString(source
, source
+ entityLen
) == entity
)
214 // replace the &entity; string with the entity reference
215 label
<< wxMarkupEntities
[wxMARKUP_ELEMENT_VALUE
][j
];
216 // little exception: when the entity reference is
217 // "&" (i.e. when entity is "&"), substitute it
218 // with && instead of a single ampersand:
219 if (*wxMarkupEntities
[wxMARKUP_ELEMENT_VALUE
][j
] == wxT('&'))
221 // the -1 is because main for() loop already
223 source
+= entityLen
- 1;
245 wxString
wxStaticTextBase::EscapeMarkup(const wxString
& text
)
249 for (wxString::const_iterator source
= text
.begin();
250 source
!= text
.end(); ++source
)
252 bool isEntity
= false;
254 // search in the list of the entities and eventually escape this character
255 for (size_t j
=0; j
< wxMARKUP_ENTITY_MAX
; j
++)
257 if (*source
== *wxMarkupEntities
[wxMARKUP_ELEMENT_VALUE
][j
])
259 ret
<< wxMarkupEntities
[wxMARKUP_ELEMENT_NAME
][j
];
266 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 #define wxELLIPSE_REPLACEMENT wxT("...")
317 wxString
wxStaticTextBase::Ellipsize(const wxString
& label
) const
319 wxSize
sz(GetSize());
320 if (sz
.GetWidth() < 2 || sz
.GetHeight() < 2)
322 // the size of this window is not valid (yet)
326 wxClientDC
dc(wx_const_cast(wxStaticTextBase
*, this));
327 dc
.SetFont(GetFont());
329 wxArrayInt charOffsets
;
332 // these cannot be cached as they can change because of e.g. a font change
333 int replacementWidth
= dc
.GetTextExtent(wxELLIPSE_REPLACEMENT
).GetWidth();
334 int marginWidth
= dc
.GetCharWidth()*2;
336 // handle correctly labels with newlines
340 for ( wxString::const_iterator pc
= label
.begin(); ; ++pc
)
342 if ( pc
== label
.end() || *pc
== _T('\n') )
344 len
= curLine
.length();
346 dc
.GetPartialTextExtents(curLine
, charOffsets
))
348 wxASSERT(charOffsets
.GetCount() == len
);
350 size_t totalWidth
= charOffsets
.Last();
351 if ( totalWidth
> (size_t)sz
.GetWidth() )
353 // we need to ellipsize this row
354 int excessPixels
= totalWidth
- sz
.GetWidth() +
356 marginWidth
; // security margin (NEEDED!)
358 // remove characters in excess
359 size_t initialChar
, // index of first char to erase
360 nChars
; // how many chars do we need to erase?
361 if (HasFlag(wxST_ELLIPSIZE_START
))
365 nChars
< len
&& charOffsets
[nChars
] < excessPixels
;
369 else if (HasFlag(wxST_ELLIPSIZE_MIDDLE
))
371 // the start & end of the removed span of chars
373 size_t endChar
= len
/2;
376 for ( ; removed
< excessPixels
; )
380 // width of the initialChar-th character
381 int width
= charOffsets
[initialChar
] -
382 charOffsets
[initialChar
-1];
384 // remove the initialChar-th character
389 if (endChar
< len
- 1 &&
390 removed
< excessPixels
)
392 // width of the (endChar+1)-th character
393 int width
= charOffsets
[endChar
+1] -
394 charOffsets
[endChar
];
396 // remove the endChar-th character
401 if (initialChar
== 0 && endChar
== len
-1)
409 nChars
= endChar
- initialChar
+ 1;
413 wxASSERT(HasFlag(wxST_ELLIPSIZE_END
));
416 int maxWidth
= totalWidth
- excessPixels
;
419 charOffsets
[initialChar
] < maxWidth
;
423 if (initialChar
== 0)
429 initialChar
--; // go back one character
430 nChars
= len
- initialChar
;
436 // need to remove the entire row!
441 // erase nChars characters after initialChar (included):
442 curLine
.erase(initialChar
, nChars
+1);
444 // if there is space for the replacement dots, add them
445 if (sz
.GetWidth() > replacementWidth
)
446 curLine
.insert(initialChar
, wxELLIPSE_REPLACEMENT
);
449 // if everything was ok, we should have shortened this line
450 // enough to make it fit in sz.GetWidth():
451 wxASSERT(dc
.GetTextExtent(curLine
).GetWidth() < sz
.GetWidth());
455 // add this (ellipsized) row to the rest of the label
457 if ( pc
== label
.end() )
467 // we need to remove mnemonics from the label for correct calculations
468 else if ( *pc
== _T('&') )
470 // pc+1 is safe: at worst we'll be at end()
471 wxString::const_iterator next
= pc
+ 1;
472 if ( next
!= label
.end() && *next
== _T('&') )
473 curLine
+= _T('&'); // && becomes &
474 //else: remove this ampersand
476 // we need also to expand tabs to properly calc their size
477 else if ( *pc
== _T('\t') )
479 // Windows natively expands the TABs to 6 spaces. Do the same:
491 #endif // wxUSE_STATTEXT