OS/2 scrolling support for controls
[wxWidgets.git] / src / os2 / stattext.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: stattext.cpp
3 // Purpose: wxStaticText
4 // Author: David Webster
5 // Modified by:
6 // Created: 10/17/99
7 // RCS-ID: $Id$
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "stattext.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifndef WX_PRECOMP
20 #include "wx/event.h"
21 #include "wx/app.h"
22 #include "wx/brush.h"
23 #endif
24
25 #include "wx/stattext.h"
26 #include "wx/os2/private.h"
27 #include <stdio.h>
28
29 IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxControl)
30
31 bool wxStaticText::Create(
32 wxWindow* pParent
33 , wxWindowID vId
34 , const wxString& rsLabel
35 , const wxPoint& rPos
36 , const wxSize& rSize
37 , long lStyle
38 , const wxString& rsName
39 )
40 {
41 SetName(rsName);
42 if (pParent)
43 pParent->AddChild(this);
44
45 SetBackgroundColour(pParent->GetBackgroundColour()) ;
46 SetForegroundColour(pParent->GetForegroundColour()) ;
47
48 if ( vId == -1 )
49 m_windowId = (int)NewControlId();
50 else
51 m_windowId = vId;
52
53 int nX = rPos.x;
54 int nY = rPos.y;
55 int nWidth = rSize.x;
56 int nHeight = rSize.y;
57
58 m_windowStyle = lStyle;
59
60 long lSstyle = 0L;
61
62 lSstyle = WS_VISIBLE | SS_TEXT | DT_VCENTER;
63 if (m_windowStyle & wxALIGN_CENTRE)
64 lSstyle |= DT_CENTER;
65 else if (m_windowStyle & wxALIGN_RIGHT)
66 lSstyle |= DT_RIGHT;
67 else
68 lSstyle |= DT_LEFT;
69 //
70 // If the parent is a scrolled window the controls must
71 // have this style or they will overlap the scrollbars
72 //
73 if (pParent)
74 if (pParent->IsKindOf(CLASSINFO(wxScrolledWindow)) ||
75 pParent->IsKindOf(CLASSINFO(wxGenericScrolledWindow)))
76 lSstyle |= WS_CLIPSIBLINGS;
77
78 m_hWnd = (WXHWND)::WinCreateWindow( (HWND)GetHwndOf(pParent) // Parent window handle
79 ,WC_STATIC // Window class
80 ,(PSZ)rsLabel.c_str() // Initial Text
81 ,(ULONG)lSstyle // Style flags
82 ,0L, 0L, 0L, 0L // Origin -- 0 size
83 ,(HWND)GetHwndOf(pParent) // owner window handle (same as parent
84 ,HWND_TOP // initial z position
85 ,(ULONG)m_windowId // Window identifier
86 ,NULL // no control data
87 ,NULL // no Presentation parameters
88 );
89
90 wxCHECK_MSG(m_hWnd, FALSE, wxT("Failed to create static ctrl"));
91
92 wxColour vColour;
93
94 vColour.Set(wxString("BLACK"));
95
96 LONG lColor = (LONG)vColour.GetPixel();
97
98 ::WinSetPresParam( m_hWnd
99 ,PP_FOREGROUNDCOLOR
100 ,sizeof(LONG)
101 ,(PVOID)&lColor
102 );
103 lColor = (LONG)m_backgroundColour.GetPixel();
104
105 ::WinSetPresParam( m_hWnd
106 ,PP_BACKGROUNDCOLOR
107 ,sizeof(LONG)
108 ,(PVOID)&lColor
109 );
110
111 SubclassWin(m_hWnd);
112 wxControl::SetFont(pParent->GetFont());
113 SetSize( nX
114 ,nY
115 ,nWidth
116 ,nHeight
117 );
118 return TRUE;
119 } // end of wxStaticText::Create
120
121 wxSize wxStaticText::DoGetBestSize() const
122 {
123 wxString sText(wxGetWindowText(GetHWND()));
124 int nWidthTextMax = 0;
125 int nWidthLine = 0;
126 int nHeightTextTotal = 0;
127 int nHeightLineDefault = 0;
128 int nHeightLine = 0;
129 wxString sCurLine;
130
131 for (const wxChar *pc = sText; ; pc++)
132 {
133 if ( *pc == wxT('\n') || *pc == wxT('\0') )
134 {
135 if (!sCurLine )
136 {
137 //
138 // We can't use GetTextExtent - it will return 0 for both width
139 // and height and an empty line should count in height
140 // calculation
141 //
142 if (!nHeightLineDefault)
143 nHeightLineDefault = nHeightLine;
144 if (!nHeightLineDefault)
145 GetTextExtent(_T("W"), NULL, &nHeightLineDefault);
146 nHeightTextTotal += nHeightLineDefault;
147 }
148 else
149 {
150 GetTextExtent( sCurLine
151 ,&nWidthLine
152 ,&nHeightLine
153 );
154 if (nWidthLine > nWidthTextMax)
155 nWidthTextMax = nWidthLine;
156 nHeightTextTotal += nHeightLine;
157 }
158
159 if ( *pc == wxT('\n') )
160 {
161 sCurLine.Empty();
162 }
163 else
164 {
165 break;
166 }
167 }
168 else
169 {
170 sCurLine += *pc;
171 }
172 }
173 return wxSize( nWidthTextMax
174 ,nHeightTextTotal
175 );
176 } // end of wxStaticText::DoGetBestSize
177
178 void wxStaticText::DoSetSize(
179 int nX
180 , int nY
181 , int nWidth
182 , int nHeight
183 , int nSizeFlags
184 )
185 {
186 //
187 // We need to refresh the window after changing its size as the standard
188 // control doesn't always update itself properly.
189 //
190 wxStaticTextBase::DoSetSize( nX
191 ,nY
192 ,nWidth
193 ,nHeight
194 ,nSizeFlags
195 );
196 Refresh();
197 } // end of wxStaticText::DoSetSize
198
199 bool wxStaticText::SetFont(
200 const wxFont& rFont
201 )
202 {
203 bool bRet = wxControl::SetFont(rFont);
204
205 //
206 // Adjust the size of the window to fit to the label unless autoresizing is
207 // disabled
208 //
209 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) )
210 {
211 DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT);
212 }
213 return bRet;
214 } // end of wxStaticText::SetFont
215
216 void wxStaticText::SetLabel(
217 const wxString& rsLabel
218 )
219 {
220 ::WinSetWindowText(GetHwnd(), rsLabel.c_str());
221
222 //
223 // Adjust the size of the window to fit to the label unless autoresizing is
224 // disabled
225 //
226 if (!(GetWindowStyle() & wxST_NO_AUTORESIZE))
227 {
228 DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT);
229 }
230 DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT);
231 } // end of wxStaticText::SetLabel
232
233 MRESULT wxStaticText::OS2WindowProc(
234 WXUINT uMsg
235 , WXWPARAM wParam
236 , WXLPARAM lParam
237 )
238 {
239 return wxWindow::OS2WindowProc( uMsg
240 ,wParam
241 ,lParam
242 );
243 } // end of wxStaticText::OS2WindowProc
244
245