From 05282ceeec5fc9872f73f7de93136fc218f8ecb4 Mon Sep 17 00:00:00 2001 From: Pavel Sanda Date: Fri, 14 Dec 2018 19:19:08 +0100 Subject: [PATCH] Backport 641ae5c7add025. The main reason is to prepare #5348 for landing. --- src/BufferView.cpp | 2 +- src/Cursor.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++ src/Cursor.h | 6 ++++++ 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/src/BufferView.cpp b/src/BufferView.cpp index ac5c318909..9ecbdbb3af 100644 --- a/src/BufferView.cpp +++ b/src/BufferView.cpp @@ -1149,7 +1149,7 @@ bool BufferView::getStatus(FuncRequest const & cmd, FuncStatus & flag) break; case LFUN_GRAPHICS_UNIFY: - flag.setEnabled(cur.selection()); + flag.setEnabled(cur.countInsetsInSelection(GRAPHICS_CODE)>1); break; case LFUN_WORD_FINDADV: { diff --git a/src/Cursor.cpp b/src/Cursor.cpp index 92c9bdda26..e277c880fa 100644 --- a/src/Cursor.cpp +++ b/src/Cursor.cpp @@ -1014,6 +1014,56 @@ void Cursor::clearSelection() } +int Cursor::countInsetsInSelection(InsetCode const & inset_code) +{ + if (!selection_) + return 0; + + DocIterator from, to; + from = selectionBegin(); + to = selectionEnd(); + + int count = 0; + + if (!from.nextInset()) //move to closest inset + from.forwardInset(); + + while (!from.empty() && from < to) { + Inset * inset = from.nextInset(); + if (!inset) + break; + if (inset->lyxCode() == inset_code) + count ++; + from.forwardInset(); + } + return count; +} + + +bool Cursor::insetInSelection(InsetCode const & inset_code) +{ + if (!selection_) + return false; + + DocIterator from, to; + from = selectionBegin(); + to = selectionEnd(); + + if (!from.nextInset()) //move to closest inset + from.forwardInset(); + + while (!from.empty() && from < to) { + Inset * inset = from.nextInset(); + if (!inset) + break; + if (inset->lyxCode() == inset_code) + return true; + from.forwardInset(); + } + return false; +} + + void Cursor::setTargetX(int x) { x_target_ = x; diff --git a/src/Cursor.h b/src/Cursor.h index 55cc30e941..1da9dbd17c 100644 --- a/src/Cursor.h +++ b/src/Cursor.h @@ -186,6 +186,12 @@ public: void setSelection(DocIterator const & where, int n); /// void clearSelection(); + /// check whether selection contains specific type of inset + /// returns 0 if no selection was made + bool insetInSelection(InsetCode const & inset); + /// count occurences of specific inset type in the selection + /// returns 0 if no selection was made + int countInsetsInSelection(InsetCode const & inset); /// access start of selection CursorSlice selBegin() const; /// access end of selection -- 2.39.5