]>
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; | |
5d44b24e DW |
69 | // |
70 | // If the parent is a scrolled window the controls must | |
71 | // have this style or they will overlap the scrollbars | |
72 | // | |
73 | if (pParent) | |
74 | if (pParent->IsKindOf(CLASSINFO(wxScrolledWindow)) || | |
75 | pParent->IsKindOf(CLASSINFO(wxGenericScrolledWindow))) | |
76 | lSstyle |= WS_CLIPSIBLINGS; | |
77 | ||
1f3154c8 DW |
78 | m_hWnd = (WXHWND)::WinCreateWindow( (HWND)GetHwndOf(pParent) // Parent window handle |
79 | ,WC_STATIC // Window class | |
80 | ,(PSZ)rsLabel.c_str() // Initial Text | |
81 | ,(ULONG)lSstyle // Style flags | |
82 | ,0L, 0L, 0L, 0L // Origin -- 0 size | |
83 | ,(HWND)GetHwndOf(pParent) // owner window handle (same as parent | |
84 | ,HWND_TOP // initial z position | |
85 | ,(ULONG)m_windowId // Window identifier | |
86 | ,NULL // no control data | |
87 | ,NULL // no Presentation parameters | |
88 | ); | |
89 | ||
90 | wxCHECK_MSG(m_hWnd, FALSE, wxT("Failed to create static ctrl")); | |
d90895ac | 91 | |
3c299c3a DW |
92 | wxColour vColour; |
93 | ||
94 | vColour.Set(wxString("BLACK")); | |
95 | ||
96 | LONG lColor = (LONG)vColour.GetPixel(); | |
97 | ||
98 | ::WinSetPresParam( m_hWnd | |
99 | ,PP_FOREGROUNDCOLOR | |
100 | ,sizeof(LONG) | |
101 | ,(PVOID)&lColor | |
102 | ); | |
7993e67c DW |
103 | lColor = (LONG)m_backgroundColour.GetPixel(); |
104 | ||
105 | ::WinSetPresParam( m_hWnd | |
106 | ,PP_BACKGROUNDCOLOR | |
107 | ,sizeof(LONG) | |
108 | ,(PVOID)&lColor | |
109 | ); | |
3c299c3a | 110 | |
1f3154c8 DW |
111 | SubclassWin(m_hWnd); |
112 | wxControl::SetFont(pParent->GetFont()); | |
3c299c3a DW |
113 | SetSize( nX |
114 | ,nY | |
115 | ,nWidth | |
116 | ,nHeight | |
117 | ); | |
118 | return TRUE; | |
1f3154c8 | 119 | } // end of wxStaticText::Create |
0e320a79 | 120 | |
e78c4d50 | 121 | wxSize wxStaticText::DoGetBestSize() const |
0e320a79 | 122 | { |
1f3154c8 DW |
123 | wxString sText(wxGetWindowText(GetHWND())); |
124 | int nWidthTextMax = 0; | |
125 | int nWidthLine = 0; | |
126 | int nHeightTextTotal = 0; | |
127 | int nHeightLineDefault = 0; | |
128 | int nHeightLine = 0; | |
129 | wxString sCurLine; | |
130 | ||
3c299c3a | 131 | for (const wxChar *pc = sText; ; pc++) |
1f3154c8 DW |
132 | { |
133 | if ( *pc == wxT('\n') || *pc == wxT('\0') ) | |
134 | { | |
135 | if (!sCurLine ) | |
136 | { | |
137 | // | |
138 | // We can't use GetTextExtent - it will return 0 for both width | |
e78c4d50 DW |
139 | // and height and an empty line should count in height |
140 | // calculation | |
1f3154c8 DW |
141 | // |
142 | if (!nHeightLineDefault) | |
143 | nHeightLineDefault = nHeightLine; | |
144 | if (!nHeightLineDefault) | |
145 | GetTextExtent(_T("W"), NULL, &nHeightLineDefault); | |
146 | nHeightTextTotal += nHeightLineDefault; | |
e78c4d50 | 147 | } |
1f3154c8 DW |
148 | else |
149 | { | |
150 | GetTextExtent( sCurLine | |
151 | ,&nWidthLine | |
152 | ,&nHeightLine | |
153 | ); | |
154 | if (nWidthLine > nWidthTextMax) | |
155 | nWidthTextMax = nWidthLine; | |
156 | nHeightTextTotal += nHeightLine; | |
e78c4d50 | 157 | } |
d90895ac | 158 | |
1f3154c8 DW |
159 | if ( *pc == wxT('\n') ) |
160 | { | |
161 | sCurLine.Empty(); | |
d90895ac | 162 | } |
1f3154c8 DW |
163 | else |
164 | { | |
d90895ac DW |
165 | break; |
166 | } | |
167 | } | |
1f3154c8 DW |
168 | else |
169 | { | |
170 | sCurLine += *pc; | |
d90895ac DW |
171 | } |
172 | } | |
1f3154c8 DW |
173 | return wxSize( nWidthTextMax |
174 | ,nHeightTextTotal | |
175 | ); | |
176 | } // end of wxStaticText::DoGetBestSize | |
177 | ||
7993e67c DW |
178 | void wxStaticText::DoSetSize( |
179 | int nX | |
180 | , int nY | |
181 | , int nWidth | |
182 | , int nHeight | |
183 | , int nSizeFlags | |
184 | ) | |
185 | { | |
186 | // | |
187 | // We need to refresh the window after changing its size as the standard | |
188 | // control doesn't always update itself properly. | |
189 | // | |
190 | wxStaticTextBase::DoSetSize( nX | |
191 | ,nY | |
192 | ,nWidth | |
193 | ,nHeight | |
194 | ,nSizeFlags | |
195 | ); | |
196 | Refresh(); | |
197 | } // end of wxStaticText::DoSetSize | |
198 | ||
3c299c3a DW |
199 | bool wxStaticText::SetFont( |
200 | const wxFont& rFont | |
1f3154c8 | 201 | ) |
0e320a79 | 202 | { |
3c299c3a | 203 | bool bRet = wxControl::SetFont(rFont); |
1f3154c8 DW |
204 | |
205 | // | |
206 | // Adjust the size of the window to fit to the label unless autoresizing is | |
207 | // disabled | |
208 | // | |
3c299c3a | 209 | if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) ) |
1f3154c8 DW |
210 | { |
211 | DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT); | |
212 | } | |
3c299c3a DW |
213 | return bRet; |
214 | } // end of wxStaticText::SetFont | |
d90895ac | 215 | |
3c299c3a DW |
216 | void wxStaticText::SetLabel( |
217 | const wxString& rsLabel | |
1f3154c8 | 218 | ) |
d90895ac | 219 | { |
3c299c3a | 220 | ::WinSetWindowText(GetHwnd(), rsLabel.c_str()); |
1f3154c8 DW |
221 | |
222 | // | |
223 | // Adjust the size of the window to fit to the label unless autoresizing is | |
224 | // disabled | |
225 | // | |
3c299c3a | 226 | if (!(GetWindowStyle() & wxST_NO_AUTORESIZE)) |
1f3154c8 DW |
227 | { |
228 | DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT); | |
229 | } | |
3c299c3a DW |
230 | DoSetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH | wxSIZE_AUTO_HEIGHT); |
231 | } // end of wxStaticText::SetLabel | |
1f3154c8 DW |
232 | |
233 | MRESULT wxStaticText::OS2WindowProc( | |
234 | WXUINT uMsg | |
235 | , WXWPARAM wParam | |
236 | , WXLPARAM lParam | |
237 | ) | |
d90895ac | 238 | { |
1f3154c8 DW |
239 | return wxWindow::OS2WindowProc( uMsg |
240 | ,wParam | |
241 | ,lParam | |
242 | ); | |
243 | } // end of wxStaticText::OS2WindowProc | |
244 | ||
0e320a79 | 245 |