]> git.saurik.com Git - wxWidgets.git/blob - src/common/stattextcmn.cpp
Implement incremental search in wxGenericListCtrl.
[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 // ----------------------------------------------------------------------------
199 // wxStaticTextBase - generic implementation for wxST_ELLIPSIZE_* support
200 // ----------------------------------------------------------------------------
201
202 void wxStaticTextBase::UpdateLabel()
203 {
204 if (!IsEllipsized())
205 return;
206
207 wxString newlabel = GetEllipsizedLabel();
208
209 // we need to touch the "real" label (i.e. the text set inside the control,
210 // using port-specific functions) instead of the string returned by GetLabel().
211 //
212 // In fact, we must be careful not to touch the original label passed to
213 // SetLabel() otherwise GetLabel() will behave in a strange way to the user
214 // (e.g. returning a "Ver...ing" instead of "Very long string") !
215 if (newlabel == DoGetLabel())
216 return;
217 DoSetLabel(newlabel);
218 }
219
220 wxString wxStaticTextBase::GetEllipsizedLabel() const
221 {
222 // this function should be used only by ports which do not support
223 // ellipsis in static texts: we first remove markup (which cannot
224 // be handled safely by Ellipsize()) and then ellipsize the result.
225
226 wxString ret(m_labelOrig);
227
228 if (IsEllipsized())
229 ret = Ellipsize(ret);
230
231 return ret;
232 }
233
234 wxString wxStaticTextBase::Ellipsize(const wxString& label) const
235 {
236 wxSize sz(GetSize());
237 if (sz.GetWidth() < 2 || sz.GetHeight() < 2)
238 {
239 // the size of this window is not valid (yet)
240 return label;
241 }
242
243 wxClientDC dc(const_cast<wxStaticTextBase*>(this));
244 dc.SetFont(GetFont());
245
246 wxEllipsizeMode mode;
247 if ( HasFlag(wxST_ELLIPSIZE_START) )
248 mode = wxELLIPSIZE_START;
249 else if ( HasFlag(wxST_ELLIPSIZE_MIDDLE) )
250 mode = wxELLIPSIZE_MIDDLE;
251 else if ( HasFlag(wxST_ELLIPSIZE_END) )
252 mode = wxELLIPSIZE_END;
253 else
254 {
255 wxFAIL_MSG( "should only be called if have one of wxST_ELLIPSIZE_XXX" );
256
257 return label;
258 }
259
260 return wxControl::Ellipsize(label, dc, mode, sz.GetWidth());
261 }
262
263 #endif // wxUSE_STATTEXT