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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "stattext.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
31 #include "wx/stattext.h"
32 #include "wx/msw/private.h"
35 #if wxUSE_EXTENDED_RTTI
36 WX_DEFINE_FLAGS( wxStaticTextStyle
)
38 WX_BEGIN_FLAGS( wxStaticTextStyle
)
39 // new style border flags, we put them first to
40 // use them for streaming out
41 WX_FLAGS_MEMBER(wxBORDER_SIMPLE
)
42 WX_FLAGS_MEMBER(wxBORDER_SUNKEN
)
43 WX_FLAGS_MEMBER(wxBORDER_DOUBLE
)
44 WX_FLAGS_MEMBER(wxBORDER_RAISED
)
45 WX_FLAGS_MEMBER(wxBORDER_STATIC
)
46 WX_FLAGS_MEMBER(wxBORDER_NONE
)
48 // old style border flags
49 WX_FLAGS_MEMBER(wxSIMPLE_BORDER
)
50 WX_FLAGS_MEMBER(wxSUNKEN_BORDER
)
51 WX_FLAGS_MEMBER(wxDOUBLE_BORDER
)
52 WX_FLAGS_MEMBER(wxRAISED_BORDER
)
53 WX_FLAGS_MEMBER(wxSTATIC_BORDER
)
54 WX_FLAGS_MEMBER(wxNO_BORDER
)
56 // standard window styles
57 WX_FLAGS_MEMBER(wxTAB_TRAVERSAL
)
58 WX_FLAGS_MEMBER(wxCLIP_CHILDREN
)
59 WX_FLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
60 WX_FLAGS_MEMBER(wxWANTS_CHARS
)
61 WX_FLAGS_MEMBER(wxNO_FULL_REPAINT_ON_RESIZE
)
62 WX_FLAGS_MEMBER(wxALWAYS_SHOW_SB
)
63 WX_FLAGS_MEMBER(wxVSCROLL
)
64 WX_FLAGS_MEMBER(wxHSCROLL
)
66 WX_FLAGS_MEMBER(wxST_NO_AUTORESIZE
)
67 WX_FLAGS_MEMBER(wxALIGN_LEFT
)
68 WX_FLAGS_MEMBER(wxALIGN_RIGHT
)
69 WX_FLAGS_MEMBER(wxALIGN_CENTRE
)
71 WX_END_FLAGS( wxStaticTextStyle
)
73 IMPLEMENT_DYNAMIC_CLASS_XTI(wxStaticText
, wxControl
,"wx/stattext.h")
75 WX_BEGIN_PROPERTIES_TABLE(wxStaticText
)
76 WX_PROPERTY( Label
,wxString
, SetLabel
, GetLabel
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
77 WX_PROPERTY_FLAGS( WindowStyle
, wxStaticTextStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
78 WX_END_PROPERTIES_TABLE()
80 WX_BEGIN_HANDLERS_TABLE(wxStaticText
)
81 WX_END_HANDLERS_TABLE()
83 WX_CONSTRUCTOR_6( wxStaticText
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Label
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
85 IMPLEMENT_DYNAMIC_CLASS(wxStaticText
, wxControl
)
88 bool wxStaticText::Create(wxWindow
*parent
,
90 const wxString
& label
,
96 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
99 if ( !MSWCreateControl(wxT("STATIC"), label
, pos
, size
) )
105 wxBorder
wxStaticText::GetDefaultBorder() const
107 return wxBORDER_NONE
;
110 WXDWORD
wxStaticText::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
112 WXDWORD msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
114 // translate the alignment flags to the Windows ones
116 // note that both wxALIGN_LEFT and SS_LEFT are equal to 0 so we shouldn't
117 // test for them using & operator
118 if ( style
& wxALIGN_CENTRE
)
119 msStyle
|= SS_CENTER
;
120 else if ( style
& wxALIGN_RIGHT
)
128 wxSize
wxStaticText::DoGetBestSize() const
130 wxString
text(wxGetWindowText(GetHWND()));
132 int widthTextMax
= 0, widthLine
,
133 heightTextTotal
= 0, heightLineDefault
= 0, heightLine
= 0;
135 bool lastWasAmpersand
= FALSE
;
138 for ( const wxChar
*pc
= text
; ; pc
++ )
140 if ( *pc
== wxT('\n') || *pc
== wxT('\0') )
144 // we can't use GetTextExtent - it will return 0 for both width
145 // and height and an empty line should count in height
147 if ( !heightLineDefault
)
148 heightLineDefault
= heightLine
;
149 if ( !heightLineDefault
)
150 GetTextExtent(_T("W"), NULL
, &heightLineDefault
);
152 heightTextTotal
+= heightLineDefault
;
156 GetTextExtent(curLine
, &widthLine
, &heightLine
);
157 if ( widthLine
> widthTextMax
)
158 widthTextMax
= widthLine
;
159 heightTextTotal
+= heightLine
;
162 if ( *pc
== wxT('\n') )
174 // we shouldn't take into account the '&' which just introduces the
175 // mnemonic characters and so are not shown on the screen -- except
176 // when it is preceded by another '&' in which case it stands for a
178 if ( *pc
== _T('&') )
180 if ( !lastWasAmpersand
)
182 lastWasAmpersand
= TRUE
;
184 // skip the statement adding pc to curLine below
188 // it is a literal ampersand
189 lastWasAmpersand
= FALSE
;
196 return wxSize(widthTextMax
, heightTextTotal
);
199 void wxStaticText::DoSetSize(int x
, int y
, int w
, int h
, int sizeFlags
)
201 // we need to refresh the window after changing its size as the standard
202 // control doesn't always update itself properly
203 wxStaticTextBase::DoSetSize(x
, y
, w
, h
, sizeFlags
);
208 void wxStaticText::SetLabel(const wxString
& label
)
210 wxStaticTextBase::SetLabel(label
);
212 // adjust the size of the window to fit to the label unless autoresizing is
214 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE
) )
216 DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH
| wxSIZE_AUTO_HEIGHT
);
221 bool wxStaticText::SetFont(const wxFont
& font
)
223 bool ret
= wxControl::SetFont(font
);
225 // adjust the size of the window to fit to the label unless autoresizing is
227 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE
) )
229 DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH
| wxSIZE_AUTO_HEIGHT
);
235 #endif // wxUSE_STATTEXT