Blob Blame History Raw
From 4a8fa9d37030d92b523707602e938e6604412727 Mon Sep 17 00:00:00 2001
From: Miklos Vajna <vmiklos@collabora.co.uk>
Date: Tue, 6 Oct 2015 09:21:38 +0200
Subject: [PATCH 179/398] LOK: add CALLBACK_SEARCH_RESULT_SELECTION and
 implement it in sw

(cherry picked from commit 94752d5970be7ce22e053f9cd83bd59711446a0a)

Change-Id: I4c2a5418101976e1cb38c0fa71dbd66fc883f905
---
 include/LibreOfficeKit/LibreOfficeKitEnums.h | 21 +++++++++-
 sw/source/uibase/uiview/viewsrch.cxx         | 57 +++++++++++++++++++++++++++-
 2 files changed, 76 insertions(+), 2 deletions(-)

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