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 7744b464ed0efe66b65bf8eb5c6fdf6cd5bc867b
Author:     Miklos Vajna <vmik...@collabora.com>
AuthorDate: Thu Mar 24 15:18:28 2022 +0100
Commit:     Miklos Vajna <vmik...@collabora.com>
CommitDate: Fri Mar 25 11:00:32 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.
    
    (cherry picked from commit 1acf8e3cfaf1ef92008e39514a32ace0d036e552)
    
    Conflicts:
            sw/inc/fldbas.hxx
            sw/qa/core/unocore/unocore.cxx
            sw/source/core/fields/fldbas.cxx
            sw/source/core/unocore/unomap.cxx
    
    Change-Id: I986792f5e55e0b96489347be482d640d155113cc

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 4f275e792fb9..08169564b0f9 100644
--- a/sw/inc/fldbas.hxx
+++ b/sw/inc/fldbas.hxx
@@ -299,6 +299,8 @@ private:
     bool                m_bIsAutomaticLanguage;
     sal_uInt32          m_nFormat;              /// this can be either 
SvxNumType or SwChapterFormat depending on the subtype
     SwFieldType*        m_pType;
+    /// 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;
@@ -391,6 +393,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 e5df225828ae..d86e28fcc170 100644
--- a/sw/qa/core/unocore/unocore.cxx
+++ b/sw/qa/core/unocore/unocore.cxx
@@ -28,6 +28,7 @@
 #include <com/sun/star/style/LineSpacing.hpp>
 #include <com/sun/star/view/XSelectionSupplier.hpp>
 #include <com/sun/star/text/XTextViewCursorSupplier.hpp>
+#include <com/sun/star/text/XDependentTextField.hpp>
 
 #include <comphelper/propertyvalue.hxx>
 #include <toolkit/helper/vclunohelper.hxx>
@@ -165,6 +166,34 @@ CPPUNIT_TEST_FIXTURE(SwCoreUnocoreTest, 
testBrokenEmbeddedObject)
         
xEmbeddedObject->supportsService("com.sun.star.comp.embed.OCommonEmbeddedObject"));
 }
 
+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 382880ecd8f4..5ba77adbc56b 100644
--- a/sw/source/core/fields/fldbas.cxx
+++ b/sw/source/core/fields/fldbas.cxx
@@ -336,6 +336,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);
     }
@@ -353,6 +358,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);
     }
@@ -817,6 +831,7 @@ void SwField::dumpAsXml(xmlTextWriterPtr pWriter) const
     xmlTextWriterWriteFormatAttribute(pWriter, BAD_CAST("ptr"), "%p", this);
     xmlTextWriterWriteAttribute(pWriter, BAD_CAST("m_nFormat"), 
BAD_CAST(OString::number(m_nFormat).getStr()));
     xmlTextWriterWriteAttribute(pWriter, BAD_CAST("m_nLang"), 
BAD_CAST(OString::number(m_nLang.get()).getStr()));
+    xmlTextWriterWriteAttribute(pWriter, BAD_CAST("m_aTitle"), 
BAD_CAST(m_aTitle.toUtf8().getStr()));
 
     xmlTextWriterEndElement(pWriter);
 }
diff --git a/sw/source/core/fields/usrfld.cxx b/sw/source/core/fields/usrfld.cxx
index 642f47c7f156..37fb6391c734 100644
--- a/sw/source/core/fields/usrfld.cxx
+++ b/sw/source/core/fields/usrfld.cxx
@@ -68,6 +68,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 2bb13a66faa3..f215e448dde0 100644
--- a/sw/source/core/inc/unofldmid.h
+++ b/sw/source/core/inc/unofldmid.h
@@ -46,6 +46,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 8da991f62451..7318be4e3e5a 100644
--- a/sw/source/core/unocore/unomap.cxx
+++ b/sw/source/core/unocore/unomap.cxx
@@ -68,6 +68,7 @@ using namespace ::com::sun::star::beans;
 #define COMMON_FLDTYP_PROPERTIES \
                     { OUString(UNO_NAME_IS_FIELD_USED),      
FIELD_PROP_IS_FIELD_USED,      cppu::UnoType<float>::get(), 
PropertyAttribute::READONLY, 0},\
                     { OUString(UNO_NAME_IS_FIELD_DISPLAYED), 
FIELD_PROP_IS_FIELD_DISPLAYED, cppu::UnoType<sal_Int16>::get(), 
PropertyAttribute::READONLY, 0},\
+                    { OUString(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 c023047a5560..493aff4441c3 100644
--- a/sw/source/uibase/docvw/edtwin2.cxx
+++ b/sw/source/uibase/docvw/edtwin2.cxx
@@ -301,6 +301,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