LibreOffice Module sw (master) 1
ddefld.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 <sal/config.h>
21
22#include <o3tl/any.hxx>
23#include <osl/diagnose.h>
24#include <osl/thread.h>
25#include <rtl/ustrbuf.hxx>
26#include <sfx2/linkmgr.hxx>
27#include <sot/exchange.hxx>
28#include <doc.hxx>
30#include <IDocumentState.hxx>
32#include <editsh.hxx>
33#include <fmtfld.hxx>
34#include <ddefld.hxx>
35#include <swddetbl.hxx>
36#include <swbaslnk.hxx>
37#include <unofldmid.h>
38#include <hints.hxx>
39#include <utility>
40
41using namespace ::com::sun::star;
42
43namespace {
44
45class SwIntrnlRefLink : public SwBaseLink
46{
47 SwDDEFieldType& m_rFieldType;
48
49public:
50 SwIntrnlRefLink(SwDDEFieldType& rType, SfxLinkUpdateMode nUpdateType)
52 , m_rFieldType(rType)
53 {}
54
55 virtual void Closed() override;
56 virtual ::sfx2::SvBaseLink::UpdateResult DataChanged(
57 const OUString& rMimeType, const css::uno::Any & rValue ) override;
58
59 virtual const SwNode* GetAnchor() const override;
60 virtual bool IsInRange( SwNodeOffset nSttNd, SwNodeOffset nEndNd ) const override;
61};
62
63}
64
65::sfx2::SvBaseLink::UpdateResult SwIntrnlRefLink::DataChanged( const OUString& rMimeType,
66 const uno::Any & rValue )
67{
68 switch( SotExchange::GetFormatIdFromMimeType( rMimeType ) )
69 {
70 case SotClipboardFormatId::STRING:
71 if( !IsNoDataFlag() )
72 {
73 OUString sStr;
74 if (!(rValue >>= sStr))
75 {
76 uno::Sequence< sal_Int8 > aSeq;
77 rValue >>= aSeq;
78 sStr = OUString(reinterpret_cast<char const*>(aSeq.getConstArray()), aSeq.getLength(), osl_getThreadTextEncoding());
79 }
80
81 // remove not needed CR-LF at the end
82 sal_Int32 n = sStr.getLength();
83 while( n && 0 == sStr[ n-1 ] )
84 --n;
85 if( n && 0x0a == sStr[ n-1 ] )
86 --n;
87 if( n && 0x0d == sStr[ n-1 ] )
88 --n;
89
90 bool bDel = n != sStr.getLength();
91 if( bDel )
92 sStr = sStr.copy( 0, n );
93
94 m_rFieldType.SetExpansion(sStr);
95 // set Expansion first! (otherwise this flag will be deleted)
96 m_rFieldType.SetCRLFDelFlag(bDel);
97 }
98 break;
99
100 // other formats
101 default:
102 return SUCCESS;
103 }
104
105 if(!ChkNoDataFlag())
106 m_rFieldType.UpdateDDE();
107
108 return SUCCESS;
109}
110
111void SwIntrnlRefLink::Closed()
112{
113 if (m_rFieldType.GetDoc() && !m_rFieldType.GetDoc()->IsInDtor())
114 {
115 // advise goes, convert all fields into text?
117 if (SwEditShell* pESh = m_rFieldType.GetDoc()->GetEditShell())
118 {
119 pESh->StartAllAction();
120 pESh->FieldToText(&m_rFieldType);
121 pESh->EndAllAction();
122 }
123 else
124 {
125 pSh->StartAction();
126 // to call at the doc ??
127 pSh->EndAction();
128 }
129 }
130 SvBaseLink::Closed();
131}
132
134
135const SwNode* SwIntrnlRefLink::GetAnchor() const
136{
137 // here, any anchor of the normal NodesArray should be sufficient
138 const SwNode* pNd = nullptr;
139 m_rFieldType.CallSwClientNotify(
140 sw::LinkAnchorSearchHint(m_rFieldType.GetDoc()->GetNodes(), pNd));
141 return pNd;
142}
143
144bool SwIntrnlRefLink::IsInRange( SwNodeOffset nSttNd, SwNodeOffset nEndNd ) const
145{
146 bool bInRange = false;
147 m_rFieldType.CallSwClientNotify(sw::InRangeSearchHint(nSttNd, nEndNd, bInRange));
148 return bInRange;
149}
150
152 const OUString& rCmd, SfxLinkUpdateMode nUpdateType )
154 m_aName( std::move(aName) ), m_pDoc( nullptr ), m_nRefCount( 0 )
155{
156 m_bCRLFFlag = m_bDeleted = false;
157 m_RefLink = new SwIntrnlRefLink( *this, nUpdateType );
158 SetCmd( rCmd );
159}
160
162{
163 if( m_pDoc && !m_pDoc->IsInDtor() )
165 m_RefLink->Disconnect();
166}
167
168std::unique_ptr<SwFieldType> SwDDEFieldType::Copy() const
169{
170 std::unique_ptr<SwDDEFieldType> pType(new SwDDEFieldType( m_aName, GetCmd(), GetType() ));
171 pType->m_aExpansion = m_aExpansion;
172 pType->m_bCRLFFlag = m_bCRLFFlag;
173 pType->m_bDeleted = m_bDeleted;
174 pType->SetDoc( m_pDoc );
175 return pType;
176}
177
179{
180 return m_aName;
181}
182
183void SwDDEFieldType::SetCmd( const OUString& _aStr )
184{
185 OUString aStr = _aStr;
186 sal_Int32 nIndex = 0;
187 do
188 {
189 aStr = aStr.replaceFirst(" ", " ", &nIndex);
190 } while (nIndex>=0);
191 m_RefLink->SetLinkSourceName( aStr );
192}
193
194OUString const & SwDDEFieldType::GetCmd() const
195{
196 return m_RefLink->GetLinkSourceName();
197}
198
200{
201 if( pNewDoc == m_pDoc )
202 return;
203
204 if( m_pDoc && m_RefLink.is() )
205 {
206 OSL_ENSURE( !m_nRefCount, "How do we get the references?" );
208 }
209
210 m_pDoc = pNewDoc;
211 if( m_pDoc && m_nRefCount )
212 {
215 }
216}
217
219{
220 if( m_nRefCount )
221 {
225 m_RefLink->Update();
226 }
227 else
228 {
229 Disconnect();
231 }
232}
233
234void SwDDEFieldType::QueryValue( uno::Any& rVal, sal_uInt16 nWhichId ) const
235{
236 sal_Int32 nPart = -1;
237 switch( nWhichId )
238 {
239 case FIELD_PROP_PAR2: nPart = 2; break;
240 case FIELD_PROP_PAR4: nPart = 1; break;
241 case FIELD_PROP_SUBTYPE: nPart = 0; break;
242 case FIELD_PROP_BOOL1:
243 rVal <<= GetType() == SfxLinkUpdateMode::ALWAYS;
244 break;
245 case FIELD_PROP_PAR5:
246 rVal <<= m_aExpansion;
247 break;
248 default:
249 assert(false);
250 }
251 if ( nPart>=0 )
252 rVal <<= GetCmd().getToken(nPart, sfx2::cTokenSeparator);
253}
254
255void SwDDEFieldType::PutValue( const uno::Any& rVal, sal_uInt16 nWhichId )
256{
257 sal_Int32 nPart = -1;
258 switch( nWhichId )
259 {
260 case FIELD_PROP_PAR2: nPart = 2; break;
261 case FIELD_PROP_PAR4: nPart = 1; break;
262 case FIELD_PROP_SUBTYPE: nPart = 0; break;
263 case FIELD_PROP_BOOL1:
264 SetType( *o3tl::doAccess<bool>(rVal) ?
265 SfxLinkUpdateMode::ALWAYS :
266 SfxLinkUpdateMode::ONCALL );
267 break;
268 case FIELD_PROP_PAR5:
269 rVal >>= m_aExpansion;
270 break;
271 default:
272 assert(false);
273 }
274 if( nPart<0 )
275 return;
276
277 const OUString sOldCmd( GetCmd() );
278 OUStringBuffer sNewCmd;
279 sal_Int32 nIndex = 0;
280 for (sal_Int32 i=0; i<3; ++i)
281 {
282 OUString sToken = sOldCmd.getToken(0, sfx2::cTokenSeparator, nIndex);
283 if (i==nPart)
284 {
285 rVal >>= sToken;
286 }
287 sNewCmd.append((i < 2)
288 ? sToken + OUStringChar(sfx2::cTokenSeparator) : sToken);
289 }
290 SetCmd( sNewCmd.makeStringAndClear() );
291}
292
293void SwDDEFieldType::UpdateDDE(const bool bNotifyShells)
294{
295 auto pDoc = GetDoc();
296 assert(pDoc);
297 if(IsModifyLocked())
298 return;
299 SwViewShell* pSh = bNotifyShells ? pDoc->getIDocumentLayoutAccess().GetCurrentViewShell() : nullptr;
300 SwEditShell* pESh = bNotifyShells ? pDoc->GetEditShell() : nullptr;
301
302 // Search for fields. If no valid found, disconnect.
303 LockModify();
304
305 std::vector<SwFormatField*> vFields;
306 std::vector<SwDDETable*> vTables;
307 GatherFields(vFields, false);
308 GatherDdeTables(vTables);
309 const bool bDoAction = vFields.size() || vTables.size();
310 if(bDoAction)
311 {
312 if(pESh)
313 pESh->StartAllAction();
314 else if(pSh)
315 pSh->StartAction();
316 }
317
318 // a DDE tables in the text
319 for(auto pTable: vTables)
320 pTable->ChangeContent();
321
322 UnlockModify();
323
324 if(bDoAction)
325 {
326 if(pESh)
327 pESh->EndAllAction();
328 else if(pSh)
329 pSh->EndAction();
330
331 if(pSh)
333 }
334}
335
337 : SwField(pInitType)
338{
339}
340
342{
343 if( GetTyp()->HasOnlyOneListener() )
344 static_cast<SwDDEFieldType*>(GetTyp())->Disconnect();
345}
346
347OUString SwDDEField::ExpandImpl(SwRootFrame const*const) const
348{
349 OUString aStr = static_cast<SwDDEFieldType*>(GetTyp())->GetExpansion();
350 aStr = aStr.replaceAll("\r", "");
351 aStr = aStr.replaceAll("\t", " ");
352 aStr = aStr.replaceAll("\n", "|");
353 if (aStr.endsWith("|"))
354 {
355 return aStr.copy(0, aStr.getLength()-1);
356 }
357 return aStr;
358}
359
360std::unique_ptr<SwField> SwDDEField::Copy() const
361{
362 return std::make_unique<SwDDEField>(static_cast<SwDDEFieldType*>(GetTyp()));
363}
364
366OUString SwDDEField::GetPar1() const
367{
368 return static_cast<const SwDDEFieldType*>(GetTyp())->GetName();
369}
370
372OUString SwDDEField::GetPar2() const
373{
374 return static_cast<const SwDDEFieldType*>(GetTyp())->GetCmd();
375}
376
378void SwDDEField::SetPar2(const OUString& rStr)
379{
380 static_cast<SwDDEFieldType*>(GetTyp())->SetCmd(rStr);
381}
382
383/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual const SwViewShell * GetCurrentViewShell() const =0
Returns the layout set at the document.
virtual sfx2::LinkManager & GetLinkManager()=0
virtual bool IsVisibleLinks() const =0
Insert links in-/visibly into LinkManager (linked ranges).
virtual void SetModified()=0
Must be called manually at changes of format.
static SotClipboardFormatId GetFormatIdFromMimeType(std::u16string_view rMimeType)
void UpdateDDE(const bool bNotifyShells=true)
Definition: ddefld.cxx:293
virtual std::unique_ptr< SwFieldType > Copy() const override
Definition: ddefld.cxx:168
OUString const & GetCmd() const
Definition: ddefld.cxx:194
SfxLinkUpdateMode GetType() const
Definition: ddefld.hxx:82
OUString m_aName
Definition: ddefld.hxx:52
void Disconnect()
Definition: ddefld.hxx:88
void SetDoc(SwDoc *pDoc)
Definition: ddefld.cxx:199
OUString m_aExpansion
Definition: ddefld.hxx:53
sal_uInt16 m_nRefCount
Definition: ddefld.hxx:58
SAL_DLLPRIVATE void RefCntChgd()
Definition: ddefld.cxx:218
void SetCmd(const OUString &aStr)
Definition: ddefld.cxx:183
bool m_bCRLFFlag
Definition: ddefld.hxx:59
virtual ~SwDDEFieldType() override
Definition: ddefld.cxx:161
bool m_bDeleted
Definition: ddefld.hxx:60
virtual void QueryValue(css::uno::Any &rVal, sal_uInt16 nWhich) const override
Definition: ddefld.cxx:234
tools::SvRef< sfx2::SvBaseLink > m_RefLink
Definition: ddefld.hxx:55
SwDoc * m_pDoc
Definition: ddefld.hxx:56
void SetType(SfxLinkUpdateMode nType)
Definition: ddefld.hxx:83
SwDDEFieldType(OUString aName, const OUString &rCmd, SfxLinkUpdateMode)
Definition: ddefld.cxx:151
virtual OUString GetName() const override
Only in derived classes.
Definition: ddefld.cxx:178
const SwDoc * GetDoc() const
Definition: ddefld.hxx:93
virtual void PutValue(const css::uno::Any &rVal, sal_uInt16 nWhich) override
Definition: ddefld.cxx:255
virtual OUString GetPar1() const override
Get parameter via types.
Definition: ddefld.cxx:366
virtual ~SwDDEField() override
Definition: ddefld.cxx:341
virtual void SetPar2(const OUString &rStr) override
set field type command
Definition: ddefld.cxx:378
SwDDEField(SwDDEFieldType *)
Definition: ddefld.cxx:336
virtual std::unique_ptr< SwField > Copy() const override
Definition: ddefld.cxx:360
virtual OUString ExpandImpl(SwRootFrame const *pLayout) const override
Definition: ddefld.cxx:347
virtual OUString GetPar2() const override
get field type command
Definition: ddefld.cxx:372
Definition: doc.hxx:197
IDocumentState const & getIDocumentState() const
Definition: doc.cxx:408
bool IsInDtor() const
Definition: doc.hxx:417
IDocumentLinksAdministration const & getIDocumentLinksAdministration() const
Definition: doc.cxx:274
SwEditShell const * GetEditShell() const
Definition: doccorr.cxx:330
IDocumentLayoutAccess const & getIDocumentLayoutAccess() const
Definition: doc.cxx:419
void StartAllAction()
For all views of this document.
Definition: edws.cxx:86
void EndAllAction()
Definition: edws.cxx:97
Instances of SwFields and those derived from it occur 0 to n times.
Definition: fldbas.hxx:247
void GatherDdeTables(std::vector< SwDDETable * > &rvTables) const
Definition: fldbas.cxx:210
void GatherFields(std::vector< SwFormatField * > &rvFormatFields, bool bCollectOnlyInDocNodes=true) const
Definition: fldbas.cxx:205
Base class of all fields.
Definition: fldbas.hxx:296
SwFieldType * GetTyp() const
Definition: fldbas.hxx:402
Base class of the Writer document model elements.
Definition: node.hxx:98
The root element of a Writer document layout.
Definition: rootfrm.hxx:85
void StartAction()
Definition: viewsh.hxx:619
void EndAction(const bool bIdleEnd=false)
Definition: viewsh.hxx:625
const IDocumentLayoutAccess & getIDocumentLayoutAccess() const
Provides access to the document layout interface.
Definition: viewsh.cxx:2827
SwDoc * GetDoc() const
Definition: viewsh.hxx:308
void InsertDDELink(SvBaseLink *, const OUString &rServer, std::u16string_view rTopic, std::u16string_view rItem)
void Remove(SvBaseLink const *pLink)
T * get() const
bool is() const
virtual OUString GetName() const override
SwFieldIds
Definition: fldbas.hxx:49
SotClipboardFormatId
STRING
sal_Int32 nIndex
OUString aName
sal_Int64 n
Sequence< sal_Int8 > aSeq
SfxLinkUpdateMode
aStr
int i
const sal_Unicode cTokenSeparator
OUString m_aName
virtual ~LinkAnchorSearchHint() override
Definition: ddefld.cxx:133
bool IsInRange(const WhichRangesContainer &pRange, const sal_uInt16 nId)
check if ID is in range of attribute set IDs
Definition: swatrset.cxx:451
#define FIELD_PROP_BOOL1
Definition: unofldmid.h:28
#define FIELD_PROP_PAR5
Definition: unofldmid.h:43
#define FIELD_PROP_SUBTYPE
Definition: unofldmid.h:27
#define FIELD_PROP_PAR4
Definition: unofldmid.h:36
#define FIELD_PROP_PAR2
Definition: unofldmid.h:24