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