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