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_chart2.hxx"
30 
31 #include "dlg_CreationWizard_UNO.hxx"
32 #include "dlg_CreationWizard.hxx"
33 #include "macros.hxx"
34 #include "servicenames.hxx"
35 #include "ContainerHelper.hxx"
36 #include "TimerTriggeredControllerLock.hxx"
37 #include <osl/mutex.hxx>
38 #include <vos/mutex.hxx>
39 // header for class Application
40 #include <vcl/svapp.hxx>
41 #include <toolkit/awt/vclxwindow.hxx>
42 // header for define RET_CANCEL
43 #include <vcl/msgbox.hxx>
44 // header for class OImplementationId
45 #include <cppuhelper/typeprovider.hxx>
46 #include <com/sun/star/awt/Point.hpp>
47 #include <com/sun/star/awt/Size.hpp>
48 #include <com/sun/star/beans/PropertyValue.hpp>
49 #include <com/sun/star/frame/XDesktop.hpp>
50 
51 //.............................................................................
52 namespace chart
53 {
54 //.............................................................................
55 using namespace ::com::sun::star;
56 
57 CreationWizardUnoDlg::CreationWizardUnoDlg( const uno::Reference< uno::XComponentContext >& xContext )
58                     : OComponentHelper( m_aMutex )
59                     , m_xChartModel( 0 )
60                     , m_xCC( xContext )
61                     , m_xParentWindow( 0 )
62                     , m_pDialog( 0 )
63                     , m_bUnlockControllersOnExecute(false)
64 {
65     uno::Reference< frame::XDesktop > xDesktop(
66         m_xCC->getServiceManager()->createInstanceWithContext(
67             C2U( "com.sun.star.frame.Desktop" ), m_xCC ), uno::UNO_QUERY );
68 	if( xDesktop.is() )
69 	{
70         uno::Reference< frame::XTerminateListener > xListener( this );
71 		xDesktop->addTerminateListener( xListener );
72 	}
73 }
74 CreationWizardUnoDlg::~CreationWizardUnoDlg()
75 {
76     ::vos::OGuard aSolarGuard( Application::GetSolarMutex());
77 	if( m_pDialog )
78 	{
79 		delete m_pDialog;
80 		m_pDialog = 0;
81 	}
82 }
83 //-------------------------------------------------------------------------
84 // lang::XServiceInfo
85 APPHELPER_XSERVICEINFO_IMPL(CreationWizardUnoDlg,CHART_WIZARD_DIALOG_SERVICE_IMPLEMENTATION_NAME)
86 
87     uno::Sequence< rtl::OUString > CreationWizardUnoDlg
88 ::getSupportedServiceNames_Static()
89 {
90 	uno::Sequence< rtl::OUString > aSNS( 1 );
91 	aSNS.getArray()[ 0 ] = CHART_WIZARD_DIALOG_SERVICE_NAME;
92 	return aSNS;
93 }
94 
95 //-------------------------------------------------------------------------
96 // XInterface
97 uno::Any SAL_CALL CreationWizardUnoDlg::queryInterface( const uno::Type& aType ) throw (uno::RuntimeException)
98 {
99 	return OComponentHelper::queryInterface( aType );
100 }
101 void SAL_CALL CreationWizardUnoDlg::acquire() throw ()
102 {
103     OComponentHelper::acquire();
104 }
105 void SAL_CALL CreationWizardUnoDlg::release() throw ()
106 {
107     OComponentHelper::release();
108 }
109 uno::Any SAL_CALL CreationWizardUnoDlg::queryAggregation( uno::Type const & rType ) throw (uno::RuntimeException)
110 {
111     if (rType == ::getCppuType( (uno::Reference< ui::dialogs::XExecutableDialog > const *)0 ))
112     {
113         void * p = static_cast< ui::dialogs::XExecutableDialog * >( this );
114         return uno::Any( &p, rType );
115     }
116     else if (rType == ::getCppuType( (uno::Reference< lang::XServiceInfo > const *)0 ))
117     {
118         void * p = static_cast< lang::XTypeProvider * >( this );
119         return uno::Any( &p, rType );
120     }
121     else if (rType == ::getCppuType( (uno::Reference< lang::XInitialization > const *)0 ))
122     {
123         void * p = static_cast< lang::XInitialization * >( this );
124         return uno::Any( &p, rType );
125     }
126 	else if (rType == ::getCppuType( (uno::Reference< frame::XTerminateListener > const *)0 ))
127 	{
128         void * p = static_cast< frame::XTerminateListener * >( this );
129         return uno::Any( &p, rType );
130 	}
131     else if (rType == ::getCppuType( (uno::Reference< beans::XPropertySet > const *)0 ))
132 	{
133         void * p = static_cast< beans::XPropertySet * >( this );
134         return uno::Any( &p, rType );
135 	}
136     return OComponentHelper::queryAggregation( rType );
137 }
138 
139 //-------------------------------------------------------------------------
140 #define LCL_CPPUTYPE(t) (::getCppuType( reinterpret_cast< const uno::Reference<t> *>(0)))
141 
142 uno::Sequence< uno::Type > CreationWizardUnoDlg::getTypes() throw(uno::RuntimeException)
143 {
144     static uno::Sequence< uno::Type > aTypeList;
145 
146     // /--
147     ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
148 	if( !aTypeList.getLength() )
149 	{
150         ::std::vector< uno::Type > aTypes;
151         aTypes.push_back( LCL_CPPUTYPE( lang::XComponent ));
152         aTypes.push_back( LCL_CPPUTYPE( lang::XTypeProvider ));
153         aTypes.push_back( LCL_CPPUTYPE( uno::XAggregation ));
154         aTypes.push_back( LCL_CPPUTYPE( uno::XWeak ));
155         aTypes.push_back( LCL_CPPUTYPE( lang::XServiceInfo ));
156         aTypes.push_back( LCL_CPPUTYPE( lang::XInitialization ));
157         aTypes.push_back( LCL_CPPUTYPE( frame::XTerminateListener ));
158         aTypes.push_back( LCL_CPPUTYPE( ui::dialogs::XExecutableDialog ));
159         aTypes.push_back( LCL_CPPUTYPE( beans::XPropertySet ));
160 		aTypeList = ::chart::ContainerHelper::ContainerToSequence( aTypes );
161 	}
162 
163 	return aTypeList;
164     // \--
165 }
166 uno::Sequence< sal_Int8 > SAL_CALL CreationWizardUnoDlg::getImplementationId( void ) throw( uno::RuntimeException )
167 {
168     static uno::Sequence< sal_Int8 > aId;
169 	if( aId.getLength() == 0 )
170 	{
171 		aId.realloc( 16 );
172 		rtl_createUuid( (sal_uInt8 *)aId.getArray(), 0, sal_True );
173 	}
174 	return aId;
175 }
176 
177 //-------------------------------------------------------------------------
178 
179 // XTerminateListener
180 void SAL_CALL CreationWizardUnoDlg::queryTermination( const lang::EventObject& /*Event*/ ) throw( frame::TerminationVetoException, uno::RuntimeException)
181 {
182 	::vos::OGuard aSolarGuard( Application::GetSolarMutex());
183 
184 	// we will never give a veto here
185 	if( m_pDialog && !m_pDialog->isClosable() )
186 	{
187 		m_pDialog->ToTop();
188 		throw frame::TerminationVetoException();
189 	}
190 }
191 
192 //-------------------------------------------------------------------------
193 
194 void SAL_CALL CreationWizardUnoDlg::notifyTermination( const lang::EventObject& /*Event*/ ) throw (uno::RuntimeException)
195 {
196 	// we are going down, so dispose us!
197 	dispose();
198 }
199 
200 void SAL_CALL CreationWizardUnoDlg::disposing( const lang::EventObject& /*Source*/ ) throw (uno::RuntimeException)
201 {
202     //Listener should deregister himself and relaese all references to the closing object.
203 }
204 
205 //-------------------------------------------------------------------------
206 void SAL_CALL CreationWizardUnoDlg::setTitle( const ::rtl::OUString& /*rTitle*/ ) throw(uno::RuntimeException)
207 {
208 }
209 //-------------------------------------------------------------------------
210 void CreationWizardUnoDlg::createDialogOnDemand()
211 {
212     ::vos::OGuard aSolarGuard( Application::GetSolarMutex());
213 	if( !m_pDialog )
214 	{
215 		Window* pParent = NULL;
216         if( !m_xParentWindow.is() && m_xChartModel.is() )
217         {
218             uno::Reference< frame::XController > xController(
219                 m_xChartModel->getCurrentController() );
220             if( xController.is() )
221             {
222                 uno::Reference< frame::XFrame > xFrame(
223                     xController->getFrame() );
224                 if(xFrame.is())
225                     m_xParentWindow = xFrame->getContainerWindow();
226             }
227         }
228 		if( m_xParentWindow.is() )
229 		{
230 			VCLXWindow* pImplementation = VCLXWindow::GetImplementation(m_xParentWindow);
231 			if (pImplementation)
232 				pParent = pImplementation->GetWindow();
233 		}
234         uno::Reference< XComponent > xComp( this );
235         if( m_xChartModel.is() )
236         {
237 		    m_pDialog = new CreationWizard( pParent, m_xChartModel, m_xCC );
238             m_pDialog->AddEventListener( LINK( this, CreationWizardUnoDlg, DialogEventHdl ) );
239         }
240 	}
241 }
242 //-------------------------------------------------------------------------
243 IMPL_LINK( CreationWizardUnoDlg, DialogEventHdl, VclWindowEvent*, pEvent )
244 {
245     if(pEvent && (pEvent->GetId() == VCLEVENT_OBJECT_DYING) )
246         m_pDialog = 0;//avoid duplicate destruction of m_pDialog
247     return 0;
248 }
249 
250 //-------------------------------------------------------------------------
251 sal_Int16 SAL_CALL CreationWizardUnoDlg::execute(  ) throw(uno::RuntimeException)
252 {
253     sal_Int16 nRet = RET_CANCEL;
254     {
255         ::vos::OGuard aSolarGuard( Application::GetSolarMutex());
256         createDialogOnDemand();
257         if( !m_pDialog )
258             return nRet;
259         TimerTriggeredControllerLock aTimerTriggeredControllerLock( m_xChartModel );
260         if( m_bUnlockControllersOnExecute && m_xChartModel.is() )
261             m_xChartModel->unlockControllers();
262         nRet = m_pDialog->Execute();
263     }
264     return nRet;
265 }
266 
267 //-------------------------------------------------------------------------
268 void SAL_CALL CreationWizardUnoDlg::initialize( const uno::Sequence< uno::Any >& aArguments ) throw(uno::Exception, uno::RuntimeException)
269 {
270 	const uno::Any* pArguments = aArguments.getConstArray();
271 	for(sal_Int32 i=0; i<aArguments.getLength(); ++i, ++pArguments)
272 	{
273         beans::PropertyValue aProperty;
274 		if(*pArguments >>= aProperty)
275 		{
276 			if( aProperty.Name.compareToAscii( RTL_CONSTASCII_STRINGPARAM( "ParentWindow" ) ) == 0 )
277 			{
278 				aProperty.Value >>= m_xParentWindow;
279 			}
280             else if( aProperty.Name.compareToAscii( RTL_CONSTASCII_STRINGPARAM( "ChartModel" ) ) == 0 )
281 			{
282 				aProperty.Value >>= m_xChartModel;
283 			}
284 		}
285 	}
286 }
287 
288 //-------------------------------------------------------------------------
289 // ____ OComponentHelper ____
290 /// Called in dispose method after the listeners were notified.
291 void SAL_CALL CreationWizardUnoDlg::disposing()
292 {
293     m_xChartModel.clear();
294     m_xParentWindow.clear();
295 
296     // /--
297     ::vos::OGuard aSolarGuard( Application::GetSolarMutex());
298     if( m_pDialog )
299     {
300         delete m_pDialog;
301         m_pDialog = 0;
302     }
303 
304     try
305     {
306         uno::Reference< frame::XDesktop > xDesktop(
307             m_xCC->getServiceManager()->createInstanceWithContext(
308                 C2U( "com.sun.star.frame.Desktop" ), m_xCC ), uno::UNO_QUERY );
309         if( xDesktop.is() )
310         {
311             uno::Reference< frame::XTerminateListener > xListener( this );
312             xDesktop->removeTerminateListener( xListener );
313         }
314     }
315     catch( const uno::Exception & ex )
316     {
317         ASSERT_EXCEPTION( ex );
318     }
319     // \--
320 }
321 
322 //XPropertySet
323 uno::Reference< beans::XPropertySetInfo > SAL_CALL CreationWizardUnoDlg::getPropertySetInfo()
324     throw (uno::RuntimeException)
325 {
326     OSL_ENSURE(false,"not implemented");
327     return 0;
328 }
329 
330 void SAL_CALL CreationWizardUnoDlg::setPropertyValue( const ::rtl::OUString& rPropertyName
331                                                      , const uno::Any& rValue )
332     throw (beans::UnknownPropertyException, beans::PropertyVetoException, lang::IllegalArgumentException
333           , lang::WrappedTargetException, uno::RuntimeException)
334 {
335     if( rPropertyName.equals(C2U("Position")) )
336     {
337         awt::Point aPos;
338         if( ! (rValue >>= aPos) )
339             throw lang::IllegalArgumentException( C2U("Property 'Position' requires value of type awt::Point"), 0, 0 );
340 
341         //set left upper outer corner relative to screen
342         //pixels, screen position
343         ::vos::OGuard aSolarGuard( Application::GetSolarMutex());
344         createDialogOnDemand();
345         if( m_pDialog )
346         {
347             m_pDialog->SetPosPixel( Point(0,0) );
348             Rectangle aRect( m_pDialog->GetWindowExtentsRelative( 0 ) );
349 
350             Point aNewOuterPos = Point( aPos.X - aRect.Left(), aPos.Y - aRect.Top() );
351             m_pDialog->SetPosPixel( aNewOuterPos );
352         }
353     }
354     else if( rPropertyName.equals(C2U("Size")) )
355     {
356         //read only property, do nothing
357     }
358     else if( rPropertyName.equals(C2U("UnlockControllersOnExecute")) )
359     {
360         if( ! (rValue >>= m_bUnlockControllersOnExecute) )
361             throw lang::IllegalArgumentException( C2U("Property 'UnlockControllers' requires value of type boolean"), 0, 0 );
362     }
363     else
364         throw beans::UnknownPropertyException( C2U("unknown property was tried to set to chart wizard"), 0 );
365 }
366 
367 uno::Any SAL_CALL CreationWizardUnoDlg::getPropertyValue( const ::rtl::OUString& rPropertyName )
368     throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
369 {
370     uno::Any aRet;
371     if( rPropertyName.equals(C2U("Position")) )
372     {
373         //get left upper outer corner relative to screen
374         //pixels, screen position
375         ::vos::OGuard aSolarGuard( Application::GetSolarMutex());
376         createDialogOnDemand();
377         if( m_pDialog )
378         {
379             Rectangle aRect( m_pDialog->GetWindowExtentsRelative( 0 ) );
380             awt::Point aPoint(aRect.Left(),aRect.Top());
381             aRet = uno::makeAny( aPoint );
382         }
383     }
384     else if( rPropertyName.equals(C2U("Size")) )
385     {
386         //get outer size inclusive decoration
387         //pixels, screen position
388         ::vos::OGuard aSolarGuard( Application::GetSolarMutex());
389         createDialogOnDemand();
390         if( m_pDialog )
391         {
392             Rectangle aRect( m_pDialog->GetWindowExtentsRelative( 0 ) );
393             awt::Size aSize(aRect.GetWidth(),aRect.GetHeight());
394             aRet = uno::makeAny( aSize );
395         }
396     }
397     else if( rPropertyName.equals(C2U("UnlockControllersOnExecute")) )
398     {
399         aRet = uno::makeAny( m_bUnlockControllersOnExecute );
400     }
401     else
402         throw beans::UnknownPropertyException( C2U("unknown property was tried to get from chart wizard"), 0 );
403     return aRet;
404 }
405 
406 void SAL_CALL CreationWizardUnoDlg::addPropertyChangeListener(
407         const ::rtl::OUString& /* aPropertyName */, const uno::Reference< beans::XPropertyChangeListener >& /* xListener */ )
408         throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
409 {
410     OSL_ENSURE(false,"not implemented");
411 }
412 void SAL_CALL CreationWizardUnoDlg::removePropertyChangeListener(
413     const ::rtl::OUString& /* aPropertyName */, const uno::Reference< beans::XPropertyChangeListener >& /* aListener */ )
414     throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
415 {
416     OSL_ENSURE(false,"not implemented");
417 }
418 
419 void SAL_CALL CreationWizardUnoDlg::addVetoableChangeListener( const ::rtl::OUString& /* PropertyName */, const uno::Reference< beans::XVetoableChangeListener >& /* aListener */ )
420     throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
421 {
422     OSL_ENSURE(false,"not implemented");
423 }
424 
425 void SAL_CALL CreationWizardUnoDlg::removeVetoableChangeListener( const ::rtl::OUString& /* PropertyName */, const uno::Reference< beans::XVetoableChangeListener >& /* aListener */ )
426     throw (beans::UnknownPropertyException, lang::WrappedTargetException, uno::RuntimeException)
427 {
428     OSL_ENSURE(false,"not implemented");
429 }
430 
431 //.............................................................................
432 } //namespace chart
433 //.............................................................................
434