]>
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/app.h" | |
13 | #include "wx/stattext.h" | |
14 | #include "wx/notebook.h" | |
15 | #include "wx/tabctrl.h" | |
16 | #include "wx/dc.h" | |
17 | #include "wx/dcclient.h" | |
18 | #include "wx/utils.h" | |
19 | #include "wx/settings.h" | |
20 | ||
21 | #include <stdio.h> | |
22 | ||
23 | IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxControl) | |
24 | ||
25 | #include "wx/mac/uma.h" | |
26 | ||
27 | BEGIN_EVENT_TABLE(wxStaticText, wxStaticTextBase) | |
28 | EVT_PAINT(wxStaticText::OnPaint) | |
29 | END_EVENT_TABLE() | |
30 | ||
31 | bool wxStaticText::Create(wxWindow *parent, wxWindowID id, | |
32 | const wxString& label, | |
33 | const wxPoint& pos, | |
34 | const wxSize& size, | |
35 | long style, | |
36 | const wxString& name) | |
37 | { | |
38 | m_label = wxStripMenuCodes(label) ; | |
39 | ||
40 | if ( !wxControl::Create( parent, id, pos, size, style, | |
41 | wxDefaultValidator , name ) ) | |
42 | { | |
43 | return false; | |
44 | } | |
45 | ||
46 | SetBestSize( size ) ; | |
47 | ||
48 | return true; | |
49 | } | |
50 | ||
51 | const wxString punct = wxT(" ,.-;:!?"); | |
52 | ||
53 | void wxStaticText::DrawParagraph(wxDC &dc, wxString paragraph, int &y) | |
54 | { | |
55 | long width, height ; | |
56 | ||
57 | if (paragraph.length() == 0) | |
58 | { | |
59 | // empty line | |
60 | dc.GetTextExtent( wxT("H"), &width, &height ); | |
61 | y += height; | |
62 | ||
63 | return; | |
64 | } | |
65 | ||
66 | int x = 0 ; | |
67 | ||
68 | bool linedrawn = true; | |
69 | while( paragraph.length() > 0 ) | |
70 | { | |
71 | dc.GetTextExtent( paragraph , &width , &height ) ; | |
72 | ||
73 | if ( width > m_width ) | |
74 | { | |
75 | for ( size_t p = paragraph.length() - 1 ; p > 0 ; --p ) | |
76 | { | |
77 | if ((punct.Find(paragraph[p]) != wxNOT_FOUND) || !linedrawn) | |
78 | { | |
79 | int blank = (paragraph[p] == ' ') ? 0 : 1; | |
80 | ||
81 | dc.GetTextExtent( paragraph.Left(p + blank) , &width , &height ) ; | |
82 | ||
83 | if ( width <= m_width ) | |
84 | { | |
85 | int pos = x ; | |
86 | if ( HasFlag( wxALIGN_CENTER ) ) | |
87 | { | |
88 | pos += ( m_width - width ) / 2 ; | |
89 | } | |
90 | else if ( HasFlag( wxALIGN_RIGHT ) ) | |
91 | { | |
92 | pos += ( m_width - width ) ; | |
93 | } | |
94 | ||
95 | dc.DrawText( paragraph.Left(p + blank), pos , y) ; | |
96 | y += height ; | |
97 | paragraph = paragraph.Mid(p+1) ; | |
98 | linedrawn = true; | |
99 | break ; | |
100 | } | |
101 | } | |
102 | } | |
103 | ||
104 | linedrawn = false; | |
105 | } | |
106 | else | |
107 | { | |
108 | int pos = x ; | |
109 | if ( HasFlag( wxALIGN_CENTER ) ) | |
110 | { | |
111 | pos += ( m_width - width ) / 2 ; | |
112 | } | |
113 | else if ( HasFlag( wxALIGN_RIGHT ) ) | |
114 | { | |
115 | pos += ( m_width - width ) ; | |
116 | } | |
117 | ||
118 | dc.DrawText( paragraph, pos , y) ; | |
119 | paragraph=wxEmptyString; | |
120 | y += height ; | |
121 | } | |
122 | } | |
123 | } | |
124 | ||
125 | void wxStaticText::OnDraw( wxDC &dc ) | |
126 | { | |
127 | if (m_width <= 0 || m_height <= 0) | |
128 | return; | |
129 | /* | |
130 | dc.Clear() ; | |
131 | wxRect rect(0,0,m_width,m_height) ; | |
132 | dc.SetFont(*wxSMALL_FONT) ; | |
133 | ||
134 | dc.DrawRectangle(rect) ; | |
135 | */ | |
136 | if ( !IsWindowHilited( (WindowRef) MacGetRootWindow() ) && | |
137 | ( GetBackgroundColour() == wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE ) | |
138 | || GetBackgroundColour() == wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE) ) ) | |
139 | { | |
140 | dc.SetTextForeground( wxColour( 0x80 , 0x80 , 0x80 ) ) ; | |
141 | } | |
142 | else | |
143 | { | |
144 | dc.SetTextForeground( GetForegroundColour() ) ; | |
145 | } | |
146 | ||
147 | wxString paragraph; | |
148 | size_t i = 0 ; | |
149 | wxString text = m_label; | |
150 | int y = 0 ; | |
151 | while (i < text.length()) | |
152 | { | |
153 | ||
154 | if (text[i] == 13 || text[i] == 10) | |
155 | { | |
156 | DrawParagraph(dc, paragraph,y); | |
157 | paragraph = wxEmptyString ; | |
158 | } | |
159 | else | |
160 | { | |
161 | paragraph += text[i]; | |
162 | } | |
163 | ++i; | |
164 | } | |
165 | if (paragraph.length() > 0) | |
166 | DrawParagraph(dc, paragraph,y); | |
167 | } | |
168 | ||
169 | void wxStaticText::OnPaint( wxPaintEvent & WXUNUSED(event) ) | |
170 | { | |
171 | wxPaintDC dc(this); | |
172 | OnDraw( dc ) ; | |
173 | } | |
174 | ||
175 | wxSize wxStaticText::DoGetBestSize() const | |
176 | { | |
177 | int widthTextMax = 0, widthLine, | |
178 | heightTextTotal = 0, heightLineDefault = 0, heightLine = 0; | |
179 | ||
180 | wxString curLine; | |
181 | for ( const wxChar *pc = m_label; ; pc++ ) | |
182 | { | |
183 | if ( *pc == wxT('\n') || *pc == wxT('\r') || *pc == wxT('\0') ) | |
184 | { | |
185 | if ( !curLine ) | |
186 | { | |
187 | // we can't use GetTextExtent - it will return 0 for both width | |
188 | // and height and an empty line should count in height | |
189 | // calculation | |
190 | if ( !heightLineDefault ) | |
191 | heightLineDefault = heightLine; | |
192 | if ( !heightLineDefault ) | |
193 | GetTextExtent(_T("W"), NULL, &heightLineDefault); | |
194 | ||
195 | heightTextTotal += heightLineDefault; | |
196 | ||
197 | heightTextTotal++; // FIXME: why is this necessary? | |
198 | } | |
199 | else | |
200 | { | |
201 | GetTextExtent(curLine, &widthLine, &heightLine); | |
202 | if ( widthLine > widthTextMax ) | |
203 | widthTextMax = widthLine; | |
204 | heightTextTotal += heightLine; | |
205 | ||
206 | heightTextTotal++; // FIXME: why is this necessary? | |
207 | } | |
208 | ||
209 | if ( *pc == wxT('\n') || *pc == wxT('\r')) { | |
210 | curLine.Empty(); | |
211 | } | |
212 | else { | |
213 | // the end of string | |
214 | break; | |
215 | } | |
216 | } | |
217 | else { | |
218 | curLine += *pc; | |
219 | } | |
220 | } | |
221 | ||
222 | return wxSize(widthTextMax, heightTextTotal); | |
223 | } | |
224 | ||
225 | void wxStaticText::SetLabel(const wxString& st ) | |
226 | { | |
227 | wxStaticTextBase::SetLabel( st ) ; | |
228 | m_label = st ; | |
229 | if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) ) | |
230 | { | |
231 | // temporary fix until layout measurement and drawing are in synch again | |
232 | Refresh() ; | |
233 | InvalidateBestSize(); | |
234 | SetSize( GetBestSize() ) ; | |
235 | } | |
236 | Refresh() ; | |
237 | Update() ; | |
238 | } | |
239 | ||
240 | bool wxStaticText::SetFont(const wxFont& font) | |
241 | { | |
242 | bool ret = wxControl::SetFont(font); | |
243 | ||
244 | if ( ret ) | |
245 | { | |
246 | // adjust the size of the window to fit to the label unless autoresizing is | |
247 | // disabled | |
248 | if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) ) | |
249 | { | |
250 | // temporary fix until layout measurement and drawing are in synch again | |
251 | Refresh() ; | |
252 | InvalidateBestSize(); | |
253 | SetSize( GetBestSize() ); | |
254 | } | |
255 | } | |
256 | ||
257 | return ret; | |
258 | } |