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_sdext.hxx"
30 
31 #include "unodialog.hxx"
32 #include <com/sun/star/text/XTextRange.hpp>
33 #include <com/sun/star/drawing/XShapes.hpp>
34 #include <com/sun/star/container/XIndexAccess.hpp>
35 #include <com/sun/star/view/XSelectionSupplier.hpp>
36 #include <com/sun/star/view/XControlAccess.hpp>
37 #include <com/sun/star/frame/XDispatch.hpp>
38 #include <com/sun/star/awt/XMessageBoxFactory.hpp>
39 #include <com/sun/star/awt/MessageBoxButtons.hpp>
40 
41 // -------------
42 // - UnoDialog -
43 // -------------
44 
45 using namespace ::rtl;
46 using namespace ::com::sun::star::awt;
47 using namespace ::com::sun::star::uno;
48 using namespace ::com::sun::star::util;
49 using namespace ::com::sun::star::lang;
50 using namespace ::com::sun::star::view;
51 using namespace ::com::sun::star::frame;
52 using namespace ::com::sun::star::beans;
53 using namespace ::com::sun::star::script;
54 
55 UnoDialog::UnoDialog( const Reference< XComponentContext > &rxMSF, Reference< XFrame >& rxFrame ) :
56 	mxMSF( rxMSF ),
57 	mxController( rxFrame->getController() ),
58 	mxDialogModel( mxMSF->getServiceManager()->createInstanceWithContext( OUString( RTL_CONSTASCII_USTRINGPARAM(
59 		"com.sun.star.awt.UnoControlDialogModel" ) ), mxMSF ), UNO_QUERY_THROW ),
60 	mxDialogModelMultiPropertySet( mxDialogModel, UNO_QUERY_THROW ),
61 	mxDialogModelPropertySet( mxDialogModel, UNO_QUERY_THROW ),
62 	mxDialogModelMSF( mxDialogModel, UNO_QUERY_THROW ),
63 	mxDialogModelNameContainer( mxDialogModel, UNO_QUERY_THROW ),
64 	mxDialogModelNameAccess( mxDialogModel, UNO_QUERY_THROW ),
65 	mxControlModel( mxDialogModel, UNO_QUERY_THROW ),
66 	mxDialog( mxMSF->getServiceManager()->createInstanceWithContext( OUString( RTL_CONSTASCII_USTRINGPARAM(
67 		"com.sun.star.awt.UnoControlDialog" ) ), mxMSF ), UNO_QUERY_THROW ),
68 	mxControl( mxDialog, UNO_QUERY_THROW ),
69 	mbStatus( sal_False )
70 {
71 	mxControl->setModel( mxControlModel );
72 	mxDialogControlContainer = Reference< XControlContainer >( mxDialog, UNO_QUERY_THROW );
73 	mxDialogComponent = Reference< XComponent >( mxDialog, UNO_QUERY_THROW );
74 	mxDialogWindow = Reference< XWindow >( mxDialog, UNO_QUERY_THROW );
75 
76 	Reference< XFrame > xFrame( mxController->getFrame() );
77 	Reference< XWindow > xContainerWindow( xFrame->getContainerWindow() );
78 	mxWindowPeer = Reference< XWindowPeer >( xContainerWindow, UNO_QUERY_THROW );
79 	createWindowPeer( mxWindowPeer );
80 }
81 
82 // -----------------------------------------------------------------------------
83 
84 UnoDialog::~UnoDialog()
85 {
86 
87 }
88 
89 // -----------------------------------------------------------------------------
90 
91 void UnoDialog::execute()
92 {
93 	mxDialogWindow->setEnable( sal_True );
94 	mxDialogWindow->setVisible( sal_True );
95 	mxDialog->execute();
96 }
97 
98 void UnoDialog::endExecute( sal_Bool bStatus )
99 {
100 	mbStatus = bStatus;
101 	mxDialog->endExecute();
102 }
103 
104 // -----------------------------------------------------------------------------
105 
106 Reference< XWindowPeer > UnoDialog::createWindowPeer( Reference< XWindowPeer > xParentPeer )
107 	throw ( Exception )
108 {
109 	mxDialogWindow->setVisible( sal_False );
110 	Reference< XToolkit > xToolkit( mxMSF->getServiceManager()->createInstanceWithContext( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.Toolkit" ) ), mxMSF ), UNO_QUERY_THROW  );
111 	if ( !xParentPeer.is() )
112 		xParentPeer = xToolkit->getDesktopWindow();
113 	mxReschedule = Reference< XReschedule >( xToolkit, UNO_QUERY );
114 	mxControl->createPeer( xToolkit, xParentPeer );
115 //	xWindowPeer = xControl.getPeer();
116 	return mxControl->getPeer();
117 }
118 
119 // -----------------------------------------------------------------------------
120 
121 Reference< XInterface > UnoDialog::insertControlModel( const OUString& rServiceName, const OUString& rName,
122 														const Sequence< OUString >& rPropertyNames, const Sequence< Any >& rPropertyValues )
123 {
124 	Reference< XInterface > xControlModel;
125 	try
126 	{
127         xControlModel = mxDialogModelMSF->createInstance( rServiceName );
128 		Reference< XMultiPropertySet > xMultiPropSet( xControlModel, UNO_QUERY_THROW );
129 		xMultiPropSet->setPropertyValues( rPropertyNames, rPropertyValues );
130         mxDialogModelNameContainer->insertByName( rName, Any( xControlModel ) );
131     }
132 	catch( Exception& )
133 	{
134     }
135 	return xControlModel;
136 }
137 
138 // -----------------------------------------------------------------------------
139 
140 void UnoDialog::setVisible( const OUString& rName, sal_Bool bVisible )
141 {
142 	try
143 	{
144 		Reference< XInterface > xControl( mxDialogControlContainer->getControl( rName ), UNO_QUERY_THROW );
145 		Reference< XWindow > xWindow( xControl, UNO_QUERY_THROW );
146 		xWindow->setVisible( bVisible );
147 	}
148 	catch ( Exception& )
149 	{
150 	}
151 }
152 
153 // -----------------------------------------------------------------------------
154 
155 sal_Bool UnoDialog::isHighContrast()
156 {
157 	sal_Bool bHighContrast = sal_False;
158 	try
159 	{
160 		sal_Int32 nBackgroundColor = 0;
161 		if ( mxDialogModelPropertySet->getPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "BackgroundColor" ) ) ) >>= nBackgroundColor )
162 		{
163 			sal_uInt8 nLum( static_cast< sal_uInt8 >( ( static_cast< sal_uInt8 >( nBackgroundColor >> 16 ) * 28 +
164 														static_cast< sal_uInt8 >( nBackgroundColor >> 8 ) * 151 +
165 														static_cast< sal_uInt8 >( nBackgroundColor ) * 77 ) >> 8 ) );
166 			bHighContrast = nLum <= 38;
167 		}
168 	}
169 	catch( Exception& )
170 	{
171 	}
172 	return bHighContrast;
173 }
174 
175 // -----------------------------------------------------------------------------
176 
177 Reference< XButton > UnoDialog::insertButton( const OUString& rName, Reference< XActionListener > xActionListener,
178 			const Sequence< OUString >& rPropertyNames, const Sequence< Any >& rPropertyValues )
179 {
180 	Reference< XButton > xButton;
181 	try
182 	{
183         Reference< XInterface > xButtonModel( insertControlModel( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlButtonModel" ) ),
184 			rName, rPropertyNames, rPropertyValues ) );
185 		Reference< XPropertySet > xPropertySet( xButtonModel, UNO_QUERY_THROW );
186 		xPropertySet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ) ), Any( rName ) );
187 		xButton = Reference< XButton >( mxDialogControlContainer->getControl( rName ), UNO_QUERY_THROW );
188 
189 		if ( xActionListener.is() )
190 		{
191             xButton->addActionListener( xActionListener );
192 			xButton->setActionCommand( rName );
193 		}
194 		return xButton;
195 	}
196 	catch( Exception& )
197 	{
198 	}
199 	return xButton;
200 }
201 
202 // -----------------------------------------------------------------------------
203 
204 Reference< XFixedText > UnoDialog::insertFixedText( const OUString& rName, const Sequence< OUString > rPropertyNames, const Sequence< Any > rPropertyValues )
205 {
206 	Reference< XFixedText > xFixedText;
207 	try
208 	{
209 		Reference< XPropertySet > xPropertySet( insertControlModel( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlFixedTextModel" ) ),
210 			rName, rPropertyNames, rPropertyValues ), UNO_QUERY_THROW );
211         xPropertySet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ) ), Any( rName ) );
212 		xFixedText = Reference< XFixedText >( mxDialogControlContainer->getControl( rName ), UNO_QUERY_THROW );
213 	}
214 	catch ( Exception& )
215 	{
216     }
217 	return xFixedText;
218 }
219 
220 // -----------------------------------------------------------------------------
221 
222 Reference< XCheckBox > UnoDialog::insertCheckBox( const OUString& rName, const Sequence< OUString > rPropertyNames, const Sequence< Any > rPropertyValues )
223 {
224 	Reference< XCheckBox > xCheckBox;
225 	try
226 	{
227 		Reference< XPropertySet > xPropertySet( insertControlModel( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlCheckBoxModel" ) ),
228 			rName, rPropertyNames, rPropertyValues ), UNO_QUERY_THROW );
229         xPropertySet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ) ), Any( rName ) );
230 		xCheckBox = Reference< XCheckBox >( mxDialogControlContainer->getControl( rName ), UNO_QUERY_THROW );
231 	}
232 	catch ( Exception& )
233 	{
234     }
235 	return xCheckBox;
236 }
237 
238 // -----------------------------------------------------------------------------
239 
240 Reference< XControl > UnoDialog::insertFormattedField( const OUString& rName, const Sequence< OUString > rPropertyNames, const Sequence< Any > rPropertyValues )
241 {
242 	Reference< XControl > xControl;
243 	try
244 	{
245 		Reference< XPropertySet > xPropertySet( insertControlModel( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlFormattedFieldModel" ) ),
246 			rName, rPropertyNames, rPropertyValues ), UNO_QUERY_THROW );
247         xPropertySet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ) ), Any( rName ) );
248 		xControl = Reference< XControl >( mxDialogControlContainer->getControl( rName ), UNO_QUERY_THROW );
249 	}
250 	catch ( Exception& )
251 	{
252     }
253 	return xControl;
254 }
255 
256 // -----------------------------------------------------------------------------
257 
258 Reference< XComboBox > UnoDialog::insertComboBox( const OUString& rName, const Sequence< OUString > rPropertyNames, const Sequence< Any > rPropertyValues )
259 {
260 	Reference< XComboBox > xControl;
261 	try
262 	{
263 		Reference< XPropertySet > xPropertySet( insertControlModel( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlComboBoxModel" ) ),
264 			rName, rPropertyNames, rPropertyValues ), UNO_QUERY_THROW );
265         xPropertySet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ) ), Any( rName ) );
266 		xControl = Reference< XComboBox >( mxDialogControlContainer->getControl( rName ), UNO_QUERY_THROW );
267 	}
268 	catch ( Exception& )
269 	{
270 	}
271 	return xControl;
272 }
273 
274 // -----------------------------------------------------------------------------
275 
276 Reference< XRadioButton > UnoDialog::insertRadioButton( const OUString& rName, const Sequence< OUString > rPropertyNames, const Sequence< Any > rPropertyValues )
277 {
278 	Reference< XRadioButton > xControl;
279 	try
280 	{
281 		Reference< XPropertySet > xPropertySet( insertControlModel( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlRadioButtonModel" ) ),
282 			rName, rPropertyNames, rPropertyValues ), UNO_QUERY_THROW );
283         xPropertySet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ) ), Any( rName ) );
284 		xControl = Reference< XRadioButton >( mxDialogControlContainer->getControl( rName ), UNO_QUERY_THROW );
285 	}
286 	catch ( Exception& )
287 	{
288 	}
289 	return xControl;
290 }
291 
292 // -----------------------------------------------------------------------------
293 
294 Reference< XListBox > UnoDialog::insertListBox( const OUString& rName, const Sequence< OUString > rPropertyNames, const Sequence< Any > rPropertyValues )
295 {
296 	Reference< XListBox > xControl;
297 	try
298 	{
299 		Reference< XPropertySet > xPropertySet( insertControlModel( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlListBoxModel" ) ),
300 			rName, rPropertyNames, rPropertyValues ), UNO_QUERY_THROW );
301         xPropertySet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ) ), Any( rName ) );
302 		xControl = Reference< XListBox >( mxDialogControlContainer->getControl( rName ), UNO_QUERY_THROW );
303 	}
304 	catch ( Exception& )
305 	{
306 	}
307 	return xControl;
308 }
309 
310 // -----------------------------------------------------------------------------
311 
312 Reference< XControl > UnoDialog::insertImage( const OUString& rName, const Sequence< OUString > rPropertyNames, const Sequence< Any > rPropertyValues )
313 {
314 	Reference< XControl > xControl;
315 	try
316 	{
317 		Reference< XPropertySet > xPropertySet( insertControlModel( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlImageControlModel" ) ),
318 			rName, rPropertyNames, rPropertyValues ), UNO_QUERY_THROW );
319         xPropertySet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ) ), Any( rName ) );
320 		xControl = Reference< XControl >( mxDialogControlContainer->getControl( rName ), UNO_QUERY_THROW );
321 	}
322 	catch ( Exception& )
323 	{
324     }
325 	return xControl;
326 }
327 
328 // -----------------------------------------------------------------------------
329 
330 void UnoDialog::setControlProperty( const OUString& rControlName, const OUString& rPropertyName, const Any& rPropertyValue )
331 {
332     try
333 	{
334 		if ( mxDialogModelNameAccess->hasByName( rControlName ) )
335 		{
336 			Reference< XPropertySet > xPropertySet( mxDialogModelNameAccess->getByName( rControlName ), UNO_QUERY_THROW );
337 			xPropertySet->setPropertyValue( rPropertyName, rPropertyValue );
338 		}
339     }
340 	catch ( Exception& )
341 	{
342     }
343 }
344 
345 // -----------------------------------------------------------------------------
346 #if 0
347 void UnoDialog::showMessageBox( const OUString& rTitle, const OUString& rMessage, sal_Bool bErrorBox ) const
348 {
349 	try
350 	{
351 		Reference< XMessageBoxFactory > xMessageBoxFactory( mxMSF->getServiceManager()->createInstanceWithContext( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.Toolkit" ) ), mxMSF ), UNO_QUERY_THROW  );
352 		if ( xMessageBoxFactory.is() )
353 		{
354 			Rectangle aRectangle( 0, 0, 0, 0 );
355 			Reference< XMessageBox > xMessageBox( xMessageBoxFactory->createMessageBox( mxWindowPeer, aRectangle,
356 				bErrorBox ? OUString( RTL_CONSTASCII_USTRINGPARAM( "errorbox" ) ) : OUString( RTL_CONSTASCII_USTRINGPARAM( "querybox" ) ), MessageBoxButtons::BUTTONS_OK, rTitle, rMessage ) );
357 			Reference< XComponent > xComponent( xMessageBox, UNO_QUERY_THROW );
358 			/* sal_Int16 nResult = */ xMessageBox->execute();
359 			xComponent->dispose();
360 		}
361 	}
362 	catch ( Exception& )
363 	{
364 	}
365 
366 /*
367 public void showErrorMessageBox(XWindowPeer _xParentWindowPeer, String _sTitle, String _sMessage){
368 XComponent xComponent = null;
369 try {
370     Object oToolkit = m_xMCF.createInstanceWithContext("com.sun.star.awt.Toolkit", m_xContext);
371     XMessageBoxFactory xMessageBoxFactory = (XMessageBoxFactory) UnoRuntime.queryInterface(XMessageBoxFactory.class, oToolkit);
372     // rectangle may be empty if position is in the center of the parent peer
373 
374 	Rectangle aRectangle = new Rectangle();
375     XMessageBox xMessageBox = xMessageBoxFactory.createMessageBox(_xParentWindowPeer, aRectangle, "errorbox", com.sun.star.awt.MessageBoxButtons.BUTTONS_OK, _sTitle, _sMessage);
376     xComponent = (XComponent) UnoRuntime.queryInterface(XComponent.class, xMessageBox);
377     if (xMessageBox != null){
378         short nResult = xMessageBox.execute();
379     }
380 } catch (com.sun.star.uno.Exception ex) {
381     ex.printStackTrace(System.out);
382 }
383 finally{
384     //make sure always to dispose the component and free the memory!
385     if (xComponent != null){
386         xComponent.dispose();
387     }
388 }}
389 */
390 }
391 
392 #endif
393 
394 // -----------------------------------------------------------------------------
395 
396 sal_Int32 UnoDialog::getMapsFromPixels( sal_Int32 nPixels ) const
397 {
398 	double dMaps = 0;
399 	try
400 	{
401 		sal_Int32 nMapWidth = 0;
402 		const OUString sWidth( RTL_CONSTASCII_USTRINGPARAM( "Width" ) );
403 		if ( mxDialogModelPropertySet->getPropertyValue( sWidth  ) >>= nMapWidth )
404 		{
405 			Reference< XWindow > xWindow( mxDialog, UNO_QUERY_THROW );
406 			double pxWidth = xWindow->getPosSize().Width;
407 			double mapRatio = ( pxWidth / nMapWidth );
408 			dMaps = nPixels / mapRatio;
409 		}
410 	}
411 	catch ( Exception& )
412 	{
413 	}
414 	return static_cast< sal_Int32 >( dMaps );
415 }
416 
417 // -----------------------------------------------------------------------------
418 
419 Any UnoDialog::getControlProperty( const OUString& rControlName, const OUString& rPropertyName )
420 {
421 	Any aRet;
422     try
423 	{
424 		if ( mxDialogModelNameAccess->hasByName( rControlName ) )
425 		{
426 			Reference< XPropertySet > xPropertySet( mxDialogModelNameAccess->getByName( rControlName ), UNO_QUERY_THROW );
427 			aRet = xPropertySet->getPropertyValue( rPropertyName );
428 		}
429     }
430 	catch ( Exception& )
431 	{
432     }
433 	return aRet;
434 }
435 
436 // -----------------------------------------------------------------------------
437 
438 void UnoDialog::enableControl( const OUString& rControlName )
439 {
440 	const OUString sEnabled( RTL_CONSTASCII_USTRINGPARAM( "Enabled" ) );
441 	setControlProperty( rControlName, sEnabled, Any( sal_True ) );
442 }
443 
444 // -----------------------------------------------------------------------------
445 
446 void UnoDialog::disableControl( const OUString& rControlName )
447 {
448 	const OUString sEnabled( RTL_CONSTASCII_USTRINGPARAM( "Enabled" ) );
449 	setControlProperty( rControlName, sEnabled, Any( sal_False ) );
450 }
451 
452 // -----------------------------------------------------------------------------
453