]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/stattext.cpp
1. added wxUpdateUIEvent handling to wxMGL
[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
00500f40 9// Licence: wxWindows licence
e9576ca5
SC
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"
00500f40 22#include "wx/utils.h"
f7035880 23#include "wx/settings.h"
e9576ca5
SC
24
25#include <stdio.h>
26
2f1ae414 27#if !USE_SHARED_LIBRARY
90b959ae 28IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxControl)
2f1ae414 29#endif
e9576ca5 30
d497dca4 31#include "wx/mac/uma.h"
519cb848 32
584bede0 33BEGIN_EVENT_TABLE(wxStaticText, wxStaticTextBase)
8208e181
SC
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 SetName(name);
45 m_backgroundColour = parent->GetBackgroundColour() ;
46 m_foregroundColour = parent->GetForegroundColour() ;
47
48 if ( id == -1 )
00500f40 49 m_windowId = (int)NewControlId();
8208e181 50 else
00500f40 51 m_windowId = id;
8208e181
SC
52
53 m_windowStyle = style;
54 m_label = label ;
55
00500f40
RR
56 bool ret = wxControl::Create( parent, id, pos, size, style , wxDefaultValidator , name );
57 SetBestSize( size ) ;
8208e181
SC
58
59 return ret;
60}
61
9714ffa0
SC
62const wxString punct = " ,.-;:!?";
63
64void wxStaticText::DrawParagraph(wxDC &dc, wxString paragraph)
65{
66 int x = 0 ;
67 int y = 0 ;
68 int i = 0 ;
69 long width, height ;
70 bool linedrawn = true;
71 while( paragraph.Length() > 0 )
72 {
00500f40
RR
73 dc.GetTextExtent( paragraph , &width , &height ) ;
74
75 if ( width > m_width )
76 {
77 for ( int p = paragraph.Length() -1 ; p > 0 ; --p )
78 {
79 if ((punct.Find(paragraph[p]) != wxNOT_FOUND) || !linedrawn)
80 {
81 int blank = (paragraph[p] == ' ') ? 0 : 1;
82
83 dc.GetTextExtent( paragraph.Left(p + blank) , &width , &height ) ;
84
85 if ( width <= m_width )
86 {
87 int pos = x ;
88 if ( HasFlag( wxALIGN_CENTER ) )
89 {
90 pos += ( m_width - width ) / 2 ;
91 }
92 else if ( HasFlag( wxALIGN_RIGHT ) )
93 {
94 pos += ( m_width - width ) ;
95 }
96
97 dc.DrawText( paragraph.Left(p + blank), pos , y) ;
98 y += height ;
99 paragraph = paragraph.Mid(p+1) ;
100 linedrawn = true;
101 break ;
102 }
103 }
104 }
105
106 linedrawn = false;
107 }
108 else
109 {
110 int pos = x ;
111 if ( HasFlag( wxALIGN_CENTER ) )
112 {
113 pos += ( m_width - width ) / 2 ;
114 }
115 else if ( HasFlag( wxALIGN_RIGHT ) )
116 {
117 pos += ( m_width - width ) ;
118 }
119
120 dc.DrawText( paragraph, pos , y) ;
121 paragraph="";
122 y += height ;
123 }
124 }
9714ffa0
SC
125}
126
2f1ae414 127void wxStaticText::OnDraw( wxDC &dc )
8208e181 128{
d015713e
SC
129 if (m_width <= 0 || m_height <= 0)
130 return;
131
76a5e5d2 132 if ( !IsWindowHilited( (WindowRef) MacGetRootWindow() ) &&
a756f210
VS
133 ( GetBackgroundColour() == wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE )
134 || GetBackgroundColour() == wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE) ) )
1c469f7f
SC
135 {
136 dc.SetTextForeground( wxColour( 0x80 , 0x80 , 0x80 ) ) ;
137 }
c9fdc107
SC
138 else
139 {
140 dc.SetTextForeground( GetForegroundColour() ) ;
141 }
142
607f6d2b
SC
143 wxString paragraph;
144 int i = 0 ;
145 wxString text = m_label;
146 while (i < text.Length())
147 {
148 paragraph += text[i];
149
150 if (text[i] == 13 || text[i] == 10)
151 DrawParagraph(dc, paragraph);
152
153 ++i;
154 }
155 if (paragraph.Length() > 0)
156 DrawParagraph(dc, paragraph);
8208e181
SC
157}
158
2f1ae414 159void wxStaticText::OnPaint( wxPaintEvent &event )
8208e181 160{
2f1ae414
SC
161 wxPaintDC dc(this);
162 OnDraw( dc ) ;
8208e181
SC
163}
164
2f1ae414 165wxSize wxStaticText::DoGetBestSize() const
e9576ca5 166{
00500f40 167 int x,y ;
1348e1a5 168 int widthTextMax = 0, widthLine,
2f1ae414 169 heightTextTotal = 0, heightLineDefault = 0, heightLine = 0;
1348e1a5 170
2f1ae414 171 wxString curLine;
1348e1a5
RR
172 for ( const wxChar *pc = m_label; ; pc++ )
173 {
174 if ( *pc == wxT('\n') || *pc == wxT('\0') )
175 {
176 if ( !curLine )
177 {
2f1ae414
SC
178 // we can't use GetTextExtent - it will return 0 for both width
179 // and height and an empty line should count in height
180 // calculation
181 if ( !heightLineDefault )
182 heightLineDefault = heightLine;
183 if ( !heightLineDefault )
184 GetTextExtent(_T("W"), NULL, &heightLineDefault);
185
186 heightTextTotal += heightLineDefault;
1348e1a5
RR
187
188 heightTextTotal++; // FIXME: why is this necessary?
2f1ae414 189 }
1348e1a5
RR
190 else
191 {
2f1ae414
SC
192 GetTextExtent(curLine, &widthLine, &heightLine);
193 if ( widthLine > widthTextMax )
194 widthTextMax = widthLine;
195 heightTextTotal += heightLine;
1348e1a5
RR
196
197 heightTextTotal++; // FIXME: why is this necessary?
2f1ae414
SC
198 }
199
200 if ( *pc == wxT('\n') ) {
201 curLine.Empty();
202 }
203 else {
204 // the end of string
205 break;
206 }
207 }
208 else {
209 curLine += *pc;
210 }
211 }
519cb848 212
2f1ae414 213 return wxSize(widthTextMax, heightTextTotal);
e9576ca5
SC
214}
215
2f1ae414 216void wxStaticText::SetLabel(const wxString& st )
e9576ca5 217{
607f6d2b
SC
218 SetTitle( st ) ;
219 m_label = st ;
220 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) )
221 SetSize( GetBestSize() ) ;
222
223 Refresh() ;
224 Update() ;
e9576ca5 225}