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"
19 //-----------------------------------------------------------------------------
21 //-----------------------------------------------------------------------------
23 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
,wxControl
)
25 void gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
31 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
32 // EVT_CHAR(wxTextCtrl::OnChar)
35 wxTextCtrl::wxTextCtrl(void) : streambuf()
43 wxTextCtrl::wxTextCtrl( wxWindow
*parent
, wxWindowID id
, const wxString
&value
,
44 const wxPoint
&pos
, const wxSize
&size
,
45 int style
, const wxString
&name
) : streambuf()
51 Create( parent
, id
, value
, pos
, size
, style
, name
);
54 bool wxTextCtrl::Create( wxWindow
*parent
, wxWindowID id
, const wxString
&value
,
55 const wxPoint
&pos
, const wxSize
&size
,
56 int style
, const wxString
&name
)
60 PreCreation( parent
, id
, pos
, size
, style
, name
);
62 bool bMultiLine
= (style
& wxTE_MULTILINE
) != 0;
65 // a multi-line edit control: create a vertical scrollbar by default and
66 // horizontal if requested
67 bool bHasHScrollbar
= (style
& wxHSCROLL
) != 0;
69 // create our control...
70 m_text
= gtk_text_new( NULL
, NULL
);
72 // ... and put into the upper left hand corner of the table
73 m_widget
= gtk_table_new(bHasHScrollbar
? 2 : 1, 2, FALSE
);
74 gtk_table_attach(GTK_TABLE(m_widget
), m_text
, 0, 1, 0, 1,
75 GTK_FILL
| GTK_EXPAND
,
76 GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
,
79 // put the horizontal scrollbar in the lower left hand corner
80 if ( bHasHScrollbar
) {
81 GtkWidget
*hscrollbar
= gtk_hscrollbar_new(GTK_TEXT(m_text
)->hadj
);
82 gtk_table_attach(GTK_TABLE(m_widget
), hscrollbar
, 0, 1, 1, 2,
83 GTK_EXPAND
| GTK_FILL
,
86 gtk_widget_show(hscrollbar
);
89 // finally, put the vertical scrollbar in the upper right corner
90 GtkWidget
*vscrollbar
= gtk_vscrollbar_new(GTK_TEXT(m_text
)->vadj
);
91 gtk_table_attach(GTK_TABLE(m_widget
), vscrollbar
, 1, 2, 0, 1,
93 GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
,
95 gtk_widget_show(vscrollbar
);
98 // a single-line text control: no need for scrollbars
100 m_text
= gtk_entry_new();
103 wxSize newSize
= size
;
104 if (newSize
.x
== -1) newSize
.x
= 80;
105 if (newSize
.y
== -1) newSize
.y
= 26;
106 SetSize( newSize
.x
, newSize
.y
);
111 gtk_widget_realize(m_text
);
112 gtk_widget_show(m_text
);
115 // we want to be notified about text changes
116 gtk_signal_connect(GTK_OBJECT(m_text
), "changed",
117 GTK_SIGNAL_FUNC(gtk_text_changed_callback
),
123 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &tmp
);
126 if (style
& wxTE_READONLY
)
132 gtk_text_set_editable( GTK_TEXT(m_text
), 1 );
140 wxString
wxTextCtrl::GetValue(void) const
143 if (m_windowStyle
& wxTE_MULTILINE
)
145 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
146 tmp
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
150 tmp
= gtk_entry_get_text( GTK_ENTRY(m_text
) );
155 void wxTextCtrl::SetValue( const wxString
&value
)
158 if (!value
.IsNull()) tmp
= value
;
159 if (m_windowStyle
& wxTE_MULTILINE
)
161 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
162 gtk_editable_delete_text( GTK_EDITABLE(m_text
), 0, len
);
164 gtk_editable_insert_text( GTK_EDITABLE(m_text
), tmp
, tmp
.Length(), &len
);
168 gtk_entry_set_text( GTK_ENTRY(m_text
), tmp
);
172 void wxTextCtrl::WriteText( const wxString
&text
)
174 if (text
.IsNull()) return;
176 if (m_windowStyle
& wxTE_MULTILINE
)
178 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
179 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
, text
.Length(), &len
);
183 gtk_entry_append_text( GTK_ENTRY(m_text
), text
);
187 bool wxTextCtrl::LoadFile( const wxString
&WXUNUSED(file
) )
189 wxFAIL_MSG(_("wxTextCtrl::LoadFile not implemented"));
194 bool wxTextCtrl::SaveFile( const wxString
&WXUNUSED(file
) )
196 wxFAIL_MSG(_("wxTextCtrl::SaveFile not implemented"));
202 wxString wxTextCtrl::GetLineText( long lineNo ) const
207 void wxTextCtrl::OnDropFiles( wxDropFilesEvent &event )
211 long wxTextCtrl::PositionToXY( long pos, long *x, long *y ) const
215 long wxTextCtrl::XYToPosition( long x, long y )
219 int wxTextCtrl::GetNumberOfLines(void)
224 void wxTextCtrl::SetInsertionPoint( long pos
)
227 if (m_windowStyle
& wxTE_MULTILINE
)
228 gtk_text_set_point( GTK_TEXT(m_text
), tmp
);
230 gtk_entry_set_position( GTK_ENTRY(m_text
), tmp
);
233 void wxTextCtrl::SetInsertionPointEnd(void)
236 if (m_windowStyle
& wxTE_MULTILINE
)
237 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
239 pos
= GTK_ENTRY(m_text
)->text_length
;
240 SetInsertionPoint( pos
-1 );
243 void wxTextCtrl::SetEditable( bool editable
)
245 if (m_windowStyle
& wxTE_MULTILINE
)
246 gtk_text_set_editable( GTK_TEXT(m_text
), editable
);
248 gtk_entry_set_editable( GTK_ENTRY(m_text
), editable
);
251 void wxTextCtrl::SetSelection( long from
, long to
)
253 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
256 void wxTextCtrl::ShowPosition( long WXUNUSED(pos
) )
258 wxFAIL_MSG(_("wxTextCtrl::ShowPosition not implemented"));
261 long wxTextCtrl::GetInsertionPoint(void) const
263 return (long) GTK_EDITABLE(m_text
)->current_pos
;
266 long wxTextCtrl::GetLastPosition(void) const
269 if (m_windowStyle
& wxTE_MULTILINE
)
270 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
272 pos
= GTK_ENTRY(m_text
)->text_length
;
276 void wxTextCtrl::Remove( long from
, long to
)
278 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
281 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
283 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
284 if (value
.IsNull()) return;
286 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &pos
);
289 void wxTextCtrl::Cut(void)
291 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
), 0 );
294 void wxTextCtrl::Copy(void)
296 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
), 0 );
299 void wxTextCtrl::Paste(void)
301 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
), 0 );
304 void wxTextCtrl::Delete(void)
309 void wxTextCtrl::OnChar( wxKeyEvent
&WXUNUSED(event
) )
313 int wxTextCtrl::overflow( int WXUNUSED(c
) )
315 int len
= pptr() - pbase();
316 char *txt
= new char[len
+1];
317 strncpy(txt
, pbase(), len
);
320 setp(pbase(), epptr());
325 int wxTextCtrl::sync(void)
327 int len
= pptr() - pbase();
328 char *txt
= new char[len
+1];
329 strncpy(txt
, pbase(), len
);
332 setp(pbase(), epptr());
337 int wxTextCtrl::underflow(void)
342 wxTextCtrl
& wxTextCtrl::operator<<(const wxString
& s
)
348 wxTextCtrl
& wxTextCtrl::operator<<(float f
)
350 static char buf
[100];
351 sprintf(buf
, "%.2f", f
);
356 wxTextCtrl
& wxTextCtrl::operator<<(double d
)
358 static char buf
[100];
359 sprintf(buf
, "%.2f", d
);
364 wxTextCtrl
& wxTextCtrl::operator<<(int i
)
366 static char buf
[100];
367 sprintf(buf
, "%i", i
);
372 wxTextCtrl
& wxTextCtrl::operator<<(long i
)
374 static char buf
[100];
375 sprintf(buf
, "%ld", i
);
380 wxTextCtrl
& wxTextCtrl::operator<<(const char c
)
390 GtkWidget
* wxTextCtrl::GetConnectWidget(void)
392 return GTK_WIDGET(m_text
);