]> git.saurik.com Git - wxWidgets.git/blame - src/common/stattextcmn.cpp
Make public headers compatible with Objective-C++ with ARC.
[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)
39bc0347
VZ
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
f313deaa
PC
26#if wxUSE_STATTEXT
27
39bc0347 28#ifndef WX_PRECOMP
f313deaa 29 #include "wx/stattext.h"
39bc0347
VZ
30 #include "wx/button.h"
31 #include "wx/dcclient.h"
32 #include "wx/intl.h"
523b9ce4 33 #include "wx/log.h"
39bc0347 34 #include "wx/settings.h"
39bc0347
VZ
35 #include "wx/sizer.h"
36 #include "wx/containr.h"
37#endif
38
f313deaa 39#include "wx/textwrapper.h"
39bc0347 40
0d0fdaac
VZ
41#include "wx/private/markupparser.h"
42
f313deaa 43extern WXDLLEXPORT_DATA(const char) wxStaticTextNameStr[] = "staticText";
39bc0347 44
28953245
SC
45// ----------------------------------------------------------------------------
46// XTI
47// ----------------------------------------------------------------------------
48
49wxDEFINE_FLAGS( wxStaticTextStyle )
50wxBEGIN_FLAGS( wxStaticTextStyle )
51// new style border flags, we put them first to
52// use them for streaming out
53wxFLAGS_MEMBER(wxBORDER_SIMPLE)
54wxFLAGS_MEMBER(wxBORDER_SUNKEN)
55wxFLAGS_MEMBER(wxBORDER_DOUBLE)
56wxFLAGS_MEMBER(wxBORDER_RAISED)
57wxFLAGS_MEMBER(wxBORDER_STATIC)
58wxFLAGS_MEMBER(wxBORDER_NONE)
59
60// old style border flags
61wxFLAGS_MEMBER(wxSIMPLE_BORDER)
62wxFLAGS_MEMBER(wxSUNKEN_BORDER)
63wxFLAGS_MEMBER(wxDOUBLE_BORDER)
64wxFLAGS_MEMBER(wxRAISED_BORDER)
65wxFLAGS_MEMBER(wxSTATIC_BORDER)
66wxFLAGS_MEMBER(wxBORDER)
67
68// standard window styles
69wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
70wxFLAGS_MEMBER(wxCLIP_CHILDREN)
71wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
72wxFLAGS_MEMBER(wxWANTS_CHARS)
73wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
74wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
75wxFLAGS_MEMBER(wxVSCROLL)
76wxFLAGS_MEMBER(wxHSCROLL)
77
78wxFLAGS_MEMBER(wxST_NO_AUTORESIZE)
79wxFLAGS_MEMBER(wxALIGN_LEFT)
80wxFLAGS_MEMBER(wxALIGN_RIGHT)
81wxFLAGS_MEMBER(wxALIGN_CENTRE)
82wxEND_FLAGS( wxStaticTextStyle )
83
84wxIMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticText, wxControl, "wx/stattext.h")
85
86wxBEGIN_PROPERTIES_TABLE(wxStaticText)
87wxPROPERTY( Label,wxString, SetLabel, GetLabel, wxString(), 0 /*flags*/, \
88 wxT("Helpstring"), wxT("group"))
89wxPROPERTY_FLAGS( WindowStyle, wxStaticTextStyle, long, SetWindowStyleFlag, \
90 GetWindowStyleFlag, wxEMPTY_PARAMETER_VALUE, 0 /*flags*/, \
91 wxT("Helpstring"), wxT("group")) // style
92wxEND_PROPERTIES_TABLE()
93
94wxEMPTY_HANDLERS_TABLE(wxStaticText)
95
96wxCONSTRUCTOR_6( wxStaticText, wxWindow*, Parent, wxWindowID, Id, \
97 wxString, Label, wxPoint, Position, wxSize, Size, long, WindowStyle )
98
99
39bc0347
VZ
100// ----------------------------------------------------------------------------
101// wxTextWrapper
102// ----------------------------------------------------------------------------
103
104void wxTextWrapper::Wrap(wxWindow *win, const wxString& text, int widthMax)
105{
39bc0347
VZ
106 wxString line;
107
5b871700
VS
108 wxString::const_iterator lastSpace = text.end();
109 wxString::const_iterator lineStart = text.begin();
110 for ( wxString::const_iterator p = lineStart; ; ++p )
39bc0347
VZ
111 {
112 if ( IsStartOfNewLine() )
113 {
114 OnNewLine();
115
5b871700 116 lastSpace = text.end();
39bc0347
VZ
117 line.clear();
118 lineStart = p;
119 }
120
9a83f860 121 if ( p == text.end() || *p == wxT('\n') )
39bc0347
VZ
122 {
123 DoOutputLine(line);
124
5b871700 125 if ( p == text.end() )
39bc0347
VZ
126 break;
127 }
128 else // not EOL
129 {
9a83f860 130 if ( *p == wxT(' ') )
39bc0347
VZ
131 lastSpace = p;
132
133 line += *p;
134
5b871700 135 if ( widthMax >= 0 && lastSpace != text.end() )
39bc0347
VZ
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
161class wxLabelWrapper : public wxTextWrapper
162{
163public:
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
171protected:
172 virtual void OnOutputLine(const wxString& line)
173 {
174 m_text += line;
175 }
176
177 virtual void OnNewLine()
178 {
9a83f860 179 m_text += wxT('\n');
39bc0347
VZ
180 }
181
182private:
183 wxString m_text;
184};
185
186
187// ----------------------------------------------------------------------------
188// wxStaticTextBase
189// ----------------------------------------------------------------------------
190
191void wxStaticTextBase::Wrap(int width)
192{
193 wxLabelWrapper wrapper;
194 wrapper.WrapLabel(this, width);
195}
196
4520d583
FM
197// ----------------------------------------------------------------------------
198// wxStaticTextBase - generic implementation for wxST_ELLIPSIZE_* support
199// ----------------------------------------------------------------------------
200
201void wxStaticTextBase::UpdateLabel()
202{
203 if (!IsEllipsized())
204 return;
205
3da9cffc 206 wxString newlabel = GetEllipsizedLabel();
4520d583
FM
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
3da9cffc 219wxString wxStaticTextBase::GetEllipsizedLabel() const
4520d583
FM
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
4520d583
FM
227 if (IsEllipsized())
228 ret = Ellipsize(ret);
229
230 return ret;
231}
232
233wxString 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;
41ce5eff
VZ
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 }
4520d583 258
5c87527c 259 return wxControl::Ellipsize(label, dc, mode, sz.GetWidth());
39bc0347
VZ
260}
261
262#endif // wxUSE_STATTEXT