]> git.saurik.com Git - wxWidgets.git/blob - src/mac/classic/stattext.cpp
50acd4a904e4ef489050708d51337ff4698f92cd
[wxWidgets.git] / src / mac / classic / stattext.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: stattext.cpp
3 // Purpose: wxStaticText
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
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 #include "wx/utils.h"
23 #include "wx/settings.h"
24
25 #include <stdio.h>
26
27 IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxControl)
28
29 #include "wx/mac/uma.h"
30
31 BEGIN_EVENT_TABLE(wxStaticText, wxStaticTextBase)
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 m_label = wxStripMenuCodes(label) ;
43
44 if ( !wxControl::Create( parent, id, pos, size, style,
45 wxDefaultValidator , name ) )
46 {
47 return false;
48 }
49
50 SetBestSize( size ) ;
51
52 return true;
53 }
54
55 const wxString punct = wxT(" ,.-;:!?");
56
57 void wxStaticText::DrawParagraph(wxDC &dc, wxString paragraph, int &y)
58 {
59 long width, height ;
60
61 if (paragraph.Length() == 0)
62 {
63 // empty line
64 dc.GetTextExtent( wxT("H"), &width, &height );
65 y += height;
66
67 return;
68 }
69
70 int x = 0 ;
71
72 bool linedrawn = true;
73 while( paragraph.Length() > 0 )
74 {
75 dc.GetTextExtent( paragraph , &width , &height ) ;
76
77 if ( width > m_width )
78 {
79 for ( size_t p = paragraph.Length() - 1 ; p > 0 ; --p )
80 {
81 if ((punct.Find(paragraph[p]) != wxNOT_FOUND) || !linedrawn)
82 {
83 int blank = (paragraph[p] == ' ') ? 0 : 1;
84
85 dc.GetTextExtent( paragraph.Left(p + blank) , &width , &height ) ;
86
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
99 dc.DrawText( paragraph.Left(p + blank), pos , y) ;
100 y += height ;
101 paragraph = paragraph.Mid(p+1) ;
102 linedrawn = true;
103 break ;
104 }
105 }
106 }
107
108 linedrawn = false;
109 }
110 else
111 {
112 int pos = x ;
113 if ( HasFlag( wxALIGN_CENTER ) )
114 {
115 pos += ( m_width - width ) / 2 ;
116 }
117 else if ( HasFlag( wxALIGN_RIGHT ) )
118 {
119 pos += ( m_width - width ) ;
120 }
121
122 dc.DrawText( paragraph, pos , y) ;
123 paragraph=wxEmptyString;
124 y += height ;
125 }
126 }
127 }
128
129 void wxStaticText::OnDraw( wxDC &dc )
130 {
131 if (m_width <= 0 || m_height <= 0)
132 return;
133 /*
134 dc.Clear() ;
135 wxRect rect(0,0,m_width,m_height) ;
136 dc.SetFont(*wxSMALL_FONT) ;
137
138 dc.DrawRectangle(rect) ;
139 */
140 if ( !IsWindowHilited( (WindowRef) MacGetRootWindow() ) &&
141 ( GetBackgroundColour() == wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE )
142 || GetBackgroundColour() == wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE) ) )
143 {
144 dc.SetTextForeground( wxColour( 0x80 , 0x80 , 0x80 ) ) ;
145 }
146 else
147 {
148 dc.SetTextForeground( GetForegroundColour() ) ;
149 }
150
151 wxString paragraph;
152 size_t i = 0 ;
153 wxString text = m_label;
154 int y = 0 ;
155 while (i < text.Length())
156 {
157
158 if (text[i] == 13 || text[i] == 10)
159 {
160 DrawParagraph(dc, paragraph,y);
161 paragraph = wxEmptyString ;
162 }
163 else
164 {
165 paragraph += text[i];
166 }
167 ++i;
168 }
169 if (paragraph.Length() > 0)
170 DrawParagraph(dc, paragraph,y);
171 }
172
173 void wxStaticText::OnPaint( wxPaintEvent & WXUNUSED(event) )
174 {
175 wxPaintDC dc(this);
176 OnDraw( dc ) ;
177 }
178
179 wxSize wxStaticText::DoGetBestSize() const
180 {
181 int widthTextMax = 0, widthLine,
182 heightTextTotal = 0, heightLineDefault = 0, heightLine = 0;
183
184 wxString curLine;
185 for ( const wxChar *pc = m_label; ; pc++ )
186 {
187 if ( *pc == wxT('\n') || *pc == wxT('\r') || *pc == wxT('\0') )
188 {
189 if ( !curLine )
190 {
191 // we can't use GetTextExtent - it will return 0 for both width
192 // and height and an empty line should count in height
193 // calculation
194 if ( !heightLineDefault )
195 heightLineDefault = heightLine;
196 if ( !heightLineDefault )
197 GetTextExtent(_T("W"), NULL, &heightLineDefault);
198
199 heightTextTotal += heightLineDefault;
200
201 heightTextTotal++; // FIXME: why is this necessary?
202 }
203 else
204 {
205 GetTextExtent(curLine, &widthLine, &heightLine);
206 if ( widthLine > widthTextMax )
207 widthTextMax = widthLine;
208 heightTextTotal += heightLine;
209
210 heightTextTotal++; // FIXME: why is this necessary?
211 }
212
213 if ( *pc == wxT('\n') || *pc == wxT('\r')) {
214 curLine.Empty();
215 }
216 else {
217 // the end of string
218 break;
219 }
220 }
221 else {
222 curLine += *pc;
223 }
224 }
225
226 return wxSize(widthTextMax, heightTextTotal);
227 }
228
229 void wxStaticText::SetLabel(const wxString& st )
230 {
231 SetTitle( st ) ;
232 m_label = st ;
233 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) )
234 {
235 // temporary fix until layout measurement and drawing are in synch again
236 Refresh() ;
237 InvalidateBestSize();
238 SetSize( GetBestSize() ) ;
239 }
240 Refresh() ;
241 Update() ;
242 }
243
244 bool wxStaticText::SetFont(const wxFont& font)
245 {
246 bool ret = wxControl::SetFont(font);
247
248 if ( ret )
249 {
250 // adjust the size of the window to fit to the label unless autoresizing is
251 // disabled
252 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) )
253 {
254 // temporary fix until layout measurement and drawing are in synch again
255 Refresh() ;
256 InvalidateBestSize();
257 SetSize( GetBestSize() );
258 }
259 }
260
261 return ret;
262 }