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.
This commit is contained in:
Artur Wieczorek
2017-07-31 21:36:02 +02:00
parent 5b679bfbec
commit 8317bc2844
3 changed files with 29 additions and 4 deletions

View File

@@ -80,6 +80,8 @@ protected:
// Override the base class method to use GtkEntry IM context. // Override the base class method to use GtkEntry IM context.
virtual int GTKIMFilterKeypress(GdkEventKey* event) const; virtual int GTKIMFilterKeypress(GdkEventKey* event) const;
static unsigned int GTKGetEntryTextLength(GtkEntry* entry);
private: private:
// implement this to return the associated GtkEntry or another widget // implement this to return the associated GtkEntry or another widget
// implementing GtkEditable // implementing GtkEditable

View File

@@ -1204,7 +1204,7 @@ bool wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const
} }
else // single line control else // single line control
{ {
if (pos <= gtk_entry_get_text_length(GTK_ENTRY(m_text))) if (pos <= GTKGetEntryTextLength(GTK_ENTRY(m_text)))
{ {
if ( y ) if ( y )
*y = 0; *y = 0;
@@ -1225,7 +1225,8 @@ long wxTextCtrl::XYToPosition(long x, long y ) const
{ {
if ( IsSingleLine() ) 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 -1;
return x; return x;

View File

@@ -35,6 +35,22 @@
#include "wx/gtk/private/gtk2-compat.h" #include "wx/gtk/private/gtk2-compat.h"
#include "wx/gtk/private/string.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 // 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 // check that we don't overflow the max length limit if we have it
if ( text_max_length ) 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 // We can't use new_text_length as it is in bytes while we want to count
// characters (in first approximation, anyhow...). // characters (in first approximation, anyhow...).
@@ -243,6 +259,12 @@ void wxTextEntry::Remove(long from, long to)
gtk_editable_delete_text(GetEditable(), from, to); gtk_editable_delete_text(GetEditable(), from, to);
} }
// static
unsigned int wxTextEntry::GTKGetEntryTextLength(GtkEntry* entry)
{
return GetEntryTextLength(entry);
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// clipboard operations // clipboard operations
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -320,7 +342,7 @@ long wxTextEntry::GetLastPosition() const
long pos = -1; long pos = -1;
GtkEntry* entry = (GtkEntry*)GetEditable(); GtkEntry* entry = (GtkEntry*)GetEditable();
if (GTK_IS_ENTRY(entry)) if (GTK_IS_ENTRY(entry))
pos = gtk_entry_get_text_length(entry); pos = GetEntryTextLength(entry);
return pos; return pos;
} }