]>
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 /////////////////////////////////////////////////////////////////////////////
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 IMPLEMENT_DYNAMIC_CLASS(wxStaticText
, wxControl
)
37 bool wxStaticText::Create(wxWindow
*parent
,
39 const wxString
& label
,
45 // By default, a static text should have no border.
46 if ((style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
47 style
|= wxBORDER_NONE
;
49 if ( !CreateControl(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
52 if ( !MSWCreateControl(wxT("STATIC"), label
, pos
, size
) )
58 WXDWORD
wxStaticText::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
60 WXDWORD msStyle
= wxControl::MSWGetStyle(style
, exstyle
);
62 // translate the alignment flags to the Windows ones
64 // note that both wxALIGN_LEFT and SS_LEFT are equal to 0 so we shouldn't
65 // test for them using & operator
66 if ( style
& wxALIGN_CENTRE
)
68 else if ( style
& wxALIGN_RIGHT
)
76 wxSize
wxStaticText::DoGetBestSize() const
78 wxString
text(wxGetWindowText(GetHWND()));
80 int widthTextMax
= 0, widthLine
,
81 heightTextTotal
= 0, heightLineDefault
= 0, heightLine
= 0;
83 bool lastWasAmpersand
= FALSE
;
86 for ( const wxChar
*pc
= text
; ; pc
++ )
88 if ( *pc
== wxT('\n') || *pc
== wxT('\0') )
92 // we can't use GetTextExtent - it will return 0 for both width
93 // and height and an empty line should count in height
95 if ( !heightLineDefault
)
96 heightLineDefault
= heightLine
;
97 if ( !heightLineDefault
)
98 GetTextExtent(_T("W"), NULL
, &heightLineDefault
);
100 heightTextTotal
+= heightLineDefault
;
104 GetTextExtent(curLine
, &widthLine
, &heightLine
);
105 if ( widthLine
> widthTextMax
)
106 widthTextMax
= widthLine
;
107 heightTextTotal
+= heightLine
;
110 if ( *pc
== wxT('\n') )
122 // we shouldn't take into account the '&' which just introduces the
123 // mnemonic characters and so are not shown on the screen -- except
124 // when it is preceded by another '&' in which case it stands for a
126 if ( *pc
== _T('&') )
128 if ( !lastWasAmpersand
)
130 lastWasAmpersand
= TRUE
;
132 // skip the statement adding pc to curLine below
136 // it is a literal ampersand
137 lastWasAmpersand
= FALSE
;
144 return wxSize(widthTextMax
, heightTextTotal
);
147 void wxStaticText::DoSetSize(int x
, int y
, int w
, int h
, int sizeFlags
)
149 // we need to refresh the window after changing its size as the standard
150 // control doesn't always update itself properly
151 wxStaticTextBase::DoSetSize(x
, y
, w
, h
, sizeFlags
);
156 void wxStaticText::SetLabel(const wxString
& label
)
158 wxStaticTextBase::SetLabel(label
);
160 // adjust the size of the window to fit to the label unless autoresizing is
162 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE
) )
164 DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH
| wxSIZE_AUTO_HEIGHT
);
169 bool wxStaticText::SetFont(const wxFont
& font
)
171 bool ret
= wxControl::SetFont(font
);
173 // adjust the size of the window to fit to the label unless autoresizing is
175 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE
) )
177 DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH
| wxSIZE_AUTO_HEIGHT
);
183 #endif // wxUSE_STATTEXT