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