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