>>>>> "Abdelrazak" == Abdelrazak Younes <[EMAIL PROTECTED]> writes:

>> To continue the selHandle idea, with the following patch (I did not
>> move yet to bufferview), we would avoid most of the explicit calls
>> to haveSelection in Bo's patch.

Abdelrazak> All I can say is that it needs careful testing under X11.
Abdelrazak> But I like your approach too.

OK, here is my own version of Bo's patch. I removed the ClearSelection
flag, and did the call to haveSelection in LyXFunc::dispatch (it seems
to me that this does not cost much, right). This allows to remove a
lot of code.

Of course, I have lost some of the cases that were handled by Bo's
patch by the ClearSelection flag: mouse press, deletion of text. These
will have to be reintroduced by hand, but I reckon it will not cost
much code.

Does it make sense? I will have to leave soon and will not be able to
work on it this week-end. If somebody wants to steal the patch, be my
guest.

JMarc

Index: src/insets/InsetTabular.cpp
===================================================================
--- src/insets/InsetTabular.cpp	(révision 18996)
+++ src/insets/InsetTabular.cpp	(copie de travail)
@@ -3241,28 +3241,18 @@ void InsetTabular::doDispatch(Cursor & c
 
 	case LFUN_MOUSE_RELEASE:
 		//lyxerr << "# InsetTabular::MouseRelease\n" << bvcur << endl;
-		if (cmd.button() == mouse_button::button1) {
-			if (bvcur.selection()) {
-				// Bug3238: disable persistent selection for table cells for now
-				if (tablemode(bvcur))
-					theSelection().haveSelection(true);
-				else
-					saveSelection(bvcur);
-			}
-		} else if (cmd.button() == mouse_button::button3)
+		if (cmd.button() == mouse_button::button3)
 			InsetTabularMailer(*this).showDialog(&cur.bv());
 		break;
 
 	case LFUN_CELL_BACKWARD:
 		movePrevCell(cur);
 		cur.selection() = false;
-		saveSelection(cur);
 		break;
 
 	case LFUN_CELL_FORWARD:
 		moveNextCell(cur);
 		cur.selection() = false;
-		saveSelection(cur);
 		break;
 
 	case LFUN_CHAR_FORWARD_SELECT:
@@ -3270,12 +3260,6 @@ void InsetTabular::doDispatch(Cursor & c
 		cell(cur.idx())->dispatch(cur, cmd);
 		if (!cur.result().dispatched()) {
 			isRightToLeft(cur) ? movePrevCell(cur) : moveNextCell(cur);
-			// The second case happens when LFUN_CHAR_FORWARD_SELECT
-			// is called, but the cursor is undispatched with cmd modified 
-			// to LFUN_FINISHED_RIGHT (e.g. a case in bug 3782)
-			if (cmd.action == LFUN_CHAR_FORWARD_SELECT ||
-				cmd.action == LFUN_FINISHED_RIGHT)
-				saveSelection(cur);
 			if (sl == cur.top())
 				cmd = FuncRequest(LFUN_FINISHED_RIGHT);
 			else
@@ -3288,8 +3272,6 @@ void InsetTabular::doDispatch(Cursor & c
 		cell(cur.idx())->dispatch(cur, cmd);
 		if (!cur.result().dispatched()) {
 			isRightToLeft(cur) ? moveNextCell(cur) : movePrevCell(cur);
-			if (cmd.action == LFUN_CHAR_BACKWARD_SELECT)
-				saveSelection(cur);
 			if (sl == cur.top())
 				cmd = FuncRequest(LFUN_FINISHED_LEFT);
 			else
@@ -3311,13 +3293,6 @@ void InsetTabular::doDispatch(Cursor & c
 				TextMetrics const & tm =
 					cur.bv().textMetrics(cell(cur.idx())->getText(0));
 				cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
-				if (cmd.action == LFUN_DOWN_SELECT) {
-					// Bug3238: disable persistent selection for table cells for now
-					if (tablemode(cur))
-						theSelection().haveSelection(true);
-					else
-						saveSelection(cur);
-				}
 			}
 		if (sl == cur.top()) {
 			// we trick it to go to the RIGHT after leaving the
@@ -3343,13 +3318,6 @@ void InsetTabular::doDispatch(Cursor & c
 				ParagraphMetrics const & pm =
 					tm.parMetrics(cur.lastpit());
 				cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
-				if (cmd.action == LFUN_UP_SELECT) {
-					// Bug3238: disable persistent selection for table cells for now
-					if (tablemode(cur))
-						theSelection().haveSelection(true);
-					else
-						saveSelection(cur);
-				}
 			}
 		if (sl == cur.top()) {
 			cmd = FuncRequest(LFUN_UP);
@@ -4645,7 +4613,6 @@ void InsetTabular::cutSelection(Cursor &
 	if (cur.pos() > cur.lastpos())
 		cur.pos() = cur.lastpos();
 	cur.clearSelection();
-	theSelection().haveSelection(false);
 }
 
 
Index: src/mathed/InsetMathNest.cpp
===================================================================
--- src/mathed/InsetMathNest.cpp	(révision 18996)
+++ src/mathed/InsetMathNest.cpp	(copie de travail)
@@ -582,7 +582,6 @@ goto_char_backwards:
 		cur.selection() = true;
 		cur.pos() = cur.lastpos();
 		cur.idx() = cur.lastidx();
-		cap::saveSelection(cur);
 		break;
 
 	case LFUN_PARAGRAPH_UP:
@@ -1207,7 +1206,6 @@ void InsetMathNest::lfunMouseRelease(Cur
 		else {
 			Cursor & bvcur = cur.bv().cursor();
 			bvcur.selection() = true;
-			cap::saveSelection(bvcur);
 		}
 		return;
 	}
Index: src/LyXFunc.cpp
===================================================================
--- src/LyXFunc.cpp	(révision 18996)
+++ src/LyXFunc.cpp	(copie de travail)
@@ -86,6 +86,7 @@
 #include "frontends/LyXView.h"
 #include "frontends/Menubar.h"
 #include "frontends/Toolbars.h"
+#include "frontends/Selection.h"
 
 #include "support/environment.h"
 #include "support/FileFilterList.h"
@@ -218,6 +219,10 @@ void LyXFunc::initKeySequences(KeyMap * 
 
 void LyXFunc::setLyXView(LyXView * lv)
 {
+	if (lyx_view_ && lyx_view_ != lv)
+		// save current selection to the selection buffer to allow
+		// middle-button paste in another window
+		cap::saveSelection(lyx_view_->view()->cursor());
 	lyx_view_ = lv;
 }
 
@@ -1824,6 +1829,9 @@ void LyXFunc::dispatch(FuncRequest const
 			    && !lyxaction.funcHasFlag(action, LyXAction::ReadOnly))
 				view()->buffer()->markDirty();
 
+			//Do we have a selection?
+			theSelection().haveSelection(view()->cursor().selection());
+
 			if (view()->cursor().inTexted()) {
 				lyx_view_->updateLayoutChoice();
 			}
Index: src/CutAndPaste.cpp
===================================================================
--- src/CutAndPaste.cpp	(révision 18996)
+++ src/CutAndPaste.cpp	(copie de travail)
@@ -565,7 +565,6 @@ void cutSelection(Cursor & cur, bool doc
 		// need a valid cursor. (Lgb)
 		cur.clearSelection();
 		updateLabels(cur.buffer());
-		theSelection().haveSelection(false);
 
 		// tell tabular that a recent copy happened
 		dirtyTabularStack(false);
@@ -672,24 +671,15 @@ void copySelection(Cursor & cur, docstri
 
 void saveSelection(Cursor & cur)
 {
-	LYXERR(Debug::ACTION) << BOOST_CURRENT_FUNCTION << ": `"
-	       << to_utf8(cur.selectionAsString(true)) << "'."
-	       << endl;
-
-#if 0
-	// FIXME: The two lines below would allow middle-mouse 
-	// pasting that preserves the LyX formatting when the selection
-	// is internal. They would also allow to use the feature on
-	// Windows and Mac. In the future, we may want to optionally enable
-	// this feature via a rc setting.
-	// This is currently disabled because it eats too much resources
-	// while selecting (cf. bug 3877)
-	if (cur.selection())
+	// This function is called, not when a selection is formed, but when
+	// a selection is cleared. Therefore, multiple keyboard selection
+	// will not repeatively trigger this function (bug 3877).
+	if (cur.selection()) {
+		LYXERR(Debug::ACTION) << BOOST_CURRENT_FUNCTION << ": `"
+			   << to_utf8(cur.selectionAsString(true)) << "'."
+			   << endl;
 		copySelectionToStack(cur, selectionBuffer);
-#endif
-
-	// tell X whether we now have a valid selection
-	theSelection().haveSelection(cur.selection());
+	}
 }
 
 
@@ -746,7 +736,6 @@ void pasteFromStack(Cursor & cur, ErrorL
 	pasteParagraphList(cur, theCuts[sel_index].first,
 			   theCuts[sel_index].second, errorList);
 	cur.setSelection();
-	saveSelection(cur);
 }
 
 
@@ -825,7 +814,6 @@ void replaceSelectionWithString(Cursor &
 		cur.setSelection(selbeg, -int(str.length()));
 	} else
 		cur.setSelection(selbeg, str.length());
-	saveSelection(cur);
 }
 
 
@@ -862,7 +850,6 @@ void eraseSelection(Cursor & cur)
 		}
 		// need a valid cursor. (Lgb)
 		cur.clearSelection();
-		theSelection().haveSelection(false);
 	} else {
 		lyxerr << "can't erase this selection 1" << endl;
 	}
Index: src/Text3.cpp
===================================================================
--- src/Text3.cpp	(révision 18996)
+++ src/Text3.cpp	(copie de travail)
@@ -119,7 +119,6 @@ namespace {
 	{
 		if (selecting || cur.mark())
 			cur.setSelection();
-		saveSelection(cur);
 	}
 
 
@@ -488,8 +487,7 @@ void Text::dispatch(Cursor & cur, FuncRe
 				&& cur.boundary() == oldBoundary) {
 			cur.undispatched();
 			cmd = FuncRequest(LFUN_FINISHED_RIGHT);
-		} else if (cur.selection())
-			saveSelection(cur);
+		}
 		break;
 
 	case LFUN_CHAR_BACKWARD:
@@ -506,8 +504,6 @@ void Text::dispatch(Cursor & cur, FuncRe
 			cur.undispatched();
 			cmd = FuncRequest(LFUN_FINISHED_LEFT);
 		}
-		if (cur.selection())
-			saveSelection(cur);
 		break;
 
 	case LFUN_UP_SELECT:
@@ -532,9 +528,6 @@ void Text::dispatch(Cursor & cur, FuncRe
 		} else
 			cur.undispatched();
 		
-		// save new selection
-		if (cur.selection())
-			saveSelection(cur);
 		break;
 	}
 
@@ -542,16 +535,12 @@ void Text::dispatch(Cursor & cur, FuncRe
 	case LFUN_PARAGRAPH_UP_SELECT:
 		needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_UP_SELECT);
 		needsUpdate |= cursorUpParagraph(cur);
-		if (cur.selection())
-			saveSelection(cur);
 		break;
 
 	case LFUN_PARAGRAPH_DOWN:
 	case LFUN_PARAGRAPH_DOWN_SELECT:
 		needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_DOWN_SELECT);
 		needsUpdate |= cursorDownParagraph(cur);
-		if (cur.selection())
-			saveSelection(cur);
 		break;
 
 	case LFUN_SCREEN_UP:
@@ -562,8 +551,6 @@ void Text::dispatch(Cursor & cur, FuncRe
 		else {
 			cursorPrevious(cur);
 		}
-		if (cur.selection())
-			saveSelection(cur);
 		break;
 
 	case LFUN_SCREEN_DOWN:
@@ -575,8 +562,6 @@ void Text::dispatch(Cursor & cur, FuncRe
 		else {
 			cursorNext(cur);
 		}
-		if (cur.selection())
-			saveSelection(cur);
 		break;
 
 	case LFUN_LINE_BEGIN:
@@ -589,8 +574,6 @@ void Text::dispatch(Cursor & cur, FuncRe
 	case LFUN_LINE_END_SELECT:
 		needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_END_SELECT);
 		needsUpdate |= cursorEnd(cur);
-		if (cur.selection())
-			saveSelection(cur);
 		break;
 
 	case LFUN_WORD_FORWARD:
@@ -600,8 +583,6 @@ void Text::dispatch(Cursor & cur, FuncRe
 			needsUpdate |= cursorLeftOneWord(cur);
 		else
 			needsUpdate |= cursorRightOneWord(cur);
-		if (cur.selection())
-			saveSelection(cur);
 		break;
 
 	case LFUN_WORD_BACKWARD:
@@ -611,8 +592,6 @@ void Text::dispatch(Cursor & cur, FuncRe
 			needsUpdate |= cursorRightOneWord(cur);
 		else
 			needsUpdate |= cursorLeftOneWord(cur);
-		if (cur.selection())
-			saveSelection(cur);
 		break;
 
 	case LFUN_WORD_SELECT: {
@@ -1033,7 +1012,6 @@ void Text::dispatch(Cursor & cur, FuncRe
 			cursorEnd(cur);
 			cur.setSelection();
 			bv->cursor() = cur;
-			saveSelection(cur);
 		}
 		break;
 
@@ -1145,7 +1123,6 @@ void Text::dispatch(Cursor & cur, FuncRe
 				// but bvcur is current mouse position
 				Cursor & bvcur = cur.bv().cursor();
 				bvcur.selection() = true;
-				saveSelection(bvcur);
 			}
 			needsUpdate = false;
 			cur.noUpdate();
@@ -1589,7 +1566,6 @@ void Text::dispatch(Cursor & cur, FuncRe
 	case LFUN_ESCAPE:
 		if (cur.selection()) {
 			cur.selection() = false;
-			saveSelection(cur);
 		} else {
 			cur.undispatched();
 			cmd = FuncRequest(LFUN_FINISHED_RIGHT);
Index: src/Cursor.cpp
===================================================================
--- src/Cursor.cpp	(révision 18996)
+++ src/Cursor.cpp	(copie de travail)
@@ -588,9 +588,11 @@ bool Cursor::selHandle(bool sel)
 	if (sel == selection())
 		return false;
 
+	if (!sel)
+		cap::saveSelection(*this);
+
 	resetAnchor();
 	selection() = sel;
-	cap::saveSelection(*this);
 	return true;
 }
 

Reply via email to