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