]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/stattext.cpp
added a few encoding convenience methods for pc-mac encoding and string handling...
[wxWidgets.git] / src / mac / carbon / stattext.cpp
CommitLineData
e9576ca5
SC
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"
bedaf53e
SC
18#include "wx/notebook.h"
19#include "wx/tabctrl.h"
03e11df5
GD
20#include "wx/dc.h"
21#include "wx/dcclient.h"
e9576ca5
SC
22
23#include <stdio.h>
24
2f1ae414 25#if !USE_SHARED_LIBRARY
90b959ae 26IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxControl)
2f1ae414 27#endif
e9576ca5 28
519cb848
SC
29#include <wx/mac/uma.h>
30
584bede0 31BEGIN_EVENT_TABLE(wxStaticText, wxStaticTextBase)
8208e181
SC
32 EVT_PAINT(wxStaticText::OnPaint)
33END_EVENT_TABLE()
34
35bool 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 );
584bede0 55 SetBestSize( size ) ;
8208e181
SC
56
57 return ret;
58}
59
9714ffa0
SC
60const wxString punct = " ,.-;:!?";
61
62void 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
2f1ae414 125void wxStaticText::OnDraw( wxDC &dc )
8208e181 126{
d015713e
SC
127 if (m_width <= 0 || m_height <= 0)
128 return;
129
9714ffa0
SC
130 wxString paragraph;
131 int i = 0 ;
132 wxString text = m_label;
133
8208e181 134 PrepareDC(dc);
2f1ae414 135
bedaf53e
SC
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 )
4ce431b0 156 {
bedaf53e 157 DrawThemeTabPane ( &rect, kThemeStateActive);
4ce431b0
SC
158 doClear = false ;
159 }
bedaf53e
SC
160 }
161 break ;
162 }
163
164 parent = parent->GetParent() ;
165 }
166 }
167 }
168 if ( doClear )
169 dc.Clear() ;
170
9714ffa0 171 while (i < text.Length())
2f1ae414 172 {
9714ffa0
SC
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);
8208e181
SC
182}
183
2f1ae414 184void wxStaticText::OnPaint( wxPaintEvent &event )
8208e181 185{
2f1ae414
SC
186 wxPaintDC dc(this);
187 OnDraw( dc ) ;
8208e181
SC
188}
189
2f1ae414 190wxSize wxStaticText::DoGetBestSize() const
e9576ca5 191{
1348e1a5
RR
192 int x,y ;
193 int widthTextMax = 0, widthLine,
2f1ae414 194 heightTextTotal = 0, heightLineDefault = 0, heightLine = 0;
1348e1a5 195
2f1ae414 196 wxString curLine;
1348e1a5
RR
197 for ( const wxChar *pc = m_label; ; pc++ )
198 {
199 if ( *pc == wxT('\n') || *pc == wxT('\0') )
200 {
201 if ( !curLine )
202 {
2f1ae414
SC
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;
1348e1a5
RR
212
213 heightTextTotal++; // FIXME: why is this necessary?
2f1ae414 214 }
1348e1a5
RR
215 else
216 {
2f1ae414
SC
217 GetTextExtent(curLine, &widthLine, &heightLine);
218 if ( widthLine > widthTextMax )
219 widthTextMax = widthLine;
220 heightTextTotal += heightLine;
1348e1a5
RR
221
222 heightTextTotal++; // FIXME: why is this necessary?
2f1ae414
SC
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 }
519cb848 237
2f1ae414 238 return wxSize(widthTextMax, heightTextTotal);
e9576ca5
SC
239}
240
2f1ae414 241void wxStaticText::SetLabel(const wxString& st )
e9576ca5 242{
e7549107 243 SetTitle( st ) ;
2f1ae414
SC
244 m_label = st ;
245 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) )
584bede0 246 SetSize( GetBestSize() ) ;
9ff647cf
SC
247
248 Refresh() ;
249 MacUpdateImmediately() ;
250// wxClientDC dc(this);
251// OnDraw( dc ) ;
e9576ca5 252}