refactor wxStaticText::Ellipsize so it's a static public utility function; document...
[wxWidgets.git] / interface / wx / stattext.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: stattext.h
3 // Purpose: interface of wxStaticText
4 // Author: wxWidgets team
5 // RCS-ID: $Id$
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
8
9 /**
10 The different ellipsization modes supported by wxStaticText and by
11 wxStaticText::Ellipsize function.
12
13 @note
14 The members of this enum are used both as window styles for wxStaticText
15 and both as enumeration values for wxStaticText::Ellipsize static function.
16 */
17 enum wxEllipsizeMode
18 {
19 wxST_ELLIPSIZE_START = 0x0004,
20 wxST_ELLIPSIZE_MIDDLE = 0x0008,
21 wxST_ELLIPSIZE_END = 0x0010
22 };
23
24 /**
25 @class wxStaticText
26
27 A static text control displays one or more lines of read-only text.
28 wxStaticText supports the three classic text alignments, label ellipsization
29 and formatting markup.
30
31 @beginStyleTable
32 @style{wxALIGN_LEFT}
33 Align the text to the left
34 @style{wxALIGN_RIGHT}
35 Align the text to the right
36 @style{wxALIGN_CENTRE}
37 Center the text (horizontally)
38 @style{wxST_NO_AUTORESIZE}
39 By default, the control will adjust its size to exactly fit to the
40 size of the text when SetLabel is called. If this style flag is
41 given, the control will not change its size (this style is
42 especially useful with controls which also have wxALIGN_RIGHT or
43 CENTER style because otherwise they won't make sense any longer
44 after a call to SetLabel)
45 @style{wxST_ELLIPSIZE_START}
46 If the labeltext width exceeds the control width, replace the beginning
47 of the label with an ellipsis
48 @style{wxST_ELLIPSIZE_MIDDLE}
49 If the label text width exceeds the control width, replace the middle
50 of the label with an ellipsis
51 @style{wxST_ELLIPSIZE_END}
52 If the label text width exceeds the control width, replace the end
53 of the label with an ellipsis
54 @style{wxST_MARKUP}
55 Support markup in the label; see SetLabel() for more information
56 @endStyleTable
57
58 @library{wxcore}
59 @category{ctrl}
60 @appearance{statictext.png}
61
62 @see wxStaticBitmap, wxStaticBox
63 */
64 class wxStaticText : public wxControl
65 {
66 public:
67 /**
68 Default constructor.
69 */
70 wxStaticText();
71
72 /**
73 Constructor, creating and showing a text control.
74
75 @param parent
76 Parent window. Should not be @NULL.
77 @param id
78 Control identifier. A value of -1 denotes a default value.
79 @param label
80 Text label.
81 @param pos
82 Window position.
83 @param size
84 Window size.
85 @param style
86 Window style. See wxStaticText.
87 @param name
88 Window name.
89
90 @see Create()
91 */
92 wxStaticText(wxWindow* parent, wxWindowID id,
93 const wxString& label,
94 const wxPoint& pos = wxDefaultPosition,
95 const wxSize& size = wxDefaultSize,
96 long style = 0,
97 const wxString& name = wxStaticTextNameStr);
98
99 /**
100 Creation function, for two-step construction. For details see wxStaticText().
101 */
102 bool Create(wxWindow* parent, wxWindowID id, const wxString& label,
103 const wxPoint& pos = wxDefaultPosition,
104 const wxSize& size = wxDefaultSize, long style = 0,
105 const wxString& name = wxStaticTextNameStr);
106
107 /**
108 Escapes all the symbols of @a str that have a special meaning (<tt><>"'&</tt>) for
109 wxStaticText objects with the @c wxST_MARKUP style.
110 Those symbols are replaced the corresponding entities (&lt; &gt; &quot; &apos; &amp;).
111 */
112 static wxString EscapeMarkup(const wxString& str);
113
114 /**
115 Replaces parts of the @a label string with ellipsis, if needed, so
116 that it doesn't exceed @a maxWidth.
117
118 @param label
119 The string to ellipsize
120 @param dc
121 The DC used to retrieve the character widths through the
122 wxDC::GetPartialTextExtents() function.
123 @param mode
124 The ellipsization modes. See ::wxEllipsizeMode.
125 @param maxWidth
126 The maximum width of the returned string in pixels.
127 */
128 static wxString Ellipsize(const wxString& label, const wxDC& dc,
129 wxEllipsizeMode mode, int maxWidth);
130
131 /**
132 Returns the contents of the control.
133
134 Note that the returned string contains both the mnemonics (@& characters),
135 if any, and markup tags, if any.
136 Use GetLabelText() if only the label text is needed.
137 */
138 wxString GetLabel() const;
139
140 /**
141 This method returns the control's label without the mnemonics characters
142 (if any) and without the markup (if the control has @c wxST_MARKUP style).
143 */
144 wxString GetLabelText() const;
145
146 /**
147 This overload returns the given @a label string without the
148 mnemonics characters (if any) and without the markup.
149 */
150 static wxString GetLabelText(const wxString& label);
151
152 /**
153 Returns @true if the window styles for this control contains one of the
154 @c wxST_ELLIPSIZE_START, @c wxST_ELLIPSIZE_MIDDLE or @c wxST_ELLIPSIZE_END styles.
155 */
156 bool IsEllipsized() const;
157
158 /**
159 Removes the markup accepted by wxStaticText when the @c wxST_MARKUP style is used,
160 and then returns the cleaned string.
161
162 See SetLabel() for more info about the markup.
163 */
164 static wxString RemoveMarkup(const wxString& str);
165
166 /**
167 Sets the static text label and updates the controls size to exactly fit the
168 label unless the control has wxST_NO_AUTORESIZE flag.
169
170 This function allows to set decorated static label text on platforms which
171 support it (currently only GTK+ 2). For the other platforms, the markup is
172 ignored.
173
174 The supported tags are:
175 <TABLE>
176 <TR>
177 <TD>&lt;b&gt;</TD>
178 <TD>bold text</TD>
179 </TR>
180 <TR>
181 <TD>&lt;big&gt;</TD>
182 <TD>bigger text</TD>
183 </TR>
184 <TR>
185 <TD>&lt;i&gt;</TD>
186 <TD>italic text</TD>
187 </TR>
188 <TR>
189 <TD>&lt;s&gt;</TD>
190 <TD>strike-through text</TD>
191 </TR>
192 <TR>
193 <TD>&lt;sub&gt;</TD>
194 <TD>subscript text</TD>
195 </TR>
196 <TR>
197 <TD>&lt;sup&gt;</TD>
198 <TD>superscript text</TD>
199 </TR>
200 <TR>
201 <TD>&lt;small&gt;</TD>
202 <TD>smaller text</TD>
203 </TR>
204 <TR>
205 <TD>&lt;tt&gt;</TD>
206 <TD>monospaced text</TD>
207 </TR>
208 <TR>
209 <TD>&lt;u&gt;</TD>
210 <TD>underlined text</TD>
211 </TR>
212 <TR>
213 <TD>&lt;span&gt;</TD>
214 <TD>generic formatter tag; see Pango Markup
215 (http://library.gnome.org/devel/pango/unstable/PangoMarkupFormat.html)
216 for more information.</TD>
217 </TR>
218 </TABLE>
219
220 Note that the string must be well-formed (e.g. all tags must be correctly
221 closed) otherwise it can be not shown correctly or at all.
222 Also note that you need to escape the following special characters:
223
224 <TABLE>
225 <TR>
226 <TD>@b Special character</TD>
227 <TD>@b Escape as</TD>
228 </TR>
229 <TR>
230 <TD>@c &amp;</TD>
231 <TD>@c &amp;amp; or as &amp;&amp;</TD>
232 </TR>
233 <TR>
234 <TD>@c &apos;</TD>
235 <TD>@c &amp;apos;</TD>
236 </TR>
237 <TR>
238 <TD>@c &quot;</TD>
239 <TD>@c &amp;quot;</TD>
240 </TR>
241 <TR>
242 <TD>@c &lt;</TD>
243 <TD>@c &amp;lt;</TD>
244 </TR>
245 <TR>
246 <TD>@c &gt;</TD>
247 <TD>@c &amp;gt;</TD>
248 </TR>
249 </TABLE>
250
251 The non-escaped ampersand @c &amp; characters are interpreted as
252 mnemonics; see wxControl::SetLabel.
253
254 Example:
255
256 @param label
257 The new label to set.
258 It may contain newline characters and the markup tags described above.
259 */
260 virtual void SetLabel(const wxString& label);
261
262 /**
263 This functions wraps the controls label so that each of its lines becomes at
264 most @a width pixels wide if possible (the lines are broken at words
265 boundaries so it might not be the case if words are too long).
266
267 If @a width is negative, no wrapping is done. Note that this width is not
268 necessarily the total width of the control, since a few pixels for the
269 border (depending on the controls border style) may be added.
270
271 @since 2.6.2
272 */
273 void Wrap(int width);
274 };
275