]>
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
);
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
);
174 long wxStaticText::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
176 // Ensure that static items get messages. Some controls don't like this
177 // message to be intercepted (e.g. RichEdit), hence the tests.
178 if (nMsg
== WM_NCHITTEST
)
179 return (long)HTCLIENT
;
181 return wxWindow::MSWWindowProc(nMsg
, wParam
, lParam
);
184 #endif // wxUSE_STATTEXT