]> git.saurik.com Git - wxWidgets.git/blob - src/common/stattextcmn.cpp
Add wxFont::Underlined() and MakeUnderlined() methods.
[wxWidgets.git] / 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)
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 #if wxUSE_STATTEXT
28
29 #ifndef WX_PRECOMP
30 #include "wx/stattext.h"
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/sizer.h"
37 #include "wx/containr.h"
38 #endif
39
40 #include "wx/textwrapper.h"
41
42 #include "wx/private/markupparser.h"
43
44 extern WXDLLEXPORT_DATA(const char) wxStaticTextNameStr[] = "staticText";
45
46 // ----------------------------------------------------------------------------
47 // XTI
48 // ----------------------------------------------------------------------------
49
50 wxDEFINE_FLAGS( wxStaticTextStyle )
51 wxBEGIN_FLAGS( wxStaticTextStyle )
52 // new style border flags, we put them first to
53 // use them for streaming out
54 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
55 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
56 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
57 wxFLAGS_MEMBER(wxBORDER_RAISED)
58 wxFLAGS_MEMBER(wxBORDER_STATIC)
59 wxFLAGS_MEMBER(wxBORDER_NONE)
60
61 // old style border flags
62 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
63 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
64 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
65 wxFLAGS_MEMBER(wxRAISED_BORDER)
66 wxFLAGS_MEMBER(wxSTATIC_BORDER)
67 wxFLAGS_MEMBER(wxBORDER)
68
69 // standard window styles
70 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
71 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
72 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
73 wxFLAGS_MEMBER(wxWANTS_CHARS)
74 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
75 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
76 wxFLAGS_MEMBER(wxVSCROLL)
77 wxFLAGS_MEMBER(wxHSCROLL)
78
79 wxFLAGS_MEMBER(wxST_NO_AUTORESIZE)
80 wxFLAGS_MEMBER(wxALIGN_LEFT)
81 wxFLAGS_MEMBER(wxALIGN_RIGHT)
82 wxFLAGS_MEMBER(wxALIGN_CENTRE)
83 wxEND_FLAGS( wxStaticTextStyle )
84
85 wxIMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticText, wxControl, "wx/stattext.h")
86
87 wxBEGIN_PROPERTIES_TABLE(wxStaticText)
88 wxPROPERTY( Label,wxString, SetLabel, GetLabel, wxString(), 0 /*flags*/, \
89 wxT("Helpstring"), wxT("group"))
90 wxPROPERTY_FLAGS( WindowStyle, wxStaticTextStyle, long, SetWindowStyleFlag, \
91 GetWindowStyleFlag, wxEMPTY_PARAMETER_VALUE, 0 /*flags*/, \
92 wxT("Helpstring"), wxT("group")) // style
93 wxEND_PROPERTIES_TABLE()
94
95 wxEMPTY_HANDLERS_TABLE(wxStaticText)
96
97 wxCONSTRUCTOR_6( wxStaticText, wxWindow*, Parent, wxWindowID, Id, \
98 wxString, Label, wxPoint, Position, wxSize, Size, long, WindowStyle )
99
100
101 // ----------------------------------------------------------------------------
102 // wxTextWrapper
103 // ----------------------------------------------------------------------------
104
105 void wxTextWrapper::Wrap(wxWindow *win, const wxString& text, int widthMax)
106 {
107 wxString line;
108
109 wxString::const_iterator lastSpace = text.end();
110 wxString::const_iterator lineStart = text.begin();
111 for ( wxString::const_iterator p = lineStart; ; ++p )
112 {
113 if ( IsStartOfNewLine() )
114 {
115 OnNewLine();
116
117 lastSpace = text.end();
118 line.clear();
119 lineStart = p;
120 }
121
122 if ( p == text.end() || *p == wxT('\n') )
123 {
124 DoOutputLine(line);
125
126 if ( p == text.end() )
127 break;
128 }
129 else // not EOL
130 {
131 if ( *p == wxT(' ') )
132 lastSpace = p;
133
134 line += *p;
135
136 if ( widthMax >= 0 && lastSpace != text.end() )
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
162 class wxLabelWrapper : public wxTextWrapper
163 {
164 public:
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
172 protected:
173 virtual void OnOutputLine(const wxString& line)
174 {
175 m_text += line;
176 }
177
178 virtual void OnNewLine()
179 {
180 m_text += wxT('\n');
181 }
182
183 private:
184 wxString m_text;
185 };
186
187
188 // ----------------------------------------------------------------------------
189 // wxStaticTextBase
190 // ----------------------------------------------------------------------------
191
192 void wxStaticTextBase::Wrap(int width)
193 {
194 wxLabelWrapper wrapper;
195 wrapper.WrapLabel(this, width);
196 }
197
198 wxString wxStaticTextBase::GetLabelText() const
199 {
200 wxString ret(GetLabel());
201
202 if (HasFlag(wxST_MARKUP))
203 ret = RemoveMarkup(ret);
204 return RemoveMnemonics(ret);
205 }
206
207 void 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 */
219 wxString wxStaticTextBase::GetLabelText(const wxString& label)
220 {
221 wxString ret = RemoveMarkup(label);
222 // always remove the markup (this function is static
223 // and cannot check for wxST_MARKUP presence/absence)
224
225 return RemoveMnemonics(ret);
226 }
227
228 /* static */
229 wxString wxStaticTextBase::RemoveMarkup(const wxString& text)
230 {
231 return wxMarkupParser::Strip(text);
232 }
233
234 /* static */
235 wxString wxStaticTextBase::EscapeMarkup(const wxString& text)
236 {
237 return wxMarkupParser::Quote(text);
238 }
239
240
241 // ----------------------------------------------------------------------------
242 // wxStaticTextBase - generic implementation for wxST_ELLIPSIZE_* support
243 // ----------------------------------------------------------------------------
244
245 void 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
263 wxString 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
274 wxString 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
293 wxString 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;
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 }
318
319 return wxControl::Ellipsize(label, dc, mode, sz.GetWidth());
320 }
321
322 #endif // wxUSE_STATTEXT