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 #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);
188 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &tmp
);
189 SetInsertionPointEnd();
192 if (style
& wxTE_PASSWORD
)
195 gtk_entry_set_visibility( GTK_ENTRY(m_text
), FALSE
);
198 if (style
& wxTE_READONLY
)
201 gtk_entry_set_editable( GTK_ENTRY(m_text
), FALSE
);
206 gtk_text_set_editable( GTK_TEXT(m_text
), 1 );
211 SetBackgroundColour( parent
->GetBackgroundColour() );
212 SetForegroundColour( parent
->GetForegroundColour() );
217 void wxTextCtrl::CalculateScrollbar()
219 if ((m_windowStyle
& wxTE_MULTILINE
) == 0) return;
221 GtkAdjustment
*adj
= GTK_TEXT(m_text
)->vadj
;
223 if (adj
->upper
- adj
->page_size
< 0.8)
225 if (m_vScrollbarVisible
)
227 gtk_widget_hide( m_vScrollbar
);
229 m_vScrollbarVisible
= FALSE
;
234 if (!m_vScrollbarVisible
)
236 gtk_widget_show( m_vScrollbar
);
238 m_vScrollbarVisible
= TRUE
;
243 wxString
wxTextCtrl::GetValue() const
245 wxCHECK_MSG( m_text
!= NULL
, "", "invalid text ctrl" );
248 if (m_windowStyle
& wxTE_MULTILINE
)
250 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
251 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
257 tmp
= gtk_entry_get_text( GTK_ENTRY(m_text
) );
262 void wxTextCtrl::SetValue( const wxString
&value
)
264 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
267 if (!value
.IsNull()) tmp
= value
;
268 if (m_windowStyle
& wxTE_MULTILINE
)
270 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
271 gtk_editable_delete_text( GTK_EDITABLE(m_text
), 0, len
);
273 gtk_editable_insert_text( GTK_EDITABLE(m_text
), tmp
, tmp
.Length(), &len
);
277 gtk_entry_set_text( GTK_ENTRY(m_text
), tmp
);
281 void wxTextCtrl::WriteText( const wxString
&text
)
283 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
285 if (text
.IsNull()) return;
287 if (m_windowStyle
& wxTE_MULTILINE
)
289 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
290 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
, text
.Length(), &len
);
294 gtk_entry_append_text( GTK_ENTRY(m_text
), text
);
298 bool wxTextCtrl::LoadFile( const wxString
&file
)
300 wxCHECK_MSG( m_text
!= NULL
, FALSE
, "invalid text ctrl" );
302 if (!wxFileExists(file
)) return FALSE
;
306 FILE *fp
= (FILE*) NULL
;
309 if ((stat ((char*) (const char*) file
, &statb
) == -1) || (statb
.st_mode
& S_IFMT
) != S_IFREG
||
310 !(fp
= fopen ((char*) (const char*) file
, "r")))
316 gint len
= statb
.st_size
;
318 if (!(text
= (char*)malloc ((unsigned) (len
+ 1))))
323 if (fread (text
, sizeof (char), len
, fp
) != (size_t) len
)
330 if (m_windowStyle
& wxTE_MULTILINE
)
333 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
, len
, &pos
);
337 gtk_entry_set_text( GTK_ENTRY(m_text
), text
);
347 bool wxTextCtrl::SaveFile( const wxString
&file
)
349 wxCHECK_MSG( m_text
!= NULL
, FALSE
, "invalid text ctrl" );
351 if (file
== "") return FALSE
;
355 if (!(fp
= fopen ((char*) (const char*) file
, "w")))
361 char *text
= (char*) NULL
;
364 if (m_windowStyle
& wxTE_MULTILINE
)
366 len
= gtk_text_get_length( GTK_TEXT(m_text
) );
367 text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
371 text
= gtk_entry_get_text( GTK_ENTRY(m_text
) );
374 if (fwrite (text
, sizeof (char), len
, fp
) != (size_t) len
)
376 // Did not write whole file
379 // Make sure newline terminates the file
380 if (text
[len
- 1] != '\n')
385 if (m_windowStyle
& wxTE_MULTILINE
) g_free( text
);
394 wxString
wxTextCtrl::GetLineText( long lineNo
) const
396 if (m_windowStyle
& wxTE_MULTILINE
)
398 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
399 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
406 for (i
= 0; currentLine
!= lineNo
&& text
[i
]; i
++ )
411 for (j
= 0; text
[i
] && text
[i
] != '\n'; i
++, j
++ )
418 return wxEmptyString
;
422 if (lineNo
== 0) return GetValue();
423 return wxEmptyString
;
427 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
429 wxFAIL_MSG( "wxTextCtrl::OnDropFiles not implemented" );
432 long wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
434 if (!(m_windowStyle
& wxTE_MULTILINE
))
436 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
437 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
445 for (int i
= 0; i
< pos
; i
++ )
459 long wxTextCtrl::XYToPosition(long x
, long y
) const
461 if (!(m_windowStyle
& wxTE_MULTILINE
)) return 0;
465 for( int i
=1; i
<y
; i
++ ) pos
+= GetLineLength(i
);
467 pos
+=x
-1; // Pos start with 0
471 int wxTextCtrl::GetLineLength(long lineNo
) const
473 wxString str
= GetLineText (lineNo
);
474 return (int) str
.Length();
477 int wxTextCtrl::GetNumberOfLines() const
479 if (m_windowStyle
& wxTE_MULTILINE
)
481 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
482 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
487 for (int i
= 0; i
< len
; i
++ )
506 void wxTextCtrl::SetInsertionPoint( long pos
)
508 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
511 if (m_windowStyle
& wxTE_MULTILINE
)
512 gtk_text_set_point( GTK_TEXT(m_text
), tmp
);
514 gtk_entry_set_position( GTK_ENTRY(m_text
), tmp
);
517 void wxTextCtrl::SetInsertionPointEnd()
519 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
522 if (m_windowStyle
& wxTE_MULTILINE
)
523 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
525 pos
= GTK_ENTRY(m_text
)->text_length
;
527 SetInsertionPoint((pos
-1)>0 ? (pos
-1):0);
530 void wxTextCtrl::SetEditable( bool editable
)
532 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
534 if (m_windowStyle
& wxTE_MULTILINE
)
535 gtk_text_set_editable( GTK_TEXT(m_text
), editable
);
537 gtk_entry_set_editable( GTK_ENTRY(m_text
), editable
);
540 void wxTextCtrl::SetSelection( long from
, long to
)
542 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
544 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
547 void wxTextCtrl::ShowPosition( long WXUNUSED(pos
) )
549 wxFAIL_MSG( "wxTextCtrl::ShowPosition not implemented" );
552 long wxTextCtrl::GetInsertionPoint() const
554 wxCHECK_MSG( m_text
!= NULL
, 0, "invalid text ctrl" );
556 return (long) GTK_EDITABLE(m_text
)->current_pos
;
559 long wxTextCtrl::GetLastPosition() const
561 wxCHECK_MSG( m_text
!= NULL
, 0, "invalid text ctrl" );
564 if (m_windowStyle
& wxTE_MULTILINE
)
565 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
567 pos
= GTK_ENTRY(m_text
)->text_length
;
572 void wxTextCtrl::Remove( long from
, long to
)
574 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
576 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
579 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
581 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
583 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
584 if (value
.IsNull()) return;
585 gint pos
= (gint
)from
;
586 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &pos
);
589 void wxTextCtrl::Cut()
591 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
593 #if (GTK_MINOR_VERSION == 1)
594 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
) );
596 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
), 0 );
600 void wxTextCtrl::Copy()
602 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
604 #if (GTK_MINOR_VERSION == 1)
605 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
) );
607 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
), 0 );
611 void wxTextCtrl::Paste()
613 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
615 #if (GTK_MINOR_VERSION == 1)
616 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
) );
618 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
), 0 );
622 void wxTextCtrl::Clear()
627 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
629 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
631 if ((key_event
.KeyCode() == WXK_RETURN
) && (m_windowStyle
& wxPROCESS_ENTER
))
633 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
634 event
.SetEventObject(this);
635 if (GetEventHandler()->ProcessEvent(event
)) return;
641 #ifndef NO_TEXT_WINDOW_STREAM
642 int wxTextCtrl::overflow( int WXUNUSED(c
) )
644 int len
= pptr() - pbase();
645 char *txt
= new char[len
+1];
646 strncpy(txt
, pbase(), len
);
649 setp(pbase(), epptr());
654 int wxTextCtrl::sync()
656 int len
= pptr() - pbase();
657 char *txt
= new char[len
+1];
658 strncpy(txt
, pbase(), len
);
661 setp(pbase(), epptr());
666 int wxTextCtrl::underflow()
671 wxTextCtrl
& wxTextCtrl::operator<<(const wxString
& s
)
677 wxTextCtrl
& wxTextCtrl::operator<<(float f
)
679 static char buf
[100];
680 sprintf(buf
, "%.2f", f
);
685 wxTextCtrl
& wxTextCtrl::operator<<(double d
)
687 static char buf
[100];
688 sprintf(buf
, "%.2f", d
);
693 wxTextCtrl
& wxTextCtrl::operator<<(int i
)
695 static char buf
[100];
696 sprintf(buf
, "%i", i
);
701 wxTextCtrl
& wxTextCtrl::operator<<(long i
)
703 static char buf
[100];
704 sprintf(buf
, "%ld", i
);
709 wxTextCtrl
& wxTextCtrl::operator<<(const char c
)
720 GtkWidget
* wxTextCtrl::GetConnectWidget()
722 return GTK_WIDGET(m_text
);
725 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow
*window
)
727 if (m_windowStyle
& wxTE_MULTILINE
)
728 return (window
== GTK_TEXT(m_text
)->text_area
);
730 return (window
== GTK_ENTRY(m_text
)->text_area
);
733 void wxTextCtrl::SetFont( const wxFont
&WXUNUSED(font
) )
735 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
740 void wxTextCtrl::SetForegroundColour( const wxColour
&WXUNUSED(colour
) )
742 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
747 void wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
749 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
751 wxControl::SetBackgroundColour( colour
);
753 wxColour sysbg
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE
);
754 if (sysbg
.Red() == colour
.Red() &&
755 sysbg
.Green() == colour
.Green() &&
756 sysbg
.Blue() == colour
.Blue())
761 if (!m_backgroundColour
.Ok()) return;
763 if (m_windowStyle
& wxTE_MULTILINE
)
765 GdkWindow
*window
= GTK_TEXT(m_text
)->text_area
;
766 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
767 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
768 gdk_window_clear( window
);
772 void wxTextCtrl::ApplyWidgetStyle()
774 if (m_windowStyle
& wxTE_MULTILINE
)
781 gtk_widget_set_style( m_text
, m_widgetStyle
);