]> git.saurik.com Git - wxWidgets.git/blob - src/msw/stattext.cpp
don't keep dangling m_prevRadio pointer (2nd part of patch 1836644)
[wxWidgets.git] / src / msw / stattext.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/stattext.cpp
3 // Purpose: wxStaticText
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #if wxUSE_STATTEXT
20
21 #include "wx/stattext.h"
22
23 #ifndef WX_PRECOMP
24 #include "wx/event.h"
25 #include "wx/app.h"
26 #include "wx/brush.h"
27 #include "wx/dcclient.h"
28 #include "wx/settings.h"
29 #endif
30
31 #include "wx/msw/private.h"
32
33 #if wxUSE_EXTENDED_RTTI
34 WX_DEFINE_FLAGS( wxStaticTextStyle )
35
36 wxBEGIN_FLAGS( wxStaticTextStyle )
37 // new style border flags, we put them first to
38 // use them for streaming out
39 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
40 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
41 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
42 wxFLAGS_MEMBER(wxBORDER_RAISED)
43 wxFLAGS_MEMBER(wxBORDER_STATIC)
44 wxFLAGS_MEMBER(wxBORDER_NONE)
45
46 // old style border flags
47 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
48 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
49 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
50 wxFLAGS_MEMBER(wxRAISED_BORDER)
51 wxFLAGS_MEMBER(wxSTATIC_BORDER)
52 wxFLAGS_MEMBER(wxBORDER)
53
54 // standard window styles
55 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
56 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
57 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
58 wxFLAGS_MEMBER(wxWANTS_CHARS)
59 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
60 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
61 wxFLAGS_MEMBER(wxVSCROLL)
62 wxFLAGS_MEMBER(wxHSCROLL)
63
64 wxFLAGS_MEMBER(wxST_NO_AUTORESIZE)
65 wxFLAGS_MEMBER(wxALIGN_LEFT)
66 wxFLAGS_MEMBER(wxALIGN_RIGHT)
67 wxFLAGS_MEMBER(wxALIGN_CENTRE)
68
69 wxEND_FLAGS( wxStaticTextStyle )
70
71 IMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticText, wxControl,"wx/stattext.h")
72
73 wxBEGIN_PROPERTIES_TABLE(wxStaticText)
74 wxPROPERTY( Label,wxString, SetLabel, GetLabel, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
75 wxPROPERTY_FLAGS( WindowStyle , wxStaticTextStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
76 wxEND_PROPERTIES_TABLE()
77
78 wxBEGIN_HANDLERS_TABLE(wxStaticText)
79 wxEND_HANDLERS_TABLE()
80
81 wxCONSTRUCTOR_6( wxStaticText , wxWindow* , Parent , wxWindowID , Id , wxString , Label , wxPoint , Position , wxSize , Size , long , WindowStyle )
82 #else
83 IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxControl)
84 #endif
85
86 bool wxStaticText::Create(wxWindow *parent,
87 wxWindowID id,
88 const wxString& label,
89 const wxPoint& pos,
90 const wxSize& size,
91 long style,
92 const wxString& name)
93 {
94 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
95 return false;
96
97 if ( !MSWCreateControl(wxT("STATIC"), label, pos, size) )
98 return false;
99
100 return true;
101 }
102
103 WXDWORD wxStaticText::MSWGetStyle(long style, WXDWORD *exstyle) const
104 {
105 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
106
107 // translate the alignment flags to the Windows ones
108 //
109 // note that both wxALIGN_LEFT and SS_LEFT are equal to 0 so we shouldn't
110 // test for them using & operator
111 if ( style & wxALIGN_CENTRE )
112 msStyle |= SS_CENTER;
113 else if ( style & wxALIGN_RIGHT )
114 msStyle |= SS_RIGHT;
115 else
116 msStyle |= SS_LEFT;
117
118 #ifdef SS_ENDELLIPSIS
119 // this style is necessary to receive mouse events
120 // Win NT and later have the SS_ENDELLIPSIS style which is useful to us:
121 if (wxGetOsVersion() == wxOS_WINDOWS_NT)
122 {
123 // for now, add the SS_ENDELLIPSIS style if wxST_ELLIPSIZE_END is given;
124 // we may need to remove it later in ::SetLabel() if the given label
125 // has newlines
126 if ( style & wxST_ELLIPSIZE_END )
127 msStyle |= SS_ENDELLIPSIS;
128 }
129 #endif // SS_ENDELLIPSIS
130
131 msStyle |= SS_NOTIFY;
132
133 return msStyle;
134 }
135
136 wxSize wxStaticText::DoGetBestSize() const
137 {
138 wxClientDC dc(wx_const_cast(wxStaticText *, this));
139 wxFont font(GetFont());
140 if (!font.Ok())
141 font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
142
143 dc.SetFont(font);
144
145 wxCoord widthTextMax, heightTextTotal;
146 dc.GetMultiLineTextExtent(GetLabelText(), &widthTextMax, &heightTextTotal);
147
148 #ifdef __WXWINCE__
149 if ( widthTextMax )
150 widthTextMax += 2;
151 #endif // __WXWINCE__
152
153 // border takes extra space
154 //
155 // TODO: this is probably not wxStaticText-specific and should be moved
156 wxCoord border;
157 switch ( GetBorder() )
158 {
159 case wxBORDER_STATIC:
160 case wxBORDER_SIMPLE:
161 border = 1;
162 break;
163
164 case wxBORDER_SUNKEN:
165 border = 2;
166 break;
167
168 case wxBORDER_RAISED:
169 case wxBORDER_DOUBLE:
170 border = 3;
171 break;
172
173 default:
174 wxFAIL_MSG( _T("unknown border style") );
175 // fall through
176
177 case wxBORDER_NONE:
178 border = 0;
179 }
180
181 widthTextMax += 2*border;
182 heightTextTotal += 2*border;
183
184 wxSize best(widthTextMax, heightTextTotal);
185 CacheBestSize(best);
186 return best;
187 }
188
189 void wxStaticText::DoSetSize(int x, int y, int w, int h, int sizeFlags)
190 {
191 // note: we first need to set the size and _then_ call UpdateLabel
192 wxStaticTextBase::DoSetSize(x, y, w, h, sizeFlags);
193
194 #ifdef SS_ENDELLIPSIS
195 // do we need to ellipsize the contents?
196 long styleReal = ::GetWindowLong(GetHwnd(), GWL_STYLE);
197 if ( !(styleReal & SS_ENDELLIPSIS) )
198 {
199 // we don't have SS_ENDELLIPSIS style:
200 // we need to (eventually) do ellipsization ourselves
201 UpdateLabel();
202 }
203 //else: we don't or the OS will do it for us
204 #endif // SS_ENDELLIPSIS
205
206 // we need to refresh the window after changing its size as the standard
207 // control doesn't always update itself properly
208 Refresh();
209 }
210
211 void wxStaticText::SetLabel(const wxString& label)
212 {
213 #ifdef SS_ENDELLIPSIS
214 long styleReal = ::GetWindowLong(GetHwnd(), GWL_STYLE);
215 if ( HasFlag(wxST_ELLIPSIZE_END) &&
216 wxGetOsVersion() == wxOS_WINDOWS_NT )
217 {
218 // adding SS_ENDELLIPSIS or SS_ENDELLIPSIS "disables" the correct
219 // newline handling in static texts: the newlines in the labels are
220 // shown as square. Thus we don't use it even on newer OS when
221 // the static label contains a newline.
222 if ( label.Contains(wxT('\n')) )
223 styleReal &= ~SS_ENDELLIPSIS;
224 else
225 styleReal |= SS_ENDELLIPSIS;
226
227 ::SetWindowLong(GetHwnd(), GWL_STYLE, styleReal);
228 }
229 else // style not supported natively
230 {
231 styleReal &= ~SS_ENDELLIPSIS;
232 ::SetWindowLong(GetHwnd(), GWL_STYLE, styleReal);
233 }
234 #endif // SS_ENDELLIPSIS
235
236 // this call will save the label in m_labelOrig and set it into this window
237 // (through wxWindow::SetLabel)
238 m_labelOrig = label;
239
240 #ifdef SS_ENDELLIPSIS
241 if ( styleReal & SS_ENDELLIPSIS )
242 DoSetLabel(RemoveMarkup(label));
243 else
244 #endif // SS_ENDELLIPSIS
245 DoSetLabel(GetEllipsizedLabelWithoutMarkup());
246
247 // adjust the size of the window to fit to the label unless autoresizing is
248 // disabled
249 if ( !HasFlag(wxST_NO_AUTORESIZE) &&
250 !IsEllipsized() ) // if ellipsize is ON, then we don't want to get resized!
251 {
252 InvalidateBestSize();
253 DoSetSize(wxDefaultCoord, wxDefaultCoord, wxDefaultCoord, wxDefaultCoord,
254 wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT);
255 }
256 }
257
258 bool wxStaticText::SetFont(const wxFont& font)
259 {
260 bool ret = wxControl::SetFont(font);
261
262 // adjust the size of the window to fit to the label unless autoresizing is
263 // disabled
264 if ( !HasFlag(wxST_NO_AUTORESIZE) )
265 {
266 InvalidateBestSize();
267 DoSetSize(wxDefaultCoord, wxDefaultCoord, wxDefaultCoord, wxDefaultCoord,
268 wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT);
269 }
270
271 return ret;
272 }
273
274 // for wxST_ELLIPSIZE_* support:
275
276 wxString wxStaticText::DoGetLabel() const
277 {
278 return wxGetWindowText(GetHwnd());
279 }
280
281 void wxStaticText::DoSetLabel(const wxString& str)
282 {
283 SetWindowText(GetHwnd(), str.c_str());
284 }
285
286
287 #endif // wxUSE_STATTEXT