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
)
30 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
31 // EVT_CHAR(wxTextCtrl::OnChar)
34 wxTextCtrl::wxTextCtrl(void) : streambuf()
42 wxTextCtrl::wxTextCtrl( wxWindow
*parent
, wxWindowID id
, const wxString
&value
,
43 const wxPoint
&pos
, const wxSize
&size
,
44 int style
, const wxString
&name
) : streambuf()
50 Create( parent
, id
, value
, pos
, size
, style
, name
);
53 bool wxTextCtrl::Create( wxWindow
*parent
, wxWindowID id
, const wxString
&value
,
54 const wxPoint
&pos
, const wxSize
&size
,
55 int style
, const wxString
&name
)
59 PreCreation( parent
, id
, pos
, size
, style
, name
);
61 if (style
& wxTE_MULTILINE
)
62 m_widget
= gtk_text_new( NULL
, NULL
);
64 m_widget
= gtk_entry_new();
66 wxSize newSize
= size
;
67 if (newSize
.x
== -1) newSize
.x
= 80;
68 if (newSize
.y
== -1) newSize
.y
= 26;
69 SetSize( newSize
.x
, newSize
.y
);
73 // we want to be notified about text changes
74 gtk_signal_connect(GTK_OBJECT(m_widget
), "changed",
75 GTK_SIGNAL_FUNC(gtk_text_changed_callback
),
81 gtk_editable_insert_text( GTK_EDITABLE(m_widget
), value
, value
.Length(), &tmp
);
84 if (style
& wxREADONLY
)
89 if (style
& wxTE_MULTILINE
) gtk_text_set_editable( GTK_TEXT(m_widget
), 1 );
97 wxString
wxTextCtrl::GetValue(void) const
100 if (m_windowStyle
& wxTE_MULTILINE
)
102 gint len
= gtk_text_get_length( GTK_TEXT(m_widget
) );
103 tmp
= gtk_editable_get_chars( GTK_EDITABLE(m_widget
), 0, len
);
107 tmp
= gtk_entry_get_text( GTK_ENTRY(m_widget
) );
112 void wxTextCtrl::SetValue( const wxString
&value
)
115 if (!value
.IsNull()) tmp
= value
;
116 if (m_windowStyle
& wxTE_MULTILINE
)
118 gint len
= gtk_text_get_length( GTK_TEXT(m_widget
) );
119 gtk_editable_delete_text( GTK_EDITABLE(m_widget
), 0, len
);
121 gtk_editable_insert_text( GTK_EDITABLE(m_widget
), tmp
, tmp
.Length(), &len
);
125 gtk_entry_set_text( GTK_ENTRY(m_widget
), tmp
);
129 void wxTextCtrl::WriteText( const wxString
&text
)
131 if (text
.IsNull()) return;
133 if (m_windowStyle
& wxTE_MULTILINE
)
135 gint len
= gtk_text_get_length( GTK_TEXT(m_widget
) );
136 gtk_editable_insert_text( GTK_EDITABLE(m_widget
), text
, text
.Length(), &len
);
140 gtk_entry_append_text( GTK_ENTRY(m_widget
), text
);
144 bool wxTextCtrl::LoadFile( const wxString
&WXUNUSED(file
) )
146 wxFAIL_MSG("wxTextCtrl::LoadFile not implemented");
151 bool wxTextCtrl::SaveFile( const wxString
&WXUNUSED(file
) )
153 wxFAIL_MSG("wxTextCtrl::SaveFile not implemented");
159 wxString wxTextCtrl::GetLineText( long lineNo ) const
164 void wxTextCtrl::OnDropFiles( wxDropFilesEvent &event )
168 long wxTextCtrl::PositionToXY( long pos, long *x, long *y ) const
172 long wxTextCtrl::XYToPosition( long x, long y )
176 int wxTextCtrl::GetNumberOfLines(void)
181 void wxTextCtrl::SetInsertionPoint( long pos
)
184 if (m_windowStyle
& wxTE_MULTILINE
)
185 gtk_text_set_point( GTK_TEXT(m_widget
), tmp
);
187 gtk_entry_set_position( GTK_ENTRY(m_widget
), tmp
);
190 void wxTextCtrl::SetInsertionPointEnd(void)
193 if (m_windowStyle
& wxTE_MULTILINE
)
194 pos
= gtk_text_get_length( GTK_TEXT(m_widget
) );
196 pos
= GTK_ENTRY(m_widget
)->text_length
;
197 SetInsertionPoint( pos
-1 );
200 void wxTextCtrl::SetEditable( bool editable
)
202 if (m_windowStyle
& wxTE_MULTILINE
)
203 gtk_text_set_editable( GTK_TEXT(m_widget
), editable
);
205 gtk_entry_set_editable( GTK_ENTRY(m_widget
), editable
);
208 void wxTextCtrl::SetSelection( long from
, long to
)
210 gtk_editable_select_region( GTK_EDITABLE(m_widget
), (gint
)from
, (gint
)to
);
213 void wxTextCtrl::ShowPosition( long WXUNUSED(pos
) )
215 wxFAIL_MSG("wxTextCtrl::ShowPosition not implemented");
218 long wxTextCtrl::GetInsertionPoint(void) const
220 return (long) GTK_EDITABLE(m_widget
)->current_pos
;
223 long wxTextCtrl::GetLastPosition(void) const
226 if (m_windowStyle
& wxTE_MULTILINE
)
227 pos
= gtk_text_get_length( GTK_TEXT(m_widget
) );
229 pos
= GTK_ENTRY(m_widget
)->text_length
;
233 void wxTextCtrl::Remove( long from
, long to
)
235 gtk_editable_delete_text( GTK_EDITABLE(m_widget
), (gint
)from
, (gint
)to
);
238 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
240 gtk_editable_delete_text( GTK_EDITABLE(m_widget
), (gint
)from
, (gint
)to
);
241 if (value
.IsNull()) return;
243 gtk_editable_insert_text( GTK_EDITABLE(m_widget
), value
, value
.Length(), &pos
);
246 void wxTextCtrl::Cut(void)
248 gtk_editable_cut_clipboard( GTK_EDITABLE(m_widget
), 0 );
251 void wxTextCtrl::Copy(void)
253 gtk_editable_copy_clipboard( GTK_EDITABLE(m_widget
), 0 );
256 void wxTextCtrl::Paste(void)
258 gtk_editable_paste_clipboard( GTK_EDITABLE(m_widget
), 0 );
261 void wxTextCtrl::Delete(void)
266 void wxTextCtrl::OnChar( wxKeyEvent
&WXUNUSED(event
) )
270 int wxTextCtrl::overflow(int c
)
272 int len
= pptr() - pbase();
273 char *txt
= new char[len
+1];
274 strncpy(txt
, pbase(), len
);
277 setp(pbase(), epptr());
282 int wxTextCtrl::sync(void)
284 int len
= pptr() - pbase();
285 char *txt
= new char[len
+1];
286 strncpy(txt
, pbase(), len
);
289 setp(pbase(), epptr());
294 int wxTextCtrl::underflow(void)
299 wxTextCtrl
& wxTextCtrl::operator<<(const wxString
& s
)
305 wxTextCtrl
& wxTextCtrl::operator<<(float f
)
307 static char buf
[100];
308 sprintf(buf
, "%.2f", f
);
313 wxTextCtrl
& wxTextCtrl::operator<<(double d
)
315 static char buf
[100];
316 sprintf(buf
, "%.2f", d
);
321 wxTextCtrl
& wxTextCtrl::operator<<(int i
)
323 static char buf
[100];
324 sprintf(buf
, "%i", i
);
329 wxTextCtrl
& wxTextCtrl::operator<<(long i
)
331 static char buf
[100];
332 sprintf(buf
, "%ld", i
);
337 wxTextCtrl
& wxTextCtrl::operator<<(const char c
)