]> git.saurik.com Git - wxWidgets.git/blame - src/common/stattextcmn.cpp
Add wxFont::Underlined() and MakeUnderlined() methods.
[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
f313deaa
PC
27#if wxUSE_STATTEXT
28
39bc0347 29#ifndef WX_PRECOMP
f313deaa 30 #include "wx/stattext.h"
39bc0347
VZ
31 #include "wx/button.h"
32 #include "wx/dcclient.h"
33 #include "wx/intl.h"
523b9ce4 34 #include "wx/log.h"
39bc0347 35 #include "wx/settings.h"
39bc0347
VZ
36 #include "wx/sizer.h"
37 #include "wx/containr.h"
38#endif
39
f313deaa 40#include "wx/textwrapper.h"
39bc0347 41
0d0fdaac
VZ
42#include "wx/private/markupparser.h"
43
f313deaa 44extern WXDLLEXPORT_DATA(const char) wxStaticTextNameStr[] = "staticText";
39bc0347 45
28953245
SC
46// ----------------------------------------------------------------------------
47// XTI
48// ----------------------------------------------------------------------------
49
50wxDEFINE_FLAGS( wxStaticTextStyle )
51wxBEGIN_FLAGS( wxStaticTextStyle )
52// new style border flags, we put them first to
53// use them for streaming out
54wxFLAGS_MEMBER(wxBORDER_SIMPLE)
55wxFLAGS_MEMBER(wxBORDER_SUNKEN)
56wxFLAGS_MEMBER(wxBORDER_DOUBLE)
57wxFLAGS_MEMBER(wxBORDER_RAISED)
58wxFLAGS_MEMBER(wxBORDER_STATIC)
59wxFLAGS_MEMBER(wxBORDER_NONE)
60
61// old style border flags
62wxFLAGS_MEMBER(wxSIMPLE_BORDER)
63wxFLAGS_MEMBER(wxSUNKEN_BORDER)
64wxFLAGS_MEMBER(wxDOUBLE_BORDER)
65wxFLAGS_MEMBER(wxRAISED_BORDER)
66wxFLAGS_MEMBER(wxSTATIC_BORDER)
67wxFLAGS_MEMBER(wxBORDER)
68
69// standard window styles
70wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
71wxFLAGS_MEMBER(wxCLIP_CHILDREN)
72wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
73wxFLAGS_MEMBER(wxWANTS_CHARS)
74wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
75wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
76wxFLAGS_MEMBER(wxVSCROLL)
77wxFLAGS_MEMBER(wxHSCROLL)
78
79wxFLAGS_MEMBER(wxST_NO_AUTORESIZE)
80wxFLAGS_MEMBER(wxALIGN_LEFT)
81wxFLAGS_MEMBER(wxALIGN_RIGHT)
82wxFLAGS_MEMBER(wxALIGN_CENTRE)
83wxEND_FLAGS( wxStaticTextStyle )
84
85wxIMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticText, wxControl, "wx/stattext.h")
86
87wxBEGIN_PROPERTIES_TABLE(wxStaticText)
88wxPROPERTY( Label,wxString, SetLabel, GetLabel, wxString(), 0 /*flags*/, \
89 wxT("Helpstring"), wxT("group"))
90wxPROPERTY_FLAGS( WindowStyle, wxStaticTextStyle, long, SetWindowStyleFlag, \
91 GetWindowStyleFlag, wxEMPTY_PARAMETER_VALUE, 0 /*flags*/, \
92 wxT("Helpstring"), wxT("group")) // style
93wxEND_PROPERTIES_TABLE()
94
95wxEMPTY_HANDLERS_TABLE(wxStaticText)
96
97wxCONSTRUCTOR_6( wxStaticText, wxWindow*, Parent, wxWindowID, Id, \
98 wxString, Label, wxPoint, Position, wxSize, Size, long, WindowStyle )
99
100
39bc0347
VZ
101// ----------------------------------------------------------------------------
102// wxTextWrapper
103// ----------------------------------------------------------------------------
104
105void wxTextWrapper::Wrap(wxWindow *win, const wxString& text, int widthMax)
106{
39bc0347
VZ
107 wxString line;
108
5b871700
VS
109 wxString::const_iterator lastSpace = text.end();
110 wxString::const_iterator lineStart = text.begin();
111 for ( wxString::const_iterator p = lineStart; ; ++p )
39bc0347
VZ
112 {
113 if ( IsStartOfNewLine() )
114 {
115 OnNewLine();
116
5b871700 117 lastSpace = text.end();
39bc0347
VZ
118 line.clear();
119 lineStart = p;
120 }
121
9a83f860 122 if ( p == text.end() || *p == wxT('\n') )
39bc0347
VZ
123 {
124 DoOutputLine(line);
125
5b871700 126 if ( p == text.end() )
39bc0347
VZ
127 break;
128 }
129 else // not EOL
130 {
9a83f860 131 if ( *p == wxT(' ') )
39bc0347
VZ
132 lastSpace = p;
133
134 line += *p;
135
5b871700 136 if ( widthMax >= 0 && lastSpace != text.end() )
39bc0347
VZ
137 {
138 int width;
139 win->GetTextExtent(line, &width, NULL);
140
141 if ( width > widthMax )
142 {
143 // remove the last word from this line
144 line.erase(lastSpace - lineStart, p + 1 - lineStart);
145 DoOutputLine(line);
146
147 // go back to the last word of this line which we didn't
148 // output yet
149 p = lastSpace;
150 }
151 }
152 //else: no wrapping at all or impossible to wrap
153 }
154 }
155}
156
157
158// ----------------------------------------------------------------------------
159// wxLabelWrapper: helper class for wxStaticTextBase::Wrap()
160// ----------------------------------------------------------------------------
161
162class wxLabelWrapper : public wxTextWrapper
163{
164public:
165 void WrapLabel(wxWindow *text, int widthMax)
166 {
167 m_text.clear();
168 Wrap(text, text->GetLabel(), widthMax);
169 text->SetLabel(m_text);
170 }
171
172protected:
173 virtual void OnOutputLine(const wxString& line)
174 {
175 m_text += line;
176 }
177
178 virtual void OnNewLine()
179 {
9a83f860 180 m_text += wxT('\n');
39bc0347
VZ
181 }
182
183private:
184 wxString m_text;
185};
186
187
188// ----------------------------------------------------------------------------
189// wxStaticTextBase
190// ----------------------------------------------------------------------------
191
192void wxStaticTextBase::Wrap(int width)
193{
194 wxLabelWrapper wrapper;
195 wrapper.WrapLabel(this, width);
196}
197
198wxString wxStaticTextBase::GetLabelText() const
199{
200 wxString ret(GetLabel());
201
202 if (HasFlag(wxST_MARKUP))
203 ret = RemoveMarkup(ret);
204 return RemoveMnemonics(ret);
205}
206
32ee98eb
FM
207void wxStaticTextBase::SetLabelText(const wxString& text)
208{
209 wxString str = text;
210
211 if (HasFlag(wxST_MARKUP))
212 str = EscapeMarkup(str); // escapes markup and the & characters (which are also mnemonics)
213 else
214 str = EscapeMnemonics(text); // escape only the mnemonics
215 SetLabel(str);
216}
217
218/* static */
19cf1ef3
FM
219wxString wxStaticTextBase::GetLabelText(const wxString& label)
220{
19cf1ef3 221 wxString ret = RemoveMarkup(label);
ce00f59b 222 // always remove the markup (this function is static
32ee98eb
FM
223 // and cannot check for wxST_MARKUP presence/absence)
224
19cf1ef3
FM
225 return RemoveMnemonics(ret);
226}
227
32ee98eb 228/* static */
39bc0347
VZ
229wxString wxStaticTextBase::RemoveMarkup(const wxString& text)
230{
0d0fdaac 231 return wxMarkupParser::Strip(text);
39bc0347
VZ
232}
233
234/* static */
235wxString wxStaticTextBase::EscapeMarkup(const wxString& text)
236{
0d0fdaac 237 return wxMarkupParser::Quote(text);
39bc0347
VZ
238}
239
4520d583
FM
240
241// ----------------------------------------------------------------------------
242// wxStaticTextBase - generic implementation for wxST_ELLIPSIZE_* support
243// ----------------------------------------------------------------------------
244
245void wxStaticTextBase::UpdateLabel()
246{
247 if (!IsEllipsized())
248 return;
249
250 wxString newlabel = GetEllipsizedLabelWithoutMarkup();
251
252 // we need to touch the "real" label (i.e. the text set inside the control,
253 // using port-specific functions) instead of the string returned by GetLabel().
254 //
255 // In fact, we must be careful not to touch the original label passed to
256 // SetLabel() otherwise GetLabel() will behave in a strange way to the user
257 // (e.g. returning a "Ver...ing" instead of "Very long string") !
258 if (newlabel == DoGetLabel())
259 return;
260 DoSetLabel(newlabel);
261}
262
32ee98eb
FM
263wxString wxStaticTextBase::GetLabelWithoutMarkup() const
264{
265 wxString ret(m_labelOrig);
266
267 if (HasFlag(wxST_MARKUP))
268 ret = RemoveMarkup(ret);
269
270 // unlike GetLabelText() we don't remove the mnemonics here!
271 return ret;
272}
273
4520d583
FM
274wxString wxStaticTextBase::GetEllipsizedLabelWithoutMarkup() const
275{
276 // this function should be used only by ports which do not support
277 // ellipsis in static texts: we first remove markup (which cannot
278 // be handled safely by Ellipsize()) and then ellipsize the result.
279
280 wxString ret(m_labelOrig);
281
282 // the order of the following two blocks is important!
283
284 if (HasFlag(wxST_MARKUP))
285 ret = RemoveMarkup(ret);
286
287 if (IsEllipsized())
288 ret = Ellipsize(ret);
289
290 return ret;
291}
292
293wxString wxStaticTextBase::Ellipsize(const wxString& label) const
294{
295 wxSize sz(GetSize());
296 if (sz.GetWidth() < 2 || sz.GetHeight() < 2)
297 {
298 // the size of this window is not valid (yet)
299 return label;
300 }
301
302 wxClientDC dc(const_cast<wxStaticTextBase*>(this));
303 dc.SetFont(GetFont());
304
305 wxEllipsizeMode mode;
41ce5eff
VZ
306 if ( HasFlag(wxST_ELLIPSIZE_START) )
307 mode = wxELLIPSIZE_START;
308 else if ( HasFlag(wxST_ELLIPSIZE_MIDDLE) )
309 mode = wxELLIPSIZE_MIDDLE;
310 else if ( HasFlag(wxST_ELLIPSIZE_END) )
311 mode = wxELLIPSIZE_END;
312 else
313 {
314 wxFAIL_MSG( "should only be called if have one of wxST_ELLIPSIZE_XXX" );
315
316 return label;
317 }
4520d583 318
5c87527c 319 return wxControl::Ellipsize(label, dc, mode, sz.GetWidth());
39bc0347
VZ
320}
321
322#endif // wxUSE_STATTEXT