offapi/com/sun/star/text/TextField.idl |    6 ++++++
 sw/inc/fldbas.hxx                      |    4 ++++
 sw/qa/core/unocore/unocore.cxx         |   29 +++++++++++++++++++++++++++++
 sw/source/core/fields/fldbas.cxx       |   15 +++++++++++++++
 sw/source/core/fields/usrfld.cxx       |    1 +
 sw/source/core/inc/unofldmid.h         |    1 +
 sw/source/core/unocore/unomap.cxx      |    1 +
 sw/source/uibase/docvw/edtwin2.cxx     |   12 ++++++++++++
 8 files changed, 69 insertions(+)

New commits:
commit 1acf8e3cfaf1ef92008e39514a32ace0d036e552
Author:     Miklos Vajna <[email protected]>
AuthorDate: Thu Mar 24 15:18:28 2022 +0100
Commit:     Miklos Vajna <[email protected]>
CommitDate: Thu Mar 24 17:29:52 2022 +0100

    sw fields: add Title uno property
    
    The use-case is user fields, which are kind of variables in the
    document. They have a name and a value, but the name might be only
    readable to an extension or macro, not to the user. In this case, it
    makes sense to have a way to specify a user-readable tooltip.
    
    Be consistent with TextFrames which already have a Title property.
    
    Change-Id: I986792f5e55e0b96489347be482d640d155113cc
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/132077
    Reviewed-by: Miklos Vajna <[email protected]>
    Tested-by: Jenkins

diff --git a/offapi/com/sun/star/text/TextField.idl 
b/offapi/com/sun/star/text/TextField.idl
index 38c58e9afb1e..5499792b8c41 100644
--- a/offapi/com/sun/star/text/TextField.idl
+++ b/offapi/com/sun/star/text/TextField.idl
@@ -66,6 +66,12 @@ published service TextField
      */
     [optional, property, readonly] boolean IsFieldDisplayed;
 
+    /** Contains short title for the field, used to for tooltip purposes if 
it's non-empty.
+
+        @since LibreOffice 7.4
+    */
+    [optional, property] string Title;
+
 
 };
 
diff --git a/sw/inc/fldbas.hxx b/sw/inc/fldbas.hxx
index ccef32fa7921..5a256cb1884d 100644
--- a/sw/inc/fldbas.hxx
+++ b/sw/inc/fldbas.hxx
@@ -297,6 +297,8 @@ private:
     LanguageType        m_nLang;                ///< Always change via 
SetLanguage!
     bool                m_bUseFieldValueCache;  /// control the usage of the 
cached field value
     bool                m_bIsAutomaticLanguage;
+    /// Used for tooltip purposes when it's not-empty.
+    OUString m_aTitle;
 
     virtual OUString    ExpandImpl(SwRootFrame const* pLayout) const = 0;
     virtual std::unique_ptr<SwField> Copy() const = 0;
@@ -389,6 +391,8 @@ public:
     /// Is this field clickable?
     bool IsClickable() const;
     virtual void dumpAsXml(xmlTextWriterPtr pWriter) const;
+    OUString GetTitle() const { return m_aTitle; }
+    void SetTitle(const OUString& rTitle) { m_aTitle = rTitle; }
 };
 
 inline SwFieldType* SwField::GetTyp() const
diff --git a/sw/qa/core/unocore/unocore.cxx b/sw/qa/core/unocore/unocore.cxx
index 30839266a7fc..3a569d64feef 100644
--- a/sw/qa/core/unocore/unocore.cxx
+++ b/sw/qa/core/unocore/unocore.cxx
@@ -13,6 +13,7 @@
 #include <com/sun/star/text/XTextAppend.hpp>
 #include <com/sun/star/text/XTextFrame.hpp>
 #include <com/sun/star/text/XTextViewCursorSupplier.hpp>
+#include <com/sun/star/text/XDependentTextField.hpp>
 
 #include <comphelper/propertyvalue.hxx>
 #include <comphelper/sequenceashashmap.hxx>
@@ -280,6 +281,34 @@ CPPUNIT_TEST_FIXTURE(SwCoreUnocoreTest, 
testLineBreakTextPortionEnum)
     CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int16>(SwLineBreakClear::ALL), 
eClear);
 }
 
+CPPUNIT_TEST_FIXTURE(SwModelTestBase, testUserFieldTooltip)
+{
+    // Given a document with a user field:
+    loadURL("private:factory/swriter", nullptr);
+    uno::Reference<lang::XMultiServiceFactory> xFactory(mxComponent, 
uno::UNO_QUERY);
+    uno::Reference<text::XDependentTextField> xField(
+        xFactory->createInstance("com.sun.star.text.TextField.User"), 
uno::UNO_QUERY);
+    uno::Reference<beans::XPropertySet> xMaster(
+        xFactory->createInstance("com.sun.star.text.FieldMaster.User"), 
uno::UNO_QUERY);
+    xMaster->setPropertyValue("Name", uno::makeAny(OUString("a_user_field")));
+    xField->attachTextFieldMaster(xMaster);
+    xField->getTextFieldMaster()->setPropertyValue("Content", 
uno::makeAny(OUString("42")));
+    uno::Reference<text::XTextDocument> xDocument(mxComponent, uno::UNO_QUERY);
+    uno::Reference<text::XText> xText = xDocument->getText();
+    xText->insertTextContent(xText->createTextCursor(), xField, 
/*bAbsorb=*/false);
+    uno::Reference<beans::XPropertySet> xFieldProps(xField, uno::UNO_QUERY);
+
+    // When setting a tooltip on the field:
+    OUString aExpected("first line\nsecond line");
+    xFieldProps->setPropertyValue("Title", uno::makeAny(aExpected));
+
+    // Then make sure that the tooltip we read back matches the one previously 
specified:
+    // Without the accompanying fix in place, this test would have failed with:
+    // - the property is of unexpected type or void: Title
+    // i.e. reading of the tooltip was broken.
+    CPPUNIT_ASSERT_EQUAL(aExpected, getProperty<OUString>(xFieldProps, 
"Title"));
+}
+
 CPPUNIT_PLUGIN_IMPLEMENT();
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sw/source/core/fields/fldbas.cxx b/sw/source/core/fields/fldbas.cxx
index c4272c3dadea..59e54e291dbb 100644
--- a/sw/source/core/fields/fldbas.cxx
+++ b/sw/source/core/fields/fldbas.cxx
@@ -355,6 +355,11 @@ bool  SwField::QueryValue( uno::Any& rVal, sal_uInt16 
nWhichId ) const
         case FIELD_PROP_BOOL4:
             rVal <<= !m_bIsAutomaticLanguage;
         break;
+        case FIELD_PROP_TITLE:
+        {
+            rVal <<= m_aTitle;
+        }
+        break;
         default:
             assert(false);
     }
@@ -372,6 +377,15 @@ bool SwField::PutValue( const uno::Any& rVal, sal_uInt16 
nWhichId )
                 m_bIsAutomaticLanguage = !bFixed;
         }
         break;
+        case FIELD_PROP_TITLE:
+        {
+            OUString aTitle;
+            if (rVal >>= aTitle)
+            {
+                m_aTitle = aTitle;
+            }
+        }
+        break;
         default:
             assert(false);
     }
@@ -843,6 +857,7 @@ void SwField::dumpAsXml(xmlTextWriterPtr pWriter) const
     (void)xmlTextWriterWriteFormatAttribute(pWriter, BAD_CAST("ptr"), "%p", 
this);
     (void)xmlTextWriterWriteAttribute(pWriter, BAD_CAST("m_nFormat"), 
BAD_CAST(OString::number(m_nFormat).getStr()));
     (void)xmlTextWriterWriteAttribute(pWriter, BAD_CAST("m_nLang"), 
BAD_CAST(OString::number(m_nLang.get()).getStr()));
+    (void)xmlTextWriterWriteAttribute(pWriter, BAD_CAST("m_aTitle"), 
BAD_CAST(m_aTitle.toUtf8().getStr()));
 
     (void)xmlTextWriterEndElement(pWriter);
 }
diff --git a/sw/source/core/fields/usrfld.cxx b/sw/source/core/fields/usrfld.cxx
index 9dcf373edbfd..babb15c3fc32 100644
--- a/sw/source/core/fields/usrfld.cxx
+++ b/sw/source/core/fields/usrfld.cxx
@@ -69,6 +69,7 @@ std::unique_ptr<SwField> SwUserField::Copy() const
 {
     std::unique_ptr<SwField> pTmp(new 
SwUserField(static_cast<SwUserFieldType*>(GetTyp()), m_nSubType, GetFormat()));
     pTmp->SetAutomaticLanguage(IsAutomaticLanguage());
+    pTmp->SetTitle(GetTitle());
     return pTmp;
 }
 
diff --git a/sw/source/core/inc/unofldmid.h b/sw/source/core/inc/unofldmid.h
index 43e30058d470..e6146fe011a5 100644
--- a/sw/source/core/inc/unofldmid.h
+++ b/sw/source/core/inc/unofldmid.h
@@ -47,6 +47,7 @@
 #define FIELD_PROP_IS_FIELD_DISPLAYED   33
 
 #define FIELD_PROP_TEXT             34
+#define FIELD_PROP_TITLE 35
 
 #endif
 
diff --git a/sw/source/core/unocore/unomap.cxx 
b/sw/source/core/unocore/unomap.cxx
index 312b07b68786..df26fa679d6c 100644
--- a/sw/source/core/unocore/unomap.cxx
+++ b/sw/source/core/unocore/unomap.cxx
@@ -67,6 +67,7 @@ using namespace ::com::sun::star::beans;
 #define COMMON_FLDTYP_PROPERTIES \
                     { u"" UNO_NAME_IS_FIELD_USED,      
FIELD_PROP_IS_FIELD_USED,      cppu::UnoType<float>::get(), 
PropertyAttribute::READONLY, 0},\
                     { u"" UNO_NAME_IS_FIELD_DISPLAYED, 
FIELD_PROP_IS_FIELD_DISPLAYED, cppu::UnoType<sal_Int16>::get(), 
PropertyAttribute::READONLY, 0},\
+                    { u"" UNO_NAME_TITLE, FIELD_PROP_TITLE, 
cppu::UnoType<OUString>::get(), PROPERTY_NONE, 0},\
 
 const SfxItemPropertyMapEntry* 
SwUnoPropertyMapProvider::GetPropertyMapEntries(sal_uInt16 nPropertyId)
 {
diff --git a/sw/source/uibase/docvw/edtwin2.cxx 
b/sw/source/uibase/docvw/edtwin2.cxx
index a87ae42d45e3..f1c5b7c37eec 100644
--- a/sw/source/uibase/docvw/edtwin2.cxx
+++ b/sw/source/uibase/docvw/edtwin2.cxx
@@ -321,6 +321,18 @@ void SwEditWin::RequestHelp(const HelpEvent &rEvt)
                             break;
 
                         case SwFieldIds::User:
+                        {
+                            OUString aTitle = pField->GetTitle();
+                            if (!aTitle.isEmpty())
+                            {
+                                sText = aTitle;
+                            }
+                            else
+                            {
+                                sText = pField->GetPar1();
+                            }
+                            break;
+                        }
                         case SwFieldIds::HiddenText:
                             sText = pField->GetPar1();
                             break;

Reply via email to