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
)
306 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
, 0, &len
);
310 gtk_entry_set_text( GTK_ENTRY(m_text
), text
);
320 bool wxTextCtrl::SaveFile( const wxString
&file
)
322 wxCHECK_MSG( m_text
!= NULL
, FALSE
, "invalid text ctrl" );
324 if (file
== "") return FALSE
;
328 if (!(fp
= fopen ((char*) (const char*) file
, "w")))
337 if (m_windowStyle
& wxTE_MULTILINE
)
339 len
= gtk_text_get_length( GTK_TEXT(m_text
) );
340 text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
344 text
= gtk_entry_get_text( GTK_ENTRY(m_text
) );
347 if (fwrite (text
, sizeof (char), len
, fp
) != (size_t) len
)
349 // Did not write whole file
352 // Make sure newline terminates the file
353 if (text
[len
- 1] != '\n')
358 if (m_windowStyle
& wxTE_MULTILINE
) g_free( text
);
367 wxString
wxTextCtrl::GetLineText( long lineNo
) const
369 if (m_windowStyle
& wxTE_MULTILINE
)
371 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
372 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
379 for (i
= 0; currentLine
!= lineNo
&& text
[i
]; i
++ )
384 for (j
= 0; text
[i
] && text
[i
] != '\n'; i
++, j
++ )
391 return wxEmptyString
;
395 if (lineNo
== 0) return GetValue();
396 return wxEmptyString
;
400 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
402 wxFAIL_MSG( "wxTextCtrl::OnDropFiles not implemented" );
405 long wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
407 if (!(m_windowStyle
& wxTE_MULTILINE
))
409 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
410 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
418 for (int i
= 0; i
< pos
; i
++ )
432 long wxTextCtrl::XYToPosition(long x
, long y
) const
434 if (!(m_windowStyle
& wxTE_MULTILINE
)) return 0;
438 for( int i
=1; i
<y
; i
++ ) pos
+= GetLineLength(i
);
440 pos
+=x
-1; // Pos start with 0
444 int wxTextCtrl::GetLineLength(long lineNo
) const
446 wxString str
= GetLineText (lineNo
);
447 return (int) str
.Length();
450 int wxTextCtrl::GetNumberOfLines() const
452 if (m_windowStyle
& wxTE_MULTILINE
)
454 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
455 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
460 for (int i
= 0; i
< len
; i
++ )
479 void wxTextCtrl::SetInsertionPoint( long pos
)
481 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
484 if (m_windowStyle
& wxTE_MULTILINE
)
485 gtk_text_set_point( GTK_TEXT(m_text
), tmp
);
487 gtk_entry_set_position( GTK_ENTRY(m_text
), tmp
);
490 void wxTextCtrl::SetInsertionPointEnd()
492 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
495 if (m_windowStyle
& wxTE_MULTILINE
)
496 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
498 pos
= GTK_ENTRY(m_text
)->text_length
;
500 SetInsertionPoint((pos
-1)>0 ? (pos
-1):0);
503 void wxTextCtrl::SetEditable( bool editable
)
505 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
507 if (m_windowStyle
& wxTE_MULTILINE
)
508 gtk_text_set_editable( GTK_TEXT(m_text
), editable
);
510 gtk_entry_set_editable( GTK_ENTRY(m_text
), editable
);
513 void wxTextCtrl::SetSelection( long from
, long to
)
515 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
517 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
520 void wxTextCtrl::ShowPosition( long WXUNUSED(pos
) )
522 wxFAIL_MSG( "wxTextCtrl::ShowPosition not implemented" );
525 long wxTextCtrl::GetInsertionPoint() const
527 wxCHECK_MSG( m_text
!= NULL
, 0, "invalid text ctrl" );
529 return (long) GTK_EDITABLE(m_text
)->current_pos
;
532 long wxTextCtrl::GetLastPosition() const
534 wxCHECK_MSG( m_text
!= NULL
, 0, "invalid text ctrl" );
537 if (m_windowStyle
& wxTE_MULTILINE
)
538 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
540 pos
= GTK_ENTRY(m_text
)->text_length
;
545 void wxTextCtrl::Remove( long from
, long to
)
547 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
549 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
552 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
554 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
556 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
557 if (value
.IsNull()) return;
559 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &pos
);
562 void wxTextCtrl::Cut()
564 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
566 #if (GTK_MINOR_VERSION == 1)
567 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
) );
569 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
), 0 );
573 void wxTextCtrl::Copy()
575 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
577 #if (GTK_MINOR_VERSION == 1)
578 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
) );
580 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
), 0 );
584 void wxTextCtrl::Paste()
586 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
588 #if (GTK_MINOR_VERSION == 1)
589 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
) );
591 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
), 0 );
595 void wxTextCtrl::Clear()
600 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
602 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
604 if ((key_event
.KeyCode() == WXK_RETURN
) && (m_windowStyle
& wxPROCESS_ENTER
))
606 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
607 event
.SetEventObject(this);
608 if (GetEventHandler()->ProcessEvent(event
)) return;
610 else if (key_event
.KeyCode() == WXK_TAB
)
612 wxNavigationKeyEvent event
;
613 event
.SetDirection( key_event
.m_shiftDown
);
614 event
.SetWindowChange(FALSE
);
615 event
.SetEventObject(this);
617 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
);