commit 641ae5c7add025e51a2da718cdbb0dff098b29e7
Author: Pavel Sanda <sa...@lyx.org>
Date:   Wed May 2 22:35:09 2018 +0200

    Enable graphics-unify only for selections containing graphics insets.
    
    Follow-up of
    https://www.mail-archive.com/lyx-devel@lists.lyx.org/msg204907.html
---
 src/BufferView.cpp |    2 +-
 src/Cursor.cpp     |   50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 src/Cursor.h       |    6 ++++++
 3 files changed, 57 insertions(+), 1 deletions(-)

diff --git a/src/BufferView.cpp b/src/BufferView.cpp
index ed559b1..b8bc705 100644
--- a/src/BufferView.cpp
+++ b/src/BufferView.cpp
@@ -1154,7 +1154,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 07dc08d..b78c1f5 100644
--- a/src/Cursor.cpp
+++ b/src/Cursor.cpp
@@ -496,6 +496,56 @@ void CursorData::clearSelection()
 }
 
 
+int CursorData::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 CursorData::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;
+}
+
+
 bool CursorData::fixIfBroken()
 {
        bool const broken_cursor = DocIterator::fixIfBroken();
diff --git a/src/Cursor.h b/src/Cursor.h
index efce063..3c132e0 100644
--- a/src/Cursor.h
+++ b/src/Cursor.h
@@ -129,6 +129,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 to normalized selection anchor
        CursorSlice normalAnchor() const;

Reply via email to