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 static void gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
29 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, win
->m_windowId
);
30 wxString
val( win
->GetValue() );
31 if (!val
.IsNull()) event
.m_commandString
= WXSTRINGCAST val
;
32 event
.SetEventObject( win
);
33 win
->GetEventHandler()->ProcessEvent( event
);
36 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
37 EVT_CHAR(wxTextCtrl::OnChar
)
40 wxTextCtrl::wxTextCtrl(void) : streambuf()
42 if (allocate()) setp(base(),ebuf());
47 wxTextCtrl::wxTextCtrl( wxWindow
*parent
, wxWindowID id
, const wxString
&value
,
48 const wxPoint
&pos
, const wxSize
&size
,
49 int style
, const wxValidator
& validator
, const wxString
&name
) : streambuf()
51 if (allocate()) setp(base(),ebuf());
54 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
57 bool wxTextCtrl::Create( wxWindow
*parent
, wxWindowID id
, const wxString
&value
,
58 const wxPoint
&pos
, const wxSize
&size
,
59 int style
, const wxValidator
& validator
, const wxString
&name
)
63 PreCreation( parent
, id
, pos
, size
, style
, name
);
65 SetValidator( validator
);
67 bool bMultiLine
= (style
& wxTE_MULTILINE
) != 0;
70 // a multi-line edit control: create a vertical scrollbar by default and
71 // horizontal if requested
72 bool bHasHScrollbar
= (style
& wxHSCROLL
) != 0;
74 // create our control...
75 m_text
= gtk_text_new( NULL
, NULL
);
77 // ... and put into the upper left hand corner of the table
78 m_widget
= gtk_table_new(bHasHScrollbar
? 2 : 1, 2, FALSE
);
79 gtk_table_attach(GTK_TABLE(m_widget
), m_text
, 0, 1, 0, 1,
80 GTK_FILL
| GTK_EXPAND
,
81 GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
,
84 // put the horizontal scrollbar in the lower left hand corner
87 GtkWidget
*hscrollbar
= gtk_hscrollbar_new(GTK_TEXT(m_text
)->hadj
);
88 gtk_table_attach(GTK_TABLE(m_widget
), hscrollbar
, 0, 1, 1, 2,
89 GTK_EXPAND
| GTK_FILL
,
92 gtk_widget_show(hscrollbar
);
95 // finally, put the vertical scrollbar in the upper right corner
96 GtkWidget
*vscrollbar
= gtk_vscrollbar_new(GTK_TEXT(m_text
)->vadj
);
97 gtk_table_attach(GTK_TABLE(m_widget
), vscrollbar
, 1, 2, 0, 1,
99 GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
,
101 gtk_widget_show( vscrollbar
);
105 // a single-line text control: no need for scrollbars
107 m_text
= gtk_entry_new();
110 wxSize newSize
= size
;
111 if (newSize
.x
== -1) newSize
.x
= 80;
112 if (newSize
.y
== -1) newSize
.y
= 26;
113 SetSize( newSize
.x
, newSize
.y
);
119 gtk_widget_realize(m_text
);
120 gtk_widget_show(m_text
);
123 // we want to be notified about text changes
124 gtk_signal_connect(GTK_OBJECT(m_text
), "changed",
125 GTK_SIGNAL_FUNC(gtk_text_changed_callback
),
131 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &tmp
);
134 if (style
& wxTE_READONLY
)
140 gtk_text_set_editable( GTK_TEXT(m_text
), 1 );
148 wxString
wxTextCtrl::GetValue(void) const
151 if (m_windowStyle
& wxTE_MULTILINE
)
153 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
154 tmp
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
158 tmp
= gtk_entry_get_text( GTK_ENTRY(m_text
) );
163 void wxTextCtrl::SetValue( const wxString
&value
)
166 if (!value
.IsNull()) tmp
= value
;
167 if (m_windowStyle
& wxTE_MULTILINE
)
169 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
170 gtk_editable_delete_text( GTK_EDITABLE(m_text
), 0, len
);
172 gtk_editable_insert_text( GTK_EDITABLE(m_text
), tmp
, tmp
.Length(), &len
);
176 gtk_entry_set_text( GTK_ENTRY(m_text
), tmp
);
180 void wxTextCtrl::WriteText( const wxString
&text
)
182 if (text
.IsNull()) return;
184 if (m_windowStyle
& wxTE_MULTILINE
)
186 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
187 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
, text
.Length(), &len
);
191 gtk_entry_append_text( GTK_ENTRY(m_text
), text
);
195 bool wxTextCtrl::LoadFile( const wxString
&WXUNUSED(file
) )
197 wxFAIL_MSG( "wxTextCtrl::LoadFile not implemented" );
202 bool wxTextCtrl::SaveFile( const wxString
&WXUNUSED(file
) )
204 wxFAIL_MSG( "wxTextCtrl::SaveFile not implemented" );
210 wxString wxTextCtrl::GetLineText( long lineNo ) const
215 void wxTextCtrl::OnDropFiles( wxDropFilesEvent &event )
219 long wxTextCtrl::PositionToXY( long pos, long *x, long *y ) const
223 long wxTextCtrl::XYToPosition( long x, long y )
227 int wxTextCtrl::GetNumberOfLines(void)
232 void wxTextCtrl::SetInsertionPoint( long pos
)
235 if (m_windowStyle
& wxTE_MULTILINE
)
236 gtk_text_set_point( GTK_TEXT(m_text
), tmp
);
238 gtk_entry_set_position( GTK_ENTRY(m_text
), tmp
);
241 void wxTextCtrl::SetInsertionPointEnd(void)
244 if (m_windowStyle
& wxTE_MULTILINE
)
245 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
247 pos
= GTK_ENTRY(m_text
)->text_length
;
248 SetInsertionPoint( pos
-1 );
251 void wxTextCtrl::SetEditable( bool editable
)
253 if (m_windowStyle
& wxTE_MULTILINE
)
254 gtk_text_set_editable( GTK_TEXT(m_text
), editable
);
256 gtk_entry_set_editable( GTK_ENTRY(m_text
), editable
);
259 void wxTextCtrl::SetSelection( long from
, long to
)
261 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
264 void wxTextCtrl::ShowPosition( long WXUNUSED(pos
) )
266 wxFAIL_MSG(_("wxTextCtrl::ShowPosition not implemented"));
269 long wxTextCtrl::GetInsertionPoint(void) const
271 return (long) GTK_EDITABLE(m_text
)->current_pos
;
274 long wxTextCtrl::GetLastPosition(void) const
277 if (m_windowStyle
& wxTE_MULTILINE
)
278 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
280 pos
= GTK_ENTRY(m_text
)->text_length
;
284 void wxTextCtrl::Remove( long from
, long to
)
286 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
289 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
291 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
292 if (value
.IsNull()) return;
294 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &pos
);
297 void wxTextCtrl::Cut(void)
299 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
), 0 );
302 void wxTextCtrl::Copy(void)
304 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
), 0 );
307 void wxTextCtrl::Paste(void)
309 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
), 0 );
312 void wxTextCtrl::Delete(void)
317 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
319 if ((key_event
.KeyCode() == WXK_RETURN
) && (m_windowStyle
& wxPROCESS_ENTER
))
321 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
322 event
.SetEventObject(this);
323 printf( "Hallo.\n" );
324 if (GetEventHandler()->ProcessEvent(event
)) return;
326 else if (key_event
.KeyCode() == WXK_TAB
)
328 wxNavigationKeyEvent event
;
329 event
.SetDirection( key_event
.m_shiftDown
);
330 event
.SetWindowChange(FALSE
);
331 event
.SetEventObject(this);
333 if (GetEventHandler()->ProcessEvent(event
)) return;
338 int wxTextCtrl::overflow( int WXUNUSED(c
) )
340 int len
= pptr() - pbase();
341 char *txt
= new char[len
+1];
342 strncpy(txt
, pbase(), len
);
345 setp(pbase(), epptr());
350 int wxTextCtrl::sync(void)
352 int len
= pptr() - pbase();
353 char *txt
= new char[len
+1];
354 strncpy(txt
, pbase(), len
);
357 setp(pbase(), epptr());
362 int wxTextCtrl::underflow(void)
367 wxTextCtrl
& wxTextCtrl::operator<<(const wxString
& s
)
373 wxTextCtrl
& wxTextCtrl::operator<<(float f
)
375 static char buf
[100];
376 sprintf(buf
, "%.2f", f
);
381 wxTextCtrl
& wxTextCtrl::operator<<(double d
)
383 static char buf
[100];
384 sprintf(buf
, "%.2f", d
);
389 wxTextCtrl
& wxTextCtrl::operator<<(int i
)
391 static char buf
[100];
392 sprintf(buf
, "%i", i
);
397 wxTextCtrl
& wxTextCtrl::operator<<(long i
)
399 static char buf
[100];
400 sprintf(buf
, "%ld", i
);
405 wxTextCtrl
& wxTextCtrl::operator<<(const char c
)
415 GtkWidget
* wxTextCtrl::GetConnectWidget(void)
417 return GTK_WIDGET(m_text
);
420 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow
*window
)
422 if (m_windowStyle
& wxTE_MULTILINE
)
423 return (window
== GTK_TEXT(m_text
)->text_area
);
425 return (window
== GTK_ENTRY(m_text
)->text_area
);