]> git.saurik.com Git - wxWidgets.git/blob - src/mac/stattext.cpp
62e095f5facee3cee3bdeaa3fedb4d5ad7b83c50
[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 #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
24 #include <stdio.h>
25
26 #if !USE_SHARED_LIBRARY
27 IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxControl)
28 #endif
29
30 #include "wx/mac/uma.h"
31
32 BEGIN_EVENT_TABLE(wxStaticText, wxStaticTextBase)
33 EVT_PAINT(wxStaticText::OnPaint)
34 END_EVENT_TABLE()
35
36 bool wxStaticText::Create(wxWindow *parent, wxWindowID id,
37 const wxString& label,
38 const wxPoint& pos,
39 const wxSize& size,
40 long style,
41 const wxString& name)
42 {
43 SetName(name);
44 m_backgroundColour = parent->GetBackgroundColour() ;
45 m_foregroundColour = parent->GetForegroundColour() ;
46
47 if ( id == -1 )
48 m_windowId = (int)NewControlId();
49 else
50 m_windowId = id;
51
52 m_windowStyle = style;
53 m_label = label ;
54
55 bool ret = wxControl::Create( parent, id, pos, size, style , wxDefaultValidator , name );
56 SetBestSize( size ) ;
57
58 return ret;
59 }
60
61 const wxString punct = " ,.-;:!?";
62
63 void wxStaticText::DrawParagraph(wxDC &dc, wxString paragraph)
64 {
65 int x = 0 ;
66 int y = 0 ;
67 int i = 0 ;
68 long width, height ;
69 bool linedrawn = true;
70 while( paragraph.Length() > 0 )
71 {
72 dc.GetTextExtent( paragraph , &width , &height ) ;
73
74 if ( width > m_width )
75 {
76 for ( int p = paragraph.Length() -1 ; p > 0 ; --p )
77 {
78 if ((punct.Find(paragraph[p]) != wxNOT_FOUND) || !linedrawn)
79 {
80 int blank = (paragraph[p] == ' ') ? 0 : 1;
81
82 dc.GetTextExtent( paragraph.Left(p + blank) , &width , &height ) ;
83
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
96 dc.DrawText( paragraph.Left(p + blank), pos , y) ;
97 y += height ;
98 paragraph = paragraph.Mid(p+1) ;
99 linedrawn = true;
100 break ;
101 }
102 }
103 }
104
105 linedrawn = false;
106 }
107 else
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
119 dc.DrawText( paragraph, pos , y) ;
120 paragraph="";
121 y += height ;
122 }
123 }
124 }
125
126 void wxStaticText::OnDraw( wxDC &dc )
127 {
128 if (m_width <= 0 || m_height <= 0)
129 return;
130
131 wxString paragraph;
132 int i = 0 ;
133 wxString text = m_label;
134
135 int major,minor;
136 wxGetOsVersion( &major, &minor );
137
138 PrepareDC(dc);
139
140 bool doClear = true ;
141 WindowRef window = GetMacRootWindow() ;
142 if ( window )
143 {
144 wxWindow* win = wxFindWinFromMacWindow( window ) ;
145 if ( win )
146 {
147 wxWindow* parent = GetParent() ;
148 while ( parent )
149 {
150 if( parent->MacGetWindowData() )
151 {
152 break ;
153 }
154
155 if (major < 10)
156 {
157 if( parent->IsKindOf( CLASSINFO( wxNotebook ) ) || parent->IsKindOf( CLASSINFO( wxTabCtrl ) ))
158 {
159 if ( ((wxControl*)parent)->GetMacControl() ) {
160 Rect rect = { -10000 , -10000 , 10000 , 10000 } ; // MacOS X was having a coord rollover
161 if ( DrawThemeTabPane != (void*)kUnresolvedCFragSymbolAddress )
162 {
163 DrawThemeTabPane ( &rect, kThemeStateActive);
164 doClear = false ;
165 }
166 }
167 break ;
168 }
169 }
170
171 parent = parent->GetParent() ;
172 }
173 }
174 }
175
176 if ( (major < 10) && doClear )
177 dc.Clear() ;
178
179 while (i < text.Length())
180 {
181 paragraph += text[i];
182
183 if (text[i] == 13 || text[i] == 10)
184 DrawParagraph(dc, paragraph);
185
186 ++i;
187 }
188 if (paragraph.Length() > 0)
189 DrawParagraph(dc, paragraph);
190 }
191
192 void wxStaticText::OnPaint( wxPaintEvent &event )
193 {
194 wxPaintDC dc(this);
195 OnDraw( dc ) ;
196 }
197
198 wxSize wxStaticText::DoGetBestSize() const
199 {
200 int x,y ;
201 int widthTextMax = 0, widthLine,
202 heightTextTotal = 0, heightLineDefault = 0, heightLine = 0;
203
204 wxString curLine;
205 for ( const wxChar *pc = m_label; ; pc++ )
206 {
207 if ( *pc == wxT('\n') || *pc == wxT('\0') )
208 {
209 if ( !curLine )
210 {
211 // we can't use GetTextExtent - it will return 0 for both width
212 // and height and an empty line should count in height
213 // calculation
214 if ( !heightLineDefault )
215 heightLineDefault = heightLine;
216 if ( !heightLineDefault )
217 GetTextExtent(_T("W"), NULL, &heightLineDefault);
218
219 heightTextTotal += heightLineDefault;
220
221 heightTextTotal++; // FIXME: why is this necessary?
222 }
223 else
224 {
225 GetTextExtent(curLine, &widthLine, &heightLine);
226 if ( widthLine > widthTextMax )
227 widthTextMax = widthLine;
228 heightTextTotal += heightLine;
229
230 heightTextTotal++; // FIXME: why is this necessary?
231 }
232
233 if ( *pc == wxT('\n') ) {
234 curLine.Empty();
235 }
236 else {
237 // the end of string
238 break;
239 }
240 }
241 else {
242 curLine += *pc;
243 }
244 }
245
246 return wxSize(widthTextMax, heightTextTotal);
247 }
248
249 void wxStaticText::SetLabel(const wxString& st )
250 {
251 SetTitle( st ) ;
252 m_label = st ;
253 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) )
254 SetSize( GetBestSize() ) ;
255
256 Refresh() ;
257 MacUpdateImmediately() ;
258 // wxClientDC dc(this);
259 // OnDraw( dc ) ;
260 }