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>
23 #include "gdk/gdkkeysyms.h"
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
29 extern bool g_blockEventsOnDrag
;
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
36 gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
40 win
->CalculateScrollbar();
42 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, win
->m_windowId
);
43 wxString
val( win
->GetValue() );
44 if (!val
.IsNull()) event
.m_commandString
= WXSTRINGCAST val
;
45 event
.SetEventObject( win
);
46 win
->GetEventHandler()->ProcessEvent( event
);
49 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
54 gtk_text_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* WXUNUSED(alloc
), wxTextCtrl
*win
)
56 win
->CalculateScrollbar();
59 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
63 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
,wxControl
)
65 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
66 EVT_CHAR(wxTextCtrl::OnChar
)
69 wxTextCtrl::wxTextCtrl() : streambuf()
71 if (allocate()) setp(base(),ebuf());
76 wxTextCtrl::wxTextCtrl( wxWindow
*parent
, wxWindowID id
, const wxString
&value
,
77 const wxPoint
&pos
, const wxSize
&size
,
78 int style
, const wxValidator
& validator
, const wxString
&name
) : streambuf()
80 if (allocate()) setp(base(),ebuf());
83 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
86 bool wxTextCtrl::Create( wxWindow
*parent
, wxWindowID id
, const wxString
&value
,
87 const wxPoint
&pos
, const wxSize
&size
,
88 int style
, const wxValidator
& validator
, const wxString
&name
)
91 m_acceptsFocus
= TRUE
;
93 PreCreation( parent
, id
, pos
, size
, style
, name
);
95 SetValidator( validator
);
97 m_vScrollbarVisible
= TRUE
;
99 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
102 // a multi-line edit control: create a vertical scrollbar by default and
103 // horizontal if requested
104 bool bHasHScrollbar
= (style
& wxHSCROLL
) != 0;
106 // create our control...
107 m_text
= gtk_text_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
109 // ... and put into the upper left hand corner of the table
110 m_widget
= gtk_table_new(bHasHScrollbar
? 2 : 1, 2, FALSE
);
111 gtk_table_attach( GTK_TABLE(m_widget
), m_text
, 0, 1, 0, 1,
112 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
113 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
116 // put the horizontal scrollbar in the lower left hand corner
119 GtkWidget
*hscrollbar
= gtk_hscrollbar_new(GTK_TEXT(m_text
)->hadj
);
120 gtk_table_attach(GTK_TABLE(m_widget
), hscrollbar
, 0, 1, 1, 2,
121 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
),
124 gtk_widget_show(hscrollbar
);
127 // finally, put the vertical scrollbar in the upper right corner
128 m_vScrollbar
= gtk_vscrollbar_new( GTK_TEXT(m_text
)->vadj
);
129 gtk_table_attach(GTK_TABLE(m_widget
), m_vScrollbar
, 1, 2, 0, 1,
131 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
),
133 gtk_widget_show( m_vScrollbar
);
135 gtk_signal_connect( GTK_OBJECT(m_widget
), "size_allocate",
136 GTK_SIGNAL_FUNC(gtk_text_size_callback
), (gpointer
)this );
140 // a single-line text control: no need for scrollbars
142 m_text
= gtk_entry_new();
145 wxSize newSize
= size
;
146 if (newSize
.x
== -1) newSize
.x
= 80;
147 if (newSize
.y
== -1) newSize
.y
= 26;
148 SetSize( newSize
.x
, newSize
.y
);
150 m_parent
->AddChild( this );
152 (m_parent
->m_insertCallback
)( m_parent
, this );
158 gtk_widget_realize(m_text
);
159 gtk_widget_show(m_text
);
162 // we want to be notified about text changes
163 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
164 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
169 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &tmp
);
170 SetInsertionPointEnd();
173 if (style
& wxTE_PASSWORD
)
176 gtk_entry_set_visibility( GTK_ENTRY(m_text
), FALSE
);
179 if (style
& wxTE_READONLY
)
182 gtk_entry_set_editable( GTK_ENTRY(m_text
), FALSE
);
187 gtk_text_set_editable( GTK_TEXT(m_text
), 1 );
192 SetBackgroundColour( parent
->GetBackgroundColour() );
193 SetForegroundColour( parent
->GetForegroundColour() );
198 void wxTextCtrl::CalculateScrollbar()
200 if ((m_windowStyle
& wxTE_MULTILINE
) == 0) return;
202 GtkAdjustment
*adj
= GTK_TEXT(m_text
)->vadj
;
204 if (adj
->upper
- adj
->page_size
< 0.8)
206 if (m_vScrollbarVisible
)
208 gtk_widget_hide( m_vScrollbar
);
210 m_vScrollbarVisible
= FALSE
;
215 if (!m_vScrollbarVisible
)
217 gtk_widget_show( m_vScrollbar
);
219 m_vScrollbarVisible
= TRUE
;
224 wxString
wxTextCtrl::GetValue() const
226 wxCHECK_MSG( m_text
!= NULL
, "", "invalid text ctrl" );
229 if (m_windowStyle
& wxTE_MULTILINE
)
231 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
232 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
238 tmp
= gtk_entry_get_text( GTK_ENTRY(m_text
) );
243 void wxTextCtrl::SetValue( const wxString
&value
)
245 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
248 if (!value
.IsNull()) tmp
= value
;
249 if (m_windowStyle
& wxTE_MULTILINE
)
251 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
252 gtk_editable_delete_text( GTK_EDITABLE(m_text
), 0, len
);
254 gtk_editable_insert_text( GTK_EDITABLE(m_text
), tmp
, tmp
.Length(), &len
);
258 gtk_entry_set_text( GTK_ENTRY(m_text
), tmp
);
262 void wxTextCtrl::WriteText( const wxString
&text
)
264 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
266 if (text
.IsNull()) return;
268 if (m_windowStyle
& wxTE_MULTILINE
)
270 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
271 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
, text
.Length(), &len
);
275 gtk_entry_append_text( GTK_ENTRY(m_text
), text
);
279 bool wxTextCtrl::LoadFile( const wxString
&file
)
281 wxCHECK_MSG( m_text
!= NULL
, FALSE
, "invalid text ctrl" );
283 if (!wxFileExists(file
)) return FALSE
;
290 if ((stat ((char*) (const char*) file
, &statb
) == -1) || (statb
.st_mode
& S_IFMT
) != S_IFREG
||
291 !(fp
= fopen ((char*) (const char*) file
, "r")))
297 gint len
= statb
.st_size
;
299 if (!(text
= (char*)malloc ((unsigned) (len
+ 1))))
304 if (fread (text
, sizeof (char), len
, fp
) != (size_t) len
)
311 if (m_windowStyle
& wxTE_MULTILINE
)
314 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
, len
, &pos
);
318 gtk_entry_set_text( GTK_ENTRY(m_text
), text
);
328 bool wxTextCtrl::SaveFile( const wxString
&file
)
330 wxCHECK_MSG( m_text
!= NULL
, FALSE
, "invalid text ctrl" );
332 if (file
== "") return FALSE
;
336 if (!(fp
= fopen ((char*) (const char*) file
, "w")))
345 if (m_windowStyle
& wxTE_MULTILINE
)
347 len
= gtk_text_get_length( GTK_TEXT(m_text
) );
348 text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
352 text
= gtk_entry_get_text( GTK_ENTRY(m_text
) );
355 if (fwrite (text
, sizeof (char), len
, fp
) != (size_t) len
)
357 // Did not write whole file
360 // Make sure newline terminates the file
361 if (text
[len
- 1] != '\n')
366 if (m_windowStyle
& wxTE_MULTILINE
) g_free( text
);
375 wxString
wxTextCtrl::GetLineText( long lineNo
) const
377 if (m_windowStyle
& wxTE_MULTILINE
)
379 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
380 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
387 for (i
= 0; currentLine
!= lineNo
&& text
[i
]; i
++ )
392 for (j
= 0; text
[i
] && text
[i
] != '\n'; i
++, j
++ )
399 return wxEmptyString
;
403 if (lineNo
== 0) return GetValue();
404 return wxEmptyString
;
408 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
410 wxFAIL_MSG( "wxTextCtrl::OnDropFiles not implemented" );
413 long wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
415 if (!(m_windowStyle
& wxTE_MULTILINE
))
417 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
418 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
426 for (int i
= 0; i
< pos
; i
++ )
440 long wxTextCtrl::XYToPosition(long x
, long y
) const
442 if (!(m_windowStyle
& wxTE_MULTILINE
)) return 0;
446 for( int i
=1; i
<y
; i
++ ) pos
+= GetLineLength(i
);
448 pos
+=x
-1; // Pos start with 0
452 int wxTextCtrl::GetLineLength(long lineNo
) const
454 wxString str
= GetLineText (lineNo
);
455 return (int) str
.Length();
458 int wxTextCtrl::GetNumberOfLines() const
460 if (m_windowStyle
& wxTE_MULTILINE
)
462 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
463 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
468 for (int i
= 0; i
< len
; i
++ )
487 void wxTextCtrl::SetInsertionPoint( long pos
)
489 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
492 if (m_windowStyle
& wxTE_MULTILINE
)
493 gtk_text_set_point( GTK_TEXT(m_text
), tmp
);
495 gtk_entry_set_position( GTK_ENTRY(m_text
), tmp
);
498 void wxTextCtrl::SetInsertionPointEnd()
500 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
503 if (m_windowStyle
& wxTE_MULTILINE
)
504 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
506 pos
= GTK_ENTRY(m_text
)->text_length
;
508 SetInsertionPoint((pos
-1)>0 ? (pos
-1):0);
511 void wxTextCtrl::SetEditable( bool editable
)
513 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
515 if (m_windowStyle
& wxTE_MULTILINE
)
516 gtk_text_set_editable( GTK_TEXT(m_text
), editable
);
518 gtk_entry_set_editable( GTK_ENTRY(m_text
), editable
);
521 void wxTextCtrl::SetSelection( long from
, long to
)
523 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
525 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
528 void wxTextCtrl::ShowPosition( long WXUNUSED(pos
) )
530 wxFAIL_MSG( "wxTextCtrl::ShowPosition not implemented" );
533 long wxTextCtrl::GetInsertionPoint() const
535 wxCHECK_MSG( m_text
!= NULL
, 0, "invalid text ctrl" );
537 return (long) GTK_EDITABLE(m_text
)->current_pos
;
540 long wxTextCtrl::GetLastPosition() const
542 wxCHECK_MSG( m_text
!= NULL
, 0, "invalid text ctrl" );
545 if (m_windowStyle
& wxTE_MULTILINE
)
546 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
548 pos
= GTK_ENTRY(m_text
)->text_length
;
553 void wxTextCtrl::Remove( long from
, long to
)
555 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
557 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
560 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
562 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
564 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
565 if (value
.IsNull()) return;
566 gint pos
= (gint
)from
;
567 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &pos
);
570 void wxTextCtrl::Cut()
572 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
574 #if (GTK_MINOR_VERSION == 1)
575 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
) );
577 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
), 0 );
581 void wxTextCtrl::Copy()
583 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
585 #if (GTK_MINOR_VERSION == 1)
586 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
) );
588 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
), 0 );
592 void wxTextCtrl::Paste()
594 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
596 #if (GTK_MINOR_VERSION == 1)
597 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
) );
599 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
), 0 );
603 void wxTextCtrl::Clear()
608 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
610 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
612 if ((key_event
.KeyCode() == WXK_RETURN
) && (m_windowStyle
& wxPROCESS_ENTER
))
614 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
615 event
.SetEventObject(this);
616 if (GetEventHandler()->ProcessEvent(event
)) return;
622 int wxTextCtrl::overflow( int WXUNUSED(c
) )
624 int len
= pptr() - pbase();
625 char *txt
= new char[len
+1];
626 strncpy(txt
, pbase(), len
);
629 setp(pbase(), epptr());
634 int wxTextCtrl::sync()
636 int len
= pptr() - pbase();
637 char *txt
= new char[len
+1];
638 strncpy(txt
, pbase(), len
);
641 setp(pbase(), epptr());
646 int wxTextCtrl::underflow()
651 wxTextCtrl
& wxTextCtrl::operator<<(const wxString
& s
)
657 wxTextCtrl
& wxTextCtrl::operator<<(float f
)
659 static char buf
[100];
660 sprintf(buf
, "%.2f", f
);
665 wxTextCtrl
& wxTextCtrl::operator<<(double d
)
667 static char buf
[100];
668 sprintf(buf
, "%.2f", d
);
673 wxTextCtrl
& wxTextCtrl::operator<<(int i
)
675 static char buf
[100];
676 sprintf(buf
, "%i", i
);
681 wxTextCtrl
& wxTextCtrl::operator<<(long i
)
683 static char buf
[100];
684 sprintf(buf
, "%ld", i
);
689 wxTextCtrl
& wxTextCtrl::operator<<(const char c
)
699 GtkWidget
* wxTextCtrl::GetConnectWidget()
701 return GTK_WIDGET(m_text
);
704 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow
*window
)
706 if (m_windowStyle
& wxTE_MULTILINE
)
707 return (window
== GTK_TEXT(m_text
)->text_area
);
709 return (window
== GTK_ENTRY(m_text
)->text_area
);
712 void wxTextCtrl::SetFont( const wxFont
&WXUNUSED(font
) )
714 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
719 void wxTextCtrl::SetForegroundColour( const wxColour
&WXUNUSED(colour
) )
721 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
726 void wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
728 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
730 wxControl::SetBackgroundColour( colour
);
732 wxColour sysbg
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE
);
733 if (sysbg
.Red() == colour
.Red() &&
734 sysbg
.Green() == colour
.Green() &&
735 sysbg
.Blue() == colour
.Blue())
740 if (!m_backgroundColour
.Ok()) return;
742 if (m_windowStyle
& wxTE_MULTILINE
)
744 GdkWindow
*window
= GTK_TEXT(m_text
)->text_area
;
745 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
746 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
747 gdk_window_clear( window
);
751 void wxTextCtrl::ApplyWidgetStyle()
753 if (m_windowStyle
& wxTE_MULTILINE
)
760 gtk_widget_set_style( m_text
, m_widgetStyle
);