1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/textctrl.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
13 #include "wx/textctrl.h"
21 #include "wx/settings.h"
25 #include "wx/strconv.h"
26 #include "wx/fontutil.h" // for wxNativeFontInfo (GetNativeFontInfo())
27 #include "wx/evtloop.h"
29 #include <sys/types.h>
33 #include "wx/gtk1/private.h"
34 #include <gdk/gdkkeysyms.h>
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
40 extern void wxapp_install_idle_handler();
43 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
47 extern wxCursor g_globalCursor
;
48 extern wxWindowGTK
*g_delayedFocus
;
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
55 static void wxGtkTextInsert(GtkWidget
*text
,
56 const wxTextAttr
& attr
,
64 tmpFont
= attr
.GetFont();
66 // FIXME: if this crashes because tmpFont goes out of scope and the GdkFont is
67 // deleted, then we need to call gdk_font_ref on font.
68 // This is because attr.GetFont() now returns a temporary font since wxTextAttr
69 // no longer stores a wxFont object, for efficiency.
71 font
= tmpFont
.GetInternalFont();
76 GdkColor
*colFg
= attr
.HasTextColour() ? attr
.GetTextColour().GetColor()
79 GdkColor
*colBg
= attr
.HasBackgroundColour()
80 ? attr
.GetBackgroundColour().GetColor()
83 gtk_text_insert( GTK_TEXT(text
), font
, colFg
, colBg
, txt
, len
);
87 // ----------------------------------------------------------------------------
88 // "insert_text" for GtkEntry
89 // ----------------------------------------------------------------------------
93 gtk_insert_text_callback(GtkEditable
*editable
,
94 const gchar
*WXUNUSED(new_text
),
95 gint
WXUNUSED(new_text_length
),
96 gint
*WXUNUSED(position
),
100 wxapp_install_idle_handler();
102 // we should only be called if we have a max len limit at all
103 GtkEntry
*entry
= GTK_ENTRY (editable
);
105 wxCHECK_RET( entry
->text_max_length
, _T("shouldn't be called") );
107 // check that we don't overflow the max length limit
109 // FIXME: this doesn't work when we paste a string which is going to be
111 if ( entry
->text_length
== entry
->text_max_length
)
113 // we don't need to run the base class version at all
114 gtk_signal_emit_stop_by_name(GTK_OBJECT(editable
), "insert_text");
116 // remember that the next changed signal is to be ignored to avoid
117 // generating a dummy wxEVT_COMMAND_TEXT_UPDATED event
118 win
->IgnoreNextTextUpdate();
120 // and generate the correct one ourselves
121 wxCommandEvent
event(wxEVT_COMMAND_TEXT_MAXLEN
, win
->GetId());
122 event
.SetEventObject(win
);
123 event
.SetString(win
->GetValue());
124 win
->HandleWindowEvent( event
);
129 //-----------------------------------------------------------------------------
131 //-----------------------------------------------------------------------------
135 gtk_text_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
137 if ( win
->IgnoreTextUpdate() )
140 if (!win
->m_hasVMT
) return;
143 wxapp_install_idle_handler();
146 win
->UpdateFontIfNeeded();
148 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, win
->GetId() );
149 event
.SetEventObject( win
);
150 win
->HandleWindowEvent( event
);
154 //-----------------------------------------------------------------------------
155 // "changed" from vertical scrollbar
156 //-----------------------------------------------------------------------------
160 gtk_scrollbar_changed_callback( GtkWidget
*WXUNUSED(widget
), wxTextCtrl
*win
)
162 if (!win
->m_hasVMT
) return;
165 wxapp_install_idle_handler();
167 win
->CalculateScrollbar();
171 // ----------------------------------------------------------------------------
172 // redraw callback for multiline text
173 // ----------------------------------------------------------------------------
175 // redrawing a GtkText from inside a wxYield() call results in crashes (the
176 // text sample shows it in its "Add lines" command which shows wxProgressDialog
177 // which implicitly calls wxYield()) so we override GtkText::draw() and simply
178 // don't do anything if we're inside wxYield()
181 typedef void (*GtkDrawCallback
)(GtkWidget
*widget
, GdkRectangle
*rect
);
184 static GtkDrawCallback gs_gtk_text_draw
= NULL
;
187 static void wxgtk_text_draw( GtkWidget
*widget
, GdkRectangle
*rect
)
189 wxEventLoopBase
* loop
= wxEventLoopBase::GetActive();
190 if ( loop
&& !loop
->IsYielding() )
192 wxCHECK_RET( gs_gtk_text_draw
!= wxgtk_text_draw
,
193 _T("infinite recursion in wxgtk_text_draw aborted") );
195 gs_gtk_text_draw(widget
, rect
);
200 //-----------------------------------------------------------------------------
202 //-----------------------------------------------------------------------------
204 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxTextCtrlBase
)
206 BEGIN_EVENT_TABLE(wxTextCtrl
, wxTextCtrlBase
)
207 EVT_CHAR(wxTextCtrl::OnChar
)
209 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
210 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
211 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
212 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
213 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
215 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
216 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
217 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
218 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
219 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
222 void wxTextCtrl::Init()
226 SetUpdateFont(false);
231 wxTextCtrl::~wxTextCtrl()
235 wxTextCtrl::wxTextCtrl( wxWindow
*parent
,
237 const wxString
&value
,
241 const wxValidator
& validator
,
242 const wxString
&name
)
246 Create( parent
, id
, value
, pos
, size
, style
, validator
, name
);
249 bool wxTextCtrl::Create( wxWindow
*parent
,
251 const wxString
&value
,
255 const wxValidator
& validator
,
256 const wxString
&name
)
259 m_acceptsFocus
= true;
261 if (!PreCreation( parent
, pos
, size
) ||
262 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
264 wxFAIL_MSG( wxT("wxTextCtrl creation failed") );
269 m_vScrollbarVisible
= false;
271 bool multi_line
= (style
& wxTE_MULTILINE
) != 0;
275 // create our control ...
276 m_text
= gtk_text_new( NULL
, NULL
);
278 // ... and put into the upper left hand corner of the table
279 bool bHasHScrollbar
= false;
280 m_widget
= gtk_table_new(bHasHScrollbar
? 2 : 1, 2, FALSE
);
281 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
282 gtk_table_attach( GTK_TABLE(m_widget
), m_text
, 0, 1, 0, 1,
283 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
284 (GtkAttachOptions
)(GTK_FILL
| GTK_EXPAND
| GTK_SHRINK
),
288 gtk_text_set_word_wrap( GTK_TEXT(m_text
), TRUE
);
290 // finally, put the vertical scrollbar in the upper right corner
291 m_vScrollbar
= gtk_vscrollbar_new( GTK_TEXT(m_text
)->vadj
);
292 GTK_WIDGET_UNSET_FLAGS( m_vScrollbar
, GTK_CAN_FOCUS
);
293 gtk_table_attach(GTK_TABLE(m_widget
), m_vScrollbar
, 1, 2, 0, 1,
295 (GtkAttachOptions
)(GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
),
300 // a single-line text control: no need for scrollbars
302 m_text
= gtk_entry_new();
305 m_parent
->DoAddChild( this );
307 m_focusWidget
= m_text
;
312 gtk_widget_show(m_text
);
316 gtk_signal_connect(GTK_OBJECT(GTK_TEXT(m_text
)->vadj
), "changed",
317 (GtkSignalFunc
) gtk_scrollbar_changed_callback
, (gpointer
) this );
319 // only initialize gs_gtk_text_draw once, starting from the next the
320 // klass::draw will already be wxgtk_text_draw
321 if ( !gs_gtk_text_draw
)
324 draw
= GTK_WIDGET_CLASS(GTK_OBJECT(m_text
)->klass
)->draw
;
326 gs_gtk_text_draw
= draw
;
328 draw
= wxgtk_text_draw
;
334 #if !GTK_CHECK_VERSION(1, 2, 0)
335 // if we don't realize it, GTK 1.0.6 dies with a SIGSEGV in
336 // gtk_editable_insert_text()
337 gtk_widget_realize(m_text
);
342 wxWX2MBbuf val
= value
.mbc_str();
343 gtk_editable_insert_text( GTK_EDITABLE(m_text
), val
, strlen(val
), &tmp
);
345 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.length(), &tmp
);
350 // Bring editable's cursor uptodate. Bug in GTK.
351 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
355 if (style
& wxTE_PASSWORD
)
358 gtk_entry_set_visibility( GTK_ENTRY(m_text
), FALSE
);
361 if (style
& wxTE_READONLY
)
364 gtk_entry_set_editable( GTK_ENTRY(m_text
), FALSE
);
369 gtk_text_set_editable( GTK_TEXT(m_text
), 1 );
372 // We want to be notified about text changes.
373 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
374 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
376 m_cursor
= wxCursor( wxCURSOR_IBEAM
);
378 wxTextAttr
attrDef(GetForegroundColour(), GetBackgroundColour(), GetFont());
379 SetDefaultStyle( attrDef
);
385 void wxTextCtrl::CalculateScrollbar()
387 if ((m_windowStyle
& wxTE_MULTILINE
) == 0) return;
389 GtkAdjustment
*adj
= GTK_TEXT(m_text
)->vadj
;
391 if (adj
->upper
- adj
->page_size
< 0.8)
393 if (m_vScrollbarVisible
)
395 gtk_widget_hide( m_vScrollbar
);
396 m_vScrollbarVisible
= false;
401 if (!m_vScrollbarVisible
)
403 gtk_widget_show( m_vScrollbar
);
404 m_vScrollbarVisible
= true;
409 wxString
wxTextCtrl::DoGetValue() const
411 wxCHECK_MSG( m_text
!= NULL
, wxEmptyString
, wxT("invalid text ctrl") );
414 if (m_windowStyle
& wxTE_MULTILINE
)
416 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
417 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
423 tmp
= wxGTK_CONV_BACK( gtk_entry_get_text( GTK_ENTRY(m_text
) ) );
429 void wxTextCtrl::DoSetValue( const wxString
&value
, int flags
)
431 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
433 if ( !(flags
& SetValue_SendEvent
) )
435 // do not generate events
436 IgnoreNextTextUpdate();
439 if (m_windowStyle
& wxTE_MULTILINE
)
441 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
442 gtk_editable_delete_text( GTK_EDITABLE(m_text
), 0, len
);
444 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
.mbc_str(), value
.length(), &len
);
448 gtk_entry_set_text( GTK_ENTRY(m_text
), wxGTK_CONV( value
) );
451 // GRG, Jun/2000: Changed this after a lot of discussion in
452 // the lists. wxWidgets 2.2 will have a set of flags to
453 // customize this behaviour.
454 SetInsertionPoint(0);
459 void wxTextCtrl::WriteText( const wxString
&text
)
461 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
466 // gtk_text_changed_callback() will set m_modified to true but m_modified
467 // shouldn't be changed by the program writing to the text control itself,
468 // so save the old value and restore when we're done
469 bool oldModified
= m_modified
;
471 if ( m_windowStyle
& wxTE_MULTILINE
)
473 // After cursor movements, gtk_text_get_point() is wrong by one.
474 gtk_text_set_point( GTK_TEXT(m_text
), GET_EDITABLE_POS(m_text
) );
476 // always use m_defaultStyle, even if it is empty as otherwise
477 // resetting the style and appending some more text wouldn't work: if
478 // we don't specify the style explicitly, the old style would be used
479 gtk_editable_delete_selection( GTK_EDITABLE(m_text
) );
480 wxGtkTextInsert(m_text
, m_defaultStyle
, text
.c_str(), text
.length());
482 // we called wxGtkTextInsert with correct font, no need to do anything
483 // in UpdateFontIfNeeded() any longer
486 SetUpdateFont(false);
489 // Bring editable's cursor back uptodate.
490 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
494 // First remove the selection if there is one
495 gtk_editable_delete_selection( GTK_EDITABLE(m_text
) );
497 // This moves the cursor pos to behind the inserted text.
498 gint len
= GET_EDITABLE_POS(m_text
);
500 gtk_editable_insert_text( GTK_EDITABLE(m_text
), text
.c_str(), text
.length(), &len
);
502 // Bring entry's cursor uptodate.
503 gtk_entry_set_position( GTK_ENTRY(m_text
), len
);
506 m_modified
= oldModified
;
509 void wxTextCtrl::AppendText( const wxString
&text
)
511 SetInsertionPointEnd();
515 wxString
wxTextCtrl::GetLineText( long lineNo
) const
517 if (m_windowStyle
& wxTE_MULTILINE
)
519 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
520 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
527 for (i
= 0; currentLine
!= lineNo
&& text
[i
]; i
++ )
532 for (j
= 0; text
[i
] && text
[i
] != '\n'; i
++, j
++ )
540 return wxEmptyString
;
545 if (lineNo
== 0) return GetValue();
546 return wxEmptyString
;
550 void wxTextCtrl::OnDropFiles( wxDropFilesEvent
&WXUNUSED(event
) )
552 /* If you implement this, don't forget to update the documentation!
553 * (file docs/latex/wx/text.tex) */
554 wxFAIL_MSG( wxT("wxTextCtrl::OnDropFiles not implemented") );
557 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
559 if ( m_windowStyle
& wxTE_MULTILINE
)
561 wxString text
= GetValue();
563 // cast to prevent warning. But pos really should've been unsigned.
564 if( (unsigned long)pos
> text
.length() )
570 const wxChar
* stop
= text
.c_str() + pos
;
571 for ( const wxChar
*p
= text
.c_str(); p
< stop
; p
++ )
582 else // single line control
584 if ( pos
<= GTK_ENTRY(m_text
)->text_length
)
591 // index out of bounds
599 long wxTextCtrl::XYToPosition(long x
, long y
) const
601 if (!(m_windowStyle
& wxTE_MULTILINE
)) return 0;
604 for( int i
=0; i
<y
; i
++ ) pos
+= GetLineLength(i
) + 1; // one for '\n'
610 int wxTextCtrl::GetLineLength(long lineNo
) const
612 wxString str
= GetLineText (lineNo
);
613 return (int) str
.length();
616 int wxTextCtrl::GetNumberOfLines() const
618 if (m_windowStyle
& wxTE_MULTILINE
)
620 gint len
= gtk_text_get_length( GTK_TEXT(m_text
) );
621 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), 0, len
);
626 for (int i
= 0; i
< len
; i
++ )
633 // currentLine is 0 based, add 1 to get number of lines
634 return currentLine
+ 1;
647 void wxTextCtrl::SetInsertionPoint( long pos
)
649 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
653 gtk_signal_disconnect_by_func( GTK_OBJECT(m_text
),
654 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
656 /* we fake a set_point by inserting and deleting. as the user
657 isn't supposed to get to know about this non-sense, we
658 disconnect so that no events are sent to the user program. */
660 gint tmp
= (gint
)pos
;
661 gtk_editable_insert_text( GTK_EDITABLE(m_text
), " ", 1, &tmp
);
662 gtk_editable_delete_text( GTK_EDITABLE(m_text
), tmp
-1, tmp
);
664 gtk_signal_connect( GTK_OBJECT(m_text
), "changed",
665 GTK_SIGNAL_FUNC(gtk_text_changed_callback
), (gpointer
)this);
667 // bring editable's cursor uptodate. Bug in GTK.
668 SET_EDITABLE_POS(m_text
, gtk_text_get_point( GTK_TEXT(m_text
) ));
672 gtk_entry_set_position( GTK_ENTRY(m_text
), (int)pos
);
674 // Bring editable's cursor uptodate. Bug in GTK.
675 SET_EDITABLE_POS(m_text
, (guint32
)pos
);
679 void wxTextCtrl::SetInsertionPointEnd()
681 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
683 if (m_windowStyle
& wxTE_MULTILINE
)
685 SetInsertionPoint(gtk_text_get_length(GTK_TEXT(m_text
)));
689 gtk_entry_set_position( GTK_ENTRY(m_text
), -1 );
693 void wxTextCtrl::SetEditable( bool editable
)
695 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
697 if (m_windowStyle
& wxTE_MULTILINE
)
699 gtk_text_set_editable( GTK_TEXT(m_text
), editable
);
703 gtk_entry_set_editable( GTK_ENTRY(m_text
), editable
);
707 void wxTextCtrl::DoEnable( bool enable
)
709 if (m_windowStyle
& wxTE_MULTILINE
)
711 gtk_text_set_editable( GTK_TEXT(m_text
), enable
);
715 gtk_widget_set_sensitive( m_text
, enable
);
719 // wxGTK-specific: called recursively by Enable,
720 // to give widgets an oppprtunity to correct their colours after they
721 // have been changed by Enable
722 void wxTextCtrl::OnEnabled( bool WXUNUSED(enable
) )
724 if ( IsSingleLine() )
727 // If we have a custom background colour, we use this colour in both
728 // disabled and enabled mode, or we end up with a different colour under the
730 wxColour oldColour
= GetBackgroundColour();
733 // Need to set twice or it'll optimize the useful stuff out
734 if (oldColour
== * wxWHITE
)
735 SetBackgroundColour(*wxBLACK
);
737 SetBackgroundColour(*wxWHITE
);
738 SetBackgroundColour(oldColour
);
742 void wxTextCtrl::MarkDirty()
747 void wxTextCtrl::DiscardEdits()
752 // ----------------------------------------------------------------------------
753 // max text length support
754 // ----------------------------------------------------------------------------
756 void wxTextCtrl::IgnoreNextTextUpdate()
758 m_ignoreNextUpdate
= true;
761 bool wxTextCtrl::IgnoreTextUpdate()
763 if ( m_ignoreNextUpdate
)
765 m_ignoreNextUpdate
= false;
773 void wxTextCtrl::SetMaxLength(unsigned long len
)
775 if ( !HasFlag(wxTE_MULTILINE
) )
777 gtk_entry_set_max_length(GTK_ENTRY(m_text
), len
);
779 // there is a bug in GTK+ 1.2.x: "changed" signal is emitted even if
780 // we had tried to enter more text than allowed by max text length and
781 // the text wasn't really changed
783 // to detect this and generate TEXT_MAXLEN event instead of
784 // TEXT_CHANGED one in this case we also catch "insert_text" signal
786 // when max len is set to 0 we disconnect our handler as it means that
787 // we shouldn't check anything any more
790 gtk_signal_connect( GTK_OBJECT(m_text
),
792 GTK_SIGNAL_FUNC(gtk_insert_text_callback
),
797 gtk_signal_disconnect_by_func
800 GTK_SIGNAL_FUNC(gtk_insert_text_callback
),
807 void wxTextCtrl::SetSelection( long from
, long to
)
809 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
811 if (from
== -1 && to
== -1)
814 to
= GetValue().length();
817 if ( (m_windowStyle
& wxTE_MULTILINE
) &&
818 !GTK_TEXT(m_text
)->line_start_cache
)
820 // tell the programmer that it didn't work
821 wxLogDebug(_T("Can't call SetSelection() before realizing the control"));
825 if (m_windowStyle
& wxTE_MULTILINE
)
827 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
831 gtk_editable_select_region( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
835 void wxTextCtrl::ShowPosition( long pos
)
837 if (m_windowStyle
& wxTE_MULTILINE
)
839 GtkAdjustment
*vp
= GTK_TEXT(m_text
)->vadj
;
840 float totalLines
= (float) GetNumberOfLines();
843 PositionToXY(pos
, &posX
, &posY
);
844 float posLine
= (float) posY
;
845 float p
= (posLine
/totalLines
)*(vp
->upper
- vp
->lower
) + vp
->lower
;
846 gtk_adjustment_set_value(GTK_TEXT(m_text
)->vadj
, p
);
850 long wxTextCtrl::GetInsertionPoint() const
852 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
853 return (long) GET_EDITABLE_POS(m_text
);
856 wxTextPos
wxTextCtrl::GetLastPosition() const
858 wxCHECK_MSG( m_text
!= NULL
, 0, wxT("invalid text ctrl") );
862 if (m_windowStyle
& wxTE_MULTILINE
)
864 pos
= gtk_text_get_length( GTK_TEXT(m_text
) );
868 pos
= GTK_ENTRY(m_text
)->text_length
;
874 void wxTextCtrl::Remove( long from
, long to
)
876 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
877 gtk_editable_delete_text( GTK_EDITABLE(m_text
), (gint
)from
, (gint
)to
);
880 void wxTextCtrl::Replace( long from
, long to
, const wxString
&value
)
882 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
888 gint pos
= (gint
)from
;
890 wxWX2MBbuf buf
= value
.mbc_str();
891 gtk_editable_insert_text( GTK_EDITABLE(m_text
), buf
, strlen(buf
), &pos
);
893 gtk_editable_insert_text( GTK_EDITABLE(m_text
), value
, value
.length(), &pos
);
894 #endif // wxUSE_UNICODE
898 void wxTextCtrl::Cut()
900 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
901 gtk_editable_cut_clipboard(GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
904 void wxTextCtrl::Copy()
906 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
907 gtk_editable_copy_clipboard(GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
910 void wxTextCtrl::Paste()
912 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
913 gtk_editable_paste_clipboard(GTK_EDITABLE(m_text
) DUMMY_CLIPBOARD_ARG
);
917 void wxTextCtrl::Undo()
920 wxFAIL_MSG( wxT("wxTextCtrl::Undo not implemented") );
923 void wxTextCtrl::Redo()
926 wxFAIL_MSG( wxT("wxTextCtrl::Redo not implemented") );
929 bool wxTextCtrl::CanUndo() const
932 //wxFAIL_MSG( wxT("wxTextCtrl::CanUndo not implemented") );
936 bool wxTextCtrl::CanRedo() const
939 //wxFAIL_MSG( wxT("wxTextCtrl::CanRedo not implemented") );
943 // If the return values from and to are the same, there is no
945 void wxTextCtrl::GetSelection(long* fromOut
, long* toOut
) const
947 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
951 bool haveSelection
= false;
953 if ( (GTK_EDITABLE(m_text
)->has_selection
) )
955 haveSelection
= true;
956 from
= (long) GTK_EDITABLE(m_text
)->selection_start_pos
;
957 to
= (long) GTK_EDITABLE(m_text
)->selection_end_pos
;
960 if (! haveSelection
)
961 from
= to
= GetInsertionPoint();
965 // exchange them to be compatible with wxMSW
978 bool wxTextCtrl::IsEditable() const
980 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
981 return GTK_EDITABLE(m_text
)->editable
;
984 bool wxTextCtrl::IsModified() const
989 void wxTextCtrl::Clear()
991 SetValue( wxEmptyString
);
994 void wxTextCtrl::OnChar( wxKeyEvent
&key_event
)
996 wxCHECK_RET( m_text
!= NULL
, wxT("invalid text ctrl") );
998 if ((key_event
.GetKeyCode() == WXK_RETURN
) && (m_windowStyle
& wxTE_PROCESS_ENTER
))
1000 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
1001 event
.SetEventObject(this);
1002 event
.SetString(GetValue());
1003 if (HandleWindowEvent(event
)) return;
1006 if ((key_event
.GetKeyCode() == WXK_RETURN
) && !(m_windowStyle
& wxTE_MULTILINE
))
1008 // This will invoke the dialog default action, such
1009 // as the clicking the default button.
1011 wxWindow
*top_frame
= m_parent
;
1012 while (top_frame
->GetParent() && !(top_frame
->IsTopLevel()))
1013 top_frame
= top_frame
->GetParent();
1015 if (top_frame
&& GTK_IS_WINDOW(top_frame
->m_widget
))
1017 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
1019 if (window
->default_widget
)
1021 gtk_widget_activate (window
->default_widget
);
1030 GtkWidget
* wxTextCtrl::GetConnectWidget()
1032 return GTK_WIDGET(m_text
);
1035 bool wxTextCtrl::IsOwnGtkWindow( GdkWindow
*window
)
1037 if (m_windowStyle
& wxTE_MULTILINE
)
1039 return (window
== GTK_TEXT(m_text
)->text_area
);
1043 return (window
== GTK_ENTRY(m_text
)->text_area
);
1047 // the font will change for subsequent text insertiongs
1048 bool wxTextCtrl::SetFont( const wxFont
&font
)
1050 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1052 if ( !wxTextCtrlBase::SetFont(font
) )
1054 // font didn't change, nothing to do
1058 if ( m_windowStyle
& wxTE_MULTILINE
)
1060 SetUpdateFont(true);
1062 m_defaultStyle
.SetFont(font
);
1064 ChangeFontGlobally();
1070 void wxTextCtrl::ChangeFontGlobally()
1072 // this method is very inefficient and hence should be called as rarely as
1074 wxASSERT_MSG( (m_windowStyle
& wxTE_MULTILINE
) && m_updateFont
,
1076 _T("shouldn't be called for single line controls") );
1078 wxString value
= GetValue();
1079 if ( !value
.empty() )
1081 SetUpdateFont(false);
1088 void wxTextCtrl::UpdateFontIfNeeded()
1091 ChangeFontGlobally();
1094 bool wxTextCtrl::SetForegroundColour(const wxColour
& colour
)
1096 if ( !wxControl::SetForegroundColour(colour
) )
1099 // update default fg colour too
1100 m_defaultStyle
.SetTextColour(colour
);
1105 bool wxTextCtrl::SetBackgroundColour( const wxColour
&colour
)
1107 wxCHECK_MSG( m_text
!= NULL
, false, wxT("invalid text ctrl") );
1109 if ( !wxControl::SetBackgroundColour( colour
) )
1112 if (!m_widget
->window
)
1115 if (!m_backgroundColour
.Ok())
1118 if (m_windowStyle
& wxTE_MULTILINE
)
1120 GdkWindow
*window
= GTK_TEXT(m_text
)->text_area
;
1123 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
1124 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
1125 gdk_window_clear( window
);
1128 // change active background color too
1129 m_defaultStyle
.SetBackgroundColour( colour
);
1134 bool wxTextCtrl::SetStyle( long start
, long end
, const wxTextAttr
& style
)
1136 if ( m_windowStyle
& wxTE_MULTILINE
)
1138 if ( style
.IsDefault() )
1144 // VERY dirty way to do that - removes the required text and re-adds it
1145 // with styling (FIXME)
1147 gint l
= gtk_text_get_length( GTK_TEXT(m_text
) );
1149 wxCHECK_MSG( start
>= 0 && end
<= l
, false,
1150 _T("invalid range in wxTextCtrl::SetStyle") );
1152 gint old_pos
= gtk_editable_get_position( GTK_EDITABLE(m_text
) );
1153 char *text
= gtk_editable_get_chars( GTK_EDITABLE(m_text
), start
, end
);
1154 wxString
tmp(text
,*wxConvCurrent
);
1157 gtk_editable_delete_text( GTK_EDITABLE(m_text
), start
, end
);
1158 gtk_editable_set_position( GTK_EDITABLE(m_text
), start
);
1161 wxWX2MBbuf buf
= tmp
.mbc_str();
1162 const char *txt
= buf
;
1163 size_t txtlen
= strlen(buf
);
1165 const char *txt
= tmp
;
1166 size_t txtlen
= tmp
.length();
1169 // use the attributes from style which are set in it and fall back
1170 // first to the default style and then to the text control default
1171 // colours for the others
1172 wxGtkTextInsert(m_text
,
1173 wxTextAttr::Combine(style
, m_defaultStyle
, this),
1177 /* does not seem to help under GTK+ 1.2 !!!
1178 gtk_editable_set_position( GTK_EDITABLE(m_text), old_pos ); */
1179 SetInsertionPoint( old_pos
);
1185 // cannot do this for GTK+'s Entry widget
1189 void wxTextCtrl::DoApplyWidgetStyle(GtkRcStyle
*style
)
1191 gtk_widget_modify_style(m_text
, style
);
1194 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
1199 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1204 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1209 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
1214 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
1219 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1221 event
.Enable( CanCut() );
1224 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1226 event
.Enable( CanCopy() );
1229 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1231 event
.Enable( CanPaste() );
1234 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1236 event
.Enable( CanUndo() );
1239 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1241 event
.Enable( CanRedo() );
1244 void wxTextCtrl::OnInternalIdle()
1246 wxCursor cursor
= m_cursor
;
1247 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
1251 GdkWindow
*window
= NULL
;
1252 if (HasFlag(wxTE_MULTILINE
))
1253 window
= GTK_TEXT(m_text
)->text_area
;
1255 window
= GTK_ENTRY(m_text
)->text_area
;
1258 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1260 if (!g_globalCursor
.Ok())
1261 cursor
= *wxSTANDARD_CURSOR
;
1263 window
= m_widget
->window
;
1264 if ((window
) && !(GTK_WIDGET_NO_WINDOW(m_widget
)))
1265 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1268 if (g_delayedFocus
== this)
1270 if (GTK_WIDGET_REALIZED(m_widget
))
1272 gtk_widget_grab_focus( m_widget
);
1273 g_delayedFocus
= NULL
;
1277 if (wxUpdateUIEvent::CanUpdate(this))
1278 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
1281 wxSize
wxTextCtrl::DoGetBestSize() const
1283 // FIXME should be different for multi-line controls...
1284 wxSize
ret( wxControl::DoGetBestSize() );
1285 wxSize
best(80, ret
.y
);
1286 CacheBestSize(best
);
1290 // ----------------------------------------------------------------------------
1292 // ----------------------------------------------------------------------------
1294 void wxTextCtrl::DoFreeze()
1296 if ( HasFlag(wxTE_MULTILINE
) )
1298 gtk_text_freeze(GTK_TEXT(m_text
));
1302 void wxTextCtrl::DoThaw()
1304 if ( HasFlag(wxTE_MULTILINE
) )
1306 GTK_TEXT(m_text
)->vadj
->value
= 0.0;
1308 gtk_text_thaw(GTK_TEXT(m_text
));
1312 // ----------------------------------------------------------------------------
1314 // ----------------------------------------------------------------------------
1316 GtkAdjustment
*wxTextCtrl::GetVAdj() const
1318 if ( !IsMultiLine() )
1321 return GTK_TEXT(m_text
)->vadj
;
1324 bool wxTextCtrl::DoScroll(GtkAdjustment
*adj
, int diff
)
1326 float value
= adj
->value
+ diff
;
1331 float upper
= adj
->upper
- adj
->page_size
;
1332 if ( value
> upper
)
1335 // did we noticeably change the scroll position?
1336 if ( fabs(adj
->value
- value
) < 0.2 )
1338 // well, this is what Robert does in wxScrollBar, so it must be good...
1343 gtk_signal_emit_by_name(GTK_OBJECT(adj
), "value_changed");
1348 bool wxTextCtrl::ScrollLines(int lines
)
1350 GtkAdjustment
*adj
= GetVAdj();
1354 // this is hardcoded to 10 in GTK+ 1.2 (great idea)
1355 int diff
= 10*lines
;
1357 return DoScroll(adj
, diff
);
1360 bool wxTextCtrl::ScrollPages(int pages
)
1362 GtkAdjustment
*adj
= GetVAdj();
1366 return DoScroll(adj
, (int)ceil(pages
*adj
->page_increment
));
1372 wxTextCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1374 return GetDefaultAttributesFromGTKWidget(gtk_entry_new
, true);