]>
Commit | Line | Data |
---|---|---|
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 | 28 | IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxControl) |
2f1ae414 | 29 | #endif |
e9576ca5 | 30 | |
d497dca4 | 31 | #include "wx/mac/uma.h" |
519cb848 | 32 | |
584bede0 | 33 | BEGIN_EVENT_TABLE(wxStaticText, wxStaticTextBase) |
8208e181 SC |
34 | EVT_PAINT(wxStaticText::OnPaint) |
35 | END_EVENT_TABLE() | |
36 | ||
37 | bool 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 ) ; | |
bb751cf9 | 58 | |
8208e181 SC |
59 | return ret; |
60 | } | |
61 | ||
9714ffa0 SC |
62 | const wxString punct = " ,.-;:!?"; |
63 | ||
7595f381 | 64 | void wxStaticText::DrawParagraph(wxDC &dc, wxString paragraph, int &y) |
9714ffa0 SC |
65 | { |
66 | int x = 0 ; | |
7595f381 | 67 | |
9714ffa0 SC |
68 | int i = 0 ; |
69 | long width, height ; | |
70 | bool linedrawn = true; | |
71 | while( paragraph.Length() > 0 ) | |
72 | { | |
00500f40 | 73 | dc.GetTextExtent( paragraph , &width , &height ) ; |
bb751cf9 | 74 | |
00500f40 RR |
75 | if ( width > m_width ) |
76 | { | |
77 | for ( int p = paragraph.Length() -1 ; p > 0 ; --p ) | |
bb751cf9 | 78 | { |
00500f40 RR |
79 | if ((punct.Find(paragraph[p]) != wxNOT_FOUND) || !linedrawn) |
80 | { | |
81 | int blank = (paragraph[p] == ' ') ? 0 : 1; | |
bb751cf9 | 82 | |
00500f40 | 83 | dc.GetTextExtent( paragraph.Left(p + blank) , &width , &height ) ; |
bb751cf9 | 84 | |
00500f40 | 85 | if ( width <= m_width ) |
bb751cf9 | 86 | { |
00500f40 RR |
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 | } | |
bb751cf9 | 96 | |
00500f40 RR |
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 | } | |
bb751cf9 | 105 | |
00500f40 RR |
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 | } | |
bb751cf9 | 119 | |
00500f40 RR |
120 | dc.DrawText( paragraph, pos , y) ; |
121 | paragraph=""; | |
122 | y += height ; | |
123 | } | |
124 | } | |
9714ffa0 SC |
125 | } |
126 | ||
2f1ae414 | 127 | void wxStaticText::OnDraw( wxDC &dc ) |
8208e181 | 128 | { |
d015713e SC |
129 | if (m_width <= 0 || m_height <= 0) |
130 | return; | |
7595f381 SC |
131 | /* |
132 | dc.Clear() ; | |
133 | wxRect rect(0,0,m_width,m_height) ; | |
134 | dc.SetFont(*wxSMALL_FONT) ; | |
bb751cf9 | 135 | |
7595f381 SC |
136 | dc.DrawRectangle(rect) ; |
137 | */ | |
bb751cf9 RD |
138 | if ( !IsWindowHilited( (WindowRef) MacGetRootWindow() ) && |
139 | ( GetBackgroundColour() == wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE ) | |
a756f210 | 140 | || GetBackgroundColour() == wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE) ) ) |
1c469f7f SC |
141 | { |
142 | dc.SetTextForeground( wxColour( 0x80 , 0x80 , 0x80 ) ) ; | |
143 | } | |
c9fdc107 SC |
144 | else |
145 | { | |
146 | dc.SetTextForeground( GetForegroundColour() ) ; | |
147 | } | |
bb751cf9 | 148 | |
607f6d2b SC |
149 | wxString paragraph; |
150 | int i = 0 ; | |
151 | wxString text = m_label; | |
7595f381 SC |
152 | int y = 0 ; |
153 | while (i < text.Length()) | |
154 | { | |
bb751cf9 | 155 | |
607f6d2b | 156 | if (text[i] == 13 || text[i] == 10) |
7595f381 SC |
157 | { |
158 | DrawParagraph(dc, paragraph,y); | |
159 | paragraph = "" ; | |
bb751cf9 | 160 | } |
7595f381 SC |
161 | else |
162 | { | |
163 | paragraph += text[i]; | |
164 | } | |
607f6d2b SC |
165 | ++i; |
166 | } | |
167 | if (paragraph.Length() > 0) | |
7595f381 | 168 | DrawParagraph(dc, paragraph,y); |
8208e181 SC |
169 | } |
170 | ||
bb751cf9 | 171 | void wxStaticText::OnPaint( wxPaintEvent &event ) |
8208e181 | 172 | { |
2f1ae414 SC |
173 | wxPaintDC dc(this); |
174 | OnDraw( dc ) ; | |
8208e181 SC |
175 | } |
176 | ||
2f1ae414 | 177 | wxSize wxStaticText::DoGetBestSize() const |
e9576ca5 | 178 | { |
1348e1a5 | 179 | int widthTextMax = 0, widthLine, |
2f1ae414 | 180 | heightTextTotal = 0, heightLineDefault = 0, heightLine = 0; |
bb751cf9 | 181 | |
2f1ae414 | 182 | wxString curLine; |
1348e1a5 RR |
183 | for ( const wxChar *pc = m_label; ; pc++ ) |
184 | { | |
185 | if ( *pc == wxT('\n') || *pc == wxT('\0') ) | |
186 | { | |
187 | if ( !curLine ) | |
188 | { | |
2f1ae414 SC |
189 | // we can't use GetTextExtent - it will return 0 for both width |
190 | // and height and an empty line should count in height | |
191 | // calculation | |
192 | if ( !heightLineDefault ) | |
193 | heightLineDefault = heightLine; | |
194 | if ( !heightLineDefault ) | |
195 | GetTextExtent(_T("W"), NULL, &heightLineDefault); | |
196 | ||
197 | heightTextTotal += heightLineDefault; | |
bb751cf9 | 198 | |
1348e1a5 | 199 | heightTextTotal++; // FIXME: why is this necessary? |
2f1ae414 | 200 | } |
1348e1a5 RR |
201 | else |
202 | { | |
2f1ae414 SC |
203 | GetTextExtent(curLine, &widthLine, &heightLine); |
204 | if ( widthLine > widthTextMax ) | |
205 | widthTextMax = widthLine; | |
206 | heightTextTotal += heightLine; | |
bb751cf9 | 207 | |
1348e1a5 | 208 | heightTextTotal++; // FIXME: why is this necessary? |
2f1ae414 SC |
209 | } |
210 | ||
211 | if ( *pc == wxT('\n') ) { | |
212 | curLine.Empty(); | |
213 | } | |
214 | else { | |
215 | // the end of string | |
216 | break; | |
217 | } | |
218 | } | |
219 | else { | |
220 | curLine += *pc; | |
221 | } | |
222 | } | |
519cb848 | 223 | |
2f1ae414 | 224 | return wxSize(widthTextMax, heightTextTotal); |
e9576ca5 SC |
225 | } |
226 | ||
2f1ae414 | 227 | void wxStaticText::SetLabel(const wxString& st ) |
e9576ca5 | 228 | { |
bb751cf9 RD |
229 | SetTitle( st ) ; |
230 | m_label = st ; | |
231 | if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) ) | |
232 | SetSize( GetBestSize() ) ; | |
233 | ||
234 | Refresh() ; | |
235 | Update() ; | |
236 | } | |
237 | ||
238 | bool wxStaticText::SetFont(const wxFont& font) | |
239 | { | |
240 | bool ret = wxControl::SetFont(font); | |
607f6d2b | 241 | |
bb751cf9 RD |
242 | // adjust the size of the window to fit to the label unless autoresizing is |
243 | // disabled | |
244 | if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) ) | |
245 | SetSize( GetBestSize() ); | |
246 | ||
247 | return ret; | |
e9576ca5 | 248 | } |