]> git.saurik.com Git - wxWidgets.git/blame - src/os2/stattext.cpp
added IsSizeDeferred() (part of patch 1199639)
[wxWidgets.git] / src / os2 / stattext.cpp
CommitLineData
0e320a79
DW
1/////////////////////////////////////////////////////////////////////////////
2// Name: stattext.cpp
3// Purpose: wxStaticText
d90895ac 4// Author: David Webster
0e320a79 5// Modified by:
d90895ac 6// Created: 10/17/99
0e320a79 7// RCS-ID: $Id$
d90895ac 8// Copyright: (c) David Webster
65571936 9// Licence: wxWindows licence
0e320a79
DW
10/////////////////////////////////////////////////////////////////////////////
11
aa213887
SN
12#ifdef __GNUG__
13#pragma implementation "stattext.h"
14#endif
15
d90895ac
DW
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
0e320a79 18
d90895ac
DW
19#ifndef WX_PRECOMP
20#include "wx/event.h"
0e320a79 21#include "wx/app.h"
d90895ac 22#include "wx/brush.h"
a4a16252 23#include "wx/scrolwin.h"
d90895ac 24#endif
0e320a79 25
d90895ac
DW
26#include "wx/stattext.h"
27#include "wx/os2/private.h"
0e320a79
DW
28#include <stdio.h>
29
0e320a79 30IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxControl)
0e320a79 31
1f3154c8
DW
32bool 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)
0e320a79 41{
1f3154c8
DW
42 SetName(rsName);
43 if (pParent)
44 pParent->AddChild(this);
d90895ac 45
1f3154c8
DW
46 SetBackgroundColour(pParent->GetBackgroundColour()) ;
47 SetForegroundColour(pParent->GetForegroundColour()) ;
0e320a79 48
f15b4952 49 if ( vId == -1 )
d90895ac
DW
50 m_windowId = (int)NewControlId();
51 else
1f3154c8 52 m_windowId = vId;
0e320a79 53
1f3154c8
DW
54 int nX = rPos.x;
55 int nY = rPos.y;
56 int nWidth = rSize.x;
57 int nHeight = rSize.y;
0e320a79 58
1f3154c8 59 m_windowStyle = lStyle;
0e320a79 60
1f3154c8 61 long lSstyle = 0L;
0e320a79 62
e94d504d 63 lSstyle = WS_VISIBLE | SS_TEXT | DT_VCENTER | DT_MNEMONIC;
1f3154c8
DW
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;
e94d504d
SN
70
71 wxString sLabel = ::wxPMTextToLabel(rsLabel);
72
1f3154c8
DW
73 m_hWnd = (WXHWND)::WinCreateWindow( (HWND)GetHwndOf(pParent) // Parent window handle
74 ,WC_STATIC // Window class
e94d504d 75 ,(PSZ)sLabel.c_str() // Initial Text
1f3154c8
DW
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"));
d90895ac 86
3c299c3a
DW
87 wxColour vColour;
88
0fba44b4 89 vColour.Set(wxString(wxT("BLACK")));
3c299c3a
DW
90
91 LONG lColor = (LONG)vColour.GetPixel();
92
93 ::WinSetPresParam( m_hWnd
94 ,PP_FOREGROUNDCOLOR
95 ,sizeof(LONG)
96 ,(PVOID)&lColor
97 );
7993e67c
DW
98 lColor = (LONG)m_backgroundColour.GetPixel();
99
100 ::WinSetPresParam( m_hWnd
101 ,PP_BACKGROUNDCOLOR
102 ,sizeof(LONG)
103 ,(PVOID)&lColor
104 );
3c299c3a 105
1f3154c8 106 SubclassWin(m_hWnd);
b720b24d
DW
107 SetFont(*wxSMALL_FONT);
108 SetXComp(0);
109 SetYComp(0);
3c299c3a
DW
110 SetSize( nX
111 ,nY
112 ,nWidth
113 ,nHeight
114 );
115 return TRUE;
1f3154c8 116} // end of wxStaticText::Create
0e320a79 117
e78c4d50 118wxSize wxStaticText::DoGetBestSize() const
0e320a79 119{
1f3154c8
DW
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;
e94d504d 127 bool bLastWasTilde = FALSE;
1f3154c8 128
3c299c3a 129 for (const wxChar *pc = sText; ; pc++)
1f3154c8
DW
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
e78c4d50
DW
137 // and height and an empty line should count in height
138 // calculation
1f3154c8
DW
139 //
140 if (!nHeightLineDefault)
141 nHeightLineDefault = nHeightLine;
142 if (!nHeightLineDefault)
143 GetTextExtent(_T("W"), NULL, &nHeightLineDefault);
144 nHeightTextTotal += nHeightLineDefault;
e78c4d50 145 }
1f3154c8
DW
146 else
147 {
148 GetTextExtent( sCurLine
149 ,&nWidthLine
150 ,&nHeightLine
151 );
152 if (nWidthLine > nWidthTextMax)
153 nWidthTextMax = nWidthLine;
154 nHeightTextTotal += nHeightLine;
e78c4d50 155 }
d90895ac 156
1f3154c8
DW
157 if ( *pc == wxT('\n') )
158 {
159 sCurLine.Empty();
d90895ac 160 }
1f3154c8
DW
161 else
162 {
d90895ac
DW
163 break;
164 }
165 }
1f3154c8
DW
166 else
167 {
6e348b12 168 //
e94d504d 169 // We shouldn't take into account the '~' which just introduces the
6e348b12 170 // mnemonic characters and so are not shown on the screen -- except
e94d504d
SN
171 // when it is preceded by another '~' in which case it stands for a
172 // literal tilde
6e348b12 173 //
e94d504d 174 if (*pc == _T('~'))
6e348b12 175 {
e94d504d 176 if (!bLastWasTilde)
6e348b12 177 {
e94d504d 178 bLastWasTilde = TRUE;
6e348b12
DW
179
180 //
181 // Skip the statement adding pc to curLine below
182 //
183 continue;
184 }
185
186 //
e94d504d 187 // It is a literal tilde
6e348b12 188 //
e94d504d 189 bLastWasTilde = FALSE;
6e348b12 190 }
1f3154c8 191 sCurLine += *pc;
d90895ac
DW
192 }
193 }
1f3154c8
DW
194 return wxSize( nWidthTextMax
195 ,nHeightTextTotal
196 );
197} // end of wxStaticText::DoGetBestSize
198
7993e67c
DW
199void 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
3c299c3a
DW
220bool wxStaticText::SetFont(
221 const wxFont& rFont
1f3154c8 222)
0e320a79 223{
3c299c3a 224 bool bRet = wxControl::SetFont(rFont);
1f3154c8
DW
225
226 //
227 // Adjust the size of the window to fit to the label unless autoresizing is
228 // disabled
229 //
3c299c3a 230 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) )
1f3154c8
DW
231 {
232 DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT);
233 }
3c299c3a
DW
234 return bRet;
235} // end of wxStaticText::SetFont
d90895ac 236
3c299c3a
DW
237void wxStaticText::SetLabel(
238 const wxString& rsLabel
1f3154c8 239)
d90895ac 240{
e94d504d
SN
241 wxString sLabel = ::wxPMTextToLabel(rsLabel);
242 ::WinSetWindowText(GetHwnd(), (PSZ)sLabel.c_str());
1f3154c8
DW
243
244 //
245 // Adjust the size of the window to fit to the label unless autoresizing is
246 // disabled
247 //
3c299c3a 248 if (!(GetWindowStyle() & wxST_NO_AUTORESIZE))
1f3154c8 249 {
b720b24d
DW
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);
1f3154c8 261 }
3c299c3a 262} // end of wxStaticText::SetLabel
1f3154c8
DW
263
264MRESULT wxStaticText::OS2WindowProc(
265 WXUINT uMsg
266, WXWPARAM wParam
267, WXLPARAM lParam
268)
d90895ac 269{
1f3154c8
DW
270 return wxWindow::OS2WindowProc( uMsg
271 ,wParam
272 ,lParam
273 );
274} // end of wxStaticText::OS2WindowProc