LibreOffice Module UnoControls (master)  1
framecontrol.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 <framecontrol.hxx>
22 
23 #include <com/sun/star/awt/XControlContainer.hpp>
24 #include <com/sun/star/beans/PropertyAttribute.hpp>
25 #include <com/sun/star/frame/Frame.hpp>
26 #include <com/sun/star/frame/FrameSearchFlag.hpp>
27 #include <com/sun/star/frame/XDispatch.hpp>
28 #include <com/sun/star/util/URLTransformer.hpp>
29 #include <com/sun/star/util/XURLTransformer.hpp>
32 #include <osl/diagnose.h>
33 
34 // namespaces
35 
36 using namespace ::osl;
37 using namespace ::cppu;
38 using namespace ::com::sun::star::uno;
39 using namespace ::com::sun::star::lang;
40 using namespace ::com::sun::star::beans;
41 using namespace ::com::sun::star::awt;
42 using namespace ::com::sun::star::frame;
43 using namespace ::com::sun::star::util;
44 
45 namespace unocontrols {
46 
47 namespace {
48 
49 enum PropertyHandle // values represent index in PropertyArray
50 { // for FrameControl
51  Componenturl = 0,
52  Frame = 1,
53  Loaderarguments = 2
54 };
55 
56 }
57 
58 // construct/destruct
59 
61  : BaseControl ( rxContext )
63  , OPropertySetHelper ( *static_cast< OBroadcastHelper * >(this) )
64  , m_aConnectionPointContainer ( new OConnectionPointContainerHelper(m_aMutex) )
65 {
66 }
67 
69 {
70 }
71 
72 // XInterface
73 
74 Any SAL_CALL FrameControl::queryInterface( const Type& rType )
75 {
76  // Attention:
77  // Don't use mutex or guard in this method!!! Is a method of XInterface.
78  Any aReturn;
80  if ( xDel.is() )
81  {
82  // If a delegator exists, forward question to its queryInterface.
83  // Delegator will ask its own queryAggregation!
84  aReturn = xDel->queryInterface( rType );
85  }
86  else
87  {
88  // If a delegator is unknown, forward question to own queryAggregation.
89  aReturn = queryAggregation( rType );
90  }
91 
92  return aReturn;
93 }
94 
95 // XInterface
96 
97 void SAL_CALL FrameControl::acquire() throw()
98 {
99  // Attention:
100  // Don't use mutex or guard in this method!!! Is a method of XInterface.
101 
102  // Forward to baseclass
104 }
105 
106 // XInterface
107 
108 void SAL_CALL FrameControl::release() throw()
109 {
110  // Attention:
111  // Don't use mutex or guard in this method!!! Is a method of XInterface.
112 
113  // Forward to baseclass
115 }
116 
117 // XTypeProvider
118 
119 Sequence< Type > SAL_CALL FrameControl::getTypes()
120 {
121  static OTypeCollection ourTypeCollection(
126 
127  return ourTypeCollection.getTypes();
128 }
129 
130 // XAggregation
131 
132 Any SAL_CALL FrameControl::queryAggregation( const Type& aType )
133 {
134  // Ask for my own supported interfaces ...
135  // Attention: XTypeProvider and XInterface are supported by OComponentHelper!
136  Any aReturn ( ::cppu::queryInterface( aType ,
137  static_cast< XControlModel* > ( this ) ,
138  static_cast< XConnectionPointContainer* > ( this )
139  )
140  );
141 
142  // If searched interface not supported by this class ...
143  if ( !aReturn.hasValue() )
144  {
145  // ... ask baseclasses.
146  aReturn = OPropertySetHelper::queryInterface( aType );
147  if ( !aReturn.hasValue() )
148  {
149  aReturn = BaseControl::queryAggregation( aType );
150  }
151  }
152 
153  return aReturn;
154 }
155 
157 {
158  return "stardiv.UnoControls.FrameControl";
159 }
160 
161 css::uno::Sequence<OUString> FrameControl::getSupportedServiceNames()
162 {
163  return { "com.sun.star.frame.FrameControl" };
164 }
165 
166 // XControl
167 
168 void SAL_CALL FrameControl::createPeer( const Reference< XToolkit >& xToolkit ,
169  const Reference< XWindowPeer >& xParentPeer )
170 {
171  BaseControl::createPeer( xToolkit, xParentPeer );
172  if ( impl_getPeerWindow().is() )
173  {
174  if( !m_sComponentURL.isEmpty() )
175  {
177  }
178  }
179 }
180 
181 // XControl
182 
183 sal_Bool SAL_CALL FrameControl::setModel( const Reference< XControlModel >& /*xModel*/ )
184 {
185  // We have no model.
186  return false;
187 }
188 
189 // XControl
190 
191 Reference< XControlModel > SAL_CALL FrameControl::getModel()
192 {
193  // We have no model.
194  return Reference< XControlModel >();
195 }
196 
197 // XControl
198 
199 void SAL_CALL FrameControl::dispose()
200 {
203 }
204 
205 // XView
206 
207 sal_Bool SAL_CALL FrameControl::setGraphics( const Reference< XGraphics >& /*xDevice*/ )
208 {
209  // it is not possible to print this control
210  return false;
211 }
212 
213 // XView
214 
215 Reference< XGraphics > SAL_CALL FrameControl::getGraphics()
216 {
217  // when it's not possible to set graphics ! then it's possible to return null
218  return Reference< XGraphics >();
219 }
220 
221 // XConnectionPointContainer
222 
223 Sequence< Type > SAL_CALL FrameControl::getConnectionPointTypes()
224 {
225  // Forwarded to helper class
226  return m_aConnectionPointContainer->getConnectionPointTypes();
227 }
228 
229 // XConnectionPointContainer
230 
231 Reference< XConnectionPoint > SAL_CALL FrameControl::queryConnectionPoint( const Type& aType )
232 {
233  // Forwarded to helper class
234  return m_aConnectionPointContainer->queryConnectionPoint( aType );
235 }
236 
237 // XConnectionPointContainer
238 
239 void SAL_CALL FrameControl::advise( const Type& aType ,
240  const Reference< XInterface >& xListener )
241 {
242  // Forwarded to helper class
243  m_aConnectionPointContainer->advise( aType, xListener );
244 }
245 
246 // XConnectionPointContainer
247 
248 void SAL_CALL FrameControl::unadvise( const Type& aType ,
249  const Reference< XInterface >& xListener )
250 {
251  // Forwarded to helper class
252  m_aConnectionPointContainer->unadvise( aType, xListener );
253 }
254 
255 // OPropertySetHelper
256 
258  Any& rOldValue ,
259  sal_Int32 nHandle ,
260  const Any& rValue )
261 {
262  bool bReturn = false;
263  switch (nHandle)
264  {
265  case PropertyHandle::Componenturl : rConvertedValue = rValue;
266  rOldValue <<= m_sComponentURL;
267  bReturn = true;
268  break;
269 
270  case PropertyHandle::Loaderarguments : rConvertedValue = rValue;
271  rOldValue <<= m_seqLoaderArguments;
272  bReturn = true;
273  break;
274  }
275 
276  if ( !bReturn )
277  {
278  throw IllegalArgumentException();
279  }
280 
281  return bReturn;
282 }
283 
284 // OPropertySetHelper
285 
287  const Any& rValue )
288 {
289  // this method only set the value
290  MutexGuard aGuard (m_aMutex);
291  switch (nHandle)
292  {
293  case PropertyHandle::Componenturl : rValue >>= m_sComponentURL;
294  if (getPeer().is())
295  {
297  }
298  break;
299 
300  case PropertyHandle::Loaderarguments : rValue >>= m_seqLoaderArguments;
301  break;
302 
303  default : OSL_ENSURE ( nHandle == -1, "This is an invalid property handle." );
304  }
305 }
306 
307 // OPropertySetHelper
308 
310  sal_Int32 nHandle ) const
311 {
312  MutexGuard aGuard ( Mutex::getGlobalMutex() );
313 
314  switch (nHandle)
315  {
316  case PropertyHandle::Componenturl : rRet <<= m_sComponentURL;
317  break;
318 
319  case PropertyHandle::Loaderarguments : rRet <<= m_seqLoaderArguments;
320  break;
321 
322  case PropertyHandle::Frame : rRet <<= m_xFrame;
323  break;
324 
325  default : OSL_ENSURE ( nHandle == -1, "This is an invalid property handle." );
326  }
327 }
328 
329 // OPropertySetHelper
330 
331 IPropertyArrayHelper& FrameControl::getInfoHelper()
332 {
333  // Create a table that map names to index values.
334  // attention: properties need to be sorted by name!
335  static OPropertyArrayHelper ourPropertyInfo(
336  {
337  Property( "ComponentUrl", PropertyHandle::Componenturl, cppu::UnoType<OUString>::get(),
338  PropertyAttribute::BOUND | PropertyAttribute::CONSTRAINED ),
339  Property( "Frame", PropertyHandle::Frame, cppu::UnoType<XFrame>::get(),
340  PropertyAttribute::BOUND | PropertyAttribute::TRANSIENT ),
341  Property( "LoaderArguments", PropertyHandle::Loaderarguments, cppu::UnoType<Sequence<PropertyValue>>::get(),
342  PropertyAttribute::BOUND | PropertyAttribute::CONSTRAINED )
343  },
344  true );
345 
346  return ourPropertyInfo;
347 }
348 
349 // OPropertySetHelper
350 
351 Reference< XPropertySetInfo > SAL_CALL FrameControl::getPropertySetInfo()
352 {
353  // Create structure of propertysetinfo for baseclass "OPropertySetHelper".
354  // (Use method "getInfoHelper()".)
355  static Reference< XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
356 
357  return xInfo;
358 }
359 
360 // BaseControl
361 
362 WindowDescriptor FrameControl::impl_getWindowDescriptor( const Reference< XWindowPeer >& xParentPeer )
363 {
364  WindowDescriptor aDescriptor;
365 
366  aDescriptor.Type = WindowClass_CONTAINER;
367  aDescriptor.ParentIndex = -1;
368  aDescriptor.Parent = xParentPeer;
369  aDescriptor.Bounds = getPosSize ();
370  aDescriptor.WindowAttributes = 0;
371 
372  return aDescriptor;
373 }
374 
375 // private method
376 
377 void FrameControl::impl_createFrame( const Reference< XWindowPeer >& xPeer ,
378  const OUString& rURL ,
379  const Sequence< PropertyValue >& rArguments )
380 {
381  Reference< XFrame2 > xOldFrame;
382  Reference< XFrame2 > xNewFrame;
383 
384  {
385  MutexGuard aGuard ( m_aMutex );
386  xOldFrame = m_xFrame;
387  }
388 
389  xNewFrame = Frame::create( impl_getComponentContext() );
390 
391  Reference< XWindow > xWP ( xPeer, UNO_QUERY );
392  xNewFrame->initialize ( xWP );
393 
394  // option
395  //xFrame->setName( "WhatYouWant" );
396 
397  Reference< XURLTransformer > xTrans = URLTransformer::create( impl_getComponentContext() );
398  // load file
399  URL aURL;
400  aURL.Complete = rURL;
401  xTrans->parseStrict( aURL );
402 
403  Reference< XDispatch > xDisp = xNewFrame->queryDispatch ( aURL, OUString (), FrameSearchFlag::SELF );
404  if (xDisp.is())
405  {
406  xDisp->dispatch ( aURL, rArguments );
407  }
408 
409  // set the frame
410  {
411  MutexGuard aGuard ( m_aMutex );
412  m_xFrame = xNewFrame;
413  }
414 
415  // notify the listeners
416  sal_Int32 nFrameId = PropertyHandle::Frame;
417  Any aNewFrame ( &xNewFrame, cppu::UnoType<XFrame>::get());
418  Any aOldFrame ( &xOldFrame, cppu::UnoType<XFrame>::get());
419 
420  fire ( &nFrameId, &aNewFrame, &aOldFrame, 1, false );
421 
422  if (xOldFrame.is())
423  {
424  xOldFrame->dispose ();
425  }
426 }
427 
428 // private method
429 
431 {
432  Reference< XFrame2 > xOldFrame;
433  Reference< XFrame2 > xNullFrame;
434 
435  {
436  // do not dispose the frame in this guarded section (deadlock?)
437  MutexGuard aGuard( m_aMutex );
438  xOldFrame = m_xFrame;
439  m_xFrame.clear();
440  }
441 
442  // notify the listeners
443  sal_Int32 nFrameId = PropertyHandle::Frame;
444  Any aNewFrame( &xNullFrame, cppu::UnoType<XFrame2>::get());
445  Any aOldFrame( &xOldFrame, cppu::UnoType<XFrame2>::get());
446  fire( &nFrameId, &aNewFrame, &aOldFrame, 1, false );
447 
448  // dispose the frame
449  if( xOldFrame.is() )
450  xOldFrame->dispose();
451 }
452 
453 
454 } // namespace unocontrols
455 
456 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
458  css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
459 {
460  return cppu::acquire(new unocontrols::FrameControl(context));
461 }
462 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual css::uno::Reference< css::lang::XConnectionPoint > SAL_CALL queryConnectionPoint(const css::uno::Type &aType) override
css::uno::Sequence< css::beans::PropertyValue > m_seqLoaderArguments
Type
virtual ::cppu::IPropertyArrayHelper &SAL_CALL getInfoHelper() override
static css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL createPropertySetInfo(IPropertyArrayHelper &rProperties)
URL aURL
virtual void SAL_CALL createPeer(const css::uno::Reference< css::awt::XToolkit > &xToolkit, const css::uno::Reference< css::awt::XWindowPeer > &xParent) override
const css::uno::Reference< css::uno::XComponentContext > & impl_getComponentContext() const
osl::Mutex m_aMutex
void impl_createFrame(const css::uno::Reference< css::awt::XWindowPeer > &xPeer, const OUString &sURL, const css::uno::Sequence< css::beans::PropertyValue > &seqArguments)
css::uno::Any SAL_CALL queryAggregation(const css::uno::Type &aType) override
virtual void SAL_CALL dispose() override
virtual void SAL_CALL advise(const css::uno::Type &aType, const css::uno::Reference< css::uno::XInterface > &xListener) override
virtual ~FrameControl() override
virtual css::awt::WindowDescriptor impl_getWindowDescriptor(const css::uno::Reference< css::awt::XWindowPeer > &xParentPeer) override
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * stardiv_UnoControls_FrameControl_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
virtual void SAL_CALL createPeer(const css::uno::Reference< css::awt::XToolkit > &xToolkit, const css::uno::Reference< css::awt::XWindowPeer > &xParent) override
virtual void SAL_CALL dispose() override
rtl::Reference< OConnectionPointContainerHelper > m_aConnectionPointContainer
OBroadcastHelperVar< OMultiTypeInterfaceContainerHelper, OMultiTypeInterfaceContainerHelper::keyType > OBroadcastHelper
OUString SAL_CALL getImplementationName() override
css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo() override
css::uno::Reference< css::frame::XFrame2 > m_xFrame
unsigned char sal_Bool
virtual void SAL_CALL acquire() override
increment refcount XInterface release() A RuntimeException is thrown.
Definition: basecontrol.cxx:91
virtual css::uno::Sequence< css::uno::Type > SAL_CALL getConnectionPointTypes() override
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &aType) override
css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual css::uno::Any SAL_CALL queryAggregation(const css::uno::Type &aType) override
virtual void SAL_CALL setFastPropertyValue_NoBroadcast(sal_Int32 nHandle, const css::uno::Any &rValue) override
virtual css::awt::Rectangle SAL_CALL getPosSize() override
FrameControl(const css::uno::Reference< css::uno::XComponentContext > &rxContext)
virtual void SAL_CALL unadvise(const css::uno::Type &aType, const css::uno::Reference< css::uno::XInterface > &xListener) override
virtual sal_Bool SAL_CALL setGraphics(const css::uno::Reference< css::awt::XGraphics > &xDevice) override
virtual css::uno::Reference< css::awt::XControlModel > SAL_CALL getModel() override
void SAL_CALL fire(sal_Int32 *pnHandles, const css::uno::Any *pNewValues, const css::uno::Any *pOldValues, sal_Int32 nCount, sal_Bool bVetoable)
virtual css::uno::Reference< css::awt::XWindowPeer > SAL_CALL getPeer() override
virtual css::uno::Reference< css::awt::XGraphics > SAL_CALL getGraphics() override
virtual void SAL_CALL acquire() override
increment refcount XInterface release() A RuntimeException is thrown.
const css::uno::Reference< css::uno::XInterface > & impl_getDelegator() const
virtual css::uno::Sequence< css::uno::Type > SAL_CALL getTypes() override
get information about supported interfaces XTypeProvider
virtual css::uno::Sequence< css::uno::Type > SAL_CALL getTypes() override
get information about supported interfaces XTypeProvider
virtual sal_Bool SAL_CALL convertFastPropertyValue(css::uno::Any &rConvertedValue, css::uno::Any &rOldValue, sal_Int32 nHandle, const css::uno::Any &rValue) override
virtual void SAL_CALL release() override
decrement refcount XInterface acquire() A RuntimeException is thrown.
css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType, Interface1 *p1)
virtual void SAL_CALL release() override
decrement refcount XInterface acquire() A RuntimeException is thrown.
const css::uno::Reference< css::awt::XWindow > & impl_getPeerWindow() const
virtual void SAL_CALL getFastPropertyValue(css::uno::Any &rValue, sal_Int32 nHandle) const override
virtual sal_Bool SAL_CALL setModel(const css::uno::Reference< css::awt::XControlModel > &xModel) override