1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "textctrl.h"
14 #include "wx/textctrl.h"
18 #include <sys/types.h>
22 //-----------------------------------------------------------------------------
24 //-----------------------------------------------------------------------------
26 static void gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
30 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, win
->m_windowId
);
31 wxString
val( win
->GetValue() );
32 if (!val
.IsNull()) event
.m_commandString
= WXSTRINGCAST val
;
33 event
.SetEventObject( win
);
34 win
->GetEventHandler()->ProcessEvent( event
);
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
41 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
,wxControl
)
43 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
44 EVT_CHAR(wxTextCtrl::OnChar
)
47 wxTextCtrl::wxTextCtrl() : streambuf()
49 if (allocate()) setp(base(),ebuf());
54 wxTextCtrl::wxTextCtrl( wxWindow
*parent
, wxWindowID id
, const wxString
&value
,
55 const wxPoint
&pos
, const wxSize
&size
,
56 int style
, const wxValidator
& validator
, const wxString
&name
) : streambuf()
58 if (allocate()) setp(base(),ebuf());
61 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
64 bool wxTextCtrl::Create( wxWindow
*parent
, wxWindowID id
, const wxString
&value
,
65 const wxPoint
&pos
, const wxSize
&size
,
66 int style
, const wxValidator
& validator
, const wxString
&name
)
70 PreCreation( parent
, id
, pos
, size
, style
, name
);
72 SetValidator( validator
);
74 bool bMultiLine
= (style
& wxTE_MULTILINE
) != 0;
77 // a multi-line edit control: create a vertical scrollbar by default and
78 // horizontal if requested
79 bool bHasHScrollbar
= (style
& wxHSCROLL
) != 0;
81 // create our control...
82 m_text
= gtk_text_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
84 // ... and put into the upper left hand corner of the table
85 m_widget
= gtk_table_new(bHasHScrollbar
? 2 : 1, 2, FALSE
);
86 gtk_table_attach(GTK_TABLE(m_widget
), m_text
, 0, 1, 0, 1,
87 GTK_FILL
| GTK_EXPAND
,
88 GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
,
91 // put the horizontal scrollbar in the lower left hand corner
94 GtkWidget
*hscrollbar
= gtk_hscrollbar_new(GTK_TEXT(m_text
)->hadj
);
95 gtk_table_attach(GTK_TABLE(m_widget
), hscrollbar
, 0, 1, 1, 2,
96 GTK_EXPAND
| GTK_FILL
,
99 gtk_widget_show(hscrollbar
);
102 // finally, put the vertical scrollbar in the upper right corner
103 GtkWidget
*vscrollbar
= gtk_vscrollbar_new(GTK_TEXT(m_text
)->vadj
);
104 gtk_table_attach(GTK_TABLE(m_widget
), vscrollbar
, 1, 2, 0, 1,
106 GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
,
108 gtk_widget_show( vscrollbar
);
112 // a single-line text control: no need for scrollbars
114 m_text
= gtk_entry_new();
117 wxSize newSize
= size
;
118 if (newSize
.x
== -1) newSize
.x
= 80;
119 if (newSize
.y
== -1) newSize
.y
= 26;
120 SetSize( newSize
.x
, newSize
.y
);
126 gtk_widget_realize(m_text
);
127 gtk_widget_show(m_text
);
130 // we want to be notified about text changes
131 gtk_signal_connect(GTK_OBJECT(m_text
), "changed",
132 GTK_SIGNAL_FUNC(gtk_text_changed_callback
),
138 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &tmp
);
141 if (style
& wxTE_READONLY
)
147 gtk_text_set_editable( GTK_TEXT(m_text
), 1 );
152 SetBackgroundColour( parent
->GetBackgroundColour() );
153 SetForegroundColour( parent
->GetForegroundColour() );
158 wxString
wxTextCtrl::GetValue() const
160 wxCHECK_MSG( m_text
!= NULL
, "", "invalid text ctrl" );
163 if (m_windowStyle
& wxTE_MULTILINE
)
165 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
166 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
172 tmp
= gtk_entry_get_text( GTK_ENTRY(m_text
) );
177 void wxTextCtrl::SetValue( const wxString
&value
)
179 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
182 if (!value
.IsNull()) tmp
= value
;
183 if (m_windowStyle
& wxTE_MULTILINE
)
185 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
186 gtk_editable_delete_text( GTK_EDITABLE(m_text
), 0, len
);
188 gtk_editable_insert_text( GTK_EDITABLE(m_text
), tmp
, tmp
.Length(), &len
);
192 gtk_entry_set_text( GTK_ENTRY(m_text
), tmp
);
196 void wxTextCtrl::WriteText( const wxString
&text
)
198 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
200 if (text
.IsNull()) return;
202 if (m_windowStyle
& wxTE_MULTILINE
)
204 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
205 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
, text
.Length(), &len
);
209 gtk_entry_append_text( GTK_ENTRY(m_text
), text
);
213 bool wxTextCtrl::LoadFile( const wxString
&file
)
215 wxCHECK_MSG( m_text
!= NULL
, FALSE
, "invalid text ctrl" );
217 if (!wxFileExists(file
)) return FALSE
;
224 if ((stat ((char*) (const char*) file
, &statb
) == -1) || (statb
.st_mode
& S_IFMT
) != S_IFREG
||
225 !(fp
= fopen ((char*) (const char*) file
, "r")))
231 gint len
= statb
.st_size
;
233 if (!(text
= (char*)malloc ((unsigned) (len
+ 1))))
238 if (fread (text
, sizeof (char), len
, fp
) != (size_t) len
)
245 if (m_windowStyle
& wxTE_MULTILINE
)
247 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
, 0, &len
);
251 gtk_entry_set_text( GTK_ENTRY(m_text
), text
);
261 bool wxTextCtrl::SaveFile( const wxString
&file
)
263 wxCHECK_MSG( m_text
!= NULL
, FALSE
, "invalid text ctrl" );
265 if (file
== "") return FALSE
;
269 if (!(fp
= fopen ((char*) (const char*) file
, "w")))
278 if (m_windowStyle
& wxTE_MULTILINE
)
280 len
= gtk_text_get_length( GTK_TEXT(m_text
) );
281 text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
285 text
= gtk_entry_get_text( GTK_ENTRY(m_text
) );
288 if (fwrite (text
, sizeof (char), len
, fp
) != (size_t) len
)
290 // Did not write whole file
293 // Make sure newline terminates the file
294 if (text
[len
- 1] != '\n')
299 if (m_windowStyle
& wxTE_MULTILINE
) g_free( text
);
308 wxString
wxTextCtrl::GetLineText( long lineNo
) const
310 if (m_windowStyle
& wxTE_MULTILINE
)
312 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
313 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
320 for (i
= 0; currentLine
!= lineNo
&& text
[i
]; i
++ )
325 for (j
= 0; text
[i
] && text
[i
] != '\n'; i
++, j
++ )
332 return wxEmptyString
;
336 if (lineNo
== 0) return GetValue();
337 return wxEmptyString
;
341 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
343 wxFAIL_MSG( "wxTextCtrl::OnDropFiles not implemented" );
346 long wxTextCtrl::PositionToXY( long WXUNUSED(pos
), long *WXUNUSED(x
), long *WXUNUSED(y
) ) const
348 wxFAIL_MSG( "wxTextCtrl::XYToPosition not implemented" );
353 long wxTextCtrl::XYToPosition( long WXUNUSED(x
), long WXUNUSED(y
) ) const
355 wxFAIL_MSG( "wxTextCtrl::XYToPosition not implemented" );
360 int wxTextCtrl::GetLineLength(long lineNo
) const
362 wxString str
= GetLineText (lineNo
);
363 return (int) str
.Length();
366 int wxTextCtrl::GetNumberOfLines() const
368 if (m_windowStyle
& wxTE_MULTILINE
)
370 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
371 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
376 for (int i
= 0; i
< len
; i
++ )
392 void wxTextCtrl::SetInsertionPoint( long pos
)
394 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
397 if (m_windowStyle
& wxTE_MULTILINE
)
398 gtk_text_set_point( GTK_TEXT(m_text
), tmp
);
400 gtk_entry_set_position( GTK_ENTRY(m_text
), tmp
);
403 void wxTextCtrl::SetInsertionPointEnd()
405 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
408 if (m_windowStyle
& wxTE_MULTILINE
)
409 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
411 pos
= GTK_ENTRY(m_text
)->text_length
;
412 SetInsertionPoint( pos
-1 );
415 void wxTextCtrl::SetEditable( bool editable
)
417 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
419 if (m_windowStyle
& wxTE_MULTILINE
)
420 gtk_text_set_editable( GTK_TEXT(m_text
), editable
);
422 gtk_entry_set_editable( GTK_ENTRY(m_text
), editable
);
425 void wxTextCtrl::SetSelection( long from
, long to
)
427 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
429 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
432 void wxTextCtrl::ShowPosition( long WXUNUSED(pos
) )
434 wxFAIL_MSG( "wxTextCtrl::ShowPosition not implemented" );
437 long wxTextCtrl::GetInsertionPoint() const
439 wxCHECK_MSG( m_text
!= NULL
, 0, "invalid text ctrl" );
441 return (long) GTK_EDITABLE(m_text
)->current_pos
;
444 long wxTextCtrl::GetLastPosition() const
446 wxCHECK_MSG( m_text
!= NULL
, 0, "invalid text ctrl" );
449 if (m_windowStyle
& wxTE_MULTILINE
)
450 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
452 pos
= GTK_ENTRY(m_text
)->text_length
;
456 void wxTextCtrl::Remove( long from
, long to
)
458 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
460 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
463 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
465 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
467 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
468 if (value
.IsNull()) return;
470 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &pos
);
473 void wxTextCtrl::Cut()
475 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
477 #if (GTK_MINOR_VERSION == 1)
478 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
) );
480 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
), 0 );
484 void wxTextCtrl::Copy()
486 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
488 #if (GTK_MINOR_VERSION == 1)
489 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
) );
491 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
), 0 );
495 void wxTextCtrl::Paste()
497 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
499 #if (GTK_MINOR_VERSION == 1)
500 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
) );
502 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
), 0 );
506 void wxTextCtrl::Clear()
511 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
513 if ((key_event
.KeyCode() == WXK_RETURN
) && (m_windowStyle
& wxPROCESS_ENTER
))
515 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
516 event
.SetEventObject(this);
517 if (GetEventHandler()->ProcessEvent(event
)) return;
519 else if (key_event
.KeyCode() == WXK_TAB
)
521 wxNavigationKeyEvent event
;
522 event
.SetDirection( key_event
.m_shiftDown
);
523 event
.SetWindowChange(FALSE
);
524 event
.SetEventObject(this);
526 if (GetEventHandler()->ProcessEvent(event
)) return;
531 int wxTextCtrl::overflow( int WXUNUSED(c
) )
533 int len
= pptr() - pbase();
534 char *txt
= new char[len
+1];
535 strncpy(txt
, pbase(), len
);
538 setp(pbase(), epptr());
543 int wxTextCtrl::sync()
545 int len
= pptr() - pbase();
546 char *txt
= new char[len
+1];
547 strncpy(txt
, pbase(), len
);
550 setp(pbase(), epptr());
555 int wxTextCtrl::underflow()
560 wxTextCtrl
& wxTextCtrl::operator<<(const wxString
& s
)
566 wxTextCtrl
& wxTextCtrl::operator<<(float f
)
568 static char buf
[100];
569 sprintf(buf
, "%.2f", f
);
574 wxTextCtrl
& wxTextCtrl::operator<<(double d
)
576 static char buf
[100];
577 sprintf(buf
, "%.2f", d
);
582 wxTextCtrl
& wxTextCtrl::operator<<(int i
)
584 static char buf
[100];
585 sprintf(buf
, "%i", i
);
590 wxTextCtrl
& wxTextCtrl::operator<<(long i
)
592 static char buf
[100];
593 sprintf(buf
, "%ld", i
);
598 wxTextCtrl
& wxTextCtrl::operator<<(const char c
)
608 GtkWidget
* wxTextCtrl::GetConnectWidget()
610 return GTK_WIDGET(m_text
);
613 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow
*window
)
615 if (m_windowStyle
& wxTE_MULTILINE
)
616 return (window
== GTK_TEXT(m_text
)->text_area
);
618 return (window
== GTK_ENTRY(m_text
)->text_area
);
621 void wxTextCtrl::SetFont( const wxFont
&WXUNUSED(font
) )
623 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
628 void wxTextCtrl::SetForegroundColour( const wxColour
&WXUNUSED(colour
) )
630 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
635 void wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
637 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
639 wxControl::SetBackgroundColour( colour
);
641 if (!m_backgroundColour
.Ok()) return;
643 if (m_windowStyle
& wxTE_MULTILINE
)
645 GdkWindow
*window
= GTK_TEXT(m_text
)->text_area
;
646 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
647 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
648 gdk_window_clear( window
);
652 void wxTextCtrl::ApplyWidgetStyle()
654 if (m_windowStyle
& wxTE_MULTILINE
)
660 gtk_widget_set_style( m_text
, m_widgetStyle
);