]>
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 | |
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" |
e9576ca5 SC |
23 | |
24 | #include <stdio.h> | |
25 | ||
2f1ae414 | 26 | #if !USE_SHARED_LIBRARY |
90b959ae | 27 | IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxControl) |
2f1ae414 | 28 | #endif |
e9576ca5 | 29 | |
d497dca4 | 30 | #include "wx/mac/uma.h" |
519cb848 | 31 | |
584bede0 | 32 | BEGIN_EVENT_TABLE(wxStaticText, wxStaticTextBase) |
8208e181 SC |
33 | EVT_PAINT(wxStaticText::OnPaint) |
34 | END_EVENT_TABLE() | |
35 | ||
36 | bool wxStaticText::Create(wxWindow *parent, wxWindowID id, | |
37 | const wxString& label, | |
38 | const wxPoint& pos, | |
39 | const wxSize& size, | |
40 | long style, | |
41 | const wxString& name) | |
42 | { | |
43 | SetName(name); | |
44 | m_backgroundColour = parent->GetBackgroundColour() ; | |
45 | m_foregroundColour = parent->GetForegroundColour() ; | |
46 | ||
47 | if ( id == -1 ) | |
00500f40 | 48 | m_windowId = (int)NewControlId(); |
8208e181 | 49 | else |
00500f40 | 50 | m_windowId = id; |
8208e181 SC |
51 | |
52 | m_windowStyle = style; | |
53 | m_label = label ; | |
54 | ||
00500f40 RR |
55 | bool ret = wxControl::Create( parent, id, pos, size, style , wxDefaultValidator , name ); |
56 | SetBestSize( size ) ; | |
8208e181 SC |
57 | |
58 | return ret; | |
59 | } | |
60 | ||
9714ffa0 SC |
61 | const wxString punct = " ,.-;:!?"; |
62 | ||
63 | void wxStaticText::DrawParagraph(wxDC &dc, wxString paragraph) | |
64 | { | |
65 | int x = 0 ; | |
66 | int y = 0 ; | |
67 | int i = 0 ; | |
68 | long width, height ; | |
69 | bool linedrawn = true; | |
70 | while( paragraph.Length() > 0 ) | |
71 | { | |
00500f40 RR |
72 | dc.GetTextExtent( paragraph , &width , &height ) ; |
73 | ||
74 | if ( width > m_width ) | |
75 | { | |
76 | for ( int p = paragraph.Length() -1 ; p > 0 ; --p ) | |
77 | { | |
78 | if ((punct.Find(paragraph[p]) != wxNOT_FOUND) || !linedrawn) | |
79 | { | |
80 | int blank = (paragraph[p] == ' ') ? 0 : 1; | |
81 | ||
82 | dc.GetTextExtent( paragraph.Left(p + blank) , &width , &height ) ; | |
83 | ||
84 | if ( width <= m_width ) | |
85 | { | |
86 | int pos = x ; | |
87 | if ( HasFlag( wxALIGN_CENTER ) ) | |
88 | { | |
89 | pos += ( m_width - width ) / 2 ; | |
90 | } | |
91 | else if ( HasFlag( wxALIGN_RIGHT ) ) | |
92 | { | |
93 | pos += ( m_width - width ) ; | |
94 | } | |
95 | ||
96 | dc.DrawText( paragraph.Left(p + blank), pos , y) ; | |
97 | y += height ; | |
98 | paragraph = paragraph.Mid(p+1) ; | |
99 | linedrawn = true; | |
100 | break ; | |
101 | } | |
102 | } | |
103 | } | |
104 | ||
105 | linedrawn = false; | |
106 | } | |
107 | else | |
108 | { | |
109 | int pos = x ; | |
110 | if ( HasFlag( wxALIGN_CENTER ) ) | |
111 | { | |
112 | pos += ( m_width - width ) / 2 ; | |
113 | } | |
114 | else if ( HasFlag( wxALIGN_RIGHT ) ) | |
115 | { | |
116 | pos += ( m_width - width ) ; | |
117 | } | |
118 | ||
119 | dc.DrawText( paragraph, pos , y) ; | |
120 | paragraph=""; | |
121 | y += height ; | |
122 | } | |
123 | } | |
9714ffa0 SC |
124 | } |
125 | ||
2f1ae414 | 126 | void wxStaticText::OnDraw( wxDC &dc ) |
8208e181 | 127 | { |
d015713e SC |
128 | if (m_width <= 0 || m_height <= 0) |
129 | return; | |
130 | ||
1c469f7f SC |
131 | if ( !IsWindowHilited( MacGetRootWindow() ) ) |
132 | { | |
133 | dc.SetTextForeground( wxColour( 0x80 , 0x80 , 0x80 ) ) ; | |
134 | } | |
607f6d2b SC |
135 | wxString paragraph; |
136 | int i = 0 ; | |
137 | wxString text = m_label; | |
138 | while (i < text.Length()) | |
139 | { | |
140 | paragraph += text[i]; | |
141 | ||
142 | if (text[i] == 13 || text[i] == 10) | |
143 | DrawParagraph(dc, paragraph); | |
144 | ||
145 | ++i; | |
146 | } | |
147 | if (paragraph.Length() > 0) | |
148 | DrawParagraph(dc, paragraph); | |
8208e181 SC |
149 | } |
150 | ||
2f1ae414 | 151 | void wxStaticText::OnPaint( wxPaintEvent &event ) |
8208e181 | 152 | { |
2f1ae414 SC |
153 | wxPaintDC dc(this); |
154 | OnDraw( dc ) ; | |
8208e181 SC |
155 | } |
156 | ||
2f1ae414 | 157 | wxSize wxStaticText::DoGetBestSize() const |
e9576ca5 | 158 | { |
00500f40 | 159 | int x,y ; |
1348e1a5 | 160 | int widthTextMax = 0, widthLine, |
2f1ae414 | 161 | heightTextTotal = 0, heightLineDefault = 0, heightLine = 0; |
1348e1a5 | 162 | |
2f1ae414 | 163 | wxString curLine; |
1348e1a5 RR |
164 | for ( const wxChar *pc = m_label; ; pc++ ) |
165 | { | |
166 | if ( *pc == wxT('\n') || *pc == wxT('\0') ) | |
167 | { | |
168 | if ( !curLine ) | |
169 | { | |
2f1ae414 SC |
170 | // we can't use GetTextExtent - it will return 0 for both width |
171 | // and height and an empty line should count in height | |
172 | // calculation | |
173 | if ( !heightLineDefault ) | |
174 | heightLineDefault = heightLine; | |
175 | if ( !heightLineDefault ) | |
176 | GetTextExtent(_T("W"), NULL, &heightLineDefault); | |
177 | ||
178 | heightTextTotal += heightLineDefault; | |
1348e1a5 RR |
179 | |
180 | heightTextTotal++; // FIXME: why is this necessary? | |
2f1ae414 | 181 | } |
1348e1a5 RR |
182 | else |
183 | { | |
2f1ae414 SC |
184 | GetTextExtent(curLine, &widthLine, &heightLine); |
185 | if ( widthLine > widthTextMax ) | |
186 | widthTextMax = widthLine; | |
187 | heightTextTotal += heightLine; | |
1348e1a5 RR |
188 | |
189 | heightTextTotal++; // FIXME: why is this necessary? | |
2f1ae414 SC |
190 | } |
191 | ||
192 | if ( *pc == wxT('\n') ) { | |
193 | curLine.Empty(); | |
194 | } | |
195 | else { | |
196 | // the end of string | |
197 | break; | |
198 | } | |
199 | } | |
200 | else { | |
201 | curLine += *pc; | |
202 | } | |
203 | } | |
519cb848 | 204 | |
2f1ae414 | 205 | return wxSize(widthTextMax, heightTextTotal); |
e9576ca5 SC |
206 | } |
207 | ||
2f1ae414 | 208 | void wxStaticText::SetLabel(const wxString& st ) |
e9576ca5 | 209 | { |
607f6d2b SC |
210 | SetTitle( st ) ; |
211 | m_label = st ; | |
212 | if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) ) | |
213 | SetSize( GetBestSize() ) ; | |
214 | ||
215 | Refresh() ; | |
216 | Update() ; | |
e9576ca5 | 217 | } |