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