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 wxTextAttr
attrDef( colFg
, m_backgroundColour
, parent
->GetFont() );
329 SetDefaultStyle( attrDef
);
336 void wxTextCtrl::CalculateScrollbar()
338 if ((m_windowStyle
& wxTE_MULTILINE
) == 0) return;
340 GtkAdjustment
*adj
= GTK_TEXT(m_text
)->vadj
;
342 if (adj
->upper
- adj
->page_size
< 0.8)
344 if (m_vScrollbarVisible
)
346 gtk_widget_hide( m_vScrollbar
);
347 m_vScrollbarVisible
= FALSE
;
352 if (!m_vScrollbarVisible
)
354 gtk_widget_show( m_vScrollbar
);
355 m_vScrollbarVisible
= TRUE
;
360 wxString
wxTextCtrl::GetValue() const
362 wxCHECK_MSG( m_text
!= NULL
, wxT(""), wxT("invalid text ctrl") );
365 if (m_windowStyle
& wxTE_MULTILINE
)
367 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
368 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
369 tmp
= wxString(text
,*wxConvCurrent
);
374 tmp
= wxString(gtk_entry_get_text( GTK_ENTRY(m_text
) ),*wxConvCurrent
);
379 void wxTextCtrl::SetValue( const wxString
&value
)
381 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
383 if (m_windowStyle
& wxTE_MULTILINE
)
385 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
386 gtk_editable_delete_text( GTK_EDITABLE(m_text
), 0, len
);
389 wxWX2MBbuf tmpbuf
= value
.mbc_str();
390 gtk_editable_insert_text( GTK_EDITABLE(m_text
), tmpbuf
, strlen(tmpbuf
), &len
);
392 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
.mbc_str(), value
.Length(), &len
);
397 gtk_entry_set_text( GTK_ENTRY(m_text
), value
.mbc_str() );
400 // GRG, Jun/2000: Changed this after a lot of discussion in
401 // the lists. wxWindows 2.2 will have a set of flags to
402 // customize this behaviour.
403 SetInsertionPoint(0);
408 void wxTextCtrl::WriteText( const wxString
&text
)
410 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
416 wxWX2MBbuf buf
= text
.mbc_str();
417 const char *txt
= buf
;
418 size_t txtlen
= strlen(buf
);
420 const char *txt
= text
;
421 size_t txtlen
= text
.length();
424 if ( m_windowStyle
& wxTE_MULTILINE
)
426 /* this moves the cursor pos to behind the inserted text */
427 gint len
= GTK_EDITABLE(m_text
)->current_pos
;
429 // if we have any special style, use it
430 if ( !m_defaultStyle
.IsDefault() )
432 GdkFont
*font
= m_defaultStyle
.HasFont()
433 ? m_defaultStyle
.GetFont().GetInternalFont()
436 GdkColor
*colFg
= m_defaultStyle
.HasTextColour()
437 ? m_defaultStyle
.GetTextColour().GetColor()
440 GdkColor
*colBg
= m_defaultStyle
.HasBackgroundColour()
441 ? m_defaultStyle
.GetBackgroundColour().GetColor()
444 gtk_text_insert( GTK_TEXT(m_text
), font
, colFg
, colBg
, txt
, txtlen
);
448 gtk_editable_insert_text( GTK_EDITABLE(m_text
), txt
, txtlen
, &len
);
451 /* bring editable's cursor uptodate. bug in GTK. */
452 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
456 /* this moves the cursor pos to behind the inserted text */
457 gint len
= GTK_EDITABLE(m_text
)->current_pos
;
458 gtk_editable_insert_text( GTK_EDITABLE(m_text
), txt
, txtlen
, &len
);
460 /* bring editable's cursor uptodate. bug in GTK. */
461 GTK_EDITABLE(m_text
)->current_pos
+= text
.Len();
463 /* bring entry's cursor uptodate. bug in GTK. */
464 gtk_entry_set_position( GTK_ENTRY(m_text
), GTK_EDITABLE(m_text
)->current_pos
);
468 void wxTextCtrl::AppendText( const wxString
&text
)
470 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
476 wxWX2MBbuf buf
= text
.mbc_str();
477 const char *txt
= buf
;
478 size_t txtlen
= strlen(buf
);
480 const char *txt
= text
;
481 size_t txtlen
= text
.length();
484 if (m_windowStyle
& wxTE_MULTILINE
)
486 if ( !m_defaultStyle
.IsDefault() )
488 wxFont font
= m_defaultStyle
.HasFont() ? m_defaultStyle
.GetFont()
490 GdkFont
*fnt
= font
.Ok() ? font
.GetInternalFont() : NULL
;
492 wxColour col
= m_defaultStyle
.HasTextColour()
493 ? m_defaultStyle
.GetTextColour()
494 : m_foregroundColour
;
495 GdkColor
*colFg
= col
.Ok() ? col
.GetColor() : NULL
;
497 col
= m_defaultStyle
.HasBackgroundColour()
498 ? m_defaultStyle
.GetBackgroundColour()
499 : m_backgroundColour
;
500 GdkColor
*colBg
= col
.Ok() ? col
.GetColor() : NULL
;
502 gtk_text_insert( GTK_TEXT(m_text
), fnt
, colFg
, colBg
, txt
, txtlen
);
506 /* we'll insert at the last position */
507 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
508 gtk_editable_insert_text( GTK_EDITABLE(m_text
), txt
, txtlen
, &len
);
511 /* bring editable's cursor uptodate. bug in GTK. */
512 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
516 gtk_entry_append_text( GTK_ENTRY(m_text
), txt
);
520 wxString
wxTextCtrl::GetLineText( long lineNo
) const
522 if (m_windowStyle
& wxTE_MULTILINE
)
524 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
525 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
529 wxString
buf(wxT(""));
532 for (i
= 0; currentLine
!= lineNo
&& text
[i
]; i
++ )
537 for (j
= 0; text
[i
] && text
[i
] != '\n'; i
++, j
++ )
544 return wxEmptyString
;
548 if (lineNo
== 0) return GetValue();
549 return wxEmptyString
;
553 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
555 /* If you implement this, don't forget to update the documentation!
556 * (file docs/latex/wx/text.tex) */
557 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
560 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
562 if ( m_windowStyle
& wxTE_MULTILINE
)
564 wxString text
= GetValue();
566 // cast to prevent warning. But pos really should've been unsigned.
567 if( (unsigned long)pos
> text
.Len() )
573 const wxChar
* stop
= text
.c_str() + pos
;
574 for ( const wxChar
*p
= text
.c_str(); p
< stop
; p
++ )
585 else // single line control
587 if ( pos
<= GTK_ENTRY(m_text
)->text_length
)
594 // index out of bounds
602 long wxTextCtrl::XYToPosition(long x
, long y
) const
604 if (!(m_windowStyle
& wxTE_MULTILINE
)) return 0;
607 for( int i
=0; i
<y
; i
++ ) pos
+= GetLineLength(i
) + 1; // one for '\n'
613 int wxTextCtrl::GetLineLength(long lineNo
) const
615 wxString str
= GetLineText (lineNo
);
616 return (int) str
.Length();
619 int wxTextCtrl::GetNumberOfLines() const
621 if (m_windowStyle
& wxTE_MULTILINE
)
623 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
624 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
629 for (int i
= 0; i
< len
; i
++ )
636 // currentLine is 0 based, add 1 to get number of lines
637 return currentLine
+ 1;
650 void wxTextCtrl::SetInsertionPoint( long pos
)
652 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
654 if (m_windowStyle
& wxTE_MULTILINE
)
656 /* seems to be broken in GTK 1.0.X:
657 gtk_text_set_point( GTK_TEXT(m_text), (int)pos ); */
659 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
660 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
662 /* we fake a set_point by inserting and deleting. as the user
663 isn't supposed to get to know about thos non-sense, we
664 disconnect so that no events are sent to the user program. */
666 gint tmp
= (gint
)pos
;
667 gtk_editable_insert_text( GTK_EDITABLE(m_text
), " ", 1, &tmp
);
668 gtk_editable_delete_text( GTK_EDITABLE(m_text
), tmp
-1, tmp
);
670 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
671 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
673 /* bring editable's cursor uptodate. another bug in GTK. */
675 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
679 gtk_entry_set_position( GTK_ENTRY(m_text
), (int)pos
);
681 /* bring editable's cursor uptodate. bug in GTK. */
683 GTK_EDITABLE(m_text
)->current_pos
= (guint32
)pos
;
687 void wxTextCtrl::SetInsertionPointEnd()
689 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
691 if (m_windowStyle
& wxTE_MULTILINE
)
692 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text
)));
694 gtk_entry_set_position( GTK_ENTRY(m_text
), -1 );
697 void wxTextCtrl::SetEditable( bool editable
)
699 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
701 if (m_windowStyle
& wxTE_MULTILINE
)
702 gtk_text_set_editable( GTK_TEXT(m_text
), editable
);
704 gtk_entry_set_editable( GTK_ENTRY(m_text
), editable
);
707 bool wxTextCtrl::Enable( bool enable
)
709 if (!wxWindowBase::Enable(enable
))
715 if (m_windowStyle
& wxTE_MULTILINE
)
717 gtk_text_set_editable( GTK_TEXT(m_text
), enable
);
718 OnParentEnable(enable
);
722 gtk_widget_set_sensitive( m_text
, enable
);
728 // wxGTK-specific: called recursively by Enable,
729 // to give widgets an oppprtunity to correct their colours after they
730 // have been changed by Enable
731 void wxTextCtrl::OnParentEnable( bool enable
)
733 // If we have a custom background colour, we use this colour in both
734 // disabled and enabled mode, or we end up with a different colour under the
736 wxColour oldColour
= GetBackgroundColour();
739 // Need to set twice or it'll optimize the useful stuff out
740 if (oldColour
== * wxWHITE
)
741 SetBackgroundColour(*wxBLACK
);
743 SetBackgroundColour(*wxWHITE
);
744 SetBackgroundColour(oldColour
);
748 void wxTextCtrl::DiscardEdits()
753 // ----------------------------------------------------------------------------
754 // max text length support
755 // ----------------------------------------------------------------------------
757 void wxTextCtrl::IgnoreNextTextUpdate()
759 m_ignoreNextUpdate
= TRUE
;
762 bool wxTextCtrl::IgnoreTextUpdate()
764 if ( m_ignoreNextUpdate
)
766 m_ignoreNextUpdate
= FALSE
;
774 void wxTextCtrl::SetMaxLength(unsigned long len
)
776 if ( !HasFlag(wxTE_MULTILINE
) )
778 gtk_entry_set_max_length(GTK_ENTRY(m_text
), len
);
780 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
781 // we had tried to enter more text than allowed by max text length and
782 // the text wasn't really changed
784 // to detect this and generate TEXT_MAXLEN event instead of
785 // TEXT_CHANGED one in this case we also catch "insert_text" signal
787 // when max len is set to 0 we disconnect our handler as it means that
788 // we shouldn't check anything any more
791 gtk_signal_connect( GTK_OBJECT(m_text
),
793 GTK_SIGNAL_FUNC(gtk_insert_text_callback
),
798 gtk_signal_disconnect_by_func
801 GTK_SIGNAL_FUNC(gtk_insert_text_callback
),
808 void wxTextCtrl::SetSelection( long from
, long to
)
810 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
812 if ( (m_windowStyle
& wxTE_MULTILINE
) &&
813 !GTK_TEXT(m_text
)->line_start_cache
)
815 // tell the programmer that it didn't work
816 wxLogDebug(_T("Can't call SetSelection() before realizing the control"));
820 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
823 void wxTextCtrl::ShowPosition( long WXUNUSED(pos
) )
825 // SetInsertionPoint( pos );
828 long wxTextCtrl::GetInsertionPoint() const
830 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
832 return (long) GTK_EDITABLE(m_text
)->current_pos
;
835 long wxTextCtrl::GetLastPosition() const
837 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
840 if (m_windowStyle
& wxTE_MULTILINE
)
841 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
843 pos
= GTK_ENTRY(m_text
)->text_length
;
848 void wxTextCtrl::Remove( long from
, long to
)
850 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
852 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
855 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
857 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
859 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
861 if (!value
.IsEmpty())
863 gint pos
= (gint
)from
;
865 wxWX2MBbuf buf
= value
.mbc_str();
866 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buf
, strlen(buf
), &pos
);
868 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &pos
);
873 void wxTextCtrl::Cut()
875 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
877 #if (GTK_MINOR_VERSION > 0)
878 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
) );
880 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
), 0 );
884 void wxTextCtrl::Copy()
886 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
888 #if (GTK_MINOR_VERSION > 0)
889 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
) );
891 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
), 0 );
895 void wxTextCtrl::Paste()
897 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
899 #if (GTK_MINOR_VERSION > 0)
900 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
) );
902 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
), 0 );
907 void wxTextCtrl::Undo()
910 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
913 void wxTextCtrl::Redo()
916 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
919 bool wxTextCtrl::CanUndo() const
922 wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
926 bool wxTextCtrl::CanRedo() const
929 wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
933 // If the return values from and to are the same, there is no
935 void wxTextCtrl::GetSelection(long* fromOut
, long* toOut
) const
937 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
940 if ( !(GTK_EDITABLE(m_text
)->has_selection
) )
943 to
= GetInsertionPoint();
945 else // got selection
947 from
= (long) GTK_EDITABLE(m_text
)->selection_start_pos
;
948 to
= (long) GTK_EDITABLE(m_text
)->selection_end_pos
;
952 // exchange them to be compatible with wxMSW
965 bool wxTextCtrl::IsEditable() const
967 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
969 return GTK_EDITABLE(m_text
)->editable
;
972 bool wxTextCtrl::IsModified() const
977 void wxTextCtrl::Clear()
982 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
984 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
986 if ((key_event
.KeyCode() == WXK_RETURN
) && (m_windowStyle
& wxPROCESS_ENTER
))
988 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
989 event
.SetEventObject(this);
990 event
.SetString(GetValue());
991 if (GetEventHandler()->ProcessEvent(event
)) return;
994 if ((key_event
.KeyCode() == WXK_RETURN
) && !(m_windowStyle
& wxTE_MULTILINE
))
996 wxWindow
*top_frame
= m_parent
;
997 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
998 top_frame
= top_frame
->GetParent();
999 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
1001 if (window
->default_widget
)
1003 gtk_widget_activate (window
->default_widget
);
1011 GtkWidget
* wxTextCtrl::GetConnectWidget()
1013 return GTK_WIDGET(m_text
);
1016 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow
*window
)
1018 if (m_windowStyle
& wxTE_MULTILINE
)
1019 return (window
== GTK_TEXT(m_text
)->text_area
);
1021 return (window
== GTK_ENTRY(m_text
)->text_area
);
1024 // the font will change for subsequent text insertiongs
1025 bool wxTextCtrl::SetFont( const wxFont
&font
)
1027 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
1029 if ( !wxWindowBase::SetFont(font
) )
1031 // font didn't change, nothing to do
1035 if ( m_windowStyle
& wxTE_MULTILINE
)
1037 m_updateFont
= TRUE
;
1039 m_defaultStyle
.SetFont(font
);
1041 ChangeFontGlobally();
1047 void wxTextCtrl::ChangeFontGlobally()
1049 // this method is very inefficient and hence should be called as rarely as
1051 wxASSERT_MSG( (m_windowStyle
& wxTE_MULTILINE
) && m_updateFont
,
1052 _T("shouldn't be called for single line controls") );
1054 wxString value
= GetValue();
1055 if ( !value
.IsEmpty() )
1057 m_updateFont
= FALSE
;
1064 void wxTextCtrl::UpdateFontIfNeeded()
1067 ChangeFontGlobally();
1070 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1072 if ( !wxControl::SetForegroundColour(colour
) )
1075 // update default fg colour too
1076 m_defaultStyle
.SetTextColour(colour
);
1081 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
1083 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
1085 if ( !wxControl::SetBackgroundColour( colour
) )
1088 if (!m_widget
->window
)
1091 wxColour sysbg
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE
);
1092 if (sysbg
.Red() == colour
.Red() &&
1093 sysbg
.Green() == colour
.Green() &&
1094 sysbg
.Blue() == colour
.Blue())
1096 return FALSE
; // FIXME or TRUE?
1099 if (!m_backgroundColour
.Ok())
1102 if (m_windowStyle
& wxTE_MULTILINE
)
1104 GdkWindow
*window
= GTK_TEXT(m_text
)->text_area
;
1107 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
1108 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
1109 gdk_window_clear( window
);
1112 // change active background color too
1113 m_defaultStyle
.SetBackgroundColour( colour
);
1118 bool wxTextCtrl::SetStyle( long start
, long end
, const wxTextAttr
&style
)
1120 /* VERY dirty way to do that - removes the required text and re-adds it
1121 with styling (FIXME) */
1122 if ( m_windowStyle
& wxTE_MULTILINE
)
1124 if ( style
.IsDefault() )
1130 gint l
= gtk_text_get_length( GTK_TEXT(m_text
) );
1132 wxCHECK_MSG( start
>= 0 && end
<= l
, FALSE
,
1133 _T("invalid range in wxTextCtrl::SetStyle") );
1135 gint old_pos
= gtk_editable_get_position( GTK_EDITABLE(m_text
) );
1136 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), start
, end
);
1137 wxString
tmp(text
,*wxConvCurrent
);
1140 gtk_editable_delete_text( GTK_EDITABLE(m_text
), start
, end
);
1141 gtk_editable_set_position( GTK_EDITABLE(m_text
), start
);
1144 wxWX2MBbuf buf
= tmp
.mbc_str();
1145 const char *txt
= buf
;
1146 size_t txtlen
= strlen(buf
);
1148 const char *txt
= tmp
;
1149 size_t txtlen
= tmp
.length();
1152 GdkFont
*font
= style
.HasFont()
1153 ? style
.GetFont().GetInternalFont()
1156 GdkColor
*colFg
= style
.HasTextColour()
1157 ? style
.GetTextColour().GetColor()
1160 GdkColor
*colBg
= style
.HasBackgroundColour()
1161 ? style
.GetBackgroundColour().GetColor()
1164 gtk_text_insert( GTK_TEXT(m_text
), font
, colFg
, colBg
, txt
, txtlen
);
1166 /* does not seem to help under GTK+ 1.2 !!!
1167 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
1168 SetInsertionPoint( old_pos
);
1173 // cannot do this for GTK+'s Entry widget
1178 void wxTextCtrl::ApplyWidgetStyle()
1180 if (m_windowStyle
& wxTE_MULTILINE
)
1187 gtk_widget_set_style( m_text
, m_widgetStyle
);
1191 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1196 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1201 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1206 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1211 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1216 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1218 event
.Enable( CanCut() );
1221 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1223 event
.Enable( CanCopy() );
1226 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1228 event
.Enable( CanPaste() );
1231 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1233 event
.Enable( CanUndo() );
1236 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1238 event
.Enable( CanRedo() );
1241 void wxTextCtrl::OnInternalIdle()
1243 wxCursor cursor
= m_cursor
;
1244 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
1248 GdkWindow
*window
= (GdkWindow
*) NULL
;
1249 if (HasFlag(wxTE_MULTILINE
))
1250 window
= GTK_TEXT(m_text
)->text_area
;
1252 window
= GTK_ENTRY(m_text
)->text_area
;
1255 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1257 if (!g_globalCursor
.Ok())
1258 cursor
= *wxSTANDARD_CURSOR
;
1260 window
= m_widget
->window
;
1261 if ((window
) && !(GTK_WIDGET_NO_WINDOW(m_widget
)))
1262 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1268 wxSize
wxTextCtrl::DoGetBestSize() const
1270 // FIXME should be different for multi-line controls...
1271 wxSize
ret( wxControl::DoGetBestSize() );
1272 return wxSize(80, ret
.y
);
1275 // ----------------------------------------------------------------------------
1277 // ----------------------------------------------------------------------------
1279 void wxTextCtrl::Freeze()
1281 if ( HasFlag(wxTE_MULTILINE
) )
1283 gtk_text_freeze(GTK_TEXT(m_text
));
1287 void wxTextCtrl::Thaw()
1289 if ( HasFlag(wxTE_MULTILINE
) )
1291 GTK_TEXT(m_text
)->vadj
->value
= 0.0;
1293 gtk_text_thaw(GTK_TEXT(m_text
));
1297 // ----------------------------------------------------------------------------
1299 // ----------------------------------------------------------------------------
1301 GtkAdjustment
*wxTextCtrl::GetVAdj() const
1303 return HasFlag(wxTE_MULTILINE
) ? GTK_TEXT(m_text
)->vadj
: NULL
;
1306 bool wxTextCtrl::DoScroll(GtkAdjustment
*adj
, int diff
)
1308 float value
= adj
->value
+ diff
;
1313 float upper
= adj
->upper
- adj
->page_size
;
1314 if ( value
> upper
)
1317 // did we noticeably change the scroll position?
1318 if ( fabs(adj
->value
- value
) < 0.2 )
1320 // well, this is what Robert does in wxScrollBar, so it must be good...
1326 gtk_signal_emit_by_name(GTK_OBJECT(adj
), "value_changed");
1331 bool wxTextCtrl::ScrollLines(int lines
)
1333 GtkAdjustment
*adj
= GetVAdj();
1337 // this is hardcoded to 10 in GTK+ 1.2 (great idea)
1338 static const int KEY_SCROLL_PIXELS
= 10;
1340 return DoScroll(adj
, lines
*KEY_SCROLL_PIXELS
);
1343 bool wxTextCtrl::ScrollPages(int pages
)
1345 GtkAdjustment
*adj
= GetVAdj();
1349 return DoScroll(adj
, (int)ceil(pages
*adj
->page_increment
));