]>
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"
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 if (!(GetParent()->GetWindowStyleFlag() & wxUSER_COLOURS))
88 Ctl3dSubclassCtl(static_item);
94 wxControl::SetFont(parent
->GetFont());
95 SetSize(x
, y
, width
, height
);
100 wxSize
wxStaticText::DoGetBestSize() const
102 wxString
text(wxGetWindowText(GetHWND()));
104 int widthTextMax
= 0, widthLine
,
105 heightTextTotal
= 0, heightLineDefault
= 0, heightLine
= 0;
108 for ( const wxChar
*pc
= text
; ; pc
++ ) {
109 if ( *pc
== wxT('\n') || *pc
== wxT('\0') ) {
111 // we can't use GetTextExtent - it will return 0 for both width
112 // and height and an empty line should count in height
114 if ( !heightLineDefault
)
115 heightLineDefault
= heightLine
;
116 if ( !heightLineDefault
)
117 GetTextExtent(_T("W"), NULL
, &heightLineDefault
);
119 heightTextTotal
+= heightLineDefault
;
122 GetTextExtent(curLine
, &widthLine
, &heightLine
);
123 if ( widthLine
> widthTextMax
)
124 widthTextMax
= widthLine
;
125 heightTextTotal
+= heightLine
;
128 if ( *pc
== wxT('\n') ) {
141 return wxSize(widthTextMax
, heightTextTotal
);
144 void wxStaticText::SetLabel(const wxString
& label
)
146 SetWindowText(GetHwnd(), label
);
148 // adjust the size of the window to fit to the label unless autoresizing is
150 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE
) )
152 DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH
| wxSIZE_AUTO_HEIGHT
);
157 bool wxStaticText::SetFont(const wxFont
& font
)
159 bool ret
= wxControl::SetFont(font
);
161 // adjust the size of the window to fit to the label unless autoresizing is
163 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE
) )
165 DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH
| wxSIZE_AUTO_HEIGHT
);
172 long wxStaticText::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
174 // Ensure that static items get messages. Some controls don't like this
175 // message to be intercepted (e.g. RichEdit), hence the tests.
176 if (nMsg
== WM_NCHITTEST
)
177 return (long)HTCLIENT
;
179 return wxWindow::MSWWindowProc(nMsg
, wParam
, lParam
);
182 #endif // wxUSE_STATTEXT