1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sot/formats.hxx>
21 #include <sfx2/app.hxx>
22 #include <sfx2/linkmgr.hxx>
23 #include "servobj.hxx"
27 #include "rangenam.hxx"
30 using namespace formula
;
32 static bool lcl_FillRangeFromName( ScRange
& rRange
, ScDocShell
* pDocSh
, const OUString
& rName
)
36 ScDocument
& rDoc
= pDocSh
->GetDocument();
37 ScRangeName
* pNames
= rDoc
.GetRangeName();
40 const ScRangeData
* pData
= pNames
->findByUpperName(ScGlobal::pCharClass
->uppercase(rName
));
43 if ( pData
->IsValidReference( rRange
) )
51 ScServerObjectSvtListenerForwarder::ScServerObjectSvtListenerForwarder(
52 ScServerObject
* pObjP
)
57 ScServerObjectSvtListenerForwarder::~ScServerObjectSvtListenerForwarder()
59 //! do NOT access pObj
62 void ScServerObjectSvtListenerForwarder::Notify( const SfxHint
& rHint
)
64 pObj
->Notify( aBroadcaster
, rHint
);
67 ScServerObject::ScServerObject( ScDocShell
* pShell
, const OUString
& rItem
) :
70 bRefreshListener( false )
74 if ( lcl_FillRangeFromName( aRange
, pDocSh
, rItem
) )
76 aItemStr
= rItem
; // must be parsed again on ref update
81 ScDocument
& rDoc
= pDocSh
->GetDocument();
82 SCTAB nTab
= ScDocShell::GetCurTab();
83 aRange
.aStart
.SetTab( nTab
);
85 // For DDE link, we always must parse references using OOO A1 convention.
87 if ( aRange
.Parse( rItem
, &rDoc
, FormulaGrammar::CONV_OOO
) & SCA_VALID
)
91 else if ( aRange
.aStart
.Parse( rItem
, &rDoc
, FormulaGrammar::CONV_OOO
) & SCA_VALID
)
94 aRange
.aEnd
= aRange
.aStart
;
98 OSL_FAIL("ScServerObject: invalid item");
102 pDocSh
->GetDocument().GetLinkManager()->InsertServer( this );
103 pDocSh
->GetDocument().StartListeningArea( aRange
, false, &aForwarder
);
105 StartListening(*pDocSh
); // um mitzubekommen, wenn die DocShell geloescht wird
106 StartListening(*SfxGetpApp()); // for SC_HINT_AREAS_CHANGED
109 ScServerObject::~ScServerObject()
114 void ScServerObject::Clear()
118 ScDocShell
* pTemp
= pDocSh
;
121 pTemp
->GetDocument().EndListeningArea(aRange
, false, &aForwarder
);
122 pTemp
->GetDocument().GetLinkManager()->RemoveServer( this );
123 EndListening(*pTemp
);
124 EndListening(*SfxGetpApp());
128 void ScServerObject::EndListeningAll()
130 aForwarder
.EndListeningAll();
131 SfxListener::EndListeningAll();
134 bool ScServerObject::GetData(
135 ::com::sun::star::uno::Any
& rData
/*out param*/,
136 const OUString
& rMimeType
, bool /* bSynchron */ )
141 // named ranges may have changed -> update aRange
142 if ( !aItemStr
.isEmpty() )
145 if ( lcl_FillRangeFromName( aNew
, pDocSh
, aItemStr
) && aNew
!= aRange
)
148 bRefreshListener
= true;
152 if ( bRefreshListener
)
154 // refresh the listeners now (this is called from a timer)
157 pDocSh
->GetDocument().StartListeningArea( aRange
, false, &aForwarder
);
158 StartListening(*pDocSh
);
159 StartListening(*SfxGetpApp());
160 bRefreshListener
= false;
163 OUString aDdeTextFmt
= pDocSh
->GetDdeTextFmt();
164 ScDocument
& rDoc
= pDocSh
->GetDocument();
166 if( SotClipboardFormatId::STRING
== SotExchange::GetFormatIdFromMimeType( rMimeType
))
168 ScImportExport
aObj( &rDoc
, aRange
);
169 if( aDdeTextFmt
[0] == 'F' )
170 aObj
.SetFormulas( true );
171 if( aDdeTextFmt
== "SYLK" || aDdeTextFmt
== "FSYLK" )
174 if( aObj
.ExportByteString( aByteData
, osl_getThreadTextEncoding(), SotClipboardFormatId::SYLK
) )
176 rData
<<= ::com::sun::star::uno::Sequence
< sal_Int8
>(
177 reinterpret_cast<const sal_Int8
*>(aByteData
.getStr()),
178 aByteData
.getLength() + 1 );
183 if( aDdeTextFmt
== "CSV" || aDdeTextFmt
== "FCSV" )
184 aObj
.SetSeparator( ',' );
185 aObj
.SetExportTextOptions( ScExportTextOptions( ScExportTextOptions::ToSpace
, ' ', false ) );
186 return aObj
.ExportData( rMimeType
, rData
);
189 ScImportExport
aObj( &rDoc
, aRange
);
190 aObj
.SetExportTextOptions( ScExportTextOptions( ScExportTextOptions::ToSpace
, ' ', false ) );
192 return aObj
.ExportData( rMimeType
, rData
);
196 void ScServerObject::Notify( SfxBroadcaster
& rBC
, const SfxHint
& rHint
)
198 bool bDataChanged
= false;
200 // DocShell can't be tested via type info, because SFX_HINT_DYING comes from the dtor
201 if ( &rBC
== pDocSh
)
203 // from DocShell, only SFX_HINT_DYING is interesting
204 const SfxSimpleHint
* pSimpleHint
= dynamic_cast<const SfxSimpleHint
*>( &rHint
);
205 if ( pSimpleHint
&& pSimpleHint
->GetId() == SFX_HINT_DYING
)
208 EndListening(*SfxGetpApp());
209 // don't access DocShell anymore for EndListening etc.
212 else if (rBC
.ISA(SfxApplication
))
214 const SfxSimpleHint
* pSimpleHint
= dynamic_cast<const SfxSimpleHint
*>( &rHint
);
215 if ( !aItemStr
.isEmpty() && pSimpleHint
&&
216 pSimpleHint
->GetId() == SC_HINT_AREAS_CHANGED
)
218 // check if named range was modified
220 if ( lcl_FillRangeFromName( aNew
, pDocSh
, aItemStr
) && aNew
!= aRange
)
226 // must be from Area broadcasters
228 const ScHint
* pScHint
= dynamic_cast<const ScHint
*>( &rHint
);
229 if (pScHint
&& (pScHint
->GetId() & SC_HINT_DATACHANGED
))
231 else if ( dynamic_cast<const ScAreaChangedHint
*>(&rHint
) ) // position of broadcaster changed
233 ScRange aNewRange
= static_cast<const ScAreaChangedHint
&>(rHint
).GetRange();
234 if ( aRange
!= aNewRange
)
236 bRefreshListener
= true;
240 else if ( dynamic_cast<const SfxSimpleHint
*>(&rHint
) )
242 sal_uLong nId
= static_cast<const SfxSimpleHint
&>(rHint
).GetId();
243 if (nId
== SFX_HINT_DYING
)
245 // If the range is being deleted, listening must be restarted
246 // after the deletion is complete (done in GetData)
247 bRefreshListener
= true;
253 if ( bDataChanged
&& HasDataLinks() )
254 SvLinkSource::NotifyDataChanged();
257 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */