officecfg/registry/data/org/openoffice/Office/UI/WriterCommands.xcu | 10 + sw/inc/cmdid.h | 3 sw/inc/doc.hxx | 1 sw/inc/editsh.hxx | 1 sw/qa/uitest/writer_tests8/tdf45946.py | 24 +++ sw/sdi/_textsh.sdi | 6 sw/sdi/swriter.sdi | 18 ++ sw/source/core/doc/doc.cxx | 76 ++++++++++ sw/source/core/edit/edfld.cxx | 8 + sw/source/uibase/shells/textfld.cxx | 33 ++++ sw/uiconfig/sglobal/popupmenu/table.xml | 1 sw/uiconfig/sglobal/popupmenu/text.xml | 1 sw/uiconfig/swform/popupmenu/table.xml | 1 sw/uiconfig/swform/popupmenu/text.xml | 1 sw/uiconfig/swreport/popupmenu/table.xml | 1 sw/uiconfig/swreport/popupmenu/text.xml | 1 sw/uiconfig/swriter/menubar/menubar.xml | 1 sw/uiconfig/swriter/popupmenu/table.xml | 1 sw/uiconfig/swriter/popupmenu/text.xml | 1 sw/uiconfig/swxform/popupmenu/table.xml | 1 sw/uiconfig/swxform/popupmenu/text.xml | 1 21 files changed, 187 insertions(+), 4 deletions(-)
New commits: commit e846c8e5ed07e6b67a4375dd8ab4d4ca30fd9066 Author: Oliver Specht <oliver.spe...@cib.de> AuthorDate: Tue May 21 14:27:04 2024 +0200 Commit: Thorsten Behrens <thorsten.behr...@allotropia.de> CommitDate: Mon Mar 17 15:39:22 2025 +0100 tdf#45946 Add function to convert field to text The current field can be converted to it's representing text. Exceptions are fields in header or footer that have multiple representations like page number or chapter fields. Change-Id: I62b92b2d0b2ed766a2722ffd804496008b72e792 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/167900 Tested-by: Gabor Kelemen <gabor.kelemen.ext...@allotropia.de> Tested-by: Jenkins Reviewed-by: Thorsten Behrens <thorsten.behr...@allotropia.de> diff --git a/officecfg/registry/data/org/openoffice/Office/UI/WriterCommands.xcu b/officecfg/registry/data/org/openoffice/Office/UI/WriterCommands.xcu index 17a9da6656e3..a3399fd6466a 100644 --- a/officecfg/registry/data/org/openoffice/Office/UI/WriterCommands.xcu +++ b/officecfg/registry/data/org/openoffice/Office/UI/WriterCommands.xcu @@ -1757,7 +1757,15 @@ <value>1</value> </prop> </node> - <node oor:name=".uno:LinkDialog" oor:op="replace"> + <node oor:name=".uno:ConvertSelectedField" oor:op="replace"> + <prop oor:name="Label" oor:type="xs:string"> + <value xml:lang="en-US">Convert Field To Text</value> + </prop> + <prop oor:name="Properties" oor:type="xs:int"> + <value>1</value> + </prop> + </node> + <node oor:name=".uno:LinkDialog" oor:op="replace"> <prop oor:name="Label" oor:type="xs:string"> <value xml:lang="en-US">E~xternal Links...</value> </prop> diff --git a/sw/inc/cmdid.h b/sw/inc/cmdid.h index abfe12fa9e78..9baa9c18f86a 100644 --- a/sw/inc/cmdid.h +++ b/sw/inc/cmdid.h @@ -46,7 +46,7 @@ class SwUINumRuleItem; #define FN_FRAME (SID_SW_START + 1300) #define FN_INSERT2 (SID_SW_START + 1400) #define FN_FORMAT2 (SID_SW_START + 1600) -//#define FN_EDIT2 (SID_SW_START + 1800) defined in svxids.hrc +//#define FN_EDIT2 (SID_SW_START + 1800) defined in sfxsids.hrc #define FN_QUERY2 (SID_SW_START + 2000) #define FN_EXTRA2 (SID_SW_START + 2200) #define FN_PARAM2 (SID_SW_START + 2400) @@ -153,6 +153,7 @@ class SwUINumRuleItem; #define FN_REDLINE_REJECT_TONEXT (FN_EDIT2 + 46) /* Redlining Reject and jump to next*/ #define FN_TRANSFORM_DOCUMENT_STRUCTURE (FN_EDIT2 + 47) /* overwrite text of content control, and more*/ #define FN_COPY_FIELD (FN_EDIT2 + 48) /* show field content in readonly documents to copy content*/ +#define FN_CONVERT_SEL_FIELD (FN_EDIT2 + 49) /* convert selected field to text */ // Region: View #define FN_DRAW_WRAP_DLG TypedWhichId<SfxInt16Item>(FN_VIEW + 3) /* Draw wrapping dlg */ diff --git a/sw/inc/doc.hxx b/sw/inc/doc.hxx index ca8da4e293e3..394195589919 100644 --- a/sw/inc/doc.hxx +++ b/sw/inc/doc.hxx @@ -1508,6 +1508,7 @@ public: // Replace fields by text - mailmerge support bool ConvertFieldsToText(SwRootFrame const& rLayout); + bool ConvertFieldToText(SwField& rField, SwRootFrame const& rLayout); // Create sub-documents according to given collection. // If no collection is given, use chapter styles for 1st level. diff --git a/sw/inc/editsh.hxx b/sw/inc/editsh.hxx index 67e699a4deec..6ad93013acca 100644 --- a/sw/inc/editsh.hxx +++ b/sw/inc/editsh.hxx @@ -406,6 +406,7 @@ public: bool InsertField(SwField const &, const bool bForceExpandHints); SW_DLLPUBLIC void UpdateOneField(SwField &); ///< One single field. + SW_DLLPUBLIC void ConvertOneFieldToText(SwField& rField); SW_DLLPUBLIC size_t GetFieldTypeCount(SwFieldIds nResId = SwFieldIds::Unknown) const; SW_DLLPUBLIC SwFieldType* GetFieldType(size_t nField, SwFieldIds nResId = SwFieldIds::Unknown) const; diff --git a/sw/qa/uitest/writer_tests8/tdf45946.py b/sw/qa/uitest/writer_tests8/tdf45946.py new file mode 100644 index 000000000000..f31a250cbb5e --- /dev/null +++ b/sw/qa/uitest/writer_tests8/tdf45946.py @@ -0,0 +1,24 @@ +# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*- +# +# This file is part of the LibreOffice project. +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +# + +from uitest.framework import UITestCase +from uitest.uihelper.common import get_state_as_dict +from libreoffice.uno.propertyvalue import mkPropertyValues + +class tdf45946(UITestCase): + + def test_tdf45946_convert_selected_field(self): + with self.ui_test.create_doc_in_start_center("writer") as writer_doc: + self.xUITest.executeCommand(".uno:InsertDateField") + self.xUITest.executeCommand(".uno:GoToStartOfLine") + self.assertTrue(writer_doc.getTextFields().createEnumeration().hasMoreElements()) + self.xUITest.executeCommand(".uno:ConvertSelectedField") + self.assertFalse(writer_doc.getTextFields().createEnumeration().hasMoreElements()) + +# vim: set shiftwidth=4 softtabstop=4 expandtab: diff --git a/sw/sdi/_textsh.sdi b/sw/sdi/_textsh.sdi index b385e798ad9b..f7b0c2c9f4bc 100644 --- a/sw/sdi/_textsh.sdi +++ b/sw/sdi/_textsh.sdi @@ -1046,6 +1046,12 @@ interface BaseText StateMethod = StateField ; DisableFlags="SfxDisableFlags::SwOnProtectedCursor"; ] + FN_CONVERT_SEL_FIELD + [ + ExecMethod = ExecField ; + StateMethod = StateField ; + DisableFlags="SfxDisableFlags::SwOnProtectedCursor"; + ] FN_GLOSSARY_DLG // status() [ ExecMethod = ExecGlossary ; diff --git a/sw/sdi/swriter.sdi b/sw/sdi/swriter.sdi index 2268643f4575..6fd96a160846 100644 --- a/sw/sdi/swriter.sdi +++ b/sw/sdi/swriter.sdi @@ -1265,6 +1265,24 @@ SfxVoidItem UpdateSelectedField FN_UPDATE_SEL_FIELD GroupId = SfxGroupId::Edit; ] +SfxVoidItem ConvertSelectedField FN_CONVERT_SEL_FIELD +() +[ + AutoUpdate = FALSE, + FastCall = FALSE, + ReadOnlyDoc = FALSE, + Toggle = FALSE, + Container = FALSE, + RecordAbsolute = FALSE, + RecordPerSet; + Asynchron; + + AccelConfig = TRUE, + MenuConfig = TRUE, + ToolBoxConfig = TRUE, + GroupId = SfxGroupId::Edit; +] + SfxBoolItem Fieldnames FN_VIEW_FIELDNAME [ diff --git a/sw/source/core/doc/doc.cxx b/sw/source/core/doc/doc.cxx index 97ef7e27ed10..dbfe2511dda3 100644 --- a/sw/source/core/doc/doc.cxx +++ b/sw/source/core/doc/doc.cxx @@ -1998,6 +1998,82 @@ static bool IsMailMergeField(SwFieldIds fieldId) } } +bool SwDoc::ConvertFieldToText(SwField& rField, SwRootFrame const& rLayout) +{ + bool bRet = false; + getIDocumentFieldsAccess().LockExpFields(); + GetIDocumentUndoRedo().StartUndo( SwUndoId::UI_REPLACE, nullptr ); + + SwFieldType* pFieldType = rField.GetTyp(); + SwFormatField* pFormatField = pFieldType->FindFormatForField(&rField); + const SwTextField *pTextField = pFormatField ? pFormatField->GetTextField() : nullptr; + if (pTextField) + { + bool bInHeaderFooter = IsInHeaderFooter(*pTextField->GetpTextNode()); + const SwFormatField& rFormatField = pTextField->GetFormatField(); + const SwField* pField = rFormatField.GetField(); + + //#i55595# some fields have to be excluded in headers/footers + SwFieldIds nWhich = pField->GetTyp()->Which(); + if(!bInHeaderFooter || + (nWhich != SwFieldIds::PageNumber && + nWhich != SwFieldIds::Chapter && + nWhich != SwFieldIds::GetExp&& + nWhich != SwFieldIds::SetExp&& + nWhich != SwFieldIds::Input&& + nWhich != SwFieldIds::RefPageGet&& + nWhich != SwFieldIds::RefPageSet)) + { + OUString sText = pField->ExpandField(true, &rLayout); + + // database fields should not convert their command into text + if( SwFieldIds::Database == pFieldType->Which() && !static_cast<const SwDBField*>(pField)->IsInitialized()) + sText.clear(); + + SwPaM aInsertPam(*pTextField->GetpTextNode(), pTextField->GetStart()); + aInsertPam.SetMark(); + + // go to the end of the field + const SwTextField *pFieldAtEnd = sw::DocumentFieldsManager::GetTextFieldAtPos(*aInsertPam.End()); + if (pFieldAtEnd && pFieldAtEnd->Which() == RES_TXTATR_INPUTFIELD) + { + SwPosition &rEndPos = *aInsertPam.GetPoint(); + rEndPos.SetContent( SwCursorShell::EndOfInputFieldAtPos( *aInsertPam.End() ) ); + } + else + { + aInsertPam.Move(); + } + + // first insert the text after field to keep the field's attributes, + // then delete the field + if (!sText.isEmpty()) + { + // to keep the position after insert + SwPaM aDelPam( *aInsertPam.GetMark(), *aInsertPam.GetPoint() ); + aDelPam.Move( fnMoveBackward ); + aInsertPam.DeleteMark(); + + getIDocumentContentOperations().InsertString( aInsertPam, sText ); + + aDelPam.Move(); + // finally remove the field + getIDocumentContentOperations().DeleteAndJoin( aDelPam ); + } + else + { + getIDocumentContentOperations().DeleteAndJoin( aInsertPam ); + } + + bRet = true; + } + } + if( bRet ) + getIDocumentState().SetModified(); + GetIDocumentUndoRedo().EndUndo( SwUndoId::UI_REPLACE, nullptr ); + getIDocumentFieldsAccess().UnlockExpFields(); + return bRet; +} bool SwDoc::ConvertFieldsToText(SwRootFrame const& rLayout) { bool bRet = false; diff --git a/sw/source/core/edit/edfld.cxx b/sw/source/core/edit/edfld.cxx index e9be8f003abd..e27caa9fad0b 100644 --- a/sw/source/core/edit/edfld.cxx +++ b/sw/source/core/edit/edfld.cxx @@ -291,6 +291,14 @@ void SwEditShell::UpdateOneField(SwField &rField) EndAllAction(); } +void SwEditShell::ConvertOneFieldToText(SwField& rField) +{ + CurrShell aCurr( this ); + StartAllAction(); + GetDoc()->ConvertFieldToText(rField, *GetLayout()); + EndAllAction(); +} + SwDBData const & SwEditShell::GetDBData() const { return GetDoc()->GetDBData(); diff --git a/sw/source/uibase/shells/textfld.cxx b/sw/source/uibase/shells/textfld.cxx index 4a9a11eba7f2..968bceac3c2d 100644 --- a/sw/source/uibase/shells/textfld.cxx +++ b/sw/source/uibase/shells/textfld.cxx @@ -229,6 +229,15 @@ void SwTextShell::ExecField(SfxRequest &rReq) } break; } + case FN_CONVERT_SEL_FIELD: + { + SwField* pField = rSh.GetCurField(); + if (pField) + { + rSh.ConvertOneFieldToText(*pField); + } + break; + } case FN_EXECUTE_MACROFIELD: { SwField* pField = rSh.GetCurField(); @@ -1631,11 +1640,31 @@ void SwTextShell::StateField( SfxItemSet &rSet ) case FN_UPDATE_SEL_FIELD: { pField = rSh.GetCurField(); - if (!pField) rSet.DisableItem( nWhich ); } - + break; + case FN_CONVERT_SEL_FIELD: + { + pField = rSh.GetCurField(); + SwFieldIds eFieldIds = pField ? pField->GetTyp()->Which() : SwFieldIds::Unknown; + bool bInHeaderFooter = rSh.IsInHeaderFooter(); + if (!pField || + eFieldIds == SwFieldIds::Postit || + eFieldIds == SwFieldIds::SetRef || + eFieldIds == SwFieldIds::SetExp || + eFieldIds == SwFieldIds::RefPageSet|| + eFieldIds == SwFieldIds::Input || + eFieldIds == SwFieldIds::JumpEdit || + (bInHeaderFooter && + (eFieldIds == SwFieldIds::PageNumber || + eFieldIds == SwFieldIds::Chapter || + eFieldIds == SwFieldIds::GetExp || + eFieldIds == SwFieldIds::RefPageGet || + eFieldIds == SwFieldIds::GetRef + ))) + rSet.DisableItem( nWhich ); + } break; case FN_EXECUTE_MACROFIELD: diff --git a/sw/uiconfig/sglobal/popupmenu/table.xml b/sw/uiconfig/sglobal/popupmenu/table.xml index 05b67ac1d72b..aee5e19d5c1d 100644 --- a/sw/uiconfig/sglobal/popupmenu/table.xml +++ b/sw/uiconfig/sglobal/popupmenu/table.xml @@ -124,6 +124,7 @@ <menu:menuitem menu:id=".uno:InsertFormula"/> <menu:menuitem menu:id=".uno:InsertAnnotation"/> <menu:menuitem menu:id=".uno:FieldDialog"/> + <menu:menuitem menu:id=".uno:ConvertSelectedField"/> <menu:menuitem menu:id=".uno:AcceptTrackedChange"/> <menu:menuitem menu:id=".uno:RejectTrackedChange"/> <menu:menuitem menu:id=".uno:NextTrackedChange"/> diff --git a/sw/uiconfig/sglobal/popupmenu/text.xml b/sw/uiconfig/sglobal/popupmenu/text.xml index 209510f5fe6c..a9046d728686 100644 --- a/sw/uiconfig/sglobal/popupmenu/text.xml +++ b/sw/uiconfig/sglobal/popupmenu/text.xml @@ -23,6 +23,7 @@ <menu:menuitem menu:id=".uno:RemoveTableOf"/> <menu:menuseparator/> <menu:menuitem menu:id=".uno:FieldDialog"/> + <menu:menuitem menu:id=".uno:ConvertSelectedField"/> <menu:menuitem menu:id=".uno:ContentControlProperties"/> <menu:menuitem menu:id=".uno:DeleteContentControl"/> <menu:menuitem menu:id=".uno:EditFootnote"/> diff --git a/sw/uiconfig/swform/popupmenu/table.xml b/sw/uiconfig/swform/popupmenu/table.xml index 05b67ac1d72b..aee5e19d5c1d 100644 --- a/sw/uiconfig/swform/popupmenu/table.xml +++ b/sw/uiconfig/swform/popupmenu/table.xml @@ -124,6 +124,7 @@ <menu:menuitem menu:id=".uno:InsertFormula"/> <menu:menuitem menu:id=".uno:InsertAnnotation"/> <menu:menuitem menu:id=".uno:FieldDialog"/> + <menu:menuitem menu:id=".uno:ConvertSelectedField"/> <menu:menuitem menu:id=".uno:AcceptTrackedChange"/> <menu:menuitem menu:id=".uno:RejectTrackedChange"/> <menu:menuitem menu:id=".uno:NextTrackedChange"/> diff --git a/sw/uiconfig/swform/popupmenu/text.xml b/sw/uiconfig/swform/popupmenu/text.xml index 209510f5fe6c..a9046d728686 100644 --- a/sw/uiconfig/swform/popupmenu/text.xml +++ b/sw/uiconfig/swform/popupmenu/text.xml @@ -23,6 +23,7 @@ <menu:menuitem menu:id=".uno:RemoveTableOf"/> <menu:menuseparator/> <menu:menuitem menu:id=".uno:FieldDialog"/> + <menu:menuitem menu:id=".uno:ConvertSelectedField"/> <menu:menuitem menu:id=".uno:ContentControlProperties"/> <menu:menuitem menu:id=".uno:DeleteContentControl"/> <menu:menuitem menu:id=".uno:EditFootnote"/> diff --git a/sw/uiconfig/swreport/popupmenu/table.xml b/sw/uiconfig/swreport/popupmenu/table.xml index f6734784cf4d..11045d07129d 100644 --- a/sw/uiconfig/swreport/popupmenu/table.xml +++ b/sw/uiconfig/swreport/popupmenu/table.xml @@ -71,6 +71,7 @@ <menu:menuitem menu:id=".uno:OutlineBullet"/> <menu:menuseparator/> <menu:menuitem menu:id=".uno:FieldDialog"/> + <menu:menuitem menu:id=".uno:ConvertSelectedField"/> <menu:menuitem menu:id=".uno:EditFootnote"/> <menu:menuitem menu:id=".uno:IndexEntryDialog"/> <menu:menuitem menu:id=".uno:AuthoritiesEntryDialog"/> diff --git a/sw/uiconfig/swreport/popupmenu/text.xml b/sw/uiconfig/swreport/popupmenu/text.xml index e99e2653a3a1..1f4f5f040a2f 100644 --- a/sw/uiconfig/swreport/popupmenu/text.xml +++ b/sw/uiconfig/swreport/popupmenu/text.xml @@ -28,6 +28,7 @@ <menu:menuitem menu:id=".uno:RemoveTableOf"/> <menu:menuseparator/> <menu:menuitem menu:id=".uno:FieldDialog"/> + <menu:menuitem menu:id=".uno:ConvertSelectedField"/> <menu:menuitem menu:id=".uno:ContentControlProperties"/> <menu:menuitem menu:id=".uno:DeleteContentControl"/> <menu:menuitem menu:id=".uno:EditFootnote"/> diff --git a/sw/uiconfig/swriter/menubar/menubar.xml b/sw/uiconfig/swriter/menubar/menubar.xml index 7bd02bc1bc5f..c6c0c5a0e292 100644 --- a/sw/uiconfig/swriter/menubar/menubar.xml +++ b/sw/uiconfig/swriter/menubar/menubar.xml @@ -791,6 +791,7 @@ <menu:menuseparator/> <menu:menuitem menu:id=".uno:Repaginate"/> <menu:menuitem menu:id=".uno:UpdateSelectedField"/> + <menu:menuitem menu:id=".uno:ConvertSelectedField"/> <menu:menuitem menu:id=".uno:UpdateFields"/> <menu:menuitem menu:id=".uno:UpdateAllIndexes"/> <menu:menuitem menu:id=".uno:UpdateCurIndex"/> diff --git a/sw/uiconfig/swriter/popupmenu/table.xml b/sw/uiconfig/swriter/popupmenu/table.xml index c40c3f7ad193..296e6e388e2b 100644 --- a/sw/uiconfig/swriter/popupmenu/table.xml +++ b/sw/uiconfig/swriter/popupmenu/table.xml @@ -129,6 +129,7 @@ <menu:menuitem menu:id=".uno:InsertFormula"/> <menu:menuitem menu:id=".uno:InsertAnnotation"/> <menu:menuitem menu:id=".uno:UpdateSelectedField"/> + <menu:menuitem menu:id=".uno:ConvertSelectedField"/> <menu:menuitem menu:id=".uno:FieldDialog"/> <menu:menuitem menu:id=".uno:AcceptTrackedChange"/> <menu:menuitem menu:id=".uno:RejectTrackedChange"/> diff --git a/sw/uiconfig/swriter/popupmenu/text.xml b/sw/uiconfig/swriter/popupmenu/text.xml index e9a559e43db4..3f97e7491223 100644 --- a/sw/uiconfig/swriter/popupmenu/text.xml +++ b/sw/uiconfig/swriter/popupmenu/text.xml @@ -25,6 +25,7 @@ <menu:menuitem menu:id=".uno:RemoveTableOf"/> <menu:menuseparator/> <menu:menuitem menu:id=".uno:UpdateSelectedField"/> + <menu:menuitem menu:id=".uno:ConvertSelectedField"/> <menu:menuitem menu:id=".uno:FieldDialog"/> <menu:menuitem menu:id=".uno:ContentControlProperties"/> <menu:menuitem menu:id=".uno:DeleteContentControl"/> diff --git a/sw/uiconfig/swxform/popupmenu/table.xml b/sw/uiconfig/swxform/popupmenu/table.xml index 05b67ac1d72b..aee5e19d5c1d 100644 --- a/sw/uiconfig/swxform/popupmenu/table.xml +++ b/sw/uiconfig/swxform/popupmenu/table.xml @@ -124,6 +124,7 @@ <menu:menuitem menu:id=".uno:InsertFormula"/> <menu:menuitem menu:id=".uno:InsertAnnotation"/> <menu:menuitem menu:id=".uno:FieldDialog"/> + <menu:menuitem menu:id=".uno:ConvertSelectedField"/> <menu:menuitem menu:id=".uno:AcceptTrackedChange"/> <menu:menuitem menu:id=".uno:RejectTrackedChange"/> <menu:menuitem menu:id=".uno:NextTrackedChange"/> diff --git a/sw/uiconfig/swxform/popupmenu/text.xml b/sw/uiconfig/swxform/popupmenu/text.xml index 004b2f3717f2..b7a3a75d9c14 100644 --- a/sw/uiconfig/swxform/popupmenu/text.xml +++ b/sw/uiconfig/swxform/popupmenu/text.xml @@ -23,6 +23,7 @@ <menu:menuitem menu:id=".uno:RemoveTableOf"/> <menu:menuseparator/> <menu:menuitem menu:id=".uno:FieldDialog"/> + <menu:menuitem menu:id=".uno:ConvertSelectedField"/> <menu:menuitem menu:id=".uno:ContentControlProperties"/> <menu:menuitem menu:id=".uno:DeleteContentControl"/> <menu:menuitem menu:id=".uno:EditFootnote"/>