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 //-----------------------------------------------------------------------------
25 //-----------------------------------------------------------------------------
28 gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
32 win
->CalculateScrollbar();
34 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, win
->m_windowId
);
35 wxString
val( win
->GetValue() );
36 if (!val
.IsNull()) event
.m_commandString
= WXSTRINGCAST val
;
37 event
.SetEventObject( win
);
38 win
->GetEventHandler()->ProcessEvent( event
);
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
46 gtk_text_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* WXUNUSED(alloc
), wxTextCtrl
*win
)
48 win
->CalculateScrollbar();
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
56 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
,wxControl
)
58 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
59 EVT_CHAR(wxTextCtrl::OnChar
)
62 wxTextCtrl::wxTextCtrl() : streambuf()
64 if (allocate()) setp(base(),ebuf());
69 wxTextCtrl::wxTextCtrl( wxWindow
*parent
, wxWindowID id
, const wxString
&value
,
70 const wxPoint
&pos
, const wxSize
&size
,
71 int style
, const wxValidator
& validator
, const wxString
&name
) : streambuf()
73 if (allocate()) setp(base(),ebuf());
76 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
79 bool wxTextCtrl::Create( wxWindow
*parent
, wxWindowID id
, const wxString
&value
,
80 const wxPoint
&pos
, const wxSize
&size
,
81 int style
, const wxValidator
& validator
, const wxString
&name
)
85 PreCreation( parent
, id
, pos
, size
, style
, name
);
87 SetValidator( validator
);
89 m_vScrollbarVisible
= TRUE
;
91 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
94 // a multi-line edit control: create a vertical scrollbar by default and
95 // horizontal if requested
96 bool bHasHScrollbar
= (style
& wxHSCROLL
) != 0;
98 // create our control...
99 m_text
= gtk_text_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
101 // ... and put into the upper left hand corner of the table
102 m_widget
= gtk_table_new(bHasHScrollbar
? 2 : 1, 2, FALSE
);
103 gtk_table_attach( GTK_TABLE(m_widget
), m_text
, 0, 1, 0, 1,
104 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
105 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
108 // put the horizontal scrollbar in the lower left hand corner
111 GtkWidget
*hscrollbar
= gtk_hscrollbar_new(GTK_TEXT(m_text
)->hadj
);
112 gtk_table_attach(GTK_TABLE(m_widget
), hscrollbar
, 0, 1, 1, 2,
113 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
),
116 gtk_widget_show(hscrollbar
);
119 // finally, put the vertical scrollbar in the upper right corner
120 m_vScrollbar
= gtk_vscrollbar_new( GTK_TEXT(m_text
)->vadj
);
121 gtk_table_attach(GTK_TABLE(m_widget
), m_vScrollbar
, 1, 2, 0, 1,
123 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
),
125 gtk_widget_show( m_vScrollbar
);
127 gtk_signal_connect( GTK_OBJECT(m_widget
), "size_allocate",
128 GTK_SIGNAL_FUNC(gtk_text_size_callback
), (gpointer
)this );
132 // a single-line text control: no need for scrollbars
134 m_text
= gtk_entry_new();
137 wxSize newSize
= size
;
138 if (newSize
.x
== -1) newSize
.x
= 80;
139 if (newSize
.y
== -1) newSize
.y
= 26;
140 SetSize( newSize
.x
, newSize
.y
);
142 m_parent
->AddChild( this );
144 (m_parent
->m_insertCallback
)( m_parent
, this );
150 gtk_widget_realize(m_text
);
151 gtk_widget_show(m_text
);
154 // we want to be notified about text changes
155 gtk_signal_connect(GTK_OBJECT(m_text
), "changed",
156 GTK_SIGNAL_FUNC(gtk_text_changed_callback
),
162 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &tmp
);
163 SetInsertionPointEnd();
166 if (style
& wxTE_PASSWORD
)
169 gtk_entry_set_visibility( GTK_ENTRY(m_text
), FALSE
);
172 if (style
& wxTE_READONLY
)
175 gtk_entry_set_editable( GTK_ENTRY(m_text
), FALSE
);
180 gtk_text_set_editable( GTK_TEXT(m_text
), 1 );
185 SetBackgroundColour( parent
->GetBackgroundColour() );
186 SetForegroundColour( parent
->GetForegroundColour() );
191 void wxTextCtrl::CalculateScrollbar()
193 if ((m_windowStyle
& wxTE_MULTILINE
) == 0) return;
195 GtkAdjustment
*adj
= GTK_TEXT(m_text
)->vadj
;
197 if (adj
->upper
- adj
->page_size
< 0.8)
199 if (m_vScrollbarVisible
)
201 gtk_widget_hide( m_vScrollbar
);
203 m_vScrollbarVisible
= FALSE
;
208 if (!m_vScrollbarVisible
)
210 gtk_widget_show( m_vScrollbar
);
212 m_vScrollbarVisible
= TRUE
;
217 wxString
wxTextCtrl::GetValue() const
219 wxCHECK_MSG( m_text
!= NULL
, "", "invalid text ctrl" );
222 if (m_windowStyle
& wxTE_MULTILINE
)
224 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
225 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
231 tmp
= gtk_entry_get_text( GTK_ENTRY(m_text
) );
236 void wxTextCtrl::SetValue( const wxString
&value
)
238 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
241 if (!value
.IsNull()) tmp
= value
;
242 if (m_windowStyle
& wxTE_MULTILINE
)
244 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
245 gtk_editable_delete_text( GTK_EDITABLE(m_text
), 0, len
);
247 gtk_editable_insert_text( GTK_EDITABLE(m_text
), tmp
, tmp
.Length(), &len
);
251 gtk_entry_set_text( GTK_ENTRY(m_text
), tmp
);
255 void wxTextCtrl::WriteText( const wxString
&text
)
257 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
259 if (text
.IsNull()) return;
261 if (m_windowStyle
& wxTE_MULTILINE
)
263 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
264 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
, text
.Length(), &len
);
268 gtk_entry_append_text( GTK_ENTRY(m_text
), text
);
272 bool wxTextCtrl::LoadFile( const wxString
&file
)
274 wxCHECK_MSG( m_text
!= NULL
, FALSE
, "invalid text ctrl" );
276 if (!wxFileExists(file
)) return FALSE
;
283 if ((stat ((char*) (const char*) file
, &statb
) == -1) || (statb
.st_mode
& S_IFMT
) != S_IFREG
||
284 !(fp
= fopen ((char*) (const char*) file
, "r")))
290 gint len
= statb
.st_size
;
292 if (!(text
= (char*)malloc ((unsigned) (len
+ 1))))
297 if (fread (text
, sizeof (char), len
, fp
) != (size_t) len
)
304 if (m_windowStyle
& wxTE_MULTILINE
)
307 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
, len
, &pos
);
311 gtk_entry_set_text( GTK_ENTRY(m_text
), text
);
321 bool wxTextCtrl::SaveFile( const wxString
&file
)
323 wxCHECK_MSG( m_text
!= NULL
, FALSE
, "invalid text ctrl" );
325 if (file
== "") return FALSE
;
329 if (!(fp
= fopen ((char*) (const char*) file
, "w")))
338 if (m_windowStyle
& wxTE_MULTILINE
)
340 len
= gtk_text_get_length( GTK_TEXT(m_text
) );
341 text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
345 text
= gtk_entry_get_text( GTK_ENTRY(m_text
) );
348 if (fwrite (text
, sizeof (char), len
, fp
) != (size_t) len
)
350 // Did not write whole file
353 // Make sure newline terminates the file
354 if (text
[len
- 1] != '\n')
359 if (m_windowStyle
& wxTE_MULTILINE
) g_free( text
);
368 wxString
wxTextCtrl::GetLineText( long lineNo
) const
370 if (m_windowStyle
& wxTE_MULTILINE
)
372 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
373 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
380 for (i
= 0; currentLine
!= lineNo
&& text
[i
]; i
++ )
385 for (j
= 0; text
[i
] && text
[i
] != '\n'; i
++, j
++ )
392 return wxEmptyString
;
396 if (lineNo
== 0) return GetValue();
397 return wxEmptyString
;
401 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
403 wxFAIL_MSG( "wxTextCtrl::OnDropFiles not implemented" );
406 long wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
408 if (!(m_windowStyle
& wxTE_MULTILINE
))
410 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
411 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
419 for (int i
= 0; i
< pos
; i
++ )
433 long wxTextCtrl::XYToPosition(long x
, long y
) const
435 if (!(m_windowStyle
& wxTE_MULTILINE
)) return 0;
439 for( int i
=1; i
<y
; i
++ ) pos
+= GetLineLength(i
);
441 pos
+=x
-1; // Pos start with 0
445 int wxTextCtrl::GetLineLength(long lineNo
) const
447 wxString str
= GetLineText (lineNo
);
448 return (int) str
.Length();
451 int wxTextCtrl::GetNumberOfLines() const
453 if (m_windowStyle
& wxTE_MULTILINE
)
455 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
456 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
461 for (int i
= 0; i
< len
; i
++ )
480 void wxTextCtrl::SetInsertionPoint( long pos
)
482 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
485 if (m_windowStyle
& wxTE_MULTILINE
)
486 gtk_text_set_point( GTK_TEXT(m_text
), tmp
);
488 gtk_entry_set_position( GTK_ENTRY(m_text
), tmp
);
491 void wxTextCtrl::SetInsertionPointEnd()
493 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
496 if (m_windowStyle
& wxTE_MULTILINE
)
497 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
499 pos
= GTK_ENTRY(m_text
)->text_length
;
501 SetInsertionPoint((pos
-1)>0 ? (pos
-1):0);
504 void wxTextCtrl::SetEditable( bool editable
)
506 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
508 if (m_windowStyle
& wxTE_MULTILINE
)
509 gtk_text_set_editable( GTK_TEXT(m_text
), editable
);
511 gtk_entry_set_editable( GTK_ENTRY(m_text
), editable
);
514 void wxTextCtrl::SetSelection( long from
, long to
)
516 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
518 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
521 void wxTextCtrl::ShowPosition( long WXUNUSED(pos
) )
523 wxFAIL_MSG( "wxTextCtrl::ShowPosition not implemented" );
526 long wxTextCtrl::GetInsertionPoint() const
528 wxCHECK_MSG( m_text
!= NULL
, 0, "invalid text ctrl" );
530 return (long) GTK_EDITABLE(m_text
)->current_pos
;
533 long wxTextCtrl::GetLastPosition() const
535 wxCHECK_MSG( m_text
!= NULL
, 0, "invalid text ctrl" );
538 if (m_windowStyle
& wxTE_MULTILINE
)
539 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
541 pos
= GTK_ENTRY(m_text
)->text_length
;
546 void wxTextCtrl::Remove( long from
, long to
)
548 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
550 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
553 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
555 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
557 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
558 if (value
.IsNull()) return;
559 gint pos
= (gint
)from
;
560 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &pos
);
563 void wxTextCtrl::Cut()
565 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
567 #if (GTK_MINOR_VERSION == 1)
568 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
) );
570 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
), 0 );
574 void wxTextCtrl::Copy()
576 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
578 #if (GTK_MINOR_VERSION == 1)
579 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
) );
581 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
), 0 );
585 void wxTextCtrl::Paste()
587 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
589 #if (GTK_MINOR_VERSION == 1)
590 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
) );
592 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
), 0 );
596 void wxTextCtrl::Clear()
601 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
603 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
605 if ((key_event
.KeyCode() == WXK_RETURN
) && (m_windowStyle
& wxPROCESS_ENTER
))
607 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
608 event
.SetEventObject(this);
609 if (GetEventHandler()->ProcessEvent(event
)) return;
611 else if (key_event
.KeyCode() == WXK_TAB
)
613 wxNavigationKeyEvent event
;
614 event
.SetDirection( key_event
.m_shiftDown
);
615 event
.SetWindowChange(FALSE
);
616 event
.SetEventObject(this);
618 if (GetEventHandler()->ProcessEvent(event
)) return;
623 int wxTextCtrl::overflow( int WXUNUSED(c
) )
625 int len
= pptr() - pbase();
626 char *txt
= new char[len
+1];
627 strncpy(txt
, pbase(), len
);
630 setp(pbase(), epptr());
635 int wxTextCtrl::sync()
637 int len
= pptr() - pbase();
638 char *txt
= new char[len
+1];
639 strncpy(txt
, pbase(), len
);
642 setp(pbase(), epptr());
647 int wxTextCtrl::underflow()
652 wxTextCtrl
& wxTextCtrl::operator<<(const wxString
& s
)
658 wxTextCtrl
& wxTextCtrl::operator<<(float f
)
660 static char buf
[100];
661 sprintf(buf
, "%.2f", f
);
666 wxTextCtrl
& wxTextCtrl::operator<<(double d
)
668 static char buf
[100];
669 sprintf(buf
, "%.2f", d
);
674 wxTextCtrl
& wxTextCtrl::operator<<(int i
)
676 static char buf
[100];
677 sprintf(buf
, "%i", i
);
682 wxTextCtrl
& wxTextCtrl::operator<<(long i
)
684 static char buf
[100];
685 sprintf(buf
, "%ld", i
);
690 wxTextCtrl
& wxTextCtrl::operator<<(const char c
)
700 GtkWidget
* wxTextCtrl::GetConnectWidget()
702 return GTK_WIDGET(m_text
);
705 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow
*window
)
707 if (m_windowStyle
& wxTE_MULTILINE
)
708 return (window
== GTK_TEXT(m_text
)->text_area
);
710 return (window
== GTK_ENTRY(m_text
)->text_area
);
713 void wxTextCtrl::SetFont( const wxFont
&WXUNUSED(font
) )
715 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
720 void wxTextCtrl::SetForegroundColour( const wxColour
&WXUNUSED(colour
) )
722 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
727 void wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
729 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
731 wxControl::SetBackgroundColour( colour
);
733 wxColour sysbg
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE
);
734 if (sysbg
.Red() == colour
.Red() &&
735 sysbg
.Green() == colour
.Green() &&
736 sysbg
.Blue() == colour
.Blue())
741 if (!m_backgroundColour
.Ok()) return;
743 if (m_windowStyle
& wxTE_MULTILINE
)
745 GdkWindow
*window
= GTK_TEXT(m_text
)->text_area
;
746 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
747 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
748 gdk_window_clear( window
);
752 void wxTextCtrl::ApplyWidgetStyle()
754 if (m_windowStyle
& wxTE_MULTILINE
)
761 gtk_widget_set_style( m_text
, m_widgetStyle
);