]> git.saurik.com Git - wxWidgets.git/blame - src/mac/classic/stattext.cpp
COW cleanup patch (1583966):
[wxWidgets.git] / src / mac / classic / stattext.cpp
CommitLineData
2646f485 1/////////////////////////////////////////////////////////////////////////////
fb5246be 2// Name: src/mac/classic/stattext.cpp
2646f485
SC
3// Purpose: wxStaticText
4// Author: Stefan Csomor
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Stefan Csomor
65571936 9// Licence: wxWindows licence
2646f485
SC
10/////////////////////////////////////////////////////////////////////////////
11
670f9935
WS
12#include "wx/wxprec.h"
13
2646f485 14#include "wx/stattext.h"
670f9935
WS
15
16#ifndef WX_PRECOMP
17 #include "wx/app.h"
de6185e2 18 #include "wx/utils.h"
da80ae71 19 #include "wx/dc.h"
ed4b0fdc 20 #include "wx/dcclient.h"
9eddec69 21 #include "wx/settings.h"
670f9935
WS
22#endif
23
2646f485
SC
24#include "wx/notebook.h"
25#include "wx/tabctrl.h"
2646f485
SC
26
27#include <stdio.h>
28
2646f485 29IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxControl)
2646f485
SC
30
31#include "wx/mac/uma.h"
32
33BEGIN_EVENT_TABLE(wxStaticText, wxStaticTextBase)
34 EVT_PAINT(wxStaticText::OnPaint)
35END_EVENT_TABLE()
36
37bool 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
57const wxString punct = wxT(" ,.-;:!?");
58
59void wxStaticText::DrawParagraph(wxDC &dc, wxString paragraph, int &y)
60{
61 long width, height ;
fb5246be
WS
62
63 if (paragraph.length() == 0)
2646f485
SC
64 {
65 // empty line
66 dc.GetTextExtent( wxT("H"), &width, &height );
67 y += height;
fb5246be 68
2646f485
SC
69 return;
70 }
71
72 int x = 0 ;
73
74 bool linedrawn = true;
fb5246be 75 while( paragraph.length() > 0 )
2646f485
SC
76 {
77 dc.GetTextExtent( paragraph , &width , &height ) ;
fb5246be 78
2646f485
SC
79 if ( width > m_width )
80 {
fb5246be 81 for ( size_t p = paragraph.length() - 1 ; p > 0 ; --p )
2646f485
SC
82 {
83 if ((punct.Find(paragraph[p]) != wxNOT_FOUND) || !linedrawn)
84 {
85 int blank = (paragraph[p] == ' ') ? 0 : 1;
fb5246be 86
2646f485 87 dc.GetTextExtent( paragraph.Left(p + blank) , &width , &height ) ;
fb5246be 88
2646f485
SC
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 }
fb5246be 100
2646f485
SC
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 }
fb5246be 109
2646f485
SC
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 }
fb5246be 123
2646f485
SC
124 dc.DrawText( paragraph, pos , y) ;
125 paragraph=wxEmptyString;
126 y += height ;
127 }
128 }
129}
130
131void 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) ;
fb5246be 139
2646f485
SC
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 }
fb5246be 152
2646f485
SC
153 wxString paragraph;
154 size_t i = 0 ;
155 wxString text = m_label;
156 int y = 0 ;
fb5246be 157 while (i < text.length())
2646f485 158 {
fb5246be 159
2646f485
SC
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 }
fb5246be 171 if (paragraph.length() > 0)
2646f485
SC
172 DrawParagraph(dc, paragraph,y);
173}
174
175void wxStaticText::OnPaint( wxPaintEvent & WXUNUSED(event) )
176{
177 wxPaintDC dc(this);
178 OnDraw( dc ) ;
179}
180
181wxSize 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
231void wxStaticText::SetLabel(const wxString& st )
232{
fb5246be 233 wxStaticTextBase::SetLabel( st ) ;
2646f485 234 m_label = st ;
c0e6c051
RD
235 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) )
236 {
237 // temporary fix until layout measurement and drawing are in synch again
238 Refresh() ;
9f884528 239 InvalidateBestSize();
c0e6c051
RD
240 SetSize( GetBestSize() ) ;
241 }
2646f485
SC
242 Refresh() ;
243 Update() ;
244}
245
c0e6c051
RD
246bool wxStaticText::SetFont(const wxFont& font)
247{
248 bool ret = wxControl::SetFont(font);
249
fb5246be
WS
250 if ( ret )
251 {
252 // adjust the size of the window to fit to the label unless autoresizing is
253 // disabled
254 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) )
255 {
256 // temporary fix until layout measurement and drawing are in synch again
257 Refresh() ;
258 InvalidateBestSize();
259 SetSize( GetBestSize() );
260 }
261 }
c0e6c051
RD
262
263 return ret;
264}