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