commit 73d3816e0f2083a2dc53d744f32d206a153556be
Author: Guillaume Munch <g...@lyx.org>
Date:   Tue Oct 13 20:26:21 2015 +0100

    Improve the TexRow Cursor->Row algorithm for selections.
    
    Selections were incorrect after the addition of math due to the 
invalidation of
    an invariant.

diff --git a/src/TexRow.cpp b/src/TexRow.cpp
index a34acf0..fe2f694 100644
--- a/src/TexRow.cpp
+++ b/src/TexRow.cpp
@@ -13,7 +13,7 @@
 
 #include <config.h>
 
-#include "DocIterator.h"
+#include "Cursor.h"
 #include "Paragraph.h"
 #include "TexRow.h"
 
@@ -400,6 +400,22 @@ std::pair<int,int> TexRow::rowFromDocIterator(DocIterator 
const & dit) const
 }
 
 
+std::pair<int,int> TexRow::rowFromCursor(Cursor const & cur) const
+{
+       DocIterator beg = cur.selectionBegin();
+       std::pair<int,int> beg_rows = rowFromDocIterator(beg);
+       if (cur.selection()) {
+               DocIterator end = cur.selectionEnd();
+               if (!cur.selIsMultiCell())
+                       end.top().backwardPos();        
+               std::pair<int,int> end_rows = rowFromDocIterator(end);
+               return std::make_pair(std::min(beg_rows.first, end_rows.first),
+                                                         
std::max(beg_rows.second, end_rows.second));
+       } else
+               return std::make_pair(beg_rows.first, beg_rows.second);
+}
+
+
 // debugging functions
 
 ///
diff --git a/src/TexRow.h b/src/TexRow.h
index 47a7d74..7468386 100644
--- a/src/TexRow.h
+++ b/src/TexRow.h
@@ -23,6 +23,7 @@
 namespace lyx {
 
 class LyXErr;
+class Cursor;
 class CursorSlice;
 class DocIterator;
 class docstring_list;
@@ -145,6 +146,11 @@ public:
        /// Finds the best pair of rows for dit
        /// returns (-1,-1) if not found.
        std::pair<int,int> rowFromDocIterator(DocIterator const & dit) const;
+
+       /// Finds the best pair of rows for cursor, taking the selection into
+       /// account
+       /// returns (-1,-1) if not found.
+       std::pair<int,int> rowFromCursor(Cursor const & dit) const;
        
        /// Returns the number of rows contained
        int rows() const { return rowlist_.size(); }
diff --git a/src/frontends/qt4/GuiViewSource.cpp 
b/src/frontends/qt4/GuiViewSource.cpp
index c908a2e..4a0381d 100644
--- a/src/frontends/qt4/GuiViewSource.cpp
+++ b/src/frontends/qt4/GuiViewSource.cpp
@@ -257,20 +257,9 @@ void ViewSourceWidget::realUpdateView()
        } else if (texrow_.get()) {
                // Use the available position-to-row conversion to highlight
                // the current selection in the source
-               int beg_row, end_row;
-               {
-                       DocIterator beg = bv_->cursor().selectionBegin();
-                       DocIterator end = bv_->cursor().selectionEnd();
-                       std::pair<int,int> beg_rows = 
texrow_->rowFromDocIterator(beg);
-                       beg_row = beg_rows.first;
-                       if (beg != end) {
-                               end.backwardChar();
-                               std::pair<int,int> end_rows = 
texrow_->rowFromDocIterator(end);
-                               end_row = end_rows.second;
-                       } else {
-                               end_row = beg_rows.second;
-                       }
-               }
+               std::pair<int,int> rows = texrow_->rowFromCursor(bv_->cursor());
+               int const beg_row = rows.first;
+               int const end_row = rows.second;
 
                QTextCursor c = QTextCursor(viewSourceTV->document());
 

Reply via email to