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
26 // TODO: reimplement wxTextCtrl using GtkTextView
28 #define GTK_ENABLE_BROKEN // need this to get GtkText at all
31 #include "wx/gtk/private.h"
32 #include <gdk/gdkkeysyms.h>
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 extern void wxapp_install_idle_handler();
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 extern bool g_blockEventsOnDrag
;
46 extern wxCursor g_globalCursor
;
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 static void wxGtkTextInsert(GtkWidget
*text
,
53 const wxTextAttr
& attr
,
57 GdkFont
*font
= attr
.HasFont() ? attr
.GetFont().GetInternalFont()
60 GdkColor
*colFg
= attr
.HasTextColour() ? attr
.GetTextColour().GetColor()
63 GdkColor
*colBg
= attr
.HasBackgroundColour()
64 ? attr
.GetBackgroundColour().GetColor()
67 gtk_text_insert( GTK_TEXT(text
), font
, colFg
, colBg
, txt
, len
);
70 // ----------------------------------------------------------------------------
71 // "insert_text" for GtkEntry
72 // ----------------------------------------------------------------------------
75 gtk_insert_text_callback(GtkEditable
*editable
,
76 const gchar
*new_text
,
82 wxapp_install_idle_handler();
84 // we should only be called if we have a max len limit at all
85 GtkEntry
*entry
= GTK_ENTRY (editable
);
87 wxCHECK_RET( entry
->text_max_length
, _T("shouldn't be called") );
89 // check that we don't overflow the max length limit
91 // FIXME: this doesn't work when we paste a string which is going to be
93 if ( entry
->text_length
== entry
->text_max_length
)
95 // we don't need to run the base class version at all
96 gtk_signal_emit_stop_by_name(GTK_OBJECT(editable
), "insert_text");
98 // remember that the next changed signal is to be ignored to avoid
99 // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event
100 win
->IgnoreNextTextUpdate();
102 // and generate the correct one ourselves
103 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, win
->GetId());
104 event
.SetEventObject(win
);
105 event
.SetString(win
->GetValue());
106 win
->GetEventHandler()->ProcessEvent( event
);
110 //-----------------------------------------------------------------------------
112 //-----------------------------------------------------------------------------
115 gtk_text_changed_callback( GtkWidget
*widget
, wxTextCtrl
*win
)
117 if ( win
->IgnoreTextUpdate() )
120 if (!win
->m_hasVMT
) return;
123 wxapp_install_idle_handler();
126 win
->UpdateFontIfNeeded();
128 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, win
->GetId() );
129 event
.SetEventObject( win
);
130 event
.SetString( win
->GetValue() );
131 win
->GetEventHandler()->ProcessEvent( event
);
134 //-----------------------------------------------------------------------------
135 // "changed" from vertical scrollbar
136 //-----------------------------------------------------------------------------
139 gtk_scrollbar_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
141 if (!win
->m_hasVMT
) return;
144 wxapp_install_idle_handler();
146 win
->CalculateScrollbar();
149 //-----------------------------------------------------------------------------
151 //-----------------------------------------------------------------------------
153 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
,wxControl
)
155 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
156 EVT_CHAR(wxTextCtrl::OnChar
)
158 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
159 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
160 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
161 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
162 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
164 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
165 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
166 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
167 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
168 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
171 void wxTextCtrl::Init()
175 m_updateFont
= FALSE
;
177 m_vScrollbar
= (GtkWidget
*)NULL
;
180 wxTextCtrl::wxTextCtrl( wxWindow
*parent
,
182 const wxString
&value
,
186 const wxValidator
& validator
,
187 const wxString
&name
)
191 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
194 bool wxTextCtrl::Create( wxWindow
*parent
,
196 const wxString
&value
,
200 const wxValidator
& validator
,
201 const wxString
&name
)
204 m_acceptsFocus
= TRUE
;
206 if (!PreCreation( parent
, pos
, size
) ||
207 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
209 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
214 m_vScrollbarVisible
= FALSE
;
216 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
220 /* a multi-line edit control: create a vertical scrollbar by default and
221 horizontal if requested */
222 bool bHasHScrollbar
= (style
& wxHSCROLL
) != 0;
224 bool bHasHScrollbar
= FALSE
;
227 /* create our control ... */
228 m_text
= gtk_text_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
230 /* ... and put into the upper left hand corner of the table */
231 m_widget
= gtk_table_new(bHasHScrollbar
? 2 : 1, 2, FALSE
);
232 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
233 gtk_table_attach( GTK_TABLE(m_widget
), m_text
, 0, 1, 0, 1,
234 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
235 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
238 /* always wrap words */
239 gtk_text_set_word_wrap( GTK_TEXT(m_text
), TRUE
);
242 /* put the horizontal scrollbar in the lower left hand corner */
245 GtkWidget
*hscrollbar
= gtk_hscrollbar_new(GTK_TEXT(m_text
)->hadj
);
246 GTK_WIDGET_UNSET_FLAGS( hscrollbar
, GTK_CAN_FOCUS
);
247 gtk_table_attach(GTK_TABLE(m_widget
), hscrollbar
, 0, 1, 1, 2,
248 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
),
251 gtk_widget_show(hscrollbar
);
253 /* don't wrap lines, otherwise we wouldn't need the scrollbar */
254 gtk_text_set_line_wrap( GTK_TEXT(m_text
), FALSE
);
258 /* finally, put the vertical scrollbar in the upper right corner */
259 m_vScrollbar
= gtk_vscrollbar_new( GTK_TEXT(m_text
)->vadj
);
260 GTK_WIDGET_UNSET_FLAGS( m_vScrollbar
, GTK_CAN_FOCUS
);
261 gtk_table_attach(GTK_TABLE(m_widget
), m_vScrollbar
, 1, 2, 0, 1,
263 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
),
268 /* a single-line text control: no need for scrollbars */
270 m_text
= gtk_entry_new();
273 m_parent
->DoAddChild( this );
275 m_focusWidget
= m_text
;
279 SetFont( parent
->GetFont() );
281 wxSize
size_best( DoGetBestSize() );
282 wxSize
new_size( size
);
283 if (new_size
.x
== -1)
284 new_size
.x
= size_best
.x
;
285 if (new_size
.y
== -1)
286 new_size
.y
= size_best
.y
;
287 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
288 SetSize( new_size
.x
, new_size
.y
);
291 gtk_widget_show(m_text
);
295 gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text
)->vadj
), "changed",
296 (GtkSignalFunc
) gtk_scrollbar_changed_callback
, (gpointer
) this );
299 if (!value
.IsEmpty())
303 #if !GTK_CHECK_VERSION(1, 2, 0)
304 // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in
305 // gtk_editable_insert_text()
306 gtk_widget_realize(m_text
);
310 wxWX2MBbuf val
= value
.mbc_str();
311 gtk_editable_insert_text( GTK_EDITABLE(m_text
), val
, strlen(val
), &tmp
);
313 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &tmp
);
314 #endif // Unicode/!Unicode
318 /* bring editable's cursor uptodate. bug in GTK. */
319 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
323 if (style
& wxTE_PASSWORD
)
326 gtk_entry_set_visibility( GTK_ENTRY(m_text
), FALSE
);
329 if (style
& wxTE_READONLY
)
332 gtk_entry_set_editable( GTK_ENTRY(m_text
), FALSE
);
337 gtk_text_set_editable( GTK_TEXT(m_text
), 1 );
340 /* we want to be notified about text changes */
341 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
342 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
344 /* we don't set a valid background colour, because the window
345 manager should use a default one */
346 m_backgroundColour
= wxColour();
348 wxColour colFg
= parent
->GetForegroundColour();
349 SetForegroundColour( colFg
);
351 m_cursor
= wxCursor( wxCURSOR_IBEAM
);
353 wxTextAttr
attrDef( colFg
, m_backgroundColour
, parent
->GetFont() );
354 SetDefaultStyle( attrDef
);
361 void wxTextCtrl::CalculateScrollbar()
363 if ((m_windowStyle
& wxTE_MULTILINE
) == 0) return;
365 GtkAdjustment
*adj
= GTK_TEXT(m_text
)->vadj
;
367 if (adj
->upper
- adj
->page_size
< 0.8)
369 if (m_vScrollbarVisible
)
371 gtk_widget_hide( m_vScrollbar
);
372 m_vScrollbarVisible
= FALSE
;
377 if (!m_vScrollbarVisible
)
379 gtk_widget_show( m_vScrollbar
);
380 m_vScrollbarVisible
= TRUE
;
385 wxString
wxTextCtrl::GetValue() const
387 wxCHECK_MSG( m_text
!= NULL
, wxT(""), wxT("invalid text ctrl") );
390 if (m_windowStyle
& wxTE_MULTILINE
)
392 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
393 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
394 tmp
= wxString(text
,*wxConvCurrent
);
399 tmp
= wxString(gtk_entry_get_text( GTK_ENTRY(m_text
) ),*wxConvCurrent
);
404 void wxTextCtrl::SetValue( const wxString
&value
)
406 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
408 if (m_windowStyle
& wxTE_MULTILINE
)
410 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
411 gtk_editable_delete_text( GTK_EDITABLE(m_text
), 0, len
);
414 wxWX2MBbuf tmpbuf
= value
.mbc_str();
415 gtk_editable_insert_text( GTK_EDITABLE(m_text
), tmpbuf
, strlen(tmpbuf
), &len
);
417 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
.mbc_str(), value
.Length(), &len
);
422 gtk_entry_set_text( GTK_ENTRY(m_text
), value
.mbc_str() );
425 // GRG, Jun/2000: Changed this after a lot of discussion in
426 // the lists. wxWindows 2.2 will have a set of flags to
427 // customize this behaviour.
428 SetInsertionPoint(0);
433 void wxTextCtrl::WriteText( const wxString
&text
)
435 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
441 wxWX2MBbuf buf
= text
.mbc_str();
442 const char *txt
= buf
;
443 size_t txtlen
= strlen(buf
);
445 const char *txt
= text
;
446 size_t txtlen
= text
.length();
449 if ( m_windowStyle
& wxTE_MULTILINE
)
451 // After cursor movements, gtk_text_get_point() is wrong by one.
452 gtk_text_set_point( GTK_TEXT(m_text
), GET_EDITABLE_POS(m_text
) );
454 // if we have any special style, use it
455 if ( !m_defaultStyle
.IsDefault() )
459 wxGtkTextInsert(m_text
, m_defaultStyle
, txt
, txtlen
);
463 gint len
= GET_EDITABLE_POS(m_text
);
464 gtk_editable_insert_text( GTK_EDITABLE(m_text
), txt
, txtlen
, &len
);
467 // Bring editable's cursor back uptodate.
468 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
472 // This moves the cursor pos to behind the inserted text.
473 gint len
= GET_EDITABLE_POS(m_text
);
474 gtk_editable_insert_text( GTK_EDITABLE(m_text
), txt
, txtlen
, &len
);
476 // Bring editable's cursor uptodate.
479 // Bring entry's cursor uptodate.
480 gtk_entry_set_position( GTK_ENTRY(m_text
), len
);
486 void wxTextCtrl::AppendText( const wxString
&text
)
488 SetInsertionPointEnd();
492 wxString
wxTextCtrl::GetLineText( long lineNo
) const
494 if (m_windowStyle
& wxTE_MULTILINE
)
496 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
497 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
501 wxString
buf(wxT(""));
504 for (i
= 0; currentLine
!= lineNo
&& text
[i
]; i
++ )
509 for (j
= 0; text
[i
] && text
[i
] != '\n'; i
++, j
++ )
517 return wxEmptyString
;
522 if (lineNo
== 0) return GetValue();
523 return wxEmptyString
;
527 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
529 /* If you implement this, don't forget to update the documentation!
530 * (file docs/latex/wx/text.tex) */
531 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
534 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
536 if ( m_windowStyle
& wxTE_MULTILINE
)
538 wxString text
= GetValue();
540 // cast to prevent warning. But pos really should've been unsigned.
541 if( (unsigned long)pos
> text
.Len() )
547 const wxChar
* stop
= text
.c_str() + pos
;
548 for ( const wxChar
*p
= text
.c_str(); p
< stop
; p
++ )
559 else // single line control
561 if ( pos
<= GTK_ENTRY(m_text
)->text_length
)
568 // index out of bounds
576 long wxTextCtrl::XYToPosition(long x
, long y
) const
578 if (!(m_windowStyle
& wxTE_MULTILINE
)) return 0;
581 for( int i
=0; i
<y
; i
++ ) pos
+= GetLineLength(i
) + 1; // one for '\n'
587 int wxTextCtrl::GetLineLength(long lineNo
) const
589 wxString str
= GetLineText (lineNo
);
590 return (int) str
.Length();
593 int wxTextCtrl::GetNumberOfLines() const
595 if (m_windowStyle
& wxTE_MULTILINE
)
597 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
598 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
603 for (int i
= 0; i
< len
; i
++ )
610 // currentLine is 0 based, add 1 to get number of lines
611 return currentLine
+ 1;
624 void wxTextCtrl::SetInsertionPoint( long pos
)
626 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
628 if (m_windowStyle
& wxTE_MULTILINE
)
630 /* seems to be broken in GTK 1.0.X:
631 gtk_text_set_point( GTK_TEXT(m_text), (int)pos ); */
633 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
634 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
636 /* we fake a set_point by inserting and deleting. as the user
637 isn't supposed to get to know about thos non-sense, we
638 disconnect so that no events are sent to the user program. */
640 gint tmp
= (gint
)pos
;
641 gtk_editable_insert_text( GTK_EDITABLE(m_text
), " ", 1, &tmp
);
642 gtk_editable_delete_text( GTK_EDITABLE(m_text
), tmp
-1, tmp
);
644 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
645 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
647 /* bring editable's cursor uptodate. another bug in GTK. */
649 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
653 gtk_entry_set_position( GTK_ENTRY(m_text
), (int)pos
);
655 /* bring editable's cursor uptodate. bug in GTK. */
657 SET_EDITABLE_POS(m_text
, (guint32
)pos
);
661 void wxTextCtrl::SetInsertionPointEnd()
663 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
665 if (m_windowStyle
& wxTE_MULTILINE
)
666 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text
)));
668 gtk_entry_set_position( GTK_ENTRY(m_text
), -1 );
671 void wxTextCtrl::SetEditable( bool editable
)
673 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
675 if (m_windowStyle
& wxTE_MULTILINE
)
676 gtk_text_set_editable( GTK_TEXT(m_text
), editable
);
678 gtk_entry_set_editable( GTK_ENTRY(m_text
), editable
);
681 bool wxTextCtrl::Enable( bool enable
)
683 if (!wxWindowBase::Enable(enable
))
689 if (m_windowStyle
& wxTE_MULTILINE
)
691 gtk_text_set_editable( GTK_TEXT(m_text
), enable
);
692 OnParentEnable(enable
);
696 gtk_widget_set_sensitive( m_text
, enable
);
702 // wxGTK-specific: called recursively by Enable,
703 // to give widgets an oppprtunity to correct their colours after they
704 // have been changed by Enable
705 void wxTextCtrl::OnParentEnable( bool enable
)
707 // If we have a custom background colour, we use this colour in both
708 // disabled and enabled mode, or we end up with a different colour under the
710 wxColour oldColour
= GetBackgroundColour();
713 // Need to set twice or it'll optimize the useful stuff out
714 if (oldColour
== * wxWHITE
)
715 SetBackgroundColour(*wxBLACK
);
717 SetBackgroundColour(*wxWHITE
);
718 SetBackgroundColour(oldColour
);
722 void wxTextCtrl::DiscardEdits()
727 // ----------------------------------------------------------------------------
728 // max text length support
729 // ----------------------------------------------------------------------------
731 void wxTextCtrl::IgnoreNextTextUpdate()
733 m_ignoreNextUpdate
= TRUE
;
736 bool wxTextCtrl::IgnoreTextUpdate()
738 if ( m_ignoreNextUpdate
)
740 m_ignoreNextUpdate
= FALSE
;
748 void wxTextCtrl::SetMaxLength(unsigned long len
)
750 if ( !HasFlag(wxTE_MULTILINE
) )
752 gtk_entry_set_max_length(GTK_ENTRY(m_text
), len
);
754 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
755 // we had tried to enter more text than allowed by max text length and
756 // the text wasn't really changed
758 // to detect this and generate TEXT_MAXLEN event instead of
759 // TEXT_CHANGED one in this case we also catch "insert_text" signal
761 // when max len is set to 0 we disconnect our handler as it means that
762 // we shouldn't check anything any more
765 gtk_signal_connect( GTK_OBJECT(m_text
),
767 GTK_SIGNAL_FUNC(gtk_insert_text_callback
),
772 gtk_signal_disconnect_by_func
775 GTK_SIGNAL_FUNC(gtk_insert_text_callback
),
782 void wxTextCtrl::SetSelection( long from
, long to
)
784 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
786 if ( (m_windowStyle
& wxTE_MULTILINE
) &&
787 !GTK_TEXT(m_text
)->line_start_cache
)
789 // tell the programmer that it didn't work
790 wxLogDebug(_T("Can't call SetSelection() before realizing the control"));
794 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
797 void wxTextCtrl::ShowPosition( long pos
)
799 if (m_windowStyle
& wxTE_MULTILINE
)
801 GtkAdjustment
*vp
= GTK_TEXT(m_text
)->vadj
;
802 float totalLines
= (float) GetNumberOfLines();
805 PositionToXY(pos
, &posX
, &posY
);
806 float posLine
= (float) posY
;
807 float p
= (posLine
/totalLines
)*(vp
->upper
- vp
->lower
) + vp
->lower
;
808 gtk_adjustment_set_value(GTK_TEXT(m_text
)->vadj
, p
);
812 long wxTextCtrl::GetInsertionPoint() const
814 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
816 return (long) GET_EDITABLE_POS(m_text
);
819 long wxTextCtrl::GetLastPosition() const
821 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
824 if (m_windowStyle
& wxTE_MULTILINE
)
825 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
827 pos
= GTK_ENTRY(m_text
)->text_length
;
832 void wxTextCtrl::Remove( long from
, long to
)
834 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
836 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
839 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
841 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
843 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
845 if (!value
.IsEmpty())
847 gint pos
= (gint
)from
;
849 wxWX2MBbuf buf
= value
.mbc_str();
850 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buf
, strlen(buf
), &pos
);
852 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.Length(), &pos
);
857 void wxTextCtrl::Cut()
859 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
861 gtk_editable_cut_clipboard( GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
864 void wxTextCtrl::Copy()
866 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
868 gtk_editable_copy_clipboard( GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
871 void wxTextCtrl::Paste()
873 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
875 gtk_editable_paste_clipboard( GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
879 void wxTextCtrl::Undo()
882 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
885 void wxTextCtrl::Redo()
888 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
891 bool wxTextCtrl::CanUndo() const
894 wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
898 bool wxTextCtrl::CanRedo() const
901 wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
905 // If the return values from and to are the same, there is no
907 void wxTextCtrl::GetSelection(long* fromOut
, long* toOut
) const
909 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
913 if ( !gtk_editable_get_selection_bounds(GTK_EDITABLE(m_text
), &from
, &to
) )
915 if ( !(GTK_EDITABLE(m_text
)->has_selection
) )
919 to
= GetInsertionPoint();
921 else // got selection
924 from
= (long) GTK_EDITABLE(m_text
)->selection_start_pos
;
925 to
= (long) GTK_EDITABLE(m_text
)->selection_end_pos
;
930 // exchange them to be compatible with wxMSW
943 bool wxTextCtrl::IsEditable() const
945 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
948 return gtk_editable_get_editable(GTK_EDITABLE(m_text
));
950 return GTK_EDITABLE(m_text
)->editable
;
954 bool wxTextCtrl::IsModified() const
959 void wxTextCtrl::Clear()
964 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
966 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
968 if ((key_event
.KeyCode() == WXK_RETURN
) && (m_windowStyle
& wxPROCESS_ENTER
))
970 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
971 event
.SetEventObject(this);
972 event
.SetString(GetValue());
973 if (GetEventHandler()->ProcessEvent(event
)) return;
976 if ((key_event
.KeyCode() == WXK_RETURN
) && !(m_windowStyle
& wxTE_MULTILINE
))
978 // This will invoke the dialog default action, such
979 // as the clicking the default button.
981 wxWindow
*top_frame
= m_parent
;
982 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
983 top_frame
= top_frame
->GetParent();
985 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
987 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
989 if (window
->default_widget
)
991 gtk_widget_activate (window
->default_widget
);
1000 GtkWidget
* wxTextCtrl::GetConnectWidget()
1002 return GTK_WIDGET(m_text
);
1005 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow
*window
)
1007 if (m_windowStyle
& wxTE_MULTILINE
)
1008 return (window
== GTK_TEXT(m_text
)->text_area
);
1010 return (window
== GTK_ENTRY(m_text
)->text_area
);
1013 // the font will change for subsequent text insertiongs
1014 bool wxTextCtrl::SetFont( const wxFont
&font
)
1016 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
1018 if ( !wxTextCtrlBase::SetFont(font
) )
1020 // font didn't change, nothing to do
1024 if ( m_windowStyle
& wxTE_MULTILINE
)
1026 m_updateFont
= TRUE
;
1028 m_defaultStyle
.SetFont(font
);
1030 ChangeFontGlobally();
1036 void wxTextCtrl::ChangeFontGlobally()
1038 // this method is very inefficient and hence should be called as rarely as
1040 wxASSERT_MSG( (m_windowStyle
& wxTE_MULTILINE
) && m_updateFont
,
1041 _T("shouldn't be called for single line controls") );
1043 wxString value
= GetValue();
1044 if ( !value
.IsEmpty() )
1046 m_updateFont
= FALSE
;
1053 void wxTextCtrl::UpdateFontIfNeeded()
1056 ChangeFontGlobally();
1059 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1061 if ( !wxControl::SetForegroundColour(colour
) )
1064 // update default fg colour too
1065 m_defaultStyle
.SetTextColour(colour
);
1070 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
1072 wxCHECK_MSG( m_text
!= NULL
, FALSE
, wxT("invalid text ctrl") );
1074 if ( !wxControl::SetBackgroundColour( colour
) )
1077 if (!m_widget
->window
)
1080 wxColour sysbg
= wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
);
1081 if (sysbg
.Red() == colour
.Red() &&
1082 sysbg
.Green() == colour
.Green() &&
1083 sysbg
.Blue() == colour
.Blue())
1085 return FALSE
; // FIXME or TRUE?
1088 if (!m_backgroundColour
.Ok())
1091 if (m_windowStyle
& wxTE_MULTILINE
)
1093 GdkWindow
*window
= GTK_TEXT(m_text
)->text_area
;
1096 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
1097 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
1098 gdk_window_clear( window
);
1101 // change active background color too
1102 m_defaultStyle
.SetBackgroundColour( colour
);
1107 bool wxTextCtrl::SetStyle( long start
, long end
, const wxTextAttr
& style
)
1109 /* VERY dirty way to do that - removes the required text and re-adds it
1110 with styling (FIXME) */
1111 if ( m_windowStyle
& wxTE_MULTILINE
)
1113 if ( style
.IsDefault() )
1119 gint l
= gtk_text_get_length( GTK_TEXT(m_text
) );
1121 wxCHECK_MSG( start
>= 0 && end
<= l
, FALSE
,
1122 _T("invalid range in wxTextCtrl::SetStyle") );
1124 gint old_pos
= gtk_editable_get_position( GTK_EDITABLE(m_text
) );
1125 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), start
, end
);
1126 wxString
tmp(text
,*wxConvCurrent
);
1129 gtk_editable_delete_text( GTK_EDITABLE(m_text
), start
, end
);
1130 gtk_editable_set_position( GTK_EDITABLE(m_text
), start
);
1133 wxWX2MBbuf buf
= tmp
.mbc_str();
1134 const char *txt
= buf
;
1135 size_t txtlen
= strlen(buf
);
1137 const char *txt
= tmp
;
1138 size_t txtlen
= tmp
.length();
1141 // use the attributes from style which are set in it and fall back
1142 // first to the default style and then to the text control default
1143 // colours for the others
1144 wxGtkTextInsert(m_text
,
1145 wxTextAttr::Combine(style
, m_defaultStyle
, this),
1149 /* does not seem to help under GTK+ 1.2 !!!
1150 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
1151 SetInsertionPoint( old_pos
);
1156 // cannot do this for GTK+'s Entry widget
1161 void wxTextCtrl::ApplyWidgetStyle()
1163 if (m_windowStyle
& wxTE_MULTILINE
)
1170 gtk_widget_set_style( m_text
, m_widgetStyle
);
1174 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1179 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1184 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1189 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1194 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1199 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1201 event
.Enable( CanCut() );
1204 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1206 event
.Enable( CanCopy() );
1209 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1211 event
.Enable( CanPaste() );
1214 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1216 event
.Enable( CanUndo() );
1219 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1221 event
.Enable( CanRedo() );
1224 void wxTextCtrl::OnInternalIdle()
1226 wxCursor cursor
= m_cursor
;
1227 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
1231 GdkWindow
*window
= (GdkWindow
*) NULL
;
1232 if (HasFlag(wxTE_MULTILINE
))
1233 window
= GTK_TEXT(m_text
)->text_area
;
1235 window
= GTK_ENTRY(m_text
)->text_area
;
1238 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1240 if (!g_globalCursor
.Ok())
1241 cursor
= *wxSTANDARD_CURSOR
;
1243 window
= m_widget
->window
;
1244 if ((window
) && !(GTK_WIDGET_NO_WINDOW(m_widget
)))
1245 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1251 wxSize
wxTextCtrl::DoGetBestSize() const
1253 // FIXME should be different for multi-line controls...
1254 wxSize
ret( wxControl::DoGetBestSize() );
1255 return wxSize(80, ret
.y
);
1258 // ----------------------------------------------------------------------------
1260 // ----------------------------------------------------------------------------
1262 void wxTextCtrl::Freeze()
1264 if ( HasFlag(wxTE_MULTILINE
) )
1266 gtk_text_freeze(GTK_TEXT(m_text
));
1270 void wxTextCtrl::Thaw()
1272 if ( HasFlag(wxTE_MULTILINE
) )
1274 GTK_TEXT(m_text
)->vadj
->value
= 0.0;
1276 gtk_text_thaw(GTK_TEXT(m_text
));
1280 // ----------------------------------------------------------------------------
1282 // ----------------------------------------------------------------------------
1284 GtkAdjustment
*wxTextCtrl::GetVAdj() const
1286 return HasFlag(wxTE_MULTILINE
) ? GTK_TEXT(m_text
)->vadj
: NULL
;
1289 bool wxTextCtrl::DoScroll(GtkAdjustment
*adj
, int diff
)
1291 float value
= adj
->value
+ diff
;
1296 float upper
= adj
->upper
- adj
->page_size
;
1297 if ( value
> upper
)
1300 // did we noticeably change the scroll position?
1301 if ( fabs(adj
->value
- value
) < 0.2 )
1303 // well, this is what Robert does in wxScrollBar, so it must be good...
1309 gtk_signal_emit_by_name(GTK_OBJECT(adj
), "value_changed");
1314 bool wxTextCtrl::ScrollLines(int lines
)
1316 GtkAdjustment
*adj
= GetVAdj();
1320 // this is hardcoded to 10 in GTK+ 1.2 (great idea)
1321 static const int KEY_SCROLL_PIXELS
= 10;
1323 return DoScroll(adj
, lines
*KEY_SCROLL_PIXELS
);
1326 bool wxTextCtrl::ScrollPages(int pages
)
1328 GtkAdjustment
*adj
= GetVAdj();
1332 return DoScroll(adj
, (int)ceil(pages
*adj
->page_increment
));