]>
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 DW |
22 | #include "wx/brush.h" |
23 | #endif | |
0e320a79 | 24 | |
d90895ac DW |
25 | #include "wx/stattext.h" |
26 | #include "wx/os2/private.h" | |
0e320a79 DW |
27 | #include <stdio.h> |
28 | ||
0e320a79 | 29 | IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxControl) |
0e320a79 | 30 | |
1f3154c8 DW |
31 | bool wxStaticText::Create( |
32 | wxWindow* pParent | |
33 | , wxWindowID vId | |
34 | , const wxString& rsLabel | |
35 | , const wxPoint& rPos | |
36 | , const wxSize& rSize | |
37 | , long lStyle | |
38 | , const wxString& rsName | |
39 | ) | |
0e320a79 | 40 | { |
1f3154c8 DW |
41 | SetName(rsName); |
42 | if (pParent) | |
43 | pParent->AddChild(this); | |
d90895ac | 44 | |
1f3154c8 DW |
45 | SetBackgroundColour(pParent->GetBackgroundColour()) ; |
46 | SetForegroundColour(pParent->GetForegroundColour()) ; | |
0e320a79 | 47 | |
f15b4952 | 48 | if ( vId == -1 ) |
d90895ac DW |
49 | m_windowId = (int)NewControlId(); |
50 | else | |
1f3154c8 | 51 | m_windowId = vId; |
0e320a79 | 52 | |
1f3154c8 DW |
53 | int nX = rPos.x; |
54 | int nY = rPos.y; | |
55 | int nWidth = rSize.x; | |
56 | int nHeight = rSize.y; | |
0e320a79 | 57 | |
1f3154c8 | 58 | m_windowStyle = lStyle; |
0e320a79 | 59 | |
1f3154c8 | 60 | long lSstyle = 0L; |
0e320a79 | 61 | |
1f3154c8 DW |
62 | lSstyle = WS_VISIBLE | SS_TEXT | DT_VCENTER; |
63 | if (m_windowStyle & wxALIGN_CENTRE) | |
64 | lSstyle |= DT_CENTER; | |
65 | else if (m_windowStyle & wxALIGN_RIGHT) | |
66 | lSstyle |= DT_RIGHT; | |
67 | else | |
68 | lSstyle |= DT_LEFT; | |
69 | m_hWnd = (WXHWND)::WinCreateWindow( (HWND)GetHwndOf(pParent) // Parent window handle | |
70 | ,WC_STATIC // Window class | |
71 | ,(PSZ)rsLabel.c_str() // Initial Text | |
72 | ,(ULONG)lSstyle // Style flags | |
73 | ,0L, 0L, 0L, 0L // Origin -- 0 size | |
74 | ,(HWND)GetHwndOf(pParent) // owner window handle (same as parent | |
75 | ,HWND_TOP // initial z position | |
76 | ,(ULONG)m_windowId // Window identifier | |
77 | ,NULL // no control data | |
78 | ,NULL // no Presentation parameters | |
79 | ); | |
80 | ||
81 | wxCHECK_MSG(m_hWnd, FALSE, wxT("Failed to create static ctrl")); | |
d90895ac | 82 | |
3c299c3a DW |
83 | wxColour vColour; |
84 | ||
85 | vColour.Set(wxString("BLACK")); | |
86 | ||
87 | LONG lColor = (LONG)vColour.GetPixel(); | |
88 | ||
89 | ::WinSetPresParam( m_hWnd | |
90 | ,PP_FOREGROUNDCOLOR | |
91 | ,sizeof(LONG) | |
92 | ,(PVOID)&lColor | |
93 | ); | |
7993e67c DW |
94 | lColor = (LONG)m_backgroundColour.GetPixel(); |
95 | ||
96 | ::WinSetPresParam( m_hWnd | |
97 | ,PP_BACKGROUNDCOLOR | |
98 | ,sizeof(LONG) | |
99 | ,(PVOID)&lColor | |
100 | ); | |
3c299c3a | 101 | |
1f3154c8 DW |
102 | SubclassWin(m_hWnd); |
103 | wxControl::SetFont(pParent->GetFont()); | |
3c299c3a DW |
104 | SetSize( nX |
105 | ,nY | |
106 | ,nWidth | |
107 | ,nHeight | |
108 | ); | |
109 | return TRUE; | |
1f3154c8 | 110 | } // end of wxStaticText::Create |
0e320a79 | 111 | |
e78c4d50 | 112 | wxSize wxStaticText::DoGetBestSize() const |
0e320a79 | 113 | { |
1f3154c8 DW |
114 | wxString sText(wxGetWindowText(GetHWND())); |
115 | int nWidthTextMax = 0; | |
116 | int nWidthLine = 0; | |
117 | int nHeightTextTotal = 0; | |
118 | int nHeightLineDefault = 0; | |
119 | int nHeightLine = 0; | |
120 | wxString sCurLine; | |
121 | ||
3c299c3a | 122 | for (const wxChar *pc = sText; ; pc++) |
1f3154c8 DW |
123 | { |
124 | if ( *pc == wxT('\n') || *pc == wxT('\0') ) | |
125 | { | |
126 | if (!sCurLine ) | |
127 | { | |
128 | // | |
129 | // We can't use GetTextExtent - it will return 0 for both width | |
e78c4d50 DW |
130 | // and height and an empty line should count in height |
131 | // calculation | |
1f3154c8 DW |
132 | // |
133 | if (!nHeightLineDefault) | |
134 | nHeightLineDefault = nHeightLine; | |
135 | if (!nHeightLineDefault) | |
136 | GetTextExtent(_T("W"), NULL, &nHeightLineDefault); | |
137 | nHeightTextTotal += nHeightLineDefault; | |
e78c4d50 | 138 | } |
1f3154c8 DW |
139 | else |
140 | { | |
141 | GetTextExtent( sCurLine | |
142 | ,&nWidthLine | |
143 | ,&nHeightLine | |
144 | ); | |
145 | if (nWidthLine > nWidthTextMax) | |
146 | nWidthTextMax = nWidthLine; | |
147 | nHeightTextTotal += nHeightLine; | |
e78c4d50 | 148 | } |
d90895ac | 149 | |
1f3154c8 DW |
150 | if ( *pc == wxT('\n') ) |
151 | { | |
152 | sCurLine.Empty(); | |
d90895ac | 153 | } |
1f3154c8 DW |
154 | else |
155 | { | |
d90895ac DW |
156 | break; |
157 | } | |
158 | } | |
1f3154c8 DW |
159 | else |
160 | { | |
161 | sCurLine += *pc; | |
d90895ac DW |
162 | } |
163 | } | |
1f3154c8 DW |
164 | return wxSize( nWidthTextMax |
165 | ,nHeightTextTotal | |
166 | ); | |
167 | } // end of wxStaticText::DoGetBestSize | |
168 | ||
7993e67c DW |
169 | void wxStaticText::DoSetSize( |
170 | int nX | |
171 | , int nY | |
172 | , int nWidth | |
173 | , int nHeight | |
174 | , int nSizeFlags | |
175 | ) | |
176 | { | |
177 | // | |
178 | // We need to refresh the window after changing its size as the standard | |
179 | // control doesn't always update itself properly. | |
180 | // | |
181 | wxStaticTextBase::DoSetSize( nX | |
182 | ,nY | |
183 | ,nWidth | |
184 | ,nHeight | |
185 | ,nSizeFlags | |
186 | ); | |
187 | Refresh(); | |
188 | } // end of wxStaticText::DoSetSize | |
189 | ||
3c299c3a DW |
190 | bool wxStaticText::SetFont( |
191 | const wxFont& rFont | |
1f3154c8 | 192 | ) |
0e320a79 | 193 | { |
3c299c3a | 194 | bool bRet = wxControl::SetFont(rFont); |
1f3154c8 DW |
195 | |
196 | // | |
197 | // Adjust the size of the window to fit to the label unless autoresizing is | |
198 | // disabled | |
199 | // | |
3c299c3a | 200 | if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) ) |
1f3154c8 DW |
201 | { |
202 | DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT); | |
203 | } | |
3c299c3a DW |
204 | return bRet; |
205 | } // end of wxStaticText::SetFont | |
d90895ac | 206 | |
3c299c3a DW |
207 | void wxStaticText::SetLabel( |
208 | const wxString& rsLabel | |
1f3154c8 | 209 | ) |
d90895ac | 210 | { |
3c299c3a | 211 | ::WinSetWindowText(GetHwnd(), rsLabel.c_str()); |
1f3154c8 DW |
212 | |
213 | // | |
214 | // Adjust the size of the window to fit to the label unless autoresizing is | |
215 | // disabled | |
216 | // | |
3c299c3a | 217 | if (!(GetWindowStyle() & wxST_NO_AUTORESIZE)) |
1f3154c8 DW |
218 | { |
219 | DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT); | |
220 | } | |
3c299c3a DW |
221 | DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT); |
222 | } // end of wxStaticText::SetLabel | |
1f3154c8 DW |
223 | |
224 | MRESULT wxStaticText::OS2WindowProc( | |
225 | WXUINT uMsg | |
226 | , WXWPARAM wParam | |
227 | , WXLPARAM lParam | |
228 | ) | |
d90895ac | 229 | { |
1f3154c8 DW |
230 | return wxWindow::OS2WindowProc( uMsg |
231 | ,wParam | |
232 | ,lParam | |
233 | ); | |
234 | } // end of wxStaticText::OS2WindowProc | |
235 | ||
0e320a79 | 236 |