LibreOffice Module sfx2 (master) 1
iframe.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 <com/sun/star/awt/XVclWindowPeer.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/task/InteractionHandler.hpp>
28#include <com/sun/star/util/URLTransformer.hpp>
29#include <com/sun/star/util/XURLTransformer.hpp>
30#include <com/sun/star/util/XCloseable.hpp>
31#include <com/sun/star/lang/XEventListener.hpp>
32#include <com/sun/star/lang/XServiceInfo.hpp>
33#include <com/sun/star/beans/XPropertySet.hpp>
34#include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
35#include <com/sun/star/embed/XEmbeddedObject.hpp>
36
40#include <officecfg/Office/Common.hxx>
41#include <svl/itemprop.hxx>
42#include <sfx2/docfile.hxx>
43#include <sfx2/frmdescr.hxx>
44#include <sfx2/objsh.hxx>
45#include <sfx2/sfxdlg.hxx>
47#include <utility>
48#include <vcl/window.hxx>
49#include <tools/debug.hxx>
50#include <macroloader.hxx>
51#include <eventsupplier.hxx>
52
53using namespace ::com::sun::star;
54
55namespace {
56
57class IFrameObject : public ::cppu::WeakImplHelper <
58 css::util::XCloseable,
59 css::lang::XEventListener,
60 css::frame::XSynchronousFrameLoader,
61 css::ui::dialogs::XExecutableDialog,
62 css::lang::XServiceInfo,
63 css::beans::XPropertySet >
64{
65 css::uno::Reference < css::uno::XComponentContext > mxContext;
66 css::uno::Reference < css::frame::XFrame2 > mxFrame;
67 css::uno::Reference < css::embed::XEmbeddedObject > mxObj;
68 SfxItemPropertyMap maPropMap;
69 SfxFrameDescriptor maFrmDescr;
70
71public:
74 IFrameObject(css::uno::Reference < css::uno::XComponentContext> xContext, const css::uno::Sequence< css::uno::Any >& aArguments);
75
76 virtual OUString SAL_CALL getImplementationName() override
77 {
78 return "com.sun.star.comp.sfx2.IFrameObject";
79 }
80
81 virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
82 {
83 return cppu::supportsService(this, ServiceName);
84 }
85
86 virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
87 {
88 css::uno::Sequence< OUString > aSeq { "com.sun.star.frame.SpecialEmbeddedObject" };
89 return aSeq;
90 }
91
92 virtual sal_Bool SAL_CALL load( const css::uno::Sequence < css::beans::PropertyValue >& lDescriptor,
93 const css::uno::Reference < css::frame::XFrame >& xFrame ) override;
94 virtual void SAL_CALL cancel() override;
95 virtual void SAL_CALL close( sal_Bool bDeliverOwnership ) override;
96 virtual void SAL_CALL addCloseListener( const css::uno::Reference < css::util::XCloseListener >& xListener ) override;
97 virtual void SAL_CALL removeCloseListener( const css::uno::Reference < css::util::XCloseListener >& xListener ) override;
98 virtual void SAL_CALL disposing( const css::lang::EventObject& aEvent ) override ;
99 virtual void SAL_CALL setTitle( const OUString& aTitle ) override;
100 virtual ::sal_Int16 SAL_CALL execute( ) override;
101 virtual css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo() override;
102 virtual void SAL_CALL addPropertyChangeListener(const OUString& aPropertyName, const css::uno::Reference< css::beans::XPropertyChangeListener > & aListener) override;
103 virtual void SAL_CALL removePropertyChangeListener(const OUString& aPropertyName, const css::uno::Reference< css::beans::XPropertyChangeListener > & aListener) override;
104 virtual void SAL_CALL addVetoableChangeListener(const OUString& aPropertyName, const css::uno::Reference< css::beans::XVetoableChangeListener > & aListener) override;
105 virtual void SAL_CALL removeVetoableChangeListener(const OUString& aPropertyName, const css::uno::Reference< css::beans::XVetoableChangeListener > & aListener) override;
106 virtual void SAL_CALL setPropertyValue( const OUString& aPropertyName, const css::uno::Any& aValue ) override;
107 virtual css::uno::Any SAL_CALL getPropertyValue( const OUString& PropertyName ) override;
108};
109
110class IFrameWindow_Impl : public vcl::Window
111{
112public:
113 IFrameWindow_Impl( vcl::Window *pParent, bool bHasBorder );
114};
115
116IFrameWindow_Impl::IFrameWindow_Impl( vcl::Window *pParent, bool bHasBorder )
118{
119 if ( !bHasBorder )
120 SetBorderStyle( WindowBorderStyle::NOBORDER );
121 else
122 SetBorderStyle( WindowBorderStyle::NORMAL );
123}
124
125#define PROPERTY_UNBOUND 0
126
127#define WID_FRAME_URL 1
128#define WID_FRAME_NAME 2
129#define WID_FRAME_IS_AUTO_SCROLL 3
130#define WID_FRAME_IS_SCROLLING_MODE 4
131#define WID_FRAME_IS_BORDER 5
132#define WID_FRAME_IS_AUTO_BORDER 6
133#define WID_FRAME_MARGIN_WIDTH 7
134#define WID_FRAME_MARGIN_HEIGHT 8
135
136o3tl::span<const SfxItemPropertyMapEntry> lcl_GetIFramePropertyMap_Impl()
137{
138 static const SfxItemPropertyMapEntry aIFramePropertyMap_Impl[] =
139 {
148 };
149 return aIFramePropertyMap_Impl;
150}
151
152IFrameObject::IFrameObject(uno::Reference < uno::XComponentContext > xContext, const css::uno::Sequence< css::uno::Any >& aArguments)
153 : mxContext(std::move( xContext ))
154 , maPropMap( lcl_GetIFramePropertyMap_Impl() )
155{
156 if ( aArguments.hasElements() )
157 aArguments[0] >>= mxObj;
158}
159
160sal_Bool SAL_CALL IFrameObject::load(
161 const uno::Sequence < css::beans::PropertyValue >& /*lDescriptor*/,
162 const uno::Reference < frame::XFrame >& xFrame )
163{
164 if ( officecfg::Office::Common::Misc::PluginsEnabled::get() )
165 {
166 util::URL aTargetURL;
167 aTargetURL.Complete = maFrmDescr.GetURL().GetMainURL( INetURLObject::DecodeMechanism::NONE );
168 uno::Reference < util::XURLTransformer > xTrans( util::URLTransformer::create( mxContext ) );
169 xTrans->parseStrict( aTargetURL );
170
171 INetURLObject aURLObject(aTargetURL.Complete);
172 if (aURLObject.GetProtocol() == INetProtocol::Macro || aURLObject.isSchemeEqualTo(u"vnd.sun.star.script"))
173 return false;
174
175 uno::Reference<frame::XFramesSupplier> xParentFrame = xFrame->getCreator();
177
178 const bool bIsFactoryURL = aTargetURL.Complete.startsWith("private:factory/");
179 if (!bIsFactoryURL)
180 {
181 bool bUpdateAllowed(true);
182 if (pDoc)
183 {
184 comphelper::EmbeddedObjectContainer& rEmbeddedObjectContainer = pDoc->getEmbeddedObjectContainer();
185 bUpdateAllowed = rEmbeddedObjectContainer.getUserAllowsLinkUpdate();
186 }
187 if (!bUpdateAllowed)
188 return false;
189 }
190
191 OUString sReferer;
192 if (pDoc && pDoc->HasName())
193 sReferer = pDoc->GetMedium()->GetName();
194
195 uno::Reference<css::awt::XWindow> xParentWindow(xFrame->getContainerWindow());
196
197 if (!mxFrame.is())
198 {
199 VclPtr<vcl::Window> pParent = VCLUnoHelper::GetWindow(xParentWindow);
200 VclPtr<IFrameWindow_Impl> pWin = VclPtr<IFrameWindow_Impl>::Create( pParent, maFrmDescr.IsFrameBorderOn() );
201 pWin->SetSizePixel( pParent->GetOutputSizePixel() );
202 pWin->SetBackground();
203 pWin->Show();
204
205 uno::Reference < awt::XWindow > xWindow( pWin->GetComponentInterface(), uno::UNO_QUERY );
206 xFrame->setComponent( xWindow, uno::Reference < frame::XController >() );
207
208 // we must destroy the IFrame before the parent is destroyed
209 xWindow->addEventListener( this );
210
211 mxFrame = frame::Frame::create( mxContext );
212 uno::Reference < awt::XWindow > xWin( pWin->GetComponentInterface(), uno::UNO_QUERY );
213 mxFrame->initialize( xWin );
214 mxFrame->setName( maFrmDescr.GetName() );
215
216 uno::Reference < frame::XFramesSupplier > xFramesSupplier( xFrame, uno::UNO_QUERY );
217 if ( xFramesSupplier.is() )
218 mxFrame->setCreator( xFramesSupplier );
219 }
220
221 uno::Reference<task::XInteractionHandler> xInteractionHandler(task::InteractionHandler::createWithParent(mxContext, xParentWindow));
222 uno::Sequence < beans::PropertyValue > aProps{
223 comphelper::makePropertyValue("PluginMode", sal_Int16(2)),
224 comphelper::makePropertyValue("ReadOnly", true),
225 comphelper::makePropertyValue("InteractionHandler", xInteractionHandler),
226 comphelper::makePropertyValue("Referer", sReferer)
227 };
228 uno::Reference < frame::XDispatch > xDisp = mxFrame->queryDispatch( aTargetURL, "_self", 0 );
229 if ( xDisp.is() )
230 xDisp->dispatch( aTargetURL, aProps );
231
232 return true;
233 }
234
235 return false;
236}
237
238void SAL_CALL IFrameObject::cancel()
239{
240 try
241 {
242 uno::Reference < util::XCloseable > xClose( mxFrame, uno::UNO_QUERY );
243 if ( xClose.is() )
244 xClose->close( true );
245 mxFrame = nullptr;
246 }
247 catch (const uno::Exception&)
248 {
249 }
250}
251
252void SAL_CALL IFrameObject::close( sal_Bool /*bDeliverOwnership*/ )
253{
254}
255
256void SAL_CALL IFrameObject::addCloseListener( const css::uno::Reference < css::util::XCloseListener >& )
257{
258}
259
260void SAL_CALL IFrameObject::removeCloseListener( const css::uno::Reference < css::util::XCloseListener >& )
261{
262}
263
264void SAL_CALL IFrameObject::disposing( const css::lang::EventObject& )
265{
266 cancel();
267}
268
269uno::Reference< beans::XPropertySetInfo > SAL_CALL IFrameObject::getPropertySetInfo()
270{
271 static uno::Reference< beans::XPropertySetInfo > xInfo = new SfxItemPropertySetInfo( maPropMap );
272 return xInfo;
273}
274
275void SAL_CALL IFrameObject::setPropertyValue(const OUString& aPropertyName, const uno::Any& aAny)
276{
277 const SfxItemPropertyMapEntry* pEntry = maPropMap.getByName( aPropertyName );
278 if( !pEntry )
279 throw beans::UnknownPropertyException(aPropertyName);
280 switch( pEntry->nWID )
281 {
282 case WID_FRAME_URL:
283 {
284 OUString aURL;
285 aAny >>= aURL;
286 maFrmDescr.SetURL( aURL );
287 }
288 break;
289 case WID_FRAME_NAME:
290 {
291 OUString aName;
292 if ( aAny >>= aName )
293 maFrmDescr.SetName( aName );
294 }
295 break;
297 {
298 bool bIsAutoScroll;
299 if ( (aAny >>= bIsAutoScroll) && bIsAutoScroll )
300 maFrmDescr.SetScrollingMode( ScrollingMode::Auto );
301 }
302 break;
304 {
305 bool bIsScroll;
306 if ( aAny >>= bIsScroll )
307 maFrmDescr.SetScrollingMode( bIsScroll ? ScrollingMode::Yes : ScrollingMode::No );
308 }
309 break;
311 {
312 bool bIsBorder;
313 if ( aAny >>= bIsBorder )
314 maFrmDescr.SetFrameBorder( bIsBorder );
315 }
316 break;
318 {
319 bool bIsAutoBorder;
320 if ( aAny >>= bIsAutoBorder )
321 {
322 bool bBorder = maFrmDescr.IsFrameBorderOn();
323 maFrmDescr.ResetBorder();
324 if ( bIsAutoBorder )
325 maFrmDescr.SetFrameBorder( bBorder );
326 }
327 }
328 break;
330 {
331 sal_Int32 nMargin = 0;
332 Size aSize = maFrmDescr.GetMargin();
333 if ( aAny >>= nMargin )
334 {
335 aSize.setWidth( nMargin );
336 maFrmDescr.SetMargin( aSize );
337 }
338 }
339 break;
341 {
342 sal_Int32 nMargin = 0;
343 Size aSize = maFrmDescr.GetMargin();
344 if ( aAny >>= nMargin )
345 {
346 aSize.setHeight( nMargin );
347 maFrmDescr.SetMargin( aSize );
348 }
349 }
350 break;
351 default: ;
352 }
353}
354
355uno::Any SAL_CALL IFrameObject::getPropertyValue(const OUString& aPropertyName)
356{
357 const SfxItemPropertyMapEntry* pEntry = maPropMap.getByName( aPropertyName );
358 if( !pEntry )
359 throw beans::UnknownPropertyException(aPropertyName);
360 uno::Any aAny;
361 switch( pEntry->nWID )
362 {
363 case WID_FRAME_URL:
364 {
365 aAny <<= maFrmDescr.GetURL().GetMainURL( INetURLObject::DecodeMechanism::NONE );
366 }
367 break;
368 case WID_FRAME_NAME:
369 {
370 aAny <<= maFrmDescr.GetName();
371 }
372 break;
374 {
375 bool bIsAutoScroll = ( maFrmDescr.GetScrollingMode() == ScrollingMode::Auto );
376 aAny <<= bIsAutoScroll;
377 }
378 break;
380 {
381 bool bIsScroll = ( maFrmDescr.GetScrollingMode() == ScrollingMode::Yes );
382 aAny <<= bIsScroll;
383 }
384 break;
386 {
387 bool bIsBorder = maFrmDescr.IsFrameBorderOn();
388 aAny <<= bIsBorder;
389 }
390 break;
392 {
393 bool bIsAutoBorder = !maFrmDescr.IsFrameBorderSet();
394 aAny <<= bIsAutoBorder;
395 }
396 break;
398 {
399 aAny <<= static_cast<sal_Int32>(maFrmDescr.GetMargin().Width());
400 }
401 break;
403 {
404 aAny <<= static_cast<sal_Int32>(maFrmDescr.GetMargin().Height());
405 }
406 break;
407 default: ;
408 }
409 return aAny;
410}
411
412void SAL_CALL IFrameObject::addPropertyChangeListener(const OUString&, const css::uno::Reference< css::beans::XPropertyChangeListener > & )
413{
414}
415
416void SAL_CALL IFrameObject::removePropertyChangeListener(const OUString&, const css::uno::Reference< css::beans::XPropertyChangeListener > & )
417{
418}
419
420void SAL_CALL IFrameObject::addVetoableChangeListener(const OUString&, const css::uno::Reference< css::beans::XVetoableChangeListener > & )
421{
422}
423
424void SAL_CALL IFrameObject::removeVetoableChangeListener(const OUString&, const css::uno::Reference< css::beans::XVetoableChangeListener > & )
425{
426}
427
428::sal_Int16 SAL_CALL IFrameObject::execute()
429{
431 //we really should set a parent here
432 ScopedVclPtr<VclAbstractDialog> pDlg(pFact->CreateEditObjectDialog(nullptr, ".uno:InsertObjectFloatingFrame", mxObj));
433 pDlg->Execute();
434 return 0;
435}
436
437void SAL_CALL IFrameObject::setTitle( const OUString& )
438{
439}
440
441}
442
443extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
445 css::uno::XComponentContext *context,
446 css::uno::Sequence<css::uno::Any> const &arguments)
447{
448 return cppu::acquire(new IFrameObject(context, arguments));
449}
450
451/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual VclPtr< VclAbstractDialog > CreateEditObjectDialog(weld::Window *pParent, const OUString &rCommand, const css::uno::Reference< css::embed::XEmbeddedObject > &xObj)=0
static SfxAbstractDialogFactory * Create()
Definition: sfxdlg.cxx:22
static SfxObjectShell * GetObjectShell(const css::uno::Reference< css::frame::XFrame > &xFrame)
Definition: macroloader.cxx:71
const OUString & GetName() const
Definition: docfile.cxx:3592
bool HasName() const
Definition: objsh.hxx:266
SfxMedium * GetMedium() const
Definition: objsh.hxx:261
virtual comphelper::EmbeddedObjectContainer & getEmbeddedObjectContainer() const override
Definition: objsh.hxx:635
void setWidth(tools::Long nWidth)
void setHeight(tools::Long nHeight)
static vcl::Window * GetWindow(const css::uno::Reference< css::awt::XWindow > &rxWindow)
static VclPtr< reference_type > Create(Arg &&... arg)
css::uno::Type const & get()
URL aURL
bool close
uno::Reference< uno::XComponentContext > mxContext
float u
#define WID_FRAME_MARGIN_HEIGHT
Definition: iframe.cxx:134
#define PROPERTY_UNBOUND
Definition: iframe.cxx:125
#define WID_FRAME_IS_SCROLLING_MODE
Definition: iframe.cxx:130
#define WID_FRAME_IS_AUTO_SCROLL
Definition: iframe.cxx:129
#define WID_FRAME_MARGIN_WIDTH
Definition: iframe.cxx:133
#define WID_FRAME_IS_AUTO_BORDER
Definition: iframe.cxx:132
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_sfx2_IFrameObject_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &arguments)
Definition: iframe.cxx:444
#define WID_FRAME_IS_BORDER
Definition: iframe.cxx:131
#define WID_FRAME_URL
Definition: iframe.cxx:127
#define WID_FRAME_NAME
Definition: iframe.cxx:128
Sequence< PropertyValue > aArguments
OUString aName
Sequence< sal_Int8 > aSeq
Definition: lnkbase2.cxx:83
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
VBAHELPER_DLLPUBLIC bool setPropertyValue(css::uno::Sequence< css::beans::PropertyValue > &aProp, const OUString &aName, const css::uno::Any &aValue)
bool getPropertyValue(ValueType &rValue, css::uno::Reference< css::beans::XPropertySet > const &xPropSet, OUString const &propName)
Reference< XFrame > xFrame
unsigned char sal_Bool
OUString aTargetURL
WinBits const WB_NODIALOGCONTROL
WinBits const WB_CLIPCHILDREN