]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: textctrl.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Created: 01/02/97 | |
6 | // Id: | |
7 | // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | #ifdef __GNUG__ | |
12 | #pragma implementation "textctrl.h" | |
13 | #endif | |
14 | ||
15 | #include "wx/textctrl.h" | |
16 | #include "wx/utils.h" | |
17 | ||
18 | //----------------------------------------------------------------------------- | |
19 | // wxTextCtrl | |
20 | //----------------------------------------------------------------------------- | |
21 | ||
22 | IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl,wxControl) | |
23 | ||
24 | ||
25 | BEGIN_EVENT_TABLE(wxTextCtrl, wxControl) | |
26 | // EVT_CHAR(wxTextCtrl::OnChar) | |
27 | END_EVENT_TABLE() | |
28 | ||
29 | wxTextCtrl::wxTextCtrl(void) : streambuf() | |
30 | { | |
31 | }; | |
32 | ||
33 | wxTextCtrl::wxTextCtrl( wxWindow *parent, const wxWindowID id, const wxString &value, | |
34 | const wxPoint &pos, const wxSize &size, | |
35 | const int style, const wxString &name ) : streambuf() | |
36 | { | |
37 | Create( parent, id, value, pos, size, style, name ); | |
38 | }; | |
39 | ||
40 | bool wxTextCtrl::Create( wxWindow *parent, const wxWindowID id, const wxString &value, | |
41 | const wxPoint &pos, const wxSize &size, | |
42 | const int style, const wxString &name ) | |
43 | { | |
44 | m_needParent = TRUE; | |
45 | ||
46 | PreCreation( parent, id, pos, size, style, name ); | |
47 | ||
48 | if (style & wxTE_MULTILINE) | |
49 | m_widget = gtk_text_new( NULL, NULL ); | |
50 | else | |
51 | m_widget = gtk_entry_new(); | |
52 | ||
53 | if (!value.IsNull()) | |
54 | { | |
55 | gint tmp = 0; | |
56 | gtk_editable_insert_text( GTK_EDITABLE(m_widget), value, value.Length(), &tmp ); | |
57 | }; | |
58 | ||
59 | wxSize newSize = size; | |
60 | if (newSize.x == -1) newSize.x = 80; | |
61 | if (newSize.y == -1) newSize.y = 26; | |
62 | SetSize( newSize.x, newSize.y ); | |
63 | ||
64 | PostCreation(); | |
65 | ||
66 | Show( TRUE ); | |
67 | ||
68 | return TRUE; | |
69 | }; | |
70 | ||
71 | wxString wxTextCtrl::GetValue(void) const | |
72 | { | |
73 | wxString tmp; | |
74 | if (m_windowStyle & wxTE_MULTILINE) | |
75 | { | |
76 | gint len = gtk_text_get_length( GTK_TEXT(m_widget) ); | |
77 | tmp = gtk_editable_get_chars( GTK_EDITABLE(m_widget), 0, len-1 ); | |
78 | } | |
79 | else | |
80 | { | |
81 | tmp = gtk_entry_get_text( GTK_ENTRY(m_widget) ); | |
82 | }; | |
83 | return tmp; | |
84 | }; | |
85 | ||
86 | void wxTextCtrl::SetValue( const wxString &value ) | |
87 | { | |
88 | wxString tmp = ""; | |
89 | if (!value.IsNull()) tmp = value; | |
90 | if (m_windowStyle & wxTE_MULTILINE) | |
91 | { | |
92 | gint len = gtk_text_get_length( GTK_TEXT(m_widget) ); | |
93 | gtk_editable_delete_text( GTK_EDITABLE(m_widget), 0, len-1 ); | |
94 | len = 0; | |
95 | gtk_editable_insert_text( GTK_EDITABLE(m_widget), tmp, tmp.Length(), &len ); | |
96 | } | |
97 | else | |
98 | { | |
99 | gtk_entry_set_text( GTK_ENTRY(m_widget), tmp ); | |
100 | }; | |
101 | }; | |
102 | ||
103 | void wxTextCtrl::WriteText( const wxString &text ) | |
104 | { | |
105 | if (text.IsNull()) return; | |
106 | if (m_windowStyle & wxTE_MULTILINE) | |
107 | { | |
108 | gint len = gtk_text_get_length( GTK_TEXT(m_widget) ); | |
109 | gtk_editable_insert_text( GTK_EDITABLE(m_widget), text, text.Length(), &len ); | |
110 | } | |
111 | else | |
112 | { | |
113 | gtk_entry_append_text( GTK_ENTRY(m_widget), text ); | |
114 | }; | |
115 | }; | |
116 | ||
117 | /* | |
118 | wxString wxTextCtrl::GetLineText( const long lineNo ) const | |
119 | { | |
120 | }; | |
121 | ||
122 | bool wxTextCtrl::LoadFile( const wxString &file ) | |
123 | { | |
124 | }; | |
125 | ||
126 | bool wxTextCtrl::SaveFile( const wxString &file ) | |
127 | { | |
128 | }; | |
129 | ||
130 | void wxTextCtrl::DiscardEdits(void) | |
131 | { | |
132 | }; | |
133 | ||
134 | bool wxTextCtrl::IsModified(void) | |
135 | { | |
136 | }; | |
137 | ||
138 | void wxTextCtrl::OnDropFiles( wxDropFilesEvent &event ) | |
139 | { | |
140 | }; | |
141 | ||
142 | long wxTextCtrl::PositionToXY( const long pos, long *x, long *y ) const | |
143 | { | |
144 | }; | |
145 | ||
146 | long wxTextCtrl::XYToPosition( const long x, const long y ) | |
147 | { | |
148 | }; | |
149 | ||
150 | int wxTextCtrl::GetNumberOfLines(void) | |
151 | { | |
152 | }; | |
153 | ||
154 | */ | |
155 | void wxTextCtrl::SetInsertionPoint( const long pos ) | |
156 | { | |
157 | int tmp = (int) pos; | |
158 | if (m_windowStyle & wxTE_MULTILINE) | |
159 | gtk_text_set_point( GTK_TEXT(m_widget), tmp ); | |
160 | else | |
161 | gtk_entry_set_position( GTK_ENTRY(m_widget), tmp ); | |
162 | }; | |
163 | ||
164 | void wxTextCtrl::SetInsertionPointEnd(void) | |
165 | { | |
166 | int pos = 0; | |
167 | if (m_windowStyle & wxTE_MULTILINE) | |
168 | pos = gtk_text_get_length( GTK_TEXT(m_widget) ); | |
169 | else | |
170 | pos = GTK_ENTRY(m_widget)->text_length; | |
171 | SetInsertionPoint( pos-1 ); | |
172 | }; | |
173 | ||
174 | void wxTextCtrl::SetEditable( const bool editable ) | |
175 | { | |
176 | if (m_windowStyle & wxTE_MULTILINE) | |
177 | gtk_text_set_editable( GTK_TEXT(m_widget), editable ); | |
178 | else | |
179 | gtk_entry_set_editable( GTK_ENTRY(m_widget), editable ); | |
180 | }; | |
181 | ||
182 | void wxTextCtrl::SetSelection( const long from, const long to ) | |
183 | { | |
184 | gtk_editable_select_region( GTK_EDITABLE(m_widget), (gint)from, (gint)to ); | |
185 | }; | |
186 | ||
187 | void wxTextCtrl::ShowPosition( const long WXUNUSED(pos) ) | |
188 | { | |
189 | }; | |
190 | ||
191 | long wxTextCtrl::GetInsertionPoint(void) const | |
192 | { | |
193 | return (long) GTK_EDITABLE(m_widget)->current_pos; | |
194 | }; | |
195 | ||
196 | long wxTextCtrl::GetLastPosition(void) const | |
197 | { | |
198 | int pos = 0; | |
199 | if (m_windowStyle & wxTE_MULTILINE) | |
200 | pos = gtk_text_get_length( GTK_TEXT(m_widget) ); | |
201 | else | |
202 | pos = GTK_ENTRY(m_widget)->text_length; | |
203 | return (long)pos-1; | |
204 | }; | |
205 | ||
206 | void wxTextCtrl::Remove( const long from, const long to ) | |
207 | { | |
208 | gtk_editable_delete_text( GTK_EDITABLE(m_widget), (gint)from, (gint)to ); | |
209 | }; | |
210 | ||
211 | void wxTextCtrl::Replace( const long from, const long to, const wxString &value ) | |
212 | { | |
213 | gtk_editable_delete_text( GTK_EDITABLE(m_widget), (gint)from, (gint)to ); | |
214 | if (value.IsNull()) return; | |
215 | gint pos = (gint)to; | |
216 | gtk_editable_insert_text( GTK_EDITABLE(m_widget), value, value.Length(), &pos ); | |
217 | }; | |
218 | ||
219 | void wxTextCtrl::Cut(void) | |
220 | { | |
221 | gtk_editable_cut_clipboard( GTK_EDITABLE(m_widget), 0 ); | |
222 | }; | |
223 | ||
224 | void wxTextCtrl::Copy(void) | |
225 | { | |
226 | gtk_editable_copy_clipboard( GTK_EDITABLE(m_widget), 0 ); | |
227 | }; | |
228 | ||
229 | void wxTextCtrl::Paste(void) | |
230 | { | |
231 | gtk_editable_paste_clipboard( GTK_EDITABLE(m_widget), 0 ); | |
232 | }; | |
233 | ||
234 | void wxTextCtrl::Delete(void) | |
235 | { | |
236 | SetValue( "" ); | |
237 | }; | |
238 | ||
239 | void wxTextCtrl::OnChar( wxKeyEvent &WXUNUSED(event) ) | |
240 | { | |
241 | }; | |
242 | ||
243 | int wxTextCtrl::overflow(int c) | |
244 | { | |
245 | // Make sure there is a holding area | |
246 | if ( allocate()==EOF ) | |
247 | { | |
248 | wxError("Streambuf allocation failed","Internal error"); | |
249 | return EOF; | |
250 | } | |
251 | ||
252 | // Verify that there are no characters in get area | |
253 | if ( gptr() && gptr() < egptr() ) | |
254 | { | |
255 | wxError("Who's trespassing my get area?","Internal error"); | |
256 | return EOF; | |
257 | } | |
258 | ||
259 | // Reset get area | |
260 | setg(0,0,0); | |
261 | ||
262 | // Make sure there is a put area | |
263 | if ( ! pptr() ) | |
264 | { | |
265 | /* This doesn't seem to be fatal so comment out error message */ | |
266 | // wxError("Put area not opened","Internal error"); | |
267 | setp( base(), base() ); | |
268 | } | |
269 | ||
270 | // Determine how many characters have been inserted but no consumed | |
271 | int plen = pptr() - pbase(); | |
272 | ||
273 | // Now Jerry relies on the fact that the buffer is at least 2 chars | |
274 | // long, but the holding area "may be as small as 1" ??? | |
275 | // And we need an additional \0, so let's keep this inefficient but | |
276 | // safe copy. | |
277 | ||
278 | // If c!=EOF, it is a character that must also be comsumed | |
279 | int xtra = c==EOF? 0 : 1; | |
280 | ||
281 | // Write temporary C-string to wxTextWindow | |
282 | { | |
283 | char *txt = new char[plen+xtra+1]; | |
284 | memcpy(txt, pbase(), plen); | |
285 | txt[plen] = (char)c; // append c | |
286 | txt[plen+xtra] = '\0'; // append '\0' or overwrite c | |
287 | // If the put area already contained \0, output will be truncated there | |
288 | WriteText(txt); | |
289 | delete[] txt; | |
290 | } | |
291 | ||
292 | // Reset put area | |
293 | setp(pbase(), epptr()); | |
294 | ||
295 | #if defined(__WATCOMC__) | |
296 | return __NOT_EOF; | |
297 | #elif defined(zapeof) // HP-UX (all cfront based?) | |
298 | return zapeof(c); | |
299 | #else | |
300 | return c!=EOF ? c : 0; // this should make everybody happy | |
301 | #endif | |
302 | ||
303 | /* OLD CODE | |
304 | int len = pptr() - pbase(); | |
305 | char *txt = new char[len+1]; | |
306 | strncpy(txt, pbase(), len); | |
307 | txt[len] = '\0'; | |
308 | (*this) << txt; | |
309 | setp(pbase(), epptr()); | |
310 | delete[] txt; | |
311 | return EOF; | |
312 | */ | |
313 | }; | |
314 | ||
315 | int wxTextCtrl::sync(void) | |
316 | { | |
317 | // Verify that there are no characters in get area | |
318 | if ( gptr() && gptr() < egptr() ) | |
319 | { | |
320 | wxError("Who's trespassing my get area?","Internal error"); | |
321 | return EOF; | |
322 | } | |
323 | ||
324 | if ( pptr() && pptr() > pbase() ) return overflow(EOF); | |
325 | ||
326 | return 0; | |
327 | /* OLD CODE | |
328 | int len = pptr() - pbase(); | |
329 | char *txt = new char[len+1]; | |
330 | strncpy(txt, pbase(), len); | |
331 | txt[len] = '\0'; | |
332 | (*this) << txt; | |
333 | setp(pbase(), epptr()); | |
334 | delete[] txt; | |
335 | return 0; | |
336 | */ | |
337 | }; | |
338 | ||
339 | int wxTextCtrl::underflow(void) | |
340 | { | |
341 | return EOF; | |
342 | }; | |
343 | ||
344 | wxTextCtrl& wxTextCtrl::operator<<(const wxString& s) | |
345 | { | |
346 | WriteText(s); | |
347 | return *this; | |
348 | } | |
349 | ||
350 | wxTextCtrl& wxTextCtrl::operator<<(const float f) | |
351 | { | |
352 | static char buf[100]; | |
353 | sprintf(buf, "%.2f", f); | |
354 | WriteText(buf); | |
355 | return *this; | |
356 | } | |
357 | ||
358 | wxTextCtrl& wxTextCtrl::operator<<(const double d) | |
359 | { | |
360 | static char buf[100]; | |
361 | sprintf(buf, "%.2f", d); | |
362 | WriteText(buf); | |
363 | return *this; | |
364 | } | |
365 | ||
366 | wxTextCtrl& wxTextCtrl::operator<<(const int i) | |
367 | { | |
368 | static char buf[100]; | |
369 | sprintf(buf, "%i", i); | |
370 | WriteText(buf); | |
371 | return *this; | |
372 | } | |
373 | ||
374 | wxTextCtrl& wxTextCtrl::operator<<(const long i) | |
375 | { | |
376 | static char buf[100]; | |
377 | sprintf(buf, "%ld", i); | |
378 | WriteText(buf); | |
379 | return *this; | |
380 | } | |
381 | ||
382 | wxTextCtrl& wxTextCtrl::operator<<(const char c) | |
383 | { | |
384 | char buf[2]; | |
385 | ||
386 | buf[0] = c; | |
387 | buf[1] = 0; | |
388 | WriteText(buf); | |
389 | return *this; | |
390 | } | |
391 |