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"
17 #include "wx/settings.h"
19 #include <sys/types.h>
25 #include "gdk/gdkkeysyms.h"
27 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
31 extern bool g_blockEventsOnDrag
;
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
38 gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
42 win
->CalculateScrollbar();
44 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, win
->m_windowId
);
45 event
.SetString( copystring(win
->GetValue()) );
46 event
.SetEventObject( win
);
47 win
->GetEventHandler()->ProcessEvent( event
);
48 delete[] event
.GetString();
51 //-----------------------------------------------------------------------------
53 //-----------------------------------------------------------------------------
56 gtk_text_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* WXUNUSED(alloc
), wxTextCtrl
*win
)
58 win
->CalculateScrollbar();
61 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
65 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
,wxControl
)
67 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
68 EVT_CHAR(wxTextCtrl::OnChar
)
71 #ifndef NO_TEXT_WINDOW_STREAM
72 wxTextCtrl::wxTextCtrl() : streambuf()
74 if (allocate()) setp(base(),ebuf());
79 wxTextCtrl::wxTextCtrl()
85 #ifndef NO_TEXT_WINDOW_STREAM
86 wxTextCtrl::wxTextCtrl( wxWindow
*parent
, wxWindowID id
, const wxString
&value
,
87 const wxPoint
&pos
, const wxSize
&size
,
88 int style
, const wxValidator
& validator
, const wxString
&name
) : streambuf()
90 if (allocate()) setp(base(),ebuf());
93 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
96 wxTextCtrl::wxTextCtrl( wxWindow
*parent
, wxWindowID id
, const wxString
&value
,
97 const wxPoint
&pos
, const wxSize
&size
,
98 int style
, const wxValidator
& validator
, const wxString
&name
)
101 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
105 bool wxTextCtrl::Create( wxWindow
*parent
, wxWindowID id
, const wxString
&value
,
106 const wxPoint
&pos
, const wxSize
&size
,
107 int style
, const wxValidator
& validator
, const wxString
&name
)
110 m_acceptsFocus
= TRUE
;
112 PreCreation( parent
, id
, pos
, size
, style
, name
);
114 SetValidator( validator
);
116 m_vScrollbarVisible
= TRUE
;
118 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
121 // a multi-line edit control: create a vertical scrollbar by default and
122 // horizontal if requested
123 bool bHasHScrollbar
= (style
& wxHSCROLL
) != 0;
125 // create our control...
126 m_text
= gtk_text_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
128 // ... and put into the upper left hand corner of the table
129 m_widget
= gtk_table_new(bHasHScrollbar
? 2 : 1, 2, FALSE
);
130 gtk_table_attach( GTK_TABLE(m_widget
), m_text
, 0, 1, 0, 1,
131 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
132 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
135 // put the horizontal scrollbar in the lower left hand corner
138 GtkWidget
*hscrollbar
= gtk_hscrollbar_new(GTK_TEXT(m_text
)->hadj
);
139 gtk_table_attach(GTK_TABLE(m_widget
), hscrollbar
, 0, 1, 1, 2,
140 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
),
143 gtk_widget_show(hscrollbar
);
146 // finally, put the vertical scrollbar in the upper right corner
147 m_vScrollbar
= gtk_vscrollbar_new( GTK_TEXT(m_text
)->vadj
);
148 gtk_table_attach(GTK_TABLE(m_widget
), m_vScrollbar
, 1, 2, 0, 1,
150 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
),
152 gtk_widget_show( m_vScrollbar
);
154 gtk_signal_connect( GTK_OBJECT(m_widget
), "size_allocate",
155 GTK_SIGNAL_FUNC(gtk_text_size_callback
), (gpointer
)this );
159 // a single-line text control: no need for scrollbars
161 m_text
= gtk_entry_new();
164 wxSize newSize
= size
;
165 if (newSize
.x
== -1) newSize
.x
= 80;
166 if (newSize
.y
== -1) newSize
.y
= 26;
167 SetSize( newSize
.x
, newSize
.y
);
169 m_parent
->AddChild( this );
171 (m_parent
->m_insertCallback
)( m_parent
, this );
177 gtk_widget_realize(m_text
);
178 gtk_widget_show(m_text
);
181 // we want to be notified about text changes
182 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
183 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
185 if (!value
.IsEmpty())
188 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &tmp
);
192 /* bring editable's cursor uptodate. bug in GTK. */
194 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
198 if (style
& wxTE_PASSWORD
)
201 gtk_entry_set_visibility( GTK_ENTRY(m_text
), FALSE
);
204 if (style
& wxTE_READONLY
)
207 gtk_entry_set_editable( GTK_ENTRY(m_text
), FALSE
);
212 gtk_text_set_editable( GTK_TEXT(m_text
), 1 );
217 SetBackgroundColour( parent
->GetBackgroundColour() );
218 SetForegroundColour( parent
->GetForegroundColour() );
223 void wxTextCtrl::CalculateScrollbar()
225 if ((m_windowStyle
& wxTE_MULTILINE
) == 0) return;
227 GtkAdjustment
*adj
= GTK_TEXT(m_text
)->vadj
;
229 if (adj
->upper
- adj
->page_size
< 0.8)
231 if (m_vScrollbarVisible
)
233 gtk_widget_hide( m_vScrollbar
);
235 m_vScrollbarVisible
= FALSE
;
240 if (!m_vScrollbarVisible
)
242 gtk_widget_show( m_vScrollbar
);
244 m_vScrollbarVisible
= TRUE
;
249 wxString
wxTextCtrl::GetValue() const
251 wxCHECK_MSG( m_text
!= NULL
, "", "invalid text ctrl" );
254 if (m_windowStyle
& wxTE_MULTILINE
)
256 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
257 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
263 tmp
= gtk_entry_get_text( GTK_ENTRY(m_text
) );
268 void wxTextCtrl::SetValue( const wxString
&value
)
270 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
273 if (!value
.IsNull()) tmp
= value
;
274 if (m_windowStyle
& wxTE_MULTILINE
)
276 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
277 gtk_editable_delete_text( GTK_EDITABLE(m_text
), 0, len
);
279 gtk_editable_insert_text( GTK_EDITABLE(m_text
), tmp
, tmp
.Length(), &len
);
283 gtk_entry_set_text( GTK_ENTRY(m_text
), tmp
);
287 void wxTextCtrl::WriteText( const wxString
&text
)
289 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
291 if (text
.IsNull()) return;
293 if (m_windowStyle
& wxTE_MULTILINE
)
295 /* this moves the cursor pos to behind the inserted text */
296 gint len
= GTK_EDITABLE(m_text
)->current_pos
;
297 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
, text
.Length(), &len
);
299 /* bring editable's cursor uptodate. bug in GTK. */
300 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
304 /* this moves the cursor pos to behind the inserted text */
305 gint len
= GTK_EDITABLE(m_text
)->current_pos
;
306 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
, text
.Length(), &len
);
308 /* bring editable's cursor uptodate. bug in GTK. */
309 GTK_EDITABLE(m_text
)->current_pos
+= text
.Len();
311 /* bring entry's cursor uptodate. bug in GTK. */
312 gtk_entry_set_position( GTK_ENTRY(m_text
), GTK_EDITABLE(m_text
)->current_pos
);
316 void wxTextCtrl::AppendText( const wxString
&text
)
318 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
320 if (m_windowStyle
& wxTE_MULTILINE
)
322 /* we'll insert at the last position */
323 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
324 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
, text
.Length(), &len
);
326 /* bring editable's cursor uptodate. bug in GTK. */
327 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
331 gtk_entry_append_text( GTK_ENTRY(m_text
), text
);
335 bool wxTextCtrl::LoadFile( const wxString
&file
)
337 wxCHECK_MSG( m_text
!= NULL
, FALSE
, "invalid text ctrl" );
339 if (!wxFileExists(file
)) return FALSE
;
343 FILE *fp
= (FILE*) NULL
;
346 if ((stat ((char*) (const char*) file
, &statb
) == -1) || (statb
.st_mode
& S_IFMT
) != S_IFREG
||
347 !(fp
= fopen ((char*) (const char*) file
, "r")))
353 gint len
= statb
.st_size
;
355 if (!(text
= (char*)malloc ((unsigned) (len
+ 1))))
360 if (fread (text
, sizeof (char), len
, fp
) != (size_t) len
)
367 if (m_windowStyle
& wxTE_MULTILINE
)
370 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
, len
, &pos
);
374 gtk_entry_set_text( GTK_ENTRY(m_text
), text
);
384 bool wxTextCtrl::SaveFile( const wxString
&file
)
386 wxCHECK_MSG( m_text
!= NULL
, FALSE
, "invalid text ctrl" );
388 if (file
== "") return FALSE
;
392 if (!(fp
= fopen ((char*) (const char*) file
, "w")))
398 char *text
= (char*) NULL
;
401 if (m_windowStyle
& wxTE_MULTILINE
)
403 len
= gtk_text_get_length( GTK_TEXT(m_text
) );
404 text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
408 text
= gtk_entry_get_text( GTK_ENTRY(m_text
) );
411 if (fwrite (text
, sizeof (char), len
, fp
) != (size_t) len
)
413 // Did not write whole file
416 // Make sure newline terminates the file
417 if (text
[len
- 1] != '\n')
422 if (m_windowStyle
& wxTE_MULTILINE
) g_free( text
);
431 wxString
wxTextCtrl::GetLineText( long lineNo
) const
433 if (m_windowStyle
& wxTE_MULTILINE
)
435 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
436 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
443 for (i
= 0; currentLine
!= lineNo
&& text
[i
]; i
++ )
448 for (j
= 0; text
[i
] && text
[i
] != '\n'; i
++, j
++ )
455 return wxEmptyString
;
459 if (lineNo
== 0) return GetValue();
460 return wxEmptyString
;
464 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
466 /* If you implement this, don't forget to update the documentation!
467 * (file docs/latex/wx/text.tex) */
468 wxFAIL_MSG( "wxTextCtrl::OnDropFiles not implemented" );
471 long wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
473 if ( m_windowStyle
& wxTE_MULTILINE
)
475 wxString text
= GetValue();
477 // cast to prevent warning. But pos really should've been unsigned.
478 if( (unsigned long)pos
> text
.Len() )
484 const char* stop
= text
.c_str() + pos
;
485 for ( const char *p
= text
.c_str(); p
< stop
; p
++ )
496 else // single line control
498 if ( pos
<= GTK_ENTRY(m_text
)->text_length
)
505 // index out of bounds
513 long wxTextCtrl::XYToPosition(long x
, long y
) const
515 if (!(m_windowStyle
& wxTE_MULTILINE
)) return 0;
518 for( int i
=0; i
<y
; i
++ ) pos
+= GetLineLength(i
) + 1; // one for '\n'
524 int wxTextCtrl::GetLineLength(long lineNo
) const
526 wxString str
= GetLineText (lineNo
);
527 return (int) str
.Length();
530 int wxTextCtrl::GetNumberOfLines() const
532 if (m_windowStyle
& wxTE_MULTILINE
)
534 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
535 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
540 for (int i
= 0; i
< len
; i
++ )
547 // currentLine is 0 based, add 1 to get number of lines
548 return currentLine
+ 1;
561 void wxTextCtrl::SetInsertionPoint( long pos
)
563 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
565 if (m_windowStyle
& wxTE_MULTILINE
)
567 /* seems to be broken in GTK 1.0.X:
568 gtk_text_set_point( GTK_TEXT(m_text), (int)pos ); */
570 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
571 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
573 /* we fake a set_point by inserting and deleting. as the user
574 isn't supposed to get to know about thos non-sense, we
575 disconnect so that no events are sent to the user program. */
577 gint tmp
= (gint
)pos
;
578 gtk_editable_insert_text( GTK_EDITABLE(m_text
), " ", 1, &tmp
);
579 gtk_editable_delete_text( GTK_EDITABLE(m_text
), tmp
-1, tmp
);
581 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
582 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
584 /* bring editable's cursor uptodate. another bug in GTK. */
586 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
590 gtk_entry_set_position( GTK_ENTRY(m_text
), (int)pos
);
592 /* bring editable's cursor uptodate. bug in GTK. */
594 GTK_EDITABLE(m_text
)->current_pos
= pos
;
598 void wxTextCtrl::SetInsertionPointEnd()
600 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
602 if (m_windowStyle
& wxTE_MULTILINE
)
603 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text
)));
605 gtk_entry_set_position( GTK_ENTRY(m_text
), -1 );
608 void wxTextCtrl::SetEditable( bool editable
)
610 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
612 if (m_windowStyle
& wxTE_MULTILINE
)
613 gtk_text_set_editable( GTK_TEXT(m_text
), editable
);
615 gtk_entry_set_editable( GTK_ENTRY(m_text
), editable
);
618 void wxTextCtrl::SetSelection( long from
, long to
)
620 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
622 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
625 void wxTextCtrl::ShowPosition( long WXUNUSED(pos
) )
627 wxFAIL_MSG( "wxTextCtrl::ShowPosition not implemented" );
630 long wxTextCtrl::GetInsertionPoint() const
632 wxCHECK_MSG( m_text
!= NULL
, 0, "invalid text ctrl" );
634 return (long) GTK_EDITABLE(m_text
)->current_pos
;
637 long wxTextCtrl::GetLastPosition() const
639 wxCHECK_MSG( m_text
!= NULL
, 0, "invalid text ctrl" );
642 if (m_windowStyle
& wxTE_MULTILINE
)
643 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
645 pos
= GTK_ENTRY(m_text
)->text_length
;
650 void wxTextCtrl::Remove( long from
, long to
)
652 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
654 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
657 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
659 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
661 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
662 if (value
.IsNull()) return;
663 gint pos
= (gint
)from
;
664 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &pos
);
667 void wxTextCtrl::Cut()
669 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
671 #if (GTK_MINOR_VERSION == 1)
672 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
) );
674 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
), 0 );
678 void wxTextCtrl::Copy()
680 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
682 #if (GTK_MINOR_VERSION == 1)
683 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
) );
685 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
), 0 );
689 void wxTextCtrl::Paste()
691 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
693 #if (GTK_MINOR_VERSION == 1)
694 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
) );
696 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
), 0 );
700 void wxTextCtrl::Clear()
705 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
707 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
709 if ((key_event
.KeyCode() == WXK_RETURN
) && (m_windowStyle
& wxPROCESS_ENTER
))
711 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
712 event
.SetEventObject(this);
713 if (GetEventHandler()->ProcessEvent(event
)) return;
719 #ifndef NO_TEXT_WINDOW_STREAM
720 int wxTextCtrl::overflow( int WXUNUSED(c
) )
722 int len
= pptr() - pbase();
723 char *txt
= new char[len
+1];
724 strncpy(txt
, pbase(), len
);
727 setp(pbase(), epptr());
732 int wxTextCtrl::sync()
734 int len
= pptr() - pbase();
735 char *txt
= new char[len
+1];
736 strncpy(txt
, pbase(), len
);
739 setp(pbase(), epptr());
744 int wxTextCtrl::underflow()
749 wxTextCtrl
& wxTextCtrl::operator<<(const wxString
& s
)
755 wxTextCtrl
& wxTextCtrl::operator<<(float f
)
757 static char buf
[100];
758 sprintf(buf
, "%.2f", f
);
763 wxTextCtrl
& wxTextCtrl::operator<<(double d
)
765 static char buf
[100];
766 sprintf(buf
, "%.2f", d
);
771 wxTextCtrl
& wxTextCtrl::operator<<(int i
)
773 static char buf
[100];
774 sprintf(buf
, "%i", i
);
779 wxTextCtrl
& wxTextCtrl::operator<<(long i
)
781 static char buf
[100];
782 sprintf(buf
, "%ld", i
);
787 wxTextCtrl
& wxTextCtrl::operator<<(const char c
)
798 GtkWidget
* wxTextCtrl::GetConnectWidget()
800 return GTK_WIDGET(m_text
);
803 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow
*window
)
805 if (m_windowStyle
& wxTE_MULTILINE
)
806 return (window
== GTK_TEXT(m_text
)->text_area
);
808 return (window
== GTK_ENTRY(m_text
)->text_area
);
811 void wxTextCtrl::SetFont( const wxFont
&WXUNUSED(font
) )
813 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
818 void wxTextCtrl::SetForegroundColour( const wxColour
&WXUNUSED(colour
) )
820 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
825 void wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
827 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
829 wxControl::SetBackgroundColour( colour
);
831 wxColour sysbg
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE
);
832 if (sysbg
.Red() == colour
.Red() &&
833 sysbg
.Green() == colour
.Green() &&
834 sysbg
.Blue() == colour
.Blue())
839 if (!m_backgroundColour
.Ok()) return;
841 if (m_windowStyle
& wxTE_MULTILINE
)
843 GdkWindow
*window
= GTK_TEXT(m_text
)->text_area
;
844 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
845 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
846 gdk_window_clear( window
);
850 void wxTextCtrl::ApplyWidgetStyle()
852 if (m_windowStyle
& wxTE_MULTILINE
)
859 gtk_widget_set_style( m_text
, m_widgetStyle
);