]> git.saurik.com Git - wxWidgets.git/blame - src/mac/stattext.cpp
wxMac merge
[wxWidgets.git] / src / mac / stattext.cpp
CommitLineData
e9576ca5
SC
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
2f1ae414 21#if !USE_SHARED_LIBRARY
e9576ca5 22IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxControl)
2f1ae414 23#endif
e9576ca5 24
519cb848
SC
25#include <wx/mac/uma.h>
26
8208e181
SC
27BEGIN_EVENT_TABLE(wxStaticText, wxControl)
28 EVT_PAINT(wxStaticText::OnPaint)
29END_EVENT_TABLE()
30
31bool 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
2f1ae414 56void wxStaticText::OnDraw( wxDC &dc )
8208e181 57{
8208e181
SC
58 PrepareDC(dc);
59 dc.Clear() ;
2f1ae414
SC
60
61 int x = 0 ;
62 int y = 0 ;
63 wxString text = m_label ;
64 wxString paragraph ;
65 int i = 0 ;
66 int laststop = 0 ;
67 long width, height ;
8208e181 68
2f1ae414
SC
69 while( i < text.Length() )
70 {
71 if( text[i] == 13 || text[i] == 10)
72 {
73 paragraph = text.Mid( laststop , i - laststop ) ;
74 while( paragraph.Length() > 0 )
75 {
76 dc.GetTextExtent( paragraph , &width , &height ) ;
77 if ( width > m_width )
78 {
79 for ( int p = paragraph.Length() -1 ; p > 0 ; --p )
80 {
81 if ( paragraph[p]=='.' )
82 {
83 dc.GetTextExtent( paragraph.Left(p+1) , &width , &height ) ;
84 if ( width <= m_width )
85 {
86 int pos = x ;
87 if ( HasFlag( wxALIGN_CENTER ) )
88 {
89 pos += ( m_width - width ) / 2 ;
90 }
91 else if ( HasFlag( wxALIGN_RIGHT ) )
92 {
93 pos += ( m_width - width ) ;
94 }
95 dc.DrawText( paragraph.Left(p+1), pos , y) ;
96 y += height ;
97 paragraph = paragraph.Mid(p+1) ;
98 break ;
99 }
100 }
101 if ( paragraph[p]==' ' )
102 {
103 dc.GetTextExtent( paragraph.Left(p) , &width , &height ) ;
104 if ( width <= m_width )
105 {
106 int pos = x ;
107 if ( HasFlag( wxALIGN_CENTER ) )
108 {
109 pos += ( m_width - width ) / 2 ;
110 }
111 else if ( HasFlag( wxALIGN_RIGHT ) )
112 {
113 pos += ( m_width - width ) ;
114 }
115 dc.DrawText( paragraph.Left(p), pos , y) ;
116 y += height ;
117 paragraph = paragraph.Mid(p+1) ;
118 break ;
119 }
120 }
121 }
122 }
123 else
124 {
125 dc.DrawText( paragraph, x , y) ;
126 paragraph="";
127 y += height ;
128 }
129 }
130 laststop = i+1 ;
131 }
132 ++i ;
133 }
134 paragraph = text.Mid( laststop , text.Length() - laststop ) ;
135 while( paragraph.Length() > 0 )
136 {
137 dc.GetTextExtent( paragraph , &width , &height ) ;
138 if ( width > m_width )
139 {
140 for ( int p = paragraph.Length() -1 ; p > 0 ; --p )
141 {
142 if ( paragraph[p]=='.' )
143 {
144 dc.GetTextExtent( paragraph.Left(p+1) , &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+1), pos , y) ;
157 y += height ;
158 paragraph = paragraph.Mid(p+1) ;
159 break ;
160 }
161 }
162 if ( paragraph[p]==' ' )
163 {
164 dc.GetTextExtent( paragraph.Left(p) , &width , &height ) ;
165 if ( width <= m_width )
166 {
167 int pos = x ;
168 if ( HasFlag( wxALIGN_CENTER ) )
169 {
170 pos += ( m_width - width ) / 2 ;
171 }
172 else if ( HasFlag( wxALIGN_RIGHT ) )
173 {
174 pos += ( m_width - width ) ;
175 }
176 dc.DrawText( paragraph.Left(p), pos , y) ;
177 y += height ;
178 paragraph = paragraph.Mid(p+1) ;
179 break ;
180 }
181 }
182 }
183 }
184 else
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, pos , y) ;
196 paragraph="";
197 y += height ;
198 }
199 }
8208e181
SC
200}
201
2f1ae414 202void wxStaticText::OnPaint( wxPaintEvent &event )
8208e181 203{
2f1ae414
SC
204 wxPaintDC dc(this);
205 OnDraw( dc ) ;
8208e181
SC
206}
207
2f1ae414 208wxSize wxStaticText::DoGetBestSize() const
e9576ca5 209{
2f1ae414
SC
210 int x , y ;
211 int widthTextMax = 0, widthLine,
212 heightTextTotal = 0, heightLineDefault = 0, heightLine = 0;
519cb848 213
2f1ae414
SC
214 wxString curLine;
215 for ( const wxChar *pc = m_label; ; pc++ ) {
216 if ( *pc == wxT('\n') || *pc == wxT('\0') ) {
217 if ( !curLine ) {
218 // we can't use GetTextExtent - it will return 0 for both width
219 // and height and an empty line should count in height
220 // calculation
221 if ( !heightLineDefault )
222 heightLineDefault = heightLine;
223 if ( !heightLineDefault )
224 GetTextExtent(_T("W"), NULL, &heightLineDefault);
225
226 heightTextTotal += heightLineDefault;
227 }
228 else {
229 GetTextExtent(curLine, &widthLine, &heightLine);
230 if ( widthLine > widthTextMax )
231 widthTextMax = widthLine;
232 heightTextTotal += heightLine;
233 }
234
235 if ( *pc == wxT('\n') ) {
236 curLine.Empty();
237 }
238 else {
239 // the end of string
240 break;
241 }
242 }
243 else {
244 curLine += *pc;
245 }
246 }
519cb848 247
2f1ae414 248 return wxSize(widthTextMax, heightTextTotal);
e9576ca5
SC
249}
250
2f1ae414 251void wxStaticText::SetLabel(const wxString& st )
e9576ca5 252{
e7549107 253 SetTitle( st ) ;
2f1ae414
SC
254 m_label = st ;
255 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) )
256 SetSizeOrDefault() ;
519cb848 257
2f1ae414
SC
258 wxClientDC dc(this);
259 OnDraw( dc ) ;
e9576ca5 260}