]>
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 IMPLEMENT_DYNAMIC_CLASS(wxStaticText
, wxControl
)
35 bool wxStaticText::Create(wxWindow
*parent
, wxWindowID id
,
36 const wxString
& label
,
43 if (parent
) parent
->AddChild(this);
45 SetBackgroundColour(parent
->GetBackgroundColour()) ;
46 SetForegroundColour(parent
->GetForegroundColour()) ;
49 m_windowId
= (int)NewControlId();
58 m_windowStyle
= style
;
60 long msStyle
= WS_CHILD
| WS_VISIBLE
;
62 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
63 msStyle
|= WS_CLIPSIBLINGS
;
64 if (m_windowStyle
& wxALIGN_CENTRE
)
66 else if (m_windowStyle
& wxALIGN_RIGHT
)
71 // Even with extended styles, need to combine with WS_BORDER
72 // for them to look right.
73 if ( wxStyleHasBorder(m_windowStyle
) )
76 m_hWnd
= (WXHWND
)::CreateWindowEx(MakeExtendedStyle(m_windowStyle
), wxT("STATIC"), (const wxChar
*)label
,
78 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
79 wxGetInstance(), NULL
);
81 wxCHECK_MSG( m_hWnd
, FALSE
, wxT("Failed to create static ctrl") );
85 if (!(GetParent()->GetWindowStyleFlag() & wxUSER_COLOURS))
86 Ctl3dSubclassCtl(static_item);
92 wxControl::SetFont(parent
->GetFont());
93 SetSize(x
, y
, width
, height
);
98 wxSize
wxStaticText::DoGetBestSize() const
100 wxString
text(wxGetWindowText(GetHWND()));
102 int widthTextMax
= 0, widthLine
,
103 heightTextTotal
= 0, heightLineDefault
= 0, heightLine
= 0;
106 for ( const wxChar
*pc
= text
; ; pc
++ ) {
107 if ( *pc
== wxT('\n') || *pc
== wxT('\0') ) {
109 // we can't use GetTextExtent - it will return 0 for both width
110 // and height and an empty line should count in height
112 if ( !heightLineDefault
)
113 heightLineDefault
= heightLine
;
114 if ( !heightLineDefault
)
115 GetTextExtent(_T("W"), NULL
, &heightLineDefault
);
117 heightTextTotal
+= heightLineDefault
;
120 GetTextExtent(curLine
, &widthLine
, &heightLine
);
121 if ( widthLine
> widthTextMax
)
122 widthTextMax
= widthLine
;
123 heightTextTotal
+= heightLine
;
126 if ( *pc
== wxT('\n') ) {
139 return wxSize(widthTextMax
, heightTextTotal
);
142 void wxStaticText::SetLabel(const wxString
& label
)
144 SetWindowText(GetHwnd(), label
);
146 // adjust the size of the window to fit to the label unless autoresizing is
148 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE
) )
150 DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH
| wxSIZE_AUTO_HEIGHT
);
155 bool wxStaticText::SetFont(const wxFont
& font
)
157 bool ret
= wxControl::SetFont(font
);
159 // adjust the size of the window to fit to the label unless autoresizing is
161 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE
) )
163 DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH
| wxSIZE_AUTO_HEIGHT
);
170 long wxStaticText::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
172 // Ensure that static items get messages. Some controls don't like this
173 // message to be intercepted (e.g. RichEdit), hence the tests.
174 if (nMsg
== WM_NCHITTEST
)
175 return (long)HTCLIENT
;
177 return wxWindow::MSWWindowProc(nMsg
, wParam
, lParam
);