sw/inc/doc.hxx                    |    7 +++++++
 sw/source/core/doc/docnew.cxx     |    2 ++
 sw/source/ui/vba/vbafind.cxx      |   30 ++++++++++++++++++++++++++++--
 sw/source/ui/vba/vbafind.hxx      |    5 +++--
 sw/source/ui/vba/vbaselection.cxx |    3 ++-
 5 files changed, 42 insertions(+), 5 deletions(-)

New commits:
commit f6be2bf969bac1f5c6ac452b313374ba09ffaa58
Author:     Hannah Meeks <hmeeks4...@gmail.com>
AuthorDate: Wed Jun 29 16:27:17 2022 +0100
Commit:     Tomaž Vajngerl <qui...@gmail.com>
CommitDate: Tue Jul 12 07:38:07 2022 +0200

    tdf#149775 - VBA: Find should stay around.
    
    Previously we would create and destroy it for each set of a
    property on it making it useless. VBA Find object oddly
    represents the Find dialog in Word. Add it to SwDoc.
    
    Change-Id: Id9850cbd2296b24f9c93dc99b2967095bd84921f
    Signed-off-by: Hannah Meeks <hmeeks4...@gmail.com>
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/136630
    Tested-by: Jenkins
    Reviewed-by: Tomaž Vajngerl <qui...@gmail.com>

diff --git a/sw/inc/doc.hxx b/sw/inc/doc.hxx
index 624686890850..e88214977e76 100644
--- a/sw/inc/doc.hxx
+++ b/sw/inc/doc.hxx
@@ -175,6 +175,10 @@ namespace com::sun::star {
     namespace script::vba { class XVBAEventProcessor; }
 }
 
+namespace ooo::vba::word {
+    class XFind;
+}
+
 namespace sfx2 {
     class IXmlIdRegistry;
 }
@@ -281,6 +285,7 @@ class SW_DLLPUBLIC SwDoc final
     std::unique_ptr<IGrammarContact> mpGrammarContact;             //< for 
grammar checking in paragraphs during editing
 
     css::uno::Reference< css::script::vba::XVBAEventProcessor > mxVbaEvents;
+    css::uno::Reference< ooo::vba::word::XFind > mxVbaFind;
     css::uno::Reference<css::container::XNameContainer> 
m_xTemplateToProjectCache;
 
     /// Table styles (autoformats that are applied with table changes).
@@ -1619,6 +1624,8 @@ public:
     void SetDefaultPageMode(bool bSquaredPageMode);
     bool IsSquaredPageMode() const;
 
+    css::uno::Reference< ooo::vba::word::XFind > getVbaFind() const { return 
mxVbaFind; }
+    void setVbaFind( const css::uno::Reference< ooo::vba::word::XFind > 
&xFind) { mxVbaFind = xFind; }
     css::uno::Reference< css::script::vba::XVBAEventProcessor > const & 
GetVbaEventProcessor();
     void SetVBATemplateToProjectCache( css::uno::Reference< 
css::container::XNameContainer > const & xCache ) { m_xTemplateToProjectCache = 
xCache; };
     const css::uno::Reference< css::container::XNameContainer >& 
GetVBATemplateToProjectCache() const { return m_xTemplateToProjectCache; };
diff --git a/sw/source/core/doc/docnew.cxx b/sw/source/core/doc/docnew.cxx
index 1a9f7e868980..576dde4c7a89 100644
--- a/sw/source/core/doc/docnew.cxx
+++ b/sw/source/core/doc/docnew.cxx
@@ -395,6 +395,8 @@ SwDoc::SwDoc()
  */
 SwDoc::~SwDoc()
 {
+    mxVbaFind.clear();
+
     // nothing here should create Undo actions!
     GetIDocumentUndoRedo().DoUndo(false);
 
diff --git a/sw/source/ui/vba/vbafind.cxx b/sw/source/ui/vba/vbafind.cxx
index db34a32f9b64..3c9940cc0e1f 100644
--- a/sw/source/ui/vba/vbafind.cxx
+++ b/sw/source/ui/vba/vbafind.cxx
@@ -22,13 +22,18 @@
 #include <ooo/vba/word/WdReplace.hpp>
 #include <com/sun/star/frame/XModel.hpp>
 #include <com/sun/star/text/XTextRangeCompare.hpp>
+#include <doc.hxx>
+#include <docsh.hxx>
 #include "wordvbahelper.hxx"
+#include <rtl/ref.hxx>
+#include <sal/log.hxx>
 
 using namespace ::ooo::vba;
 using namespace ::com::sun::star;
 
-SwVbaFind::SwVbaFind( const uno::Reference< ooo::vba::XHelperInterface >& 
rParent, const uno::Reference< uno::XComponentContext >& rContext, const 
uno::Reference< frame::XModel >& xModel, const uno::Reference< text::XTextRange 
>& xTextRange ) :
-    SwVbaFind_BASE( rParent, rContext ), mxModel( xModel ), mxTextRange( 
xTextRange ), mbReplace( false ), mnReplaceType( word::WdReplace::wdReplaceOne 
), mnWrap( word::WdFindWrap::wdFindStop )
+
+SwVbaFind::SwVbaFind( const uno::Reference< ooo::vba::XHelperInterface >& 
rParent, const uno::Reference< uno::XComponentContext >& rContext, const 
uno::Reference< frame::XModel >& xModel ) :
+    SwVbaFind_BASE( rParent, rContext ), mxModel( xModel ), mbReplace( false 
), mnReplaceType( word::WdReplace::wdReplaceOne ), mnWrap( 
word::WdFindWrap::wdFindStop )
 {
     mxReplaceable.set( mxModel, uno::UNO_QUERY_THROW );
     mxPropertyReplace.set( mxReplaceable->createReplaceDescriptor(), 
uno::UNO_QUERY_THROW );
@@ -40,6 +45,27 @@ SwVbaFind::~SwVbaFind()
 {
 }
 
+uno::Reference< word::XFind > SwVbaFind::GetOrCreateFind(const uno::Reference< 
ooo::vba::XHelperInterface >& rParent,
+                                                         const uno::Reference< 
uno::XComponentContext >& rContext,
+                                                         const uno::Reference< 
frame::XModel >& xModel,
+                                                         const uno::Reference< 
text::XTextRange >& xTextRange)
+{
+    rtl::Reference< SwVbaFind > xFind;
+    SwDoc* pDoc = word::getDocShell( xModel )->GetDoc();
+    if( pDoc )
+        xFind = dynamic_cast<SwVbaFind *>( pDoc->getVbaFind().get() );
+    if ( !xFind )
+    {
+        xFind = new SwVbaFind( rParent, rContext, xModel );
+        if ( pDoc )
+            pDoc->setVbaFind( xFind );
+    }
+    xFind->mxTextRange = xTextRange;
+
+    return xFind;
+}
+
+
 bool SwVbaFind::InRange( const uno::Reference< text::XTextRange >& 
xCurrentRange )
 {
     uno::Reference< text::XTextRangeCompare > xTRC( mxTextRange->getText(), 
uno::UNO_QUERY_THROW );
diff --git a/sw/source/ui/vba/vbafind.hxx b/sw/source/ui/vba/vbafind.hxx
index cdbcc95538f7..ed2df832f00e 100644
--- a/sw/source/ui/vba/vbafind.hxx
+++ b/sw/source/ui/vba/vbafind.hxx
@@ -57,9 +57,10 @@ private:
     /// @throws css::uno::RuntimeException
     bool SearchReplace();
 
-public:
     /// @throws css::uno::RuntimeException
-    SwVbaFind( const css::uno::Reference< ooo::vba::XHelperInterface >& 
rParent, const css::uno::Reference< css::uno::XComponentContext >& rContext, 
const css::uno::Reference< css::frame::XModel >& xModel, const 
css::uno::Reference< css::text::XTextRange >& xTextRange );
+    SwVbaFind( const css::uno::Reference< ooo::vba::XHelperInterface >& 
rParent, const css::uno::Reference< css::uno::XComponentContext >& rContext, 
const css::uno::Reference< css::frame::XModel >& xModel );
+public:
+    static css::uno::Reference< ooo::vba::word::XFind > GetOrCreateFind(const 
css::uno::Reference< ooo::vba::XHelperInterface >& rParent, const 
css::uno::Reference< com::sun::star::uno::XComponentContext >& rContext, const 
css::uno::Reference< com::sun::star::frame::XModel >& xModel, const 
css::uno::Reference< css::text::XTextRange >& xTextRange);
     virtual ~SwVbaFind() override;
 
     // Attributes
diff --git a/sw/source/ui/vba/vbaselection.cxx 
b/sw/source/ui/vba/vbaselection.cxx
index dde7d4e21fb5..43da7dc6af48 100644
--- a/sw/source/ui/vba/vbaselection.cxx
+++ b/sw/source/ui/vba/vbaselection.cxx
@@ -60,6 +60,7 @@
 #include "vbastyle.hxx"
 #include <docsh.hxx>
 #include <tblenum.hxx>
+#include <sal/log.hxx>
 #include <fesh.hxx>
 
 using namespace ::ooo::vba;
@@ -518,7 +519,7 @@ uno::Reference< word::XFind > SAL_CALL
 SwVbaSelection::getFind()
 {
     uno::Reference< text::XTextRange > xTextRange = GetSelectedRange();
-    return uno::Reference< word::XFind >( new SwVbaFind( this, mxContext, 
mxModel, xTextRange ) );
+    return SwVbaFind::GetOrCreateFind(this, mxContext, mxModel, xTextRange);
 }
 
 uno::Any SAL_CALL

Reply via email to