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

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