]> git.saurik.com Git - wxWidgets.git/blame - src/msw/stattext.cpp
For wxGTK2, link with X11 explicitly, since we use many X11 functions directly.
[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
6dd16e4f
VZ
33bool wxStaticText::Create(wxWindow *parent,
34 wxWindowID id,
35 const wxString& label,
36 const wxPoint& pos,
37 const wxSize& size,
38 long style,
39 const wxString& name)
2bda0e17 40{
6dd16e4f 41 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
57f4f925 42 return false;
2bda0e17 43
8f23c287 44 if ( !MSWCreateControl(wxT("STATIC"), wxEmptyString, pos, size) )
57f4f925 45 return false;
2bda0e17 46
8f23c287
FM
47 // we set the label here and not through MSWCreateControl() because we
48 // need to do many operation on it for ellipsization&markup support
49 SetLabel(label);
50
fab97536
VZ
51 // as we didn't pass the correct label to MSWCreateControl(), it didn't set
52 // the initial size correctly -- do it now
53 InvalidateBestSize();
54 SetInitialSize(size);
55
8f23c287
FM
56 // NOTE: if the label contains ampersand characters which are interpreted as
57 // accelerators, they will be rendered (at least on WinXP) only if the
58 // static text is placed inside a window class which correctly handles
59 // focusing by TAB traversal (e.g. wxPanel).
60
57f4f925 61 return true;
6dd16e4f 62}
c085e333 63
6dd16e4f
VZ
64WXDWORD wxStaticText::MSWGetStyle(long style, WXDWORD *exstyle) const
65{
66 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
67
68 // translate the alignment flags to the Windows ones
69 //
70 // note that both wxALIGN_LEFT and SS_LEFT are equal to 0 so we shouldn't
71 // test for them using & operator
72 if ( style & wxALIGN_CENTRE )
73 msStyle |= SS_CENTER;
74 else if ( style & wxALIGN_RIGHT )
75 msStyle |= SS_RIGHT;
76 else
77 msStyle |= SS_LEFT;
78
d6f2a891 79#ifdef SS_ENDELLIPSIS
42b1fb63 80 // this style is necessary to receive mouse events
39bc0347
VZ
81 // Win NT and later have the SS_ENDELLIPSIS style which is useful to us:
82 if (wxGetOsVersion() == wxOS_WINDOWS_NT)
83 {
84 // for now, add the SS_ENDELLIPSIS style if wxST_ELLIPSIZE_END is given;
85 // we may need to remove it later in ::SetLabel() if the given label
86 // has newlines
87 if ( style & wxST_ELLIPSIZE_END )
88 msStyle |= SS_ENDELLIPSIS;
89 }
d6f2a891 90#endif // SS_ENDELLIPSIS
39bc0347 91
42b1fb63
VZ
92 msStyle |= SS_NOTIFY;
93
6dd16e4f 94 return msStyle;
2bda0e17
KB
95}
96
743b4266 97wxSize wxStaticText::DoGetBestClientSize() const
2bda0e17 98{
5c33522f 99 wxClientDC dc(const_cast<wxStaticText *>(this));
008881c3
JS
100 wxFont font(GetFont());
101 if (!font.Ok())
102 font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
7a0e7e55 103
008881c3 104 dc.SetFont(font);
8812e098
VZ
105
106 wxCoord widthTextMax, heightTextTotal;
32cd189d 107 dc.GetMultiLineTextExtent(GetLabelText(), &widthTextMax, &heightTextTotal);
8812e098 108
37b53d2a 109#ifdef __WXWINCE__
8812e098
VZ
110 if ( widthTextMax )
111 widthTextMax += 2;
112#endif // __WXWINCE__
113
e279a9e7
VZ
114 // It looks like the static control needs "slightly" more vertical space
115 // than the character height and while the text isn't actually truncated if
116 // we use just the minimal height, it is positioned differently than when
117 // the control has enough space and this result in the text in edit and
118 // static controls not being aligned when the controls themselves are. As
119 // this is something you really should be able to count on, increase the
120 // space allocated for the control so that the base lines do align
121 // correctly. Notice that while the above is true at least for the single
122 // line controls, there doesn't seem to do any harm to allocate two extra
123 // pixels in multi-line case neither so do it always for consistency.
124 //
125 // I still have no idea why exactly is this needed nor why should we use 2
126 // and not something else. This seems to work in all the configurations
127 // though (small/large fonts, different OS versions, ...) so just hard code
128 // it for now. If we need something better later it might be worth looking
129 // at the height of the text control returned by ::GetComboBoxInfo() as it
130 // seems to be the "minimal acceptable" height.
131 heightTextTotal += 2;
132
743b4266 133 return wxSize(widthTextMax, heightTextTotal);
2bda0e17
KB
134}
135
1e3a888e
VZ
136void wxStaticText::DoSetSize(int x, int y, int w, int h, int sizeFlags)
137{
39bc0347 138 // note: we first need to set the size and _then_ call UpdateLabel
1e3a888e
VZ
139 wxStaticTextBase::DoSetSize(x, y, w, h, sizeFlags);
140
d6f2a891 141#ifdef SS_ENDELLIPSIS
39bc0347
VZ
142 // do we need to ellipsize the contents?
143 long styleReal = ::GetWindowLong(GetHwnd(), GWL_STYLE);
144 if ( !(styleReal & SS_ENDELLIPSIS) )
145 {
146 // we don't have SS_ENDELLIPSIS style:
147 // we need to (eventually) do ellipsization ourselves
148 UpdateLabel();
149 }
150 //else: we don't or the OS will do it for us
d6f2a891 151#endif // SS_ENDELLIPSIS
39bc0347
VZ
152
153 // we need to refresh the window after changing its size as the standard
154 // control doesn't always update itself properly
1e3a888e
VZ
155 Refresh();
156}
157
c0e6c051
RD
158void wxStaticText::SetLabel(const wxString& label)
159{
d6f2a891 160#ifdef SS_ENDELLIPSIS
39bc0347
VZ
161 long styleReal = ::GetWindowLong(GetHwnd(), GWL_STYLE);
162 if ( HasFlag(wxST_ELLIPSIZE_END) &&
163 wxGetOsVersion() == wxOS_WINDOWS_NT )
164 {
dc797d8e
JS
165 // adding SS_ENDELLIPSIS or SS_ENDELLIPSIS "disables" the correct
166 // newline handling in static texts: the newlines in the labels are
167 // shown as square. Thus we don't use it even on newer OS when
39bc0347
VZ
168 // the static label contains a newline.
169 if ( label.Contains(wxT('\n')) )
170 styleReal &= ~SS_ENDELLIPSIS;
171 else
172 styleReal |= SS_ENDELLIPSIS;
173
174 ::SetWindowLong(GetHwnd(), GWL_STYLE, styleReal);
175 }
176 else // style not supported natively
177 {
178 styleReal &= ~SS_ENDELLIPSIS;
179 ::SetWindowLong(GetHwnd(), GWL_STYLE, styleReal);
180 }
d6f2a891 181#endif // SS_ENDELLIPSIS
39bc0347 182
ce00f59b 183 // save the label in m_labelOrig with both the markup (if any) and
32ee98eb 184 // the mnemonics characters (if any)
39bc0347
VZ
185 m_labelOrig = label;
186
d6f2a891
VZ
187#ifdef SS_ENDELLIPSIS
188 if ( styleReal & SS_ENDELLIPSIS )
3da9cffc 189 DoSetLabel(GetLabel());
d6f2a891
VZ
190 else
191#endif // SS_ENDELLIPSIS
3da9cffc 192 DoSetLabel(GetEllipsizedLabel());
c0e6c051
RD
193
194 // adjust the size of the window to fit to the label unless autoresizing is
195 // disabled
39bc0347
VZ
196 if ( !HasFlag(wxST_NO_AUTORESIZE) &&
197 !IsEllipsized() ) // if ellipsize is ON, then we don't want to get resized!
c0e6c051 198 {
9f884528 199 InvalidateBestSize();
57f4f925
WS
200 DoSetSize(wxDefaultCoord, wxDefaultCoord, wxDefaultCoord, wxDefaultCoord,
201 wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT);
c0e6c051
RD
202 }
203}
204
c0e6c051
RD
205bool wxStaticText::SetFont(const wxFont& font)
206{
207 bool ret = wxControl::SetFont(font);
208
209 // adjust the size of the window to fit to the label unless autoresizing is
210 // disabled
39bc0347 211 if ( !HasFlag(wxST_NO_AUTORESIZE) )
c0e6c051 212 {
9f884528 213 InvalidateBestSize();
57f4f925
WS
214 DoSetSize(wxDefaultCoord, wxDefaultCoord, wxDefaultCoord, wxDefaultCoord,
215 wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT);
c0e6c051
RD
216 }
217
218 return ret;
219}
486fd225 220
39bc0347
VZ
221// for wxST_ELLIPSIZE_* support:
222
223wxString wxStaticText::DoGetLabel() const
224{
225 return wxGetWindowText(GetHwnd());
226}
227
228void wxStaticText::DoSetLabel(const wxString& str)
229{
230 SetWindowText(GetHwnd(), str.c_str());
231}
232
233
1e6feb95 234#endif // wxUSE_STATTEXT