]>
Commit | Line | Data |
---|---|---|
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 DW |
8 | // Copyright: (c) David Webster |
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 | 30 | IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxControl) |
0e320a79 | 31 | |
1f3154c8 DW |
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 | ) | |
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 | |
1f3154c8 DW |
63 | lSstyle = WS_VISIBLE | SS_TEXT | DT_VCENTER; |
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 | m_hWnd = (WXHWND)::WinCreateWindow( (HWND)GetHwndOf(pParent) // Parent window handle | |
71 | ,WC_STATIC // Window class | |
72 | ,(PSZ)rsLabel.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")); | |
d90895ac | 83 | |
3c299c3a DW |
84 | wxColour vColour; |
85 | ||
86 | vColour.Set(wxString("BLACK")); | |
87 | ||
88 | LONG lColor = (LONG)vColour.GetPixel(); | |
89 | ||
90 | ::WinSetPresParam( m_hWnd | |
91 | ,PP_FOREGROUNDCOLOR | |
92 | ,sizeof(LONG) | |
93 | ,(PVOID)&lColor | |
94 | ); | |
7993e67c DW |
95 | lColor = (LONG)m_backgroundColour.GetPixel(); |
96 | ||
97 | ::WinSetPresParam( m_hWnd | |
98 | ,PP_BACKGROUNDCOLOR | |
99 | ,sizeof(LONG) | |
100 | ,(PVOID)&lColor | |
101 | ); | |
3c299c3a | 102 | |
1f3154c8 | 103 | SubclassWin(m_hWnd); |
b720b24d DW |
104 | SetFont(*wxSMALL_FONT); |
105 | SetXComp(0); | |
106 | SetYComp(0); | |
3c299c3a DW |
107 | SetSize( nX |
108 | ,nY | |
109 | ,nWidth | |
110 | ,nHeight | |
111 | ); | |
112 | return TRUE; | |
1f3154c8 | 113 | } // end of wxStaticText::Create |
0e320a79 | 114 | |
e78c4d50 | 115 | wxSize wxStaticText::DoGetBestSize() const |
0e320a79 | 116 | { |
1f3154c8 DW |
117 | wxString sText(wxGetWindowText(GetHWND())); |
118 | int nWidthTextMax = 0; | |
119 | int nWidthLine = 0; | |
120 | int nHeightTextTotal = 0; | |
121 | int nHeightLineDefault = 0; | |
122 | int nHeightLine = 0; | |
123 | wxString sCurLine; | |
6e348b12 | 124 | bool bLastWasAmpersand = FALSE; |
1f3154c8 | 125 | |
3c299c3a | 126 | for (const wxChar *pc = sText; ; pc++) |
1f3154c8 DW |
127 | { |
128 | if ( *pc == wxT('\n') || *pc == wxT('\0') ) | |
129 | { | |
130 | if (!sCurLine ) | |
131 | { | |
132 | // | |
133 | // We can't use GetTextExtent - it will return 0 for both width | |
e78c4d50 DW |
134 | // and height and an empty line should count in height |
135 | // calculation | |
1f3154c8 DW |
136 | // |
137 | if (!nHeightLineDefault) | |
138 | nHeightLineDefault = nHeightLine; | |
139 | if (!nHeightLineDefault) | |
140 | GetTextExtent(_T("W"), NULL, &nHeightLineDefault); | |
141 | nHeightTextTotal += nHeightLineDefault; | |
e78c4d50 | 142 | } |
1f3154c8 DW |
143 | else |
144 | { | |
145 | GetTextExtent( sCurLine | |
146 | ,&nWidthLine | |
147 | ,&nHeightLine | |
148 | ); | |
149 | if (nWidthLine > nWidthTextMax) | |
150 | nWidthTextMax = nWidthLine; | |
151 | nHeightTextTotal += nHeightLine; | |
e78c4d50 | 152 | } |
d90895ac | 153 | |
1f3154c8 DW |
154 | if ( *pc == wxT('\n') ) |
155 | { | |
156 | sCurLine.Empty(); | |
d90895ac | 157 | } |
1f3154c8 DW |
158 | else |
159 | { | |
d90895ac DW |
160 | break; |
161 | } | |
162 | } | |
1f3154c8 DW |
163 | else |
164 | { | |
6e348b12 DW |
165 | // |
166 | // We shouldn't take into account the '&' which just introduces the | |
167 | // mnemonic characters and so are not shown on the screen -- except | |
168 | // when it is preceded by another '&' in which case it stands for a | |
169 | // literal ampersand | |
170 | // | |
171 | if (*pc == _T('&')) | |
172 | { | |
173 | if (!bLastWasAmpersand) | |
174 | { | |
175 | bLastWasAmpersand = TRUE; | |
176 | ||
177 | // | |
178 | // Skip the statement adding pc to curLine below | |
179 | // | |
180 | continue; | |
181 | } | |
182 | ||
183 | // | |
184 | // It is a literal ampersand | |
185 | // | |
186 | bLastWasAmpersand = FALSE; | |
187 | } | |
1f3154c8 | 188 | sCurLine += *pc; |
d90895ac DW |
189 | } |
190 | } | |
1f3154c8 DW |
191 | return wxSize( nWidthTextMax |
192 | ,nHeightTextTotal | |
193 | ); | |
194 | } // end of wxStaticText::DoGetBestSize | |
195 | ||
7993e67c DW |
196 | void wxStaticText::DoSetSize( |
197 | int nX | |
198 | , int nY | |
199 | , int nWidth | |
200 | , int nHeight | |
201 | , int nSizeFlags | |
202 | ) | |
203 | { | |
204 | // | |
205 | // We need to refresh the window after changing its size as the standard | |
206 | // control doesn't always update itself properly. | |
207 | // | |
208 | wxStaticTextBase::DoSetSize( nX | |
209 | ,nY | |
210 | ,nWidth | |
211 | ,nHeight | |
212 | ,nSizeFlags | |
213 | ); | |
214 | Refresh(); | |
215 | } // end of wxStaticText::DoSetSize | |
216 | ||
3c299c3a DW |
217 | bool wxStaticText::SetFont( |
218 | const wxFont& rFont | |
1f3154c8 | 219 | ) |
0e320a79 | 220 | { |
3c299c3a | 221 | bool bRet = wxControl::SetFont(rFont); |
1f3154c8 DW |
222 | |
223 | // | |
224 | // Adjust the size of the window to fit to the label unless autoresizing is | |
225 | // disabled | |
226 | // | |
3c299c3a | 227 | if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) ) |
1f3154c8 DW |
228 | { |
229 | DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT); | |
230 | } | |
3c299c3a DW |
231 | return bRet; |
232 | } // end of wxStaticText::SetFont | |
d90895ac | 233 | |
3c299c3a DW |
234 | void wxStaticText::SetLabel( |
235 | const wxString& rsLabel | |
1f3154c8 | 236 | ) |
d90895ac | 237 | { |
3c299c3a | 238 | ::WinSetWindowText(GetHwnd(), rsLabel.c_str()); |
1f3154c8 DW |
239 | |
240 | // | |
241 | // Adjust the size of the window to fit to the label unless autoresizing is | |
242 | // disabled | |
243 | // | |
3c299c3a | 244 | if (!(GetWindowStyle() & wxST_NO_AUTORESIZE)) |
1f3154c8 | 245 | { |
b720b24d DW |
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); | |
1f3154c8 | 257 | } |
3c299c3a | 258 | } // end of wxStaticText::SetLabel |
1f3154c8 DW |
259 | |
260 | MRESULT wxStaticText::OS2WindowProc( | |
261 | WXUINT uMsg | |
262 | , WXWPARAM wParam | |
263 | , WXLPARAM lParam | |
264 | ) | |
d90895ac | 265 | { |
1f3154c8 DW |
266 | return wxWindow::OS2WindowProc( uMsg |
267 | ,wParam | |
268 | ,lParam | |
269 | ); | |
270 | } // end of wxStaticText::OS2WindowProc |