]> git.saurik.com Git - wxWidgets.git/blame - src/msw/stattext.cpp
Simplified event based Drag API for wxDataViewCtrl
[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
8f23c287 97 if ( !MSWCreateControl(wxT("STATIC"), wxEmptyString, pos, size) )
57f4f925 98 return false;
2bda0e17 99
8f23c287
FM
100 // we set the label here and not through MSWCreateControl() because we
101 // need to do many operation on it for ellipsization&markup support
102 SetLabel(label);
103
104 // NOTE: if the label contains ampersand characters which are interpreted as
105 // accelerators, they will be rendered (at least on WinXP) only if the
106 // static text is placed inside a window class which correctly handles
107 // focusing by TAB traversal (e.g. wxPanel).
108
57f4f925 109 return true;
6dd16e4f 110}
c085e333 111
6dd16e4f
VZ
112WXDWORD wxStaticText::MSWGetStyle(long style, WXDWORD *exstyle) const
113{
114 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
115
116 // translate the alignment flags to the Windows ones
117 //
118 // note that both wxALIGN_LEFT and SS_LEFT are equal to 0 so we shouldn't
119 // test for them using & operator
120 if ( style & wxALIGN_CENTRE )
121 msStyle |= SS_CENTER;
122 else if ( style & wxALIGN_RIGHT )
123 msStyle |= SS_RIGHT;
124 else
125 msStyle |= SS_LEFT;
126
d6f2a891 127#ifdef SS_ENDELLIPSIS
42b1fb63 128 // this style is necessary to receive mouse events
39bc0347
VZ
129 // Win NT and later have the SS_ENDELLIPSIS style which is useful to us:
130 if (wxGetOsVersion() == wxOS_WINDOWS_NT)
131 {
132 // for now, add the SS_ENDELLIPSIS style if wxST_ELLIPSIZE_END is given;
133 // we may need to remove it later in ::SetLabel() if the given label
134 // has newlines
135 if ( style & wxST_ELLIPSIZE_END )
136 msStyle |= SS_ENDELLIPSIS;
137 }
d6f2a891 138#endif // SS_ENDELLIPSIS
39bc0347 139
42b1fb63
VZ
140 msStyle |= SS_NOTIFY;
141
6dd16e4f 142 return msStyle;
2bda0e17
KB
143}
144
f68586e5 145wxSize wxStaticText::DoGetBestSize() const
2bda0e17 146{
5c33522f 147 wxClientDC dc(const_cast<wxStaticText *>(this));
008881c3
JS
148 wxFont font(GetFont());
149 if (!font.Ok())
150 font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
7a0e7e55 151
008881c3 152 dc.SetFont(font);
8812e098
VZ
153
154 wxCoord widthTextMax, heightTextTotal;
32cd189d 155 dc.GetMultiLineTextExtent(GetLabelText(), &widthTextMax, &heightTextTotal);
8812e098 156
37b53d2a 157#ifdef __WXWINCE__
8812e098
VZ
158 if ( widthTextMax )
159 widthTextMax += 2;
160#endif // __WXWINCE__
161
7a0e7e55
VZ
162 // border takes extra space
163 //
164 // TODO: this is probably not wxStaticText-specific and should be moved
165 wxCoord border;
166 switch ( GetBorder() )
167 {
168 case wxBORDER_STATIC:
169 case wxBORDER_SIMPLE:
170 border = 1;
171 break;
172
173 case wxBORDER_SUNKEN:
174 border = 2;
175 break;
176
177 case wxBORDER_RAISED:
178 case wxBORDER_DOUBLE:
179 border = 3;
180 break;
181
182 default:
183 wxFAIL_MSG( _T("unknown border style") );
184 // fall through
185
186 case wxBORDER_NONE:
187 border = 0;
188 }
189
190 widthTextMax += 2*border;
191 heightTextTotal += 2*border;
192
31582e4e
RD
193 wxSize best(widthTextMax, heightTextTotal);
194 CacheBestSize(best);
195 return best;
2bda0e17
KB
196}
197
1e3a888e
VZ
198void wxStaticText::DoSetSize(int x, int y, int w, int h, int sizeFlags)
199{
39bc0347 200 // note: we first need to set the size and _then_ call UpdateLabel
1e3a888e
VZ
201 wxStaticTextBase::DoSetSize(x, y, w, h, sizeFlags);
202
d6f2a891 203#ifdef SS_ENDELLIPSIS
39bc0347
VZ
204 // do we need to ellipsize the contents?
205 long styleReal = ::GetWindowLong(GetHwnd(), GWL_STYLE);
206 if ( !(styleReal & SS_ENDELLIPSIS) )
207 {
208 // we don't have SS_ENDELLIPSIS style:
209 // we need to (eventually) do ellipsization ourselves
210 UpdateLabel();
211 }
212 //else: we don't or the OS will do it for us
d6f2a891 213#endif // SS_ENDELLIPSIS
39bc0347
VZ
214
215 // we need to refresh the window after changing its size as the standard
216 // control doesn't always update itself properly
1e3a888e
VZ
217 Refresh();
218}
219
c0e6c051
RD
220void wxStaticText::SetLabel(const wxString& label)
221{
d6f2a891 222#ifdef SS_ENDELLIPSIS
39bc0347
VZ
223 long styleReal = ::GetWindowLong(GetHwnd(), GWL_STYLE);
224 if ( HasFlag(wxST_ELLIPSIZE_END) &&
225 wxGetOsVersion() == wxOS_WINDOWS_NT )
226 {
dc797d8e
JS
227 // adding SS_ENDELLIPSIS or SS_ENDELLIPSIS "disables" the correct
228 // newline handling in static texts: the newlines in the labels are
229 // shown as square. Thus we don't use it even on newer OS when
39bc0347
VZ
230 // the static label contains a newline.
231 if ( label.Contains(wxT('\n')) )
232 styleReal &= ~SS_ENDELLIPSIS;
233 else
234 styleReal |= SS_ENDELLIPSIS;
235
236 ::SetWindowLong(GetHwnd(), GWL_STYLE, styleReal);
237 }
238 else // style not supported natively
239 {
240 styleReal &= ~SS_ENDELLIPSIS;
241 ::SetWindowLong(GetHwnd(), GWL_STYLE, styleReal);
242 }
d6f2a891 243#endif // SS_ENDELLIPSIS
39bc0347
VZ
244
245 // this call will save the label in m_labelOrig and set it into this window
246 // (through wxWindow::SetLabel)
247 m_labelOrig = label;
248
d6f2a891
VZ
249#ifdef SS_ENDELLIPSIS
250 if ( styleReal & SS_ENDELLIPSIS )
39bc0347 251 DoSetLabel(RemoveMarkup(label));
d6f2a891
VZ
252 else
253#endif // SS_ENDELLIPSIS
254 DoSetLabel(GetEllipsizedLabelWithoutMarkup());
c0e6c051
RD
255
256 // adjust the size of the window to fit to the label unless autoresizing is
257 // disabled
39bc0347
VZ
258 if ( !HasFlag(wxST_NO_AUTORESIZE) &&
259 !IsEllipsized() ) // if ellipsize is ON, then we don't want to get resized!
c0e6c051 260 {
9f884528 261 InvalidateBestSize();
57f4f925
WS
262 DoSetSize(wxDefaultCoord, wxDefaultCoord, wxDefaultCoord, wxDefaultCoord,
263 wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT);
c0e6c051
RD
264 }
265}
266
c0e6c051
RD
267bool wxStaticText::SetFont(const wxFont& font)
268{
269 bool ret = wxControl::SetFont(font);
270
271 // adjust the size of the window to fit to the label unless autoresizing is
272 // disabled
39bc0347 273 if ( !HasFlag(wxST_NO_AUTORESIZE) )
c0e6c051 274 {
9f884528 275 InvalidateBestSize();
57f4f925
WS
276 DoSetSize(wxDefaultCoord, wxDefaultCoord, wxDefaultCoord, wxDefaultCoord,
277 wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT);
c0e6c051
RD
278 }
279
280 return ret;
281}
486fd225 282
39bc0347
VZ
283// for wxST_ELLIPSIZE_* support:
284
285wxString wxStaticText::DoGetLabel() const
286{
287 return wxGetWindowText(GetHwnd());
288}
289
290void wxStaticText::DoSetLabel(const wxString& str)
291{
292 SetWindowText(GetHwnd(), str.c_str());
293}
294
295
1e6feb95 296#endif // wxUSE_STATTEXT