]> git.lyx.org Git - features.git/commitdiff
When selecting text with the mouse, inset selection happens in the middle
authorGuillaume Munch <gm@lyx.org>
Sat, 18 Feb 2017 22:03:53 +0000 (23:03 +0100)
committerGuillaume Munch <gm@lyx.org>
Sat, 18 Feb 2017 22:03:53 +0000 (23:03 +0100)
src/Row.cpp
src/Row.h
src/Text3.cpp
src/TextMetrics.cpp
src/TextMetrics.h

index 70e3dcafc3ac3683d72d4d9555e2b7a59df19fc3..cc78cff3390f8e3579164155f98f90b0174b4d07 100644 (file)
@@ -95,7 +95,7 @@ double Row::Element::pos2x(pos_type const i) const
 }
 
 
-pos_type Row::Element::x2pos(int &x) const
+pos_type Row::Element::x2pos(int &x, bool const select) const
 {
        //lyxerr << "x2pos: x=" << x << " w=" << width() << " " << *this;
        size_t i = 0;
@@ -112,17 +112,18 @@ pos_type Row::Element::x2pos(int &x) const
                x = isRTL() ? int(full_width()) : 0;
                break;
        case INSET:
-       case SPACE:
+       case SPACE: {
+               int const boundary = select ? (full_width() + 1) / 2 : full_width();
                // those elements contain only one position. Round to
                // the closest side.
-               if (x > full_width()) {
+               if (x > boundary) {
                        x = int(full_width());
                        i = !isRTL();
                } else {
                        x = 0;
                        i = isRTL();
                }
-
+       }
        }
        //lyxerr << "=> p=" << pos + i << " x=" << x << endl;
        return pos + i;
index 498fd07d7f8f8ce5c08335171605061ed6269701..c840698f1c071b00bd1980197a8537270afe8f3a 100644 (file)
--- a/src/Row.h
+++ b/src/Row.h
@@ -80,12 +80,13 @@ public:
                /** Return position in pixels (from the left) of position
                 * \param i in the row element.
                 */
-               double pos2x(pos_type const i) const;
+               double pos2x(pos_type i) const;
                /** Return character position that is the closest to
                 *  pixel position \param x. The value \param x is
                 *  adjusted to the actual pixel position.
-               */
-               pos_type x2pos(int &x) const;
+                *  \param select if true, return the right edge when closer.
+                */
+               pos_type x2pos(int & x, bool select = false) const;
                /** Break the element if possible, so that its width is less
                 * than \param w. Returns true on success. When \param force
                 * is true, the string is cut at any place, other wise it
index cb5f0b5760e6bbbb906390b1a25e6036b1d970f8..43c68ca23fbe36dc8512db86ce09edd7f335b4bd 100644 (file)
@@ -1690,7 +1690,7 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                int const wh = bv->workHeight();
                int const y = max(0, min(wh - 1, cmd.y()));
 
-               tm->setCursorFromCoordinates(cur, cmd.x(), y);
+               tm->setCursorFromCoordinates(cur, cmd.x(), y, true);
                cur.setTargetX(cmd.x());
                // Don't allow selecting a separator inset
                if (cur.pos() && cur.paragraph().isEnvSeparator(cur.pos() - 1))
index a93f9ba4cd3dce3ef33cb0d31193aff3f0847e2b..87a381eed0d09a66e436728cf0cba2c75d8464c1 100644 (file)
@@ -1094,7 +1094,7 @@ void TextMetrics::setRowHeight(Row & row) const
 // returns the column near the specified x-coordinate of the row
 // x is set to the real beginning of this column
 pos_type TextMetrics::getPosNearX(Row const & row, int & x,
-                                 bool & boundary) const
+                                  bool & boundary, bool const select) const
 {
        //LYXERR0("getPosNearX(" << x << ") row=" << row);
        /// For the main Text, it is possible that this pit is not
@@ -1124,7 +1124,7 @@ pos_type TextMetrics::getPosNearX(Row const & row, int & x,
                for ( ; cit != cend; ++cit) {
                        if (w <= x &&  w + cit->full_width() > x) {
                                int x_offset = int(x - w);
-                               pos = cit->x2pos(x_offset);
+                               pos = cit->x2pos(x_offset, select);
                                x = int(x_offset + w);
                                break;
                        }
@@ -1382,7 +1382,8 @@ Inset * TextMetrics::editXY(Cursor & cur, int x, int y,
 }
 
 
-void TextMetrics::setCursorFromCoordinates(Cursor & cur, int const x, int const y)
+void TextMetrics::setCursorFromCoordinates(Cursor & cur, int const x,
+                                           int const y, bool const select)
 {
        LASSERT(text_ == cur.text(), return);
        pit_type const pit = getPitNearY(y);
@@ -1409,7 +1410,7 @@ void TextMetrics::setCursorFromCoordinates(Cursor & cur, int const x, int const
 
        bool bound = false;
        int xx = x;
-       pos_type const pos = getPosNearX(row, xx, bound);
+       pos_type const pos = getPosNearX(row, xx, bound, select);
 
        LYXERR(Debug::DEBUG, "setting cursor pit: " << pit << " pos: " << pos);
 
index 2f6181d0b0d91592d92198f0112473e5d83dd001..ade98739ed8c59d50c18d4f1a5860928fa12f48a 100644 (file)
@@ -150,7 +150,8 @@ public:
        /// returns the position near the specified x-coordinate of the row.
        /// x is an absolute screen coord, it is set to the real beginning
        /// of this column. This takes in account horizontal cursor row scrolling.
-       pos_type getPosNearX(Row const & row, int & x, bool & boundary) const;
+       pos_type getPosNearX(Row const & row, int & x, bool & boundary,
+                            bool select = false) const;
 
        /// returns pos in given par at given x coord.
        pos_type x2pos(pit_type pit, int row, int x) const;
@@ -188,7 +189,10 @@ public:
 
        /// sets cursor only within this Text.
        /// x,y are screen coordinates
-       void setCursorFromCoordinates(Cursor & cur, int x, int y);
+       /// If select is true, move to the next position if closer to the right
+       /// edge.
+       void setCursorFromCoordinates(Cursor & cur, int x, int y,
+                                     bool select = false);
 
        ///
        int cursorX(CursorSlice const & cursor, bool boundary) const;