1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "textctrl.h"
15 #include "wx/textctrl.h"
18 //-----------------------------------------------------------------------------
20 //-----------------------------------------------------------------------------
22 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
,wxControl
)
24 void gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
26 win
->m_modified
= TRUE
;
30 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
31 // EVT_CHAR(wxTextCtrl::OnChar)
34 wxTextCtrl::wxTextCtrl(void) : streambuf()
39 wxTextCtrl::wxTextCtrl( wxWindow
*parent
, wxWindowID id
, const wxString
&value
,
40 const wxPoint
&pos
, const wxSize
&size
,
41 int style
, const wxString
&name
) : streambuf()
44 Create( parent
, id
, value
, pos
, size
, style
, name
);
47 bool wxTextCtrl::Create( wxWindow
*parent
, wxWindowID id
, const wxString
&value
,
48 const wxPoint
&pos
, const wxSize
&size
,
49 int style
, const wxString
&name
)
53 PreCreation( parent
, id
, pos
, size
, style
, name
);
55 if (style
& wxTE_MULTILINE
)
56 m_widget
= gtk_text_new( NULL
, NULL
);
58 m_widget
= gtk_entry_new();
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
);
67 // we want to be notified about text changes
68 gtk_signal_connect(GTK_OBJECT(m_widget
), "changed",
69 GTK_SIGNAL_FUNC(gtk_text_changed_callback
),
75 gtk_editable_insert_text( GTK_EDITABLE(m_widget
), value
, value
.Length(), &tmp
);
78 if (style
& wxREADONLY
)
83 if (style
& wxTE_MULTILINE
) gtk_text_set_editable( GTK_TEXT(m_widget
), 1 );
91 wxString
wxTextCtrl::GetValue(void) const
94 if (m_windowStyle
& wxTE_MULTILINE
)
96 gint len
= gtk_text_get_length( GTK_TEXT(m_widget
) );
97 tmp
= gtk_editable_get_chars( GTK_EDITABLE(m_widget
), 0, len
);
101 tmp
= gtk_entry_get_text( GTK_ENTRY(m_widget
) );
106 void wxTextCtrl::SetValue( const wxString
&value
)
109 if (!value
.IsNull()) tmp
= value
;
110 if (m_windowStyle
& wxTE_MULTILINE
)
112 gint len
= gtk_text_get_length( GTK_TEXT(m_widget
) );
113 gtk_editable_delete_text( GTK_EDITABLE(m_widget
), 0, len
);
115 gtk_editable_insert_text( GTK_EDITABLE(m_widget
), tmp
, tmp
.Length(), &len
);
119 gtk_entry_set_text( GTK_ENTRY(m_widget
), tmp
);
123 void wxTextCtrl::WriteText( const wxString
&text
)
125 if (text
.IsNull()) return;
127 if (m_windowStyle
& wxTE_MULTILINE
)
129 gint len
= gtk_text_get_length( GTK_TEXT(m_widget
) );
130 gtk_editable_insert_text( GTK_EDITABLE(m_widget
), text
, text
.Length(), &len
);
134 gtk_entry_append_text( GTK_ENTRY(m_widget
), text
);
138 bool wxTextCtrl::LoadFile( const wxString
&WXUNUSED(file
) )
140 wxFAIL_MSG("wxTextCtrl::LoadFile not implemented");
145 bool wxTextCtrl::SaveFile( const wxString
&WXUNUSED(file
) )
147 wxFAIL_MSG("wxTextCtrl::SaveFile not implemented");
152 bool wxTextCtrl::IsModified(void)
157 void wxTextCtrl::DiscardEdits(void)
163 wxString wxTextCtrl::GetLineText( long lineNo ) const
168 void wxTextCtrl::OnDropFiles( wxDropFilesEvent &event )
172 long wxTextCtrl::PositionToXY( long pos, long *x, long *y ) const
176 long wxTextCtrl::XYToPosition( long x, long y )
180 int wxTextCtrl::GetNumberOfLines(void)
185 void wxTextCtrl::SetInsertionPoint( long pos
)
188 if (m_windowStyle
& wxTE_MULTILINE
)
189 gtk_text_set_point( GTK_TEXT(m_widget
), tmp
);
191 gtk_entry_set_position( GTK_ENTRY(m_widget
), tmp
);
194 void wxTextCtrl::SetInsertionPointEnd(void)
197 if (m_windowStyle
& wxTE_MULTILINE
)
198 pos
= gtk_text_get_length( GTK_TEXT(m_widget
) );
200 pos
= GTK_ENTRY(m_widget
)->text_length
;
201 SetInsertionPoint( pos
-1 );
204 void wxTextCtrl::SetEditable( bool editable
)
206 if (m_windowStyle
& wxTE_MULTILINE
)
207 gtk_text_set_editable( GTK_TEXT(m_widget
), editable
);
209 gtk_entry_set_editable( GTK_ENTRY(m_widget
), editable
);
212 void wxTextCtrl::SetSelection( long from
, long to
)
214 gtk_editable_select_region( GTK_EDITABLE(m_widget
), (gint
)from
, (gint
)to
);
217 void wxTextCtrl::ShowPosition( long WXUNUSED(pos
) )
219 wxFAIL_MSG("wxTextCtrl::ShowPosition not implemented");
222 long wxTextCtrl::GetInsertionPoint(void) const
224 return (long) GTK_EDITABLE(m_widget
)->current_pos
;
227 long wxTextCtrl::GetLastPosition(void) const
230 if (m_windowStyle
& wxTE_MULTILINE
)
231 pos
= gtk_text_get_length( GTK_TEXT(m_widget
) );
233 pos
= GTK_ENTRY(m_widget
)->text_length
;
237 void wxTextCtrl::Remove( long from
, long to
)
239 gtk_editable_delete_text( GTK_EDITABLE(m_widget
), (gint
)from
, (gint
)to
);
242 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
244 gtk_editable_delete_text( GTK_EDITABLE(m_widget
), (gint
)from
, (gint
)to
);
245 if (value
.IsNull()) return;
247 gtk_editable_insert_text( GTK_EDITABLE(m_widget
), value
, value
.Length(), &pos
);
250 void wxTextCtrl::Cut(void)
252 gtk_editable_cut_clipboard( GTK_EDITABLE(m_widget
), 0 );
255 void wxTextCtrl::Copy(void)
257 gtk_editable_copy_clipboard( GTK_EDITABLE(m_widget
), 0 );
260 void wxTextCtrl::Paste(void)
262 gtk_editable_paste_clipboard( GTK_EDITABLE(m_widget
), 0 );
265 void wxTextCtrl::Delete(void)
270 void wxTextCtrl::OnChar( wxKeyEvent
&WXUNUSED(event
) )
274 int wxTextCtrl::overflow(int c
)
276 // Make sure there is a holding area
277 if ( allocate()==EOF
)
279 wxError("Streambuf allocation failed","Internal error");
283 // Verify that there are no characters in get area
284 if ( gptr() && gptr() < egptr() )
286 wxError("Who's trespassing my get area?","Internal error");
293 // Make sure there is a put area
296 /* This doesn't seem to be fatal so comment out error message */
297 // wxError("Put area not opened","Internal error");
298 setp( base(), base() );
301 // Determine how many characters have been inserted but no consumed
302 int plen
= pptr() - pbase();
304 // Now Jerry relies on the fact that the buffer is at least 2 chars
305 // long, but the holding area "may be as small as 1" ???
306 // And we need an additional \0, so let's keep this inefficient but
309 // If c!=EOF, it is a character that must also be comsumed
310 int xtra
= c
==EOF
? 0 : 1;
312 // Write temporary C-string to wxTextWindow
314 char *txt
= new char[plen
+xtra
+1];
315 memcpy(txt
, pbase(), plen
);
316 txt
[plen
] = (char)c
; // append c
317 txt
[plen
+xtra
] = '\0'; // append '\0' or overwrite c
318 // If the put area already contained \0, output will be truncated there
324 setp(pbase(), epptr());
326 #if defined(__WATCOMC__)
328 #elif defined(zapeof) // HP-UX (all cfront based?)
331 return c
!=EOF
? c
: 0; // this should make everybody happy
335 int len = pptr() - pbase();
336 char *txt = new char[len+1];
337 strncpy(txt, pbase(), len);
340 setp(pbase(), epptr());
346 int wxTextCtrl::sync(void)
348 // Verify that there are no characters in get area
349 if ( gptr() && gptr() < egptr() )
351 wxError("Who's trespassing my get area?","Internal error");
355 if ( pptr() && pptr() > pbase() ) return overflow(EOF
);
359 int len = pptr() - pbase();
360 char *txt = new char[len+1];
361 strncpy(txt, pbase(), len);
364 setp(pbase(), epptr());
370 int wxTextCtrl::underflow(void)
375 wxTextCtrl
& wxTextCtrl::operator<<(const wxString
& s
)
381 wxTextCtrl
& wxTextCtrl::operator<<(float f
)
383 static char buf
[100];
384 sprintf(buf
, "%.2f", f
);
389 wxTextCtrl
& wxTextCtrl::operator<<(double d
)
391 static char buf
[100];
392 sprintf(buf
, "%.2f", d
);
397 wxTextCtrl
& wxTextCtrl::operator<<(int i
)
399 static char buf
[100];
400 sprintf(buf
, "%i", i
);
405 wxTextCtrl
& wxTextCtrl::operator<<(long i
)
407 static char buf
[100];
408 sprintf(buf
, "%ld", i
);
413 wxTextCtrl
& wxTextCtrl::operator<<(const char c
)