Get rid of overridden wxTextCtrl::DoSetValue() in wxGTK

This method seems completely unnecessary, the base
wxTextEntry::DoSetValue(), which delegates to Remove() and WriteText(),
seems to work just as well and avoids code duplication between this
method and wxTextCtrl::WriteText().

Notice that gtk_text_buffer_set_text() is just a trivial wrapper around
gtk_text_buffer_delete() and gtk_text_buffer_insert() anyhow, so there
is no efficiency loss in not using it neither.
This commit is contained in:
Vadim Zeitlin 2017-08-24 17:25:16 +02:00
parent 1a55f4cfa2
commit 1c946a469a
2 changed files with 12 additions and 90 deletions

View File

@ -158,7 +158,6 @@ protected:
// override this and return true.
virtual bool UseGTKStyleBase() const wxOVERRIDE { return true; }
virtual void DoSetValue(const wxString &value, int flags = 0) wxOVERRIDE;
virtual wxString DoGetValue() const wxOVERRIDE;
// Override this to use either GtkEntry or GtkTextView IME depending on the

View File

@ -1030,102 +1030,24 @@ bool wxTextCtrl::IsEmpty() const
return wxTextEntry::IsEmpty();
}
// Helper class used by DoSetValue() and WriteText() to temporarily reset
// m_imKeyEvent to NULL and restore its original value in the dtor.
class TemporarilyUnsetIMKeyEvent
{
public:
explicit TemporarilyUnsetIMKeyEvent(GdkEventKey*& event)
: m_event(event),
m_eventOrig(event)
{
m_event = NULL;
}
~TemporarilyUnsetIMKeyEvent()
{
m_event = m_eventOrig;
}
private:
GdkEventKey*& m_event;
GdkEventKey* const m_eventOrig;
wxDECLARE_NO_COPY_CLASS(TemporarilyUnsetIMKeyEvent);
};
void wxTextCtrl::DoSetValue( const wxString &value, int flags )
{
wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
m_modified = false;
if ( !IsMultiLine() )
{
wxTextEntry::DoSetValue(value, flags);
return;
}
if (value.IsEmpty())
{
if ( !(flags & SetValue_SendEvent) )
EnableTextChangedEvents(false);
gtk_text_buffer_set_text( m_buffer, "", 0 );
if ( !(flags & SetValue_SendEvent) )
EnableTextChangedEvents(true);
return;
}
#if wxUSE_UNICODE
const wxCharBuffer buffer(value.utf8_str());
#else
wxFontEncoding enc = m_defaultStyle.HasFont()
? m_defaultStyle.GetFont().GetEncoding()
: wxFONTENCODING_SYSTEM;
if ( enc == wxFONTENCODING_SYSTEM )
enc = GetTextEncoding();
const wxCharBuffer buffer(wxGTK_CONV_ENC(value, enc));
if ( !buffer )
{
// see comment in WriteText() as to why we must warn the user about
// this
wxLogWarning(_("Failed to set text in the text control."));
return;
}
#endif
if ( !(flags & SetValue_SendEvent) )
{
EnableTextChangedEvents(false);
}
// This will emit insert-text signal which assumes that if m_imKeyEvent is
// set, it is called in response to this key press -- which is not the case
// here (but m_imKeyEvent might still be set e.g. because we're called from
// a menu event handler triggered by a keyboard accelerator).
TemporarilyUnsetIMKeyEvent unset(m_imKeyEvent);
gtk_text_buffer_set_text( m_buffer, buffer, strlen(buffer) );
if ( !(flags & SetValue_SendEvent) )
{
EnableTextChangedEvents(true);
}
}
void wxTextCtrl::WriteText( const wxString &text )
{
wxCHECK_RET( m_text != NULL, wxT("invalid text ctrl") );
if ( text.empty() )
return;
// we're changing the text programmatically
DontMarkDirtyOnNextChange();
// avoid generating wxEVT_CHAR when called from wxEVT_CHAR handler
TemporarilyUnsetIMKeyEvent unset(m_imKeyEvent);
// Inserting new text into the control below will emit insert-text signal
// which assumes that if m_imKeyEvent is set, it is called in response to
// this key press -- which is not the case here (but m_imKeyEvent might
// still be set e.g. because we're called from a menu event handler
// triggered by a keyboard accelerator), so reset m_imKeyEvent temporarily.
GdkEventKey* const imKeyEvent_save = m_imKeyEvent;
m_imKeyEvent = NULL;
wxON_BLOCK_EXIT_SET(m_imKeyEvent, imKeyEvent_save);
if ( !IsMultiLine() )
{
@ -1165,6 +1087,7 @@ void wxTextCtrl::WriteText( const wxString &text )
GtkTextIter iter;
gtk_text_buffer_get_iter_at_mark( m_buffer, &iter,
gtk_text_buffer_get_insert (m_buffer) );
gtk_text_buffer_insert( m_buffer, &iter, buffer, strlen(buffer) );
// Scroll to cursor, but only if scrollbar thumb is at the very bottom