Include wx/event.h according to precompiled headers of wx/wx.h (with other minor...
[wxWidgets.git] / src / os2 / stattext.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/os2/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 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifndef WX_PRECOMP
16 #include "wx/event.h"
17 #include "wx/app.h"
18 #include "wx/brush.h"
19 #include "wx/scrolwin.h"
20 #endif
21
22 #include "wx/stattext.h"
23 #include "wx/os2/private.h"
24 #include <stdio.h>
25
26 IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxControl)
27
28 bool wxStaticText::Create( wxWindow* pParent,
29 wxWindowID vId,
30 const wxString& rsLabel,
31 const wxPoint& rPos,
32 const wxSize& rSize,
33 long lStyle,
34 const wxString& rsName )
35 {
36 SetName(rsName);
37 if (pParent)
38 pParent->AddChild(this);
39
40 SetBackgroundColour(pParent->GetBackgroundColour()) ;
41 SetForegroundColour(pParent->GetForegroundColour()) ;
42
43 if ( vId == wxID_ANY )
44 m_windowId = (int)NewControlId();
45 else
46 m_windowId = vId;
47
48 int nX = rPos.x;
49 int nY = rPos.y;
50 int nWidth = rSize.x;
51 int nHeight = rSize.y;
52
53 m_windowStyle = lStyle;
54
55 long lSstyle = 0L;
56
57 // Used to have DT_VCENTER but that doesn't work correctly with
58 // multiline strings and DT_WORDBREAK. Accept a reasonable
59 // compromise for now
60 lSstyle = WS_VISIBLE | SS_TEXT | DT_WORDBREAK | DT_MNEMONIC;
61 if (m_windowStyle & wxALIGN_CENTRE)
62 lSstyle |= DT_CENTER;
63 else if (m_windowStyle & wxALIGN_RIGHT)
64 lSstyle |= DT_RIGHT;
65 else
66 lSstyle |= DT_LEFT;
67
68 wxString sLabel = ::wxPMTextToLabel(rsLabel);
69
70 m_hWnd = (WXHWND)::WinCreateWindow( (HWND)GetHwndOf(pParent) // Parent window handle
71 ,WC_STATIC // Window class
72 ,(PSZ)sLabel.c_str() // Initial Text
73 ,(ULONG)lSstyle // Style flags
74 ,0L, 0L, 0L, 0L // Origin -- 0 size
75 ,(HWND)GetHwndOf(pParent) // owner window handle (same as parent
76 ,HWND_TOP // initial z position
77 ,(ULONG)m_windowId // Window identifier
78 ,NULL // no control data
79 ,NULL // no Presentation parameters
80 );
81
82 wxCHECK_MSG(m_hWnd, false, wxT("Failed to create static ctrl"));
83
84 LONG lColor = (LONG)wxBLACK->GetPixel();
85
86 ::WinSetPresParam( m_hWnd
87 ,PP_FOREGROUNDCOLOR
88 ,sizeof(LONG)
89 ,(PVOID)&lColor
90 );
91 lColor = (LONG)m_backgroundColour.GetPixel();
92
93 ::WinSetPresParam( m_hWnd
94 ,PP_BACKGROUNDCOLOR
95 ,sizeof(LONG)
96 ,(PVOID)&lColor
97 );
98
99 SubclassWin(m_hWnd);
100 SetFont(*wxSMALL_FONT);
101 SetXComp(0);
102 SetYComp(0);
103 SetSize( nX, nY, nWidth, nHeight );
104
105 return true;
106 } // end of wxStaticText::Create
107
108 wxSize wxStaticText::DoGetBestSize() const
109 {
110 wxString sText(wxGetWindowText(GetHWND()));
111 int nWidthTextMax = 0;
112 int nWidthLine = 0;
113 int nHeightTextTotal = 0;
114 int nHeightLineDefault = 0;
115 int nHeightLine = 0;
116 wxString sCurLine;
117 bool bLastWasTilde = false;
118
119 for (const wxChar *pc = sText; ; pc++)
120 {
121 if ( *pc == wxT('\n') || *pc == wxT('\0') )
122 {
123 if (!sCurLine )
124 {
125 //
126 // We can't use GetTextExtent - it will return 0 for both width
127 // and height and an empty line should count in height
128 // calculation
129 //
130 if (!nHeightLineDefault)
131 nHeightLineDefault = nHeightLine;
132 if (!nHeightLineDefault)
133 GetTextExtent(_T("W"), NULL, &nHeightLineDefault);
134 nHeightTextTotal += nHeightLineDefault;
135 }
136 else
137 {
138 GetTextExtent( sCurLine
139 ,&nWidthLine
140 ,&nHeightLine
141 );
142 if (nWidthLine > nWidthTextMax)
143 nWidthTextMax = nWidthLine;
144 nHeightTextTotal += nHeightLine;
145 }
146
147 if ( *pc == wxT('\n') )
148 {
149 sCurLine.Empty();
150 }
151 else
152 {
153 break;
154 }
155 }
156 else
157 {
158 //
159 // We shouldn't take into account the '~' which just introduces the
160 // mnemonic characters and so are not shown on the screen -- except
161 // when it is preceded by another '~' in which case it stands for a
162 // literal tilde
163 //
164 if (*pc == _T('~'))
165 {
166 if (!bLastWasTilde)
167 {
168 bLastWasTilde = true;
169
170 //
171 // Skip the statement adding pc to curLine below
172 //
173 continue;
174 }
175
176 //
177 // It is a literal tilde
178 //
179 bLastWasTilde = false;
180 }
181 sCurLine += *pc;
182 }
183 }
184 return wxSize( nWidthTextMax
185 ,nHeightTextTotal
186 );
187 } // end of wxStaticText::DoGetBestSize
188
189 void wxStaticText::DoSetSize(
190 int nX
191 , int nY
192 , int nWidth
193 , int nHeight
194 , int nSizeFlags
195 )
196 {
197 //
198 // We need to refresh the window after changing its size as the standard
199 // control doesn't always update itself properly.
200 //
201 wxStaticTextBase::DoSetSize( nX
202 ,nY
203 ,nWidth
204 ,nHeight
205 ,nSizeFlags
206 );
207 Refresh();
208 } // end of wxStaticText::DoSetSize
209
210 bool wxStaticText::SetFont(
211 const wxFont& rFont
212 )
213 {
214 bool bRet = wxControl::SetFont(rFont);
215
216 //
217 // Adjust the size of the window to fit to the label unless autoresizing is
218 // disabled
219 //
220 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) )
221 {
222 DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT);
223 }
224 return bRet;
225 } // end of wxStaticText::SetFont
226
227 void wxStaticText::SetLabel(
228 const wxString& rsLabel
229 )
230 {
231 wxString sLabel = ::wxPMTextToLabel(rsLabel);
232 ::WinSetWindowText(GetHwnd(), (PSZ)sLabel.c_str());
233
234 //
235 // Adjust the size of the window to fit to the label unless autoresizing is
236 // disabled
237 //
238 if (!(GetWindowStyle() & wxST_NO_AUTORESIZE))
239 {
240 wxCoord vX;
241 wxCoord vY;
242 wxCoord vWidth;
243 wxCoord vHeight;
244
245 GetPosition(&vX, &vY);
246 GetSize(&vWidth, &vHeight);
247 if (!(vX == -1 && vY == -1 && vWidth == -1 && vHeight == -1))
248 DoSetSize(vX, vY, vWidth, vHeight, wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT);
249 else
250 DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT);
251 }
252 } // end of wxStaticText::SetLabel
253
254 MRESULT wxStaticText::OS2WindowProc(
255 WXUINT uMsg
256 , WXWPARAM wParam
257 , WXLPARAM lParam
258 )
259 {
260 return wxWindow::OS2WindowProc( uMsg
261 ,wParam
262 ,lParam
263 );
264 } // end of wxStaticText::OS2WindowProc