From 7bcce4180d11f6f49a71f681eeefc556fc2302ba Mon Sep 17 00:00:00 2001 From: Rafael Lima Date: Thu, 25 Jan 2024 23:54:39 +0100 Subject: tdf#131641 Enter selected text in the search bar (Basic IDE) This patch makes the selected text in the code editor be automatically inserted in the search bar. Change-Id: Ibbe64aa3375a5a47dedb762001ed4b99f4b22e46 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/162548 Reviewed-by: Andreas Heinisch Tested-by: Andreas Heinisch --- basctl/source/basicide/unomodel.cxx | 51 ++++++++++++++++++++++++++ basctl/source/basicide/unomodel.hxx | 3 ++ svx/source/tbxctrls/tbunosearchcontrollers.cxx | 20 ++++++++-- 3 files changed, 70 insertions(+), 4 deletions(-) diff --git a/basctl/source/basicide/unomodel.cxx b/basctl/source/basicide/unomodel.cxx index 5d3946b426c6..180bf3d17fa7 100644 --- a/basctl/source/basicide/unomodel.cxx +++ b/basctl/source/basicide/unomodel.cxx @@ -19,6 +19,7 @@ #include "basdoc.hxx" +#include #include #include #include @@ -29,6 +30,41 @@ #include "unomodel.hxx" + +namespace { + +// Implements XEnumeration to hold a single selected portion of text +// This will actually only hold a single string value +class SelectionEnumeration : public ::cppu::WeakImplHelper +{ +private: + OUString m_sText; + bool m_bHasElements; + +public: + explicit SelectionEnumeration(OUString& sSelectedText) + : m_sText(sSelectedText) + , m_bHasElements(true) {} + + virtual sal_Bool SAL_CALL hasMoreElements() override + { + return m_bHasElements; + } + + virtual css::uno::Any SAL_CALL nextElement() override + { + if (m_bHasElements) + { + m_bHasElements = false; + return css::uno::Any(m_sText); + } + + throw css::container::NoSuchElementException(); + } +}; + +} // End of unnamed namespace + namespace basctl { @@ -114,6 +150,21 @@ void SIDEModel::notImplemented() throw io::IOException("Can't store IDE model" ); } +// XModel +css::uno::Reference< css::uno::XInterface > SAL_CALL SIDEModel::getCurrentSelection() +{ + SolarMutexGuard aGuard; + uno::Reference xEnum; + Shell* pShell = GetShell(); + + if (pShell) + { + OUString sText = GetShell()->GetSelectionText(false); + xEnum = new SelectionEnumeration(sText); + } + return xEnum; +} + } // namespace basctl extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface* diff --git a/basctl/source/basicide/unomodel.hxx b/basctl/source/basicide/unomodel.hxx index b47873005699..9b0289032660 100644 --- a/basctl/source/basicide/unomodel.hxx +++ b/basctl/source/basicide/unomodel.hxx @@ -53,6 +53,9 @@ public: const css::uno::Sequence< css::beans::PropertyValue >& seqArguments ) override; virtual void SAL_CALL storeToURL( const OUString& sURL, const css::uno::Sequence< css::beans::PropertyValue >& seqArguments ) override; + + // XModel + virtual css::uno::Reference< css::uno::XInterface > SAL_CALL getCurrentSelection() override; }; } // namespace basctl diff --git a/svx/source/tbxctrls/tbunosearchcontrollers.cxx b/svx/source/tbxctrls/tbunosearchcontrollers.cxx index f8c4436443bc..2f23bfaa97f4 100644 --- a/svx/source/tbxctrls/tbunosearchcontrollers.cxx +++ b/svx/source/tbxctrls/tbunosearchcontrollers.cxx @@ -45,6 +45,7 @@ #include #include #include +#include #include #include #include @@ -253,11 +254,22 @@ void FindTextFieldControl::SetTextToSelected_Impl() else { uno::Reference xModel(xController->getModel(), uno::UNO_SET_THROW); - uno::Reference xIndexAccess(xModel->getCurrentSelection(), uno::UNO_QUERY_THROW); - if (xIndexAccess->getCount() > 0) + uno::Reference xSelection = xModel->getCurrentSelection(); + uno::Reference xIndexAccess(xSelection, uno::UNO_QUERY); + if (xIndexAccess.is()) { - uno::Reference xTextRange(xIndexAccess->getByIndex(0), uno::UNO_QUERY_THROW); - aString = xTextRange->getString(); + if (xIndexAccess->getCount() > 0) + { + uno::Reference xTextRange(xIndexAccess->getByIndex(0), uno::UNO_QUERY_THROW); + aString = xTextRange->getString(); + } + } + else + { + // The Basic IDE returns a XEnumeration with a single item + uno::Reference xEnum(xSelection, uno::UNO_QUERY_THROW); + if (xEnum->hasMoreElements()) + xEnum->nextElement() >>= aString; } } } -- cgit