From 07a1442b2441cc032c05fd9abf5b1a8db6c7e007 Mon Sep 17 00:00:00 2001 From: Miklos Vajna Date: Thu, 7 Apr 2022 09:11:09 +0200 Subject: sw content controls: select the content on click when showing placeholder - teach SwCursorShell::GetContentAtPos() about a new IsAttrAtPos::ContentControl - add a new SwCursorShell::GotoFormatContentControl() to select a content control, and a SwWrtShell::GotoContentControl() wrapper around it - combine these together in SwEditWin::MouseButtonUp() The intention is that when you open a document and you click on a placeholder text like "Click here to enter text", then this content is pre-selected (so typing overwrites it), but typing real content there disables this behavior. Change-Id: Ia539865da7b18c41cbfb398282842bdb2e25f0bc Reviewed-on: https://gerrit.libreoffice.org/c/core/+/132652 Reviewed-by: Miklos Vajna Tested-by: Jenkins --- sw/qa/uibase/wrtsh/wrtsh.cxx | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'sw/qa/uibase') diff --git a/sw/qa/uibase/wrtsh/wrtsh.cxx b/sw/qa/uibase/wrtsh/wrtsh.cxx index 9f63ed2fc7e6..d73ba55ef09f 100644 --- a/sw/qa/uibase/wrtsh/wrtsh.cxx +++ b/sw/qa/uibase/wrtsh/wrtsh.cxx @@ -13,6 +13,7 @@ #include #include +#include #include #include @@ -21,6 +22,8 @@ #include #include #include +#include +#include namespace { @@ -51,6 +54,42 @@ CPPUNIT_TEST_FIXTURE(Test, testInsertLineBreak) auto eClear = getProperty(xLineBreak, "Clear"); CPPUNIT_ASSERT_EQUAL(static_cast(SwLineBreakClear::ALL), eClear); } + +CPPUNIT_TEST_FIXTURE(Test, testGotoContentControl) +{ + // Given a document with a content control: + SwDoc* pDoc = createSwDoc(); + uno::Reference xMSF(mxComponent, uno::UNO_QUERY); + uno::Reference xTextDocument(mxComponent, uno::UNO_QUERY); + uno::Reference xText = xTextDocument->getText(); + uno::Reference xCursor = xText->createTextCursor(); + xText->insertString(xCursor, "test", /*bAbsorb=*/false); + xCursor->gotoStart(/*bExpand=*/false); + xCursor->gotoEnd(/*bExpand=*/true); + uno::Reference xContentControl( + xMSF->createInstance("com.sun.star.text.ContentControl"), uno::UNO_QUERY); + uno::Reference xContentControlProps(xContentControl, uno::UNO_QUERY); + xContentControlProps->setPropertyValue("ShowingPlaceHolder", uno::makeAny(true)); + xText->insertTextContent(xCursor, xContentControl, /*bAbsorb=*/true); + + // When going to that content control in placeholder mode: + SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell(); + SwNodeOffset nIndex = pWrtShell->GetCursor()->GetNode().GetIndex(); + SwTextNode* pTextNode = pDoc->GetNodes()[nIndex]->GetTextNode(); + SwTextAttr* pAttr = pTextNode->GetTextAttrForCharAt(0, RES_TXTATR_CONTENTCONTROL); + auto pTextContentControl = static_txtattr_cast(pAttr); + auto& rFormatContentControl + = static_cast(pTextContentControl->GetAttr()); + pWrtShell->GotoContentControl(rFormatContentControl); + + // Then make sure that the content control is selected (without the dummy character): + // Without the accompanying fix in place, this test would have failed, the user had to manually + // select the placeholder text. + sal_Int32 nStart = pWrtShell->GetCursor()->Start()->nContent.GetIndex(); + CPPUNIT_ASSERT_EQUAL(static_cast(1), nStart); + sal_Int32 nEnd = pWrtShell->GetCursor()->End()->nContent.GetIndex(); + CPPUNIT_ASSERT_EQUAL(static_cast(5), nEnd); +} } CPPUNIT_PLUGIN_IMPLEMENT(); -- cgit