]> git.saurik.com Git - wxWidgets.git/blob - src/common/stattextcmn.cpp
Define __VISUALC__ for ICC under Windows again.
[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 // Copyright: (c) 1999-2006 Vadim Zeitlin
7 // (c) 2007 Francesco Montorsi
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #if wxUSE_STATTEXT
27
28 #ifndef WX_PRECOMP
29 #include "wx/stattext.h"
30 #include "wx/button.h"
31 #include "wx/dcclient.h"
32 #include "wx/intl.h"
33 #include "wx/log.h"
34 #include "wx/settings.h"
35 #include "wx/sizer.h"
36 #include "wx/containr.h"
37 #endif
38
39 #include "wx/textwrapper.h"
40
41 #include "wx/private/markupparser.h"
42
43 extern WXDLLEXPORT_DATA(const char) wxStaticTextNameStr[] = "staticText";
44
45 // ----------------------------------------------------------------------------
46 // XTI
47 // ----------------------------------------------------------------------------
48
49 wxDEFINE_FLAGS( wxStaticTextStyle )
50 wxBEGIN_FLAGS( wxStaticTextStyle )
51 // new style border flags, we put them first to
52 // use them for streaming out
53 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
54 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
55 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
56 wxFLAGS_MEMBER(wxBORDER_RAISED)
57 wxFLAGS_MEMBER(wxBORDER_STATIC)
58 wxFLAGS_MEMBER(wxBORDER_NONE)
59
60 // old style border flags
61 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
62 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
63 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
64 wxFLAGS_MEMBER(wxRAISED_BORDER)
65 wxFLAGS_MEMBER(wxSTATIC_BORDER)
66 wxFLAGS_MEMBER(wxBORDER)
67
68 // standard window styles
69 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
70 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
71 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
72 wxFLAGS_MEMBER(wxWANTS_CHARS)
73 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
74 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
75 wxFLAGS_MEMBER(wxVSCROLL)
76 wxFLAGS_MEMBER(wxHSCROLL)
77
78 wxFLAGS_MEMBER(wxST_NO_AUTORESIZE)
79 wxFLAGS_MEMBER(wxALIGN_LEFT)
80 wxFLAGS_MEMBER(wxALIGN_RIGHT)
81 wxFLAGS_MEMBER(wxALIGN_CENTRE)
82 wxEND_FLAGS( wxStaticTextStyle )
83
84 wxIMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticText, wxControl, "wx/stattext.h")
85
86 wxBEGIN_PROPERTIES_TABLE(wxStaticText)
87 wxPROPERTY( Label,wxString, SetLabel, GetLabel, wxString(), 0 /*flags*/, \
88 wxT("Helpstring"), wxT("group"))
89 wxPROPERTY_FLAGS( WindowStyle, wxStaticTextStyle, long, SetWindowStyleFlag, \
90 GetWindowStyleFlag, wxEMPTY_PARAMETER_VALUE, 0 /*flags*/, \
91 wxT("Helpstring"), wxT("group")) // style
92 wxEND_PROPERTIES_TABLE()
93
94 wxEMPTY_HANDLERS_TABLE(wxStaticText)
95
96 wxCONSTRUCTOR_6( wxStaticText, wxWindow*, Parent, wxWindowID, Id, \
97 wxString, Label, wxPoint, Position, wxSize, Size, long, WindowStyle )
98
99
100 // ----------------------------------------------------------------------------
101 // wxTextWrapper
102 // ----------------------------------------------------------------------------
103
104 void wxTextWrapper::Wrap(wxWindow *win, const wxString& text, int widthMax)
105 {
106 wxString line;
107
108 wxString::const_iterator lastSpace = text.end();
109 wxString::const_iterator lineStart = text.begin();
110 for ( wxString::const_iterator p = lineStart; ; ++p )
111 {
112 if ( IsStartOfNewLine() )
113 {
114 OnNewLine();
115
116 lastSpace = text.end();
117 line.clear();
118 lineStart = p;
119 }
120
121 if ( p == text.end() || *p == wxT('\n') )
122 {
123 DoOutputLine(line);
124
125 if ( p == text.end() )
126 break;
127 }
128 else // not EOL
129 {
130 if ( *p == wxT(' ') )
131 lastSpace = p;
132
133 line += *p;
134
135 if ( widthMax >= 0 && lastSpace != text.end() )
136 {
137 int width;
138 win->GetTextExtent(line, &width, NULL);
139
140 if ( width > widthMax )
141 {
142 // remove the last word from this line
143 line.erase(lastSpace - lineStart, p + 1 - lineStart);
144 DoOutputLine(line);
145
146 // go back to the last word of this line which we didn't
147 // output yet
148 p = lastSpace;
149 }
150 }
151 //else: no wrapping at all or impossible to wrap
152 }
153 }
154 }
155
156
157 // ----------------------------------------------------------------------------
158 // wxLabelWrapper: helper class for wxStaticTextBase::Wrap()
159 // ----------------------------------------------------------------------------
160
161 class wxLabelWrapper : public wxTextWrapper
162 {
163 public:
164 void WrapLabel(wxWindow *text, int widthMax)
165 {
166 m_text.clear();
167 Wrap(text, text->GetLabel(), widthMax);
168 text->SetLabel(m_text);
169 }
170
171 protected:
172 virtual void OnOutputLine(const wxString& line)
173 {
174 m_text += line;
175 }
176
177 virtual void OnNewLine()
178 {
179 m_text += wxT('\n');
180 }
181
182 private:
183 wxString m_text;
184 };
185
186
187 // ----------------------------------------------------------------------------
188 // wxStaticTextBase
189 // ----------------------------------------------------------------------------
190
191 void wxStaticTextBase::Wrap(int width)
192 {
193 wxLabelWrapper wrapper;
194 wrapper.WrapLabel(this, width);
195 }
196
197 // ----------------------------------------------------------------------------
198 // wxStaticTextBase - generic implementation for wxST_ELLIPSIZE_* support
199 // ----------------------------------------------------------------------------
200
201 void wxStaticTextBase::UpdateLabel()
202 {
203 if (!IsEllipsized())
204 return;
205
206 wxString newlabel = GetEllipsizedLabel();
207
208 // we need to touch the "real" label (i.e. the text set inside the control,
209 // using port-specific functions) instead of the string returned by GetLabel().
210 //
211 // In fact, we must be careful not to touch the original label passed to
212 // SetLabel() otherwise GetLabel() will behave in a strange way to the user
213 // (e.g. returning a "Ver...ing" instead of "Very long string") !
214 if (newlabel == DoGetLabel())
215 return;
216 DoSetLabel(newlabel);
217 }
218
219 wxString wxStaticTextBase::GetEllipsizedLabel() const
220 {
221 // this function should be used only by ports which do not support
222 // ellipsis in static texts: we first remove markup (which cannot
223 // be handled safely by Ellipsize()) and then ellipsize the result.
224
225 wxString ret(m_labelOrig);
226
227 if (IsEllipsized())
228 ret = Ellipsize(ret);
229
230 return ret;
231 }
232
233 wxString wxStaticTextBase::Ellipsize(const wxString& label) const
234 {
235 wxSize sz(GetSize());
236 if (sz.GetWidth() < 2 || sz.GetHeight() < 2)
237 {
238 // the size of this window is not valid (yet)
239 return label;
240 }
241
242 wxClientDC dc(const_cast<wxStaticTextBase*>(this));
243 dc.SetFont(GetFont());
244
245 wxEllipsizeMode mode;
246 if ( HasFlag(wxST_ELLIPSIZE_START) )
247 mode = wxELLIPSIZE_START;
248 else if ( HasFlag(wxST_ELLIPSIZE_MIDDLE) )
249 mode = wxELLIPSIZE_MIDDLE;
250 else if ( HasFlag(wxST_ELLIPSIZE_END) )
251 mode = wxELLIPSIZE_END;
252 else
253 {
254 wxFAIL_MSG( "should only be called if have one of wxST_ELLIPSIZE_XXX" );
255
256 return label;
257 }
258
259 return wxControl::Ellipsize(label, dc, mode, sz.GetWidth());
260 }
261
262 #endif // wxUSE_STATTEXT