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
*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 // After cursor movements, gtk_text_get_point() is wrong by one.
427 gtk_text_set_point( GTK_TEXT(m_text
), 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()
445 gtk_text_insert( GTK_TEXT(m_text
), font
, colFg
, colBg
, txt
, -1 );
449 gint len
= GTK_EDITABLE(m_text
)->current_pos
;
450 gtk_editable_insert_text( GTK_EDITABLE(m_text
), txt
, txtlen
, &len
);
453 // Bring editable's cursor back uptodate.
454 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
458 // This moves the cursor pos to behind the inserted text.
459 gint len
= GTK_EDITABLE(m_text
)->current_pos
;
460 gtk_editable_insert_text( GTK_EDITABLE(m_text
), txt
, txtlen
, &len
);
462 // Bring editable's cursor uptodate.
463 GTK_EDITABLE(m_text
)->current_pos
+= text
.Len();
465 // Bring entry's cursor uptodate.
466 gtk_entry_set_position( GTK_ENTRY(m_text
), GTK_EDITABLE(m_text
)->current_pos
);
472 void wxTextCtrl::AppendText( const wxString
&text
)
474 SetInsertionPointEnd();
478 wxString
wxTextCtrl::GetLineText( long lineNo
) const
480 if (m_windowStyle
& wxTE_MULTILINE
)
482 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
483 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
487 wxString
buf(wxT(""));
490 for (i
= 0; currentLine
!= lineNo
&& text
[i
]; i
++ )
495 for (j
= 0; text
[i
] && text
[i
] != '\n'; i
++, j
++ )
503 return wxEmptyString
;
508 if (lineNo
== 0) return GetValue();
509 return wxEmptyString
;
513 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
515 /* If you implement this, don't forget to update the documentation!
516 * (file docs/latex/wx/text.tex) */
517 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
520 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
522 if ( m_windowStyle
& wxTE_MULTILINE
)
524 wxString text
= GetValue();
526 // cast to prevent warning. But pos really should've been unsigned.
527 if( (unsigned long)pos
> text
.Len() )
533 const wxChar
* stop
= text
.c_str() + pos
;
534 for ( const wxChar
*p
= text
.c_str(); p
< stop
; p
++ )
545 else // single line control
547 if ( pos
<= GTK_ENTRY(m_text
)->text_length
)
554 // index out of bounds
562 long wxTextCtrl::XYToPosition(long x
, long y
) const
564 if (!(m_windowStyle
& wxTE_MULTILINE
)) return 0;
567 for( int i
=0; i
<y
; i
++ ) pos
+= GetLineLength(i
) + 1; // one for '\n'
573 int wxTextCtrl::GetLineLength(long lineNo
) const
575 wxString str
= GetLineText (lineNo
);
576 return (int) str
.Length();
579 int wxTextCtrl::GetNumberOfLines() const
581 if (m_windowStyle
& wxTE_MULTILINE
)
583 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
584 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
589 for (int i
= 0; i
< len
; i
++ )
596 // currentLine is 0 based, add 1 to get number of lines
597 return currentLine
+ 1;
610 void wxTextCtrl::SetInsertionPoint( long pos
)
612 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
614 if (m_windowStyle
& wxTE_MULTILINE
)
616 /* seems to be broken in GTK 1.0.X:
617 gtk_text_set_point( GTK_TEXT(m_text), (int)pos ); */
619 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
620 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
622 /* we fake a set_point by inserting and deleting. as the user
623 isn't supposed to get to know about thos non-sense, we
624 disconnect so that no events are sent to the user program. */
626 gint tmp
= (gint
)pos
;
627 gtk_editable_insert_text( GTK_EDITABLE(m_text
), " ", 1, &tmp
);
628 gtk_editable_delete_text( GTK_EDITABLE(m_text
), tmp
-1, tmp
);
630 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
631 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
633 /* bring editable's cursor uptodate. another bug in GTK. */
635 GTK_EDITABLE(m_text
)->current_pos
= gtk_text_get_point( GTK_TEXT(m_text
) );
639 gtk_entry_set_position( GTK_ENTRY(m_text
), (int)pos
);
641 /* bring editable's cursor uptodate. bug in GTK. */
643 GTK_EDITABLE(m_text
)->current_pos
= (guint32
)pos
;
647 void wxTextCtrl::SetInsertionPointEnd()
649 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
651 if (m_windowStyle
& wxTE_MULTILINE
)
652 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text
)));
654 gtk_entry_set_position( GTK_ENTRY(m_text
), -1 );
657 void wxTextCtrl::SetEditable( bool editable
)
659 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
661 if (m_windowStyle
& wxTE_MULTILINE
)
662 gtk_text_set_editable( GTK_TEXT(m_text
), editable
);
664 gtk_entry_set_editable( GTK_ENTRY(m_text
), editable
);
667 bool wxTextCtrl::Enable( bool enable
)
669 if (!wxWindowBase::Enable(enable
))
675 if (m_windowStyle
& wxTE_MULTILINE
)
677 gtk_text_set_editable( GTK_TEXT(m_text
), enable
);
678 OnParentEnable(enable
);
682 gtk_widget_set_sensitive( m_text
, enable
);
688 // wxGTK-specific: called recursively by Enable,
689 // to give widgets an oppprtunity to correct their colours after they
690 // have been changed by Enable
691 void wxTextCtrl::OnParentEnable( bool enable
)
693 // If we have a custom background colour, we use this colour in both
694 // disabled and enabled mode, or we end up with a different colour under the
696 wxColour oldColour
= GetBackgroundColour();
699 // Need to set twice or it'll optimize the useful stuff out
700 if (oldColour
== * wxWHITE
)
701 SetBackgroundColour(*wxBLACK
);
703 SetBackgroundColour(*wxWHITE
);
704 SetBackgroundColour(oldColour
);
708 void wxTextCtrl::DiscardEdits()
713 // ----------------------------------------------------------------------------
714 // max text length support
715 // ----------------------------------------------------------------------------
717 void wxTextCtrl::IgnoreNextTextUpdate()
719 m_ignoreNextUpdate
= TRUE
;
722 bool wxTextCtrl::IgnoreTextUpdate()
724 if ( m_ignoreNextUpdate
)
726 m_ignoreNextUpdate
= FALSE
;
734 void wxTextCtrl::SetMaxLength(unsigned long len
)
736 if ( !HasFlag(wxTE_MULTILINE
) )
738 gtk_entry_set_max_length(GTK_ENTRY(m_text
), len
);
740 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
741 // we had tried to enter more text than allowed by max text length and
742 // the text wasn't really changed
744 // to detect this and generate TEXT_MAXLEN event instead of
745 // TEXT_CHANGED one in this case we also catch "insert_text" signal
747 // when max len is set to 0 we disconnect our handler as it means that
748 // we shouldn't check anything any more
751 gtk_signal_connect( GTK_OBJECT(m_text
),
753 GTK_SIGNAL_FUNC(gtk_insert_text_callback
),
758 gtk_signal_disconnect_by_func
761 GTK_SIGNAL_FUNC(gtk_insert_text_callback
),
768 void wxTextCtrl::SetSelection( long from
, long to
)
770 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
772 if ( (m_windowStyle
& wxTE_MULTILINE
) &&
773 !GTK_TEXT(m_text
)->line_start_cache
)
775 // tell the programmer that it didn't work
776 wxLogDebug(_T("Can't call SetSelection() before realizing the control"));
780 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
783 void wxTextCtrl::ShowPosition( long WXUNUSED(pos
) )
785 // SetInsertionPoint( pos );
788 long wxTextCtrl::GetInsertionPoint() const
790 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
792 return (long) GTK_EDITABLE(m_text
)->current_pos
;
795 long wxTextCtrl::GetLastPosition() const
797 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
800 if (m_windowStyle
& wxTE_MULTILINE
)
801 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
803 pos
= GTK_ENTRY(m_text
)->text_length
;
808 void wxTextCtrl::Remove( long from
, long to
)
810 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
812 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
815 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
817 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
819 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
821 if (!value
.IsEmpty())
823 gint pos
= (gint
)from
;
825 wxWX2MBbuf buf
= value
.mbc_str();
826 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buf
, strlen(buf
), &pos
);
828 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &pos
);
833 void wxTextCtrl::Cut()
835 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
837 #if (GTK_MINOR_VERSION > 0)
838 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
) );
840 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
), 0 );
844 void wxTextCtrl::Copy()
846 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
848 #if (GTK_MINOR_VERSION > 0)
849 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
) );
851 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
), 0 );
855 void wxTextCtrl::Paste()
857 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
859 #if (GTK_MINOR_VERSION > 0)
860 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
) );
862 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
), 0 );
867 void wxTextCtrl::Undo()
870 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
873 void wxTextCtrl::Redo()
876 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
879 bool wxTextCtrl::CanUndo() const
882 wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
886 bool wxTextCtrl::CanRedo() const
889 wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
893 // If the return values from and to are the same, there is no
895 void wxTextCtrl::GetSelection(long* fromOut
, long* toOut
) const
897 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
900 if ( !(GTK_EDITABLE(m_text
)->has_selection
) )
903 to
= GetInsertionPoint();
905 else // got selection
907 from
= (long) GTK_EDITABLE(m_text
)->selection_start_pos
;
908 to
= (long) GTK_EDITABLE(m_text
)->selection_end_pos
;
912 // exchange them to be compatible with wxMSW
925 bool wxTextCtrl::IsEditable() const
927 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
929 return GTK_EDITABLE(m_text
)->editable
;
932 bool wxTextCtrl::IsModified() const
937 void wxTextCtrl::Clear()
942 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
944 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
946 if ((key_event
.KeyCode() == WXK_RETURN
) && (m_windowStyle
& wxPROCESS_ENTER
))
948 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
949 event
.SetEventObject(this);
950 event
.SetString(GetValue());
951 if (GetEventHandler()->ProcessEvent(event
)) return;
954 if ((key_event
.KeyCode() == WXK_RETURN
) && !(m_windowStyle
& wxTE_MULTILINE
))
956 wxWindow
*top_frame
= m_parent
;
957 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
958 top_frame
= top_frame
->GetParent();
959 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
961 if (window
->default_widget
)
963 gtk_widget_activate (window
->default_widget
);
971 GtkWidget
* wxTextCtrl::GetConnectWidget()
973 return GTK_WIDGET(m_text
);
976 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow
*window
)
978 if (m_windowStyle
& wxTE_MULTILINE
)
979 return (window
== GTK_TEXT(m_text
)->text_area
);
981 return (window
== GTK_ENTRY(m_text
)->text_area
);
984 // the font will change for subsequent text insertiongs
985 bool wxTextCtrl::SetFont( const wxFont
&font
)
987 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
989 if ( !wxWindowBase::SetFont(font
) )
991 // font didn't change, nothing to do
995 if ( m_windowStyle
& wxTE_MULTILINE
)
999 m_defaultStyle
.SetFont(font
);
1001 ChangeFontGlobally();
1007 void wxTextCtrl::ChangeFontGlobally()
1009 // this method is very inefficient and hence should be called as rarely as
1011 wxASSERT_MSG( (m_windowStyle
& wxTE_MULTILINE
) && m_updateFont
,
1012 _T("shouldn't be called for single line controls") );
1014 wxString value
= GetValue();
1015 if ( !value
.IsEmpty() )
1017 m_updateFont
= FALSE
;
1024 void wxTextCtrl::UpdateFontIfNeeded()
1027 ChangeFontGlobally();
1030 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1032 if ( !wxControl::SetForegroundColour(colour
) )
1035 // update default fg colour too
1036 m_defaultStyle
.SetTextColour(colour
);
1041 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
1043 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
1045 if ( !wxControl::SetBackgroundColour( colour
) )
1048 if (!m_widget
->window
)
1051 wxColour sysbg
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE
);
1052 if (sysbg
.Red() == colour
.Red() &&
1053 sysbg
.Green() == colour
.Green() &&
1054 sysbg
.Blue() == colour
.Blue())
1056 return FALSE
; // FIXME or TRUE?
1059 if (!m_backgroundColour
.Ok())
1062 if (m_windowStyle
& wxTE_MULTILINE
)
1064 GdkWindow
*window
= GTK_TEXT(m_text
)->text_area
;
1067 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
1068 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
1069 gdk_window_clear( window
);
1072 // change active background color too
1073 m_defaultStyle
.SetBackgroundColour( colour
);
1078 bool wxTextCtrl::SetStyle( long start
, long end
, const wxTextAttr
&style
)
1080 /* VERY dirty way to do that - removes the required text and re-adds it
1081 with styling (FIXME) */
1082 if ( m_windowStyle
& wxTE_MULTILINE
)
1084 if ( style
.IsDefault() )
1090 gint l
= gtk_text_get_length( GTK_TEXT(m_text
) );
1092 wxCHECK_MSG( start
>= 0 && end
<= l
, FALSE
,
1093 _T("invalid range in wxTextCtrl::SetStyle") );
1095 gint old_pos
= gtk_editable_get_position( GTK_EDITABLE(m_text
) );
1096 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), start
, end
);
1097 wxString
tmp(text
,*wxConvCurrent
);
1100 gtk_editable_delete_text( GTK_EDITABLE(m_text
), start
, end
);
1101 gtk_editable_set_position( GTK_EDITABLE(m_text
), start
);
1104 wxWX2MBbuf buf
= tmp
.mbc_str();
1105 const char *txt
= buf
;
1106 size_t txtlen
= strlen(buf
);
1108 const char *txt
= tmp
;
1109 size_t txtlen
= tmp
.length();
1112 GdkFont
*font
= style
.HasFont()
1113 ? style
.GetFont().GetInternalFont()
1116 GdkColor
*colFg
= style
.HasTextColour()
1117 ? style
.GetTextColour().GetColor()
1120 GdkColor
*colBg
= style
.HasBackgroundColour()
1121 ? style
.GetBackgroundColour().GetColor()
1124 gtk_text_insert( GTK_TEXT(m_text
), font
, colFg
, colBg
, txt
, txtlen
);
1126 /* does not seem to help under GTK+ 1.2 !!!
1127 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
1128 SetInsertionPoint( old_pos
);
1133 // cannot do this for GTK+'s Entry widget
1138 void wxTextCtrl::ApplyWidgetStyle()
1140 if (m_windowStyle
& wxTE_MULTILINE
)
1147 gtk_widget_set_style( m_text
, m_widgetStyle
);
1151 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1156 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1161 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1166 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1171 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1176 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1178 event
.Enable( CanCut() );
1181 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1183 event
.Enable( CanCopy() );
1186 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1188 event
.Enable( CanPaste() );
1191 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1193 event
.Enable( CanUndo() );
1196 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1198 event
.Enable( CanRedo() );
1201 void wxTextCtrl::OnInternalIdle()
1203 wxCursor cursor
= m_cursor
;
1204 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
1208 GdkWindow
*window
= (GdkWindow
*) NULL
;
1209 if (HasFlag(wxTE_MULTILINE
))
1210 window
= GTK_TEXT(m_text
)->text_area
;
1212 window
= GTK_ENTRY(m_text
)->text_area
;
1215 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1217 if (!g_globalCursor
.Ok())
1218 cursor
= *wxSTANDARD_CURSOR
;
1220 window
= m_widget
->window
;
1221 if ((window
) && !(GTK_WIDGET_NO_WINDOW(m_widget
)))
1222 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1228 wxSize
wxTextCtrl::DoGetBestSize() const
1230 // FIXME should be different for multi-line controls...
1231 wxSize
ret( wxControl::DoGetBestSize() );
1232 return wxSize(80, ret
.y
);
1235 // ----------------------------------------------------------------------------
1237 // ----------------------------------------------------------------------------
1239 void wxTextCtrl::Freeze()
1241 if ( HasFlag(wxTE_MULTILINE
) )
1243 gtk_text_freeze(GTK_TEXT(m_text
));
1247 void wxTextCtrl::Thaw()
1249 if ( HasFlag(wxTE_MULTILINE
) )
1251 GTK_TEXT(m_text
)->vadj
->value
= 0.0;
1253 gtk_text_thaw(GTK_TEXT(m_text
));
1257 // ----------------------------------------------------------------------------
1259 // ----------------------------------------------------------------------------
1261 GtkAdjustment
*wxTextCtrl::GetVAdj() const
1263 return HasFlag(wxTE_MULTILINE
) ? GTK_TEXT(m_text
)->vadj
: NULL
;
1266 bool wxTextCtrl::DoScroll(GtkAdjustment
*adj
, int diff
)
1268 float value
= adj
->value
+ diff
;
1273 float upper
= adj
->upper
- adj
->page_size
;
1274 if ( value
> upper
)
1277 // did we noticeably change the scroll position?
1278 if ( fabs(adj
->value
- value
) < 0.2 )
1280 // well, this is what Robert does in wxScrollBar, so it must be good...
1286 gtk_signal_emit_by_name(GTK_OBJECT(adj
), "value_changed");
1291 bool wxTextCtrl::ScrollLines(int lines
)
1293 GtkAdjustment
*adj
= GetVAdj();
1297 // this is hardcoded to 10 in GTK+ 1.2 (great idea)
1298 static const int KEY_SCROLL_PIXELS
= 10;
1300 return DoScroll(adj
, lines
*KEY_SCROLL_PIXELS
);
1303 bool wxTextCtrl::ScrollPages(int pages
)
1305 GtkAdjustment
*adj
= GetVAdj();
1309 return DoScroll(adj
, (int)ceil(pages
*adj
->page_increment
));