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