]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/stattext.cpp
corrections for Mac OS X
[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/dc.h"
19 #include "wx/dcclient.h"
20
21 #include <stdio.h>
22
23 #if !USE_SHARED_LIBRARY
24 IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxControl)
25 #endif
26
27 #include <wx/mac/uma.h>
28
29 BEGIN_EVENT_TABLE(wxStaticText, wxControl)
30 EVT_PAINT(wxStaticText::OnPaint)
31 END_EVENT_TABLE()
32
33 bool wxStaticText::Create(wxWindow *parent, wxWindowID id,
34 const wxString& label,
35 const wxPoint& pos,
36 const wxSize& size,
37 long style,
38 const wxString& name)
39 {
40 SetName(name);
41 m_backgroundColour = parent->GetBackgroundColour() ;
42 m_foregroundColour = parent->GetForegroundColour() ;
43
44 if ( id == -1 )
45 m_windowId = (int)NewControlId();
46 else
47 m_windowId = id;
48
49 m_windowStyle = style;
50 m_label = label ;
51
52 bool ret = wxControl::Create( parent, id, pos, size, style , wxDefaultValidator , name );
53 SetSizeOrDefault( size ) ;
54
55 return ret;
56 }
57
58 void wxStaticText::OnDraw( wxDC &dc )
59 {
60 if (m_width <= 0 || m_height <= 0)
61 return;
62
63 PrepareDC(dc);
64 dc.Clear() ;
65
66 int x = 0 ;
67 int y = 0 ;
68 wxString text = m_label ;
69 wxString paragraph ;
70 int i = 0 ;
71 int laststop = 0 ;
72 long width, height ;
73
74 while( i < text.Length() )
75 {
76 if( text[i] == 13 || text[i] == 10)
77 {
78 paragraph = text.Mid( laststop , i - laststop ) ;
79 while( paragraph.Length() > 0 )
80 {
81 dc.GetTextExtent( paragraph , &width , &height ) ;
82 if ( width > m_width )
83 {
84 for ( int p = paragraph.Length() -1 ; p > 0 ; --p )
85 {
86 if ( paragraph[p]=='.' )
87 {
88 dc.GetTextExtent( paragraph.Left(p+1) , &width , &height ) ;
89 if ( width <= m_width )
90 {
91 int pos = x ;
92 if ( HasFlag( wxALIGN_CENTER ) )
93 {
94 pos += ( m_width - width ) / 2 ;
95 }
96 else if ( HasFlag( wxALIGN_RIGHT ) )
97 {
98 pos += ( m_width - width ) ;
99 }
100 dc.DrawText( paragraph.Left(p+1), pos , y) ;
101 y += height ;
102 paragraph = paragraph.Mid(p+1) ;
103 break ;
104 }
105 }
106 if ( paragraph[p]==' ' )
107 {
108 dc.GetTextExtent( paragraph.Left(p) , &width , &height ) ;
109 if ( width <= m_width )
110 {
111 int pos = x ;
112 if ( HasFlag( wxALIGN_CENTER ) )
113 {
114 pos += ( m_width - width ) / 2 ;
115 }
116 else if ( HasFlag( wxALIGN_RIGHT ) )
117 {
118 pos += ( m_width - width ) ;
119 }
120 dc.DrawText( paragraph.Left(p), pos , y) ;
121 y += height ;
122 paragraph = paragraph.Mid(p+1) ;
123 break ;
124 }
125 }
126 }
127 }
128 else
129 {
130 dc.DrawText( paragraph, x , y) ;
131 paragraph="";
132 y += height ;
133 }
134 }
135 laststop = i+1 ;
136 }
137 ++i ;
138 }
139 paragraph = text.Mid( laststop , text.Length() - laststop ) ;
140 while( paragraph.Length() > 0 )
141 {
142 dc.GetTextExtent( paragraph , &width , &height ) ;
143 if ( width > m_width )
144 {
145 for ( int p = paragraph.Length() -1 ; p > 0 ; --p )
146 {
147 if ( paragraph[p]=='.' )
148 {
149 dc.GetTextExtent( paragraph.Left(p+1) , &width , &height ) ;
150 if ( width <= m_width )
151 {
152 int pos = x ;
153 if ( HasFlag( wxALIGN_CENTER ) )
154 {
155 pos += ( m_width - width ) / 2 ;
156 }
157 else if ( HasFlag( wxALIGN_RIGHT ) )
158 {
159 pos += ( m_width - width ) ;
160 }
161 dc.DrawText( paragraph.Left(p+1), pos , y) ;
162 y += height ;
163 paragraph = paragraph.Mid(p+1) ;
164 break ;
165 }
166 }
167 if ( paragraph[p]==' ' )
168 {
169 dc.GetTextExtent( paragraph.Left(p) , &width , &height ) ;
170 if ( width <= m_width )
171 {
172 int pos = x ;
173 if ( HasFlag( wxALIGN_CENTER ) )
174 {
175 pos += ( m_width - width ) / 2 ;
176 }
177 else if ( HasFlag( wxALIGN_RIGHT ) )
178 {
179 pos += ( m_width - width ) ;
180 }
181 dc.DrawText( paragraph.Left(p), pos , y) ;
182 y += height ;
183 paragraph = paragraph.Mid(p+1) ;
184 break ;
185 }
186 }
187 }
188 }
189 else
190 {
191 int pos = x ;
192 if ( HasFlag( wxALIGN_CENTER ) )
193 {
194 pos += ( m_width - width ) / 2 ;
195 }
196 else if ( HasFlag( wxALIGN_RIGHT ) )
197 {
198 pos += ( m_width - width ) ;
199 }
200 dc.DrawText( paragraph, pos , y) ;
201 paragraph="";
202 y += height ;
203 }
204 }
205 }
206
207 void wxStaticText::OnPaint( wxPaintEvent &event )
208 {
209 wxPaintDC dc(this);
210 OnDraw( dc ) ;
211 }
212
213 wxSize wxStaticText::DoGetBestSize() const
214 {
215 int x , y ;
216 int widthTextMax = 0, widthLine,
217 heightTextTotal = 0, heightLineDefault = 0, heightLine = 0;
218
219 wxString curLine;
220 for ( const wxChar *pc = m_label; ; pc++ ) {
221 if ( *pc == wxT('\n') || *pc == wxT('\0') ) {
222 if ( !curLine ) {
223 // we can't use GetTextExtent - it will return 0 for both width
224 // and height and an empty line should count in height
225 // calculation
226 if ( !heightLineDefault )
227 heightLineDefault = heightLine;
228 if ( !heightLineDefault )
229 GetTextExtent(_T("W"), NULL, &heightLineDefault);
230
231 heightTextTotal += heightLineDefault;
232 }
233 else {
234 GetTextExtent(curLine, &widthLine, &heightLine);
235 if ( widthLine > widthTextMax )
236 widthTextMax = widthLine;
237 heightTextTotal += heightLine;
238 }
239
240 if ( *pc == wxT('\n') ) {
241 curLine.Empty();
242 }
243 else {
244 // the end of string
245 break;
246 }
247 }
248 else {
249 curLine += *pc;
250 }
251 }
252
253 return wxSize(widthTextMax, heightTextTotal);
254 }
255
256 void wxStaticText::SetLabel(const wxString& st )
257 {
258 SetTitle( st ) ;
259 m_label = st ;
260 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) )
261 SetSizeOrDefault() ;
262
263 wxClientDC dc(this);
264 OnDraw( dc ) ;
265 }