]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/mac/classic/stattext.cpp | |
3 | // Purpose: wxStaticText | |
4 | // Author: Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Stefan Csomor | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #include "wx/stattext.h" | |
15 | ||
16 | #ifndef WX_PRECOMP | |
17 | #include "wx/app.h" | |
18 | #include "wx/utils.h" | |
19 | #include "wx/dc.h" | |
20 | #include "wx/dcclient.h" | |
21 | #include "wx/settings.h" | |
22 | #endif | |
23 | ||
24 | #include "wx/notebook.h" | |
25 | #include "wx/tabctrl.h" | |
26 | ||
27 | #include <stdio.h> | |
28 | ||
29 | IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxControl) | |
30 | ||
31 | #include "wx/mac/uma.h" | |
32 | ||
33 | BEGIN_EVENT_TABLE(wxStaticText, wxStaticTextBase) | |
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 | m_label = wxStripMenuCodes(label) ; | |
45 | ||
46 | if ( !wxControl::Create( parent, id, pos, size, style, | |
47 | wxDefaultValidator , name ) ) | |
48 | { | |
49 | return false; | |
50 | } | |
51 | ||
52 | SetBestSize( size ) ; | |
53 | ||
54 | return true; | |
55 | } | |
56 | ||
57 | const wxString punct = wxT(" ,.-;:!?"); | |
58 | ||
59 | void wxStaticText::DrawParagraph(wxDC &dc, wxString paragraph, int &y) | |
60 | { | |
61 | long width, height ; | |
62 | ||
63 | if (paragraph.length() == 0) | |
64 | { | |
65 | // empty line | |
66 | dc.GetTextExtent( wxT("H"), &width, &height ); | |
67 | y += height; | |
68 | ||
69 | return; | |
70 | } | |
71 | ||
72 | int x = 0 ; | |
73 | ||
74 | bool linedrawn = true; | |
75 | while( paragraph.length() > 0 ) | |
76 | { | |
77 | dc.GetTextExtent( paragraph , &width , &height ) ; | |
78 | ||
79 | if ( width > m_width ) | |
80 | { | |
81 | for ( size_t p = paragraph.length() - 1 ; p > 0 ; --p ) | |
82 | { | |
83 | if ((punct.Find(paragraph[p]) != wxNOT_FOUND) || !linedrawn) | |
84 | { | |
85 | int blank = (paragraph[p] == ' ') ? 0 : 1; | |
86 | ||
87 | dc.GetTextExtent( paragraph.Left(p + blank) , &width , &height ) ; | |
88 | ||
89 | if ( width <= m_width ) | |
90 | { | |
91 | int pos = x ; | |
92 | if ( HasFlag( wxALIGN_CENTER ) ) | |
93 | { | |
94 | pos += ( m_width - width ) / 2 ; | |
95 | } | |
96 | else if ( HasFlag( wxALIGN_RIGHT ) ) | |
97 | { | |
98 | pos += ( m_width - width ) ; | |
99 | } | |
100 | ||
101 | dc.DrawText( paragraph.Left(p + blank), pos , y) ; | |
102 | y += height ; | |
103 | paragraph = paragraph.Mid(p+1) ; | |
104 | linedrawn = true; | |
105 | break ; | |
106 | } | |
107 | } | |
108 | } | |
109 | ||
110 | linedrawn = false; | |
111 | } | |
112 | else | |
113 | { | |
114 | int pos = x ; | |
115 | if ( HasFlag( wxALIGN_CENTER ) ) | |
116 | { | |
117 | pos += ( m_width - width ) / 2 ; | |
118 | } | |
119 | else if ( HasFlag( wxALIGN_RIGHT ) ) | |
120 | { | |
121 | pos += ( m_width - width ) ; | |
122 | } | |
123 | ||
124 | dc.DrawText( paragraph, pos , y) ; | |
125 | paragraph=wxEmptyString; | |
126 | y += height ; | |
127 | } | |
128 | } | |
129 | } | |
130 | ||
131 | void wxStaticText::OnDraw( wxDC &dc ) | |
132 | { | |
133 | if (m_width <= 0 || m_height <= 0) | |
134 | return; | |
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) ) ) | |
145 | { | |
146 | dc.SetTextForeground( wxColour( 0x80 , 0x80 , 0x80 ) ) ; | |
147 | } | |
148 | else | |
149 | { | |
150 | dc.SetTextForeground( GetForegroundColour() ) ; | |
151 | } | |
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); | |
163 | paragraph = wxEmptyString ; | |
164 | } | |
165 | else | |
166 | { | |
167 | paragraph += text[i]; | |
168 | } | |
169 | ++i; | |
170 | } | |
171 | if (paragraph.length() > 0) | |
172 | DrawParagraph(dc, paragraph,y); | |
173 | } | |
174 | ||
175 | void wxStaticText::OnPaint( wxPaintEvent & WXUNUSED(event) ) | |
176 | { | |
177 | wxPaintDC dc(this); | |
178 | OnDraw( dc ) ; | |
179 | } | |
180 | ||
181 | wxSize wxStaticText::DoGetBestSize() const | |
182 | { | |
183 | int widthTextMax = 0, widthLine, | |
184 | heightTextTotal = 0, heightLineDefault = 0, heightLine = 0; | |
185 | ||
186 | wxString curLine; | |
187 | for ( const wxChar *pc = m_label; ; pc++ ) | |
188 | { | |
189 | if ( *pc == wxT('\n') || *pc == wxT('\r') || *pc == wxT('\0') ) | |
190 | { | |
191 | if ( !curLine ) | |
192 | { | |
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; | |
202 | ||
203 | heightTextTotal++; // FIXME: why is this necessary? | |
204 | } | |
205 | else | |
206 | { | |
207 | GetTextExtent(curLine, &widthLine, &heightLine); | |
208 | if ( widthLine > widthTextMax ) | |
209 | widthTextMax = widthLine; | |
210 | heightTextTotal += heightLine; | |
211 | ||
212 | heightTextTotal++; // FIXME: why is this necessary? | |
213 | } | |
214 | ||
215 | if ( *pc == wxT('\n') || *pc == wxT('\r')) { | |
216 | curLine.Empty(); | |
217 | } | |
218 | else { | |
219 | // the end of string | |
220 | break; | |
221 | } | |
222 | } | |
223 | else { | |
224 | curLine += *pc; | |
225 | } | |
226 | } | |
227 | ||
228 | return wxSize(widthTextMax, heightTextTotal); | |
229 | } | |
230 | ||
231 | void wxStaticText::SetLabel(const wxString& st ) | |
232 | { | |
233 | wxStaticTextBase::SetLabel( st ) ; | |
234 | m_label = st ; | |
235 | if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) ) | |
236 | { | |
237 | // temporary fix until layout measurement and drawing are in synch again | |
238 | Refresh() ; | |
239 | InvalidateBestSize(); | |
240 | SetSize( GetBestSize() ) ; | |
241 | } | |
242 | Refresh() ; | |
243 | Update() ; | |
244 | } | |
245 | ||
246 | bool wxStaticText::SetFont(const wxFont& font) | |
247 | { | |
248 | bool ret = wxControl::SetFont(font); | |
249 | ||
250 | if ( ret ) | |
251 | { | |
252 | // adjust the size of the window to fit to the label unless autoresizing is | |
253 | // disabled | |
254 | if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) ) | |
255 | { | |
256 | // temporary fix until layout measurement and drawing are in synch again | |
257 | Refresh() ; | |
258 | InvalidateBestSize(); | |
259 | SetSize( GetBestSize() ); | |
260 | } | |
261 | } | |
262 | ||
263 | return ret; | |
264 | } |