From 21d23b882fcce7b7582ae3e3f71a069a6d2940df Mon Sep 17 00:00:00 2001
From: Mart Raudsepp <leio@gentoo.org>
Date: Mon, 8 Aug 2005 08:53:48 +0000
Subject: [PATCH] [wxGTK2] wxTextCtrl::XYToPosition(): Return -1 if x or y is
 out of range

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@35147 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 docs/latex/wx/text.tex |  2 +-
 src/gtk/textctrl.cpp   | 10 ++++++++--
 src/gtk1/textctrl.cpp  | 10 ++++++++--
 3 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/docs/latex/wx/text.tex b/docs/latex/wx/text.tex
index 0da0113a0f..a3413657ec 100644
--- a/docs/latex/wx/text.tex
+++ b/docs/latex/wx/text.tex
@@ -1247,7 +1247,7 @@ Converts the given zero based column and line number to a position.
 
 \wxheading{Return value}
 
-The position value.
+The position value, or -1 if {\tt x} or {\tt y} was invalid.
 
 
 \membersection{wxTextCtrl::operator \cinsert}\label{wxtextctrlinsert}
diff --git a/src/gtk/textctrl.cpp b/src/gtk/textctrl.cpp
index 9d6379854d..9f5e5be54f 100644
--- a/src/gtk/textctrl.cpp
+++ b/src/gtk/textctrl.cpp
@@ -1210,8 +1210,14 @@ long wxTextCtrl::XYToPosition(long x, long y ) const
 
 #ifdef __WXGTK20__
     GtkTextIter iter;
-    gtk_text_buffer_get_iter_at_line_offset(m_buffer, &iter, y, x);
-    return gtk_text_iter_get_offset(&iter);
+    if (y >= gtk_text_buffer_get_line_count (m_buffer))
+        return -1;
+
+    gtk_text_buffer_get_iter_at_line(m_buffer, &iter, y);
+    if (x >= gtk_text_iter_get_chars_in_line (&iter))
+        return -1;
+
+    return gtk_text_iter_get_offset(&iter) + x;
 #else
     long pos=0;
     for( int i=0; i<y; i++ ) pos += GetLineLength(i) + 1; // one for '\n'
diff --git a/src/gtk1/textctrl.cpp b/src/gtk1/textctrl.cpp
index 9d6379854d..9f5e5be54f 100644
--- a/src/gtk1/textctrl.cpp
+++ b/src/gtk1/textctrl.cpp
@@ -1210,8 +1210,14 @@ long wxTextCtrl::XYToPosition(long x, long y ) const
 
 #ifdef __WXGTK20__
     GtkTextIter iter;
-    gtk_text_buffer_get_iter_at_line_offset(m_buffer, &iter, y, x);
-    return gtk_text_iter_get_offset(&iter);
+    if (y >= gtk_text_buffer_get_line_count (m_buffer))
+        return -1;
+
+    gtk_text_buffer_get_iter_at_line(m_buffer, &iter, y);
+    if (x >= gtk_text_iter_get_chars_in_line (&iter))
+        return -1;
+
+    return gtk_text_iter_get_offset(&iter) + x;
 #else
     long pos=0;
     for( int i=0; i<y; i++ ) pos += GetLineLength(i) + 1; // one for '\n'
-- 
2.50.0

