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