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 #if (GTK_MINOR_VERSION > 2)
150 /* a multi-line edit control: create a vertical scrollbar by default and
151 horizontal if requested */
152 bool bHasHScrollbar
= (style
& wxHSCROLL
) != 0;
154 bool bHasHScrollbar
= FALSE
;
157 /* create our control ... */
158 m_text
= gtk_text_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
160 /* ... and put into the upper left hand corner of the table */
161 m_widget
= gtk_table_new(bHasHScrollbar
? 2 : 1, 2, FALSE
);
162 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
163 gtk_table_attach( GTK_TABLE(m_widget
), m_text
, 0, 1, 0, 1,
164 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
165 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
168 /* always wrap words */
169 gtk_text_set_word_wrap( GTK_TEXT(m_text
), TRUE
);
171 #if (GTK_MINOR_VERSION > 2)
172 /* put the horizontal scrollbar in the lower left hand corner */
175 GtkWidget
*hscrollbar
= gtk_hscrollbar_new(GTK_TEXT(m_text
)->hadj
);
176 GTK_WIDGET_UNSET_FLAGS( hscrollbar
, GTK_CAN_FOCUS
);
177 gtk_table_attach(GTK_TABLE(m_widget
), hscrollbar
, 0, 1, 1, 2,
178 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
),
181 gtk_widget_show(hscrollbar
);
183 /* don't wrap lines, otherwise we wouldn't need the scrollbar */
184 gtk_text_set_line_wrap( GTK_TEXT(m_text
), FALSE
);
188 /* finally, put the vertical scrollbar in the upper right corner */
189 m_vScrollbar
= gtk_vscrollbar_new( GTK_TEXT(m_text
)->vadj
);
190 GTK_WIDGET_UNSET_FLAGS( m_vScrollbar
, GTK_CAN_FOCUS
);
191 gtk_table_attach(GTK_TABLE(m_widget
), m_vScrollbar
, 1, 2, 0, 1,
193 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
),
198 /* a single-line text control: no need for scrollbars */
200 m_text
= gtk_entry_new();
203 wxSize newSize
= size
;
204 if (newSize
.x
== -1) newSize
.x
= 80;
205 if (newSize
.y
== -1) newSize
.y
= 26;
206 SetSize( newSize
.x
, newSize
.y
);
208 m_parent
->DoAddChild( this );
213 gtk_widget_show(m_text
);
215 /* we want to be notified about text changes */
216 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
217 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
221 gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text
)->vadj
), "changed",
222 (GtkSignalFunc
) gtk_scrollbar_changed_callback
, (gpointer
) this );
225 if (!value
.IsEmpty())
229 #if GTK_MINOR_VERSION == 0
230 // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in
231 // gtk_editable_insert_text()
232 gtk_widget_realize(m_text
);
236 wxWX2MBbuf val
= value
.mbc_str();
237 gtk_editable_insert_text( GTK_EDITABLE(m_text
), val
, strlen(val
), &tmp
);
239 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &tmp
);
240 #endif // Unicode/!Unicode
244 /* bring editable's cursor uptodate. bug in GTK. */
246 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
250 if (style
& wxTE_PASSWORD
)
253 gtk_entry_set_visibility( GTK_ENTRY(m_text
), FALSE
);
256 if (style
& wxTE_READONLY
)
259 gtk_entry_set_editable( GTK_ENTRY(m_text
), FALSE
);
264 gtk_text_set_editable( GTK_TEXT(m_text
), 1 );
267 SetBackgroundColour( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
) );
268 SetForegroundColour( parent
->GetForegroundColour() );
275 void wxTextCtrl::CalculateScrollbar()
277 if ((m_windowStyle
& wxTE_MULTILINE
) == 0) return;
279 GtkAdjustment
*adj
= GTK_TEXT(m_text
)->vadj
;
281 if (adj
->upper
- adj
->page_size
< 0.8)
283 if (m_vScrollbarVisible
)
285 gtk_widget_hide( m_vScrollbar
);
286 m_vScrollbarVisible
= FALSE
;
291 if (!m_vScrollbarVisible
)
293 gtk_widget_show( m_vScrollbar
);
294 m_vScrollbarVisible
= TRUE
;
299 wxString
wxTextCtrl::GetValue() const
301 wxCHECK_MSG( m_text
!= NULL
, _T(""), _T("invalid text ctrl") );
304 if (m_windowStyle
& wxTE_MULTILINE
)
306 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
307 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
308 tmp
= wxString(text
,*wxConvCurrent
);
313 tmp
= wxString(gtk_entry_get_text( GTK_ENTRY(m_text
) ),*wxConvCurrent
);
318 void wxTextCtrl::SetValue( const wxString
&value
)
320 wxCHECK_RET( m_text
!= NULL
, _T("invalid text ctrl") );
322 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
323 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
325 wxString tmp
= _T("");
326 if (!value
.IsNull()) tmp
= value
;
327 if (m_windowStyle
& wxTE_MULTILINE
)
329 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
330 gtk_editable_delete_text( GTK_EDITABLE(m_text
), 0, len
);
333 wxWX2MBbuf tmpbuf
= tmp
.mbc_str();
334 gtk_editable_insert_text( GTK_EDITABLE(m_text
), tmpbuf
, strlen(tmpbuf
), &len
);
336 gtk_editable_insert_text( GTK_EDITABLE(m_text
), tmp
.mbc_str(), tmp
.Length(), &len
);
341 gtk_entry_set_text( GTK_ENTRY(m_text
), tmp
.mbc_str() );
344 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
345 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
348 void wxTextCtrl::WriteText( const wxString
&text
)
350 wxCHECK_RET( m_text
!= NULL
, _T("invalid text ctrl") );
352 if (text
.IsEmpty()) return;
354 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
355 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
357 if (m_windowStyle
& wxTE_MULTILINE
)
359 /* this moves the cursor pos to behind the inserted text */
360 gint len
= GTK_EDITABLE(m_text
)->current_pos
;
363 wxWX2MBbuf buf
= text
.mbc_str();
364 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buf
, strlen(buf
), &len
);
366 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
, text
.Length(), &len
);
369 /* bring editable's cursor uptodate. bug in GTK. */
370 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
374 /* this moves the cursor pos to behind the inserted text */
375 gint len
= GTK_EDITABLE(m_text
)->current_pos
;
377 wxWX2MBbuf buf
= text
.mbc_str();
378 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buf
, strlen(buf
), &len
);
380 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
, text
.Length(), &len
);
383 /* bring editable's cursor uptodate. bug in GTK. */
384 GTK_EDITABLE(m_text
)->current_pos
+= text
.Len();
386 /* bring entry's cursor uptodate. bug in GTK. */
387 gtk_entry_set_position( GTK_ENTRY(m_text
), GTK_EDITABLE(m_text
)->current_pos
);
390 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
391 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
394 void wxTextCtrl::AppendText( const wxString
&text
)
396 wxCHECK_RET( m_text
!= NULL
, _T("invalid text ctrl") );
398 if (text
.IsEmpty()) return;
400 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
401 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
403 if (m_windowStyle
& wxTE_MULTILINE
)
405 bool hasSpecialAttributes
= m_font
.Ok() ||
406 m_foregroundColour
.Ok() ||
407 m_backgroundColour
.Ok();
408 if ( hasSpecialAttributes
)
410 gtk_text_insert( GTK_TEXT(m_text
),
411 m_font
.GetInternalFont(),
412 m_foregroundColour
.GetColor(),
413 m_backgroundColour
.GetColor(),
414 text
, text
.length());
419 /* we'll insert at the last position */
420 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
422 wxWX2MBbuf buf
= text
.mbc_str();
423 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buf
, strlen(buf
), &len
);
425 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
, text
.Length(), &len
);
429 /* bring editable's cursor uptodate. bug in GTK. */
430 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
434 gtk_entry_append_text( GTK_ENTRY(m_text
), text
.mbc_str() );
437 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
438 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
441 bool wxTextCtrl::LoadFile( const wxString
&file
)
443 wxCHECK_MSG( m_text
!= NULL
, FALSE
, _T("invalid text ctrl") );
445 if (!wxFileExists(file
)) return FALSE
;
449 FILE *fp
= (FILE*) NULL
;
452 if ((stat (FNSTRINGCAST file
.fn_str(), &statb
) == -1) || (statb
.st_mode
& S_IFMT
) != S_IFREG
||
453 !(fp
= fopen (FNSTRINGCAST file
.fn_str(), "r")))
459 gint len
= statb
.st_size
;
461 if (!(text
= (char*)malloc ((unsigned) (len
+ 1))))
466 if (fread (text
, sizeof (char), len
, fp
) != (size_t) len
)
473 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
474 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
476 if (m_windowStyle
& wxTE_MULTILINE
)
479 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
, len
, &pos
);
483 gtk_entry_set_text( GTK_ENTRY(m_text
), text
);
486 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
487 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
496 bool wxTextCtrl::SaveFile( const wxString
&file
)
498 wxCHECK_MSG( m_text
!= NULL
, FALSE
, _T("invalid text ctrl") );
500 if (file
== _T("")) return FALSE
;
504 if (!(fp
= fopen (FNSTRINGCAST file
.fn_str(), "w")))
510 char *text
= (char*) NULL
;
513 if (m_windowStyle
& wxTE_MULTILINE
)
515 len
= gtk_text_get_length( GTK_TEXT(m_text
) );
516 text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
520 text
= gtk_entry_get_text( GTK_ENTRY(m_text
) );
523 if (fwrite (text
, sizeof (char), len
, fp
) != (size_t) len
)
525 // Did not write whole file
528 // Make sure newline terminates the file
529 if (text
[len
- 1] != '\n')
534 if (m_windowStyle
& wxTE_MULTILINE
) g_free( text
);
543 wxString
wxTextCtrl::GetLineText( long lineNo
) const
545 if (m_windowStyle
& wxTE_MULTILINE
)
547 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
548 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
552 wxString
buf(_T(""));
555 for (i
= 0; currentLine
!= lineNo
&& text
[i
]; i
++ )
560 for (j
= 0; text
[i
] && text
[i
] != '\n'; i
++, j
++ )
567 return wxEmptyString
;
571 if (lineNo
== 0) return GetValue();
572 return wxEmptyString
;
576 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
578 /* If you implement this, don't forget to update the documentation!
579 * (file docs/latex/wx/text.tex) */
580 wxFAIL_MSG( _T("wxTextCtrl::OnDropFiles not implemented") );
583 long wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
585 if ( m_windowStyle
& wxTE_MULTILINE
)
587 wxString text
= GetValue();
589 // cast to prevent warning. But pos really should've been unsigned.
590 if( (unsigned long)pos
> text
.Len() )
596 const wxChar
* stop
= text
.c_str() + pos
;
597 for ( const wxChar
*p
= text
.c_str(); p
< stop
; p
++ )
608 else // single line control
610 if ( pos
<= GTK_ENTRY(m_text
)->text_length
)
617 // index out of bounds
625 long wxTextCtrl::XYToPosition(long x
, long y
) const
627 if (!(m_windowStyle
& wxTE_MULTILINE
)) return 0;
630 for( int i
=0; i
<y
; i
++ ) pos
+= GetLineLength(i
) + 1; // one for '\n'
636 int wxTextCtrl::GetLineLength(long lineNo
) const
638 wxString str
= GetLineText (lineNo
);
639 return (int) str
.Length();
642 int wxTextCtrl::GetNumberOfLines() const
644 if (m_windowStyle
& wxTE_MULTILINE
)
646 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
647 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
652 for (int i
= 0; i
< len
; i
++ )
659 // currentLine is 0 based, add 1 to get number of lines
660 return currentLine
+ 1;
673 void wxTextCtrl::SetInsertionPoint( long pos
)
675 wxCHECK_RET( m_text
!= NULL
, _T("invalid text ctrl") );
677 if (m_windowStyle
& wxTE_MULTILINE
)
679 /* seems to be broken in GTK 1.0.X:
680 gtk_text_set_point( GTK_TEXT(m_text), (int)pos ); */
682 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
683 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
685 /* we fake a set_point by inserting and deleting. as the user
686 isn't supposed to get to know about thos non-sense, we
687 disconnect so that no events are sent to the user program. */
689 gint tmp
= (gint
)pos
;
690 gtk_editable_insert_text( GTK_EDITABLE(m_text
), " ", 1, &tmp
);
691 gtk_editable_delete_text( GTK_EDITABLE(m_text
), tmp
-1, tmp
);
693 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
694 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
696 /* bring editable's cursor uptodate. another bug in GTK. */
698 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
702 gtk_entry_set_position( GTK_ENTRY(m_text
), (int)pos
);
704 /* bring editable's cursor uptodate. bug in GTK. */
706 GTK_EDITABLE(m_text
)->current_pos
= pos
;
710 void wxTextCtrl::SetInsertionPointEnd()
712 wxCHECK_RET( m_text
!= NULL
, _T("invalid text ctrl") );
714 if (m_windowStyle
& wxTE_MULTILINE
)
715 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text
)));
717 gtk_entry_set_position( GTK_ENTRY(m_text
), -1 );
720 void wxTextCtrl::SetEditable( bool editable
)
722 wxCHECK_RET( m_text
!= NULL
, _T("invalid text ctrl") );
724 if (m_windowStyle
& wxTE_MULTILINE
)
725 gtk_text_set_editable( GTK_TEXT(m_text
), editable
);
727 gtk_entry_set_editable( GTK_ENTRY(m_text
), editable
);
730 void wxTextCtrl::SetSelection( long from
, long to
)
732 wxCHECK_RET( m_text
!= NULL
, _T("invalid text ctrl") );
734 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
737 void wxTextCtrl::ShowPosition( long WXUNUSED(pos
) )
739 // SetInsertionPoint( pos );
742 long wxTextCtrl::GetInsertionPoint() const
744 wxCHECK_MSG( m_text
!= NULL
, 0, _T("invalid text ctrl") );
746 return (long) GTK_EDITABLE(m_text
)->current_pos
;
749 long wxTextCtrl::GetLastPosition() const
751 wxCHECK_MSG( m_text
!= NULL
, 0, _T("invalid text ctrl") );
754 if (m_windowStyle
& wxTE_MULTILINE
)
755 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
757 pos
= GTK_ENTRY(m_text
)->text_length
;
762 void wxTextCtrl::Remove( long from
, long to
)
764 wxCHECK_RET( m_text
!= NULL
, _T("invalid text ctrl") );
766 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
767 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
769 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
771 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
772 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
775 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
777 wxCHECK_RET( m_text
!= NULL
, _T("invalid text ctrl") );
779 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
780 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
782 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
784 if (!value
.IsEmpty())
786 gint pos
= (gint
)from
;
788 wxWX2MBbuf buf
= value
.mbc_str();
789 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buf
, strlen(buf
), &pos
);
791 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &pos
);
795 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
796 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
799 void wxTextCtrl::Cut()
801 wxCHECK_RET( m_text
!= NULL
, _T("invalid text ctrl") );
803 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
804 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
806 #if (GTK_MINOR_VERSION > 0)
807 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
) );
809 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
), 0 );
812 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
813 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
816 void wxTextCtrl::Copy()
818 wxCHECK_RET( m_text
!= NULL
, _T("invalid text ctrl") );
820 #if (GTK_MINOR_VERSION > 0)
821 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
) );
823 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
), 0 );
827 void wxTextCtrl::Paste()
829 wxCHECK_RET( m_text
!= NULL
, _T("invalid text ctrl") );
831 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
832 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
834 #if (GTK_MINOR_VERSION > 0)
835 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
) );
837 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
), 0 );
840 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
841 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
844 bool wxTextCtrl::CanCopy() const
846 // Can copy if there's a selection
848 GetSelection(& from
, & to
);
849 return (from
!= to
) ;
852 bool wxTextCtrl::CanCut() const
854 // Can cut if there's a selection
856 GetSelection(& from
, & to
);
857 return (from
!= to
) ;
860 bool wxTextCtrl::CanPaste() const
862 return IsEditable() ;
866 void wxTextCtrl::Undo()
869 wxFAIL_MSG( _T("wxTextCtrl::Undo not implemented") );
872 void wxTextCtrl::Redo()
875 wxFAIL_MSG( _T("wxTextCtrl::Redo not implemented") );
878 bool wxTextCtrl::CanUndo() const
881 wxFAIL_MSG( _T("wxTextCtrl::CanUndo not implemented") );
885 bool wxTextCtrl::CanRedo() const
888 wxFAIL_MSG( _T("wxTextCtrl::CanRedo not implemented") );
892 // If the return values from and to are the same, there is no
894 void wxTextCtrl::GetSelection(long* from
, long* to
) const
896 wxCHECK_RET( m_text
!= NULL
, _T("invalid text ctrl") );
898 if (!(GTK_EDITABLE(m_text
)->has_selection
))
905 if (from
) *from
= (long) GTK_EDITABLE(m_text
)->selection_start_pos
;
906 if (to
) *to
= (long) GTK_EDITABLE(m_text
)->selection_end_pos
;
909 bool wxTextCtrl::IsEditable() const
911 wxCHECK_MSG( m_text
!= NULL
, FALSE
, _T("invalid text ctrl") );
913 return GTK_EDITABLE(m_text
)->editable
;
916 void wxTextCtrl::Clear()
921 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
923 wxCHECK_RET( m_text
!= NULL
, _T("invalid text ctrl") );
925 if ((key_event
.KeyCode() == WXK_RETURN
) && (m_windowStyle
& wxPROCESS_ENTER
))
927 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
928 event
.SetEventObject(this);
929 if (GetEventHandler()->ProcessEvent(event
)) return;
935 GtkWidget
* wxTextCtrl::GetConnectWidget()
937 return GTK_WIDGET(m_text
);
940 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow
*window
)
942 if (m_windowStyle
& wxTE_MULTILINE
)
943 return (window
== GTK_TEXT(m_text
)->text_area
);
945 return (window
== GTK_ENTRY(m_text
)->text_area
);
948 // the font will change for subsequent text insertiongs
949 bool wxTextCtrl::SetFont( const wxFont
&font
)
951 wxCHECK_MSG( m_text
!= NULL
, FALSE
, _T("invalid text ctrl") );
953 if ( !wxWindowBase::SetFont(font
) )
955 // font didn't change, nothing to do
959 if ( m_windowStyle
& wxTE_MULTILINE
)
961 // for compatibility with other ports: the font is a global controls
962 // characteristic, so change the font globally
963 wxString value
= GetValue();
964 if ( !value
.IsEmpty() )
975 bool wxTextCtrl::SetForegroundColour( const wxColour
&WXUNUSED(colour
) )
977 wxCHECK_MSG( m_text
!= NULL
, FALSE
, _T("invalid text ctrl") );
983 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
985 wxCHECK_MSG( m_text
!= NULL
, FALSE
, _T("invalid text ctrl") );
987 wxControl::SetBackgroundColour( colour
);
989 if (!m_widget
->window
)
992 wxColour sysbg
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE
);
993 if (sysbg
.Red() == colour
.Red() &&
994 sysbg
.Green() == colour
.Green() &&
995 sysbg
.Blue() == colour
.Blue())
997 return FALSE
; // FIXME or TRUE?
1000 if (!m_backgroundColour
.Ok())
1003 if (m_windowStyle
& wxTE_MULTILINE
)
1005 GdkWindow
*window
= GTK_TEXT(m_text
)->text_area
;
1008 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
1009 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
1010 gdk_window_clear( window
);
1016 void wxTextCtrl::ApplyWidgetStyle()
1018 if (m_windowStyle
& wxTE_MULTILINE
)
1025 gtk_widget_set_style( m_text
, m_widgetStyle
);
1029 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1034 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1039 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1044 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1049 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1054 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1056 event
.Enable( CanCut() );
1059 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1061 event
.Enable( CanCopy() );
1064 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1066 event
.Enable( CanPaste() );
1069 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1071 event
.Enable( CanUndo() );
1074 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1076 event
.Enable( CanRedo() );