]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/stattext.cpp
1 /////////////////////////////////////////////////////////////////////////////
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"
29 #include "wx/stattext.h"
30 #include "wx/msw/private.h"
33 #if !USE_SHARED_LIBRARY
34 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
;
63 if (m_windowStyle
& wxALIGN_CENTRE
)
65 else if (m_windowStyle
& wxALIGN_RIGHT
)
70 // Even with extended styles, need to combine with WS_BORDER
71 // for them to look right.
72 if ( wxStyleHasBorder(m_windowStyle
) )
75 m_hWnd
= (WXHWND
)::CreateWindowEx(MakeExtendedStyle(m_windowStyle
), wxT("STATIC"), (const wxChar
*)label
,
77 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
78 wxGetInstance(), NULL
);
80 wxCHECK_MSG( m_hWnd
, FALSE
, wxT("Failed to create static ctrl") );
84 if (!(GetParent()->GetWindowStyleFlag() & wxUSER_COLOURS))
85 Ctl3dSubclassCtl(static_item);
91 SetFont(parent
->GetFont());
92 SetSize(x
, y
, width
, height
);
97 wxSize
wxStaticText::DoGetBestSize() const
99 wxString
text(wxGetWindowText(GetHWND()));
101 int widthTextMax
= 0, widthLine
,
102 heightTextTotal
= 0, heightLineDefault
= 0, heightLine
= 0;
105 for ( const wxChar
*pc
= text
; ; pc
++ ) {
106 if ( *pc
== wxT('\n') || *pc
== wxT('\0') ) {
108 // we can't use GetTextExtent - it will return 0 for both width
109 // and height and an empty line should count in height
111 if ( !heightLineDefault
)
112 heightLineDefault
= heightLine
;
113 if ( !heightLineDefault
)
114 GetTextExtent(_T("W"), NULL
, &heightLineDefault
);
116 heightTextTotal
+= heightLineDefault
;
119 GetTextExtent(curLine
, &widthLine
, &heightLine
);
120 if ( widthLine
> widthTextMax
)
121 widthTextMax
= widthLine
;
122 heightTextTotal
+= heightLine
;
125 if ( *pc
== wxT('\n') ) {
138 return wxSize(widthTextMax
, heightTextTotal
);
141 void wxStaticText::SetLabel(const wxString
& label
)
143 SetWindowText(GetHwnd(), label
);
145 // adjust the size of the window to fit to the label unless autoresizing is
147 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE
) )
149 DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH
| wxSIZE_AUTO_HEIGHT
);
153 WXHBRUSH
wxStaticText::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
154 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
160 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
162 if (hbrush != (HBRUSH) 0)
165 return (HBRUSH)MSWDefWindowProc(message, wParam, lParam);
170 if (GetParent()->GetTransparentBackground())
171 SetBkMode((HDC
) pDC
, TRANSPARENT
);
173 SetBkMode((HDC
) pDC
, OPAQUE
);
175 ::SetBkColor((HDC
) pDC
, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
176 ::SetTextColor((HDC
) pDC
, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
178 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
180 // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush
181 // has a zero usage count.
182 // backgroundBrush->RealizeResource();
183 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();
186 long wxStaticText::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
188 // Ensure that static items get messages. Some controls don't like this
189 // message to be intercepted (e.g. RichEdit), hence the tests.
190 if (nMsg
== WM_NCHITTEST
)
191 return (long)HTCLIENT
;
193 return wxWindow::MSWWindowProc(nMsg
, wParam
, lParam
);