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"
18 #include "wx/settings.h"
21 #include <sys/types.h>
24 #include <math.h> // for fabs
28 #include "gdk/gdkkeysyms.h"
30 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
34 extern void wxapp_install_idle_handler();
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
41 extern bool g_blockEventsOnDrag
;
42 extern wxCursor g_globalCursor
;
44 // ----------------------------------------------------------------------------
45 // "insert_text" for GtkEntry
46 // ----------------------------------------------------------------------------
49 gtk_insert_text_callback(GtkEditable
*editable
,
50 const gchar
*new_text
,
56 wxapp_install_idle_handler();
58 // we should only be called if we have a max len limit at all
59 GtkEntry
*entry
= GTK_ENTRY (editable
);
61 wxCHECK_RET( entry
->text_max_length
, _T("shouldn't be called") );
63 // check that we don't overflow the max length limit
65 // FIXME: this doesn't work when we paste a string which is going to be
67 if ( entry
->text_length
== entry
->text_max_length
)
69 // we don't need to run the base class version at all
70 gtk_signal_emit_stop_by_name(GTK_OBJECT(editable
), "insert_text");
72 // remember that the next changed signal is to be ignored to avoid
73 // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event
74 win
->IgnoreNextTextUpdate();
76 // and generate the correct one ourselves
77 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, win
->GetId());
78 event
.SetEventObject(win
);
79 event
.SetString(win
->GetValue());
80 win
->GetEventHandler()->ProcessEvent( event
);
84 //-----------------------------------------------------------------------------
86 //-----------------------------------------------------------------------------
89 gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
91 if ( win
->IgnoreTextUpdate() )
94 if (!win
->m_hasVMT
) return;
97 wxapp_install_idle_handler();
100 win
->UpdateFontIfNeeded();
102 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, win
->GetId() );
103 event
.SetEventObject( win
);
104 event
.SetString( win
->GetValue() );
105 win
->GetEventHandler()->ProcessEvent( event
);
108 //-----------------------------------------------------------------------------
109 // "changed" from vertical scrollbar
110 //-----------------------------------------------------------------------------
113 gtk_scrollbar_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
115 if (!win
->m_hasVMT
) return;
118 wxapp_install_idle_handler();
120 win
->CalculateScrollbar();
123 //-----------------------------------------------------------------------------
125 //-----------------------------------------------------------------------------
127 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
,wxControl
)
129 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
130 EVT_CHAR(wxTextCtrl::OnChar
)
132 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
133 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
134 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
135 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
136 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
138 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
139 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
140 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
141 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
142 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
145 void wxTextCtrl::Init()
149 m_updateFont
= FALSE
;
151 m_vScrollbar
= (GtkWidget
*)NULL
;
154 wxTextCtrl::wxTextCtrl( wxWindow
*parent
,
156 const wxString
&value
,
160 const wxValidator
& validator
,
161 const wxString
&name
)
165 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
168 bool wxTextCtrl::Create( wxWindow
*parent
,
170 const wxString
&value
,
174 const wxValidator
& validator
,
175 const wxString
&name
)
178 m_acceptsFocus
= TRUE
;
180 if (!PreCreation( parent
, pos
, size
) ||
181 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
183 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
188 m_vScrollbarVisible
= FALSE
;
190 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
193 #if (GTK_MINOR_VERSION > 2)
194 /* a multi-line edit control: create a vertical scrollbar by default and
195 horizontal if requested */
196 bool bHasHScrollbar
= (style
& wxHSCROLL
) != 0;
198 bool bHasHScrollbar
= FALSE
;
201 /* create our control ... */
202 m_text
= gtk_text_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
204 /* ... and put into the upper left hand corner of the table */
205 m_widget
= gtk_table_new(bHasHScrollbar
? 2 : 1, 2, FALSE
);
206 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
207 gtk_table_attach( GTK_TABLE(m_widget
), m_text
, 0, 1, 0, 1,
208 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
209 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
212 /* always wrap words */
213 gtk_text_set_word_wrap( GTK_TEXT(m_text
), TRUE
);
215 #if (GTK_MINOR_VERSION > 2)
216 /* put the horizontal scrollbar in the lower left hand corner */
219 GtkWidget
*hscrollbar
= gtk_hscrollbar_new(GTK_TEXT(m_text
)->hadj
);
220 GTK_WIDGET_UNSET_FLAGS( hscrollbar
, GTK_CAN_FOCUS
);
221 gtk_table_attach(GTK_TABLE(m_widget
), hscrollbar
, 0, 1, 1, 2,
222 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
),
225 gtk_widget_show(hscrollbar
);
227 /* don't wrap lines, otherwise we wouldn't need the scrollbar */
228 gtk_text_set_line_wrap( GTK_TEXT(m_text
), FALSE
);
232 /* finally, put the vertical scrollbar in the upper right corner */
233 m_vScrollbar
= gtk_vscrollbar_new( GTK_TEXT(m_text
)->vadj
);
234 GTK_WIDGET_UNSET_FLAGS( m_vScrollbar
, GTK_CAN_FOCUS
);
235 gtk_table_attach(GTK_TABLE(m_widget
), m_vScrollbar
, 1, 2, 0, 1,
237 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
),
242 /* a single-line text control: no need for scrollbars */
244 m_text
= gtk_entry_new();
247 m_parent
->DoAddChild( this );
249 m_focusWidget
= m_text
;
253 SetFont( parent
->GetFont() );
255 wxSize
size_best( DoGetBestSize() );
256 wxSize
new_size( size
);
257 if (new_size
.x
== -1)
258 new_size
.x
= size_best
.x
;
259 if (new_size
.y
== -1)
260 new_size
.y
= size_best
.y
;
261 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
262 SetSize( new_size
.x
, new_size
.y
);
265 gtk_widget_show(m_text
);
269 gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text
)->vadj
), "changed",
270 (GtkSignalFunc
) gtk_scrollbar_changed_callback
, (gpointer
) this );
273 if (!value
.IsEmpty())
277 #if GTK_MINOR_VERSION == 0
278 // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in
279 // gtk_editable_insert_text()
280 gtk_widget_realize(m_text
);
284 wxWX2MBbuf val
= value
.mbc_str();
285 gtk_editable_insert_text( GTK_EDITABLE(m_text
), val
, strlen(val
), &tmp
);
287 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &tmp
);
288 #endif // Unicode/!Unicode
292 /* bring editable's cursor uptodate. bug in GTK. */
294 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
298 if (style
& wxTE_PASSWORD
)
301 gtk_entry_set_visibility( GTK_ENTRY(m_text
), FALSE
);
304 if (style
& wxTE_READONLY
)
307 gtk_entry_set_editable( GTK_ENTRY(m_text
), FALSE
);
312 gtk_text_set_editable( GTK_TEXT(m_text
), 1 );
315 /* we want to be notified about text changes */
316 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
317 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
319 /* we don't set a valid background colour, because the window
320 manager should use a default one */
321 m_backgroundColour
= wxColour();
323 wxColour colFg
= parent
->GetForegroundColour();
324 SetForegroundColour( colFg
);
326 m_cursor
= wxCursor( wxCURSOR_IBEAM
);
328 // FIXME: is the bg colour correct here?
329 wxTextAttr
attrDef( colFg
,
330 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
),
332 SetDefaultStyle( attrDef
);
339 void wxTextCtrl::CalculateScrollbar()
341 if ((m_windowStyle
& wxTE_MULTILINE
) == 0) return;
343 GtkAdjustment
*adj
= GTK_TEXT(m_text
)->vadj
;
345 if (adj
->upper
- adj
->page_size
< 0.8)
347 if (m_vScrollbarVisible
)
349 gtk_widget_hide( m_vScrollbar
);
350 m_vScrollbarVisible
= FALSE
;
355 if (!m_vScrollbarVisible
)
357 gtk_widget_show( m_vScrollbar
);
358 m_vScrollbarVisible
= TRUE
;
363 wxString
wxTextCtrl::GetValue() const
365 wxCHECK_MSG( m_text
!= NULL
, wxT(""), wxT("invalid text ctrl") );
368 if (m_windowStyle
& wxTE_MULTILINE
)
370 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
371 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
372 tmp
= wxString(text
,*wxConvCurrent
);
377 tmp
= wxString(gtk_entry_get_text( GTK_ENTRY(m_text
) ),*wxConvCurrent
);
382 void wxTextCtrl::SetValue( const wxString
&value
)
384 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
386 if (m_windowStyle
& wxTE_MULTILINE
)
388 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
389 gtk_editable_delete_text( GTK_EDITABLE(m_text
), 0, len
);
392 wxWX2MBbuf tmpbuf
= value
.mbc_str();
393 gtk_editable_insert_text( GTK_EDITABLE(m_text
), tmpbuf
, strlen(tmpbuf
), &len
);
395 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
.mbc_str(), value
.Length(), &len
);
400 gtk_entry_set_text( GTK_ENTRY(m_text
), value
.mbc_str() );
403 // GRG, Jun/2000: Changed this after a lot of discussion in
404 // the lists. wxWindows 2.2 will have a set of flags to
405 // customize this behaviour.
406 SetInsertionPoint(0);
411 void wxTextCtrl::WriteText( const wxString
&text
)
413 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
419 wxWX2MBbuf buf
= text
.mbc_str();
420 const char *txt
= buf
;
421 size_t txtlen
= strlen(buf
);
423 const char *txt
= text
;
424 size_t txtlen
= text
.length();
427 if ( m_windowStyle
& wxTE_MULTILINE
)
429 /* this moves the cursor pos to behind the inserted text */
430 gint len
= GTK_EDITABLE(m_text
)->current_pos
;
432 // if we have any special style, use it
433 if ( !m_defaultStyle
.IsDefault() )
435 GdkFont
*font
= m_defaultStyle
.HasFont()
436 ? m_defaultStyle
.GetFont().GetInternalFont()
439 GdkColor
*colFg
= m_defaultStyle
.HasTextColour()
440 ? m_defaultStyle
.GetTextColour().GetColor()
443 GdkColor
*colBg
= m_defaultStyle
.HasBackgroundColour()
444 ? m_defaultStyle
.GetBackgroundColour().GetColor()
447 gtk_text_insert( GTK_TEXT(m_text
), font
, colFg
, colBg
, txt
, txtlen
);
451 gtk_editable_insert_text( GTK_EDITABLE(m_text
), txt
, txtlen
, &len
);
454 /* bring editable's cursor uptodate. bug in GTK. */
455 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
459 /* this moves the cursor pos to behind the inserted text */
460 gint len
= GTK_EDITABLE(m_text
)->current_pos
;
461 gtk_editable_insert_text( GTK_EDITABLE(m_text
), txt
, txtlen
, &len
);
463 /* bring editable's cursor uptodate. bug in GTK. */
464 GTK_EDITABLE(m_text
)->current_pos
+= text
.Len();
466 /* bring entry's cursor uptodate. bug in GTK. */
467 gtk_entry_set_position( GTK_ENTRY(m_text
), GTK_EDITABLE(m_text
)->current_pos
);
471 void wxTextCtrl::AppendText( const wxString
&text
)
473 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
479 wxWX2MBbuf buf
= text
.mbc_str();
480 const char *txt
= buf
;
481 size_t txtlen
= strlen(buf
);
483 const char *txt
= text
;
484 size_t txtlen
= text
.length();
487 if (m_windowStyle
& wxTE_MULTILINE
)
489 if ( !m_defaultStyle
.IsDefault() )
491 wxFont font
= m_defaultStyle
.HasFont() ? m_defaultStyle
.GetFont()
493 GdkFont
*fnt
= font
.Ok() ? font
.GetInternalFont() : NULL
;
495 wxColour col
= m_defaultStyle
.HasTextColour()
496 ? m_defaultStyle
.GetTextColour()
497 : m_foregroundColour
;
498 GdkColor
*colFg
= col
.Ok() ? col
.GetColor() : NULL
;
500 col
= m_defaultStyle
.HasBackgroundColour()
501 ? m_defaultStyle
.GetBackgroundColour()
502 : m_backgroundColour
;
503 GdkColor
*colBg
= col
.Ok() ? col
.GetColor() : NULL
;
505 gtk_text_insert( GTK_TEXT(m_text
), fnt
, colFg
, colBg
, txt
, txtlen
);
509 /* we'll insert at the last position */
510 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
511 gtk_editable_insert_text( GTK_EDITABLE(m_text
), txt
, txtlen
, &len
);
514 /* bring editable's cursor uptodate. bug in GTK. */
515 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
519 gtk_entry_append_text( GTK_ENTRY(m_text
), txt
);
523 wxString
wxTextCtrl::GetLineText( long lineNo
) const
525 if (m_windowStyle
& wxTE_MULTILINE
)
527 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
528 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
532 wxString
buf(wxT(""));
535 for (i
= 0; currentLine
!= lineNo
&& text
[i
]; i
++ )
540 for (j
= 0; text
[i
] && text
[i
] != '\n'; i
++, j
++ )
547 return wxEmptyString
;
551 if (lineNo
== 0) return GetValue();
552 return wxEmptyString
;
556 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
558 /* If you implement this, don't forget to update the documentation!
559 * (file docs/latex/wx/text.tex) */
560 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
563 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
565 if ( m_windowStyle
& wxTE_MULTILINE
)
567 wxString text
= GetValue();
569 // cast to prevent warning. But pos really should've been unsigned.
570 if( (unsigned long)pos
> text
.Len() )
576 const wxChar
* stop
= text
.c_str() + pos
;
577 for ( const wxChar
*p
= text
.c_str(); p
< stop
; p
++ )
588 else // single line control
590 if ( pos
<= GTK_ENTRY(m_text
)->text_length
)
597 // index out of bounds
605 long wxTextCtrl::XYToPosition(long x
, long y
) const
607 if (!(m_windowStyle
& wxTE_MULTILINE
)) return 0;
610 for( int i
=0; i
<y
; i
++ ) pos
+= GetLineLength(i
) + 1; // one for '\n'
616 int wxTextCtrl::GetLineLength(long lineNo
) const
618 wxString str
= GetLineText (lineNo
);
619 return (int) str
.Length();
622 int wxTextCtrl::GetNumberOfLines() const
624 if (m_windowStyle
& wxTE_MULTILINE
)
626 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
627 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
632 for (int i
= 0; i
< len
; i
++ )
639 // currentLine is 0 based, add 1 to get number of lines
640 return currentLine
+ 1;
653 void wxTextCtrl::SetInsertionPoint( long pos
)
655 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
657 if (m_windowStyle
& wxTE_MULTILINE
)
659 /* seems to be broken in GTK 1.0.X:
660 gtk_text_set_point( GTK_TEXT(m_text), (int)pos ); */
662 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
663 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
665 /* we fake a set_point by inserting and deleting. as the user
666 isn't supposed to get to know about thos non-sense, we
667 disconnect so that no events are sent to the user program. */
669 gint tmp
= (gint
)pos
;
670 gtk_editable_insert_text( GTK_EDITABLE(m_text
), " ", 1, &tmp
);
671 gtk_editable_delete_text( GTK_EDITABLE(m_text
), tmp
-1, tmp
);
673 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
674 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
676 /* bring editable's cursor uptodate. another bug in GTK. */
678 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
682 gtk_entry_set_position( GTK_ENTRY(m_text
), (int)pos
);
684 /* bring editable's cursor uptodate. bug in GTK. */
686 GTK_EDITABLE(m_text
)->current_pos
= (guint32
)pos
;
690 void wxTextCtrl::SetInsertionPointEnd()
692 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
694 if (m_windowStyle
& wxTE_MULTILINE
)
695 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text
)));
697 gtk_entry_set_position( GTK_ENTRY(m_text
), -1 );
700 void wxTextCtrl::SetEditable( bool editable
)
702 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
704 if (m_windowStyle
& wxTE_MULTILINE
)
705 gtk_text_set_editable( GTK_TEXT(m_text
), editable
);
707 gtk_entry_set_editable( GTK_ENTRY(m_text
), editable
);
710 bool wxTextCtrl::Enable( bool enable
)
712 if (!wxWindowBase::Enable(enable
))
718 if (m_windowStyle
& wxTE_MULTILINE
)
720 gtk_text_set_editable( GTK_TEXT(m_text
), enable
);
721 OnParentEnable(enable
);
725 gtk_widget_set_sensitive( m_text
, enable
);
731 // wxGTK-specific: called recursively by Enable,
732 // to give widgets an oppprtunity to correct their colours after they
733 // have been changed by Enable
734 void wxTextCtrl::OnParentEnable( bool enable
)
736 // If we have a custom background colour, we use this colour in both
737 // disabled and enabled mode, or we end up with a different colour under the
739 wxColour oldColour
= GetBackgroundColour();
742 // Need to set twice or it'll optimize the useful stuff out
743 if (oldColour
== * wxWHITE
)
744 SetBackgroundColour(*wxBLACK
);
746 SetBackgroundColour(*wxWHITE
);
747 SetBackgroundColour(oldColour
);
751 void wxTextCtrl::DiscardEdits()
756 // ----------------------------------------------------------------------------
757 // max text length support
758 // ----------------------------------------------------------------------------
760 void wxTextCtrl::IgnoreNextTextUpdate()
762 m_ignoreNextUpdate
= TRUE
;
765 bool wxTextCtrl::IgnoreTextUpdate()
767 if ( m_ignoreNextUpdate
)
769 m_ignoreNextUpdate
= FALSE
;
777 void wxTextCtrl::SetMaxLength(unsigned long len
)
779 if ( !HasFlag(wxTE_MULTILINE
) )
781 gtk_entry_set_max_length(GTK_ENTRY(m_text
), len
);
783 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
784 // we had tried to enter more text than allowed by max text length and
785 // the text wasn't really changed
787 // to detect this and generate TEXT_MAXLEN event instead of
788 // TEXT_CHANGED one in this case we also catch "insert_text" signal
790 // when max len is set to 0 we disconnect our handler as it means that
791 // we shouldn't check anything any more
794 gtk_signal_connect( GTK_OBJECT(m_text
),
796 GTK_SIGNAL_FUNC(gtk_insert_text_callback
),
801 gtk_signal_disconnect_by_func
804 GTK_SIGNAL_FUNC(gtk_insert_text_callback
),
811 void wxTextCtrl::SetSelection( long from
, long to
)
813 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
815 if ( (m_windowStyle
& wxTE_MULTILINE
) &&
816 !GTK_TEXT(m_text
)->line_start_cache
)
818 // tell the programmer that it didn't work
819 wxLogDebug(_T("Can't call SetSelection() before realizing the control"));
823 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
826 void wxTextCtrl::ShowPosition( long WXUNUSED(pos
) )
828 // SetInsertionPoint( pos );
831 long wxTextCtrl::GetInsertionPoint() const
833 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
835 return (long) GTK_EDITABLE(m_text
)->current_pos
;
838 long wxTextCtrl::GetLastPosition() const
840 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
843 if (m_windowStyle
& wxTE_MULTILINE
)
844 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
846 pos
= GTK_ENTRY(m_text
)->text_length
;
851 void wxTextCtrl::Remove( long from
, long to
)
853 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
855 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
858 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
860 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
862 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
864 if (!value
.IsEmpty())
866 gint pos
= (gint
)from
;
868 wxWX2MBbuf buf
= value
.mbc_str();
869 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buf
, strlen(buf
), &pos
);
871 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &pos
);
876 void wxTextCtrl::Cut()
878 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
880 #if (GTK_MINOR_VERSION > 0)
881 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
) );
883 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
), 0 );
887 void wxTextCtrl::Copy()
889 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
891 #if (GTK_MINOR_VERSION > 0)
892 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
) );
894 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
), 0 );
898 void wxTextCtrl::Paste()
900 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
902 #if (GTK_MINOR_VERSION > 0)
903 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
) );
905 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
), 0 );
910 void wxTextCtrl::Undo()
913 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
916 void wxTextCtrl::Redo()
919 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
922 bool wxTextCtrl::CanUndo() const
925 wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
929 bool wxTextCtrl::CanRedo() const
932 wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
936 // If the return values from and to are the same, there is no
938 void wxTextCtrl::GetSelection(long* fromOut
, long* toOut
) const
940 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
943 if ( !(GTK_EDITABLE(m_text
)->has_selection
) )
946 to
= GetInsertionPoint();
948 else // got selection
950 from
= (long) GTK_EDITABLE(m_text
)->selection_start_pos
;
951 to
= (long) GTK_EDITABLE(m_text
)->selection_end_pos
;
955 // exchange them to be compatible with wxMSW
968 bool wxTextCtrl::IsEditable() const
970 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
972 return GTK_EDITABLE(m_text
)->editable
;
975 bool wxTextCtrl::IsModified() const
980 void wxTextCtrl::Clear()
985 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
987 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
989 if ((key_event
.KeyCode() == WXK_RETURN
) && (m_windowStyle
& wxPROCESS_ENTER
))
991 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
992 event
.SetEventObject(this);
993 event
.SetString(GetValue());
994 if (GetEventHandler()->ProcessEvent(event
)) return;
997 if ((key_event
.KeyCode() == WXK_RETURN
) && !(m_windowStyle
& wxTE_MULTILINE
))
999 wxWindow
*top_frame
= m_parent
;
1000 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
1001 top_frame
= top_frame
->GetParent();
1002 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
1004 if (window
->default_widget
)
1006 gtk_widget_activate (window
->default_widget
);
1014 GtkWidget
* wxTextCtrl::GetConnectWidget()
1016 return GTK_WIDGET(m_text
);
1019 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow
*window
)
1021 if (m_windowStyle
& wxTE_MULTILINE
)
1022 return (window
== GTK_TEXT(m_text
)->text_area
);
1024 return (window
== GTK_ENTRY(m_text
)->text_area
);
1027 // the font will change for subsequent text insertiongs
1028 bool wxTextCtrl::SetFont( const wxFont
&font
)
1030 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
1032 if ( !wxWindowBase::SetFont(font
) )
1034 // font didn't change, nothing to do
1038 if ( m_windowStyle
& wxTE_MULTILINE
)
1040 m_updateFont
= TRUE
;
1042 m_defaultStyle
.SetFont(font
);
1044 ChangeFontGlobally();
1050 void wxTextCtrl::ChangeFontGlobally()
1052 // this method is very inefficient and hence should be called as rarely as
1054 wxASSERT_MSG( (m_windowStyle
& wxTE_MULTILINE
) && m_updateFont
,
1055 _T("shouldn't be called for single line controls") );
1057 wxString value
= GetValue();
1058 if ( !value
.IsEmpty() )
1060 m_updateFont
= FALSE
;
1067 void wxTextCtrl::UpdateFontIfNeeded()
1070 ChangeFontGlobally();
1073 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1075 if ( !wxControl::SetForegroundColour(colour
) )
1078 // update default fg colour too
1079 m_defaultStyle
.SetTextColour(colour
);
1084 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
1086 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
1088 if ( !wxControl::SetBackgroundColour( colour
) )
1091 if (!m_widget
->window
)
1094 wxColour sysbg
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE
);
1095 if (sysbg
.Red() == colour
.Red() &&
1096 sysbg
.Green() == colour
.Green() &&
1097 sysbg
.Blue() == colour
.Blue())
1099 return FALSE
; // FIXME or TRUE?
1102 if (!m_backgroundColour
.Ok())
1105 if (m_windowStyle
& wxTE_MULTILINE
)
1107 GdkWindow
*window
= GTK_TEXT(m_text
)->text_area
;
1110 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
1111 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
1112 gdk_window_clear( window
);
1115 // change active background color too
1116 m_defaultStyle
.SetBackgroundColour( colour
);
1121 bool wxTextCtrl::SetStyle( long start
, long end
, const wxTextAttr
&style
)
1123 /* VERY dirty way to do that - removes the required text and re-adds it
1124 with styling (FIXME) */
1125 if ( m_windowStyle
& wxTE_MULTILINE
)
1127 if ( style
.IsDefault() )
1133 gint l
= gtk_text_get_length( GTK_TEXT(m_text
) );
1135 wxCHECK_MSG( start
>= 0 && end
<= l
, FALSE
,
1136 _T("invalid range in wxTextCtrl::SetStyle") );
1138 gint old_pos
= gtk_editable_get_position( GTK_EDITABLE(m_text
) );
1139 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), start
, end
);
1140 wxString
tmp(text
,*wxConvCurrent
);
1143 gtk_editable_delete_text( GTK_EDITABLE(m_text
), start
, end
);
1144 gtk_editable_set_position( GTK_EDITABLE(m_text
), start
);
1147 wxWX2MBbuf buf
= tmp
.mbc_str();
1148 const char *txt
= buf
;
1149 size_t txtlen
= strlen(buf
);
1151 const char *txt
= tmp
;
1152 size_t txtlen
= tmp
.length();
1155 GdkFont
*font
= style
.HasFont()
1156 ? style
.GetFont().GetInternalFont()
1159 GdkColor
*colFg
= style
.HasTextColour()
1160 ? style
.GetTextColour().GetColor()
1163 GdkColor
*colBg
= style
.HasBackgroundColour()
1164 ? style
.GetBackgroundColour().GetColor()
1167 gtk_text_insert( GTK_TEXT(m_text
), font
, colFg
, colBg
, txt
, txtlen
);
1169 /* does not seem to help under GTK+ 1.2 !!!
1170 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
1171 SetInsertionPoint( old_pos
);
1176 // cannot do this for GTK+'s Entry widget
1181 void wxTextCtrl::ApplyWidgetStyle()
1183 if (m_windowStyle
& wxTE_MULTILINE
)
1190 gtk_widget_set_style( m_text
, m_widgetStyle
);
1194 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1199 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1204 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1209 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1214 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1219 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1221 event
.Enable( CanCut() );
1224 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1226 event
.Enable( CanCopy() );
1229 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1231 event
.Enable( CanPaste() );
1234 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1236 event
.Enable( CanUndo() );
1239 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1241 event
.Enable( CanRedo() );
1244 void wxTextCtrl::OnInternalIdle()
1246 wxCursor cursor
= m_cursor
;
1247 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
1251 GdkWindow
*window
= (GdkWindow
*) NULL
;
1252 if (HasFlag(wxTE_MULTILINE
))
1253 window
= GTK_TEXT(m_text
)->text_area
;
1255 window
= GTK_ENTRY(m_text
)->text_area
;
1258 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1260 if (!g_globalCursor
.Ok())
1261 cursor
= *wxSTANDARD_CURSOR
;
1263 window
= m_widget
->window
;
1264 if ((window
) && !(GTK_WIDGET_NO_WINDOW(m_widget
)))
1265 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1271 wxSize
wxTextCtrl::DoGetBestSize() const
1273 // FIXME should be different for multi-line controls...
1274 wxSize
ret( wxControl::DoGetBestSize() );
1275 return wxSize(80, ret
.y
);
1278 // ----------------------------------------------------------------------------
1280 // ----------------------------------------------------------------------------
1282 void wxTextCtrl::Freeze()
1284 if ( HasFlag(wxTE_MULTILINE
) )
1286 gtk_text_freeze(GTK_TEXT(m_text
));
1290 void wxTextCtrl::Thaw()
1292 if ( HasFlag(wxTE_MULTILINE
) )
1294 GTK_TEXT(m_text
)->vadj
->value
= 0.0;
1296 gtk_text_thaw(GTK_TEXT(m_text
));
1300 // ----------------------------------------------------------------------------
1302 // ----------------------------------------------------------------------------
1304 GtkAdjustment
*wxTextCtrl::GetVAdj() const
1306 return HasFlag(wxTE_MULTILINE
) ? GTK_TEXT(m_text
)->vadj
: NULL
;
1309 bool wxTextCtrl::DoScroll(GtkAdjustment
*adj
, int diff
)
1311 float value
= adj
->value
+ diff
;
1316 float upper
= adj
->upper
- adj
->page_size
;
1317 if ( value
> upper
)
1320 // did we noticeably change the scroll position?
1321 if ( fabs(adj
->value
- value
) < 0.2 )
1323 // well, this is what Robert does in wxScrollBar, so it must be good...
1329 gtk_signal_emit_by_name(GTK_OBJECT(adj
), "value_changed");
1334 bool wxTextCtrl::ScrollLines(int lines
)
1336 GtkAdjustment
*adj
= GetVAdj();
1340 // this is hardcoded to 10 in GTK+ 1.2 (great idea)
1341 static const int KEY_SCROLL_PIXELS
= 10;
1343 return DoScroll(adj
, lines
*KEY_SCROLL_PIXELS
);
1346 bool wxTextCtrl::ScrollPages(int pages
)
1348 GtkAdjustment
*adj
= GetVAdj();
1352 return DoScroll(adj
, (int)ceil(pages
*adj
->page_increment
));