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