]>
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 | |
9 | // Licence: wxWindows licence | |
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" | |
e9576ca5 SC |
22 | |
23 | #include <stdio.h> | |
24 | ||
2f1ae414 | 25 | #if !USE_SHARED_LIBRARY |
584bede0 | 26 | IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxStaticTextBase) |
2f1ae414 | 27 | #endif |
e9576ca5 | 28 | |
519cb848 SC |
29 | #include <wx/mac/uma.h> |
30 | ||
584bede0 | 31 | BEGIN_EVENT_TABLE(wxStaticText, wxStaticTextBase) |
8208e181 SC |
32 | EVT_PAINT(wxStaticText::OnPaint) |
33 | END_EVENT_TABLE() | |
34 | ||
35 | bool wxStaticText::Create(wxWindow *parent, wxWindowID id, | |
36 | const wxString& label, | |
37 | const wxPoint& pos, | |
38 | const wxSize& size, | |
39 | long style, | |
40 | const wxString& name) | |
41 | { | |
42 | SetName(name); | |
43 | m_backgroundColour = parent->GetBackgroundColour() ; | |
44 | m_foregroundColour = parent->GetForegroundColour() ; | |
45 | ||
46 | if ( id == -1 ) | |
47 | m_windowId = (int)NewControlId(); | |
48 | else | |
49 | m_windowId = id; | |
50 | ||
51 | m_windowStyle = style; | |
52 | m_label = label ; | |
53 | ||
54 | bool ret = wxControl::Create( parent, id, pos, size, style , wxDefaultValidator , name ); | |
584bede0 | 55 | SetBestSize( size ) ; |
8208e181 SC |
56 | |
57 | return ret; | |
58 | } | |
59 | ||
9714ffa0 SC |
60 | const wxString punct = " ,.-;:!?"; |
61 | ||
62 | void wxStaticText::DrawParagraph(wxDC &dc, wxString paragraph) | |
63 | { | |
64 | int x = 0 ; | |
65 | int y = 0 ; | |
66 | int i = 0 ; | |
67 | long width, height ; | |
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 ( int 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=""; | |
120 | y += height ; | |
121 | } | |
122 | } | |
123 | } | |
124 | ||
2f1ae414 | 125 | void wxStaticText::OnDraw( wxDC &dc ) |
8208e181 | 126 | { |
d015713e SC |
127 | if (m_width <= 0 || m_height <= 0) |
128 | return; | |
129 | ||
9714ffa0 SC |
130 | wxString paragraph; |
131 | int i = 0 ; | |
132 | wxString text = m_label; | |
133 | ||
8208e181 | 134 | PrepareDC(dc); |
2f1ae414 | 135 | |
bedaf53e SC |
136 | bool doClear = true ; |
137 | WindowRef window = GetMacRootWindow() ; | |
138 | if ( window ) | |
139 | { | |
140 | wxWindow* win = wxFindWinFromMacWindow( window ) ; | |
141 | if ( win ) | |
142 | { | |
143 | wxWindow* parent = GetParent() ; | |
144 | while ( parent ) | |
145 | { | |
146 | if( parent->MacGetWindowData() ) | |
147 | { | |
148 | break ; | |
149 | } | |
150 | ||
151 | if( parent->IsKindOf( CLASSINFO( wxNotebook ) ) || parent->IsKindOf( CLASSINFO( wxTabCtrl ) )) | |
152 | { | |
153 | if ( ((wxControl*)parent)->GetMacControl() ) { | |
154 | Rect rect = { -32767 , -32767 , 32767 , 32767 } ; | |
155 | if ( DrawThemeTabPane != (void*)kUnresolvedCFragSymbolAddress ) | |
4ce431b0 | 156 | { |
bedaf53e | 157 | DrawThemeTabPane ( &rect, kThemeStateActive); |
4ce431b0 SC |
158 | doClear = false ; |
159 | } | |
bedaf53e SC |
160 | } |
161 | break ; | |
162 | } | |
163 | ||
164 | parent = parent->GetParent() ; | |
165 | } | |
166 | } | |
167 | } | |
168 | if ( doClear ) | |
169 | dc.Clear() ; | |
170 | ||
9714ffa0 | 171 | while (i < text.Length()) |
2f1ae414 | 172 | { |
9714ffa0 SC |
173 | paragraph += text[i]; |
174 | ||
175 | if (text[i] == 13 || text[i] == 10) | |
176 | DrawParagraph(dc, paragraph); | |
177 | ||
178 | ++i; | |
179 | } | |
180 | if (paragraph.Length() > 0) | |
181 | DrawParagraph(dc, paragraph); | |
8208e181 SC |
182 | } |
183 | ||
2f1ae414 | 184 | void wxStaticText::OnPaint( wxPaintEvent &event ) |
8208e181 | 185 | { |
2f1ae414 SC |
186 | wxPaintDC dc(this); |
187 | OnDraw( dc ) ; | |
8208e181 SC |
188 | } |
189 | ||
2f1ae414 | 190 | wxSize wxStaticText::DoGetBestSize() const |
e9576ca5 | 191 | { |
2f1ae414 SC |
192 | int x , y ; |
193 | int widthTextMax = 0, widthLine, | |
194 | heightTextTotal = 0, heightLineDefault = 0, heightLine = 0; | |
519cb848 | 195 | |
2f1ae414 SC |
196 | wxString curLine; |
197 | for ( const wxChar *pc = m_label; ; pc++ ) { | |
198 | if ( *pc == wxT('\n') || *pc == wxT('\0') ) { | |
199 | if ( !curLine ) { | |
200 | // we can't use GetTextExtent - it will return 0 for both width | |
201 | // and height and an empty line should count in height | |
202 | // calculation | |
203 | if ( !heightLineDefault ) | |
204 | heightLineDefault = heightLine; | |
205 | if ( !heightLineDefault ) | |
206 | GetTextExtent(_T("W"), NULL, &heightLineDefault); | |
207 | ||
208 | heightTextTotal += heightLineDefault; | |
209 | } | |
210 | else { | |
211 | GetTextExtent(curLine, &widthLine, &heightLine); | |
212 | if ( widthLine > widthTextMax ) | |
213 | widthTextMax = widthLine; | |
214 | heightTextTotal += heightLine; | |
215 | } | |
216 | ||
217 | if ( *pc == wxT('\n') ) { | |
218 | curLine.Empty(); | |
219 | } | |
220 | else { | |
221 | // the end of string | |
222 | break; | |
223 | } | |
224 | } | |
225 | else { | |
226 | curLine += *pc; | |
227 | } | |
228 | } | |
519cb848 | 229 | |
2f1ae414 | 230 | return wxSize(widthTextMax, heightTextTotal); |
e9576ca5 SC |
231 | } |
232 | ||
2f1ae414 | 233 | void wxStaticText::SetLabel(const wxString& st ) |
e9576ca5 | 234 | { |
e7549107 | 235 | SetTitle( st ) ; |
2f1ae414 SC |
236 | m_label = st ; |
237 | if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) ) | |
584bede0 | 238 | SetSize( GetBestSize() ) ; |
9ff647cf SC |
239 | |
240 | Refresh() ; | |
241 | MacUpdateImmediately() ; | |
242 | // wxClientDC dc(this); | |
243 | // OnDraw( dc ) ; | |
e9576ca5 | 244 | } |