xref: /trunk/main/sfx2/source/doc/objembed.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sfx2.hxx"
30 #include <com/sun/star/container/XChild.hpp>
31 #include <com/sun/star/embed/XEmbedObjectCreator.hpp>
32 #include <com/sun/star/embed/XComponentSupplier.hpp>
33 #include <com/sun/star/embed/XEmbedPersist.hpp>
34 #include <com/sun/star/util/XCloseable.hpp>
35 #include <com/sun/star/embed/EmbedStates.hpp>
36 
37 #include <sfx2/objsh.hxx>
38 #include <sfx2/app.hxx>
39 #include "objshimp.hxx"
40 #include <sfx2/sfx.hrc>
41 #include <sfx2/event.hxx>
42 
43 #include <comphelper/seqstream.hxx>
44 #include <comphelper/processfactory.hxx>
45 #include <comphelper/storagehelper.hxx>
46 #include <svtools/embedtransfer.hxx>
47 #include <vcl/outdev.hxx>
48 #include <vcl/gdimtf.hxx>
49 
50 using namespace ::com::sun::star;
51 
52 // -----------------------------------------------------------------------
53 // TODO/LATER: this workaround must be replaced by API in future if possible
54 SfxObjectShell* SfxObjectShell::GetParentShellByModel_Impl()
55 {
56     SfxObjectShell* pResult = NULL;
57 
58     try {
59         uno::Reference< container::XChild > xChildModel( GetModel(), uno::UNO_QUERY );
60         if ( xChildModel.is() )
61         {
62             uno::Reference< lang::XUnoTunnel > xParentTunnel( xChildModel->getParent(), uno::UNO_QUERY );
63             if ( xParentTunnel.is() )
64             {
65                 SvGlobalName aSfxIdent( SFX_GLOBAL_CLASSID );
66                 pResult = reinterpret_cast<SfxObjectShell*>(xParentTunnel->getSomething(
67                                                 uno::Sequence< sal_Int8 >( aSfxIdent.GetByteSequence() ) ) );
68             }
69         }
70     }
71     catch( uno::Exception& )
72     {
73         // TODO: error handling
74     }
75 
76     return pResult;
77 }
78 
79 // -----------------------------------------------------------------------
80 Printer* SfxObjectShell::GetDocumentPrinter()
81 {
82     SfxObjectShell* pParent = GetParentShellByModel_Impl();
83     if ( pParent )
84         return pParent->GetDocumentPrinter();
85     return NULL;
86 }
87 
88 // -----------------------------------------------------------------------
89 OutputDevice* SfxObjectShell::GetDocumentRefDev()
90 {
91     SfxObjectShell* pParent = GetParentShellByModel_Impl();
92     if ( pParent )
93         return pParent->GetDocumentRefDev();
94     return NULL;
95 }
96 
97 // -----------------------------------------------------------------------
98 void SfxObjectShell::OnDocumentPrinterChanged( Printer* /*pNewPrinter*/ )
99 {
100     // virtual method
101 }
102 
103 // -----------------------------------------------------------------------
104 Rectangle SfxObjectShell::GetVisArea( sal_uInt16 nAspect ) const
105 {
106     if( nAspect == ASPECT_CONTENT )
107         return pImp->m_aVisArea;
108     else if( nAspect == ASPECT_THUMBNAIL )
109     {
110         Rectangle aRect;
111         aRect.SetSize( OutputDevice::LogicToLogic( Size( 5000, 5000 ),
112                                          MAP_100TH_MM, GetMapUnit() ) );
113         return aRect;
114     }
115     return Rectangle();
116 }
117 
118 // -----------------------------------------------------------------------
119 const Rectangle& SfxObjectShell::GetVisArea() const
120 {
121     pImp->m_aVisArea = GetVisArea( ASPECT_CONTENT );
122     return pImp->m_aVisArea;
123 }
124 
125 // -----------------------------------------------------------------------
126 void SfxObjectShell::SetVisArea( const Rectangle & rVisArea )
127 {
128     if( pImp->m_aVisArea != rVisArea )
129     {
130         pImp->m_aVisArea = rVisArea;
131         if ( GetCreateMode() == SFX_CREATE_MODE_EMBEDDED )
132         {
133             if ( IsEnableSetModified() )
134                 SetModified( sal_True );
135 
136             SFX_APP()->NotifyEvent(SfxEventHint( SFX_EVENT_VISAREACHANGED, GlobalEventConfig::GetEventName(STR_EVENT_VISAREACHANGED), this));
137 
138             /*
139             Size aSize (GetVisArea().GetSize());
140             if ( GetIPEnv() && GetIPEnv()->GetEditWin() )
141                 ViewChanged( ASPECT_CONTENT );
142             */
143 
144 
145             // OutPlace die Gr"o\se des MDI-Fensters anpassen
146             // Unbedingt den Gr"o\senvergleich machen, spart nicht nur Zeit, sondern
147             // vermeidet auch Rundungsfehler !
148             /*
149                 // in case of ole outplace editing the frame should be found
150                 SfxViewFrame* pFrameToResize = pFrame ? pFrame : SfxViewFrame::GetFirst( GetObjectShell() );
151 
152                 if ( pFrameToResize && !pIPF && rRect.GetSize() != aSize &&
153                     !pFrameToResize->IsAdjustPosSizePixelLocked_Impl() )
154 
155                 {
156                     // Zuerst die logischen Koordinaten von IP-Objekt und EditWindow
157                     // ber"ucksichtigen
158                     SfxViewShell *pShell = pFrameToResize->GetViewShell();
159                     Window *pWindow = pShell->GetWindow();
160 
161                     // Da in den Applikationen bei der R"ucktransformation immer die
162                     // Eckpunkte tranformiert werden und nicht die Size (um die Ecken
163                     // alignen zu k"onnen), transformieren wir hier auch die Punkte, um
164                     // m"oglichst wenig Rundungsfehler zu erhalten.
165                     Rectangle aRect = pWindow->LogicToPixel( rRect );
166                     Size aSize = aRect.GetSize();
167                     pShell->GetWindow()->SetSizePixel( aSize );
168                     pFrameToResize->DoAdjustPosSizePixel( pShell, Point(), aSize );
169                 }
170 
171             // bei InPlace die View skalieren
172             if ( GetIPEnv() && GetIPEnv()->GetEditWin() && !bDisableViewScaling && pIPF )
173                 pIPF->GetEnv_Impl()->MakeScale( rRect.GetSize(), GetMapUnit(),
174                             pIPF->GetViewShell()->GetWindow()->GetOutputSizePixel() );
175            */
176         }
177     }
178 }
179 
180 // -----------------------------------------------------------------------
181 void SfxObjectShell::SetVisAreaSize( const Size & rVisSize )
182 {
183     SetVisArea( Rectangle( GetVisArea().TopLeft(), rVisSize ) );
184 }
185 
186 // -----------------------------------------------------------------------
187 sal_uIntPtr SfxObjectShell::GetMiscStatus() const
188 {
189     return 0;
190 }
191 
192 // -----------------------------------------------------------------------
193 MapUnit SfxObjectShell::GetMapUnit() const
194 {
195     return pImp->m_nMapUnit;
196 }
197 
198 // -----------------------------------------------------------------------
199 void SfxObjectShell::SetMapUnit( MapUnit nMapUnit )
200 {
201     pImp->m_nMapUnit = nMapUnit;
202 }
203 
204 // -----------------------------------------------------------------------
205 void SfxObjectShell::FillTransferableObjectDescriptor( TransferableObjectDescriptor& rDesc ) const
206 {
207     sal_uInt32 nClipFormat;
208     String aAppName, aShortName;
209     FillClass( &rDesc.maClassName, &nClipFormat, &aAppName, &rDesc.maTypeName, &aShortName, SOFFICE_FILEFORMAT_CURRENT );
210 
211     rDesc.mnViewAspect = ASPECT_CONTENT;
212     rDesc.mnOle2Misc = GetMiscStatus();
213     rDesc.maSize = OutputDevice::LogicToLogic( GetVisArea().GetSize(), GetMapUnit(), MAP_100TH_MM );
214     rDesc.maDragStartPos = Point();
215     rDesc.maDisplayName = String();
216     rDesc.mbCanLink = sal_False;
217 }
218 
219 // -----------------------------------------------------------------------
220 void SfxObjectShell::DoDraw( OutputDevice* pDev,
221                             const Point & rObjPos,
222                             const Size & rSize,
223                             const JobSetup & rSetup,
224                             sal_uInt16 nAspect )
225 {
226     MapMode aMod = pDev->GetMapMode();
227     Size aSize = GetVisArea( nAspect ).GetSize();
228     MapMode aWilliMode( GetMapUnit() );
229     aSize = pDev->LogicToLogic( aSize, &aWilliMode, &aMod );
230     if( aSize.Width() && aSize.Height() )
231     {
232         Fraction aXF( rSize.Width(), aSize.Width() );
233         Fraction aYF( rSize.Height(), aSize.Height() );
234 
235 //REMOVE            Point aOrg = rObjPos;
236 //REMOVE            aMod.SetMapUnit( MAP_100TH_MM );
237 //REMOVE            aSize = pDev->LogicToLogic( GetVisArea( nAspect ).GetSize(), &aMod, &aWilliMode );
238         DoDraw_Impl( pDev, rObjPos, aXF, aYF, rSetup, nAspect );
239     }
240 }
241 
242 // -----------------------------------------------------------------------
243 void SfxObjectShell::DoDraw_Impl( OutputDevice* pDev,
244                                const Point & rViewPos,
245                                const Fraction & rScaleX,
246                                const Fraction & rScaleY,
247                                const JobSetup & rSetup,
248                                sal_uInt16 nAspect )
249 {
250     Rectangle aVisArea  = GetVisArea( nAspect );
251     // MapUnit des Ziels
252     MapMode aMapMode( GetMapUnit() );
253     aMapMode.SetScaleX( rScaleX );
254     aMapMode.SetScaleY( rScaleY );
255 
256     // Ziel in Pixel
257     Point aOrg   = pDev->LogicToLogic( rViewPos, NULL, &aMapMode );
258     Point aDelta = aOrg - aVisArea.TopLeft();
259 
260     // Origin entsprechend zum sichtbaren Bereich verschieben
261     // Origin mit Scale setzen
262     aMapMode.SetOrigin( aDelta );
263 
264     // Deviceeinstellungen sichern
265     pDev->Push();
266 
267     Region aRegion;
268     if( pDev->IsClipRegion() && pDev->GetOutDevType() != OUTDEV_PRINTER )
269     {
270         aRegion = pDev->GetClipRegion();
271         aRegion = pDev->LogicToPixel( aRegion );
272     }
273     pDev->SetRelativeMapMode( aMapMode );
274 
275     GDIMetaFile * pMtf = pDev->GetConnectMetaFile();
276     if( pMtf )
277     {
278         if( pMtf->IsRecord() && pDev->GetOutDevType() != OUTDEV_PRINTER )
279             pMtf->Stop();
280         else
281             pMtf = NULL;
282     }
283 // #ifndef UNX
284     if( pDev->IsClipRegion() && pDev->GetOutDevType() != OUTDEV_PRINTER )
285 // #endif
286     {
287         aRegion = pDev->PixelToLogic( aRegion );
288         pDev->SetClipRegion( aRegion );
289     }
290     if( pMtf )
291         pMtf->Record( pDev );
292 
293 //REMOVE        SvOutPlaceObjectRef xOutRef( this );
294 //REMOVE        if ( xOutRef.Is() )
295 //REMOVE            xOutRef->DrawObject( pDev, rSetup, rSize, nAspect );
296 //REMOVE        else
297         Draw( pDev, rSetup, nAspect );
298 //REMOVE        DrawHatch( pDev, aVisArea.TopLeft(), aVisArea.GetSize() );
299 
300     // Deviceeinstellungen wieder herstellen
301     pDev->Pop();
302 
303 }
304 
305 comphelper::EmbeddedObjectContainer& SfxObjectShell::GetEmbeddedObjectContainer() const
306 {
307     if ( !pImp->mpObjectContainer )
308         pImp->mpObjectContainer = new comphelper::EmbeddedObjectContainer( ((SfxObjectShell*)this)->GetStorage(), GetModel() );
309     return *pImp->mpObjectContainer;
310 }
311 
312 void SfxObjectShell::ClearEmbeddedObjects()
313 {
314     // frees alle space taken by embedded objects
315     DELETEZ( pImp->mpObjectContainer );
316 }
317 
318