]>
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 and Markus Holzem
9 // Licence: wxWindows license
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
, wxWindowID id
,
38 const wxString
& label
,
45 if (parent
) parent
->AddChild(this);
47 SetBackgroundColour(parent
->GetBackgroundColour()) ;
48 SetForegroundColour(parent
->GetForegroundColour()) ;
51 m_windowId
= (int)NewControlId();
60 m_windowStyle
= style
;
62 long msStyle
= WS_CHILD
| WS_VISIBLE
;
64 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
65 msStyle
|= WS_CLIPSIBLINGS
;
66 if (m_windowStyle
& wxALIGN_CENTRE
)
68 else if (m_windowStyle
& wxALIGN_RIGHT
)
73 // Even with extended styles, need to combine with WS_BORDER
74 // for them to look right.
75 if ( wxStyleHasBorder(m_windowStyle
) )
78 m_hWnd
= (WXHWND
)::CreateWindowEx(MakeExtendedStyle(m_windowStyle
), wxT("STATIC"), (const wxChar
*)label
,
80 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
81 wxGetInstance(), NULL
);
83 wxCHECK_MSG( m_hWnd
, FALSE
, wxT("Failed to create static ctrl") );
87 wxControl::SetFont(parent
->GetFont());
88 SetSize(x
, y
, width
, height
);
93 wxSize
wxStaticText::DoGetBestSize() const
95 wxString
text(wxGetWindowText(GetHWND()));
97 int widthTextMax
= 0, widthLine
,
98 heightTextTotal
= 0, heightLineDefault
= 0, heightLine
= 0;
101 for ( const wxChar
*pc
= text
; ; pc
++ ) {
102 if ( *pc
== wxT('\n') || *pc
== wxT('\0') ) {
104 // we can't use GetTextExtent - it will return 0 for both width
105 // and height and an empty line should count in height
107 if ( !heightLineDefault
)
108 heightLineDefault
= heightLine
;
109 if ( !heightLineDefault
)
110 GetTextExtent(_T("W"), NULL
, &heightLineDefault
);
112 heightTextTotal
+= heightLineDefault
;
115 GetTextExtent(curLine
, &widthLine
, &heightLine
);
116 if ( widthLine
> widthTextMax
)
117 widthTextMax
= widthLine
;
118 heightTextTotal
+= heightLine
;
121 if ( *pc
== wxT('\n') ) {
134 return wxSize(widthTextMax
, heightTextTotal
);
137 void wxStaticText::DoSetSize(int x
, int y
, int w
, int h
, int sizeFlags
)
139 // we need to refresh the window after changing its size as the standard
140 // control doesn't always update itself properly
141 wxStaticTextBase::DoSetSize(x
, y
, w
, h
, sizeFlags
);
146 void wxStaticText::SetLabel(const wxString
& label
)
148 wxStaticTextBase::SetLabel(label
);
150 // adjust the size of the window to fit to the label unless autoresizing is
152 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE
) )
154 DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH
| wxSIZE_AUTO_HEIGHT
);
159 bool wxStaticText::SetFont(const wxFont
& font
)
161 bool ret
= wxControl::SetFont(font
);
163 // adjust the size of the window to fit to the label unless autoresizing is
165 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE
) )
167 DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH
| wxSIZE_AUTO_HEIGHT
);
173 long wxStaticText::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
175 // Ensure that static items get messages. Some controls don't like this
176 // message to be intercepted (e.g. RichEdit), hence the tests.
177 // Messes up display with Windows XP, apparently, so have to
178 // do explicit hit-testing in wxWindowMSW.
180 if (nMsg
== WM_NCHITTEST
)
181 return (long)HTCLIENT
;
183 return wxWindow::MSWWindowProc(nMsg
, wParam
, lParam
);
185 #endif // wxUSE_STATTEXT