]>
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
;
61 if (m_windowStyle
& wxALIGN_CENTRE
)
63 else if (m_windowStyle
& wxALIGN_RIGHT
)
68 // Even with extended styles, need to combine with WS_BORDER
69 // for them to look right.
70 if ( wxStyleHasBorder(m_windowStyle
) )
73 m_hWnd
= (WXHWND
)::CreateWindowEx(MakeExtendedStyle(m_windowStyle
), wxT("STATIC"), (const wxChar
*)label
,
75 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
76 wxGetInstance(), NULL
);
78 wxCHECK_MSG( m_hWnd
, FALSE
, wxT("Failed to create static ctrl") );
82 if (!(GetParent()->GetWindowStyleFlag() & wxUSER_COLOURS))
83 Ctl3dSubclassCtl(static_item);
89 SetFont(parent
->GetFont());
90 SetSize(x
, y
, width
, height
);
95 wxSize
wxStaticText::DoGetBestSize() const
97 wxString
text(wxGetWindowText(GetHWND()));
99 int widthTextMax
= 0, widthLine
,
100 heightTextTotal
= 0, heightLineDefault
= 0, heightLine
= 0;
103 for ( const wxChar
*pc
= text
; ; pc
++ ) {
104 if ( *pc
== wxT('\n') || *pc
== wxT('\0') ) {
106 // we can't use GetTextExtent - it will return 0 for both width
107 // and height and an empty line should count in height
109 if ( !heightLineDefault
)
110 heightLineDefault
= heightLine
;
111 if ( !heightLineDefault
)
112 GetTextExtent(_T("W"), NULL
, &heightLineDefault
);
114 heightTextTotal
+= heightLineDefault
;
117 GetTextExtent(curLine
, &widthLine
, &heightLine
);
118 if ( widthLine
> widthTextMax
)
119 widthTextMax
= widthLine
;
120 heightTextTotal
+= heightLine
;
123 if ( *pc
== wxT('\n') ) {
136 return wxSize(widthTextMax
, heightTextTotal
);
139 void wxStaticText::SetLabel(const wxString
& label
)
141 SetWindowText(GetHwnd(), label
);
143 // adjust the size of the window to fit to the label unless autoresizing is
145 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE
) )
147 DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH
| wxSIZE_AUTO_HEIGHT
);
151 long wxStaticText::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
153 // Ensure that static items get messages. Some controls don't like this
154 // message to be intercepted (e.g. RichEdit), hence the tests.
155 if (nMsg
== WM_NCHITTEST
)
156 return (long)HTCLIENT
;
158 return wxWindow::MSWWindowProc(nMsg
, wParam
, lParam
);