Blame SOURCES/0179-LOK-add-CALLBACK_SEARCH_RESULT_SELECTION-and-impleme.patch

f325b2
From 4a8fa9d37030d92b523707602e938e6604412727 Mon Sep 17 00:00:00 2001
f325b2
From: Miklos Vajna <vmiklos@collabora.co.uk>
f325b2
Date: Tue, 6 Oct 2015 09:21:38 +0200
f325b2
Subject: [PATCH 179/398] LOK: add CALLBACK_SEARCH_RESULT_SELECTION and
f325b2
 implement it in sw
f325b2
f325b2
(cherry picked from commit 94752d5970be7ce22e053f9cd83bd59711446a0a)
f325b2
f325b2
Change-Id: I4c2a5418101976e1cb38c0fa71dbd66fc883f905
f325b2
---
f325b2
 include/LibreOfficeKit/LibreOfficeKitEnums.h | 21 +++++++++-
f325b2
 sw/source/uibase/uiview/viewsrch.cxx         | 57 +++++++++++++++++++++++++++-
f325b2
 2 files changed, 76 insertions(+), 2 deletions(-)
f325b2
f325b2
diff --git a/include/LibreOfficeKit/LibreOfficeKitEnums.h b/include/LibreOfficeKit/LibreOfficeKitEnums.h
f325b2
index 97c089ffef50..b87f69a39989 100644
f325b2
--- a/include/LibreOfficeKit/LibreOfficeKitEnums.h
f325b2
+++ b/include/LibreOfficeKit/LibreOfficeKitEnums.h
f325b2
@@ -161,7 +161,26 @@ typedef enum
f325b2
      * Number of search results followed by the original searched phrase.
f325b2
      * count;phrase
f325b2
      */
f325b2
-    LOK_CALLBACK_SEARCH_RESULT_COUNT
f325b2
+    LOK_CALLBACK_SEARCH_RESULT_COUNT,
f325b2
+
f325b2
+    /**
f325b2
+     * Selection rectangles of the search result when find all is performed.
f325b2
+     *
f325b2
+     * Payload format example, in case of two matches:
f325b2
+     *
f325b2
+     * {
f325b2
+     *     "searchString": "...",
f325b2
+     *     "searchResultSelection": [
f325b2
+     *         "...",
f325b2
+     *         "..."
f325b2
+     *     ]
f325b2
+     * }
f325b2
+     *
f325b2
+     * - searchString is the search query
f325b2
+     * - searchResultSelection is an array of rectangle list, in
f325b2
+     *   LOK_CALLBACK_TEXT_SELECTION format.
f325b2
+     */
f325b2
+    LOK_CALLBACK_SEARCH_RESULT_SELECTION
f325b2
 }
f325b2
 LibreOfficeKitCallbackType;
f325b2
 
f325b2
diff --git a/sw/source/uibase/uiview/viewsrch.cxx b/sw/source/uibase/uiview/viewsrch.cxx
f325b2
index 3ccc852065fb..570fadeb561a 100644
f325b2
--- a/sw/source/uibase/uiview/viewsrch.cxx
f325b2
+++ b/sw/source/uibase/uiview/viewsrch.cxx
f325b2
@@ -22,6 +22,7 @@
f325b2
 #include <string>
f325b2
 
f325b2
 #include <boost/scoped_ptr.hpp>
f325b2
+#include <boost/property_tree/json_parser.hpp>
f325b2
 
f325b2
 #include <hintids.hxx>
f325b2
 
f325b2
@@ -58,6 +59,7 @@
f325b2
 #include <doc.hxx>
f325b2
 #include <unocrsr.hxx>
f325b2
 #include <LibreOfficeKit/LibreOfficeKitEnums.h>
f325b2
+#include <comphelper/lok.hxx>
f325b2
 
f325b2
 #include <view.hrc>
f325b2
 #include <SwRewriter.hxx>
f325b2
@@ -85,6 +87,21 @@ static vcl::Window* GetParentWindow( SvxSearchDialog* pSrchDlg )
f325b2
     return pSrchDlg && pSrchDlg->IsVisible() ? pSrchDlg : 0;
f325b2
 }
f325b2
 
f325b2
+/// Adds rMatches using rKey as a key to the rTree tree.
f325b2
+static void lcl_addContainerToJson(boost::property_tree::ptree& rTree, const OString& rKey, const std::vector<OString>& rMatches)
f325b2
+{
f325b2
+    boost::property_tree::ptree aChildren;
f325b2
+
f325b2
+    for (const OString& rMatch : rMatches)
f325b2
+    {
f325b2
+        boost::property_tree::ptree aChild;
f325b2
+        aChild.put("", rMatch.getStr());
f325b2
+        aChildren.push_back(std::make_pair("", aChild));
f325b2
+    }
f325b2
+
f325b2
+    rTree.add_child(rKey.getStr(), aChildren);
f325b2
+}
f325b2
+
f325b2
 void SwView::ExecSearch(SfxRequest& rReq, bool bNoMessage)
f325b2
 {
f325b2
     const SfxItemSet* pArgs = rReq.GetArgs();
f325b2
@@ -223,10 +240,48 @@ void SwView::ExecSearch(SfxRequest& rReq, bool bNoMessage)
f325b2
 #endif
f325b2
                     m_bFound = false;
f325b2
                 }
f325b2
-                else
f325b2
+                else if (comphelper::LibreOfficeKit::isActive())
f325b2
                 {
f325b2
                     OString aPayload = OString::number(nFound) + ";" + m_pSrchItem->GetSearchString().toUtf8();
f325b2
                     m_pWrtShell->libreOfficeKitCallback(LOK_CALLBACK_SEARCH_RESULT_COUNT, aPayload.getStr());
f325b2
+
f325b2
+                    // Emit a callback also about the selection rectangles, grouped by matches.
f325b2
+                    if (SwPaM* pPaM = m_pWrtShell->GetCrsr())
f325b2
+                    {
f325b2
+                        std::vector<OString> aMatches;
f325b2
+                        for (SwPaM& rPaM : pPaM->GetRingContainer())
f325b2
+                        {
f325b2
+                            if (SwShellCrsr* pShellCrsr = dynamic_cast<SwShellCrsr*>(&rPaM))
f325b2
+                            {
f325b2
+                                std::vector<OString> aSelectionRectangles;
f325b2
+                                pShellCrsr->SwSelPaintRects::Show(&aSelectionRectangles);
f325b2
+                                std::stringstream ss;
f325b2
+                                bool bFirst = true;
f325b2
+                                for (size_t i = 0; i < aSelectionRectangles.size(); ++i)
f325b2
+                                {
f325b2
+                                    const OString& rSelectionRectangle = aSelectionRectangles[i];
f325b2
+                                    if (rSelectionRectangle.isEmpty())
f325b2
+                                        continue;
f325b2
+                                    if (bFirst)
f325b2
+                                        bFirst = false;
f325b2
+                                    else
f325b2
+                                        ss << "; ";
f325b2
+                                    ss << rSelectionRectangle.getStr();
f325b2
+                                }
f325b2
+                                OString sRect = ss.str().c_str();
f325b2
+                                aMatches.push_back(sRect);
f325b2
+                            }
f325b2
+                        }
f325b2
+                        boost::property_tree::ptree aTree;
f325b2
+                        aTree.put("searchString", m_pSrchItem->GetSearchString().toUtf8().getStr());
f325b2
+                        lcl_addContainerToJson(aTree, "searchResultSelection", aMatches);
f325b2
+
f325b2
+                        std::stringstream aStream;
f325b2
+                        boost::property_tree::write_json(aStream, aTree);
f325b2
+                        aPayload = aStream.str().c_str();
f325b2
+
f325b2
+                        m_pWrtShell->libreOfficeKitCallback(LOK_CALLBACK_SEARCH_RESULT_SELECTION, aPayload.getStr());
f325b2
+                    }
f325b2
                 }
f325b2
                 rReq.SetReturnValue(SfxBoolItem(nSlot, bRet));
f325b2
 #if HAVE_FEATURE_DESKTOP
f325b2
-- 
f325b2
2.12.0
f325b2