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 bool bMultiLine
= (style
& wxTE_MULTILINE
) != 0;
64 // a multi-line edit control: create a vertical scrollbar by default and
65 // horizontal if requested
66 bool bHasHScrollbar
= (style
& wxHSCROLL
) != 0;
68 // create our control...
69 m_text
= gtk_text_new( NULL
, NULL
);
71 // ... and put into the upper left hand corner of the table
72 m_widget
= gtk_table_new(bHasHScrollbar
? 2 : 1, 2, FALSE
);
73 gtk_table_attach(GTK_TABLE(m_widget
), m_text
, 0, 1, 0, 1,
74 GTK_FILL
| GTK_EXPAND
,
75 GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
,
78 // put the horizontal scrollbar in the lower left hand corner
79 if ( bHasHScrollbar
) {
80 GtkWidget
*hscrollbar
= gtk_hscrollbar_new(GTK_TEXT(m_text
)->hadj
);
81 gtk_table_attach(GTK_TABLE(m_widget
), hscrollbar
, 0, 1, 1, 2,
82 GTK_EXPAND
| GTK_FILL
,
85 gtk_widget_show(hscrollbar
);
88 // finally, put the vertical scrollbar in the upper right corner
89 GtkWidget
*vscrollbar
= gtk_vscrollbar_new(GTK_TEXT(m_text
)->vadj
);
90 gtk_table_attach(GTK_TABLE(m_widget
), vscrollbar
, 1, 2, 0, 1,
92 GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
,
94 gtk_widget_show(vscrollbar
);
97 // a single-line text control: no need for scrollbars
99 m_text
= gtk_entry_new();
102 wxSize newSize
= size
;
103 if (newSize
.x
== -1) newSize
.x
= 80;
104 if (newSize
.y
== -1) newSize
.y
= 26;
105 SetSize( newSize
.x
, newSize
.y
);
110 gtk_widget_realize(m_text
);
111 gtk_widget_show(m_text
);
114 // we want to be notified about text changes
115 gtk_signal_connect(GTK_OBJECT(m_text
), "changed",
116 GTK_SIGNAL_FUNC(gtk_text_changed_callback
),
122 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &tmp
);
125 if (style
& wxREADONLY
)
131 gtk_text_set_editable( GTK_TEXT(m_text
), 1 );
139 wxString
wxTextCtrl::GetValue(void) const
142 if (m_windowStyle
& wxTE_MULTILINE
)
144 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
145 tmp
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
149 tmp
= gtk_entry_get_text( GTK_ENTRY(m_text
) );
154 void wxTextCtrl::SetValue( const wxString
&value
)
157 if (!value
.IsNull()) tmp
= value
;
158 if (m_windowStyle
& wxTE_MULTILINE
)
160 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
161 gtk_editable_delete_text( GTK_EDITABLE(m_text
), 0, len
);
163 gtk_editable_insert_text( GTK_EDITABLE(m_text
), tmp
, tmp
.Length(), &len
);
167 gtk_entry_set_text( GTK_ENTRY(m_text
), tmp
);
171 void wxTextCtrl::WriteText( const wxString
&text
)
173 if (text
.IsNull()) return;
175 if (m_windowStyle
& wxTE_MULTILINE
)
177 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
178 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
, text
.Length(), &len
);
182 gtk_entry_append_text( GTK_ENTRY(m_text
), text
);
186 bool wxTextCtrl::LoadFile( const wxString
&WXUNUSED(file
) )
188 wxFAIL_MSG("wxTextCtrl::LoadFile not implemented");
193 bool wxTextCtrl::SaveFile( const wxString
&WXUNUSED(file
) )
195 wxFAIL_MSG("wxTextCtrl::SaveFile not implemented");
201 wxString wxTextCtrl::GetLineText( long lineNo ) const
206 void wxTextCtrl::OnDropFiles( wxDropFilesEvent &event )
210 long wxTextCtrl::PositionToXY( long pos, long *x, long *y ) const
214 long wxTextCtrl::XYToPosition( long x, long y )
218 int wxTextCtrl::GetNumberOfLines(void)
223 void wxTextCtrl::SetInsertionPoint( long pos
)
226 if (m_windowStyle
& wxTE_MULTILINE
)
227 gtk_text_set_point( GTK_TEXT(m_text
), tmp
);
229 gtk_entry_set_position( GTK_ENTRY(m_text
), tmp
);
232 void wxTextCtrl::SetInsertionPointEnd(void)
235 if (m_windowStyle
& wxTE_MULTILINE
)
236 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
238 pos
= GTK_ENTRY(m_text
)->text_length
;
239 SetInsertionPoint( pos
-1 );
242 void wxTextCtrl::SetEditable( bool editable
)
244 if (m_windowStyle
& wxTE_MULTILINE
)
245 gtk_text_set_editable( GTK_TEXT(m_text
), editable
);
247 gtk_entry_set_editable( GTK_ENTRY(m_text
), editable
);
250 void wxTextCtrl::SetSelection( long from
, long to
)
252 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
255 void wxTextCtrl::ShowPosition( long WXUNUSED(pos
) )
257 wxFAIL_MSG("wxTextCtrl::ShowPosition not implemented");
260 long wxTextCtrl::GetInsertionPoint(void) const
262 return (long) GTK_EDITABLE(m_text
)->current_pos
;
265 long wxTextCtrl::GetLastPosition(void) const
268 if (m_windowStyle
& wxTE_MULTILINE
)
269 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
271 pos
= GTK_ENTRY(m_text
)->text_length
;
275 void wxTextCtrl::Remove( long from
, long to
)
277 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
280 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
282 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
283 if (value
.IsNull()) return;
285 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &pos
);
288 void wxTextCtrl::Cut(void)
290 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
), 0 );
293 void wxTextCtrl::Copy(void)
295 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
), 0 );
298 void wxTextCtrl::Paste(void)
300 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
), 0 );
303 void wxTextCtrl::Delete(void)
308 void wxTextCtrl::OnChar( wxKeyEvent
&WXUNUSED(event
) )
312 int wxTextCtrl::overflow( int WXUNUSED(c
) )
314 int len
= pptr() - pbase();
315 char *txt
= new char[len
+1];
316 strncpy(txt
, pbase(), len
);
319 setp(pbase(), epptr());
324 int wxTextCtrl::sync(void)
326 int len
= pptr() - pbase();
327 char *txt
= new char[len
+1];
328 strncpy(txt
, pbase(), len
);
331 setp(pbase(), epptr());
336 int wxTextCtrl::underflow(void)
341 wxTextCtrl
& wxTextCtrl::operator<<(const wxString
& s
)
347 wxTextCtrl
& wxTextCtrl::operator<<(float f
)
349 static char buf
[100];
350 sprintf(buf
, "%.2f", f
);
355 wxTextCtrl
& wxTextCtrl::operator<<(double d
)
357 static char buf
[100];
358 sprintf(buf
, "%.2f", d
);
363 wxTextCtrl
& wxTextCtrl::operator<<(int i
)
365 static char buf
[100];
366 sprintf(buf
, "%i", i
);
371 wxTextCtrl
& wxTextCtrl::operator<<(long i
)
373 static char buf
[100];
374 sprintf(buf
, "%ld", i
);
379 wxTextCtrl
& wxTextCtrl::operator<<(const char c
)
389 GtkWidget
* wxTextCtrl::GetDropTargetWidget(void)
391 return GTK_WIDGET(m_text
);