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