Alfredo Braunstein wrote:

> Scott Kostyshak wrote:
> 
>> On Tue, Oct 7, 2014 at 10:18 AM, Alfredo Braunstein <abrau...@lyx.org>
>> wrote:
>>> Alfredo Braunstein wrote:
>>>
>>>> Now if the situation is
>>>>
>>>> [one(two|)three]
>>>>
>>>> then pressing 'End' goes outside of the inset:
>>>>
>>>> [one(two)three]|
>>>
>>> By the way, this behavior is also a way of triggering
>>> http://www.lyx.org/trac/ticket/3900 ("Mathed corners displayed without
>>> mouse hover", now closed/fixed), as when the cursor escapes wrongly the
>>> math inset, purple corners are not cleared. The patch fixes also this.
>>> Though as before, I don't know if it has other ill effects...
>> 
>> I am guessing I'm wrong but wanted to check anyway. Is this bug at all
>> related? http://www.lyx.org/trac/ticket/2542
>> 
>> Scott
> 
> Seems indeed unrelated.

I did a quick investigation on this, and there is kind of a design flaw. On 
a mouse event, the external Text starts a chain of recursive editXY calls to 
find the deepest inset (and the closest position within) containing the 
click point inside nested structures to create a Cursor. 

Then the lfun is dispatched to that Cursor; and the usual lfun catching game 
in which the cursor is iteratively chopped and the lfun is proposed to all 
insets in the path, starting from the deepest one. For a mouse click, the 
deepest editable inset will typically catch the lfun.

For mouse click+motion event (i.e. selection), insets deeper than the anchor 
ignore the lfun; so the lfun is catch by the one on the same level than the 
anchor, where the cursor should be positioned. But there is a problem: even 
if the tip of the Cursor points to the closest position to the click point, 
intermediate slices will always point to the position *before* the 
corresponding inset (otherwise the cursor will be invalid). 

An fix is in attachment (minimal, but not very pretty), where the position 
"corrected" on this last dispatch. But I think we should really find a 
better fix. Ideas?

A/
diff --git a/src/mathed/InsetMathNest.cpp b/src/mathed/InsetMathNest.cpp
index fc398e4..0794f77 100644
--- a/src/mathed/InsetMathNest.cpp
+++ b/src/mathed/InsetMathNest.cpp
@@ -1582,6 +1582,8 @@ void InsetMathNest::lfunMouseMotion(Cursor & cur, FuncRequest & cmd)
 
 	CursorSlice old = bvcur.top();
 
+	MathData & ar = cell(cur.idx());
+	cur.pos() = ar.x2pos(&cur.bv(), cur.x_target() - ar.xo(cur.bv()), 0, false);
 	// We continue with our existing selection or start a new one, so don't
 	// reset the anchor.
 	bvcur.setCursor(cur);
diff --git a/src/mathed/MathData.cpp b/src/mathed/MathData.cpp
index edd8089..8146a8f 100644
--- a/src/mathed/MathData.cpp
+++ b/src/mathed/MathData.cpp
@@ -875,7 +875,7 @@ MathData::size_type MathData::x2pos(BufferView const * bv, int targetx) const
 }
 
 
-MathData::size_type MathData::x2pos(BufferView const * bv, int targetx, int glue) const
+MathData::size_type MathData::x2pos(BufferView const * bv, int targetx, int glue, bool descend) const
 {
 	const_iterator it = begin();
 	int lastx = 0;
@@ -900,7 +900,7 @@ MathData::size_type MathData::x2pos(BufferView const * bv, int targetx, int glue
 	 * See bug 1918 for details.
 	 **/
 	if (it != begin() && currx >= targetx
-	    && ((*boost::prior(it))->asNestInset()
+	    && ((descend && (*boost::prior(it))->asNestInset())
 		|| abs(lastx - targetx) < abs(currx - targetx))) {
 		--it;
 	}
diff --git a/src/mathed/MathData.h b/src/mathed/MathData.h
index e216941..fa62846 100644
--- a/src/mathed/MathData.h
+++ b/src/mathed/MathData.h
@@ -147,7 +147,7 @@ public:
 	/// returns position of given x coordinate
 	size_type x2pos(BufferView const * bv, int targetx) const;
 	/// returns position of given x coordinate starting from a certain pos
-	size_type x2pos(BufferView const * bv, int targetx, int glue) const;
+	size_type x2pos(BufferView const * bv, int targetx, int glue, bool descend = true) const;
 	/// returns distance of this cell to the point given by x and y
 	// assumes valid position and size cache
 	int dist(BufferView const & bv, int x, int y) const;

Reply via email to