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 //-----------------------------------------------------------------------------
27 static void gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
31 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, win
->m_windowId
);
32 wxString
val( win
->GetValue() );
33 if (!val
.IsNull()) event
.m_commandString
= WXSTRINGCAST val
;
34 event
.SetEventObject( win
);
35 win
->GetEventHandler()->ProcessEvent( event
);
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
42 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
,wxControl
)
44 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
45 EVT_CHAR(wxTextCtrl::OnChar
)
48 wxTextCtrl::wxTextCtrl() : streambuf()
50 if (allocate()) setp(base(),ebuf());
55 wxTextCtrl::wxTextCtrl( wxWindow
*parent
, wxWindowID id
, const wxString
&value
,
56 const wxPoint
&pos
, const wxSize
&size
,
57 int style
, const wxValidator
& validator
, const wxString
&name
) : streambuf()
59 if (allocate()) setp(base(),ebuf());
62 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
65 bool wxTextCtrl::Create( wxWindow
*parent
, wxWindowID id
, const wxString
&value
,
66 const wxPoint
&pos
, const wxSize
&size
,
67 int style
, const wxValidator
& validator
, const wxString
&name
)
71 PreCreation( parent
, id
, pos
, size
, style
, name
);
73 SetValidator( validator
);
75 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
78 // a multi-line edit control: create a vertical scrollbar by default and
79 // horizontal if requested
80 bool bHasHScrollbar
= (style
& wxHSCROLL
) != 0;
82 // create our control...
83 m_text
= gtk_text_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
85 // ... and put into the upper left hand corner of the table
86 m_widget
= gtk_table_new(bHasHScrollbar
? 2 : 1, 2, FALSE
);
87 gtk_table_attach( GTK_TABLE(m_widget
), m_text
, 0, 1, 0, 1,
88 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
89 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
92 // put the horizontal scrollbar in the lower left hand corner
95 GtkWidget
*hscrollbar
= gtk_hscrollbar_new(GTK_TEXT(m_text
)->hadj
);
96 gtk_table_attach(GTK_TABLE(m_widget
), hscrollbar
, 0, 1, 1, 2,
97 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
),
100 gtk_widget_show(hscrollbar
);
103 // finally, put the vertical scrollbar in the upper right corner
104 GtkWidget
*vscrollbar
= gtk_vscrollbar_new(GTK_TEXT(m_text
)->vadj
);
105 gtk_table_attach(GTK_TABLE(m_widget
), vscrollbar
, 1, 2, 0, 1,
107 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
),
109 gtk_widget_show( vscrollbar
);
113 // a single-line text control: no need for scrollbars
115 m_text
= gtk_entry_new();
118 wxSize newSize
= size
;
119 if (newSize
.x
== -1) newSize
.x
= 80;
120 if (newSize
.y
== -1) newSize
.y
= 26;
121 SetSize( newSize
.x
, newSize
.y
);
123 m_parent
->AddChild( this );
125 (m_parent
->m_insertCallback
)( m_parent
, this );
131 gtk_widget_realize(m_text
);
132 gtk_widget_show(m_text
);
135 // we want to be notified about text changes
136 gtk_signal_connect(GTK_OBJECT(m_text
), "changed",
137 GTK_SIGNAL_FUNC(gtk_text_changed_callback
),
143 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &tmp
);
144 SetInsertionPointEnd();
147 if (style
& wxTE_PASSWORD
)
150 gtk_entry_set_visibility( GTK_ENTRY(m_text
), FALSE
);
153 if (style
& wxTE_READONLY
)
156 gtk_entry_set_editable( GTK_ENTRY(m_text
), FALSE
);
161 gtk_text_set_editable( GTK_TEXT(m_text
), 1 );
166 SetBackgroundColour( parent
->GetBackgroundColour() );
167 SetForegroundColour( parent
->GetForegroundColour() );
172 wxString
wxTextCtrl::GetValue() const
174 wxCHECK_MSG( m_text
!= NULL
, "", "invalid text ctrl" );
177 if (m_windowStyle
& wxTE_MULTILINE
)
179 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
180 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
186 tmp
= gtk_entry_get_text( GTK_ENTRY(m_text
) );
191 void wxTextCtrl::SetValue( const wxString
&value
)
193 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
196 if (!value
.IsNull()) tmp
= value
;
197 if (m_windowStyle
& wxTE_MULTILINE
)
199 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
200 gtk_editable_delete_text( GTK_EDITABLE(m_text
), 0, len
);
202 gtk_editable_insert_text( GTK_EDITABLE(m_text
), tmp
, tmp
.Length(), &len
);
206 gtk_entry_set_text( GTK_ENTRY(m_text
), tmp
);
210 void wxTextCtrl::WriteText( const wxString
&text
)
212 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
214 if (text
.IsNull()) return;
216 if (m_windowStyle
& wxTE_MULTILINE
)
218 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
219 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
, text
.Length(), &len
);
223 gtk_entry_append_text( GTK_ENTRY(m_text
), text
);
227 bool wxTextCtrl::LoadFile( const wxString
&file
)
229 wxCHECK_MSG( m_text
!= NULL
, FALSE
, "invalid text ctrl" );
231 if (!wxFileExists(file
)) return FALSE
;
238 if ((stat ((char*) (const char*) file
, &statb
) == -1) || (statb
.st_mode
& S_IFMT
) != S_IFREG
||
239 !(fp
= fopen ((char*) (const char*) file
, "r")))
245 gint len
= statb
.st_size
;
247 if (!(text
= (char*)malloc ((unsigned) (len
+ 1))))
252 if (fread (text
, sizeof (char), len
, fp
) != (size_t) len
)
259 if (m_windowStyle
& wxTE_MULTILINE
)
261 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
, 0, &len
);
265 gtk_entry_set_text( GTK_ENTRY(m_text
), text
);
275 bool wxTextCtrl::SaveFile( const wxString
&file
)
277 wxCHECK_MSG( m_text
!= NULL
, FALSE
, "invalid text ctrl" );
279 if (file
== "") return FALSE
;
283 if (!(fp
= fopen ((char*) (const char*) file
, "w")))
292 if (m_windowStyle
& wxTE_MULTILINE
)
294 len
= gtk_text_get_length( GTK_TEXT(m_text
) );
295 text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
299 text
= gtk_entry_get_text( GTK_ENTRY(m_text
) );
302 if (fwrite (text
, sizeof (char), len
, fp
) != (size_t) len
)
304 // Did not write whole file
307 // Make sure newline terminates the file
308 if (text
[len
- 1] != '\n')
313 if (m_windowStyle
& wxTE_MULTILINE
) g_free( text
);
322 wxString
wxTextCtrl::GetLineText( long lineNo
) const
324 if (m_windowStyle
& wxTE_MULTILINE
)
326 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
327 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
334 for (i
= 0; currentLine
!= lineNo
&& text
[i
]; i
++ )
339 for (j
= 0; text
[i
] && text
[i
] != '\n'; i
++, j
++ )
346 return wxEmptyString
;
350 if (lineNo
== 0) return GetValue();
351 return wxEmptyString
;
355 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
357 wxFAIL_MSG( "wxTextCtrl::OnDropFiles not implemented" );
360 long wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
362 if (!(m_windowStyle
& wxTE_MULTILINE
))
364 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
365 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
373 for (int i
= 0; i
< pos
; i
++ )
387 long wxTextCtrl::XYToPosition(long x
, long y
) const
389 if (!(m_windowStyle
& wxTE_MULTILINE
))
394 pos
+=GetLineLength(i
);
395 pos
+=x
-1; // Pos start with 0
399 int wxTextCtrl::GetLineLength(long lineNo
) const
401 wxString str
= GetLineText (lineNo
);
402 return (int) str
.Length();
405 int wxTextCtrl::GetNumberOfLines() 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
);
415 for (int i
= 0; i
< len
; i
++ )
431 void wxTextCtrl::SetInsertionPoint( long pos
)
433 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
436 if (m_windowStyle
& wxTE_MULTILINE
)
437 gtk_text_set_point( GTK_TEXT(m_text
), tmp
);
439 gtk_entry_set_position( GTK_ENTRY(m_text
), tmp
);
442 void wxTextCtrl::SetInsertionPointEnd()
444 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
447 if (m_windowStyle
& wxTE_MULTILINE
)
448 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
450 pos
= GTK_ENTRY(m_text
)->text_length
;
451 SetInsertionPoint((pos
-1)>0 ? (pos
-1):0);
454 void wxTextCtrl::SetEditable( bool editable
)
456 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
458 if (m_windowStyle
& wxTE_MULTILINE
)
459 gtk_text_set_editable( GTK_TEXT(m_text
), editable
);
461 gtk_entry_set_editable( GTK_ENTRY(m_text
), editable
);
464 void wxTextCtrl::SetSelection( long from
, long to
)
466 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
468 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
471 void wxTextCtrl::ShowPosition( long WXUNUSED(pos
) )
473 wxFAIL_MSG( "wxTextCtrl::ShowPosition not implemented" );
476 long wxTextCtrl::GetInsertionPoint() const
478 wxCHECK_MSG( m_text
!= NULL
, 0, "invalid text ctrl" );
480 return (long) GTK_EDITABLE(m_text
)->current_pos
;
483 long wxTextCtrl::GetLastPosition() const
485 wxCHECK_MSG( m_text
!= NULL
, 0, "invalid text ctrl" );
488 if (m_windowStyle
& wxTE_MULTILINE
)
489 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
491 pos
= GTK_ENTRY(m_text
)->text_length
;
495 void wxTextCtrl::Remove( long from
, long to
)
497 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
499 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
502 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
504 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
506 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
507 if (value
.IsNull()) return;
509 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &pos
);
512 void wxTextCtrl::Cut()
514 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
516 #if (GTK_MINOR_VERSION == 1)
517 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
) );
519 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
), 0 );
523 void wxTextCtrl::Copy()
525 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
527 #if (GTK_MINOR_VERSION == 1)
528 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
) );
530 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
), 0 );
534 void wxTextCtrl::Paste()
536 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
538 #if (GTK_MINOR_VERSION == 1)
539 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
) );
541 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
), 0 );
545 void wxTextCtrl::Clear()
550 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
552 if ((key_event
.KeyCode() == WXK_RETURN
) && (m_windowStyle
& wxPROCESS_ENTER
))
554 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
555 event
.SetEventObject(this);
556 if (GetEventHandler()->ProcessEvent(event
)) return;
558 else if (key_event
.KeyCode() == WXK_TAB
)
560 wxNavigationKeyEvent event
;
561 event
.SetDirection( key_event
.m_shiftDown
);
562 event
.SetWindowChange(FALSE
);
563 event
.SetEventObject(this);
565 if (GetEventHandler()->ProcessEvent(event
)) return;
570 int wxTextCtrl::overflow( int WXUNUSED(c
) )
572 int len
= pptr() - pbase();
573 char *txt
= new char[len
+1];
574 strncpy(txt
, pbase(), len
);
577 setp(pbase(), epptr());
582 int wxTextCtrl::sync()
584 int len
= pptr() - pbase();
585 char *txt
= new char[len
+1];
586 strncpy(txt
, pbase(), len
);
589 setp(pbase(), epptr());
594 int wxTextCtrl::underflow()
599 wxTextCtrl
& wxTextCtrl::operator<<(const wxString
& s
)
605 wxTextCtrl
& wxTextCtrl::operator<<(float f
)
607 static char buf
[100];
608 sprintf(buf
, "%.2f", f
);
613 wxTextCtrl
& wxTextCtrl::operator<<(double d
)
615 static char buf
[100];
616 sprintf(buf
, "%.2f", d
);
621 wxTextCtrl
& wxTextCtrl::operator<<(int i
)
623 static char buf
[100];
624 sprintf(buf
, "%i", i
);
629 wxTextCtrl
& wxTextCtrl::operator<<(long i
)
631 static char buf
[100];
632 sprintf(buf
, "%ld", i
);
637 wxTextCtrl
& wxTextCtrl::operator<<(const char c
)
647 GtkWidget
* wxTextCtrl::GetConnectWidget()
649 return GTK_WIDGET(m_text
);
652 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow
*window
)
654 if (m_windowStyle
& wxTE_MULTILINE
)
655 return (window
== GTK_TEXT(m_text
)->text_area
);
657 return (window
== GTK_ENTRY(m_text
)->text_area
);
660 void wxTextCtrl::SetFont( const wxFont
&WXUNUSED(font
) )
662 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
667 void wxTextCtrl::SetForegroundColour( const wxColour
&WXUNUSED(colour
) )
669 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
674 void wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
676 wxCHECK_RET( m_text
!= NULL
, "invalid text ctrl" );
678 wxControl::SetBackgroundColour( colour
);
680 wxColour sysbg
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE
);
681 if (sysbg
.Red() == colour
.Red() &&
682 sysbg
.Green() == colour
.Green() &&
683 sysbg
.Blue() == colour
.Blue())
688 if (!m_backgroundColour
.Ok()) return;
690 if (m_windowStyle
& wxTE_MULTILINE
)
692 GdkWindow
*window
= GTK_TEXT(m_text
)->text_area
;
693 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
694 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
695 gdk_window_clear( window
);
699 void wxTextCtrl::ApplyWidgetStyle()
701 if (m_windowStyle
& wxTE_MULTILINE
)
707 gtk_widget_set_style( m_text
, m_widgetStyle
);