135360
From 27ebd2b9c14a691889e6c7332024feab7c723d49 Mon Sep 17 00:00:00 2001
135360
From: Miklos Vajna <vmiklos@collabora.co.uk>
135360
Date: Wed, 16 Sep 2015 09:30:41 +0200
135360
Subject: [PATCH 139/398] lok::Document: add get/setView()
135360
135360
Change-Id: Ic3bce8f01d7e048e853c063c4bce1255845c60d0
135360
(cherry picked from commit 46588c42a546d4517b773853856b9c8f8c2e5ece)
135360
---
135360
 desktop/qa/desktop_lib/test_desktop_lib.cxx |  6 ++++++
135360
 desktop/source/lib/init.cxx                 | 18 ++++++++++++++++++
135360
 include/LibreOfficeKit/LibreOfficeKit.h     |  4 ++++
135360
 include/LibreOfficeKit/LibreOfficeKit.hxx   | 20 +++++++++++++++++++-
135360
 include/sfx2/lokhelper.hxx                  |  4 ++++
135360
 sfx2/source/view/lokhelper.cxx              | 24 ++++++++++++++++++++++++
135360
 6 files changed, 75 insertions(+), 1 deletion(-)
135360
135360
diff --git a/desktop/qa/desktop_lib/test_desktop_lib.cxx b/desktop/qa/desktop_lib/test_desktop_lib.cxx
135360
index 6baaa32239c9..7ad81278d64a 100644
135360
--- a/desktop/qa/desktop_lib/test_desktop_lib.cxx
135360
+++ b/desktop/qa/desktop_lib/test_desktop_lib.cxx
135360
@@ -139,6 +139,12 @@ void DesktopLOKTest::testCreateView()
135360
     int nId = pDocument->m_pDocumentClass->createView(pDocument);
135360
     CPPUNIT_ASSERT_EQUAL(2, SfxLokHelper::getViews());
135360
 
135360
+    // Make sure the created view is the active one, then switch to the old
135360
+    // one.
135360
+    CPPUNIT_ASSERT_EQUAL(1, pDocument->m_pDocumentClass->getView(pDocument));
135360
+    pDocument->m_pDocumentClass->setView(pDocument, 0);
135360
+    CPPUNIT_ASSERT_EQUAL(0, pDocument->m_pDocumentClass->getView(pDocument));
135360
+
135360
     pDocument->m_pDocumentClass->destroyView(pDocument, nId);
135360
     CPPUNIT_ASSERT_EQUAL(1, SfxLokHelper::getViews());
135360
     closeDoc();
135360
diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx
135360
index 3b1286e5944d..d76de5e5bb3c 100644
135360
--- a/desktop/source/lib/init.cxx
135360
+++ b/desktop/source/lib/init.cxx
135360
@@ -252,6 +252,8 @@ static char* doc_getCommandValues(LibreOfficeKitDocument* pThis, const char* pCo
135360
 
135360
 static int doc_createView(LibreOfficeKitDocument* pThis);
135360
 static void doc_destroyView(LibreOfficeKitDocument* pThis, int nId);
135360
+static void doc_setView(LibreOfficeKitDocument* pThis, int nId);
135360
+static int doc_getView(LibreOfficeKitDocument* pThis);
135360
 
135360
 LibLODocument_Impl::LibLODocument_Impl(const uno::Reference <css::lang::XComponent> &xComponent) :
135360
     mxComponent( xComponent )
135360
@@ -285,6 +287,8 @@ LibLODocument_Impl::LibLODocument_Impl(const uno::Reference 
135360
 
135360
         m_pDocumentClass->createView = doc_createView;
135360
         m_pDocumentClass->destroyView = doc_destroyView;
135360
+        m_pDocumentClass->setView = doc_setView;
135360
+        m_pDocumentClass->getView = doc_getView;
135360
 
135360
         gDocumentClass = m_pDocumentClass;
135360
     }
135360
@@ -1067,6 +1071,20 @@ static void doc_destroyView(LibreOfficeKitDocument* /*pThis*/, int nId)
135360
     SfxLokHelper::destroyView(nId);
135360
 }
135360
 
135360
+static void doc_setView(LibreOfficeKitDocument* /*pThis*/, int nId)
135360
+{
135360
+    SolarMutexGuard aGuard;
135360
+
135360
+    SfxLokHelper::setView(nId);
135360
+}
135360
+
135360
+static int doc_getView(LibreOfficeKitDocument* /*pThis*/)
135360
+{
135360
+    SolarMutexGuard aGuard;
135360
+
135360
+    return SfxLokHelper::getView();
135360
+}
135360
+
135360
 static char* lo_getError (LibreOfficeKit *pThis)
135360
 {
135360
     LibLibreOffice_Impl* pLib = static_cast<LibLibreOffice_Impl*>(pThis);
135360
diff --git a/include/LibreOfficeKit/LibreOfficeKit.h b/include/LibreOfficeKit/LibreOfficeKit.h
135360
index 7f41d13ff393..18f4b3edc624 100644
135360
--- a/include/LibreOfficeKit/LibreOfficeKit.h
135360
+++ b/include/LibreOfficeKit/LibreOfficeKit.h
135360
@@ -170,6 +170,10 @@ struct _LibreOfficeKitDocumentClass
135360
     int (*createView) (LibreOfficeKitDocument* pThis);
135360
     /// @see lok::Document::destroyView().
135360
     void (*destroyView) (LibreOfficeKitDocument* pThis, int nId);
135360
+    /// @see lok::Document::setView().
135360
+    void (*setView) (LibreOfficeKitDocument* pThis, int nId);
135360
+    /// @see lok::Document::getView().
135360
+    int (*getView) (LibreOfficeKitDocument* pThis);
135360
 #endif // LOK_USE_UNSTABLE_API
135360
 };
135360
 
135360
diff --git a/include/LibreOfficeKit/LibreOfficeKit.hxx b/include/LibreOfficeKit/LibreOfficeKit.hxx
135360
index 3e1a0ac20b07..f5821b71191a 100644
135360
--- a/include/LibreOfficeKit/LibreOfficeKit.hxx
135360
+++ b/include/LibreOfficeKit/LibreOfficeKit.hxx
135360
@@ -269,13 +269,31 @@ public:
135360
     }
135360
 
135360
     /**
135360
-     * Destroy a view of an existring document.
135360
+     * Destroy a view of an existing document.
135360
      * @param nId a view ID, returned by createView().
135360
      */
135360
     void destroyView(int nId)
135360
     {
135360
         mpDoc->pClass->destroyView(mpDoc, nId);
135360
     }
135360
+
135360
+    /**
135360
+     * Set an existing view of an existing document as current.
135360
+     * @param nId a view ID, returned by createView().
135360
+     */
135360
+    void setView(int nId)
135360
+    {
135360
+        mpDoc->pClass->setView(mpDoc, nId);
135360
+    }
135360
+
135360
+    /**
135360
+     * Get the current view.
135360
+     * @return a view ID, previously returned by createView().
135360
+     */
135360
+    int getView()
135360
+    {
135360
+        return mpDoc->pClass->getView(mpDoc);
135360
+    }
135360
 #endif // LOK_USE_UNSTABLE_API
135360
 };
135360
 
135360
diff --git a/include/sfx2/lokhelper.hxx b/include/sfx2/lokhelper.hxx
135360
index b57cb7d75b23..a05cd5d4b210 100644
135360
--- a/include/sfx2/lokhelper.hxx
135360
+++ b/include/sfx2/lokhelper.hxx
135360
@@ -21,6 +21,10 @@ public:
135360
     static int createView(SfxViewShell* pViewShell);
135360
     /// Destroy a view shell from the global shell list.
135360
     static void destroyView(size_t nId);
135360
+    /// Set a view shell as current one.
135360
+    static void setView(size_t nId);
135360
+    /// Get the currently active view.
135360
+    static size_t getView();
135360
 
135360
     /// Total number of view shells.
135360
     static int getViews();
135360
diff --git a/sfx2/source/view/lokhelper.cxx b/sfx2/source/view/lokhelper.cxx
135360
index 557478a78ae6..0aea6db9c24e 100644
135360
--- a/sfx2/source/view/lokhelper.cxx
135360
+++ b/sfx2/source/view/lokhelper.cxx
135360
@@ -38,6 +38,30 @@ void SfxLokHelper::destroyView(size_t nId)
135360
     pViewFrame->Exec_Impl(aRequest);
135360
 }
135360
 
135360
+void SfxLokHelper::setView(size_t nId)
135360
+{
135360
+    SfxViewShellArr_Impl& rViewArr = SfxGetpApp()->GetViewShells_Impl();
135360
+    if (nId > rViewArr.size() - 1)
135360
+        return;
135360
+
135360
+    SfxViewShell* pViewShell = rViewArr[nId];
135360
+    if (SfxViewFrame* pViewFrame = pViewShell->GetViewFrame())
135360
+        pViewFrame->GetWindow().GrabFocus();
135360
+}
135360
+
135360
+size_t SfxLokHelper::getView()
135360
+{
135360
+    SfxViewShellArr_Impl& rViewArr = SfxGetpApp()->GetViewShells_Impl();
135360
+    SfxViewFrame* pViewFrame = SfxViewFrame::Current();
135360
+    for (size_t i = 0; i < rViewArr.size(); ++i)
135360
+    {
135360
+        if (rViewArr[i]->GetViewFrame() == pViewFrame)
135360
+            return i;
135360
+    }
135360
+    assert(false);
135360
+    return 0;
135360
+}
135360
+
135360
 int SfxLokHelper::getViews()
135360
 {
135360
     SfxViewShellArr_Impl& rViewArr = SfxGetpApp()->GetViewShells_Impl();
135360
-- 
135360
2.12.0
135360