xref: /aoo42x/main/basic/source/classes/eventatt.cxx (revision e1f63238)
1*e1f63238SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*e1f63238SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*e1f63238SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*e1f63238SAndrew Rist  * distributed with this work for additional information
6*e1f63238SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*e1f63238SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*e1f63238SAndrew Rist  * "License"); you may not use this file except in compliance
9*e1f63238SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*e1f63238SAndrew Rist  *
11*e1f63238SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*e1f63238SAndrew Rist  *
13*e1f63238SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*e1f63238SAndrew Rist  * software distributed under the License is distributed on an
15*e1f63238SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*e1f63238SAndrew Rist  * KIND, either express or implied.  See the License for the
17*e1f63238SAndrew Rist  * specific language governing permissions and limitations
18*e1f63238SAndrew Rist  * under the License.
19*e1f63238SAndrew Rist  *
20*e1f63238SAndrew Rist  *************************************************************/
21*e1f63238SAndrew Rist 
22*e1f63238SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_basic.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir //#include <stl_queue.h>
28cdf0e10cSrcweir #include <osl/mutex.hxx>
29cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include <com/sun/star/script/XEventAttacher.hpp>
33cdf0e10cSrcweir #include <com/sun/star/script/XAllListener.hpp>
34cdf0e10cSrcweir #include <com/sun/star/script/XScriptEventsSupplier.hpp>
35cdf0e10cSrcweir #include <com/sun/star/script/XScriptEventsAttacher.hpp>
36cdf0e10cSrcweir #include <com/sun/star/script/ScriptEventDescriptor.hpp>
37cdf0e10cSrcweir #include <com/sun/star/script/XLibraryContainer.hpp>
38cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
39cdf0e10cSrcweir #include <com/sun/star/container/XNameContainer.hpp>
40cdf0e10cSrcweir #include <com/sun/star/resource/XStringResourceSupplier.hpp>
41cdf0e10cSrcweir #include <com/sun/star/resource/XStringResourceManager.hpp>
42cdf0e10cSrcweir #include <com/sun/star/awt/XControlContainer.hpp>
43cdf0e10cSrcweir #include <com/sun/star/awt/XControlModel.hpp>
44cdf0e10cSrcweir #include <com/sun/star/awt/XControl.hpp>
45cdf0e10cSrcweir #include <com/sun/star/awt/XDialog.hpp>
46cdf0e10cSrcweir #include <com/sun/star/awt/XWindow.hpp>
47cdf0e10cSrcweir #include <com/sun/star/script/provider/XScriptProviderFactory.hpp>
48cdf0e10cSrcweir 
49cdf0e10cSrcweir #include <com/sun/star/script/provider/XScriptProviderSupplier.hpp>
50cdf0e10cSrcweir #include <com/sun/star/script/provider/XScriptProvider.hpp>
51cdf0e10cSrcweir #include <com/sun/star/awt/XDialogProvider.hpp>
52cdf0e10cSrcweir 
53cdf0e10cSrcweir #include <com/sun/star/frame/XModel.hpp>
54cdf0e10cSrcweir #include <com/sun/star/frame/XDesktop.hpp>
55cdf0e10cSrcweir #include <com/sun/star/container/XEnumerationAccess.hpp>
56cdf0e10cSrcweir #include <basic/basicmanagerrepository.hxx>
57cdf0e10cSrcweir #include <basic/basmgr.hxx>
58cdf0e10cSrcweir //==================================================================================================
59cdf0e10cSrcweir 
60cdf0e10cSrcweir #include <xmlscript/xmldlg_imexp.hxx>
61cdf0e10cSrcweir #include <sbunoobj.hxx>
62cdf0e10cSrcweir #include <basic/sbstar.hxx>
63cdf0e10cSrcweir #include <basic/sbmeth.hxx>
64cdf0e10cSrcweir #include <basic/sbuno.hxx>
65cdf0e10cSrcweir #include <runtime.hxx>
66cdf0e10cSrcweir #include <sbintern.hxx>
67cdf0e10cSrcweir 
68cdf0e10cSrcweir 
69cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
70cdf0e10cSrcweir using namespace ::com::sun::star;
71cdf0e10cSrcweir using namespace ::com::sun::star::uno;
72cdf0e10cSrcweir using namespace ::com::sun::star::script;
73cdf0e10cSrcweir using namespace ::com::sun::star::resource;
74cdf0e10cSrcweir 
75cdf0e10cSrcweir using namespace ::com::sun::star::uno;
76cdf0e10cSrcweir using namespace ::com::sun::star::lang;
77cdf0e10cSrcweir using namespace ::com::sun::star::beans;
78cdf0e10cSrcweir using namespace ::com::sun::star::script;
79cdf0e10cSrcweir using namespace ::com::sun::star::container;
80cdf0e10cSrcweir using namespace ::com::sun::star::reflection;
81cdf0e10cSrcweir using namespace ::com::sun::star::awt;
82cdf0e10cSrcweir using namespace ::com::sun::star::io;
83cdf0e10cSrcweir using namespace ::cppu;
84cdf0e10cSrcweir using namespace ::osl;
85cdf0e10cSrcweir 
86cdf0e10cSrcweir 
SFURL_firing_impl(const ScriptEvent & aScriptEvent,Any * pRet,const Reference<frame::XModel> & xModel)87cdf0e10cSrcweir void SFURL_firing_impl( const ScriptEvent& aScriptEvent, Any* pRet, const Reference< frame::XModel >& xModel )
88cdf0e10cSrcweir {
89cdf0e10cSrcweir         OSL_TRACE("SFURL_firing_impl() processing script url %s",
90cdf0e10cSrcweir             ::rtl::OUStringToOString( aScriptEvent.ScriptCode,
91cdf0e10cSrcweir                 RTL_TEXTENCODING_ASCII_US ).pData->buffer );
92cdf0e10cSrcweir         try
93cdf0e10cSrcweir         {
94cdf0e10cSrcweir             Reference< provider::XScriptProvider > xScriptProvider;
95cdf0e10cSrcweir             if ( xModel.is() )
96cdf0e10cSrcweir             {
97cdf0e10cSrcweir                 Reference< provider::XScriptProviderSupplier > xSupplier( xModel, UNO_QUERY );
98cdf0e10cSrcweir                 OSL_ENSURE( xSupplier.is(), "SFURL_firing_impl: failed to get script provider supplier" );
99cdf0e10cSrcweir                 if ( xSupplier.is() )
100cdf0e10cSrcweir                     xScriptProvider.set( xSupplier->getScriptProvider() );
101cdf0e10cSrcweir             }
102cdf0e10cSrcweir             else
103cdf0e10cSrcweir             {
104cdf0e10cSrcweir                 Reference< XComponentContext > xContext;
105cdf0e10cSrcweir                 Reference< XPropertySet > xProps( ::comphelper::getProcessServiceFactory(), UNO_QUERY );
106cdf0e10cSrcweir                 OSL_ASSERT( xProps.is() );
107cdf0e10cSrcweir                 OSL_VERIFY( xProps->getPropertyValue( ::rtl::OUString::createFromAscii( "DefaultContext" ) ) >>= xContext );
108cdf0e10cSrcweir                 if ( xContext.is() )
109cdf0e10cSrcweir                 {
110cdf0e10cSrcweir                     Reference< provider::XScriptProviderFactory > xFactory(
111cdf0e10cSrcweir                         xContext->getValueByName(
112cdf0e10cSrcweir                         ::rtl::OUString::createFromAscii( "/singletons/com.sun.star.script.provider.theMasterScriptProviderFactory" ) ),
113cdf0e10cSrcweir                         UNO_QUERY );
114cdf0e10cSrcweir                     OSL_ENSURE( xFactory.is(), "SFURL_firing_impl: failed to get master script provider factory" );
115cdf0e10cSrcweir                     if ( xFactory.is() )
116cdf0e10cSrcweir                     {
117cdf0e10cSrcweir                         Any aCtx;
118cdf0e10cSrcweir                         aCtx <<= ::rtl::OUString::createFromAscii( "user" );
119cdf0e10cSrcweir                         xScriptProvider.set( xFactory->createScriptProvider( aCtx ), UNO_QUERY );
120cdf0e10cSrcweir                     }
121cdf0e10cSrcweir                 }
122cdf0e10cSrcweir             }
123cdf0e10cSrcweir 
124cdf0e10cSrcweir             if ( !xScriptProvider.is() )
125cdf0e10cSrcweir             {
126cdf0e10cSrcweir                 OSL_TRACE("SFURL_firing_impl() Failed to create msp");
127cdf0e10cSrcweir                 return;
128cdf0e10cSrcweir             }
129cdf0e10cSrcweir             Sequence< Any > inArgs( 0 );
130cdf0e10cSrcweir             Sequence< Any > outArgs( 0 );
131cdf0e10cSrcweir             Sequence< sal_Int16 > outIndex;
132cdf0e10cSrcweir 
133cdf0e10cSrcweir             // get Arguments for script
134cdf0e10cSrcweir             inArgs = aScriptEvent.Arguments;
135cdf0e10cSrcweir 
136cdf0e10cSrcweir             Reference< provider::XScript > xScript = xScriptProvider->getScript( aScriptEvent.ScriptCode );
137cdf0e10cSrcweir 
138cdf0e10cSrcweir             if ( !xScript.is() )
139cdf0e10cSrcweir             {
140cdf0e10cSrcweir                 OSL_TRACE("SFURL_firing_impl() Failed to obtain XScript");
141cdf0e10cSrcweir                 return;
142cdf0e10cSrcweir             }
143cdf0e10cSrcweir 
144cdf0e10cSrcweir             Any result = xScript->invoke( inArgs, outIndex, outArgs );
145cdf0e10cSrcweir             if ( pRet )
146cdf0e10cSrcweir             {
147cdf0e10cSrcweir                 *pRet = result;
148cdf0e10cSrcweir             }
149cdf0e10cSrcweir         }
150cdf0e10cSrcweir         catch ( RuntimeException& re )
151cdf0e10cSrcweir         {
152cdf0e10cSrcweir             OSL_TRACE("SFURL_firing_impl() Caught RuntimeException reason %s.",
153cdf0e10cSrcweir                 ::rtl::OUStringToOString( re.Message,
154cdf0e10cSrcweir                     RTL_TEXTENCODING_ASCII_US ).pData->buffer );
155cdf0e10cSrcweir         }
156cdf0e10cSrcweir         catch ( Exception& e )
157cdf0e10cSrcweir         {
158cdf0e10cSrcweir             OSL_TRACE("SFURL_firing_impl() Caught Exception reason %s.",
159cdf0e10cSrcweir                 ::rtl::OUStringToOString( e.Message,
160cdf0e10cSrcweir                     RTL_TEXTENCODING_ASCII_US ).pData->buffer );
161cdf0e10cSrcweir         }
162cdf0e10cSrcweir 
163cdf0e10cSrcweir }
164cdf0e10cSrcweir 
165cdf0e10cSrcweir 
166cdf0e10cSrcweir class BasicScriptListener_Impl : public WeakImplHelper1< XScriptListener >
167cdf0e10cSrcweir {
168cdf0e10cSrcweir 	StarBASICRef maBasicRef;
169cdf0e10cSrcweir         Reference< frame::XModel > m_xModel;
170cdf0e10cSrcweir 
171cdf0e10cSrcweir 	virtual void firing_impl(const ScriptEvent& aScriptEvent, Any* pRet);
172cdf0e10cSrcweir 
173cdf0e10cSrcweir public:
BasicScriptListener_Impl(StarBASIC * pBasic,const Reference<frame::XModel> & xModel)174cdf0e10cSrcweir     BasicScriptListener_Impl( StarBASIC* pBasic, const Reference< frame::XModel >& xModel )
175cdf0e10cSrcweir 		: maBasicRef( pBasic ), m_xModel( xModel ) {}
176cdf0e10cSrcweir 
177cdf0e10cSrcweir     // Methods of XAllListener
178cdf0e10cSrcweir     virtual void SAL_CALL firing(const ScriptEvent& aScriptEvent)
179cdf0e10cSrcweir 		throw( RuntimeException );
180cdf0e10cSrcweir     virtual Any SAL_CALL approveFiring(const ScriptEvent& aScriptEvent)
181cdf0e10cSrcweir 		throw( InvocationTargetException, RuntimeException );
182cdf0e10cSrcweir 
183cdf0e10cSrcweir     // Methods of XEventListener
184cdf0e10cSrcweir     virtual void SAL_CALL disposing(const EventObject& Source)
185cdf0e10cSrcweir 		throw( RuntimeException );
186cdf0e10cSrcweir };
187cdf0e10cSrcweir 
188cdf0e10cSrcweir // Methods XAllListener
firing(const ScriptEvent & aScriptEvent)189cdf0e10cSrcweir void BasicScriptListener_Impl::firing( const ScriptEvent& aScriptEvent ) throw ( RuntimeException )
190cdf0e10cSrcweir {
191cdf0e10cSrcweir 	firing_impl( aScriptEvent, NULL );
192cdf0e10cSrcweir }
193cdf0e10cSrcweir 
approveFiring(const ScriptEvent & aScriptEvent)194cdf0e10cSrcweir Any BasicScriptListener_Impl::approveFiring( const ScriptEvent& aScriptEvent )
195cdf0e10cSrcweir 	throw ( InvocationTargetException, RuntimeException )
196cdf0e10cSrcweir {
197cdf0e10cSrcweir 	Any aRetAny;
198cdf0e10cSrcweir 	firing_impl( aScriptEvent, &aRetAny );
199cdf0e10cSrcweir 	return aRetAny;
200cdf0e10cSrcweir }
201cdf0e10cSrcweir 
202cdf0e10cSrcweir // Methods XEventListener
disposing(const EventObject &)203cdf0e10cSrcweir void BasicScriptListener_Impl::disposing(const EventObject& ) throw ( RuntimeException )
204cdf0e10cSrcweir {
205cdf0e10cSrcweir 	// TODO: ???
206cdf0e10cSrcweir 	//vos::OGuard guard( Application::GetSolarMutex() );
207cdf0e10cSrcweir 	//xSbxObj.Clear();
208cdf0e10cSrcweir }
209cdf0e10cSrcweir 
210cdf0e10cSrcweir 
firing_impl(const ScriptEvent & aScriptEvent,Any * pRet)211cdf0e10cSrcweir void BasicScriptListener_Impl::firing_impl( const ScriptEvent& aScriptEvent, Any* pRet )
212cdf0e10cSrcweir {
213cdf0e10cSrcweir 	//Guard< Mutex > aGuard( Mutex::getGlobalMutex() );
214cdf0e10cSrcweir 	//{
215cdf0e10cSrcweir 	if( aScriptEvent.ScriptType.compareToAscii( "StarBasic" ) == 0 )
216cdf0e10cSrcweir 	{
217cdf0e10cSrcweir 		// Full qualified name?
218cdf0e10cSrcweir 		String aMacro( aScriptEvent.ScriptCode );
219cdf0e10cSrcweir 		String aLibName;
220cdf0e10cSrcweir         String aLocation;
221cdf0e10cSrcweir 		if( aMacro.GetTokenCount( '.' ) == 3 )
222cdf0e10cSrcweir 		{
223cdf0e10cSrcweir 			sal_uInt16 nLast = 0;
224cdf0e10cSrcweir 			::rtl::OUString aFullLibName = aMacro.GetToken( 0, '.', nLast );
225cdf0e10cSrcweir 
226cdf0e10cSrcweir             sal_Int32 nIndex = aFullLibName.indexOf( (sal_Unicode)':' );
227cdf0e10cSrcweir             if (nIndex >= 0)
228cdf0e10cSrcweir             {
229cdf0e10cSrcweir                 aLocation = aFullLibName.copy( 0, nIndex );
230cdf0e10cSrcweir                 aLibName = aFullLibName.copy( nIndex + 1 );
231cdf0e10cSrcweir             }
232cdf0e10cSrcweir 
233cdf0e10cSrcweir 			String aModul = aMacro.GetToken( 0, '.', nLast );
234cdf0e10cSrcweir 			aMacro.Erase( 0, nLast );
235cdf0e10cSrcweir 		}
236cdf0e10cSrcweir 
237cdf0e10cSrcweir 		SbxObject* p = maBasicRef;
238cdf0e10cSrcweir 		SbxObject* pParent = p->GetParent();
239cdf0e10cSrcweir         SbxObject* pParentParent = pParent ? pParent->GetParent() : NULL;
240cdf0e10cSrcweir 
241cdf0e10cSrcweir         StarBASICRef xAppStandardBasic;
242cdf0e10cSrcweir         StarBASICRef xDocStandardBasic;
243cdf0e10cSrcweir         if( pParentParent )
244cdf0e10cSrcweir         {
245cdf0e10cSrcweir             // Own basic must be document library
246cdf0e10cSrcweir             xAppStandardBasic = (StarBASIC*)pParentParent;
247cdf0e10cSrcweir             xDocStandardBasic = (StarBASIC*)pParent;
248cdf0e10cSrcweir         }
249cdf0e10cSrcweir         else if( pParent )
250cdf0e10cSrcweir         {
251cdf0e10cSrcweir             String aName = p->GetName();
252cdf0e10cSrcweir             if( aName.EqualsAscii("Standard") )
253cdf0e10cSrcweir             {
254cdf0e10cSrcweir                 // Own basic is doc standard lib
255cdf0e10cSrcweir                 xDocStandardBasic = (StarBASIC*)p;
256cdf0e10cSrcweir             }
257cdf0e10cSrcweir             xAppStandardBasic = (StarBASIC*)pParent;
258cdf0e10cSrcweir         }
259cdf0e10cSrcweir         else
260cdf0e10cSrcweir         {
261cdf0e10cSrcweir             xAppStandardBasic = (StarBASIC*)p;
262cdf0e10cSrcweir         }
263cdf0e10cSrcweir 
264cdf0e10cSrcweir         sal_Bool bSearchLib = true;
265cdf0e10cSrcweir         StarBASICRef xLibSearchBasic;
266cdf0e10cSrcweir         if( aLocation.EqualsAscii("application") )
267cdf0e10cSrcweir             xLibSearchBasic = xAppStandardBasic;
268cdf0e10cSrcweir         else if( aLocation.EqualsAscii("document") )
269cdf0e10cSrcweir             xLibSearchBasic = xDocStandardBasic;
270cdf0e10cSrcweir         else
271cdf0e10cSrcweir             bSearchLib = false;
272cdf0e10cSrcweir 
273cdf0e10cSrcweir         SbxVariable* pMethVar = NULL;
274cdf0e10cSrcweir         // Be still tolerant and make default search if no search basic exists
275cdf0e10cSrcweir         if( bSearchLib && xLibSearchBasic.Is() )
276cdf0e10cSrcweir         {
277cdf0e10cSrcweir             StarBASICRef xLibBasic;
278cdf0e10cSrcweir 	        sal_Int16 nCount = xLibSearchBasic->GetObjects()->Count();
279cdf0e10cSrcweir 	        for( sal_Int16 nObj = -1; nObj < nCount ; nObj++ )
280cdf0e10cSrcweir 	        {
281cdf0e10cSrcweir                 StarBASIC* pBasic;
282cdf0e10cSrcweir                 if( nObj == -1 )
283cdf0e10cSrcweir                 {
284cdf0e10cSrcweir 		            pBasic = (StarBASIC*)xLibSearchBasic;
285cdf0e10cSrcweir                 }
286cdf0e10cSrcweir                 else
287cdf0e10cSrcweir                 {
288cdf0e10cSrcweir 		            SbxVariable* pVar = xLibSearchBasic->GetObjects()->Get( nObj );
289cdf0e10cSrcweir 		            pBasic = PTR_CAST(StarBASIC,pVar);
290cdf0e10cSrcweir                 }
291cdf0e10cSrcweir 		        if( pBasic )
292cdf0e10cSrcweir                 {
293cdf0e10cSrcweir                     String aName = pBasic->GetName();
294cdf0e10cSrcweir                     if( aName == aLibName )
295cdf0e10cSrcweir                     {
296cdf0e10cSrcweir                         // Search only in the lib, not automatically in application basic
297cdf0e10cSrcweir 			            sal_uInt16 nFlags = pBasic->GetFlags();
298cdf0e10cSrcweir 			            pBasic->ResetFlag( SBX_GBLSEARCH );
299cdf0e10cSrcweir 		                pMethVar = pBasic->Find( aMacro, SbxCLASS_DONTCARE );
300cdf0e10cSrcweir 			            pBasic->SetFlags( nFlags );
301cdf0e10cSrcweir                         break;
302cdf0e10cSrcweir                     }
303cdf0e10cSrcweir                 }
304cdf0e10cSrcweir 	        }
305cdf0e10cSrcweir         }
306cdf0e10cSrcweir 
307cdf0e10cSrcweir         // Default: Be tolerant and search everywhere
308cdf0e10cSrcweir 		if( (!pMethVar || !pMethVar->ISA(SbMethod)) && maBasicRef.Is() )
309cdf0e10cSrcweir 			pMethVar = maBasicRef->FindQualified( aMacro, SbxCLASS_DONTCARE );
310cdf0e10cSrcweir 
311cdf0e10cSrcweir 		SbMethod* pMeth = PTR_CAST(SbMethod,pMethVar);
312cdf0e10cSrcweir 		if( !pMeth )
313cdf0e10cSrcweir 		    return;
314cdf0e10cSrcweir 
315cdf0e10cSrcweir 		// Setup parameters
316cdf0e10cSrcweir 		SbxArrayRef xArray;
317cdf0e10cSrcweir 		String aTmp;
318cdf0e10cSrcweir 		sal_Int32 nCnt = aScriptEvent.Arguments.getLength();
319cdf0e10cSrcweir 		if( nCnt )
320cdf0e10cSrcweir 		{
321cdf0e10cSrcweir 			xArray = new SbxArray;
322cdf0e10cSrcweir 			const Any *pArgs = aScriptEvent.Arguments.getConstArray();
323cdf0e10cSrcweir 			for( sal_Int32 i = 0; i < nCnt; i++ )
324cdf0e10cSrcweir 			{
325cdf0e10cSrcweir 				SbxVariableRef xVar = new SbxVariable( SbxVARIANT );
326cdf0e10cSrcweir 				unoToSbxValue( (SbxVariable*)xVar, pArgs[i] );
327cdf0e10cSrcweir 				xArray->Put( xVar, sal::static_int_cast< sal_uInt16 >(i+1) );
328cdf0e10cSrcweir 			}
329cdf0e10cSrcweir 		}
330cdf0e10cSrcweir 
331cdf0e10cSrcweir 		// Call method
332cdf0e10cSrcweir 		SbxVariableRef xValue = pRet ? new SbxVariable : 0;
333cdf0e10cSrcweir 		if( xArray.Is() )
334cdf0e10cSrcweir 			pMeth->SetParameters( xArray );
335cdf0e10cSrcweir 		pMeth->Call( xValue );
336cdf0e10cSrcweir 		if( pRet )
337cdf0e10cSrcweir 			*pRet = sbxToUnoValue( xValue );
338cdf0e10cSrcweir 		pMeth->SetParameters( NULL );
339cdf0e10cSrcweir 	}
340cdf0e10cSrcweir         else // scripting framework script
341cdf0e10cSrcweir         {
342cdf0e10cSrcweir             //callBasic via scripting framework
343cdf0e10cSrcweir             SFURL_firing_impl( aScriptEvent, pRet, m_xModel );
344cdf0e10cSrcweir 
345cdf0e10cSrcweir         }
346cdf0e10cSrcweir }
347cdf0e10cSrcweir 
implFindDialogLibForDialog(const Any & rDlgAny,SbxObject * pBasic)348cdf0e10cSrcweir Any implFindDialogLibForDialog( const Any& rDlgAny, SbxObject* pBasic )
349cdf0e10cSrcweir {
350cdf0e10cSrcweir 	Any aRetDlgLibAny;
351cdf0e10cSrcweir 
352cdf0e10cSrcweir     SbxVariable* pDlgLibContVar = pBasic->Find
353cdf0e10cSrcweir 		(  String::CreateFromAscii("DialogLibraries"), SbxCLASS_OBJECT );
354cdf0e10cSrcweir 	if( pDlgLibContVar && pDlgLibContVar->ISA(SbUnoObject) )
355cdf0e10cSrcweir 	{
356cdf0e10cSrcweir 		SbUnoObject* pDlgLibContUnoObj = (SbUnoObject*)(SbxBase*)pDlgLibContVar;
357cdf0e10cSrcweir 		Any aDlgLibContAny = pDlgLibContUnoObj->getUnoAny();
358cdf0e10cSrcweir 
359cdf0e10cSrcweir 		Reference< XLibraryContainer > xDlgLibContNameAccess( aDlgLibContAny, UNO_QUERY );
360cdf0e10cSrcweir         OSL_ENSURE( xDlgLibContNameAccess.is(), "implFindDialogLibForDialog: no lib container for the given dialog!" );
361cdf0e10cSrcweir 		if( xDlgLibContNameAccess.is() )
362cdf0e10cSrcweir 		{
363cdf0e10cSrcweir 			Sequence< ::rtl::OUString > aLibNames = xDlgLibContNameAccess->getElementNames();
364cdf0e10cSrcweir 			const ::rtl::OUString* pLibNames = aLibNames.getConstArray();
365cdf0e10cSrcweir 			sal_Int32 nLibNameCount = aLibNames.getLength();
366cdf0e10cSrcweir 
367cdf0e10cSrcweir 			for( sal_Int32 iLib = 0 ; iLib < nLibNameCount ; iLib++ )
368cdf0e10cSrcweir 			{
369cdf0e10cSrcweir                 if ( !xDlgLibContNameAccess->isLibraryLoaded( pLibNames[ iLib ] ) )
370cdf0e10cSrcweir                     // if the library isn't loaded, then the dialog cannot originate from this lib
371cdf0e10cSrcweir                     continue;
372cdf0e10cSrcweir 
373cdf0e10cSrcweir 				Any aDlgLibAny = xDlgLibContNameAccess->getByName( pLibNames[ iLib ] );
374cdf0e10cSrcweir 
375cdf0e10cSrcweir 				Reference< XNameAccess > xDlgLibNameAccess( aDlgLibAny, UNO_QUERY );
376cdf0e10cSrcweir                 OSL_ENSURE( xDlgLibNameAccess.is(), "implFindDialogLibForDialog: invalid dialog lib!" );
377cdf0e10cSrcweir 				if( xDlgLibNameAccess.is() )
378cdf0e10cSrcweir 				{
379cdf0e10cSrcweir 					Sequence< ::rtl::OUString > aDlgNames = xDlgLibNameAccess->getElementNames();
380cdf0e10cSrcweir 					const ::rtl::OUString* pDlgNames = aDlgNames.getConstArray();
381cdf0e10cSrcweir 					sal_Int32 nDlgNameCount = aDlgNames.getLength();
382cdf0e10cSrcweir 
383cdf0e10cSrcweir 					for( sal_Int32 iDlg = 0 ; iDlg < nDlgNameCount ; iDlg++ )
384cdf0e10cSrcweir 					{
385cdf0e10cSrcweir 						Any aDlgAny = xDlgLibNameAccess->getByName( pDlgNames[ iDlg ] );
386cdf0e10cSrcweir 						if( aDlgAny == rDlgAny )
387cdf0e10cSrcweir 						{
388cdf0e10cSrcweir 							aRetDlgLibAny = aDlgLibAny;
389cdf0e10cSrcweir 							break;
390cdf0e10cSrcweir 						}
391cdf0e10cSrcweir 					}
392cdf0e10cSrcweir 				}
393cdf0e10cSrcweir 			}
394cdf0e10cSrcweir 		}
395cdf0e10cSrcweir 	}
396cdf0e10cSrcweir 
397cdf0e10cSrcweir 	return aRetDlgLibAny;
398cdf0e10cSrcweir }
399cdf0e10cSrcweir 
implFindDialogLibForDialogBasic(const Any & aAnyISP,SbxObject * pBasic,StarBASIC * & pFoundBasic)400cdf0e10cSrcweir Any implFindDialogLibForDialogBasic( const Any& aAnyISP, SbxObject* pBasic, StarBASIC*& pFoundBasic )
401cdf0e10cSrcweir {
402cdf0e10cSrcweir 	Any aDlgLibAny;
403cdf0e10cSrcweir 	// Find dialog library for dialog, direct access is not possible here
404cdf0e10cSrcweir     StarBASIC* pStartedBasic = (StarBASIC*)pBasic;
405cdf0e10cSrcweir 	SbxObject* pParentBasic = pStartedBasic ? pStartedBasic->GetParent() : NULL;
406cdf0e10cSrcweir 	SbxObject* pParentParentBasic = pParentBasic ? pParentBasic->GetParent() : NULL;
407cdf0e10cSrcweir 
408cdf0e10cSrcweir 	SbxObject* pSearchBasic1 = NULL;
409cdf0e10cSrcweir 	SbxObject* pSearchBasic2 = NULL;
410cdf0e10cSrcweir 	if( pParentParentBasic )
411cdf0e10cSrcweir 	{
412cdf0e10cSrcweir 		pSearchBasic1 = pParentBasic;
413cdf0e10cSrcweir 		pSearchBasic2 = pParentParentBasic;
414cdf0e10cSrcweir 	}
415cdf0e10cSrcweir 	else
416cdf0e10cSrcweir 	{
417cdf0e10cSrcweir 		pSearchBasic1 = pStartedBasic;
418cdf0e10cSrcweir 		pSearchBasic2 = pParentBasic;
419cdf0e10cSrcweir 	}
420cdf0e10cSrcweir 	if( pSearchBasic1 )
421cdf0e10cSrcweir 	{
422cdf0e10cSrcweir 		aDlgLibAny = implFindDialogLibForDialog( aAnyISP, pSearchBasic1 );
423cdf0e10cSrcweir 
424cdf0e10cSrcweir 		if ( aDlgLibAny.hasValue() )
425cdf0e10cSrcweir 			pFoundBasic = (StarBASIC*)pSearchBasic1;
426cdf0e10cSrcweir 
427cdf0e10cSrcweir 		else if( pSearchBasic2 )
428cdf0e10cSrcweir 		{
429cdf0e10cSrcweir 			aDlgLibAny = implFindDialogLibForDialog( aAnyISP, pSearchBasic2 );
430cdf0e10cSrcweir 			if ( aDlgLibAny.hasValue() )
431cdf0e10cSrcweir 				pFoundBasic = (StarBASIC*)pSearchBasic2;
432cdf0e10cSrcweir 		}
433cdf0e10cSrcweir 	}
434cdf0e10cSrcweir 	return aDlgLibAny;
435cdf0e10cSrcweir }
436cdf0e10cSrcweir 
437cdf0e10cSrcweir static ::rtl::OUString aDecorationPropName =
438cdf0e10cSrcweir 	::rtl::OUString::createFromAscii( "Decoration" );
439cdf0e10cSrcweir static ::rtl::OUString aTitlePropName =
440cdf0e10cSrcweir 	::rtl::OUString::createFromAscii( "Title" );
441cdf0e10cSrcweir 
RTL_Impl_CreateUnoDialog(StarBASIC * pBasic,SbxArray & rPar,sal_Bool bWrite)442cdf0e10cSrcweir void RTL_Impl_CreateUnoDialog( StarBASIC* pBasic, SbxArray& rPar, sal_Bool bWrite )
443cdf0e10cSrcweir {
444cdf0e10cSrcweir 	static ::rtl::OUString aResourceResolverPropName = ::rtl::OUString::createFromAscii( "ResourceResolver" );
445cdf0e10cSrcweir 
446cdf0e10cSrcweir     (void)pBasic;
447cdf0e10cSrcweir     (void)bWrite;
448cdf0e10cSrcweir 
449cdf0e10cSrcweir 	Reference< XMultiServiceFactory > xMSF( comphelper::getProcessServiceFactory() );
450cdf0e10cSrcweir 	if( !xMSF.is() )
451cdf0e10cSrcweir 		return;
452cdf0e10cSrcweir 
453cdf0e10cSrcweir 	// We need at least 1 parameter
454cdf0e10cSrcweir 	if ( rPar.Count() < 2 )
455cdf0e10cSrcweir 	{
456cdf0e10cSrcweir 		StarBASIC::Error( SbERR_BAD_ARGUMENT );
457cdf0e10cSrcweir 		return;
458cdf0e10cSrcweir     }
459cdf0e10cSrcweir 
460cdf0e10cSrcweir 	// Get dialog
461cdf0e10cSrcweir 	SbxBaseRef pObj = (SbxBase*)rPar.Get( 1 )->GetObject();
462cdf0e10cSrcweir 	if( !(pObj && pObj->ISA(SbUnoObject)) )
463cdf0e10cSrcweir 	{
464cdf0e10cSrcweir 		StarBASIC::Error( SbERR_BAD_ARGUMENT );
465cdf0e10cSrcweir 		return;
466cdf0e10cSrcweir 	}
467cdf0e10cSrcweir 	SbUnoObject* pUnoObj = (SbUnoObject*)(SbxBase*)pObj;
468cdf0e10cSrcweir 	Any aAnyISP = pUnoObj->getUnoAny();
469cdf0e10cSrcweir 	TypeClass eType = aAnyISP.getValueType().getTypeClass();
470cdf0e10cSrcweir 
471cdf0e10cSrcweir 	if( eType != TypeClass_INTERFACE )
472cdf0e10cSrcweir 	{
473cdf0e10cSrcweir 		StarBASIC::Error( SbERR_BAD_ARGUMENT );
474cdf0e10cSrcweir 		return;
475cdf0e10cSrcweir 	}
476cdf0e10cSrcweir 
477cdf0e10cSrcweir 	// Create new uno dialog
478cdf0e10cSrcweir 	Reference< XNameContainer > xDialogModel( xMSF->createInstance
479cdf0e10cSrcweir 		( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlDialogModel" ) ) ),
480cdf0e10cSrcweir 			UNO_QUERY );
481cdf0e10cSrcweir 	if( !xDialogModel.is() )
482cdf0e10cSrcweir 		return;
483cdf0e10cSrcweir 
484cdf0e10cSrcweir 	Reference< XInputStreamProvider > xISP;
485cdf0e10cSrcweir 	aAnyISP >>= xISP;
486cdf0e10cSrcweir 	if( !xISP.is() )
487cdf0e10cSrcweir 		return;
488cdf0e10cSrcweir 
489cdf0e10cSrcweir     Reference< XComponentContext > xContext;
490cdf0e10cSrcweir     Reference< XPropertySet > xProps( xMSF, UNO_QUERY );
491cdf0e10cSrcweir     OSL_ASSERT( xProps.is() );
492cdf0e10cSrcweir     OSL_VERIFY( xProps->getPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DefaultContext")) ) >>= xContext );
493cdf0e10cSrcweir 
494cdf0e10cSrcweir 	// Import the DialogModel
495cdf0e10cSrcweir 	Reference< XInputStream > xInput( xISP->createInputStream() );
496cdf0e10cSrcweir 
497cdf0e10cSrcweir 	// i83963 Force decoration
498cdf0e10cSrcweir 	uno::Reference< beans::XPropertySet > xDlgModPropSet( xDialogModel, uno::UNO_QUERY );
499cdf0e10cSrcweir 	if( xDlgModPropSet.is() )
500cdf0e10cSrcweir 	{
501cdf0e10cSrcweir 		bool bDecoration = true;
502cdf0e10cSrcweir 		try
503cdf0e10cSrcweir 		{
504cdf0e10cSrcweir 			Any aDecorationAny = xDlgModPropSet->getPropertyValue( aDecorationPropName );
505cdf0e10cSrcweir 			aDecorationAny >>= bDecoration;
506cdf0e10cSrcweir 			if( !bDecoration )
507cdf0e10cSrcweir 			{
508cdf0e10cSrcweir 				xDlgModPropSet->setPropertyValue( aDecorationPropName, makeAny( true ) );
509cdf0e10cSrcweir 				xDlgModPropSet->setPropertyValue( aTitlePropName, makeAny( ::rtl::OUString() ) );
510cdf0e10cSrcweir 			}
511cdf0e10cSrcweir 		}
512cdf0e10cSrcweir 		catch( UnknownPropertyException& )
513cdf0e10cSrcweir 		{}
514cdf0e10cSrcweir 	}
515cdf0e10cSrcweir 
516cdf0e10cSrcweir     Any aDlgLibAny;
517cdf0e10cSrcweir 	bool bDocDialog = false;
518cdf0e10cSrcweir 	StarBASIC* pFoundBasic = NULL;
519cdf0e10cSrcweir     OSL_TRACE("About to try get a hold of ThisComponent");
520cdf0e10cSrcweir     Reference< frame::XModel > xModel = StarBASIC::GetModelFromBasic( pINST->GetBasic() ) ;
521cdf0e10cSrcweir 		aDlgLibAny = implFindDialogLibForDialogBasic( aAnyISP, pINST->GetBasic(), pFoundBasic );
522cdf0e10cSrcweir 		// If we found the dialog then it belongs to the Search basic
523cdf0e10cSrcweir 		if ( !pFoundBasic )
524cdf0e10cSrcweir 	{
525cdf0e10cSrcweir 			Reference< frame::XDesktop > xDesktop( xMSF->createInstance
526cdf0e10cSrcweir 		( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.Desktop" ) ) ),
527cdf0e10cSrcweir 			UNO_QUERY );
528cdf0e10cSrcweir 			Reference< container::XEnumeration > xModels;
529cdf0e10cSrcweir 			if ( xDesktop.is() )
530cdf0e10cSrcweir 	{
531cdf0e10cSrcweir 				Reference< container::XEnumerationAccess > xComponents( xDesktop->getComponents(), UNO_QUERY );
532cdf0e10cSrcweir 				if ( xComponents.is() )
533cdf0e10cSrcweir 					xModels.set( xComponents->createEnumeration(), UNO_QUERY );
534cdf0e10cSrcweir 				if ( xModels.is() )
535cdf0e10cSrcweir 				{
536cdf0e10cSrcweir 					while ( xModels->hasMoreElements() )
537cdf0e10cSrcweir 					{
538cdf0e10cSrcweir 						Reference< frame::XModel > xNextModel( xModels->nextElement(), UNO_QUERY );
539cdf0e10cSrcweir 						if ( xNextModel.is() )
540cdf0e10cSrcweir 						{
541cdf0e10cSrcweir 							BasicManager* pMgr = basic::BasicManagerRepository::getDocumentBasicManager( xNextModel );
542cdf0e10cSrcweir 							if ( pMgr )
543cdf0e10cSrcweir 								aDlgLibAny = implFindDialogLibForDialogBasic( aAnyISP, pMgr->GetLib(0), pFoundBasic );
544cdf0e10cSrcweir 							if ( aDlgLibAny.hasValue() )
545cdf0e10cSrcweir 	{
546cdf0e10cSrcweir 								bDocDialog = true;
547cdf0e10cSrcweir 								xModel = xNextModel;
548cdf0e10cSrcweir 								break;
549cdf0e10cSrcweir 	}
550cdf0e10cSrcweir 						}
551cdf0e10cSrcweir 					}
552cdf0e10cSrcweir 				}
553cdf0e10cSrcweir 			}
554cdf0e10cSrcweir 		}
555cdf0e10cSrcweir 	if ( pFoundBasic )
556cdf0e10cSrcweir 		bDocDialog = pFoundBasic->IsDocBasic();
557cdf0e10cSrcweir        Reference< XScriptListener > xScriptListener = new BasicScriptListener_Impl( pINST->GetBasic(), xModel );
558cdf0e10cSrcweir 
559cdf0e10cSrcweir 	Sequence< Any > aArgs( 4 );
560cdf0e10cSrcweir 	if( bDocDialog )
561cdf0e10cSrcweir 	   aArgs[ 0 ] <<= xModel;
562cdf0e10cSrcweir     else
563cdf0e10cSrcweir 	   aArgs[ 0 ] <<= uno::Reference< uno::XInterface >();
564cdf0e10cSrcweir 	aArgs[ 1 ] <<= xInput;
565cdf0e10cSrcweir 	aArgs[ 2 ] = aDlgLibAny;
566cdf0e10cSrcweir 	aArgs[ 3 ] <<= xScriptListener;
567cdf0e10cSrcweir 	// Create a "living" Dialog
568cdf0e10cSrcweir         Reference< XControl > xCntrl;
569cdf0e10cSrcweir         try
570cdf0e10cSrcweir         {
571cdf0e10cSrcweir 	    Reference< XDialogProvider >  xDlgProv( xMSF->createInstanceWithArguments( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.scripting.DialogProvider" ) ), aArgs ), UNO_QUERY );
572cdf0e10cSrcweir             xCntrl.set( xDlgProv->createDialog( rtl::OUString() ), UNO_QUERY_THROW );
573cdf0e10cSrcweir            // Add dialog model to dispose vector
574cdf0e10cSrcweir            Reference< XComponent > xDlgComponent( xCntrl->getModel(), UNO_QUERY );
575cdf0e10cSrcweir            pINST->getComponentVector().push_back( xDlgComponent );
576cdf0e10cSrcweir            // need ThisCompoent from calling script
577cdf0e10cSrcweir         }
578cdf0e10cSrcweir         // preserve existing bad behaviour, it's possible... but probably
579cdf0e10cSrcweir         // illegal to open 2 dialogs ( they ARE modal ) when this happens, sometimes
580cdf0e10cSrcweir         // create dialog fails.  So, in this case let's not throw, just leave basic
581cdf0e10cSrcweir         // detect the unset object.
582cdf0e10cSrcweir         catch( uno::Exception& )
583cdf0e10cSrcweir         {
584cdf0e10cSrcweir         }
585cdf0e10cSrcweir 
586cdf0e10cSrcweir 	// Return dialog
587cdf0e10cSrcweir 	Any aRetVal;
588cdf0e10cSrcweir 	aRetVal <<= xCntrl;
589cdf0e10cSrcweir 	SbxVariableRef refVar = rPar.Get(0);
590cdf0e10cSrcweir 	unoToSbxValue( (SbxVariable*)refVar, aRetVal );
591cdf0e10cSrcweir }
592cdf0e10cSrcweir 
593cdf0e10cSrcweir 
594cdf0e10cSrcweir //===================================================================
595cdf0e10cSrcweir 
596