]> git.saurik.com Git - wxWidgets.git/blame - src/msw/stattext.cpp
do use the font in DoGetTextExtent()
[wxWidgets.git] / src / msw / stattext.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
1e3a888e 2// Name: src/msw/stattext.cpp
2bda0e17
KB
3// Purpose: wxStaticText
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
6c9a19aa 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
2bda0e17
KB
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
16#pragma hdrstop
17#endif
18
1e6feb95
VZ
19#if wxUSE_STATTEXT
20
ccdc11bb
WS
21#include "wx/stattext.h"
22
2bda0e17 23#ifndef WX_PRECOMP
ac10c957
VZ
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"
2bda0e17
KB
29#endif
30
2bda0e17 31#include "wx/msw/private.h"
2bda0e17 32
51741307 33#if wxUSE_EXTENDED_RTTI
bc9fb572
JS
34WX_DEFINE_FLAGS( wxStaticTextStyle )
35
3ff066a4 36wxBEGIN_FLAGS( wxStaticTextStyle )
bc9fb572
JS
37 // new style border flags, we put them first to
38 // use them for streaming out
3ff066a4
SC
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)
57f4f925 45
bc9fb572 46 // old style border flags
3ff066a4
SC
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)
cb0afb26 52 wxFLAGS_MEMBER(wxBORDER)
bc9fb572
JS
53
54 // standard window styles
3ff066a4
SC
55 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
56 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
57 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
58 wxFLAGS_MEMBER(wxWANTS_CHARS)
cb0afb26 59 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
3ff066a4
SC
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
69wxEND_FLAGS( wxStaticTextStyle )
bc9fb572 70
51741307
SC
71IMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticText, wxControl,"wx/stattext.h")
72
3ff066a4 73wxBEGIN_PROPERTIES_TABLE(wxStaticText)
57f4f925 74 wxPROPERTY( Label,wxString, SetLabel, GetLabel, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
af498247 75 wxPROPERTY_FLAGS( WindowStyle , wxStaticTextStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3ff066a4 76wxEND_PROPERTIES_TABLE()
51741307 77
3ff066a4
SC
78wxBEGIN_HANDLERS_TABLE(wxStaticText)
79wxEND_HANDLERS_TABLE()
51741307 80
57f4f925 81wxCONSTRUCTOR_6( wxStaticText , wxWindow* , Parent , wxWindowID , Id , wxString , Label , wxPoint , Position , wxSize , Size , long , WindowStyle )
51741307 82#else
2bda0e17 83IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxControl)
51741307 84#endif
2bda0e17 85
6dd16e4f
VZ
86bool 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)
2bda0e17 93{
6dd16e4f 94 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
57f4f925 95 return false;
2bda0e17 96
6dd16e4f 97 if ( !MSWCreateControl(wxT("STATIC"), label, pos, size) )
57f4f925 98 return false;
2bda0e17 99
57f4f925 100 return true;
6dd16e4f 101}
c085e333 102
65bc172c
VZ
103wxBorder wxStaticText::GetDefaultBorder() const
104{
105 return wxBORDER_NONE;
106}
107
6dd16e4f
VZ
108WXDWORD wxStaticText::MSWGetStyle(long style, WXDWORD *exstyle) const
109{
110 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
111
112 // translate the alignment flags to the Windows ones
113 //
114 // note that both wxALIGN_LEFT and SS_LEFT are equal to 0 so we shouldn't
115 // test for them using & operator
116 if ( style & wxALIGN_CENTRE )
117 msStyle |= SS_CENTER;
118 else if ( style & wxALIGN_RIGHT )
119 msStyle |= SS_RIGHT;
120 else
121 msStyle |= SS_LEFT;
122
42b1fb63 123 // this style is necessary to receive mouse events
39bc0347
VZ
124 // Win NT and later have the SS_ENDELLIPSIS style which is useful to us:
125 if (wxGetOsVersion() == wxOS_WINDOWS_NT)
126 {
127 // for now, add the SS_ENDELLIPSIS style if wxST_ELLIPSIZE_END is given;
128 // we may need to remove it later in ::SetLabel() if the given label
129 // has newlines
130 if ( style & wxST_ELLIPSIZE_END )
131 msStyle |= SS_ENDELLIPSIS;
132 }
133
42b1fb63
VZ
134 msStyle |= SS_NOTIFY;
135
6dd16e4f 136 return msStyle;
2bda0e17
KB
137}
138
f68586e5 139wxSize wxStaticText::DoGetBestSize() const
2bda0e17 140{
8812e098 141 wxClientDC dc(wx_const_cast(wxStaticText *, this));
008881c3
JS
142 wxFont font(GetFont());
143 if (!font.Ok())
144 font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
7a0e7e55 145
008881c3 146 dc.SetFont(font);
8812e098
VZ
147
148 wxCoord widthTextMax, heightTextTotal;
32cd189d 149 dc.GetMultiLineTextExtent(GetLabelText(), &widthTextMax, &heightTextTotal);
8812e098 150
37b53d2a 151#ifdef __WXWINCE__
8812e098
VZ
152 if ( widthTextMax )
153 widthTextMax += 2;
154#endif // __WXWINCE__
155
7a0e7e55
VZ
156 // border takes extra space
157 //
158 // TODO: this is probably not wxStaticText-specific and should be moved
159 wxCoord border;
160 switch ( GetBorder() )
161 {
162 case wxBORDER_STATIC:
163 case wxBORDER_SIMPLE:
164 border = 1;
165 break;
166
167 case wxBORDER_SUNKEN:
168 border = 2;
169 break;
170
171 case wxBORDER_RAISED:
172 case wxBORDER_DOUBLE:
173 border = 3;
174 break;
175
176 default:
177 wxFAIL_MSG( _T("unknown border style") );
178 // fall through
179
180 case wxBORDER_NONE:
181 border = 0;
182 }
183
184 widthTextMax += 2*border;
185 heightTextTotal += 2*border;
186
31582e4e
RD
187 wxSize best(widthTextMax, heightTextTotal);
188 CacheBestSize(best);
189 return best;
2bda0e17
KB
190}
191
1e3a888e
VZ
192void wxStaticText::DoSetSize(int x, int y, int w, int h, int sizeFlags)
193{
39bc0347 194 // note: we first need to set the size and _then_ call UpdateLabel
1e3a888e
VZ
195 wxStaticTextBase::DoSetSize(x, y, w, h, sizeFlags);
196
39bc0347
VZ
197 // do we need to ellipsize the contents?
198 long styleReal = ::GetWindowLong(GetHwnd(), GWL_STYLE);
199 if ( !(styleReal & SS_ENDELLIPSIS) )
200 {
201 // we don't have SS_ENDELLIPSIS style:
202 // we need to (eventually) do ellipsization ourselves
203 UpdateLabel();
204 }
205 //else: we don't or the OS will do it for us
206
207 // we need to refresh the window after changing its size as the standard
208 // control doesn't always update itself properly
1e3a888e
VZ
209 Refresh();
210}
211
c0e6c051
RD
212void wxStaticText::SetLabel(const wxString& label)
213{
39bc0347
VZ
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
235 // this call will save the label in m_labelOrig and set it into this window
236 // (through wxWindow::SetLabel)
237 m_labelOrig = label;
238
239 if ((styleReal & SS_ENDELLIPSIS) == 0)
240 DoSetLabel(GetEllipsizedLabelWithoutMarkup());
241 else
242 DoSetLabel(RemoveMarkup(label));
c0e6c051
RD
243
244 // adjust the size of the window to fit to the label unless autoresizing is
245 // disabled
39bc0347
VZ
246 if ( !HasFlag(wxST_NO_AUTORESIZE) &&
247 !IsEllipsized() ) // if ellipsize is ON, then we don't want to get resized!
c0e6c051 248 {
9f884528 249 InvalidateBestSize();
57f4f925
WS
250 DoSetSize(wxDefaultCoord, wxDefaultCoord, wxDefaultCoord, wxDefaultCoord,
251 wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT);
c0e6c051
RD
252 }
253}
254
c0e6c051
RD
255bool wxStaticText::SetFont(const wxFont& font)
256{
257 bool ret = wxControl::SetFont(font);
258
259 // adjust the size of the window to fit to the label unless autoresizing is
260 // disabled
39bc0347 261 if ( !HasFlag(wxST_NO_AUTORESIZE) )
c0e6c051 262 {
9f884528 263 InvalidateBestSize();
57f4f925
WS
264 DoSetSize(wxDefaultCoord, wxDefaultCoord, wxDefaultCoord, wxDefaultCoord,
265 wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT);
c0e6c051
RD
266 }
267
268 return ret;
269}
486fd225 270
39bc0347
VZ
271// for wxST_ELLIPSIZE_* support:
272
273wxString wxStaticText::DoGetLabel() const
274{
275 return wxGetWindowText(GetHwnd());
276}
277
278void wxStaticText::DoSetLabel(const wxString& str)
279{
280 SetWindowText(GetHwnd(), str.c_str());
281}
282
283
1e6feb95 284#endif // wxUSE_STATTEXT