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 wxString
val( win
->GetValue() );
46 if (!val
.IsNull()) event
.m_commandString
= WXSTRINGCAST val
;
47 event
.SetEventObject( win
);
48 win
->GetEventHandler()->ProcessEvent( event
);
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 wxTextCtrl::wxTextCtrl() : streambuf()
73 if (allocate()) setp(base(),ebuf());
78 wxTextCtrl::wxTextCtrl( wxWindow
*parent
, wxWindowID id
, const wxString
&value
,
79 const wxPoint
&pos
, const wxSize
&size
,
80 int style
, const wxValidator
& validator
, const wxString
&name
) : streambuf()
82 if (allocate()) setp(base(),ebuf());
85 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
88 bool wxTextCtrl::Create( wxWindow
*parent
, wxWindowID id
, const wxString
&value
,
89 const wxPoint
&pos
, const wxSize
&size
,
90 int style
, const wxValidator
& validator
, const wxString
&name
)
93 m_acceptsFocus
= TRUE
;
95 PreCreation( parent
, id
, pos
, size
, style
, name
);
97 SetValidator( validator
);
99 m_vScrollbarVisible
= TRUE
;
101 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
104 // a multi-line edit control: create a vertical scrollbar by default and
105 // horizontal if requested
106 bool bHasHScrollbar
= (style
& wxHSCROLL
) != 0;
108 // create our control...
109 m_text
= gtk_text_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
111 // ... and put into the upper left hand corner of the table
112 m_widget
= gtk_table_new(bHasHScrollbar
? 2 : 1, 2, FALSE
);
113 gtk_table_attach( GTK_TABLE(m_widget
), m_text
, 0, 1, 0, 1,
114 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
115 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
118 // put the horizontal scrollbar in the lower left hand corner
121 GtkWidget
*hscrollbar
= gtk_hscrollbar_new(GTK_TEXT(m_text
)->hadj
);
122 gtk_table_attach(GTK_TABLE(m_widget
), hscrollbar
, 0, 1, 1, 2,
123 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
),
126 gtk_widget_show(hscrollbar
);
129 // finally, put the vertical scrollbar in the upper right corner
130 m_vScrollbar
= gtk_vscrollbar_new( GTK_TEXT(m_text
)->vadj
);
131 gtk_table_attach(GTK_TABLE(m_widget
), m_vScrollbar
, 1, 2, 0, 1,
133 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
),
135 gtk_widget_show( m_vScrollbar
);
137 gtk_signal_connect( GTK_OBJECT(m_widget
), "size_allocate",
138 GTK_SIGNAL_FUNC(gtk_text_size_callback
), (gpointer
)this );
142 // a single-line text control: no need for scrollbars
144 m_text
= gtk_entry_new();
147 wxSize newSize
= size
;
148 if (newSize
.x
== -1) newSize
.x
= 80;
149 if (newSize
.y
== -1) newSize
.y
= 26;
150 SetSize( newSize
.x
, newSize
.y
);
152 m_parent
->AddChild( this );
154 (m_parent
->m_insertCallback
)( m_parent
, this );
160 gtk_widget_realize(m_text
);
161 gtk_widget_show(m_text
);
164 // we want to be notified about text changes
165 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
166 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
171 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &tmp
);
172 SetInsertionPointEnd();
175 if (style
& wxTE_PASSWORD
)
178 gtk_entry_set_visibility( GTK_ENTRY(m_text
), FALSE
);
181 if (style
& wxTE_READONLY
)
184 gtk_entry_set_editable( GTK_ENTRY(m_text
), FALSE
);
189 gtk_text_set_editable( GTK_TEXT(m_text
), 1 );
194 SetBackgroundColour( parent
->GetBackgroundColour() );
195 SetForegroundColour( parent
->GetForegroundColour() );
200 void wxTextCtrl::CalculateScrollbar()
202 if ((m_windowStyle
& wxTE_MULTILINE
) == 0) return;
204 GtkAdjustment
*adj
= GTK_TEXT(m_text
)->vadj
;
206 if (adj
->upper
- adj
->page_size
< 0.8)
208 if (m_vScrollbarVisible
)
210 gtk_widget_hide( m_vScrollbar
);
212 m_vScrollbarVisible
= FALSE
;
217 if (!m_vScrollbarVisible
)
219 gtk_widget_show( m_vScrollbar
);
221 m_vScrollbarVisible
= TRUE
;
226 wxString
wxTextCtrl::GetValue() const
228 wxCHECK_MSG( m_text
!= NULL
, "", "invalid text ctrl" );
231 if (m_windowStyle
& wxTE_MULTILINE
)
233 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
234 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
240 tmp
= gtk_entry_get_text( GTK_ENTRY(m_text
) );
245 void wxTextCtrl::SetValue( const wxString
&value
)
247 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
250 if (!value
.IsNull()) tmp
= value
;
251 if (m_windowStyle
& wxTE_MULTILINE
)
253 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
254 gtk_editable_delete_text( GTK_EDITABLE(m_text
), 0, len
);
256 gtk_editable_insert_text( GTK_EDITABLE(m_text
), tmp
, tmp
.Length(), &len
);
260 gtk_entry_set_text( GTK_ENTRY(m_text
), tmp
);
264 void wxTextCtrl::WriteText( const wxString
&text
)
266 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
268 if (text
.IsNull()) return;
270 if (m_windowStyle
& wxTE_MULTILINE
)
272 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
273 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
, text
.Length(), &len
);
277 gtk_entry_append_text( GTK_ENTRY(m_text
), text
);
281 bool wxTextCtrl::LoadFile( const wxString
&file
)
283 wxCHECK_MSG( m_text
!= NULL
, FALSE
, "invalid text ctrl" );
285 if (!wxFileExists(file
)) return FALSE
;
292 if ((stat ((char*) (const char*) file
, &statb
) == -1) || (statb
.st_mode
& S_IFMT
) != S_IFREG
||
293 !(fp
= fopen ((char*) (const char*) file
, "r")))
299 gint len
= statb
.st_size
;
301 if (!(text
= (char*)malloc ((unsigned) (len
+ 1))))
306 if (fread (text
, sizeof (char), len
, fp
) != (size_t) len
)
313 if (m_windowStyle
& wxTE_MULTILINE
)
316 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
, len
, &pos
);
320 gtk_entry_set_text( GTK_ENTRY(m_text
), text
);
330 bool wxTextCtrl::SaveFile( const wxString
&file
)
332 wxCHECK_MSG( m_text
!= NULL
, FALSE
, "invalid text ctrl" );
334 if (file
== "") return FALSE
;
338 if (!(fp
= fopen ((char*) (const char*) file
, "w")))
347 if (m_windowStyle
& wxTE_MULTILINE
)
349 len
= gtk_text_get_length( GTK_TEXT(m_text
) );
350 text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
354 text
= gtk_entry_get_text( GTK_ENTRY(m_text
) );
357 if (fwrite (text
, sizeof (char), len
, fp
) != (size_t) len
)
359 // Did not write whole file
362 // Make sure newline terminates the file
363 if (text
[len
- 1] != '\n')
368 if (m_windowStyle
& wxTE_MULTILINE
) g_free( text
);
377 wxString
wxTextCtrl::GetLineText( long lineNo
) const
379 if (m_windowStyle
& wxTE_MULTILINE
)
381 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
382 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
389 for (i
= 0; currentLine
!= lineNo
&& text
[i
]; i
++ )
394 for (j
= 0; text
[i
] && text
[i
] != '\n'; i
++, j
++ )
401 return wxEmptyString
;
405 if (lineNo
== 0) return GetValue();
406 return wxEmptyString
;
410 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
412 wxFAIL_MSG( "wxTextCtrl::OnDropFiles not implemented" );
415 long wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
417 if (!(m_windowStyle
& wxTE_MULTILINE
))
419 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
420 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
428 for (int i
= 0; i
< pos
; i
++ )
442 long wxTextCtrl::XYToPosition(long x
, long y
) const
444 if (!(m_windowStyle
& wxTE_MULTILINE
)) return 0;
448 for( int i
=1; i
<y
; i
++ ) pos
+= GetLineLength(i
);
450 pos
+=x
-1; // Pos start with 0
454 int wxTextCtrl::GetLineLength(long lineNo
) const
456 wxString str
= GetLineText (lineNo
);
457 return (int) str
.Length();
460 int wxTextCtrl::GetNumberOfLines() const
462 if (m_windowStyle
& wxTE_MULTILINE
)
464 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
465 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
470 for (int i
= 0; i
< len
; i
++ )
489 void wxTextCtrl::SetInsertionPoint( long pos
)
491 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
494 if (m_windowStyle
& wxTE_MULTILINE
)
495 gtk_text_set_point( GTK_TEXT(m_text
), tmp
);
497 gtk_entry_set_position( GTK_ENTRY(m_text
), tmp
);
500 void wxTextCtrl::SetInsertionPointEnd()
502 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
505 if (m_windowStyle
& wxTE_MULTILINE
)
506 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
508 pos
= GTK_ENTRY(m_text
)->text_length
;
510 SetInsertionPoint((pos
-1)>0 ? (pos
-1):0);
513 void wxTextCtrl::SetEditable( bool editable
)
515 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
517 if (m_windowStyle
& wxTE_MULTILINE
)
518 gtk_text_set_editable( GTK_TEXT(m_text
), editable
);
520 gtk_entry_set_editable( GTK_ENTRY(m_text
), editable
);
523 void wxTextCtrl::SetSelection( long from
, long to
)
525 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
527 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
530 void wxTextCtrl::ShowPosition( long WXUNUSED(pos
) )
532 wxFAIL_MSG( "wxTextCtrl::ShowPosition not implemented" );
535 long wxTextCtrl::GetInsertionPoint() const
537 wxCHECK_MSG( m_text
!= NULL
, 0, "invalid text ctrl" );
539 return (long) GTK_EDITABLE(m_text
)->current_pos
;
542 long wxTextCtrl::GetLastPosition() const
544 wxCHECK_MSG( m_text
!= NULL
, 0, "invalid text ctrl" );
547 if (m_windowStyle
& wxTE_MULTILINE
)
548 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
550 pos
= GTK_ENTRY(m_text
)->text_length
;
555 void wxTextCtrl::Remove( long from
, long to
)
557 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
559 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
562 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
564 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
566 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
567 if (value
.IsNull()) return;
568 gint pos
= (gint
)from
;
569 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &pos
);
572 void wxTextCtrl::Cut()
574 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
576 #if (GTK_MINOR_VERSION == 1)
577 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
) );
579 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
), 0 );
583 void wxTextCtrl::Copy()
585 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
587 #if (GTK_MINOR_VERSION == 1)
588 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
) );
590 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
), 0 );
594 void wxTextCtrl::Paste()
596 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
598 #if (GTK_MINOR_VERSION == 1)
599 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
) );
601 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
), 0 );
605 void wxTextCtrl::Clear()
610 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
612 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
614 if ((key_event
.KeyCode() == WXK_RETURN
) && (m_windowStyle
& wxPROCESS_ENTER
))
616 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
617 event
.SetEventObject(this);
618 if (GetEventHandler()->ProcessEvent(event
)) return;
624 int wxTextCtrl::overflow( int WXUNUSED(c
) )
626 int len
= pptr() - pbase();
627 char *txt
= new char[len
+1];
628 strncpy(txt
, pbase(), len
);
631 setp(pbase(), epptr());
636 int wxTextCtrl::sync()
638 int len
= pptr() - pbase();
639 char *txt
= new char[len
+1];
640 strncpy(txt
, pbase(), len
);
643 setp(pbase(), epptr());
648 int wxTextCtrl::underflow()
653 wxTextCtrl
& wxTextCtrl::operator<<(const wxString
& s
)
659 wxTextCtrl
& wxTextCtrl::operator<<(float f
)
661 static char buf
[100];
662 sprintf(buf
, "%.2f", f
);
667 wxTextCtrl
& wxTextCtrl::operator<<(double d
)
669 static char buf
[100];
670 sprintf(buf
, "%.2f", d
);
675 wxTextCtrl
& wxTextCtrl::operator<<(int i
)
677 static char buf
[100];
678 sprintf(buf
, "%i", i
);
683 wxTextCtrl
& wxTextCtrl::operator<<(long i
)
685 static char buf
[100];
686 sprintf(buf
, "%ld", i
);
691 wxTextCtrl
& wxTextCtrl::operator<<(const char c
)
701 GtkWidget
* wxTextCtrl::GetConnectWidget()
703 return GTK_WIDGET(m_text
);
706 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow
*window
)
708 if (m_windowStyle
& wxTE_MULTILINE
)
709 return (window
== GTK_TEXT(m_text
)->text_area
);
711 return (window
== GTK_ENTRY(m_text
)->text_area
);
714 void wxTextCtrl::SetFont( const wxFont
&WXUNUSED(font
) )
716 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
721 void wxTextCtrl::SetForegroundColour( const wxColour
&WXUNUSED(colour
) )
723 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
728 void wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
730 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
732 wxControl::SetBackgroundColour( colour
);
734 wxColour sysbg
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE
);
735 if (sysbg
.Red() == colour
.Red() &&
736 sysbg
.Green() == colour
.Green() &&
737 sysbg
.Blue() == colour
.Blue())
742 if (!m_backgroundColour
.Ok()) return;
744 if (m_windowStyle
& wxTE_MULTILINE
)
746 GdkWindow
*window
= GTK_TEXT(m_text
)->text_area
;
747 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
748 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
749 gdk_window_clear( window
);
753 void wxTextCtrl::ApplyWidgetStyle()
755 if (m_windowStyle
& wxTE_MULTILINE
)
762 gtk_widget_set_style( m_text
, m_widgetStyle
);