sw/qa/core/txtnode/txtnode.cxx                |   31 ++++++++++++++++++++++
 sw/source/core/txtnode/attrcontentcontrol.cxx |   36 ++++++++++++++++++++++++++
 2 files changed, 67 insertions(+)

New commits:
commit c804c5354855188b5a37219cfe11dc079dc235f4
Author:     Miklos Vajna <vmik...@collabora.com>
AuthorDate: Fri Mar 10 13:36:37 2023 +0100
Commit:     Miklos Vajna <vmik...@collabora.com>
CommitDate: Fri Mar 10 17:32:07 2023 +0000

    sw content control: fix lost properties on copy&paste
    
    E.g. a checkbox content control was turned into a rich text one on
    copy&paste, so toggling didn't work on the copied content control.
    
    Change-Id: Ia7262e0b88dff12a2f87acd7b0e6cc3fbde3c3a8
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/148613
    Reviewed-by: Miklos Vajna <vmik...@collabora.com>
    Tested-by: Jenkins

diff --git a/sw/qa/core/txtnode/txtnode.cxx b/sw/qa/core/txtnode/txtnode.cxx
index 55413b4e4711..e0526bdbb361 100644
--- a/sw/qa/core/txtnode/txtnode.cxx
+++ b/sw/qa/core/txtnode/txtnode.cxx
@@ -30,6 +30,7 @@
 #include <txatbase.hxx>
 #include <ndtxt.hxx>
 #include <textcontentcontrol.hxx>
+#include <swdtflvr.hxx>
 
 /// Covers sw/source/core/txtnode/ fixes.
 class SwCoreTxtnodeTest : public SwModelTestBase
@@ -328,6 +329,36 @@ CPPUNIT_TEST_FIXTURE(SwCoreTxtnodeTest, 
testDateContentControlKeyboard)
     CPPUNIT_ASSERT(bShouldOpen);
 }
 
+CPPUNIT_TEST_FIXTURE(SwCoreTxtnodeTest, testContentControlCopy)
+{
+    // Given a document with a content control:
+    createSwDoc();
+    SwDoc* pDoc = getSwDoc();
+    SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
+    pWrtShell->InsertContentControl(SwContentControlType::CHECKBOX);
+
+    // When copying that content control:
+    pWrtShell->SelAll();
+    rtl::Reference<SwTransferable> xTransfer = new SwTransferable(*pWrtShell);
+    xTransfer->Copy();
+    // Kill the selection, go to the end of the document:
+    pWrtShell->EndOfSection();
+    TransferableDataHelper aHelper(xTransfer);
+    SwTransferable::Paste(*pWrtShell, aHelper);
+
+    // Then make sure that the copy is also a checkbox:
+    SwContentControlManager& rManager = pDoc->GetContentControlManager();
+    CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), rManager.GetCount());
+    const SwFormatContentControl& rFormat1 = 
rManager.Get(0)->GetContentControl();
+    CPPUNIT_ASSERT_EQUAL(SwContentControlType::CHECKBOX, 
rFormat1.GetContentControl()->GetType());
+    const SwFormatContentControl& rFormat2 = 
rManager.Get(1)->GetContentControl();
+    // Without the accompanying fix in place, this test would have failed with:
+    // - Expected: 1 (CHECKBOX)
+    // - Actual  : 0 (RICH_TEXT)
+    // i.e. properties were not copied from the source to the destination 
content control.
+    CPPUNIT_ASSERT_EQUAL(SwContentControlType::CHECKBOX, 
rFormat2.GetContentControl()->GetType());
+}
+
 CPPUNIT_PLUGIN_IMPLEMENT();
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sw/source/core/txtnode/attrcontentcontrol.cxx 
b/sw/source/core/txtnode/attrcontentcontrol.cxx
index 88e10609fd05..c167f8da33e9 100644
--- a/sw/source/core/txtnode/attrcontentcontrol.cxx
+++ b/sw/source/core/txtnode/attrcontentcontrol.cxx
@@ -183,6 +183,42 @@ SwContentControl::SwContentControl(SwFormatContentControl* 
pFormat)
     , m_pFormat(pFormat)
     , m_pTextNode(nullptr)
 {
+    if (!pFormat)
+    {
+        return;
+    }
+
+    const std::shared_ptr<SwContentControl>& pOther = 
pFormat->GetContentControl();
+    if (!pOther)
+    {
+        return;
+    }
+
+    SetShowingPlaceHolder(pOther->m_bShowingPlaceHolder);
+    SetCheckbox(pOther->m_bCheckbox);
+    SetChecked(pOther->m_bChecked);
+    SetCheckedState(pOther->m_aCheckedState);
+    SetUncheckedState(pOther->m_aUncheckedState);
+    SetListItems(pOther->m_aListItems);
+    SetPicture(pOther->m_bPicture);
+    SetDate(pOther->m_bDate);
+    SetDateFormat(pOther->m_aDateFormat);
+    SetDateLanguage(pOther->m_aDateLanguage);
+    SetCurrentDate(pOther->m_aCurrentDate);
+    SetPlainText(pOther->m_bPlainText);
+    SetComboBox(pOther->m_bComboBox);
+    SetDropDown(pOther->m_bDropDown);
+    SetPlaceholderDocPart(pOther->m_aPlaceholderDocPart);
+    SetDataBindingPrefixMappings(pOther->m_aDataBindingPrefixMappings);
+    SetDataBindingXpath(pOther->m_aDataBindingXpath);
+    SetDataBindingStoreItemID(pOther->m_aDataBindingStoreItemID);
+    SetColor(pOther->m_aColor);
+    SetAppearance(pOther->m_aAppearance);
+    SetAlias(pOther->m_aAlias);
+    SetTag(pOther->m_aTag);
+    SetId(pOther->m_nId);
+    SetTabIndex(pOther->m_nTabIndex);
+    SetLock(pOther->m_aLock);
 }
 
 SwContentControl::~SwContentControl() {}

Reply via email to