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