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 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 static void wxGtkTextInsert(GtkWidget
*text
,
49 const wxTextAttr
& attr
,
53 GdkFont
*font
= attr
.HasFont() ? attr
.GetFont().GetInternalFont()
56 GdkColor
*colFg
= attr
.HasTextColour() ? attr
.GetTextColour().GetColor()
59 GdkColor
*colBg
= attr
.HasBackgroundColour()
60 ? attr
.GetBackgroundColour().GetColor()
63 gtk_text_insert( GTK_TEXT(text
), font
, colFg
, colBg
, txt
, len
);
66 // ----------------------------------------------------------------------------
67 // "insert_text" for GtkEntry
68 // ----------------------------------------------------------------------------
71 gtk_insert_text_callback(GtkEditable
*editable
,
72 const gchar
*new_text
,
78 wxapp_install_idle_handler();
80 // we should only be called if we have a max len limit at all
81 GtkEntry
*entry
= GTK_ENTRY (editable
);
83 wxCHECK_RET( entry
->text_max_length
, _T("shouldn't be called") );
85 // check that we don't overflow the max length limit
87 // FIXME: this doesn't work when we paste a string which is going to be
89 if ( entry
->text_length
== entry
->text_max_length
)
91 // we don't need to run the base class version at all
92 gtk_signal_emit_stop_by_name(GTK_OBJECT(editable
), "insert_text");
94 // remember that the next changed signal is to be ignored to avoid
95 // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event
96 win
->IgnoreNextTextUpdate();
98 // and generate the correct one ourselves
99 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, win
->GetId());
100 event
.SetEventObject(win
);
101 event
.SetString(win
->GetValue());
102 win
->GetEventHandler()->ProcessEvent( event
);
106 //-----------------------------------------------------------------------------
108 //-----------------------------------------------------------------------------
111 gtk_text_changed_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
113 if ( win
->IgnoreTextUpdate() )
116 if (!win
->m_hasVMT
) return;
119 wxapp_install_idle_handler();
122 win
->UpdateFontIfNeeded();
124 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, win
->GetId() );
125 event
.SetEventObject( win
);
126 event
.SetString( win
->GetValue() );
127 win
->GetEventHandler()->ProcessEvent( event
);
130 //-----------------------------------------------------------------------------
131 // "changed" from vertical scrollbar
132 //-----------------------------------------------------------------------------
135 gtk_scrollbar_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
137 if (!win
->m_hasVMT
) return;
140 wxapp_install_idle_handler();
142 win
->CalculateScrollbar();
145 //-----------------------------------------------------------------------------
147 //-----------------------------------------------------------------------------
149 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
,wxControl
)
151 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
152 EVT_CHAR(wxTextCtrl::OnChar
)
154 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
155 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
156 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
157 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
158 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
160 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
161 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
162 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
163 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
164 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
167 void wxTextCtrl::Init()
171 m_updateFont
= FALSE
;
173 m_vScrollbar
= (GtkWidget
*)NULL
;
176 wxTextCtrl::wxTextCtrl( wxWindow
*parent
,
178 const wxString
&value
,
182 const wxValidator
& validator
,
183 const wxString
&name
)
187 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
190 bool wxTextCtrl::Create( wxWindow
*parent
,
192 const wxString
&value
,
196 const wxValidator
& validator
,
197 const wxString
&name
)
200 m_acceptsFocus
= TRUE
;
202 if (!PreCreation( parent
, pos
, size
) ||
203 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
205 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
210 m_vScrollbarVisible
= FALSE
;
212 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
215 #if (GTK_MINOR_VERSION > 2)
216 /* a multi-line edit control: create a vertical scrollbar by default and
217 horizontal if requested */
218 bool bHasHScrollbar
= (style
& wxHSCROLL
) != 0;
220 bool bHasHScrollbar
= FALSE
;
223 /* create our control ... */
224 m_text
= gtk_text_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
226 /* ... and put into the upper left hand corner of the table */
227 m_widget
= gtk_table_new(bHasHScrollbar
? 2 : 1, 2, FALSE
);
228 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
229 gtk_table_attach( GTK_TABLE(m_widget
), m_text
, 0, 1, 0, 1,
230 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
231 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
234 /* always wrap words */
235 gtk_text_set_word_wrap( GTK_TEXT(m_text
), TRUE
);
237 #if (GTK_MINOR_VERSION > 2)
238 /* put the horizontal scrollbar in the lower left hand corner */
241 GtkWidget
*hscrollbar
= gtk_hscrollbar_new(GTK_TEXT(m_text
)->hadj
);
242 GTK_WIDGET_UNSET_FLAGS( hscrollbar
, GTK_CAN_FOCUS
);
243 gtk_table_attach(GTK_TABLE(m_widget
), hscrollbar
, 0, 1, 1, 2,
244 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
),
247 gtk_widget_show(hscrollbar
);
249 /* don't wrap lines, otherwise we wouldn't need the scrollbar */
250 gtk_text_set_line_wrap( GTK_TEXT(m_text
), FALSE
);
254 /* finally, put the vertical scrollbar in the upper right corner */
255 m_vScrollbar
= gtk_vscrollbar_new( GTK_TEXT(m_text
)->vadj
);
256 GTK_WIDGET_UNSET_FLAGS( m_vScrollbar
, GTK_CAN_FOCUS
);
257 gtk_table_attach(GTK_TABLE(m_widget
), m_vScrollbar
, 1, 2, 0, 1,
259 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
),
264 /* a single-line text control: no need for scrollbars */
266 m_text
= gtk_entry_new();
269 m_parent
->DoAddChild( this );
271 m_focusWidget
= m_text
;
275 SetFont( parent
->GetFont() );
277 wxSize
size_best( DoGetBestSize() );
278 wxSize
new_size( size
);
279 if (new_size
.x
== -1)
280 new_size
.x
= size_best
.x
;
281 if (new_size
.y
== -1)
282 new_size
.y
= size_best
.y
;
283 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
284 SetSize( new_size
.x
, new_size
.y
);
287 gtk_widget_show(m_text
);
291 gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text
)->vadj
), "changed",
292 (GtkSignalFunc
) gtk_scrollbar_changed_callback
, (gpointer
) this );
295 if (!value
.IsEmpty())
299 #if GTK_MINOR_VERSION == 0
300 // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in
301 // gtk_editable_insert_text()
302 gtk_widget_realize(m_text
);
306 wxWX2MBbuf val
= value
.mbc_str();
307 gtk_editable_insert_text( GTK_EDITABLE(m_text
), val
, strlen(val
), &tmp
);
309 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &tmp
);
310 #endif // Unicode/!Unicode
314 /* bring editable's cursor uptodate. bug in GTK. */
316 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
320 if (style
& wxTE_PASSWORD
)
323 gtk_entry_set_visibility( GTK_ENTRY(m_text
), FALSE
);
326 if (style
& wxTE_READONLY
)
329 gtk_entry_set_editable( GTK_ENTRY(m_text
), FALSE
);
334 gtk_text_set_editable( GTK_TEXT(m_text
), 1 );
337 /* we want to be notified about text changes */
338 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
339 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
341 /* we don't set a valid background colour, because the window
342 manager should use a default one */
343 m_backgroundColour
= wxColour();
345 wxColour colFg
= parent
->GetForegroundColour();
346 SetForegroundColour( colFg
);
348 m_cursor
= wxCursor( wxCURSOR_IBEAM
);
350 wxTextAttr
attrDef( colFg
, m_backgroundColour
, parent
->GetFont() );
351 SetDefaultStyle( attrDef
);
358 void wxTextCtrl::CalculateScrollbar()
360 if ((m_windowStyle
& wxTE_MULTILINE
) == 0) return;
362 GtkAdjustment
*adj
= GTK_TEXT(m_text
)->vadj
;
364 if (adj
->upper
- adj
->page_size
< 0.8)
366 if (m_vScrollbarVisible
)
368 gtk_widget_hide( m_vScrollbar
);
369 m_vScrollbarVisible
= FALSE
;
374 if (!m_vScrollbarVisible
)
376 gtk_widget_show( m_vScrollbar
);
377 m_vScrollbarVisible
= TRUE
;
382 wxString
wxTextCtrl::GetValue() const
384 wxCHECK_MSG( m_text
!= NULL
, wxT(""), wxT("invalid text ctrl") );
387 if (m_windowStyle
& wxTE_MULTILINE
)
389 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
390 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
391 tmp
= wxString(text
,*wxConvCurrent
);
396 tmp
= wxString(gtk_entry_get_text( GTK_ENTRY(m_text
) ),*wxConvCurrent
);
401 void wxTextCtrl::SetValue( const wxString
&value
)
403 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
405 if (m_windowStyle
& wxTE_MULTILINE
)
407 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
408 gtk_editable_delete_text( GTK_EDITABLE(m_text
), 0, len
);
411 wxWX2MBbuf tmpbuf
= value
.mbc_str();
412 gtk_editable_insert_text( GTK_EDITABLE(m_text
), tmpbuf
, strlen(tmpbuf
), &len
);
414 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
.mbc_str(), value
.Length(), &len
);
419 gtk_entry_set_text( GTK_ENTRY(m_text
), value
.mbc_str() );
422 // GRG, Jun/2000: Changed this after a lot of discussion in
423 // the lists. wxWindows 2.2 will have a set of flags to
424 // customize this behaviour.
425 SetInsertionPoint(0);
430 void wxTextCtrl::WriteText( const wxString
&text
)
432 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
438 wxWX2MBbuf buf
= text
.mbc_str();
439 const char *txt
= buf
;
440 size_t txtlen
= strlen(buf
);
442 const char *txt
= text
;
443 size_t txtlen
= text
.length();
446 if ( m_windowStyle
& wxTE_MULTILINE
)
448 // After cursor movements, gtk_text_get_point() is wrong by one.
449 gtk_text_set_point( GTK_TEXT(m_text
), GTK_EDITABLE(m_text
)->current_pos
);
451 // if we have any special style, use it
452 if ( !m_defaultStyle
.IsDefault() )
456 wxGtkTextInsert(m_text
, m_defaultStyle
, txt
, txtlen
);
460 gint len
= GTK_EDITABLE(m_text
)->current_pos
;
461 gtk_editable_insert_text( GTK_EDITABLE(m_text
), txt
, txtlen
, &len
);
464 // Bring editable's cursor back uptodate.
465 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
469 // This moves the cursor pos to behind the inserted text.
470 gint len
= GTK_EDITABLE(m_text
)->current_pos
;
471 gtk_editable_insert_text( GTK_EDITABLE(m_text
), txt
, txtlen
, &len
);
473 // Bring editable's cursor uptodate.
474 GTK_EDITABLE(m_text
)->current_pos
+= text
.Len();
476 // Bring entry's cursor uptodate.
477 gtk_entry_set_position( GTK_ENTRY(m_text
), GTK_EDITABLE(m_text
)->current_pos
);
483 void wxTextCtrl::AppendText( const wxString
&text
)
485 SetInsertionPointEnd();
489 wxString
wxTextCtrl::GetLineText( long lineNo
) const
491 if (m_windowStyle
& wxTE_MULTILINE
)
493 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
494 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
498 wxString
buf(wxT(""));
501 for (i
= 0; currentLine
!= lineNo
&& text
[i
]; i
++ )
506 for (j
= 0; text
[i
] && text
[i
] != '\n'; i
++, j
++ )
514 return wxEmptyString
;
519 if (lineNo
== 0) return GetValue();
520 return wxEmptyString
;
524 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
526 /* If you implement this, don't forget to update the documentation!
527 * (file docs/latex/wx/text.tex) */
528 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
531 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
533 if ( m_windowStyle
& wxTE_MULTILINE
)
535 wxString text
= GetValue();
537 // cast to prevent warning. But pos really should've been unsigned.
538 if( (unsigned long)pos
> text
.Len() )
544 const wxChar
* stop
= text
.c_str() + pos
;
545 for ( const wxChar
*p
= text
.c_str(); p
< stop
; p
++ )
556 else // single line control
558 if ( pos
<= GTK_ENTRY(m_text
)->text_length
)
565 // index out of bounds
573 long wxTextCtrl::XYToPosition(long x
, long y
) const
575 if (!(m_windowStyle
& wxTE_MULTILINE
)) return 0;
578 for( int i
=0; i
<y
; i
++ ) pos
+= GetLineLength(i
) + 1; // one for '\n'
584 int wxTextCtrl::GetLineLength(long lineNo
) const
586 wxString str
= GetLineText (lineNo
);
587 return (int) str
.Length();
590 int wxTextCtrl::GetNumberOfLines() const
592 if (m_windowStyle
& wxTE_MULTILINE
)
594 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
595 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
600 for (int i
= 0; i
< len
; i
++ )
607 // currentLine is 0 based, add 1 to get number of lines
608 return currentLine
+ 1;
621 void wxTextCtrl::SetInsertionPoint( long pos
)
623 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
625 if (m_windowStyle
& wxTE_MULTILINE
)
627 /* seems to be broken in GTK 1.0.X:
628 gtk_text_set_point( GTK_TEXT(m_text), (int)pos ); */
630 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
631 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
633 /* we fake a set_point by inserting and deleting. as the user
634 isn't supposed to get to know about thos non-sense, we
635 disconnect so that no events are sent to the user program. */
637 gint tmp
= (gint
)pos
;
638 gtk_editable_insert_text( GTK_EDITABLE(m_text
), " ", 1, &tmp
);
639 gtk_editable_delete_text( GTK_EDITABLE(m_text
), tmp
-1, tmp
);
641 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
642 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
644 /* bring editable's cursor uptodate. another bug in GTK. */
646 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
650 gtk_entry_set_position( GTK_ENTRY(m_text
), (int)pos
);
652 /* bring editable's cursor uptodate. bug in GTK. */
654 GTK_EDITABLE(m_text
)->current_pos
= (guint32
)pos
;
658 void wxTextCtrl::SetInsertionPointEnd()
660 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
662 if (m_windowStyle
& wxTE_MULTILINE
)
663 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text
)));
665 gtk_entry_set_position( GTK_ENTRY(m_text
), -1 );
668 void wxTextCtrl::SetEditable( bool editable
)
670 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
672 if (m_windowStyle
& wxTE_MULTILINE
)
673 gtk_text_set_editable( GTK_TEXT(m_text
), editable
);
675 gtk_entry_set_editable( GTK_ENTRY(m_text
), editable
);
678 bool wxTextCtrl::Enable( bool enable
)
680 if (!wxWindowBase::Enable(enable
))
686 if (m_windowStyle
& wxTE_MULTILINE
)
688 gtk_text_set_editable( GTK_TEXT(m_text
), enable
);
689 OnParentEnable(enable
);
693 gtk_widget_set_sensitive( m_text
, enable
);
699 // wxGTK-specific: called recursively by Enable,
700 // to give widgets an oppprtunity to correct their colours after they
701 // have been changed by Enable
702 void wxTextCtrl::OnParentEnable( bool enable
)
704 // If we have a custom background colour, we use this colour in both
705 // disabled and enabled mode, or we end up with a different colour under the
707 wxColour oldColour
= GetBackgroundColour();
710 // Need to set twice or it'll optimize the useful stuff out
711 if (oldColour
== * wxWHITE
)
712 SetBackgroundColour(*wxBLACK
);
714 SetBackgroundColour(*wxWHITE
);
715 SetBackgroundColour(oldColour
);
719 void wxTextCtrl::DiscardEdits()
724 // ----------------------------------------------------------------------------
725 // max text length support
726 // ----------------------------------------------------------------------------
728 void wxTextCtrl::IgnoreNextTextUpdate()
730 m_ignoreNextUpdate
= TRUE
;
733 bool wxTextCtrl::IgnoreTextUpdate()
735 if ( m_ignoreNextUpdate
)
737 m_ignoreNextUpdate
= FALSE
;
745 void wxTextCtrl::SetMaxLength(unsigned long len
)
747 if ( !HasFlag(wxTE_MULTILINE
) )
749 gtk_entry_set_max_length(GTK_ENTRY(m_text
), len
);
751 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
752 // we had tried to enter more text than allowed by max text length and
753 // the text wasn't really changed
755 // to detect this and generate TEXT_MAXLEN event instead of
756 // TEXT_CHANGED one in this case we also catch "insert_text" signal
758 // when max len is set to 0 we disconnect our handler as it means that
759 // we shouldn't check anything any more
762 gtk_signal_connect( GTK_OBJECT(m_text
),
764 GTK_SIGNAL_FUNC(gtk_insert_text_callback
),
769 gtk_signal_disconnect_by_func
772 GTK_SIGNAL_FUNC(gtk_insert_text_callback
),
779 void wxTextCtrl::SetSelection( long from
, long to
)
781 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
783 if ( (m_windowStyle
& wxTE_MULTILINE
) &&
784 !GTK_TEXT(m_text
)->line_start_cache
)
786 // tell the programmer that it didn't work
787 wxLogDebug(_T("Can't call SetSelection() before realizing the control"));
791 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
794 void wxTextCtrl::ShowPosition( long WXUNUSED(pos
) )
796 // SetInsertionPoint( pos );
799 long wxTextCtrl::GetInsertionPoint() const
801 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
803 return (long) GTK_EDITABLE(m_text
)->current_pos
;
806 long wxTextCtrl::GetLastPosition() const
808 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
811 if (m_windowStyle
& wxTE_MULTILINE
)
812 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
814 pos
= GTK_ENTRY(m_text
)->text_length
;
819 void wxTextCtrl::Remove( long from
, long to
)
821 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
823 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
826 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
828 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
830 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
832 if (!value
.IsEmpty())
834 gint pos
= (gint
)from
;
836 wxWX2MBbuf buf
= value
.mbc_str();
837 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buf
, strlen(buf
), &pos
);
839 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &pos
);
844 void wxTextCtrl::Cut()
846 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
848 #if (GTK_MINOR_VERSION > 0)
849 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
) );
851 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
), 0 );
855 void wxTextCtrl::Copy()
857 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
859 #if (GTK_MINOR_VERSION > 0)
860 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
) );
862 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
), 0 );
866 void wxTextCtrl::Paste()
868 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
870 #if (GTK_MINOR_VERSION > 0)
871 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
) );
873 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
), 0 );
878 void wxTextCtrl::Undo()
881 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
884 void wxTextCtrl::Redo()
887 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
890 bool wxTextCtrl::CanUndo() const
893 wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
897 bool wxTextCtrl::CanRedo() const
900 wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
904 // If the return values from and to are the same, there is no
906 void wxTextCtrl::GetSelection(long* fromOut
, long* toOut
) const
908 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
911 if ( !(GTK_EDITABLE(m_text
)->has_selection
) )
914 to
= GetInsertionPoint();
916 else // got selection
918 from
= (long) GTK_EDITABLE(m_text
)->selection_start_pos
;
919 to
= (long) GTK_EDITABLE(m_text
)->selection_end_pos
;
923 // exchange them to be compatible with wxMSW
936 bool wxTextCtrl::IsEditable() const
938 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
940 return GTK_EDITABLE(m_text
)->editable
;
943 bool wxTextCtrl::IsModified() const
948 void wxTextCtrl::Clear()
953 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
955 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
957 if ((key_event
.KeyCode() == WXK_RETURN
) && (m_windowStyle
& wxPROCESS_ENTER
))
959 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
960 event
.SetEventObject(this);
961 event
.SetString(GetValue());
962 if (GetEventHandler()->ProcessEvent(event
)) return;
965 if ((key_event
.KeyCode() == WXK_RETURN
) && !(m_windowStyle
& wxTE_MULTILINE
))
967 // This will invoke the dialog default action, such
968 // as the clicking the default button.
970 wxWindow
*top_frame
= m_parent
;
971 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
972 top_frame
= top_frame
->GetParent();
974 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
976 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
978 if (window
->default_widget
)
980 gtk_widget_activate (window
->default_widget
);
989 GtkWidget
* wxTextCtrl::GetConnectWidget()
991 return GTK_WIDGET(m_text
);
994 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow
*window
)
996 if (m_windowStyle
& wxTE_MULTILINE
)
997 return (window
== GTK_TEXT(m_text
)->text_area
);
999 return (window
== GTK_ENTRY(m_text
)->text_area
);
1002 // the font will change for subsequent text insertiongs
1003 bool wxTextCtrl::SetFont( const wxFont
&font
)
1005 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
1007 if ( !wxTextCtrlBase::SetFont(font
) )
1009 // font didn't change, nothing to do
1013 if ( m_windowStyle
& wxTE_MULTILINE
)
1015 m_updateFont
= TRUE
;
1017 m_defaultStyle
.SetFont(font
);
1019 ChangeFontGlobally();
1025 void wxTextCtrl::ChangeFontGlobally()
1027 // this method is very inefficient and hence should be called as rarely as
1029 wxASSERT_MSG( (m_windowStyle
& wxTE_MULTILINE
) && m_updateFont
,
1030 _T("shouldn't be called for single line controls") );
1032 wxString value
= GetValue();
1033 if ( !value
.IsEmpty() )
1035 m_updateFont
= FALSE
;
1042 void wxTextCtrl::UpdateFontIfNeeded()
1045 ChangeFontGlobally();
1048 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1050 if ( !wxControl::SetForegroundColour(colour
) )
1053 // update default fg colour too
1054 m_defaultStyle
.SetTextColour(colour
);
1059 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
1061 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
1063 if ( !wxControl::SetBackgroundColour( colour
) )
1066 if (!m_widget
->window
)
1069 wxColour sysbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
1070 if (sysbg
.Red() == colour
.Red() &&
1071 sysbg
.Green() == colour
.Green() &&
1072 sysbg
.Blue() == colour
.Blue())
1074 return FALSE
; // FIXME or TRUE?
1077 if (!m_backgroundColour
.Ok())
1080 if (m_windowStyle
& wxTE_MULTILINE
)
1082 GdkWindow
*window
= GTK_TEXT(m_text
)->text_area
;
1085 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
1086 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
1087 gdk_window_clear( window
);
1090 // change active background color too
1091 m_defaultStyle
.SetBackgroundColour( colour
);
1096 bool wxTextCtrl::SetStyle( long start
, long end
, const wxTextAttr
& style
)
1098 /* VERY dirty way to do that - removes the required text and re-adds it
1099 with styling (FIXME) */
1100 if ( m_windowStyle
& wxTE_MULTILINE
)
1102 if ( style
.IsDefault() )
1108 gint l
= gtk_text_get_length( GTK_TEXT(m_text
) );
1110 wxCHECK_MSG( start
>= 0 && end
<= l
, FALSE
,
1111 _T("invalid range in wxTextCtrl::SetStyle") );
1113 gint old_pos
= gtk_editable_get_position( GTK_EDITABLE(m_text
) );
1114 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), start
, end
);
1115 wxString
tmp(text
,*wxConvCurrent
);
1118 gtk_editable_delete_text( GTK_EDITABLE(m_text
), start
, end
);
1119 gtk_editable_set_position( GTK_EDITABLE(m_text
), start
);
1122 wxWX2MBbuf buf
= tmp
.mbc_str();
1123 const char *txt
= buf
;
1124 size_t txtlen
= strlen(buf
);
1126 const char *txt
= tmp
;
1127 size_t txtlen
= tmp
.length();
1130 // use the attributes from style which are set in it and fall back
1131 // first to the default style and then to the text control default
1132 // colours for the others
1133 wxGtkTextInsert(m_text
,
1134 wxTextAttr::Combine(style
, m_defaultStyle
, this),
1138 /* does not seem to help under GTK+ 1.2 !!!
1139 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
1140 SetInsertionPoint( old_pos
);
1145 // cannot do this for GTK+'s Entry widget
1150 void wxTextCtrl::ApplyWidgetStyle()
1152 if (m_windowStyle
& wxTE_MULTILINE
)
1159 gtk_widget_set_style( m_text
, m_widgetStyle
);
1163 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1168 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1173 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1178 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1183 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1188 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1190 event
.Enable( CanCut() );
1193 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1195 event
.Enable( CanCopy() );
1198 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1200 event
.Enable( CanPaste() );
1203 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1205 event
.Enable( CanUndo() );
1208 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1210 event
.Enable( CanRedo() );
1213 void wxTextCtrl::OnInternalIdle()
1215 wxCursor cursor
= m_cursor
;
1216 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
1220 GdkWindow
*window
= (GdkWindow
*) NULL
;
1221 if (HasFlag(wxTE_MULTILINE
))
1222 window
= GTK_TEXT(m_text
)->text_area
;
1224 window
= GTK_ENTRY(m_text
)->text_area
;
1227 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1229 if (!g_globalCursor
.Ok())
1230 cursor
= *wxSTANDARD_CURSOR
;
1232 window
= m_widget
->window
;
1233 if ((window
) && !(GTK_WIDGET_NO_WINDOW(m_widget
)))
1234 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1240 wxSize
wxTextCtrl::DoGetBestSize() const
1242 // FIXME should be different for multi-line controls...
1243 wxSize
ret( wxControl::DoGetBestSize() );
1244 return wxSize(80, ret
.y
);
1247 // ----------------------------------------------------------------------------
1249 // ----------------------------------------------------------------------------
1251 void wxTextCtrl::Freeze()
1253 if ( HasFlag(wxTE_MULTILINE
) )
1255 gtk_text_freeze(GTK_TEXT(m_text
));
1259 void wxTextCtrl::Thaw()
1261 if ( HasFlag(wxTE_MULTILINE
) )
1263 GTK_TEXT(m_text
)->vadj
->value
= 0.0;
1265 gtk_text_thaw(GTK_TEXT(m_text
));
1269 // ----------------------------------------------------------------------------
1271 // ----------------------------------------------------------------------------
1273 GtkAdjustment
*wxTextCtrl::GetVAdj() const
1275 return HasFlag(wxTE_MULTILINE
) ? GTK_TEXT(m_text
)->vadj
: NULL
;
1278 bool wxTextCtrl::DoScroll(GtkAdjustment
*adj
, int diff
)
1280 float value
= adj
->value
+ diff
;
1285 float upper
= adj
->upper
- adj
->page_size
;
1286 if ( value
> upper
)
1289 // did we noticeably change the scroll position?
1290 if ( fabs(adj
->value
- value
) < 0.2 )
1292 // well, this is what Robert does in wxScrollBar, so it must be good...
1298 gtk_signal_emit_by_name(GTK_OBJECT(adj
), "value_changed");
1303 bool wxTextCtrl::ScrollLines(int lines
)
1305 GtkAdjustment
*adj
= GetVAdj();
1309 // this is hardcoded to 10 in GTK+ 1.2 (great idea)
1310 static const int KEY_SCROLL_PIXELS
= 10;
1312 return DoScroll(adj
, lines
*KEY_SCROLL_PIXELS
);
1315 bool wxTextCtrl::ScrollPages(int pages
)
1317 GtkAdjustment
*adj
= GetVAdj();
1321 return DoScroll(adj
, (int)ceil(pages
*adj
->page_increment
));