]>
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 | ||
c801d85f KB |
53 | wxSize newSize = size; |
54 | if (newSize.x == -1) newSize.x = 80; | |
55 | if (newSize.y == -1) newSize.y = 26; | |
56 | SetSize( newSize.x, newSize.y ); | |
57 | ||
58 | PostCreation(); | |
59 | ||
7f4dc78d RR |
60 | if (!value.IsNull()) |
61 | { | |
62 | gint tmp = 0; | |
63 | gtk_editable_insert_text( GTK_EDITABLE(m_widget), value, value.Length(), &tmp ); | |
64 | }; | |
65 | ||
c801d85f KB |
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; | |
7f4dc78d | 106 | |
c801d85f KB |
107 | if (m_windowStyle & wxTE_MULTILINE) |
108 | { | |
7f4dc78d | 109 | gint len = gtk_text_get_length( GTK_TEXT(m_widget) ) - 1; |
c801d85f KB |
110 | gtk_editable_insert_text( GTK_EDITABLE(m_widget), text, text.Length(), &len ); |
111 | } | |
112 | else | |
113 | { | |
114 | gtk_entry_append_text( GTK_ENTRY(m_widget), text ); | |
115 | }; | |
116 | }; | |
117 | ||
118 | /* | |
119 | wxString wxTextCtrl::GetLineText( const long lineNo ) const | |
120 | { | |
121 | }; | |
122 | ||
123 | bool wxTextCtrl::LoadFile( const wxString &file ) | |
124 | { | |
125 | }; | |
126 | ||
127 | bool wxTextCtrl::SaveFile( const wxString &file ) | |
128 | { | |
129 | }; | |
130 | ||
131 | void wxTextCtrl::DiscardEdits(void) | |
132 | { | |
133 | }; | |
134 | ||
135 | bool wxTextCtrl::IsModified(void) | |
136 | { | |
137 | }; | |
138 | ||
139 | void wxTextCtrl::OnDropFiles( wxDropFilesEvent &event ) | |
140 | { | |
141 | }; | |
142 | ||
143 | long wxTextCtrl::PositionToXY( const long pos, long *x, long *y ) const | |
144 | { | |
145 | }; | |
146 | ||
147 | long wxTextCtrl::XYToPosition( const long x, const long y ) | |
148 | { | |
149 | }; | |
150 | ||
151 | int wxTextCtrl::GetNumberOfLines(void) | |
152 | { | |
153 | }; | |
154 | ||
155 | */ | |
156 | void wxTextCtrl::SetInsertionPoint( const long pos ) | |
157 | { | |
158 | int tmp = (int) pos; | |
159 | if (m_windowStyle & wxTE_MULTILINE) | |
160 | gtk_text_set_point( GTK_TEXT(m_widget), tmp ); | |
161 | else | |
162 | gtk_entry_set_position( GTK_ENTRY(m_widget), tmp ); | |
163 | }; | |
164 | ||
165 | void wxTextCtrl::SetInsertionPointEnd(void) | |
166 | { | |
167 | int pos = 0; | |
168 | if (m_windowStyle & wxTE_MULTILINE) | |
169 | pos = gtk_text_get_length( GTK_TEXT(m_widget) ); | |
170 | else | |
171 | pos = GTK_ENTRY(m_widget)->text_length; | |
172 | SetInsertionPoint( pos-1 ); | |
173 | }; | |
174 | ||
175 | void wxTextCtrl::SetEditable( const bool editable ) | |
176 | { | |
177 | if (m_windowStyle & wxTE_MULTILINE) | |
178 | gtk_text_set_editable( GTK_TEXT(m_widget), editable ); | |
179 | else | |
180 | gtk_entry_set_editable( GTK_ENTRY(m_widget), editable ); | |
181 | }; | |
182 | ||
183 | void wxTextCtrl::SetSelection( const long from, const long to ) | |
184 | { | |
185 | gtk_editable_select_region( GTK_EDITABLE(m_widget), (gint)from, (gint)to ); | |
186 | }; | |
187 | ||
188 | void wxTextCtrl::ShowPosition( const long WXUNUSED(pos) ) | |
189 | { | |
190 | }; | |
191 | ||
192 | long wxTextCtrl::GetInsertionPoint(void) const | |
193 | { | |
194 | return (long) GTK_EDITABLE(m_widget)->current_pos; | |
195 | }; | |
196 | ||
197 | long wxTextCtrl::GetLastPosition(void) const | |
198 | { | |
199 | int pos = 0; | |
200 | if (m_windowStyle & wxTE_MULTILINE) | |
201 | pos = gtk_text_get_length( GTK_TEXT(m_widget) ); | |
202 | else | |
203 | pos = GTK_ENTRY(m_widget)->text_length; | |
204 | return (long)pos-1; | |
205 | }; | |
206 | ||
207 | void wxTextCtrl::Remove( const long from, const long to ) | |
208 | { | |
209 | gtk_editable_delete_text( GTK_EDITABLE(m_widget), (gint)from, (gint)to ); | |
210 | }; | |
211 | ||
212 | void wxTextCtrl::Replace( const long from, const long to, const wxString &value ) | |
213 | { | |
214 | gtk_editable_delete_text( GTK_EDITABLE(m_widget), (gint)from, (gint)to ); | |
215 | if (value.IsNull()) return; | |
216 | gint pos = (gint)to; | |
217 | gtk_editable_insert_text( GTK_EDITABLE(m_widget), value, value.Length(), &pos ); | |
218 | }; | |
219 | ||
220 | void wxTextCtrl::Cut(void) | |
221 | { | |
222 | gtk_editable_cut_clipboard( GTK_EDITABLE(m_widget), 0 ); | |
223 | }; | |
224 | ||
225 | void wxTextCtrl::Copy(void) | |
226 | { | |
227 | gtk_editable_copy_clipboard( GTK_EDITABLE(m_widget), 0 ); | |
228 | }; | |
229 | ||
230 | void wxTextCtrl::Paste(void) | |
231 | { | |
232 | gtk_editable_paste_clipboard( GTK_EDITABLE(m_widget), 0 ); | |
233 | }; | |
234 | ||
235 | void wxTextCtrl::Delete(void) | |
236 | { | |
237 | SetValue( "" ); | |
238 | }; | |
239 | ||
240 | void wxTextCtrl::OnChar( wxKeyEvent &WXUNUSED(event) ) | |
241 | { | |
242 | }; | |
243 | ||
244 | int wxTextCtrl::overflow(int c) | |
245 | { | |
246 | // Make sure there is a holding area | |
247 | if ( allocate()==EOF ) | |
248 | { | |
249 | wxError("Streambuf allocation failed","Internal error"); | |
250 | return EOF; | |
251 | } | |
252 | ||
253 | // Verify that there are no characters in get area | |
254 | if ( gptr() && gptr() < egptr() ) | |
255 | { | |
256 | wxError("Who's trespassing my get area?","Internal error"); | |
257 | return EOF; | |
258 | } | |
259 | ||
260 | // Reset get area | |
261 | setg(0,0,0); | |
262 | ||
263 | // Make sure there is a put area | |
264 | if ( ! pptr() ) | |
265 | { | |
266 | /* This doesn't seem to be fatal so comment out error message */ | |
267 | // wxError("Put area not opened","Internal error"); | |
268 | setp( base(), base() ); | |
269 | } | |
270 | ||
271 | // Determine how many characters have been inserted but no consumed | |
272 | int plen = pptr() - pbase(); | |
273 | ||
274 | // Now Jerry relies on the fact that the buffer is at least 2 chars | |
275 | // long, but the holding area "may be as small as 1" ??? | |
276 | // And we need an additional \0, so let's keep this inefficient but | |
277 | // safe copy. | |
278 | ||
279 | // If c!=EOF, it is a character that must also be comsumed | |
280 | int xtra = c==EOF? 0 : 1; | |
281 | ||
282 | // Write temporary C-string to wxTextWindow | |
283 | { | |
284 | char *txt = new char[plen+xtra+1]; | |
285 | memcpy(txt, pbase(), plen); | |
286 | txt[plen] = (char)c; // append c | |
287 | txt[plen+xtra] = '\0'; // append '\0' or overwrite c | |
288 | // If the put area already contained \0, output will be truncated there | |
289 | WriteText(txt); | |
290 | delete[] txt; | |
291 | } | |
292 | ||
293 | // Reset put area | |
294 | setp(pbase(), epptr()); | |
295 | ||
296 | #if defined(__WATCOMC__) | |
297 | return __NOT_EOF; | |
298 | #elif defined(zapeof) // HP-UX (all cfront based?) | |
299 | return zapeof(c); | |
300 | #else | |
301 | return c!=EOF ? c : 0; // this should make everybody happy | |
302 | #endif | |
303 | ||
304 | /* OLD CODE | |
305 | int len = pptr() - pbase(); | |
306 | char *txt = new char[len+1]; | |
307 | strncpy(txt, pbase(), len); | |
308 | txt[len] = '\0'; | |
309 | (*this) << txt; | |
310 | setp(pbase(), epptr()); | |
311 | delete[] txt; | |
312 | return EOF; | |
313 | */ | |
314 | }; | |
315 | ||
316 | int wxTextCtrl::sync(void) | |
317 | { | |
318 | // Verify that there are no characters in get area | |
319 | if ( gptr() && gptr() < egptr() ) | |
320 | { | |
321 | wxError("Who's trespassing my get area?","Internal error"); | |
322 | return EOF; | |
323 | } | |
324 | ||
325 | if ( pptr() && pptr() > pbase() ) return overflow(EOF); | |
326 | ||
327 | return 0; | |
328 | /* OLD CODE | |
329 | int len = pptr() - pbase(); | |
330 | char *txt = new char[len+1]; | |
331 | strncpy(txt, pbase(), len); | |
332 | txt[len] = '\0'; | |
333 | (*this) << txt; | |
334 | setp(pbase(), epptr()); | |
335 | delete[] txt; | |
336 | return 0; | |
337 | */ | |
338 | }; | |
339 | ||
340 | int wxTextCtrl::underflow(void) | |
341 | { | |
342 | return EOF; | |
343 | }; | |
344 | ||
345 | wxTextCtrl& wxTextCtrl::operator<<(const wxString& s) | |
346 | { | |
347 | WriteText(s); | |
348 | return *this; | |
349 | } | |
350 | ||
351 | wxTextCtrl& wxTextCtrl::operator<<(const float f) | |
352 | { | |
353 | static char buf[100]; | |
354 | sprintf(buf, "%.2f", f); | |
355 | WriteText(buf); | |
356 | return *this; | |
357 | } | |
358 | ||
359 | wxTextCtrl& wxTextCtrl::operator<<(const double d) | |
360 | { | |
361 | static char buf[100]; | |
362 | sprintf(buf, "%.2f", d); | |
363 | WriteText(buf); | |
364 | return *this; | |
365 | } | |
366 | ||
367 | wxTextCtrl& wxTextCtrl::operator<<(const int i) | |
368 | { | |
369 | static char buf[100]; | |
370 | sprintf(buf, "%i", i); | |
371 | WriteText(buf); | |
372 | return *this; | |
373 | } | |
374 | ||
375 | wxTextCtrl& wxTextCtrl::operator<<(const long i) | |
376 | { | |
377 | static char buf[100]; | |
378 | sprintf(buf, "%ld", i); | |
379 | WriteText(buf); | |
380 | return *this; | |
381 | } | |
382 | ||
383 | wxTextCtrl& wxTextCtrl::operator<<(const char c) | |
384 | { | |
385 | char buf[2]; | |
386 | ||
387 | buf[0] = c; | |
388 | buf[1] = 0; | |
389 | WriteText(buf); | |
390 | return *this; | |
391 | } | |
392 |