From 8317bc2844e5c526db5f547b86d40d5a60f144fd Mon Sep 17 00:00:00 2001 From: Artur Wieczorek Date: Mon, 31 Jul 2017 21:36:02 +0200 Subject: [PATCH] Fix determining the length of the text in wxTextEntry/wxTextCtrl (wxGTK) Several functions of wxTextEntry and wxTextCtrl call to gtk_entry_get_text_length() API to determine the length of the text in GTKEntry. This API is available since GTK+ 2.14 so we have to implement a fallback method for older GTK+ versions. Dedicated function GTKGetEntryTextLength() is implemented in wxTextEntry and exposed through its interface because it is also used in wxTextCtrl. --- include/wx/gtk/textentry.h | 2 ++ src/gtk/textctrl.cpp | 5 +++-- src/gtk/textentry.cpp | 26 ++++++++++++++++++++++++-- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/include/wx/gtk/textentry.h b/include/wx/gtk/textentry.h index f78dc7b0dd..9f573d0bb4 100644 --- a/include/wx/gtk/textentry.h +++ b/include/wx/gtk/textentry.h @@ -80,6 +80,8 @@ protected: // Override the base class method to use GtkEntry IM context. virtual int GTKIMFilterKeypress(GdkEventKey* event) const; + static unsigned int GTKGetEntryTextLength(GtkEntry* entry); + private: // implement this to return the associated GtkEntry or another widget // implementing GtkEditable diff --git a/src/gtk/textctrl.cpp b/src/gtk/textctrl.cpp index b249f9a076..b394203e98 100644 --- a/src/gtk/textctrl.cpp +++ b/src/gtk/textctrl.cpp @@ -1204,7 +1204,7 @@ bool wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const } else // single line control { - if (pos <= gtk_entry_get_text_length(GTK_ENTRY(m_text))) + if (pos <= GTKGetEntryTextLength(GTK_ENTRY(m_text))) { if ( y ) *y = 0; @@ -1225,7 +1225,8 @@ long wxTextCtrl::XYToPosition(long x, long y ) const { if ( IsSingleLine() ) { - if ( y != 0 || x >= gtk_entry_get_text_length(GTK_ENTRY(m_text)) ) + + if ( y != 0 || x >= GTKGetEntryTextLength(GTK_ENTRY(m_text)) ) return -1; return x; diff --git a/src/gtk/textentry.cpp b/src/gtk/textentry.cpp index 7be2929d2d..62be76e961 100644 --- a/src/gtk/textentry.cpp +++ b/src/gtk/textentry.cpp @@ -35,6 +35,22 @@ #include "wx/gtk/private/gtk2-compat.h" #include "wx/gtk/private/string.h" +//----------------------------------------------------------------------------- +// helper function to get the length of the text +//----------------------------------------------------------------------------- + +static unsigned int GetEntryTextLength(GtkEntry* entry) +{ +#if GTK_CHECK_VERSION(2, 14, 0) + if ( gtk_check_version(2, 14, 0) == NULL ) + { + return gtk_entry_get_text_length(entry); + } +#endif // GTK+ 2.14+ + + return strlen(gtk_entry_get_text(entry)); +} + // ============================================================================ // signal handlers implementation // ============================================================================ @@ -61,7 +77,7 @@ wx_gtk_insert_text_callback(GtkEditable *editable, // check that we don't overflow the max length limit if we have it if ( text_max_length ) { - const int text_length = gtk_entry_get_text_length(entry); + const int text_length = GetEntryTextLength(entry); // We can't use new_text_length as it is in bytes while we want to count // characters (in first approximation, anyhow...). @@ -243,6 +259,12 @@ void wxTextEntry::Remove(long from, long to) gtk_editable_delete_text(GetEditable(), from, to); } +// static +unsigned int wxTextEntry::GTKGetEntryTextLength(GtkEntry* entry) +{ + return GetEntryTextLength(entry); +} + // ---------------------------------------------------------------------------- // clipboard operations // ---------------------------------------------------------------------------- @@ -320,7 +342,7 @@ long wxTextEntry::GetLastPosition() const long pos = -1; GtkEntry* entry = (GtkEntry*)GetEditable(); if (GTK_IS_ENTRY(entry)) - pos = gtk_entry_get_text_length(entry); + pos = GetEntryTextLength(entry); return pos; }