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