]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/stattext.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/stattext.cpp
3 // Purpose: wxStaticText
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/stattext.h"
27 #include "wx/dcclient.h"
28 #include "wx/settings.h"
31 #include "wx/msw/private.h"
33 #if wxUSE_EXTENDED_RTTI
34 WX_DEFINE_FLAGS( wxStaticTextStyle
)
36 wxBEGIN_FLAGS( wxStaticTextStyle
)
37 // new style border flags, we put them first to
38 // use them for streaming out
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
)
46 // old style border flags
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
)
52 wxFLAGS_MEMBER(wxBORDER
)
54 // standard window styles
55 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
56 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
57 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
58 wxFLAGS_MEMBER(wxWANTS_CHARS
)
59 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
60 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
61 wxFLAGS_MEMBER(wxVSCROLL
)
62 wxFLAGS_MEMBER(wxHSCROLL
)
64 wxFLAGS_MEMBER(wxST_NO_AUTORESIZE
)
65 wxFLAGS_MEMBER(wxALIGN_LEFT
)
66 wxFLAGS_MEMBER(wxALIGN_RIGHT
)
67 wxFLAGS_MEMBER(wxALIGN_CENTRE
)
69 wxEND_FLAGS( wxStaticTextStyle
)
71 IMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticText
, wxControl
,"wx/stattext.h")
73 wxBEGIN_PROPERTIES_TABLE(wxStaticText
)
74 wxPROPERTY( Label
,wxString
, SetLabel
, GetLabel
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
75 wxPROPERTY_FLAGS( WindowStyle
, wxStaticTextStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
76 wxEND_PROPERTIES_TABLE()
78 wxBEGIN_HANDLERS_TABLE(wxStaticText
)
79 wxEND_HANDLERS_TABLE()
81 wxCONSTRUCTOR_6( wxStaticText
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Label
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
83 IMPLEMENT_DYNAMIC_CLASS(wxStaticText
, wxControl
)
86 bool wxStaticText::Create(wxWindow
*parent
,
88 const wxString
& label
,
94 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
97 if ( !MSWCreateControl(wxT("STATIC"), wxEmptyString
, pos
, size
) )
100 // we set the label here and not through MSWCreateControl() because we
101 // need to do many operation on it for ellipsization&markup support
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
);
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).
117 WXDWORD
wxStaticText::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
119 WXDWORD msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
121 // translate the alignment flags to the Windows ones
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
)
132 #ifdef SS_ENDELLIPSIS
133 // this style is necessary to receive mouse events
134 // Win NT and later have the SS_ENDELLIPSIS style which is useful to us:
135 if (wxGetOsVersion() == wxOS_WINDOWS_NT
)
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
140 if ( style
& wxST_ELLIPSIZE_END
)
141 msStyle
|= SS_ENDELLIPSIS
;
143 #endif // SS_ENDELLIPSIS
145 msStyle
|= SS_NOTIFY
;
150 wxSize
wxStaticText::DoGetBestClientSize() const
152 wxClientDC
dc(const_cast<wxStaticText
*>(this));
153 wxFont
font(GetFont());
155 font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
159 wxCoord widthTextMax
, heightTextTotal
;
160 dc
.GetMultiLineTextExtent(GetLabelText(), &widthTextMax
, &heightTextTotal
);
165 #endif // __WXWINCE__
167 // It looks like the static control needs "slightly" more vertical space
168 // than the character height and while the text isn't actually truncated if
169 // we use just the minimal height, it is positioned differently than when
170 // the control has enough space and this result in the text in edit and
171 // static controls not being aligned when the controls themselves are. As
172 // this is something you really should be able to count on, increase the
173 // space allocated for the control so that the base lines do align
174 // correctly. Notice that while the above is true at least for the single
175 // line controls, there doesn't seem to do any harm to allocate two extra
176 // pixels in multi-line case neither so do it always for consistency.
178 // I still have no idea why exactly is this needed nor why should we use 2
179 // and not something else. This seems to work in all the configurations
180 // though (small/large fonts, different OS versions, ...) so just hard code
181 // it for now. If we need something better later it might be worth looking
182 // at the height of the text control returned by ::GetComboBoxInfo() as it
183 // seems to be the "minimal acceptable" height.
184 heightTextTotal
+= 2;
186 return wxSize(widthTextMax
, heightTextTotal
);
189 void wxStaticText::DoSetSize(int x
, int y
, int w
, int h
, int sizeFlags
)
191 // note: we first need to set the size and _then_ call UpdateLabel
192 wxStaticTextBase::DoSetSize(x
, y
, w
, h
, sizeFlags
);
194 #ifdef SS_ENDELLIPSIS
195 // do we need to ellipsize the contents?
196 long styleReal
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
197 if ( !(styleReal
& SS_ENDELLIPSIS
) )
199 // we don't have SS_ENDELLIPSIS style:
200 // we need to (eventually) do ellipsization ourselves
203 //else: we don't or the OS will do it for us
204 #endif // SS_ENDELLIPSIS
206 // we need to refresh the window after changing its size as the standard
207 // control doesn't always update itself properly
211 void wxStaticText::SetLabel(const wxString
& label
)
213 #ifdef SS_ENDELLIPSIS
214 long styleReal
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
215 if ( HasFlag(wxST_ELLIPSIZE_END
) &&
216 wxGetOsVersion() == wxOS_WINDOWS_NT
)
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
;
225 styleReal
|= SS_ENDELLIPSIS
;
227 ::SetWindowLong(GetHwnd(), GWL_STYLE
, styleReal
);
229 else // style not supported natively
231 styleReal
&= ~SS_ENDELLIPSIS
;
232 ::SetWindowLong(GetHwnd(), GWL_STYLE
, styleReal
);
234 #endif // SS_ENDELLIPSIS
236 // save the label in m_labelOrig with both the markup (if any) and
237 // the mnemonics characters (if any)
240 #ifdef SS_ENDELLIPSIS
241 if ( styleReal
& SS_ENDELLIPSIS
)
242 DoSetLabel(GetLabelWithoutMarkup());
244 #endif // SS_ENDELLIPSIS
245 DoSetLabel(GetEllipsizedLabelWithoutMarkup());
247 // adjust the size of the window to fit to the label unless autoresizing is
249 if ( !HasFlag(wxST_NO_AUTORESIZE
) &&
250 !IsEllipsized() ) // if ellipsize is ON, then we don't want to get resized!
252 InvalidateBestSize();
253 DoSetSize(wxDefaultCoord
, wxDefaultCoord
, wxDefaultCoord
, wxDefaultCoord
,
254 wxSIZE_AUTO_WIDTH
| wxSIZE_AUTO_HEIGHT
);
258 bool wxStaticText::SetFont(const wxFont
& font
)
260 bool ret
= wxControl::SetFont(font
);
262 // adjust the size of the window to fit to the label unless autoresizing is
264 if ( !HasFlag(wxST_NO_AUTORESIZE
) )
266 InvalidateBestSize();
267 DoSetSize(wxDefaultCoord
, wxDefaultCoord
, wxDefaultCoord
, wxDefaultCoord
,
268 wxSIZE_AUTO_WIDTH
| wxSIZE_AUTO_HEIGHT
);
274 // for wxST_ELLIPSIZE_* support:
276 wxString
wxStaticText::DoGetLabel() const
278 return wxGetWindowText(GetHwnd());
281 void wxStaticText::DoSetLabel(const wxString
& str
)
283 SetWindowText(GetHwnd(), str
.c_str());
287 #endif // wxUSE_STATTEXT