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