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 static void gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
27 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, win
->m_windowId
);
28 wxString
val( win
->GetValue() );
29 if (!val
.IsNull()) event
.m_commandString
= WXSTRINGCAST val
;
30 event
.SetEventObject( win
);
31 win
->GetEventHandler()->ProcessEvent( event
);
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
,wxControl
)
40 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
41 EVT_CHAR(wxTextCtrl::OnChar
)
44 wxTextCtrl::wxTextCtrl(void) : streambuf()
46 if (allocate()) setp(base(),ebuf());
51 wxTextCtrl::wxTextCtrl( wxWindow
*parent
, wxWindowID id
, const wxString
&value
,
52 const wxPoint
&pos
, const wxSize
&size
,
53 int style
, const wxValidator
& validator
, const wxString
&name
) : streambuf()
55 if (allocate()) setp(base(),ebuf());
58 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
61 bool wxTextCtrl::Create( wxWindow
*parent
, wxWindowID id
, const wxString
&value
,
62 const wxPoint
&pos
, const wxSize
&size
,
63 int style
, const wxValidator
& validator
, const wxString
&name
)
67 PreCreation( parent
, id
, pos
, size
, style
, name
);
69 SetValidator( validator
);
71 bool bMultiLine
= (style
& wxTE_MULTILINE
) != 0;
74 // a multi-line edit control: create a vertical scrollbar by default and
75 // horizontal if requested
76 bool bHasHScrollbar
= (style
& wxHSCROLL
) != 0;
78 // create our control...
79 m_text
= gtk_text_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
81 // ... and put into the upper left hand corner of the table
82 m_widget
= gtk_table_new(bHasHScrollbar
? 2 : 1, 2, FALSE
);
83 gtk_table_attach(GTK_TABLE(m_widget
), m_text
, 0, 1, 0, 1,
84 GTK_FILL
| GTK_EXPAND
,
85 GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
,
88 // put the horizontal scrollbar in the lower left hand corner
91 GtkWidget
*hscrollbar
= gtk_hscrollbar_new(GTK_TEXT(m_text
)->hadj
);
92 gtk_table_attach(GTK_TABLE(m_widget
), hscrollbar
, 0, 1, 1, 2,
93 GTK_EXPAND
| GTK_FILL
,
96 gtk_widget_show(hscrollbar
);
99 // finally, put the vertical scrollbar in the upper right corner
100 GtkWidget
*vscrollbar
= gtk_vscrollbar_new(GTK_TEXT(m_text
)->vadj
);
101 gtk_table_attach(GTK_TABLE(m_widget
), vscrollbar
, 1, 2, 0, 1,
103 GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
,
105 gtk_widget_show( vscrollbar
);
109 // a single-line text control: no need for scrollbars
111 m_text
= gtk_entry_new();
114 wxSize newSize
= size
;
115 if (newSize
.x
== -1) newSize
.x
= 80;
116 if (newSize
.y
== -1) newSize
.y
= 26;
117 SetSize( newSize
.x
, newSize
.y
);
123 gtk_widget_realize(m_text
);
124 gtk_widget_show(m_text
);
127 // we want to be notified about text changes
128 gtk_signal_connect(GTK_OBJECT(m_text
), "changed",
129 GTK_SIGNAL_FUNC(gtk_text_changed_callback
),
135 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &tmp
);
138 if (style
& wxTE_READONLY
)
144 gtk_text_set_editable( GTK_TEXT(m_text
), 1 );
152 wxString
wxTextCtrl::GetValue(void) const
155 if (m_windowStyle
& wxTE_MULTILINE
)
157 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
158 tmp
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
162 tmp
= gtk_entry_get_text( GTK_ENTRY(m_text
) );
167 void wxTextCtrl::SetValue( const wxString
&value
)
170 if (!value
.IsNull()) tmp
= value
;
171 if (m_windowStyle
& wxTE_MULTILINE
)
173 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
174 gtk_editable_delete_text( GTK_EDITABLE(m_text
), 0, len
);
176 gtk_editable_insert_text( GTK_EDITABLE(m_text
), tmp
, tmp
.Length(), &len
);
180 gtk_entry_set_text( GTK_ENTRY(m_text
), tmp
);
184 void wxTextCtrl::WriteText( const wxString
&text
)
186 if (text
.IsNull()) return;
188 if (m_windowStyle
& wxTE_MULTILINE
)
190 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
191 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
, text
.Length(), &len
);
195 gtk_entry_append_text( GTK_ENTRY(m_text
), text
);
199 bool wxTextCtrl::LoadFile( const wxString
&WXUNUSED(file
) )
201 wxFAIL_MSG( "wxTextCtrl::LoadFile not implemented" );
206 bool wxTextCtrl::SaveFile( const wxString
&WXUNUSED(file
) )
208 wxFAIL_MSG( "wxTextCtrl::SaveFile not implemented" );
214 wxString wxTextCtrl::GetLineText( long lineNo ) const
219 void wxTextCtrl::OnDropFiles( wxDropFilesEvent &event )
223 long wxTextCtrl::PositionToXY( long pos, long *x, long *y ) const
227 long wxTextCtrl::XYToPosition( long x, long y )
231 int wxTextCtrl::GetNumberOfLines(void)
236 void wxTextCtrl::SetInsertionPoint( long pos
)
239 if (m_windowStyle
& wxTE_MULTILINE
)
240 gtk_text_set_point( GTK_TEXT(m_text
), tmp
);
242 gtk_entry_set_position( GTK_ENTRY(m_text
), tmp
);
245 void wxTextCtrl::SetInsertionPointEnd(void)
248 if (m_windowStyle
& wxTE_MULTILINE
)
249 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
251 pos
= GTK_ENTRY(m_text
)->text_length
;
252 SetInsertionPoint( pos
-1 );
255 void wxTextCtrl::SetEditable( bool editable
)
257 if (m_windowStyle
& wxTE_MULTILINE
)
258 gtk_text_set_editable( GTK_TEXT(m_text
), editable
);
260 gtk_entry_set_editable( GTK_ENTRY(m_text
), editable
);
263 void wxTextCtrl::SetSelection( long from
, long to
)
265 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
268 void wxTextCtrl::ShowPosition( long WXUNUSED(pos
) )
270 wxFAIL_MSG(_("wxTextCtrl::ShowPosition not implemented"));
273 long wxTextCtrl::GetInsertionPoint(void) const
275 return (long) GTK_EDITABLE(m_text
)->current_pos
;
278 long wxTextCtrl::GetLastPosition(void) const
281 if (m_windowStyle
& wxTE_MULTILINE
)
282 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
284 pos
= GTK_ENTRY(m_text
)->text_length
;
288 void wxTextCtrl::Remove( long from
, long to
)
290 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
293 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
295 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
296 if (value
.IsNull()) return;
298 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &pos
);
301 void wxTextCtrl::Cut(void)
303 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
), 0 );
306 void wxTextCtrl::Copy(void)
308 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
), 0 );
311 void wxTextCtrl::Paste(void)
313 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
), 0 );
316 void wxTextCtrl::Delete(void)
321 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
323 if ((key_event
.KeyCode() == WXK_RETURN
) && (m_windowStyle
& wxPROCESS_ENTER
))
325 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
326 event
.SetEventObject(this);
327 printf( "Hallo.\n" );
328 if (GetEventHandler()->ProcessEvent(event
)) return;
330 else if (key_event
.KeyCode() == WXK_TAB
)
332 wxNavigationKeyEvent event
;
333 event
.SetDirection( key_event
.m_shiftDown
);
334 event
.SetWindowChange(FALSE
);
335 event
.SetEventObject(this);
337 if (GetEventHandler()->ProcessEvent(event
)) return;
342 int wxTextCtrl::overflow( int WXUNUSED(c
) )
344 int len
= pptr() - pbase();
345 char *txt
= new char[len
+1];
346 strncpy(txt
, pbase(), len
);
349 setp(pbase(), epptr());
354 int wxTextCtrl::sync(void)
356 int len
= pptr() - pbase();
357 char *txt
= new char[len
+1];
358 strncpy(txt
, pbase(), len
);
361 setp(pbase(), epptr());
366 int wxTextCtrl::underflow(void)
371 wxTextCtrl
& wxTextCtrl::operator<<(const wxString
& s
)
377 wxTextCtrl
& wxTextCtrl::operator<<(float f
)
379 static char buf
[100];
380 sprintf(buf
, "%.2f", f
);
385 wxTextCtrl
& wxTextCtrl::operator<<(double d
)
387 static char buf
[100];
388 sprintf(buf
, "%.2f", d
);
393 wxTextCtrl
& wxTextCtrl::operator<<(int i
)
395 static char buf
[100];
396 sprintf(buf
, "%i", i
);
401 wxTextCtrl
& wxTextCtrl::operator<<(long i
)
403 static char buf
[100];
404 sprintf(buf
, "%ld", i
);
409 wxTextCtrl
& wxTextCtrl::operator<<(const char c
)
419 GtkWidget
* wxTextCtrl::GetConnectWidget(void)
421 return GTK_WIDGET(m_text
);
424 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow
*window
)
426 if (m_windowStyle
& wxTE_MULTILINE
)
427 return (window
== GTK_TEXT(m_text
)->text_area
);
429 return (window
== GTK_ENTRY(m_text
)->text_area
);