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 void wxapp_install_idle_handler();
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 extern bool g_blockEventsOnDrag
;
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
45 gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
47 if (!win
->m_hasVMT
) return;
50 wxapp_install_idle_handler();
54 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, win
->GetId() );
55 event
.SetString( win
->GetValue() );
56 event
.SetEventObject( win
);
57 win
->GetEventHandler()->ProcessEvent( event
);
60 //-----------------------------------------------------------------------------
61 // "changed" from vertical scrollbar
62 //-----------------------------------------------------------------------------
65 gtk_scrollbar_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
67 if (!win
->m_hasVMT
) return;
70 wxapp_install_idle_handler();
72 win
->CalculateScrollbar();
75 //-----------------------------------------------------------------------------
77 //-----------------------------------------------------------------------------
79 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
,wxControl
)
81 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
82 EVT_CHAR(wxTextCtrl::OnChar
)
84 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
85 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
86 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
87 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
88 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
90 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
91 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
92 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
93 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
94 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
97 #if wxUSE_STD_IOSTREAM
98 wxTextCtrl::wxTextCtrl() : streambuf()
100 if (allocate()) setp(base(),ebuf());
105 wxTextCtrl::wxTextCtrl()
111 #if wxUSE_STD_IOSTREAM
112 wxTextCtrl::wxTextCtrl( wxWindow
*parent
, wxWindowID id
, const wxString
&value
,
113 const wxPoint
&pos
, const wxSize
&size
,
114 int style
, const wxValidator
& validator
, const wxString
&name
) : streambuf()
116 if (allocate()) setp(base(),ebuf());
119 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
122 wxTextCtrl::wxTextCtrl( wxWindow
*parent
, wxWindowID id
, const wxString
&value
,
123 const wxPoint
&pos
, const wxSize
&size
,
124 int style
, const wxValidator
& validator
, const wxString
&name
)
127 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
131 bool wxTextCtrl::Create( wxWindow
*parent
, wxWindowID id
, const wxString
&value
,
132 const wxPoint
&pos
, const wxSize
&size
,
133 int style
, const wxValidator
& validator
, const wxString
&name
)
136 m_acceptsFocus
= TRUE
;
138 PreCreation( parent
, id
, pos
, size
, style
, name
);
141 SetValidator( validator
);
142 #endif // wxUSE_VALIDATORS
144 m_vScrollbarVisible
= FALSE
;
146 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
149 /* a multi-line edit control: create a vertical scrollbar by default and
150 horizontal if requested */
151 bool bHasHScrollbar
= (style
& wxHSCROLL
) != 0;
153 /* create our control ... */
154 m_text
= gtk_text_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
156 /* ... and put into the upper left hand corner of the table */
157 m_widget
= gtk_table_new(bHasHScrollbar
? 2 : 1, 2, FALSE
);
158 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
159 gtk_table_attach( GTK_TABLE(m_widget
), m_text
, 0, 1, 0, 1,
160 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
161 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
164 /* always wrap words */
165 gtk_text_set_word_wrap( GTK_TEXT(m_text
), TRUE
);
167 /* put the horizontal scrollbar in the lower left hand corner */
170 GtkWidget
*hscrollbar
= gtk_hscrollbar_new(GTK_TEXT(m_text
)->hadj
);
171 GTK_WIDGET_UNSET_FLAGS( hscrollbar
, GTK_CAN_FOCUS
);
172 gtk_table_attach(GTK_TABLE(m_widget
), hscrollbar
, 0, 1, 1, 2,
173 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
),
176 gtk_widget_show(hscrollbar
);
178 #if (GTK_MINOR_VERSION > 0)
179 /* don't wrap lines, otherwise we wouldn't need the scrollbar */
180 gtk_text_set_line_wrap( GTK_TEXT(m_text
), FALSE
);
184 /* finally, put the vertical scrollbar in the upper right corner */
185 m_vScrollbar
= gtk_vscrollbar_new( GTK_TEXT(m_text
)->vadj
);
186 GTK_WIDGET_UNSET_FLAGS( m_vScrollbar
, GTK_CAN_FOCUS
);
187 gtk_table_attach(GTK_TABLE(m_widget
), m_vScrollbar
, 1, 2, 0, 1,
189 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
),
194 /* a single-line text control: no need for scrollbars */
196 m_text
= gtk_entry_new();
199 wxSize newSize
= size
;
200 if (newSize
.x
== -1) newSize
.x
= 80;
201 if (newSize
.y
== -1) newSize
.y
= 26;
202 SetSize( newSize
.x
, newSize
.y
);
204 m_parent
->DoAddChild( this );
209 gtk_widget_show(m_text
);
211 /* we want to be notified about text changes */
212 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
213 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
217 gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text
)->vadj
), "changed",
218 (GtkSignalFunc
) gtk_scrollbar_changed_callback
, (gpointer
) this );
221 if (!value
.IsEmpty())
225 #if GTK_MINOR_VERSION == 0
226 // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in
227 // gtk_editable_insert_text()
228 gtk_widget_realize(m_text
);
232 wxWX2MBbuf val
= value
.mbc_str();
233 gtk_editable_insert_text( GTK_EDITABLE(m_text
), val
, strlen(val
), &tmp
);
235 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &tmp
);
236 #endif // Unicode/!Unicode
240 /* bring editable's cursor uptodate. bug in GTK. */
242 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
246 if (style
& wxTE_PASSWORD
)
249 gtk_entry_set_visibility( GTK_ENTRY(m_text
), FALSE
);
252 if (style
& wxTE_READONLY
)
255 gtk_entry_set_editable( GTK_ENTRY(m_text
), FALSE
);
260 gtk_text_set_editable( GTK_TEXT(m_text
), 1 );
263 SetBackgroundColour( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
) );
264 SetForegroundColour( parent
->GetForegroundColour() );
271 void wxTextCtrl::CalculateScrollbar()
273 if ((m_windowStyle
& wxTE_MULTILINE
) == 0) return;
275 GtkAdjustment
*adj
= GTK_TEXT(m_text
)->vadj
;
277 if (adj
->upper
- adj
->page_size
< 0.8)
279 if (m_vScrollbarVisible
)
281 gtk_widget_hide( m_vScrollbar
);
282 m_vScrollbarVisible
= FALSE
;
287 if (!m_vScrollbarVisible
)
289 gtk_widget_show( m_vScrollbar
);
290 m_vScrollbarVisible
= TRUE
;
295 wxString
wxTextCtrl::GetValue() const
297 wxCHECK_MSG( m_text
!= NULL
, _T(""), _T("invalid text ctrl") );
300 if (m_windowStyle
& wxTE_MULTILINE
)
302 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
303 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
304 tmp
= wxString(text
,*wxConvCurrent
);
309 tmp
= wxString(gtk_entry_get_text( GTK_ENTRY(m_text
) ),*wxConvCurrent
);
314 void wxTextCtrl::SetValue( const wxString
&value
)
316 wxCHECK_RET( m_text
!= NULL
, _T("invalid text ctrl") );
318 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
319 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
321 wxString tmp
= _T("");
322 if (!value
.IsNull()) tmp
= value
;
323 if (m_windowStyle
& wxTE_MULTILINE
)
325 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
326 gtk_editable_delete_text( GTK_EDITABLE(m_text
), 0, len
);
329 wxWX2MBbuf tmpbuf
= tmp
.mbc_str();
330 gtk_editable_insert_text( GTK_EDITABLE(m_text
), tmpbuf
, strlen(tmpbuf
), &len
);
332 gtk_editable_insert_text( GTK_EDITABLE(m_text
), tmp
.mbc_str(), tmp
.Length(), &len
);
337 gtk_entry_set_text( GTK_ENTRY(m_text
), tmp
.mbc_str() );
340 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
341 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
344 void wxTextCtrl::WriteText( const wxString
&text
)
346 wxCHECK_RET( m_text
!= NULL
, _T("invalid text ctrl") );
348 if (text
.IsEmpty()) return;
350 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
351 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
353 if (m_windowStyle
& wxTE_MULTILINE
)
355 /* this moves the cursor pos to behind the inserted text */
356 gint len
= GTK_EDITABLE(m_text
)->current_pos
;
359 wxWX2MBbuf buf
= text
.mbc_str();
360 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buf
, strlen(buf
), &len
);
362 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
, text
.Length(), &len
);
365 /* bring editable's cursor uptodate. bug in GTK. */
366 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
370 /* this moves the cursor pos to behind the inserted text */
371 gint len
= GTK_EDITABLE(m_text
)->current_pos
;
373 wxWX2MBbuf buf
= text
.mbc_str();
374 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buf
, strlen(buf
), &len
);
376 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
, text
.Length(), &len
);
379 /* bring editable's cursor uptodate. bug in GTK. */
380 GTK_EDITABLE(m_text
)->current_pos
+= text
.Len();
382 /* bring entry's cursor uptodate. bug in GTK. */
383 gtk_entry_set_position( GTK_ENTRY(m_text
), GTK_EDITABLE(m_text
)->current_pos
);
386 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
387 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
390 void wxTextCtrl::AppendText( const wxString
&text
)
392 wxCHECK_RET( m_text
!= NULL
, _T("invalid text ctrl") );
394 if (text
.IsEmpty()) return;
396 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
397 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
399 if (m_windowStyle
& wxTE_MULTILINE
)
401 /* we'll insert at the last position */
402 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
404 wxWX2MBbuf buf
= text
.mbc_str();
405 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buf
, strlen(buf
), &len
);
407 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
, text
.Length(), &len
);
410 /* bring editable's cursor uptodate. bug in GTK. */
411 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
415 gtk_entry_append_text( GTK_ENTRY(m_text
), text
.mbc_str() );
418 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
419 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
422 bool wxTextCtrl::LoadFile( const wxString
&file
)
424 wxCHECK_MSG( m_text
!= NULL
, FALSE
, _T("invalid text ctrl") );
426 if (!wxFileExists(file
)) return FALSE
;
430 FILE *fp
= (FILE*) NULL
;
433 if ((stat (FNSTRINGCAST file
.fn_str(), &statb
) == -1) || (statb
.st_mode
& S_IFMT
) != S_IFREG
||
434 !(fp
= fopen (FNSTRINGCAST file
.fn_str(), "r")))
440 gint len
= statb
.st_size
;
442 if (!(text
= (char*)malloc ((unsigned) (len
+ 1))))
447 if (fread (text
, sizeof (char), len
, fp
) != (size_t) len
)
454 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
455 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
457 if (m_windowStyle
& wxTE_MULTILINE
)
460 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
, len
, &pos
);
464 gtk_entry_set_text( GTK_ENTRY(m_text
), text
);
467 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
468 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
477 bool wxTextCtrl::SaveFile( const wxString
&file
)
479 wxCHECK_MSG( m_text
!= NULL
, FALSE
, _T("invalid text ctrl") );
481 if (file
== _T("")) return FALSE
;
485 if (!(fp
= fopen (FNSTRINGCAST file
.fn_str(), "w")))
491 char *text
= (char*) NULL
;
494 if (m_windowStyle
& wxTE_MULTILINE
)
496 len
= gtk_text_get_length( GTK_TEXT(m_text
) );
497 text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
501 text
= gtk_entry_get_text( GTK_ENTRY(m_text
) );
504 if (fwrite (text
, sizeof (char), len
, fp
) != (size_t) len
)
506 // Did not write whole file
509 // Make sure newline terminates the file
510 if (text
[len
- 1] != '\n')
515 if (m_windowStyle
& wxTE_MULTILINE
) g_free( text
);
524 wxString
wxTextCtrl::GetLineText( long lineNo
) const
526 if (m_windowStyle
& wxTE_MULTILINE
)
528 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
529 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
533 wxString
buf(_T(""));
536 for (i
= 0; currentLine
!= lineNo
&& text
[i
]; i
++ )
541 for (j
= 0; text
[i
] && text
[i
] != '\n'; i
++, j
++ )
548 return wxEmptyString
;
552 if (lineNo
== 0) return GetValue();
553 return wxEmptyString
;
557 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
559 /* If you implement this, don't forget to update the documentation!
560 * (file docs/latex/wx/text.tex) */
561 wxFAIL_MSG( _T("wxTextCtrl::OnDropFiles not implemented") );
564 long wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
566 if ( m_windowStyle
& wxTE_MULTILINE
)
568 wxString text
= GetValue();
570 // cast to prevent warning. But pos really should've been unsigned.
571 if( (unsigned long)pos
> text
.Len() )
577 const wxChar
* stop
= text
.c_str() + pos
;
578 for ( const wxChar
*p
= text
.c_str(); p
< stop
; p
++ )
589 else // single line control
591 if ( pos
<= GTK_ENTRY(m_text
)->text_length
)
598 // index out of bounds
606 long wxTextCtrl::XYToPosition(long x
, long y
) const
608 if (!(m_windowStyle
& wxTE_MULTILINE
)) return 0;
611 for( int i
=0; i
<y
; i
++ ) pos
+= GetLineLength(i
) + 1; // one for '\n'
617 int wxTextCtrl::GetLineLength(long lineNo
) const
619 wxString str
= GetLineText (lineNo
);
620 return (int) str
.Length();
623 int wxTextCtrl::GetNumberOfLines() const
625 if (m_windowStyle
& wxTE_MULTILINE
)
627 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
628 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
633 for (int i
= 0; i
< len
; i
++ )
640 // currentLine is 0 based, add 1 to get number of lines
641 return currentLine
+ 1;
654 void wxTextCtrl::SetInsertionPoint( long pos
)
656 wxCHECK_RET( m_text
!= NULL
, _T("invalid text ctrl") );
658 if (m_windowStyle
& wxTE_MULTILINE
)
660 /* seems to be broken in GTK 1.0.X:
661 gtk_text_set_point( GTK_TEXT(m_text), (int)pos ); */
663 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
664 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
666 /* we fake a set_point by inserting and deleting. as the user
667 isn't supposed to get to know about thos non-sense, we
668 disconnect so that no events are sent to the user program. */
670 gint tmp
= (gint
)pos
;
671 gtk_editable_insert_text( GTK_EDITABLE(m_text
), " ", 1, &tmp
);
672 gtk_editable_delete_text( GTK_EDITABLE(m_text
), tmp
-1, tmp
);
674 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
675 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
677 /* bring editable's cursor uptodate. another bug in GTK. */
679 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
683 gtk_entry_set_position( GTK_ENTRY(m_text
), (int)pos
);
685 /* bring editable's cursor uptodate. bug in GTK. */
687 GTK_EDITABLE(m_text
)->current_pos
= pos
;
691 void wxTextCtrl::SetInsertionPointEnd()
693 wxCHECK_RET( m_text
!= NULL
, _T("invalid text ctrl") );
695 if (m_windowStyle
& wxTE_MULTILINE
)
696 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text
)));
698 gtk_entry_set_position( GTK_ENTRY(m_text
), -1 );
701 void wxTextCtrl::SetEditable( bool editable
)
703 wxCHECK_RET( m_text
!= NULL
, _T("invalid text ctrl") );
705 if (m_windowStyle
& wxTE_MULTILINE
)
706 gtk_text_set_editable( GTK_TEXT(m_text
), editable
);
708 gtk_entry_set_editable( GTK_ENTRY(m_text
), editable
);
711 void wxTextCtrl::SetSelection( long from
, long to
)
713 wxCHECK_RET( m_text
!= NULL
, _T("invalid text ctrl") );
715 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
718 void wxTextCtrl::ShowPosition( long WXUNUSED(pos
) )
720 // SetInsertionPoint( pos );
723 long wxTextCtrl::GetInsertionPoint() const
725 wxCHECK_MSG( m_text
!= NULL
, 0, _T("invalid text ctrl") );
727 return (long) GTK_EDITABLE(m_text
)->current_pos
;
730 long wxTextCtrl::GetLastPosition() const
732 wxCHECK_MSG( m_text
!= NULL
, 0, _T("invalid text ctrl") );
735 if (m_windowStyle
& wxTE_MULTILINE
)
736 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
738 pos
= GTK_ENTRY(m_text
)->text_length
;
743 void wxTextCtrl::Remove( long from
, long to
)
745 wxCHECK_RET( m_text
!= NULL
, _T("invalid text ctrl") );
747 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
748 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
750 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
752 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
753 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
756 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
758 wxCHECK_RET( m_text
!= NULL
, _T("invalid text ctrl") );
760 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
761 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
763 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
765 if (!value
.IsEmpty())
767 gint pos
= (gint
)from
;
769 wxWX2MBbuf buf
= value
.mbc_str();
770 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buf
, strlen(buf
), &pos
);
772 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &pos
);
776 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
777 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
780 void wxTextCtrl::Cut()
782 wxCHECK_RET( m_text
!= NULL
, _T("invalid text ctrl") );
784 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
785 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
787 #if (GTK_MINOR_VERSION > 0)
788 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
) );
790 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
), 0 );
793 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
794 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
797 void wxTextCtrl::Copy()
799 wxCHECK_RET( m_text
!= NULL
, _T("invalid text ctrl") );
801 #if (GTK_MINOR_VERSION > 0)
802 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
) );
804 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
), 0 );
808 void wxTextCtrl::Paste()
810 wxCHECK_RET( m_text
!= NULL
, _T("invalid text ctrl") );
812 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
813 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
815 #if (GTK_MINOR_VERSION > 0)
816 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
) );
818 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
), 0 );
821 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
822 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
825 bool wxTextCtrl::CanCopy() const
827 // Can copy if there's a selection
829 GetSelection(& from
, & to
);
830 return (from
!= to
) ;
833 bool wxTextCtrl::CanCut() const
835 // Can cut if there's a selection
837 GetSelection(& from
, & to
);
838 return (from
!= to
) ;
841 bool wxTextCtrl::CanPaste() const
843 return IsEditable() ;
847 void wxTextCtrl::Undo()
850 wxFAIL_MSG( _T("wxTextCtrl::Undo not implemented") );
853 void wxTextCtrl::Redo()
856 wxFAIL_MSG( _T("wxTextCtrl::Redo not implemented") );
859 bool wxTextCtrl::CanUndo() const
862 wxFAIL_MSG( _T("wxTextCtrl::CanUndo not implemented") );
866 bool wxTextCtrl::CanRedo() const
869 wxFAIL_MSG( _T("wxTextCtrl::CanRedo not implemented") );
873 // If the return values from and to are the same, there is no
875 void wxTextCtrl::GetSelection(long* from
, long* to
) const
877 wxCHECK_RET( m_text
!= NULL
, _T("invalid text ctrl") );
879 if (!(GTK_EDITABLE(m_text
)->has_selection
))
886 if (from
) *from
= (long) GTK_EDITABLE(m_text
)->selection_start_pos
;
887 if (to
) *to
= (long) GTK_EDITABLE(m_text
)->selection_end_pos
;
890 bool wxTextCtrl::IsEditable() const
892 wxCHECK_MSG( m_text
!= NULL
, FALSE
, _T("invalid text ctrl") );
894 return GTK_EDITABLE(m_text
)->editable
;
897 void wxTextCtrl::Clear()
902 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
904 wxCHECK_RET( m_text
!= NULL
, _T("invalid text ctrl") );
906 if ((key_event
.KeyCode() == WXK_RETURN
) && (m_windowStyle
& wxPROCESS_ENTER
))
908 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
909 event
.SetEventObject(this);
910 if (GetEventHandler()->ProcessEvent(event
)) return;
916 #if wxUSE_STD_IOSTREAM
917 int wxTextCtrl::overflow( int WXUNUSED(c
) )
919 int len
= pptr() - pbase();
920 char *txt
= new char[len
+1];
921 strncpy(txt
, pbase(), len
);
924 setp(pbase(), epptr());
929 int wxTextCtrl::sync()
931 int len
= pptr() - pbase();
932 char *txt
= new char[len
+1];
933 strncpy(txt
, pbase(), len
);
936 setp(pbase(), epptr());
941 int wxTextCtrl::underflow()
947 wxTextCtrl
& wxTextCtrl::operator<<(const wxString
& s
)
953 wxTextCtrl
& wxTextCtrl::operator<<(float f
)
955 static char buf
[100];
956 sprintf(buf
, "%.2f", f
);
961 wxTextCtrl
& wxTextCtrl::operator<<(double d
)
963 static char buf
[100];
964 sprintf(buf
, "%.2f", d
);
969 wxTextCtrl
& wxTextCtrl::operator<<(int i
)
971 static char buf
[100];
972 sprintf(buf
, "%i", i
);
977 wxTextCtrl
& wxTextCtrl::operator<<(long i
)
979 static char buf
[100];
980 sprintf(buf
, "%ld", i
);
985 wxTextCtrl
& wxTextCtrl::operator<<(const char c
)
995 GtkWidget
* wxTextCtrl::GetConnectWidget()
997 return GTK_WIDGET(m_text
);
1000 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow
*window
)
1002 if (m_windowStyle
& wxTE_MULTILINE
)
1003 return (window
== GTK_TEXT(m_text
)->text_area
);
1005 return (window
== GTK_ENTRY(m_text
)->text_area
);
1008 bool wxTextCtrl::SetFont( const wxFont
&WXUNUSED(font
) )
1010 wxCHECK_MSG( m_text
!= NULL
, FALSE
, _T("invalid text ctrl") );
1016 bool wxTextCtrl::SetForegroundColour( const wxColour
&WXUNUSED(colour
) )
1018 wxCHECK_MSG( m_text
!= NULL
, FALSE
, _T("invalid text ctrl") );
1024 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
1026 wxCHECK_MSG( m_text
!= NULL
, FALSE
, _T("invalid text ctrl") );
1028 wxControl::SetBackgroundColour( colour
);
1030 if (!m_widget
->window
)
1033 wxColour sysbg
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE
);
1034 if (sysbg
.Red() == colour
.Red() &&
1035 sysbg
.Green() == colour
.Green() &&
1036 sysbg
.Blue() == colour
.Blue())
1038 return FALSE
; // FIXME or TRUE?
1041 if (!m_backgroundColour
.Ok())
1044 if (m_windowStyle
& wxTE_MULTILINE
)
1046 GdkWindow
*window
= GTK_TEXT(m_text
)->text_area
;
1049 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
1050 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
1051 gdk_window_clear( window
);
1057 void wxTextCtrl::ApplyWidgetStyle()
1059 if (m_windowStyle
& wxTE_MULTILINE
)
1066 gtk_widget_set_style( m_text
, m_widgetStyle
);
1070 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1075 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1080 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1085 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1090 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1095 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1097 event
.Enable( CanCut() );
1100 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1102 event
.Enable( CanCopy() );
1105 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1107 event
.Enable( CanPaste() );
1110 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1112 event
.Enable( CanUndo() );
1115 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1117 event
.Enable( CanRedo() );