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()
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");
158 bool wxTextCtrl::IsModified(void)
163 void wxTextCtrl::DiscardEdits(void)
169 wxString wxTextCtrl::GetLineText( long lineNo ) const
174 void wxTextCtrl::OnDropFiles( wxDropFilesEvent &event )
178 long wxTextCtrl::PositionToXY( long pos, long *x, long *y ) const
182 long wxTextCtrl::XYToPosition( long x, long y )
186 int wxTextCtrl::GetNumberOfLines(void)
191 void wxTextCtrl::SetInsertionPoint( long pos
)
194 if (m_windowStyle
& wxTE_MULTILINE
)
195 gtk_text_set_point( GTK_TEXT(m_widget
), tmp
);
197 gtk_entry_set_position( GTK_ENTRY(m_widget
), tmp
);
200 void wxTextCtrl::SetInsertionPointEnd(void)
203 if (m_windowStyle
& wxTE_MULTILINE
)
204 pos
= gtk_text_get_length( GTK_TEXT(m_widget
) );
206 pos
= GTK_ENTRY(m_widget
)->text_length
;
207 SetInsertionPoint( pos
-1 );
210 void wxTextCtrl::SetEditable( bool editable
)
212 if (m_windowStyle
& wxTE_MULTILINE
)
213 gtk_text_set_editable( GTK_TEXT(m_widget
), editable
);
215 gtk_entry_set_editable( GTK_ENTRY(m_widget
), editable
);
218 void wxTextCtrl::SetSelection( long from
, long to
)
220 gtk_editable_select_region( GTK_EDITABLE(m_widget
), (gint
)from
, (gint
)to
);
223 void wxTextCtrl::ShowPosition( long WXUNUSED(pos
) )
225 wxFAIL_MSG("wxTextCtrl::ShowPosition not implemented");
228 long wxTextCtrl::GetInsertionPoint(void) const
230 return (long) GTK_EDITABLE(m_widget
)->current_pos
;
233 long wxTextCtrl::GetLastPosition(void) const
236 if (m_windowStyle
& wxTE_MULTILINE
)
237 pos
= gtk_text_get_length( GTK_TEXT(m_widget
) );
239 pos
= GTK_ENTRY(m_widget
)->text_length
;
243 void wxTextCtrl::Remove( long from
, long to
)
245 gtk_editable_delete_text( GTK_EDITABLE(m_widget
), (gint
)from
, (gint
)to
);
248 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
250 gtk_editable_delete_text( GTK_EDITABLE(m_widget
), (gint
)from
, (gint
)to
);
251 if (value
.IsNull()) return;
253 gtk_editable_insert_text( GTK_EDITABLE(m_widget
), value
, value
.Length(), &pos
);
256 void wxTextCtrl::Cut(void)
258 gtk_editable_cut_clipboard( GTK_EDITABLE(m_widget
), 0 );
261 void wxTextCtrl::Copy(void)
263 gtk_editable_copy_clipboard( GTK_EDITABLE(m_widget
), 0 );
266 void wxTextCtrl::Paste(void)
268 gtk_editable_paste_clipboard( GTK_EDITABLE(m_widget
), 0 );
271 void wxTextCtrl::Delete(void)
276 void wxTextCtrl::OnChar( wxKeyEvent
&WXUNUSED(event
) )
280 int wxTextCtrl::overflow(int c
)
282 int len
= pptr() - pbase();
283 char *txt
= new char[len
+1];
284 strncpy(txt
, pbase(), len
);
287 setp(pbase(), epptr());
292 int wxTextCtrl::sync(void)
294 int len
= pptr() - pbase();
295 char *txt
= new char[len
+1];
296 strncpy(txt
, pbase(), len
);
299 setp(pbase(), epptr());
304 int wxTextCtrl::underflow(void)
309 wxTextCtrl
& wxTextCtrl::operator<<(const wxString
& s
)
315 wxTextCtrl
& wxTextCtrl::operator<<(float f
)
317 static char buf
[100];
318 sprintf(buf
, "%.2f", f
);
323 wxTextCtrl
& wxTextCtrl::operator<<(double d
)
325 static char buf
[100];
326 sprintf(buf
, "%.2f", d
);
331 wxTextCtrl
& wxTextCtrl::operator<<(int i
)
333 static char buf
[100];
334 sprintf(buf
, "%i", i
);
339 wxTextCtrl
& wxTextCtrl::operator<<(long i
)
341 static char buf
[100];
342 sprintf(buf
, "%ld", i
);
347 wxTextCtrl
& wxTextCtrl::operator<<(const char c
)