]>
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 // 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).
112 WXDWORD
wxStaticText::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
114 WXDWORD msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
116 // translate the alignment flags to the Windows ones
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
)
127 #ifdef SS_ENDELLIPSIS
128 // this style is necessary to receive mouse events
129 // Win NT and later have the SS_ENDELLIPSIS style which is useful to us:
130 if (wxGetOsVersion() == wxOS_WINDOWS_NT
)
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
135 if ( style
& wxST_ELLIPSIZE_END
)
136 msStyle
|= SS_ENDELLIPSIS
;
138 #endif // SS_ENDELLIPSIS
140 msStyle
|= SS_NOTIFY
;
145 wxSize
wxStaticText::DoGetBestSize() const
147 wxClientDC
dc(const_cast<wxStaticText
*>(this));
148 wxFont
font(GetFont());
150 font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
154 wxCoord widthTextMax
, heightTextTotal
;
155 dc
.GetMultiLineTextExtent(GetLabelText(), &widthTextMax
, &heightTextTotal
);
160 #endif // __WXWINCE__
162 // border takes extra space
164 // TODO: this is probably not wxStaticText-specific and should be moved
166 switch ( GetBorder() )
168 case wxBORDER_STATIC
:
169 case wxBORDER_SIMPLE
:
173 case wxBORDER_SUNKEN
:
177 case wxBORDER_RAISED
:
178 case wxBORDER_DOUBLE
:
183 wxFAIL_MSG( _T("unknown border style") );
190 widthTextMax
+= 2*border
;
191 heightTextTotal
+= 2*border
;
193 wxSize
best(widthTextMax
, heightTextTotal
);
198 void wxStaticText::DoSetSize(int x
, int y
, int w
, int h
, int sizeFlags
)
200 // note: we first need to set the size and _then_ call UpdateLabel
201 wxStaticTextBase::DoSetSize(x
, y
, w
, h
, sizeFlags
);
203 #ifdef SS_ENDELLIPSIS
204 // do we need to ellipsize the contents?
205 long styleReal
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
206 if ( !(styleReal
& SS_ENDELLIPSIS
) )
208 // we don't have SS_ENDELLIPSIS style:
209 // we need to (eventually) do ellipsization ourselves
212 //else: we don't or the OS will do it for us
213 #endif // SS_ENDELLIPSIS
215 // we need to refresh the window after changing its size as the standard
216 // control doesn't always update itself properly
220 void wxStaticText::SetLabel(const wxString
& label
)
222 #ifdef SS_ENDELLIPSIS
223 long styleReal
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
224 if ( HasFlag(wxST_ELLIPSIZE_END
) &&
225 wxGetOsVersion() == wxOS_WINDOWS_NT
)
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
230 // the static label contains a newline.
231 if ( label
.Contains(wxT('\n')) )
232 styleReal
&= ~SS_ENDELLIPSIS
;
234 styleReal
|= SS_ENDELLIPSIS
;
236 ::SetWindowLong(GetHwnd(), GWL_STYLE
, styleReal
);
238 else // style not supported natively
240 styleReal
&= ~SS_ENDELLIPSIS
;
241 ::SetWindowLong(GetHwnd(), GWL_STYLE
, styleReal
);
243 #endif // SS_ENDELLIPSIS
245 // this call will save the label in m_labelOrig and set it into this window
246 // (through wxWindow::SetLabel)
249 #ifdef SS_ENDELLIPSIS
250 if ( styleReal
& SS_ENDELLIPSIS
)
251 DoSetLabel(RemoveMarkup(label
));
253 #endif // SS_ENDELLIPSIS
254 DoSetLabel(GetEllipsizedLabelWithoutMarkup());
256 // adjust the size of the window to fit to the label unless autoresizing is
258 if ( !HasFlag(wxST_NO_AUTORESIZE
) &&
259 !IsEllipsized() ) // if ellipsize is ON, then we don't want to get resized!
261 InvalidateBestSize();
262 DoSetSize(wxDefaultCoord
, wxDefaultCoord
, wxDefaultCoord
, wxDefaultCoord
,
263 wxSIZE_AUTO_WIDTH
| wxSIZE_AUTO_HEIGHT
);
267 bool wxStaticText::SetFont(const wxFont
& font
)
269 bool ret
= wxControl::SetFont(font
);
271 // adjust the size of the window to fit to the label unless autoresizing is
273 if ( !HasFlag(wxST_NO_AUTORESIZE
) )
275 InvalidateBestSize();
276 DoSetSize(wxDefaultCoord
, wxDefaultCoord
, wxDefaultCoord
, wxDefaultCoord
,
277 wxSIZE_AUTO_WIDTH
| wxSIZE_AUTO_HEIGHT
);
283 // for wxST_ELLIPSIZE_* support:
285 wxString
wxStaticText::DoGetLabel() const
287 return wxGetWindowText(GetHwnd());
290 void wxStaticText::DoSetLabel(const wxString
& str
)
292 SetWindowText(GetHwnd(), str
.c_str());
296 #endif // wxUSE_STATTEXT