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;
63 // a multi-line edit control: create a vertical scrollbar by default and
64 // horizontal if requested
65 bool bHasHScrollbar
= (style
& wxHSCROLL
) != 0;
67 // create our control...
68 m_text
= gtk_text_new( NULL
, NULL
);
70 // ... and put into the upper left hand corner of the table
71 m_widget
= gtk_table_new(bHasHScrollbar
? 2 : 1, 2, FALSE
);
72 gtk_table_attach(GTK_TABLE(m_widget
), m_text
, 0, 1, 0, 1,
73 GTK_FILL
| GTK_EXPAND
,
74 GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
,
77 // put the horizontal scrollbar in the lower left hand corner
78 if ( bHasHScrollbar
) {
79 GtkWidget
*hscrollbar
= gtk_hscrollbar_new(GTK_TEXT(m_text
)->hadj
);
80 gtk_table_attach(GTK_TABLE(m_widget
), hscrollbar
, 0, 1, 1, 2,
81 GTK_EXPAND
| GTK_FILL
,
84 gtk_widget_show(hscrollbar
);
87 // finally, put the vertical scrollbar in the upper right corner
88 GtkWidget
*vscrollbar
= gtk_vscrollbar_new(GTK_TEXT(m_text
)->vadj
);
89 gtk_table_attach(GTK_TABLE(m_widget
), vscrollbar
, 1, 2, 0, 1,
91 GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
,
93 gtk_widget_show(vscrollbar
);
96 // a single-line text control: no need for scrollbars
98 m_text
= gtk_entry_new();
101 wxSize newSize
= size
;
102 if (newSize
.x
== -1) newSize
.x
= 80;
103 if (newSize
.y
== -1) newSize
.y
= 26;
104 SetSize( newSize
.x
, newSize
.y
);
109 gtk_widget_realize(m_text
);
110 gtk_widget_show(m_text
);
113 // we want to be notified about text changes
114 gtk_signal_connect(GTK_OBJECT(m_text
), "changed",
115 GTK_SIGNAL_FUNC(gtk_text_changed_callback
),
121 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &tmp
);
124 if (style
& wxREADONLY
)
130 gtk_text_set_editable( GTK_TEXT(m_text
), 1 );
138 wxString
wxTextCtrl::GetValue(void) const
141 if (m_windowStyle
& wxTE_MULTILINE
)
143 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
144 tmp
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
148 tmp
= gtk_entry_get_text( GTK_ENTRY(m_text
) );
153 void wxTextCtrl::SetValue( const wxString
&value
)
156 if (!value
.IsNull()) tmp
= value
;
157 if (m_windowStyle
& wxTE_MULTILINE
)
159 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
160 gtk_editable_delete_text( GTK_EDITABLE(m_text
), 0, len
);
162 gtk_editable_insert_text( GTK_EDITABLE(m_text
), tmp
, tmp
.Length(), &len
);
166 gtk_entry_set_text( GTK_ENTRY(m_text
), tmp
);
170 void wxTextCtrl::WriteText( const wxString
&text
)
172 if (text
.IsNull()) return;
174 if (m_windowStyle
& wxTE_MULTILINE
)
176 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
177 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
, text
.Length(), &len
);
181 gtk_entry_append_text( GTK_ENTRY(m_text
), text
);
185 bool wxTextCtrl::LoadFile( const wxString
&WXUNUSED(file
) )
187 wxFAIL_MSG("wxTextCtrl::LoadFile not implemented");
192 bool wxTextCtrl::SaveFile( const wxString
&WXUNUSED(file
) )
194 wxFAIL_MSG("wxTextCtrl::SaveFile not implemented");
200 wxString wxTextCtrl::GetLineText( long lineNo ) const
205 void wxTextCtrl::OnDropFiles( wxDropFilesEvent &event )
209 long wxTextCtrl::PositionToXY( long pos, long *x, long *y ) const
213 long wxTextCtrl::XYToPosition( long x, long y )
217 int wxTextCtrl::GetNumberOfLines(void)
222 void wxTextCtrl::SetInsertionPoint( long pos
)
225 if (m_windowStyle
& wxTE_MULTILINE
)
226 gtk_text_set_point( GTK_TEXT(m_text
), tmp
);
228 gtk_entry_set_position( GTK_ENTRY(m_text
), tmp
);
231 void wxTextCtrl::SetInsertionPointEnd(void)
234 if (m_windowStyle
& wxTE_MULTILINE
)
235 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
237 pos
= GTK_ENTRY(m_text
)->text_length
;
238 SetInsertionPoint( pos
-1 );
241 void wxTextCtrl::SetEditable( bool editable
)
243 if (m_windowStyle
& wxTE_MULTILINE
)
244 gtk_text_set_editable( GTK_TEXT(m_text
), editable
);
246 gtk_entry_set_editable( GTK_ENTRY(m_text
), editable
);
249 void wxTextCtrl::SetSelection( long from
, long to
)
251 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
254 void wxTextCtrl::ShowPosition( long WXUNUSED(pos
) )
256 wxFAIL_MSG("wxTextCtrl::ShowPosition not implemented");
259 long wxTextCtrl::GetInsertionPoint(void) const
261 return (long) GTK_EDITABLE(m_text
)->current_pos
;
264 long wxTextCtrl::GetLastPosition(void) const
267 if (m_windowStyle
& wxTE_MULTILINE
)
268 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
270 pos
= GTK_ENTRY(m_text
)->text_length
;
274 void wxTextCtrl::Remove( long from
, long to
)
276 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
279 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
281 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
282 if (value
.IsNull()) return;
284 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &pos
);
287 void wxTextCtrl::Cut(void)
289 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
), 0 );
292 void wxTextCtrl::Copy(void)
294 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
), 0 );
297 void wxTextCtrl::Paste(void)
299 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
), 0 );
302 void wxTextCtrl::Delete(void)
307 void wxTextCtrl::OnChar( wxKeyEvent
&WXUNUSED(event
) )
311 int wxTextCtrl::overflow( int WXUNUSED(c
) )
313 int len
= pptr() - pbase();
314 char *txt
= new char[len
+1];
315 strncpy(txt
, pbase(), len
);
318 setp(pbase(), epptr());
323 int wxTextCtrl::sync(void)
325 int len
= pptr() - pbase();
326 char *txt
= new char[len
+1];
327 strncpy(txt
, pbase(), len
);
330 setp(pbase(), epptr());
335 int wxTextCtrl::underflow(void)
340 wxTextCtrl
& wxTextCtrl::operator<<(const wxString
& s
)
346 wxTextCtrl
& wxTextCtrl::operator<<(float f
)
348 static char buf
[100];
349 sprintf(buf
, "%.2f", f
);
354 wxTextCtrl
& wxTextCtrl::operator<<(double d
)
356 static char buf
[100];
357 sprintf(buf
, "%.2f", d
);
362 wxTextCtrl
& wxTextCtrl::operator<<(int i
)
364 static char buf
[100];
365 sprintf(buf
, "%i", i
);
370 wxTextCtrl
& wxTextCtrl::operator<<(long i
)
372 static char buf
[100];
373 sprintf(buf
, "%ld", i
);
378 wxTextCtrl
& wxTextCtrl::operator<<(const char c
)
388 GtkWidget
* wxTextCtrl::GetDropTargetWidget(void)
390 return GTK_WIDGET(m_text
);