update credits
[LibreOffice.git] / sw / source / filter / xml / xmltexti.cxx
bloba84f99c57a77431fced4d7c48da8da99e17f9564
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <comphelper/storagehelper.hxx>
21 #include <comphelper/processfactory.hxx>
22 #include <com/sun/star/embed/EmbedStates.hpp>
23 #include <com/sun/star/embed/EmbeddedObjectCreator.hpp>
24 #include <com/sun/star/embed/OOoEmbeddedObjectFactory.hpp>
25 #include <com/sun/star/embed/XLinkCreator.hpp>
26 #include <com/sun/star/embed/XEmbeddedObject.hpp>
27 #include <com/sun/star/embed/XVisualObject.hpp>
28 #include <com/sun/star/embed/Aspects.hpp>
29 #include <com/sun/star/task/XInteractionHandler.hpp>
30 #include <rtl/ustrbuf.hxx>
31 #include <comphelper/classids.hxx>
32 #include <com/sun/star/lang/XUnoTunnel.hpp>
33 #include <xmloff/prstylei.hxx>
34 #include <xmloff/maptype.hxx>
35 #include <xmloff/xmlprmap.hxx>
36 #include <xmloff/txtprmap.hxx>
37 #include <xmloff/i18nmap.hxx>
38 #include "unocrsr.hxx"
39 #include "TextCursorHelper.hxx"
40 #include "unoframe.hxx"
41 #include "doc.hxx"
42 #include "unocoll.hxx"
43 #include <fmtfsize.hxx>
44 #include <fmtanchr.hxx>
45 #include <fmtcntnt.hxx>
46 #include "xmlimp.hxx"
47 #include "xmltbli.hxx"
48 #include "xmltexti.hxx"
49 #include "XMLRedlineImportHelper.hxx"
50 #include <xmloff/XMLFilterServiceNames.h>
51 #include <SwAppletImpl.hxx>
52 #include <ndole.hxx>
53 #include <docsh.hxx>
54 #include <sfx2/docfile.hxx>
55 #include <switerator.hxx>
57 // for locking SolarMutex: svapp + mutex
58 #include <vcl/svapp.hxx>
59 #include <osl/mutex.hxx>
61 #include <toolkit/helper/vclunohelper.hxx>
62 #include <svtools/embedhlp.hxx>
63 #include <svl/urihelper.hxx>
66 using namespace ::com::sun::star;
67 using namespace ::com::sun::star::uno;
68 using namespace ::com::sun::star::lang;
69 using namespace ::com::sun::star::text;
70 using namespace ::com::sun::star::frame;
71 using namespace ::com::sun::star::beans;
72 using namespace xml::sax;
75 struct XMLServiceMapEntry_Impl
77 const sal_Char *sFilterService;
78 sal_Int32 nFilterServiceLen;
80 sal_uInt32 n1;
81 sal_uInt16 n2, n3;
82 sal_uInt8 n4, n5, n6, n7, n8, n9, n10, n11;
85 #define SERVICE_MAP_ENTRY( app, s ) \
86 { XML_IMPORT_FILTER_##app, sizeof(XML_IMPORT_FILTER_##app)-1, \
87 SO3_##s##_CLASSID }
89 const XMLServiceMapEntry_Impl aServiceMap[] =
91 SERVICE_MAP_ENTRY( WRITER, SW ),
92 SERVICE_MAP_ENTRY( CALC, SC ),
93 SERVICE_MAP_ENTRY( DRAW, SDRAW ),
94 SERVICE_MAP_ENTRY( IMPRESS, SIMPRESS ),
95 SERVICE_MAP_ENTRY( CHART, SCH ),
96 SERVICE_MAP_ENTRY( MATH, SM ),
97 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
99 static void lcl_putHeightAndWidth ( SfxItemSet &rItemSet,
100 sal_Int32 nHeight, sal_Int32 nWidth,
101 long *pTwipHeight=0, long *pTwipWidth=0 )
103 if( nWidth > 0 && nHeight > 0 )
105 nWidth = MM100_TO_TWIP( nWidth );
106 if( nWidth < MINFLY )
107 nWidth = MINFLY;
108 nHeight = MM100_TO_TWIP( nHeight );
109 if( nHeight < MINFLY )
110 nHeight = MINFLY;
111 rItemSet.Put( SwFmtFrmSize( ATT_FIX_SIZE, nWidth, nHeight ) );
114 SwFmtAnchor aAnchor( FLY_AT_CHAR );
115 rItemSet.Put( aAnchor );
117 if( pTwipWidth )
118 *pTwipWidth = nWidth;
119 if( pTwipHeight )
120 *pTwipHeight = nHeight;
123 static void lcl_setObjectVisualArea( const uno::Reference< embed::XEmbeddedObject >& xObj,
124 sal_Int64 nAspect,
125 const Size& aVisSize,
126 const MapUnit& aUnit )
128 if( xObj.is() && nAspect != embed::Aspects::MSOLE_ICON )
130 // convert the visual area to the objects units
131 MapUnit aObjUnit = VCLUnoHelper::UnoEmbed2VCLMapUnit( xObj->getMapUnit( nAspect ) );
132 Size aObjVisSize = OutputDevice::LogicToLogic( aVisSize, aUnit, aObjUnit );
133 awt::Size aSz;
134 aSz.Width = aObjVisSize.Width();
135 aSz.Height = aObjVisSize.Height();
139 xObj->setVisualAreaSize( nAspect, aSz );
141 catch( uno::Exception& )
143 OSL_FAIL( "Couldn't set visual area of the object!\n" );
148 SwXMLTextImportHelper::SwXMLTextImportHelper(
149 const uno::Reference < XModel>& rModel,
150 SvXMLImport& rImport,
151 const uno::Reference<XPropertySet> & rInfoSet,
152 bool bInsertM, bool bStylesOnlyM, bool _bProgress,
153 bool bBlockM, bool bOrganizerM,
154 sal_Bool /*bPreserveRedlineMode*/ ) :
155 XMLTextImportHelper( rModel, rImport, bInsertM, bStylesOnlyM, _bProgress,
156 bBlockM, bOrganizerM ),
157 pRedlineHelper( NULL )
159 uno::Reference<XPropertySet> xDocPropSet( rModel, UNO_QUERY );
160 pRedlineHelper = new XMLRedlineImportHelper(
161 bInsertM || bBlockM, xDocPropSet, rInfoSet );
164 SwXMLTextImportHelper::~SwXMLTextImportHelper()
166 // the redline helper destructor sets properties on the document
167 // and may through an exception while doing so... catch this
170 delete pRedlineHelper;
172 catch ( const RuntimeException& )
174 // ignore
178 SvXMLImportContext *SwXMLTextImportHelper::CreateTableChildContext(
179 SvXMLImport& rImport,
180 sal_uInt16 nPrefix, const OUString& rLocalName,
181 const uno::Reference< XAttributeList > & xAttrList )
183 return new SwXMLTableContext(
184 (SwXMLImport&)rImport, nPrefix, rLocalName, xAttrList );
187 sal_Bool SwXMLTextImportHelper::IsInHeaderFooter() const
189 uno::Reference<XUnoTunnel> xCrsrTunnel(
190 ((SwXMLTextImportHelper *)this)->GetCursor(), UNO_QUERY );
191 OSL_ENSURE( xCrsrTunnel.is(), "missing XUnoTunnel for Cursor" );
192 OTextCursorHelper *pTxtCrsr = reinterpret_cast< OTextCursorHelper * >(
193 sal::static_int_cast< sal_IntPtr >( xCrsrTunnel->getSomething( OTextCursorHelper::getUnoTunnelId() )));
194 OSL_ENSURE( pTxtCrsr, "SwXTextCursor missing" );
195 SwDoc *pDoc = pTxtCrsr ? pTxtCrsr->GetDoc() : NULL;
197 return pDoc && pDoc->IsInHeaderFooter( pTxtCrsr->GetPaM()->GetPoint()->nNode );
200 static SwOLENode *lcl_GetOLENode( const SwFrmFmt *pFrmFmt )
202 SwOLENode *pOLENd = 0;
203 if( pFrmFmt )
205 const SwFmtCntnt& rCntnt = pFrmFmt->GetCntnt();
206 const SwNodeIndex *pNdIdx = rCntnt.GetCntntIdx();
207 pOLENd = pNdIdx->GetNodes()[pNdIdx->GetIndex() + 1]->GetOLENode();
209 OSL_ENSURE( pOLENd, "Where is the OLE node" );
210 return pOLENd;
213 uno::Reference< XPropertySet > SwXMLTextImportHelper::createAndInsertOLEObject(
214 SvXMLImport& rImport,
215 const OUString& rHRef,
216 const OUString& rStyleName,
217 const OUString& rTblName,
218 sal_Int32 nWidth, sal_Int32 nHeight )
220 // this method will modify the document directly -> lock SolarMutex
221 SolarMutexGuard aGuard;
223 uno::Reference < XPropertySet > xPropSet;
225 sal_Int32 nPos = rHRef.indexOf( ':' );
226 if( -1 == nPos )
227 return xPropSet;
229 OUString aObjName( rHRef.copy( nPos+1) );
231 if( aObjName.isEmpty() )
232 return xPropSet;
234 uno::Reference<XUnoTunnel> xCrsrTunnel( GetCursor(), UNO_QUERY );
235 OSL_ENSURE( xCrsrTunnel.is(), "missing XUnoTunnel for Cursor" );
236 OTextCursorHelper *pTxtCrsr = reinterpret_cast< OTextCursorHelper * >(
237 sal::static_int_cast< sal_IntPtr >( xCrsrTunnel->getSomething( OTextCursorHelper::getUnoTunnelId() )));
238 OSL_ENSURE( pTxtCrsr, "SwXTextCursor missing" );
239 SwDoc *pDoc = SwImport::GetDocFromXMLImport( rImport );
241 SfxItemSet aItemSet( pDoc->GetAttrPool(), RES_FRMATR_BEGIN,
242 RES_FRMATR_END );
243 Size aTwipSize( 0, 0 );
244 Rectangle aVisArea( 0, 0, nWidth, nHeight );
245 lcl_putHeightAndWidth( aItemSet, nHeight, nWidth,
246 &aTwipSize.Height(), &aTwipSize.Width() );
248 SwFrmFmt *pFrmFmt = 0;
249 SwOLENode *pOLENd = 0;
250 if( rHRef.copy( 0, nPos ) == "vnd.sun.star.ServiceName" )
252 bool bInsert = false;
253 SvGlobalName aClassName;
254 const XMLServiceMapEntry_Impl *pEntry = aServiceMap;
255 while( pEntry->sFilterService )
257 if( aObjName.equalsAsciiL( pEntry->sFilterService,
258 pEntry->nFilterServiceLen ) )
260 aClassName = SvGlobalName( pEntry->n1, pEntry->n2,
261 pEntry->n3, pEntry->n4,
262 pEntry->n5, pEntry->n6,
263 pEntry->n7, pEntry->n8,
264 pEntry->n9, pEntry->n10,
265 pEntry->n11 );
266 bInsert = true;
267 break;
269 pEntry++;
272 if( bInsert )
274 uno::Reference < embed::XStorage > xStorage = comphelper::OStorageHelper::GetTemporaryStorage();
277 // create object with desired ClassId
278 sal_Int64 nAspect = embed::Aspects::MSOLE_CONTENT;
279 OUString aName("DummyName");
280 uno::Sequence < sal_Int8 > aClass( aClassName.GetByteSequence() );
281 uno::Reference < embed::XEmbeddedObjectCreator > xFactory = embed::EmbeddedObjectCreator::create( ::comphelper::getProcessComponentContext() );
282 uno::Reference < embed::XEmbeddedObject > xObj =
283 uno::Reference < embed::XEmbeddedObject >( xFactory->createInstanceInitNew(
284 aClass, OUString(), xStorage, aName,
285 uno::Sequence < beans::PropertyValue >() ), uno::UNO_QUERY );
286 if ( xObj.is() )
288 //TODO/LATER: is it enough to only set the VisAreaSize?
289 lcl_setObjectVisualArea( xObj, nAspect, aTwipSize, MAP_TWIP );
292 pFrmFmt = pDoc->Insert( *pTxtCrsr->GetPaM(),
293 ::svt::EmbeddedObjectRef( xObj, embed::Aspects::MSOLE_CONTENT ),
294 &aItemSet,
295 NULL,
296 NULL );
297 pOLENd = lcl_GetOLENode( pFrmFmt );
298 if( pOLENd )
299 aObjName = pOLENd->GetOLEObj().GetCurrentPersistName();
301 catch ( uno::Exception& )
306 else
308 // check whether an object with this name already exists in the document
309 String aName;
310 SwIterator<SwCntntNode,SwFmtColl> aIter( *pDoc->GetDfltGrfFmtColl() );
311 for( SwCntntNode* pNd = aIter.First(); pNd; pNd = aIter.Next() )
313 SwOLENode* pExistingOLENd = pNd->GetOLENode();
314 if( pExistingOLENd )
316 OUString aExistingName = pExistingOLENd->GetOLEObj().GetCurrentPersistName();
317 if ( aExistingName.equals( aObjName ) )
319 OSL_FAIL( "The document contains duplicate object references, means it is partially broken, please let developers know how this document was generated!\n" );
321 OUString aTmpName = pDoc->GetPersist()->GetEmbeddedObjectContainer().CreateUniqueObjectName();
324 pDoc->GetPersist()->GetStorage()->copyElementTo( aObjName,
325 pDoc->GetPersist()->GetStorage(),
326 aTmpName );
327 aName = aTmpName;
329 catch ( uno::Exception& )
331 OSL_FAIL( "Couldn't create a copy of the object!\n" );
334 break;
339 if ( !aName.Len() )
340 aName = aObjName;
342 // the correct aspect will be set later
343 // TODO/LATER: Actually it should be set here
344 if( pTxtCrsr )
346 pFrmFmt = pDoc->InsertOLE( *pTxtCrsr->GetPaM(), aName, embed::Aspects::MSOLE_CONTENT, &aItemSet, NULL, NULL );
347 pOLENd = lcl_GetOLENode( pFrmFmt );
349 aObjName = aName;
352 if( !pFrmFmt )
353 return xPropSet;
355 if( IsInsertMode() )
357 if( !pOLENd )
358 pOLENd = lcl_GetOLENode( pFrmFmt );
359 if( pOLENd )
360 pOLENd->SetOLESizeInvalid( sal_True );
363 SwXFrame *pXFrame = SwXFrames::GetObject( *pFrmFmt, FLYCNTTYPE_OLE );
364 xPropSet = pXFrame;
365 if( pDoc->GetDrawModel() )
366 SwXFrame::GetOrCreateSdrObject(
367 static_cast<SwFlyFrmFmt*>( pXFrame->GetFrmFmt() ) ); // req for z-order
368 if( !rTblName.isEmpty() )
370 const SwFmtCntnt& rCntnt = pFrmFmt->GetCntnt();
371 const SwNodeIndex *pNdIdx = rCntnt.GetCntntIdx();
372 SwOLENode *pOLENode = pNdIdx->GetNodes()[pNdIdx->GetIndex() + 1]->GetOLENode();
373 OSL_ENSURE( pOLENode, "Where is the OLE node" );
375 OUStringBuffer aBuffer( rTblName.getLength() );
376 bool bQuoted = false;
377 bool bEscape = false;
378 bool bError = false;
379 for( sal_Int32 i=0; i < rTblName.getLength(); i++ )
381 bool bEndOfNameFound = false;
382 sal_Unicode c = rTblName[i];
383 switch( c )
385 case '\'':
386 if( bEscape )
388 aBuffer.append( c );
389 bEscape = false;
391 else if( bQuoted )
393 bEndOfNameFound = true;
395 else if( 0 == i )
397 bQuoted = true;
399 else
401 bError = true;
403 break;
404 case '\\':
405 if( bEscape )
407 aBuffer.append( c );
408 bEscape = false;
410 else
412 bEscape = true;
414 break;
415 case ' ':
416 case '.':
417 if( !bQuoted )
419 bEndOfNameFound = true;
421 else
423 aBuffer.append( c );
424 bEscape = false;
426 break;
427 default:
429 aBuffer.append( c );
430 bEscape = false;
432 break;
434 if( bError || bEndOfNameFound )
435 break;
437 if( !bError )
439 OUString sTblName( aBuffer.makeStringAndClear() );
440 pOLENode->SetChartTblName( GetRenameMap().Get( XML_TEXT_RENAME_TYPE_TABLE, sTblName ) );
444 sal_Int64 nDrawAspect = 0;
445 const XMLPropStyleContext *pStyle = 0;
446 bool bHasSizeProps = false;
447 if( !rStyleName.isEmpty() )
449 pStyle = FindAutoFrameStyle( rStyleName );
450 if( pStyle )
452 UniReference < SvXMLImportPropertyMapper > xImpPrMap =
453 pStyle->GetStyles()
454 ->GetImportPropertyMapper(pStyle->GetFamily());
455 OSL_ENSURE( xImpPrMap.is(), "Where is the import prop mapper?" );
456 if( xImpPrMap.is() )
458 UniReference<XMLPropertySetMapper> rPropMapper =
459 xImpPrMap->getPropertySetMapper();
461 sal_Int32 nCount = pStyle->GetProperties().size();
462 for( sal_Int32 i=0; i < nCount; i++ )
464 const XMLPropertyState& rProp = pStyle->GetProperties()[i];
465 sal_Int32 nIdx = rProp.mnIndex;
466 if( -1 == nIdx )
467 continue;
469 switch( rPropMapper->GetEntryContextId(nIdx) )
471 case CTF_OLE_VIS_AREA_LEFT:
473 sal_Int32 nVal = 0;
474 rProp.maValue >>= nVal;
475 aVisArea.setX( nVal );
477 break;
478 case CTF_OLE_VIS_AREA_TOP:
480 sal_Int32 nVal = 0;
481 rProp.maValue >>= nVal;
482 aVisArea.setY( nVal );
484 break;
485 case CTF_OLE_VIS_AREA_WIDTH:
487 sal_Int32 nVal = 0;
488 rProp.maValue >>= nVal;
489 aVisArea.setWidth( nVal );
490 bHasSizeProps = true;
492 break;
493 case CTF_OLE_VIS_AREA_HEIGHT:
495 sal_Int32 nVal = 0;
496 rProp.maValue >>= nVal;
497 aVisArea.setHeight( nVal );
498 bHasSizeProps = true;
500 break;
501 case CTF_OLE_DRAW_ASPECT:
503 rProp.maValue >>= nDrawAspect;
505 if ( !nDrawAspect )
506 nDrawAspect = embed::Aspects::MSOLE_CONTENT;
508 if ( pOLENd )
509 pOLENd->GetOLEObj().GetObject().SetViewAspect( nDrawAspect );
511 break;
518 if ( bHasSizeProps )
520 uno::Reference < embed::XEmbeddedObject > xObj =
521 pDoc->GetPersist()->GetEmbeddedObjectContainer().GetEmbeddedObject( aObjName );
522 if( xObj.is() )
523 lcl_setObjectVisualArea( xObj, ( nDrawAspect ? nDrawAspect : embed::Aspects::MSOLE_CONTENT ),
524 aVisArea.GetSize(), MAP_100TH_MM );
527 return xPropSet;
530 uno::Reference< XPropertySet > SwXMLTextImportHelper::createAndInsertOOoLink(
531 SvXMLImport& rImport,
532 const OUString& rHRef,
533 const OUString& /*rStyleName*/,
534 const OUString& /*rTblName*/,
535 sal_Int32 nWidth, sal_Int32 nHeight )
537 // this method will modify the document directly -> lock SolarMutex
538 SolarMutexGuard aGuard;
540 uno::Reference < XPropertySet > xPropSet;
542 uno::Reference<XUnoTunnel> xCrsrTunnel( GetCursor(), UNO_QUERY );
543 OSL_ENSURE( xCrsrTunnel.is(), "missing XUnoTunnel for Cursor" );
544 OTextCursorHelper *pTxtCrsr = reinterpret_cast< OTextCursorHelper * >(
545 sal::static_int_cast< sal_IntPtr >( xCrsrTunnel->getSomething( OTextCursorHelper::getUnoTunnelId() )));
546 OSL_ENSURE( pTxtCrsr, "SwXTextCursor missing" );
547 SwDoc *pDoc = SwImport::GetDocFromXMLImport( rImport );
549 SfxItemSet aItemSet( pDoc->GetAttrPool(), RES_FRMATR_BEGIN,
550 RES_FRMATR_END );
551 Size aTwipSize( 0, 0 );
552 lcl_putHeightAndWidth( aItemSet, nHeight, nWidth,
553 &aTwipSize.Height(), &aTwipSize.Width() );
555 // We'll need a (valid) URL. If we don't have do not insert the link and return early.
556 // Copy URL into URL oject on the way.
557 INetURLObject aURLObj;
558 bool bValidURL = !rHRef.isEmpty() &&
559 aURLObj.SetURL( URIHelper::SmartRel2Abs(
560 INetURLObject( GetXMLImport().GetBaseURL() ), rHRef ) );
561 if( !bValidURL )
562 return xPropSet;
564 uno::Reference < embed::XStorage > xStorage = comphelper::OStorageHelper::GetTemporaryStorage();
567 // create object with desired ClassId
568 OUString aName("DummyName");
569 uno::Reference < embed::XEmbeddedObjectCreator > xFactory =
570 embed::OOoEmbeddedObjectFactory::create(::comphelper::getProcessComponentContext());
572 uno::Sequence< beans::PropertyValue > aMediaDescriptor( 1 );
573 aMediaDescriptor[0].Name = OUString("URL");
574 aMediaDescriptor[0].Value <<= OUString( aURLObj.GetMainURL( INetURLObject::NO_DECODE ) );
575 if ( pDoc && pDoc->GetDocShell() && pDoc->GetDocShell()->GetMedium() )
577 uno::Reference< task::XInteractionHandler > xInteraction =
578 pDoc->GetDocShell()->GetMedium()->GetInteractionHandler();
579 if ( xInteraction.is() )
581 aMediaDescriptor.realloc( 2 );
582 aMediaDescriptor[1].Name = OUString( "InteractionHandler" );
583 aMediaDescriptor[1].Value <<= xInteraction;
587 uno::Reference < embed::XEmbeddedObject > xObj(
588 xFactory->createInstanceLink(
589 xStorage, aName, aMediaDescriptor, uno::Sequence< beans::PropertyValue >() ),
590 uno::UNO_QUERY_THROW );
593 SwFrmFmt *pFrmFmt = pDoc->Insert( *pTxtCrsr->GetPaM(),
594 ::svt::EmbeddedObjectRef( xObj, embed::Aspects::MSOLE_CONTENT ),
595 &aItemSet,
596 NULL,
597 NULL );
599 // TODO/LATER: in future may need a way to set replacement image url to the link ( may be even to the object ), needs oasis cws???
601 SwXFrame *pXFrame = SwXFrames::GetObject( *pFrmFmt, FLYCNTTYPE_OLE );
602 xPropSet = pXFrame;
603 if( pDoc->GetDrawModel() )
604 SwXFrame::GetOrCreateSdrObject(
605 static_cast<SwFlyFrmFmt*>( pXFrame->GetFrmFmt() ) ); // req for z-order
608 catch ( uno::Exception& )
612 // TODO/LATER: should the rStyleName and rTblName be handled as for usual embedded object?
614 return xPropSet;
617 uno::Reference< XPropertySet > SwXMLTextImportHelper::createAndInsertApplet(
618 const OUString &rName,
619 const OUString &rCode,
620 sal_Bool bMayScript,
621 const OUString& rHRef,
622 sal_Int32 nWidth, sal_Int32 nHeight )
624 // this method will modify the document directly -> lock SolarMutex
625 SolarMutexGuard aGuard;
627 uno::Reference < XPropertySet > xPropSet;
628 uno::Reference<XUnoTunnel> xCrsrTunnel( GetCursor(), UNO_QUERY );
629 OSL_ENSURE( xCrsrTunnel.is(), "missing XUnoTunnel for Cursor" );
630 OTextCursorHelper *pTxtCrsr = reinterpret_cast< OTextCursorHelper * >(
631 sal::static_int_cast< sal_IntPtr >( xCrsrTunnel->getSomething( OTextCursorHelper::getUnoTunnelId() )));
632 OSL_ENSURE( pTxtCrsr, "SwXTextCursor missing" );
633 SwDoc *pDoc = pTxtCrsr->GetDoc();
635 SfxItemSet aItemSet( pDoc->GetAttrPool(), RES_FRMATR_BEGIN,
636 RES_FRMATR_END );
637 lcl_putHeightAndWidth( aItemSet, nHeight, nWidth);
639 SwApplet_Impl aAppletImpl ( aItemSet );
641 String sCodeBase;
642 if( !rHRef.isEmpty() )
643 sCodeBase = GetXMLImport().GetAbsoluteReference( rHRef );
645 aAppletImpl.CreateApplet ( rCode, rName, bMayScript, sCodeBase, GetXMLImport().GetDocumentBase() );
647 // set the size of the applet
648 lcl_setObjectVisualArea( aAppletImpl.GetApplet(),
649 embed::Aspects::MSOLE_CONTENT,
650 Size( nWidth, nHeight ),
651 MAP_100TH_MM );
653 SwFrmFmt *pFrmFmt = pDoc->Insert( *pTxtCrsr->GetPaM(),
654 ::svt::EmbeddedObjectRef( aAppletImpl.GetApplet(), embed::Aspects::MSOLE_CONTENT ),
655 &aAppletImpl.GetItemSet(),
656 NULL,
657 NULL);
658 SwXFrame *pXFrame = SwXFrames::GetObject( *pFrmFmt, FLYCNTTYPE_OLE );
659 xPropSet = pXFrame;
660 if( pDoc->GetDrawModel() )
661 SwXFrame::GetOrCreateSdrObject(
662 static_cast<SwFlyFrmFmt*>( pXFrame->GetFrmFmt() ) ); // req for z-order
664 return xPropSet;
667 uno::Reference< XPropertySet > SwXMLTextImportHelper::createAndInsertPlugin(
668 const OUString &rMimeType,
669 const OUString& rHRef,
670 sal_Int32 nWidth, sal_Int32 nHeight )
672 uno::Reference < XPropertySet > xPropSet;
673 uno::Reference<XUnoTunnel> xCrsrTunnel( GetCursor(), UNO_QUERY );
674 OSL_ENSURE( xCrsrTunnel.is(), "missing XUnoTunnel for Cursor" );
675 OTextCursorHelper *pTxtCrsr = reinterpret_cast< OTextCursorHelper * >(
676 sal::static_int_cast< sal_IntPtr >( xCrsrTunnel->getSomething( OTextCursorHelper::getUnoTunnelId() )));
677 OSL_ENSURE( pTxtCrsr, "SwXTextCursor missing" );
678 SwDoc *pDoc = pTxtCrsr->GetDoc();
680 SfxItemSet aItemSet( pDoc->GetAttrPool(), RES_FRMATR_BEGIN,
681 RES_FRMATR_END );
682 lcl_putHeightAndWidth( aItemSet, nHeight, nWidth);
684 // We'll need a (valid) URL, or we need a MIME type. If we don't have
685 // either, do not insert plugin and return early. Copy URL into URL oject
686 // on the way.
687 INetURLObject aURLObj;
689 bool bValidURL = !rHRef.isEmpty() &&
690 aURLObj.SetURL( URIHelper::SmartRel2Abs( INetURLObject( GetXMLImport().GetBaseURL() ), rHRef ) );
691 bool bValidMimeType = !rMimeType.isEmpty();
692 if( !bValidURL && !bValidMimeType )
693 return xPropSet;
695 uno::Reference < embed::XStorage > xStorage = comphelper::OStorageHelper::GetTemporaryStorage();
698 // create object with desired ClassId
699 OUString aName("DummyName");
700 uno::Sequence < sal_Int8 > aClass( SvGlobalName( SO3_PLUGIN_CLASSID ).GetByteSequence() );
701 uno::Reference < embed::XEmbeddedObjectCreator > xFactory = embed::EmbeddedObjectCreator::create( ::comphelper::getProcessComponentContext() );
702 uno::Reference < embed::XEmbeddedObject > xObj =
703 uno::Reference < embed::XEmbeddedObject >( xFactory->createInstanceInitNew(
704 aClass, OUString(), xStorage, aName,
705 uno::Sequence < beans::PropertyValue >() ), uno::UNO_QUERY );
707 // set size to the object
708 lcl_setObjectVisualArea( xObj,
709 embed::Aspects::MSOLE_CONTENT,
710 Size( nWidth, nHeight ),
711 MAP_100TH_MM );
713 if ( svt::EmbeddedObjectRef::TryRunningState( xObj ) )
715 uno::Reference < beans::XPropertySet > xSet( xObj->getComponent(), uno::UNO_QUERY );
716 if ( xSet.is() )
718 if( bValidURL )
719 xSet->setPropertyValue( OUString("PluginURL"),
720 makeAny( OUString( aURLObj.GetMainURL( INetURLObject::NO_DECODE ) ) ) );
721 if( bValidMimeType )
722 xSet->setPropertyValue( OUString("PluginMimeType"),
723 makeAny( OUString( rMimeType ) ) );
726 SwFrmFmt *pFrmFmt = pDoc->Insert( *pTxtCrsr->GetPaM(),
727 ::svt::EmbeddedObjectRef( xObj, embed::Aspects::MSOLE_CONTENT ),
728 &aItemSet,
729 NULL,
730 NULL);
731 SwXFrame *pXFrame = SwXFrames::GetObject( *pFrmFmt, FLYCNTTYPE_OLE );
732 xPropSet = pXFrame;
733 if( pDoc->GetDrawModel() )
734 SwXFrame::GetOrCreateSdrObject(
735 static_cast<SwFlyFrmFmt*>( pXFrame->GetFrmFmt() ) ); // req for z-order
738 catch ( uno::Exception& )
742 return xPropSet;
744 uno::Reference< XPropertySet > SwXMLTextImportHelper::createAndInsertFloatingFrame(
745 const OUString& rName,
746 const OUString& rHRef,
747 const OUString& rStyleName,
748 sal_Int32 nWidth, sal_Int32 nHeight )
750 // this method will modify the document directly -> lock SolarMutex
751 SolarMutexGuard aGuard;
753 uno::Reference < XPropertySet > xPropSet;
754 uno::Reference<XUnoTunnel> xCrsrTunnel( GetCursor(), UNO_QUERY );
755 OSL_ENSURE( xCrsrTunnel.is(), "missing XUnoTunnel for Cursor" );
756 OTextCursorHelper *pTxtCrsr = reinterpret_cast< OTextCursorHelper * >(
757 sal::static_int_cast< sal_IntPtr >( xCrsrTunnel->getSomething( OTextCursorHelper::getUnoTunnelId() )));
758 OSL_ENSURE( pTxtCrsr, "SwXTextCursor missing" );
759 SwDoc *pDoc = pTxtCrsr->GetDoc();
761 SfxItemSet aItemSet( pDoc->GetAttrPool(), RES_FRMATR_BEGIN,
762 RES_FRMATR_END );
763 lcl_putHeightAndWidth( aItemSet, nHeight, nWidth);
765 ScrollingMode eScrollMode = ScrollingAuto;
766 sal_Bool bHasBorder = sal_False;
767 bool bIsBorderSet = false;
768 Size aMargin( SIZE_NOT_SET, SIZE_NOT_SET );
769 const XMLPropStyleContext *pStyle = 0;
770 if( !rStyleName.isEmpty() )
772 pStyle = FindAutoFrameStyle( rStyleName );
773 if( pStyle )
775 UniReference < SvXMLImportPropertyMapper > xImpPrMap =
776 pStyle->GetStyles()
777 ->GetImportPropertyMapper(pStyle->GetFamily());
778 OSL_ENSURE( xImpPrMap.is(), "Where is the import prop mapper?" );
779 if( xImpPrMap.is() )
781 UniReference<XMLPropertySetMapper> rPropMapper =
782 xImpPrMap->getPropertySetMapper();
784 sal_Int32 nCount = pStyle->GetProperties().size();
785 for( sal_Int32 i=0; i < nCount; i++ )
787 const XMLPropertyState& rProp = pStyle->GetProperties()[i];
788 sal_Int32 nIdx = rProp.mnIndex;
789 if( -1 == nIdx )
790 continue;
792 switch( rPropMapper->GetEntryContextId(nIdx) )
794 case CTF_FRAME_DISPLAY_SCROLLBAR:
796 sal_Bool bYes = *(sal_Bool *)rProp.maValue.getValue();
797 eScrollMode = bYes ? ScrollingYes : ScrollingNo;
799 break;
800 case CTF_FRAME_DISPLAY_BORDER:
802 bHasBorder = *(sal_Bool *)rProp.maValue.getValue();
803 bIsBorderSet = true;
805 break;
806 case CTF_FRAME_MARGIN_HORI:
808 sal_Int32 nVal = SIZE_NOT_SET;
809 rProp.maValue >>= nVal;
810 aMargin.Width() = nVal;
812 break;
813 case CTF_FRAME_MARGIN_VERT:
815 sal_Int32 nVal = SIZE_NOT_SET;
816 rProp.maValue >>= nVal;
817 aMargin.Height() = nVal;
819 break;
826 uno::Reference < embed::XStorage > xStorage = comphelper::OStorageHelper::GetTemporaryStorage();
829 // create object with desired ClassId
830 OUString aName("DummyName");
831 uno::Sequence < sal_Int8 > aClass( SvGlobalName( SO3_IFRAME_CLASSID ).GetByteSequence() );
832 uno::Reference < embed::XEmbeddedObjectCreator > xFactory = embed::EmbeddedObjectCreator::create( ::comphelper::getProcessComponentContext() );
833 uno::Reference < embed::XEmbeddedObject > xObj =
834 uno::Reference < embed::XEmbeddedObject >( xFactory->createInstanceInitNew(
835 aClass, OUString(), xStorage, aName,
836 uno::Sequence < beans::PropertyValue >() ), uno::UNO_QUERY );
838 // set size to the object
839 lcl_setObjectVisualArea( xObj,
840 embed::Aspects::MSOLE_CONTENT,
841 Size( nWidth, nHeight ),
842 MAP_100TH_MM );
844 if ( svt::EmbeddedObjectRef::TryRunningState( xObj ) )
846 uno::Reference < beans::XPropertySet > xSet( xObj->getComponent(), uno::UNO_QUERY );
847 if ( xSet.is() )
849 xSet->setPropertyValue( OUString("FrameURL"),
850 makeAny( OUString( URIHelper::SmartRel2Abs(
851 INetURLObject( GetXMLImport().GetBaseURL() ), rHRef ) ) ) );
853 xSet->setPropertyValue( OUString("FrameName"),
854 makeAny( OUString( rName ) ) );
856 if ( eScrollMode == ScrollingAuto )
857 xSet->setPropertyValue( OUString("FrameIsAutoScroll"),
858 makeAny( sal_True ) );
859 else
860 xSet->setPropertyValue( OUString("FrameIsScrollingMode"),
861 makeAny( (sal_Bool) (eScrollMode == ScrollingYes) ) );
863 if ( bIsBorderSet )
864 xSet->setPropertyValue( OUString("FrameIsBorder"),
865 makeAny( bHasBorder ) );
866 else
867 xSet->setPropertyValue( OUString("FrameIsAutoBorder"),
868 makeAny( sal_True ) );
870 xSet->setPropertyValue( OUString("FrameMarginWidth"),
871 makeAny( sal_Int32( aMargin.Width() ) ) );
873 xSet->setPropertyValue( OUString("FrameMarginHeight"),
874 makeAny( sal_Int32( aMargin.Height() ) ) );
877 SwFrmFmt *pFrmFmt = pDoc->Insert( *pTxtCrsr->GetPaM(),
878 ::svt::EmbeddedObjectRef( xObj, embed::Aspects::MSOLE_CONTENT ),
879 &aItemSet,
880 NULL,
881 NULL);
882 SwXFrame *pXFrame = SwXFrames::GetObject( *pFrmFmt, FLYCNTTYPE_OLE );
883 xPropSet = pXFrame;
884 if( pDoc->GetDrawModel() )
885 SwXFrame::GetOrCreateSdrObject(
886 static_cast<SwFlyFrmFmt*>( pXFrame->GetFrmFmt() ) ); // req for z-order
889 catch ( uno::Exception& )
893 return xPropSet;
896 void SwXMLTextImportHelper::endAppletOrPlugin(
897 const uno::Reference < XPropertySet > &rPropSet,
898 ::std::map < const OUString, OUString, ::comphelper::UStringLess > &rParamMap)
900 // this method will modify the document directly -> lock SolarMutex
901 SolarMutexGuard aGuard;
903 uno::Reference<XUnoTunnel> xCrsrTunnel( rPropSet, UNO_QUERY );
904 OSL_ENSURE( xCrsrTunnel.is(), "missing XUnoTunnel for embedded" );
905 SwXFrame *pFrame = reinterpret_cast< SwXFrame * >(
906 sal::static_int_cast< sal_IntPtr >( xCrsrTunnel->getSomething( SwXFrame::getUnoTunnelId() )));
907 OSL_ENSURE( pFrame, "SwXFrame missing" );
908 SwFrmFmt *pFrmFmt = pFrame->GetFrmFmt();
909 const SwFmtCntnt& rCntnt = pFrmFmt->GetCntnt();
910 const SwNodeIndex *pNdIdx = rCntnt.GetCntntIdx();
911 SwOLENode *pOLENd = pNdIdx->GetNodes()[pNdIdx->GetIndex() + 1]->GetNoTxtNode()->GetOLENode();
912 SwOLEObj& rOLEObj = pOLENd->GetOLEObj();
914 uno::Reference < embed::XEmbeddedObject > xEmbObj( rOLEObj.GetOleRef() );
915 if ( svt::EmbeddedObjectRef::TryRunningState( xEmbObj ) )
917 uno::Reference < beans::XPropertySet > xSet( xEmbObj->getComponent(), uno::UNO_QUERY );
918 if ( xSet.is() )
920 const sal_Int32 nCount = rParamMap.size();
921 uno::Sequence< beans::PropertyValue > aCommandSequence( nCount );
923 ::std::map < const OUString, OUString, ::comphelper::UStringLess > ::iterator aIter = rParamMap.begin();
924 ::std::map < const OUString, OUString, ::comphelper::UStringLess > ::iterator aEnd = rParamMap.end();
925 sal_Int32 nIndex=0;
926 while (aIter != aEnd )
928 aCommandSequence[nIndex].Name = (*aIter).first;
929 aCommandSequence[nIndex].Handle = -1;
930 aCommandSequence[nIndex].Value = makeAny( OUString((*aIter).second) );
931 aCommandSequence[nIndex].State = beans::PropertyState_DIRECT_VALUE;
932 ++aIter, ++nIndex;
935 // unfortunately the names of the properties are depending on the object
936 OUString aParaName("AppletCommands");
939 xSet->setPropertyValue( aParaName, makeAny( aCommandSequence ) );
941 catch ( uno::Exception& )
943 aParaName = OUString("PluginCommands");
946 xSet->setPropertyValue( aParaName, makeAny( aCommandSequence ) );
948 catch ( uno::Exception& )
956 // redlining helper methods
957 // (override to provide the real implementation)
959 void SwXMLTextImportHelper::RedlineAdd(
960 const OUString& rType,
961 const OUString& rId,
962 const OUString& rAuthor,
963 const OUString& rComment,
964 const util::DateTime& rDateTime,
965 sal_Bool bMergeLastPara)
967 // create redline helper on demand
968 OSL_ENSURE(NULL != pRedlineHelper, "helper should have been created in constructor");
969 if (NULL != pRedlineHelper)
970 pRedlineHelper->Add(rType, rId, rAuthor, rComment, rDateTime,
971 bMergeLastPara);
974 uno::Reference<XTextCursor> SwXMLTextImportHelper::RedlineCreateText(
975 uno::Reference<XTextCursor> & rOldCursor,
976 const OUString& rId)
978 uno::Reference<XTextCursor> xRet;
980 if (NULL != pRedlineHelper)
982 xRet = pRedlineHelper->CreateRedlineTextSection(rOldCursor, rId);
985 return xRet;
988 void SwXMLTextImportHelper::RedlineSetCursor(
989 const OUString& rId,
990 sal_Bool bStart,
991 sal_Bool bIsOutsideOfParagraph)
993 if (NULL != pRedlineHelper) {
994 uno::Reference<XTextRange> xTextRange( GetCursor()->getStart() );
995 pRedlineHelper->SetCursor(rId, bStart, xTextRange,
996 bIsOutsideOfParagraph);
998 // else: ignore redline (wasn't added before, else we'd have a helper)
1001 void SwXMLTextImportHelper::RedlineAdjustStartNodeCursor(
1002 sal_Bool bStart)
1004 OUString rId = GetOpenRedlineId();
1005 if ((NULL != pRedlineHelper) && !rId.isEmpty())
1007 uno::Reference<XTextRange> xTextRange( GetCursor()->getStart() );
1008 pRedlineHelper->AdjustStartNodeCursor(rId, bStart, xTextRange );
1009 ResetOpenRedlineId();
1011 // else: ignore redline (wasn't added before, or no open redline ID
1014 void SwXMLTextImportHelper::SetShowChanges( sal_Bool bShowChanges )
1016 if ( NULL != pRedlineHelper )
1017 pRedlineHelper->SetShowChanges( bShowChanges );
1020 void SwXMLTextImportHelper::SetRecordChanges( sal_Bool bRecordChanges )
1022 if ( NULL != pRedlineHelper )
1023 pRedlineHelper->SetRecordChanges( bRecordChanges );
1026 void SwXMLTextImportHelper::SetChangesProtectionKey(
1027 const Sequence<sal_Int8> & rKey )
1029 if ( NULL != pRedlineHelper )
1030 pRedlineHelper->SetProtectionKey( rKey );
1035 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */