]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/stattext.cpp
reverting to native mac pathnames
[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 DrawThemeTabPane ( &rect, kThemeStateActive);
88 doClear = false ;
89 }
90 break ;
91 }
92
93 parent = parent->GetParent() ;
94 }
95 }
96 }
97 if ( doClear )
98 dc.Clear() ;
99
100 int x = 0 ;
101 int y = 0 ;
102 wxString text = m_label ;
103 wxString paragraph ;
104 int i = 0 ;
105 int laststop = 0 ;
106 long width, height ;
107
108 while( i < text.Length() )
109 {
110 if( text[i] == 13 || text[i] == 10)
111 {
112 paragraph = text.Mid( laststop , i - laststop ) ;
113 while( paragraph.Length() > 0 )
114 {
115 dc.GetTextExtent( paragraph , &width , &height ) ;
116 if ( width > m_width )
117 {
118 for ( int p = paragraph.Length() -1 ; p > 0 ; --p )
119 {
120 if ( paragraph[p]=='.' )
121 {
122 dc.GetTextExtent( paragraph.Left(p+1) , &width , &height ) ;
123 if ( width <= m_width )
124 {
125 int pos = x ;
126 if ( HasFlag( wxALIGN_CENTER ) )
127 {
128 pos += ( m_width - width ) / 2 ;
129 }
130 else if ( HasFlag( wxALIGN_RIGHT ) )
131 {
132 pos += ( m_width - width ) ;
133 }
134 dc.DrawText( paragraph.Left(p+1), pos , y) ;
135 y += height ;
136 paragraph = paragraph.Mid(p+1) ;
137 break ;
138 }
139 }
140 if ( paragraph[p]==' ' )
141 {
142 dc.GetTextExtent( paragraph.Left(p) , &width , &height ) ;
143 if ( width <= m_width )
144 {
145 int pos = x ;
146 if ( HasFlag( wxALIGN_CENTER ) )
147 {
148 pos += ( m_width - width ) / 2 ;
149 }
150 else if ( HasFlag( wxALIGN_RIGHT ) )
151 {
152 pos += ( m_width - width ) ;
153 }
154 dc.DrawText( paragraph.Left(p), pos , y) ;
155 y += height ;
156 paragraph = paragraph.Mid(p+1) ;
157 break ;
158 }
159 }
160 }
161 }
162 else
163 {
164 dc.DrawText( paragraph, x , y) ;
165 paragraph="";
166 y += height ;
167 }
168 }
169 laststop = i+1 ;
170 }
171 ++i ;
172 }
173 paragraph = text.Mid( laststop , text.Length() - laststop ) ;
174 while( paragraph.Length() > 0 )
175 {
176 dc.GetTextExtent( paragraph , &width , &height ) ;
177 if ( width > m_width )
178 {
179 for ( int p = paragraph.Length() -1 ; p > 0 ; --p )
180 {
181 if ( paragraph[p]=='.' )
182 {
183 dc.GetTextExtent( paragraph.Left(p+1) , &width , &height ) ;
184 if ( width <= m_width )
185 {
186 int pos = x ;
187 if ( HasFlag( wxALIGN_CENTER ) )
188 {
189 pos += ( m_width - width ) / 2 ;
190 }
191 else if ( HasFlag( wxALIGN_RIGHT ) )
192 {
193 pos += ( m_width - width ) ;
194 }
195 dc.DrawText( paragraph.Left(p+1), pos , y) ;
196 y += height ;
197 paragraph = paragraph.Mid(p+1) ;
198 break ;
199 }
200 }
201 if ( paragraph[p]==' ' )
202 {
203 dc.GetTextExtent( paragraph.Left(p) , &width , &height ) ;
204 if ( width <= m_width )
205 {
206 int pos = x ;
207 if ( HasFlag( wxALIGN_CENTER ) )
208 {
209 pos += ( m_width - width ) / 2 ;
210 }
211 else if ( HasFlag( wxALIGN_RIGHT ) )
212 {
213 pos += ( m_width - width ) ;
214 }
215 dc.DrawText( paragraph.Left(p), pos , y) ;
216 y += height ;
217 paragraph = paragraph.Mid(p+1) ;
218 break ;
219 }
220 }
221 }
222 }
223 else
224 {
225 int pos = x ;
226 if ( HasFlag( wxALIGN_CENTER ) )
227 {
228 pos += ( m_width - width ) / 2 ;
229 }
230 else if ( HasFlag( wxALIGN_RIGHT ) )
231 {
232 pos += ( m_width - width ) ;
233 }
234 dc.DrawText( paragraph, pos , y) ;
235 paragraph="";
236 y += height ;
237 }
238 }
239 }
240
241 void wxStaticText::OnPaint( wxPaintEvent &event )
242 {
243 wxPaintDC dc(this);
244 OnDraw( dc ) ;
245 }
246
247 wxSize wxStaticText::DoGetBestSize() const
248 {
249 int x , y ;
250 int widthTextMax = 0, widthLine,
251 heightTextTotal = 0, heightLineDefault = 0, heightLine = 0;
252
253 wxString curLine;
254 for ( const wxChar *pc = m_label; ; pc++ ) {
255 if ( *pc == wxT('\n') || *pc == wxT('\0') ) {
256 if ( !curLine ) {
257 // we can't use GetTextExtent - it will return 0 for both width
258 // and height and an empty line should count in height
259 // calculation
260 if ( !heightLineDefault )
261 heightLineDefault = heightLine;
262 if ( !heightLineDefault )
263 GetTextExtent(_T("W"), NULL, &heightLineDefault);
264
265 heightTextTotal += heightLineDefault;
266 }
267 else {
268 GetTextExtent(curLine, &widthLine, &heightLine);
269 if ( widthLine > widthTextMax )
270 widthTextMax = widthLine;
271 heightTextTotal += heightLine;
272 }
273
274 if ( *pc == wxT('\n') ) {
275 curLine.Empty();
276 }
277 else {
278 // the end of string
279 break;
280 }
281 }
282 else {
283 curLine += *pc;
284 }
285 }
286
287 return wxSize(widthTextMax, heightTextTotal);
288 }
289
290 void wxStaticText::SetLabel(const wxString& st )
291 {
292 SetTitle( st ) ;
293 m_label = st ;
294 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) )
295 SetSizeOrDefault() ;
296
297 Refresh() ;
298 MacUpdateImmediately() ;
299 // wxClientDC dc(this);
300 // OnDraw( dc ) ;
301 }