put floating frames under managed links control
[LibreOffice.git] / sfx2 / source / doc / iframe.cxx
blobe80f4d3cebf996bdc8e47635019e31885993b290
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 <sal/config.h>
22 #include <com/sun/star/awt/XWindowPeer.hpp>
23 #include <com/sun/star/frame/XDispatch.hpp>
24 #include <com/sun/star/frame/Frame.hpp>
25 #include <com/sun/star/frame/XFrame2.hpp>
26 #include <com/sun/star/frame/XSynchronousFrameLoader.hpp>
27 #include <com/sun/star/util/URLTransformer.hpp>
28 #include <com/sun/star/util/XURLTransformer.hpp>
29 #include <com/sun/star/util/XCloseable.hpp>
30 #include <com/sun/star/lang/XEventListener.hpp>
31 #include <com/sun/star/lang/XServiceInfo.hpp>
32 #include <com/sun/star/beans/XPropertySet.hpp>
33 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
34 #include <com/sun/star/embed/XEmbeddedObject.hpp>
36 #include <comphelper/propertyvalue.hxx>
37 #include <cppuhelper/implbase.hxx>
38 #include <cppuhelper/supportsservice.hxx>
39 #include <officecfg/Office/Common.hxx>
40 #include <svl/itemprop.hxx>
41 #include <sfx2/docfile.hxx>
42 #include <sfx2/frmdescr.hxx>
43 #include <sfx2/objsh.hxx>
44 #include <sfx2/sfxdlg.hxx>
45 #include <toolkit/helper/vclunohelper.hxx>
46 #include <vcl/window.hxx>
47 #include <tools/debug.hxx>
48 #include <macroloader.hxx>
50 using namespace ::com::sun::star;
52 namespace {
54 class IFrameObject : public ::cppu::WeakImplHelper <
55 css::util::XCloseable,
56 css::lang::XEventListener,
57 css::frame::XSynchronousFrameLoader,
58 css::ui::dialogs::XExecutableDialog,
59 css::lang::XServiceInfo,
60 css::beans::XPropertySet >
62 css::uno::Reference < css::uno::XComponentContext > mxContext;
63 css::uno::Reference < css::frame::XFrame2 > mxFrame;
64 css::uno::Reference < css::embed::XEmbeddedObject > mxObj;
65 SfxItemPropertyMap maPropMap;
66 SfxFrameDescriptor maFrmDescr;
68 public:
69 /// @throws css::uno::Exception
70 /// @throws css::uno::RuntimeException
71 IFrameObject(const css::uno::Reference < css::uno::XComponentContext>& rxContext, const css::uno::Sequence< css::uno::Any >& aArguments);
73 virtual OUString SAL_CALL getImplementationName() override
75 return "com.sun.star.comp.sfx2.IFrameObject";
78 virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
80 return cppu::supportsService(this, ServiceName);
83 virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
85 css::uno::Sequence< OUString > aSeq { "com.sun.star.frame.SpecialEmbeddedObject" };
86 return aSeq;
89 virtual sal_Bool SAL_CALL load( const css::uno::Sequence < css::beans::PropertyValue >& lDescriptor,
90 const css::uno::Reference < css::frame::XFrame >& xFrame ) override;
91 virtual void SAL_CALL cancel() override;
92 virtual void SAL_CALL close( sal_Bool bDeliverOwnership ) override;
93 virtual void SAL_CALL addCloseListener( const css::uno::Reference < css::util::XCloseListener >& xListener ) override;
94 virtual void SAL_CALL removeCloseListener( const css::uno::Reference < css::util::XCloseListener >& xListener ) override;
95 virtual void SAL_CALL disposing( const css::lang::EventObject& aEvent ) override ;
96 virtual void SAL_CALL setTitle( const OUString& aTitle ) override;
97 virtual ::sal_Int16 SAL_CALL execute( ) override;
98 virtual css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo() override;
99 virtual void SAL_CALL addPropertyChangeListener(const OUString& aPropertyName, const css::uno::Reference< css::beans::XPropertyChangeListener > & aListener) override;
100 virtual void SAL_CALL removePropertyChangeListener(const OUString& aPropertyName, const css::uno::Reference< css::beans::XPropertyChangeListener > & aListener) override;
101 virtual void SAL_CALL addVetoableChangeListener(const OUString& aPropertyName, const css::uno::Reference< css::beans::XVetoableChangeListener > & aListener) override;
102 virtual void SAL_CALL removeVetoableChangeListener(const OUString& aPropertyName, const css::uno::Reference< css::beans::XVetoableChangeListener > & aListener) override;
103 virtual void SAL_CALL setPropertyValue( const OUString& aPropertyName, const css::uno::Any& aValue ) override;
104 virtual css::uno::Any SAL_CALL getPropertyValue( const OUString& PropertyName ) override;
107 class IFrameWindow_Impl : public vcl::Window
109 public:
110 IFrameWindow_Impl( vcl::Window *pParent, bool bHasBorder );
113 IFrameWindow_Impl::IFrameWindow_Impl( vcl::Window *pParent, bool bHasBorder )
114 : Window( pParent, WB_CLIPCHILDREN | WB_NODIALOGCONTROL )
116 if ( !bHasBorder )
117 SetBorderStyle( WindowBorderStyle::NOBORDER );
118 else
119 SetBorderStyle( WindowBorderStyle::NORMAL );
122 #define PROPERTY_UNBOUND 0
124 #define WID_FRAME_URL 1
125 #define WID_FRAME_NAME 2
126 #define WID_FRAME_IS_AUTO_SCROLL 3
127 #define WID_FRAME_IS_SCROLLING_MODE 4
128 #define WID_FRAME_IS_BORDER 5
129 #define WID_FRAME_IS_AUTO_BORDER 6
130 #define WID_FRAME_MARGIN_WIDTH 7
131 #define WID_FRAME_MARGIN_HEIGHT 8
133 const SfxItemPropertyMapEntry* lcl_GetIFramePropertyMap_Impl()
135 static const SfxItemPropertyMapEntry aIFramePropertyMap_Impl[] =
137 { u"FrameIsAutoBorder", WID_FRAME_IS_AUTO_BORDER, cppu::UnoType<bool>::get(), PROPERTY_UNBOUND, 0 },
138 { u"FrameIsAutoScroll", WID_FRAME_IS_AUTO_SCROLL, cppu::UnoType<bool>::get(), PROPERTY_UNBOUND, 0 },
139 { u"FrameIsBorder", WID_FRAME_IS_BORDER, cppu::UnoType<bool>::get(), PROPERTY_UNBOUND, 0 },
140 { u"FrameIsScrollingMode", WID_FRAME_IS_SCROLLING_MODE,cppu::UnoType<bool>::get(), PROPERTY_UNBOUND, 0 },
141 { u"FrameMarginHeight", WID_FRAME_MARGIN_HEIGHT, cppu::UnoType<sal_Int32>::get(), PROPERTY_UNBOUND, 0 },
142 { u"FrameMarginWidth", WID_FRAME_MARGIN_WIDTH, cppu::UnoType<sal_Int32>::get(), PROPERTY_UNBOUND, 0 },
143 { u"FrameName", WID_FRAME_NAME, cppu::UnoType<OUString>::get(), PROPERTY_UNBOUND, 0 },
144 { u"FrameURL", WID_FRAME_URL, cppu::UnoType<OUString>::get(), PROPERTY_UNBOUND, 0 },
145 { u"", 0, css::uno::Type(), 0, 0 }
147 return aIFramePropertyMap_Impl;
150 IFrameObject::IFrameObject(const uno::Reference < uno::XComponentContext >& rxContext, const css::uno::Sequence< css::uno::Any >& aArguments)
151 : mxContext( rxContext )
152 , maPropMap( lcl_GetIFramePropertyMap_Impl() )
154 if ( aArguments.hasElements() )
155 aArguments[0] >>= mxObj;
158 sal_Bool SAL_CALL IFrameObject::load(
159 const uno::Sequence < css::beans::PropertyValue >& /*lDescriptor*/,
160 const uno::Reference < frame::XFrame >& xFrame )
162 if ( officecfg::Office::Common::Misc::PluginsEnabled::get() )
164 util::URL aTargetURL;
165 aTargetURL.Complete = maFrmDescr.GetURL().GetMainURL( INetURLObject::DecodeMechanism::NONE );
166 uno::Reference < util::XURLTransformer > xTrans( util::URLTransformer::create( mxContext ) );
167 xTrans->parseStrict( aTargetURL );
169 uno::Reference<frame::XFramesSupplier> xParentFrame = xFrame->getCreator();
170 SfxObjectShell* pDoc = SfxMacroLoader::GetObjectShell(xParentFrame);
172 if (INetURLObject(aTargetURL.Complete).GetProtocol() == INetProtocol::Macro)
174 if (pDoc && !pDoc->AdjustMacroMode())
175 return false;
178 bool bUpdateAllowed(true);
179 if (pDoc)
181 // perhaps should only check for file targets, but lets default to making it strong
182 // unless there is a known need to distinguish
183 comphelper::EmbeddedObjectContainer& rEmbeddedObjectContainer = pDoc->getEmbeddedObjectContainer();
184 bUpdateAllowed = rEmbeddedObjectContainer.getUserAllowsLinkUpdate();
186 if (!bUpdateAllowed)
187 return false;
189 OUString sReferer;
190 if (pDoc && pDoc->HasName())
191 sReferer = pDoc->GetMedium()->GetName();
193 uno::Reference<css::awt::XWindow> xParentWindow(xFrame->getContainerWindow());
195 if (!mxFrame.is())
197 VclPtr<vcl::Window> pParent = VCLUnoHelper::GetWindow(xParentWindow);
198 VclPtr<IFrameWindow_Impl> pWin = VclPtr<IFrameWindow_Impl>::Create( pParent, maFrmDescr.IsFrameBorderOn() );
199 pWin->SetSizePixel( pParent->GetOutputSizePixel() );
200 pWin->SetBackground();
201 pWin->Show();
203 uno::Reference < awt::XWindow > xWindow( pWin->GetComponentInterface(), uno::UNO_QUERY );
204 xFrame->setComponent( xWindow, uno::Reference < frame::XController >() );
206 // we must destroy the IFrame before the parent is destroyed
207 xWindow->addEventListener( this );
209 mxFrame = frame::Frame::create( mxContext );
210 uno::Reference < awt::XWindow > xWin( pWin->GetComponentInterface(), uno::UNO_QUERY );
211 mxFrame->initialize( xWin );
212 mxFrame->setName( maFrmDescr.GetName() );
214 uno::Reference < frame::XFramesSupplier > xFramesSupplier( xFrame, uno::UNO_QUERY );
215 if ( xFramesSupplier.is() )
216 mxFrame->setCreator( xFramesSupplier );
219 uno::Sequence < beans::PropertyValue > aProps{
220 comphelper::makePropertyValue("PluginMode", sal_Int16(2)),
221 comphelper::makePropertyValue("ReadOnly", true),
222 comphelper::makePropertyValue("Referer", sReferer)
224 uno::Reference < frame::XDispatch > xDisp = mxFrame->queryDispatch( aTargetURL, "_self", 0 );
225 if ( xDisp.is() )
226 xDisp->dispatch( aTargetURL, aProps );
228 return true;
231 return false;
234 void SAL_CALL IFrameObject::cancel()
238 uno::Reference < util::XCloseable > xClose( mxFrame, uno::UNO_QUERY );
239 if ( xClose.is() )
240 xClose->close( true );
241 mxFrame = nullptr;
243 catch (const uno::Exception&)
248 void SAL_CALL IFrameObject::close( sal_Bool /*bDeliverOwnership*/ )
252 void SAL_CALL IFrameObject::addCloseListener( const css::uno::Reference < css::util::XCloseListener >& )
256 void SAL_CALL IFrameObject::removeCloseListener( const css::uno::Reference < css::util::XCloseListener >& )
260 void SAL_CALL IFrameObject::disposing( const css::lang::EventObject& )
262 cancel();
265 uno::Reference< beans::XPropertySetInfo > SAL_CALL IFrameObject::getPropertySetInfo()
267 static uno::Reference< beans::XPropertySetInfo > xInfo = new SfxItemPropertySetInfo( maPropMap );
268 return xInfo;
271 void SAL_CALL IFrameObject::setPropertyValue(const OUString& aPropertyName, const uno::Any& aAny)
273 const SfxItemPropertyMapEntry* pEntry = maPropMap.getByName( aPropertyName );
274 if( !pEntry )
275 throw beans::UnknownPropertyException(aPropertyName);
276 switch( pEntry->nWID )
278 case WID_FRAME_URL:
280 OUString aURL;
281 aAny >>= aURL;
282 maFrmDescr.SetURL( aURL );
284 break;
285 case WID_FRAME_NAME:
287 OUString aName;
288 if ( aAny >>= aName )
289 maFrmDescr.SetName( aName );
291 break;
292 case WID_FRAME_IS_AUTO_SCROLL:
294 bool bIsAutoScroll;
295 if ( (aAny >>= bIsAutoScroll) && bIsAutoScroll )
296 maFrmDescr.SetScrollingMode( ScrollingMode::Auto );
298 break;
299 case WID_FRAME_IS_SCROLLING_MODE:
301 bool bIsScroll;
302 if ( aAny >>= bIsScroll )
303 maFrmDescr.SetScrollingMode( bIsScroll ? ScrollingMode::Yes : ScrollingMode::No );
305 break;
306 case WID_FRAME_IS_BORDER:
308 bool bIsBorder;
309 if ( aAny >>= bIsBorder )
310 maFrmDescr.SetFrameBorder( bIsBorder );
312 break;
313 case WID_FRAME_IS_AUTO_BORDER:
315 bool bIsAutoBorder;
316 if ( aAny >>= bIsAutoBorder )
318 bool bBorder = maFrmDescr.IsFrameBorderOn();
319 maFrmDescr.ResetBorder();
320 if ( bIsAutoBorder )
321 maFrmDescr.SetFrameBorder( bBorder );
324 break;
325 case WID_FRAME_MARGIN_WIDTH:
327 sal_Int32 nMargin = 0;
328 Size aSize = maFrmDescr.GetMargin();
329 if ( aAny >>= nMargin )
331 aSize.setWidth( nMargin );
332 maFrmDescr.SetMargin( aSize );
335 break;
336 case WID_FRAME_MARGIN_HEIGHT:
338 sal_Int32 nMargin = 0;
339 Size aSize = maFrmDescr.GetMargin();
340 if ( aAny >>= nMargin )
342 aSize.setHeight( nMargin );
343 maFrmDescr.SetMargin( aSize );
346 break;
347 default: ;
351 uno::Any SAL_CALL IFrameObject::getPropertyValue(const OUString& aPropertyName)
353 const SfxItemPropertyMapEntry* pEntry = maPropMap.getByName( aPropertyName );
354 if( !pEntry )
355 throw beans::UnknownPropertyException(aPropertyName);
356 uno::Any aAny;
357 switch( pEntry->nWID )
359 case WID_FRAME_URL:
361 aAny <<= maFrmDescr.GetURL().GetMainURL( INetURLObject::DecodeMechanism::NONE );
363 break;
364 case WID_FRAME_NAME:
366 aAny <<= maFrmDescr.GetName();
368 break;
369 case WID_FRAME_IS_AUTO_SCROLL:
371 bool bIsAutoScroll = ( maFrmDescr.GetScrollingMode() == ScrollingMode::Auto );
372 aAny <<= bIsAutoScroll;
374 break;
375 case WID_FRAME_IS_SCROLLING_MODE:
377 bool bIsScroll = ( maFrmDescr.GetScrollingMode() == ScrollingMode::Yes );
378 aAny <<= bIsScroll;
380 break;
381 case WID_FRAME_IS_BORDER:
383 bool bIsBorder = maFrmDescr.IsFrameBorderOn();
384 aAny <<= bIsBorder;
386 break;
387 case WID_FRAME_IS_AUTO_BORDER:
389 bool bIsAutoBorder = !maFrmDescr.IsFrameBorderSet();
390 aAny <<= bIsAutoBorder;
392 break;
393 case WID_FRAME_MARGIN_WIDTH:
395 aAny <<= static_cast<sal_Int32>(maFrmDescr.GetMargin().Width());
397 break;
398 case WID_FRAME_MARGIN_HEIGHT:
400 aAny <<= static_cast<sal_Int32>(maFrmDescr.GetMargin().Height());
402 break;
403 default: ;
405 return aAny;
408 void SAL_CALL IFrameObject::addPropertyChangeListener(const OUString&, const css::uno::Reference< css::beans::XPropertyChangeListener > & )
412 void SAL_CALL IFrameObject::removePropertyChangeListener(const OUString&, const css::uno::Reference< css::beans::XPropertyChangeListener > & )
416 void SAL_CALL IFrameObject::addVetoableChangeListener(const OUString&, const css::uno::Reference< css::beans::XVetoableChangeListener > & )
420 void SAL_CALL IFrameObject::removeVetoableChangeListener(const OUString&, const css::uno::Reference< css::beans::XVetoableChangeListener > & )
424 ::sal_Int16 SAL_CALL IFrameObject::execute()
426 SfxAbstractDialogFactory* pFact = SfxAbstractDialogFactory::Create();
427 //we really should set a parent here
428 ScopedVclPtr<VclAbstractDialog> pDlg(pFact->CreateEditObjectDialog(nullptr, ".uno:InsertObjectFloatingFrame", mxObj));
429 pDlg->Execute();
430 return 0;
433 void SAL_CALL IFrameObject::setTitle( const OUString& )
439 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
440 com_sun_star_comp_sfx2_IFrameObject_get_implementation(
441 css::uno::XComponentContext *context,
442 css::uno::Sequence<css::uno::Any> const &arguments)
444 return cppu::acquire(new IFrameObject(context, arguments));
447 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */