]>
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 | |
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 |
34 | WX_DEFINE_FLAGS( wxStaticTextStyle ) |
35 | ||
3ff066a4 | 36 | wxBEGIN_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 | ||
69 | wxEND_FLAGS( wxStaticTextStyle ) | |
bc9fb572 | 70 | |
51741307 SC |
71 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticText, wxControl,"wx/stattext.h") |
72 | ||
3ff066a4 | 73 | wxBEGIN_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 | 76 | wxEND_PROPERTIES_TABLE() |
51741307 | 77 | |
3ff066a4 SC |
78 | wxBEGIN_HANDLERS_TABLE(wxStaticText) |
79 | wxEND_HANDLERS_TABLE() | |
51741307 | 80 | |
57f4f925 | 81 | wxCONSTRUCTOR_6( wxStaticText , wxWindow* , Parent , wxWindowID , Id , wxString , Label , wxPoint , Position , wxSize , Size , long , WindowStyle ) |
51741307 | 82 | #else |
2bda0e17 | 83 | IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxControl) |
51741307 | 84 | #endif |
2bda0e17 | 85 | |
6dd16e4f VZ |
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) | |
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 | ||
fab97536 VZ |
104 | // as we didn't pass the correct label to MSWCreateControl(), it didn't set |
105 | // the initial size correctly -- do it now | |
106 | InvalidateBestSize(); | |
107 | SetInitialSize(size); | |
108 | ||
8f23c287 FM |
109 | // NOTE: if the label contains ampersand characters which are interpreted as |
110 | // accelerators, they will be rendered (at least on WinXP) only if the | |
111 | // static text is placed inside a window class which correctly handles | |
112 | // focusing by TAB traversal (e.g. wxPanel). | |
113 | ||
57f4f925 | 114 | return true; |
6dd16e4f | 115 | } |
c085e333 | 116 | |
6dd16e4f VZ |
117 | WXDWORD wxStaticText::MSWGetStyle(long style, WXDWORD *exstyle) const |
118 | { | |
119 | WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle); | |
120 | ||
121 | // translate the alignment flags to the Windows ones | |
122 | // | |
123 | // note that both wxALIGN_LEFT and SS_LEFT are equal to 0 so we shouldn't | |
124 | // test for them using & operator | |
125 | if ( style & wxALIGN_CENTRE ) | |
126 | msStyle |= SS_CENTER; | |
127 | else if ( style & wxALIGN_RIGHT ) | |
128 | msStyle |= SS_RIGHT; | |
129 | else | |
130 | msStyle |= SS_LEFT; | |
131 | ||
d6f2a891 | 132 | #ifdef SS_ENDELLIPSIS |
42b1fb63 | 133 | // this style is necessary to receive mouse events |
39bc0347 VZ |
134 | // Win NT and later have the SS_ENDELLIPSIS style which is useful to us: |
135 | if (wxGetOsVersion() == wxOS_WINDOWS_NT) | |
136 | { | |
137 | // for now, add the SS_ENDELLIPSIS style if wxST_ELLIPSIZE_END is given; | |
138 | // we may need to remove it later in ::SetLabel() if the given label | |
139 | // has newlines | |
140 | if ( style & wxST_ELLIPSIZE_END ) | |
141 | msStyle |= SS_ENDELLIPSIS; | |
142 | } | |
d6f2a891 | 143 | #endif // SS_ENDELLIPSIS |
39bc0347 | 144 | |
42b1fb63 VZ |
145 | msStyle |= SS_NOTIFY; |
146 | ||
6dd16e4f | 147 | return msStyle; |
2bda0e17 KB |
148 | } |
149 | ||
743b4266 | 150 | wxSize wxStaticText::DoGetBestClientSize() const |
2bda0e17 | 151 | { |
5c33522f | 152 | wxClientDC dc(const_cast<wxStaticText *>(this)); |
008881c3 JS |
153 | wxFont font(GetFont()); |
154 | if (!font.Ok()) | |
155 | font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT); | |
7a0e7e55 | 156 | |
008881c3 | 157 | dc.SetFont(font); |
8812e098 VZ |
158 | |
159 | wxCoord widthTextMax, heightTextTotal; | |
32cd189d | 160 | dc.GetMultiLineTextExtent(GetLabelText(), &widthTextMax, &heightTextTotal); |
8812e098 | 161 | |
37b53d2a | 162 | #ifdef __WXWINCE__ |
8812e098 VZ |
163 | if ( widthTextMax ) |
164 | widthTextMax += 2; | |
165 | #endif // __WXWINCE__ | |
166 | ||
743b4266 | 167 | return wxSize(widthTextMax, heightTextTotal); |
2bda0e17 KB |
168 | } |
169 | ||
1e3a888e VZ |
170 | void wxStaticText::DoSetSize(int x, int y, int w, int h, int sizeFlags) |
171 | { | |
39bc0347 | 172 | // note: we first need to set the size and _then_ call UpdateLabel |
1e3a888e VZ |
173 | wxStaticTextBase::DoSetSize(x, y, w, h, sizeFlags); |
174 | ||
d6f2a891 | 175 | #ifdef SS_ENDELLIPSIS |
39bc0347 VZ |
176 | // do we need to ellipsize the contents? |
177 | long styleReal = ::GetWindowLong(GetHwnd(), GWL_STYLE); | |
178 | if ( !(styleReal & SS_ENDELLIPSIS) ) | |
179 | { | |
180 | // we don't have SS_ENDELLIPSIS style: | |
181 | // we need to (eventually) do ellipsization ourselves | |
182 | UpdateLabel(); | |
183 | } | |
184 | //else: we don't or the OS will do it for us | |
d6f2a891 | 185 | #endif // SS_ENDELLIPSIS |
39bc0347 VZ |
186 | |
187 | // we need to refresh the window after changing its size as the standard | |
188 | // control doesn't always update itself properly | |
1e3a888e VZ |
189 | Refresh(); |
190 | } | |
191 | ||
c0e6c051 RD |
192 | void wxStaticText::SetLabel(const wxString& label) |
193 | { | |
d6f2a891 | 194 | #ifdef SS_ENDELLIPSIS |
39bc0347 VZ |
195 | long styleReal = ::GetWindowLong(GetHwnd(), GWL_STYLE); |
196 | if ( HasFlag(wxST_ELLIPSIZE_END) && | |
197 | wxGetOsVersion() == wxOS_WINDOWS_NT ) | |
198 | { | |
dc797d8e JS |
199 | // adding SS_ENDELLIPSIS or SS_ENDELLIPSIS "disables" the correct |
200 | // newline handling in static texts: the newlines in the labels are | |
201 | // shown as square. Thus we don't use it even on newer OS when | |
39bc0347 VZ |
202 | // the static label contains a newline. |
203 | if ( label.Contains(wxT('\n')) ) | |
204 | styleReal &= ~SS_ENDELLIPSIS; | |
205 | else | |
206 | styleReal |= SS_ENDELLIPSIS; | |
207 | ||
208 | ::SetWindowLong(GetHwnd(), GWL_STYLE, styleReal); | |
209 | } | |
210 | else // style not supported natively | |
211 | { | |
212 | styleReal &= ~SS_ENDELLIPSIS; | |
213 | ::SetWindowLong(GetHwnd(), GWL_STYLE, styleReal); | |
214 | } | |
d6f2a891 | 215 | #endif // SS_ENDELLIPSIS |
39bc0347 | 216 | |
32ee98eb FM |
217 | // save the label in m_labelOrig with both the markup (if any) and |
218 | // the mnemonics characters (if any) | |
39bc0347 VZ |
219 | m_labelOrig = label; |
220 | ||
d6f2a891 VZ |
221 | #ifdef SS_ENDELLIPSIS |
222 | if ( styleReal & SS_ENDELLIPSIS ) | |
32ee98eb | 223 | DoSetLabel(GetLabelWithoutMarkup()); |
d6f2a891 VZ |
224 | else |
225 | #endif // SS_ENDELLIPSIS | |
226 | DoSetLabel(GetEllipsizedLabelWithoutMarkup()); | |
c0e6c051 RD |
227 | |
228 | // adjust the size of the window to fit to the label unless autoresizing is | |
229 | // disabled | |
39bc0347 VZ |
230 | if ( !HasFlag(wxST_NO_AUTORESIZE) && |
231 | !IsEllipsized() ) // if ellipsize is ON, then we don't want to get resized! | |
c0e6c051 | 232 | { |
9f884528 | 233 | InvalidateBestSize(); |
57f4f925 WS |
234 | DoSetSize(wxDefaultCoord, wxDefaultCoord, wxDefaultCoord, wxDefaultCoord, |
235 | wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT); | |
c0e6c051 RD |
236 | } |
237 | } | |
238 | ||
c0e6c051 RD |
239 | bool wxStaticText::SetFont(const wxFont& font) |
240 | { | |
241 | bool ret = wxControl::SetFont(font); | |
242 | ||
243 | // adjust the size of the window to fit to the label unless autoresizing is | |
244 | // disabled | |
39bc0347 | 245 | if ( !HasFlag(wxST_NO_AUTORESIZE) ) |
c0e6c051 | 246 | { |
9f884528 | 247 | InvalidateBestSize(); |
57f4f925 WS |
248 | DoSetSize(wxDefaultCoord, wxDefaultCoord, wxDefaultCoord, wxDefaultCoord, |
249 | wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT); | |
c0e6c051 RD |
250 | } |
251 | ||
252 | return ret; | |
253 | } | |
486fd225 | 254 | |
39bc0347 VZ |
255 | // for wxST_ELLIPSIZE_* support: |
256 | ||
257 | wxString wxStaticText::DoGetLabel() const | |
258 | { | |
259 | return wxGetWindowText(GetHwnd()); | |
260 | } | |
261 | ||
262 | void wxStaticText::DoSetLabel(const wxString& str) | |
263 | { | |
264 | SetWindowText(GetHwnd(), str.c_str()); | |
265 | } | |
266 | ||
267 | ||
1e6feb95 | 268 | #endif // wxUSE_STATTEXT |