]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/stattext.cpp
another attempt to fix wxPanel/wxFrame::m_winLastFocused handling
[wxWidgets.git] / src / mac / carbon / stattext.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: stattext.cpp
3 // Purpose: wxStaticText
4 // Author: AUTHOR
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "stattext.h"
14 #endif
15
16 #include "wx/app.h"
17 #include "wx/stattext.h"
18 #include "wx/notebook.h"
19 #include "wx/tabctrl.h"
20 #include "wx/dc.h"
21 #include "wx/dcclient.h"
22
23 #include <stdio.h>
24
25 #if !USE_SHARED_LIBRARY
26 IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxControl)
27 #endif
28
29 #include <wx/mac/uma.h>
30
31 BEGIN_EVENT_TABLE(wxStaticText, wxControl)
32 EVT_PAINT(wxStaticText::OnPaint)
33 END_EVENT_TABLE()
34
35 bool wxStaticText::Create(wxWindow *parent, wxWindowID id,
36 const wxString& label,
37 const wxPoint& pos,
38 const wxSize& size,
39 long style,
40 const wxString& name)
41 {
42 SetName(name);
43 m_backgroundColour = parent->GetBackgroundColour() ;
44 m_foregroundColour = parent->GetForegroundColour() ;
45
46 if ( id == -1 )
47 m_windowId = (int)NewControlId();
48 else
49 m_windowId = id;
50
51 m_windowStyle = style;
52 m_label = label ;
53
54 bool ret = wxControl::Create( parent, id, pos, size, style , wxDefaultValidator , name );
55 SetSizeOrDefault( size ) ;
56
57 return ret;
58 }
59
60 void wxStaticText::OnDraw( wxDC &dc )
61 {
62 if (m_width <= 0 || m_height <= 0)
63 return;
64
65 PrepareDC(dc);
66
67 bool doClear = true ;
68 WindowRef window = GetMacRootWindow() ;
69 if ( window )
70 {
71 wxWindow* win = wxFindWinFromMacWindow( window ) ;
72 if ( win )
73 {
74 wxWindow* parent = GetParent() ;
75 while ( parent )
76 {
77 if( parent->MacGetWindowData() )
78 {
79 break ;
80 }
81
82 if( parent->IsKindOf( CLASSINFO( wxNotebook ) ) || parent->IsKindOf( CLASSINFO( wxTabCtrl ) ))
83 {
84 if ( ((wxControl*)parent)->GetMacControl() ) {
85 Rect rect = { -32767 , -32767 , 32767 , 32767 } ;
86 if ( DrawThemeTabPane != (void*)kUnresolvedCFragSymbolAddress )
87 {
88 DrawThemeTabPane ( &rect, kThemeStateActive);
89 doClear = false ;
90 }
91 }
92 break ;
93 }
94
95 parent = parent->GetParent() ;
96 }
97 }
98 }
99 if ( doClear )
100 dc.Clear() ;
101
102 int x = 0 ;
103 int y = 0 ;
104 wxString text = m_label ;
105 wxString paragraph ;
106 int i = 0 ;
107 int laststop = 0 ;
108 long width, height ;
109
110 while( i < text.Length() )
111 {
112 if( text[i] == 13 || text[i] == 10)
113 {
114 paragraph = text.Mid( laststop , i - laststop ) ;
115 while( paragraph.Length() > 0 )
116 {
117 dc.GetTextExtent( paragraph , &width , &height ) ;
118 if ( width > m_width )
119 {
120 for ( int p = paragraph.Length() -1 ; p > 0 ; --p )
121 {
122 if ( paragraph[p]=='.' )
123 {
124 dc.GetTextExtent( paragraph.Left(p+1) , &width , &height ) ;
125 if ( width <= m_width )
126 {
127 int pos = x ;
128 if ( HasFlag( wxALIGN_CENTER ) )
129 {
130 pos += ( m_width - width ) / 2 ;
131 }
132 else if ( HasFlag( wxALIGN_RIGHT ) )
133 {
134 pos += ( m_width - width ) ;
135 }
136 dc.DrawText( paragraph.Left(p+1), pos , y) ;
137 y += height ;
138 paragraph = paragraph.Mid(p+1) ;
139 break ;
140 }
141 }
142 if ( paragraph[p]==' ' )
143 {
144 dc.GetTextExtent( paragraph.Left(p) , &width , &height ) ;
145 if ( width <= m_width )
146 {
147 int pos = x ;
148 if ( HasFlag( wxALIGN_CENTER ) )
149 {
150 pos += ( m_width - width ) / 2 ;
151 }
152 else if ( HasFlag( wxALIGN_RIGHT ) )
153 {
154 pos += ( m_width - width ) ;
155 }
156 dc.DrawText( paragraph.Left(p), pos , y) ;
157 y += height ;
158 paragraph = paragraph.Mid(p+1) ;
159 break ;
160 }
161 }
162 }
163 }
164 else
165 {
166 dc.DrawText( paragraph, x , y) ;
167 paragraph="";
168 y += height ;
169 }
170 }
171 laststop = i+1 ;
172 }
173 ++i ;
174 }
175 paragraph = text.Mid( laststop , text.Length() - laststop ) ;
176 while( paragraph.Length() > 0 )
177 {
178 dc.GetTextExtent( paragraph , &width , &height ) ;
179 if ( width > m_width )
180 {
181 for ( int p = paragraph.Length() -1 ; p > 0 ; --p )
182 {
183 if ( paragraph[p]=='.' )
184 {
185 dc.GetTextExtent( paragraph.Left(p+1) , &width , &height ) ;
186 if ( width <= m_width )
187 {
188 int pos = x ;
189 if ( HasFlag( wxALIGN_CENTER ) )
190 {
191 pos += ( m_width - width ) / 2 ;
192 }
193 else if ( HasFlag( wxALIGN_RIGHT ) )
194 {
195 pos += ( m_width - width ) ;
196 }
197 dc.DrawText( paragraph.Left(p+1), pos , y) ;
198 y += height ;
199 paragraph = paragraph.Mid(p+1) ;
200 break ;
201 }
202 }
203 if ( paragraph[p]==' ' )
204 {
205 dc.GetTextExtent( paragraph.Left(p) , &width , &height ) ;
206 if ( width <= m_width )
207 {
208 int pos = x ;
209 if ( HasFlag( wxALIGN_CENTER ) )
210 {
211 pos += ( m_width - width ) / 2 ;
212 }
213 else if ( HasFlag( wxALIGN_RIGHT ) )
214 {
215 pos += ( m_width - width ) ;
216 }
217 dc.DrawText( paragraph.Left(p), pos , y) ;
218 y += height ;
219 paragraph = paragraph.Mid(p+1) ;
220 break ;
221 }
222 }
223 }
224 }
225 else
226 {
227 int pos = x ;
228 if ( HasFlag( wxALIGN_CENTER ) )
229 {
230 pos += ( m_width - width ) / 2 ;
231 }
232 else if ( HasFlag( wxALIGN_RIGHT ) )
233 {
234 pos += ( m_width - width ) ;
235 }
236 dc.DrawText( paragraph, pos , y) ;
237 paragraph="";
238 y += height ;
239 }
240 }
241 }
242
243 void wxStaticText::OnPaint( wxPaintEvent &event )
244 {
245 wxPaintDC dc(this);
246 OnDraw( dc ) ;
247 }
248
249 wxSize wxStaticText::DoGetBestSize() const
250 {
251 int x , y ;
252 int widthTextMax = 0, widthLine,
253 heightTextTotal = 0, heightLineDefault = 0, heightLine = 0;
254
255 wxString curLine;
256 for ( const wxChar *pc = m_label; ; pc++ ) {
257 if ( *pc == wxT('\n') || *pc == wxT('\0') ) {
258 if ( !curLine ) {
259 // we can't use GetTextExtent - it will return 0 for both width
260 // and height and an empty line should count in height
261 // calculation
262 if ( !heightLineDefault )
263 heightLineDefault = heightLine;
264 if ( !heightLineDefault )
265 GetTextExtent(_T("W"), NULL, &heightLineDefault);
266
267 heightTextTotal += heightLineDefault;
268 }
269 else {
270 GetTextExtent(curLine, &widthLine, &heightLine);
271 if ( widthLine > widthTextMax )
272 widthTextMax = widthLine;
273 heightTextTotal += heightLine;
274 }
275
276 if ( *pc == wxT('\n') ) {
277 curLine.Empty();
278 }
279 else {
280 // the end of string
281 break;
282 }
283 }
284 else {
285 curLine += *pc;
286 }
287 }
288
289 return wxSize(widthTextMax, heightTextTotal);
290 }
291
292 void wxStaticText::SetLabel(const wxString& st )
293 {
294 SetTitle( st ) ;
295 m_label = st ;
296 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) )
297 SetSizeOrDefault() ;
298
299 Refresh() ;
300 MacUpdateImmediately() ;
301 // wxClientDC dc(this);
302 // OnDraw( dc ) ;
303 }