LibreOffice Module sw (master) 1
atrref.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <fmtrfmrk.hxx>
21
22#include <libxml/xmlwriter.h>
23
24#include <hintids.hxx>
25#include <hints.hxx>
26#include <txtrfmrk.hxx>
27#include <unorefmark.hxx>
28#include <utility>
29#include <sfx2/viewsh.hxx>
30#include <tools/json_writer.hxx>
31#include <LibreOfficeKit/LibreOfficeKitEnums.h>
32#include <comphelper/lok.hxx>
33#include <doc.hxx>
34#include <ndtxt.hxx>
35#include <pam.hxx>
36#include <translatehelper.hxx>
37#include <wrtsh.hxx>
38
40{
41}
42
45 , sw::BroadcastingModify()
46 , m_pTextAttr(nullptr)
47 , m_aRefName(std::move(aName))
48{
49}
50
53 , sw::BroadcastingModify()
54 , m_pTextAttr(nullptr)
55 , m_aRefName(rAttr.m_aRefName)
56{
57}
58
60{ m_wXReferenceMark = xMark.get(); }
61
62bool SwFormatRefMark::operator==( const SfxPoolItem& rAttr ) const
63{
64 assert(SfxPoolItem::operator==(rAttr));
65 return m_aRefName == static_cast<const SwFormatRefMark&>(rAttr).m_aRefName;
66}
67
69{
70 return new SwFormatRefMark( *this );
71}
72
74{
75 if (rHint.GetId() != SfxHintId::SwLegacyModify)
76 return;
77 auto pLegacy = static_cast<const sw::LegacyModifyHint*>(&rHint);
78 CallSwClientNotify(rHint);
79 if(RES_REMOVE_UNO_OBJECT == pLegacy->GetWhich())
80 SetXRefMark(nullptr);
81}
82
84{
86 &static_cast<sw::BroadcastingModify&>(*this)); // cast to base class (void*)
87 CallSwClientNotify(sw::LegacyModifyHint(&item, &item));
88}
89
91{
92 (void)xmlTextWriterStartElement(pWriter, BAD_CAST("SwFormatRefMark"));
93 (void)xmlTextWriterWriteFormatAttribute(pWriter, BAD_CAST("ptr"), "%p", this);
94 (void)xmlTextWriterWriteFormatAttribute(pWriter, BAD_CAST("m_pTextAttr"), "%p", m_pTextAttr);
95 (void)xmlTextWriterWriteAttribute(pWriter, BAD_CAST("ref-name"),
96 BAD_CAST(m_aRefName.toUtf8().getStr()));
98
99
100 (void)xmlTextWriterEndElement(pWriter);
101}
102
103// attribute for content references in the text
104
106 sal_Int32 const nStartPos, sal_Int32 const*const pEnd)
107 : SwTextAttr(rAttr, nStartPos)
108 , SwTextAttrEnd( rAttr, nStartPos, nStartPos )
109 , m_pTextNode( nullptr )
110 , m_pEnd( nullptr )
111{
112 rAttr.m_pTextAttr = this;
113 if ( pEnd )
114 {
115 m_nEnd = *pEnd;
116 m_pEnd = & m_nEnd;
117 }
118 else
119 {
120 SetHasDummyChar(true);
121 }
122 SetDontMoveAttr( true );
123 SetOverlapAllowedAttr( true );
124 SetDontExpand( true ); // like hyperlinks, reference markers shouldn't expand
125 SetLockExpandFlag( true ); // protect the flag
126}
127
129{
130 if (!comphelper::LibreOfficeKit::isActive() || GetTextNode().GetDoc().IsClipBoard())
131 return;
132
133 SfxViewShell* pViewShell = SfxViewShell::Current();
134 if (!pViewShell)
135 return;
136
137 OUString fieldCommand = GetRefMark().GetRefName();
138 tools::JsonWriter aJson;
139 aJson.put("commandName", ".uno:DeleteField");
140 aJson.put("success", true);
141 {
142 auto result = aJson.startNode("result");
143 aJson.put("DeleteField", fieldCommand);
144 }
145
146 pViewShell->libreOfficeKitViewCallback(LOK_CALLBACK_UNO_COMMAND_RESULT, aJson.finishAndGetAsOString());
147}
148
149const sal_Int32* SwTextRefMark::GetEnd() const
150{
151 return m_pEnd;
152}
153
154void SwTextRefMark::SetEnd(sal_Int32 n)
155{
156 *m_pEnd = n;
157 if (m_pHints)
159}
160
161void SwTextRefMark::UpdateFieldContent(SwDoc* pDoc, SwWrtShell& rWrtSh, OUString aContent)
162{
163 if (!this->End())
164 {
165 return;
166 }
167
168 // Insert markers to remember where the paste positions are.
169 const SwTextNode& rTextNode = this->GetTextNode();
170 SwPaM aMarkers(SwPosition(rTextNode, *this->End()));
172 this->SetLockExpandFlag(false);
173 this->SetDontExpand(false);
174 if (rIDCO.InsertString(aMarkers, "XY"))
175 {
176 SwPaM aPasteEnd(SwPosition(rTextNode, *this->End()));
177 aPasteEnd.Move(fnMoveBackward, GoInContent);
178
179 // Paste HTML content.
180 SwPaM* pCursorPos = rWrtSh.GetCursor();
181 *pCursorPos = aPasteEnd;
182 SwTranslateHelper::PasteHTMLToPaM(rWrtSh, pCursorPos, aContent.toUtf8());
183
184 // Update the refmark to point to the new content.
185 sal_Int32 nOldStart = this->GetStart();
186 sal_Int32 nNewStart = *this->End();
187 // First grow it to include text till the end of the paste position.
188 this->SetEnd(aPasteEnd.GetPoint()->GetContentIndex());
189 // Then shrink it to only start at the paste start: we know that the refmark was
190 // truncated to the paste start, as the refmark has to stay inside a single text node
191 this->SetStart(nNewStart);
192 rTextNode.GetSwpHints().SortIfNeedBe();
193 SwPaM aEndMarker(*aPasteEnd.GetPoint());
194 aEndMarker.SetMark();
195 aEndMarker.GetMark()->AdjustContent(1);
196 SwPaM aStartMarker(SwPosition(rTextNode, nOldStart), SwPosition(rTextNode, nNewStart));
197
198 // Remove markers. The start marker includes the old content as well.
199 rIDCO.DeleteAndJoin(aStartMarker);
200 rIDCO.DeleteAndJoin(aEndMarker);
201 }
202 // Restore flags.
203 this->SetDontExpand(true);
204 this->SetLockExpandFlag(true);
205}
206
207
209{
210 (void)xmlTextWriterStartElement(pWriter, BAD_CAST("SwTextRefMark"));
211 (void)xmlTextWriterWriteFormatAttribute(pWriter, BAD_CAST("ptr"), "%p", this);
212 SwTextAttr::dumpAsXml(pWriter);
213
214 (void)xmlTextWriterEndElement(pWriter);
215}
216
217/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Text operation/manipulation interface.
virtual bool DeleteAndJoin(SwPaM &, SwDeleteFlags flags=SwDeleteFlags::Default)=0
complete delete of a given PaM
virtual bool InsertString(const SwPaM &rRg, const OUString &, const SwInsertFlags nInsertMode=SwInsertFlags::EMPTYEXPAND)=0
Insert string into existing text node at position rRg.Point().
SfxHintId GetId() const
virtual void dumpAsXml(xmlTextWriterPtr pWriter) const
virtual void libreOfficeKitViewCallback(int nType, const OString &pPayload) const override
static SAL_WARN_UNUSED_RESULT SfxViewShell * Current()
SwCursor * GetCursor(bool bMakeTableCursor=true) const
Return pointer to the current shell cursor.
Definition: crsrsh.cxx:194
Definition: doc.hxx:197
IDocumentContentOperations const & getIDocumentContentOperations() const
Definition: doc.cxx:329
OUString m_aRefName
Definition: fmtrfmrk.hxx:43
void SetXRefMark(rtl::Reference< SwXReferenceMark > const &xMark)
Definition: atrref.cxx:59
SwFormatRefMark(OUString aText)
Definition: atrref.cxx:43
virtual ~SwFormatRefMark() override
Definition: atrref.cxx:39
virtual void SwClientNotify(const SwModify &, const SfxHint &) override
Definition: atrref.cxx:73
unotools::WeakReference< SwXReferenceMark > m_wXReferenceMark
Definition: fmtrfmrk.hxx:45
virtual SwFormatRefMark * Clone(SfxItemPool *pPool=nullptr) const override
Definition: atrref.cxx:68
SwTextRefMark * m_pTextAttr
Definition: fmtrfmrk.hxx:40
virtual bool operator==(const SfxPoolItem &) const override
"Pure virtual methods" of SfxPoolItem.
Definition: atrref.cxx:62
void InvalidateRefMark()
Definition: atrref.cxx:83
void dumpAsXml(xmlTextWriterPtr pWriter) const override
Definition: atrref.cxx:90
OUString & GetRefName()
Definition: fmtrfmrk.hxx:63
PaM is Point and Mark: a selection of the document model.
Definition: pam.hxx:188
const SwPosition * GetMark() const
Definition: pam.hxx:255
virtual void SetMark()
Unless this is called, the getter method of Mark will return Point.
Definition: pam.cxx:643
bool Move(SwMoveFnCollection const &fnMove=fnMoveForward, SwGoInDoc fnGo=GoInContent)
Movement of cursor.
Definition: pam.cxx:657
const SwPosition * GetPoint() const
Definition: pam.hxx:253
sal_Int32 m_nEnd
Definition: txatbase.hxx:138
A wrapper around SfxPoolItem to store the start position of (usually) a text portion,...
Definition: txatbase.hxx:44
void SetDontMoveAttr(bool bFlag)
Definition: txatbase.hxx:73
const SwFormatRefMark & GetRefMark() const
Definition: txatbase.hxx:238
const sal_Int32 * End() const
Definition: txatbase.hxx:156
virtual void dumpAsXml(xmlTextWriterPtr pWriter) const
Definition: txatbase.cxx:89
void SetOverlapAllowedAttr(bool bFlag)
Definition: txatbase.hxx:75
sal_Int32 GetStart() const
Definition: txatbase.hxx:88
void SetLockExpandFlag(bool bFlag)
Definition: txatbase.hxx:72
void SetStart(sal_Int32 n)
start position
Definition: txatbase.hxx:87
void SetHasDummyChar(const bool bFlag)
Definition: txatbase.hxx:78
void SetDontExpand(bool bDontExpand)
Definition: txatbase.hxx:179
SwpHints * m_pHints
Definition: txatbase.hxx:67
SwTextNode is a paragraph in the document model.
Definition: ndtxt.hxx:112
SwpHints & GetSwpHints()
getters for SwpHints
Definition: ndtxt.hxx:867
void UpdateFieldContent(SwDoc *pDoc, SwWrtShell &rWrtSh, OUString aContent)
Definition: atrref.cxx:161
const SwTextNode & GetTextNode() const
Definition: txtrfmrk.hxx:50
virtual void SetEnd(sal_Int32) override
Definition: atrref.cxx:154
sal_Int32 * m_pEnd
Definition: txtrfmrk.hxx:32
SwTextRefMark(SwFormatRefMark &rAttr, sal_Int32 const nStart, sal_Int32 const *const pEnd=nullptr)
Definition: atrref.cxx:105
void dumpAsXml(xmlTextWriterPtr pWriter) const override
Definition: atrref.cxx:208
virtual const sal_Int32 * GetEnd() const override
end position
Definition: atrref.cxx:149
Used by the UI to modify the document model.
Definition: wrtsh.hxx:97
void SortIfNeedBe() const
Trigger the sorting if necessary.
Definition: ndhints.hxx:176
void EndPosChanged() const
Definition: ndhints.hxx:213
void put(std::u16string_view pPropName, const OUString &rPropValue)
OString finishAndGetAsOString()
ScopedJsonWriterNode startNode(std::string_view)
struct _xmlTextWriter * xmlTextWriterPtr
constexpr TypedWhichId< SwPtrMsgPoolItem > RES_REMOVE_UNO_OBJECT(181)
constexpr TypedWhichId< SwFormatRefMark > RES_TXTATR_REFMARK(RES_TXTATR_WITHEND_BEGIN)
OUString aName
sal_Int64 n
SW_DLLPUBLIC void PasteHTMLToPaM(SwWrtShell &rWrtSh, SwPaM *pCursor, const OString &rData)
Dialog to specify the properties of date form field.
bool GoInContent(SwPaM &rPam, SwMoveFnCollection const &fnMove)
Definition: pam.cxx:1203
SwMoveFnCollection const & fnMoveBackward
Definition: paminit.cxx:60
Marks a position in the document model.
Definition: pam.hxx:38
sal_Int32 GetContentIndex() const
Definition: pam.hxx:85
void AdjustContent(sal_Int32 nDelta)
Adjust content index, only valid to call this if the position points to a SwContentNode subclass.
Definition: pam.cxx:262
Any result