]> git.saurik.com Git - wxWidgets.git/commitdiff
corrected an off-by-1 bug in GetNumberOfLines() and PositionToXY() for
authorVadim Zeitlin <vadim@wxwidgets.org>
Thu, 28 Jan 1999 14:01:37 +0000 (14:01 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Thu, 28 Jan 1999 14:01:37 +0000 (14:01 +0000)
single-line text controls

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@1506 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

src/gtk/textctrl.cpp
src/gtk1/textctrl.cpp

index f165c20805a586be211de61b7ae13d9771607e17..bdf1a999a995e9516c6cb2b40cd346b2e55a720c 100644 (file)
@@ -435,12 +435,18 @@ long wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const
     {
         wxString text = GetValue();
 
-        if( pos >= (long)text.Len()  )
+       // cast to prevent warning. But pos really should've been unsigned.
+        if( (unsigned long)pos > text.Len()  )
             return FALSE;
 
         *x=1;   // Col 1
         *y=1;   // Line 1
-        for ( const char *p = text.c_str(); *p; p++ )
+
+        if (pos == 0)
+            return TRUE;
+
+        const char* stop = text.c_str() + pos + 1;
+        for ( const char *p = text.c_str(); p <= stop; p++ )
         {
             if (*p == '\n')
             {
@@ -453,7 +459,7 @@ long wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const
     }
     else // single line control
     {
-        if ( pos < GTK_ENTRY(m_text)->text_length )
+        if ( pos <= GTK_ENTRY(m_text)->text_length )
         {
             *y = 1;
             *x = pos;
@@ -473,10 +479,11 @@ long wxTextCtrl::XYToPosition(long x, long y ) const
     if (!(m_windowStyle & wxTE_MULTILINE)) return 0;
 
     long pos=0;
+    /* This is a kludge; our XY values are 1-based, but GetLineLength()
+     * and --Text() start counting at 0. (and so say the docs) */
+    for( int i=1; i<y; i++ ) pos += GetLineLength(i-1) + 1; // one for '\n'
 
-    for( int i=1; i<y; i++ ) pos += GetLineLength(i);
-
-    pos +=x-1; // Pos start with 0
+    pos += x-1; // Pos start with 0
     return pos;
 }
 
@@ -502,7 +509,9 @@ int wxTextCtrl::GetNumberOfLines() const
                     currentLine++;
             }
             g_free( text );
-            return currentLine;
+
+            // currentLine is 0 based, add 1 to get number of lines
+            return currentLine + 1;
         }
         else
         {
index f165c20805a586be211de61b7ae13d9771607e17..bdf1a999a995e9516c6cb2b40cd346b2e55a720c 100644 (file)
@@ -435,12 +435,18 @@ long wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const
     {
         wxString text = GetValue();
 
-        if( pos >= (long)text.Len()  )
+       // cast to prevent warning. But pos really should've been unsigned.
+        if( (unsigned long)pos > text.Len()  )
             return FALSE;
 
         *x=1;   // Col 1
         *y=1;   // Line 1
-        for ( const char *p = text.c_str(); *p; p++ )
+
+        if (pos == 0)
+            return TRUE;
+
+        const char* stop = text.c_str() + pos + 1;
+        for ( const char *p = text.c_str(); p <= stop; p++ )
         {
             if (*p == '\n')
             {
@@ -453,7 +459,7 @@ long wxTextCtrl::PositionToXY(long pos, long *x, long *y ) const
     }
     else // single line control
     {
-        if ( pos < GTK_ENTRY(m_text)->text_length )
+        if ( pos <= GTK_ENTRY(m_text)->text_length )
         {
             *y = 1;
             *x = pos;
@@ -473,10 +479,11 @@ long wxTextCtrl::XYToPosition(long x, long y ) const
     if (!(m_windowStyle & wxTE_MULTILINE)) return 0;
 
     long pos=0;
+    /* This is a kludge; our XY values are 1-based, but GetLineLength()
+     * and --Text() start counting at 0. (and so say the docs) */
+    for( int i=1; i<y; i++ ) pos += GetLineLength(i-1) + 1; // one for '\n'
 
-    for( int i=1; i<y; i++ ) pos += GetLineLength(i);
-
-    pos +=x-1; // Pos start with 0
+    pos += x-1; // Pos start with 0
     return pos;
 }
 
@@ -502,7 +509,9 @@ int wxTextCtrl::GetNumberOfLines() const
                     currentLine++;
             }
             g_free( text );
-            return currentLine;
+
+            // currentLine is 0 based, add 1 to get number of lines
+            return currentLine + 1;
         }
         else
         {