1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sdext.hxx"
26 
27 #include "unodialog.hxx"
28 #include <com/sun/star/text/XTextRange.hpp>
29 #include <com/sun/star/drawing/XShapes.hpp>
30 #include <com/sun/star/container/XIndexAccess.hpp>
31 #include <com/sun/star/view/XSelectionSupplier.hpp>
32 #include <com/sun/star/view/XControlAccess.hpp>
33 #include <com/sun/star/frame/XDispatch.hpp>
34 #include <com/sun/star/awt/XMessageBoxFactory.hpp>
35 #include <com/sun/star/awt/MessageBoxButtons.hpp>
36 
37 // -------------
38 // - UnoDialog -
39 // -------------
40 
41 using namespace ::rtl;
42 using namespace ::com::sun::star::awt;
43 using namespace ::com::sun::star::uno;
44 using namespace ::com::sun::star::util;
45 using namespace ::com::sun::star::lang;
46 using namespace ::com::sun::star::view;
47 using namespace ::com::sun::star::frame;
48 using namespace ::com::sun::star::beans;
49 using namespace ::com::sun::star::script;
50 
51 UnoDialog::UnoDialog( const Reference< XComponentContext > &rxMSF, Reference< XFrame >& rxFrame ) :
52 	mxMSF( rxMSF ),
53 	mxController( rxFrame->getController() ),
54 	mxDialogModel( mxMSF->getServiceManager()->createInstanceWithContext( OUString( RTL_CONSTASCII_USTRINGPARAM(
55 		"com.sun.star.awt.UnoControlDialogModel" ) ), mxMSF ), UNO_QUERY_THROW ),
56 	mxDialogModelMultiPropertySet( mxDialogModel, UNO_QUERY_THROW ),
57 	mxDialogModelPropertySet( mxDialogModel, UNO_QUERY_THROW ),
58 	mxDialogModelMSF( mxDialogModel, UNO_QUERY_THROW ),
59 	mxDialogModelNameContainer( mxDialogModel, UNO_QUERY_THROW ),
60 	mxDialogModelNameAccess( mxDialogModel, UNO_QUERY_THROW ),
61 	mxControlModel( mxDialogModel, UNO_QUERY_THROW ),
62 	mxDialog( mxMSF->getServiceManager()->createInstanceWithContext( OUString( RTL_CONSTASCII_USTRINGPARAM(
63 		"com.sun.star.awt.UnoControlDialog" ) ), mxMSF ), UNO_QUERY_THROW ),
64 	mxControl( mxDialog, UNO_QUERY_THROW ),
65 	mbStatus( sal_False )
66 {
67 	mxControl->setModel( mxControlModel );
68 	mxDialogControlContainer = Reference< XControlContainer >( mxDialog, UNO_QUERY_THROW );
69 	mxDialogComponent = Reference< XComponent >( mxDialog, UNO_QUERY_THROW );
70 	mxDialogWindow = Reference< XWindow >( mxDialog, UNO_QUERY_THROW );
71 
72 	Reference< XFrame > xFrame( mxController->getFrame() );
73 	Reference< XWindow > xContainerWindow( xFrame->getContainerWindow() );
74 	mxWindowPeer = Reference< XWindowPeer >( xContainerWindow, UNO_QUERY_THROW );
75 	createWindowPeer( mxWindowPeer );
76 }
77 
78 // -----------------------------------------------------------------------------
79 
80 UnoDialog::~UnoDialog()
81 {
82 
83 }
84 
85 // -----------------------------------------------------------------------------
86 
87 void UnoDialog::execute()
88 {
89 	mxDialogWindow->setEnable( sal_True );
90 	mxDialogWindow->setVisible( sal_True );
91 	mxDialog->execute();
92 }
93 
94 void UnoDialog::endExecute( sal_Bool bStatus )
95 {
96 	mbStatus = bStatus;
97 	mxDialog->endExecute();
98 }
99 
100 // -----------------------------------------------------------------------------
101 
102 Reference< XWindowPeer > UnoDialog::createWindowPeer( Reference< XWindowPeer > xParentPeer )
103 	throw ( Exception )
104 {
105 	mxDialogWindow->setVisible( sal_False );
106 	Reference< XToolkit > xToolkit( mxMSF->getServiceManager()->createInstanceWithContext( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.Toolkit" ) ), mxMSF ), UNO_QUERY_THROW  );
107 	if ( !xParentPeer.is() )
108 		xParentPeer = xToolkit->getDesktopWindow();
109 	mxReschedule = Reference< XReschedule >( xToolkit, UNO_QUERY );
110 	mxControl->createPeer( xToolkit, xParentPeer );
111 //	xWindowPeer = xControl.getPeer();
112 	return mxControl->getPeer();
113 }
114 
115 // -----------------------------------------------------------------------------
116 
117 Reference< XInterface > UnoDialog::insertControlModel( const OUString& rServiceName, const OUString& rName,
118 														const Sequence< OUString >& rPropertyNames, const Sequence< Any >& rPropertyValues )
119 {
120 	Reference< XInterface > xControlModel;
121 	try
122 	{
123         xControlModel = mxDialogModelMSF->createInstance( rServiceName );
124 		Reference< XMultiPropertySet > xMultiPropSet( xControlModel, UNO_QUERY_THROW );
125 		xMultiPropSet->setPropertyValues( rPropertyNames, rPropertyValues );
126         mxDialogModelNameContainer->insertByName( rName, Any( xControlModel ) );
127     }
128 	catch( Exception& )
129 	{
130     }
131 	return xControlModel;
132 }
133 
134 // -----------------------------------------------------------------------------
135 
136 void UnoDialog::setVisible( const OUString& rName, sal_Bool bVisible )
137 {
138 	try
139 	{
140 		Reference< XInterface > xControl( mxDialogControlContainer->getControl( rName ), UNO_QUERY_THROW );
141 		Reference< XWindow > xWindow( xControl, UNO_QUERY_THROW );
142 		xWindow->setVisible( bVisible );
143 	}
144 	catch ( Exception& )
145 	{
146 	}
147 }
148 
149 // -----------------------------------------------------------------------------
150 
151 sal_Bool UnoDialog::isHighContrast()
152 {
153 	sal_Bool bHighContrast = sal_False;
154 	try
155 	{
156 		sal_Int32 nBackgroundColor = 0;
157 		if ( mxDialogModelPropertySet->getPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "BackgroundColor" ) ) ) >>= nBackgroundColor )
158 		{
159 			sal_uInt8 nLum( static_cast< sal_uInt8 >( ( static_cast< sal_uInt8 >( nBackgroundColor >> 16 ) * 28 +
160 														static_cast< sal_uInt8 >( nBackgroundColor >> 8 ) * 151 +
161 														static_cast< sal_uInt8 >( nBackgroundColor ) * 77 ) >> 8 ) );
162 			bHighContrast = nLum <= 38;
163 		}
164 	}
165 	catch( Exception& )
166 	{
167 	}
168 	return bHighContrast;
169 }
170 
171 // -----------------------------------------------------------------------------
172 
173 Reference< XButton > UnoDialog::insertButton( const OUString& rName, Reference< XActionListener > xActionListener,
174 			const Sequence< OUString >& rPropertyNames, const Sequence< Any >& rPropertyValues )
175 {
176 	Reference< XButton > xButton;
177 	try
178 	{
179         Reference< XInterface > xButtonModel( insertControlModel( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlButtonModel" ) ),
180 			rName, rPropertyNames, rPropertyValues ) );
181 		Reference< XPropertySet > xPropertySet( xButtonModel, UNO_QUERY_THROW );
182 		xPropertySet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ) ), Any( rName ) );
183 		xButton = Reference< XButton >( mxDialogControlContainer->getControl( rName ), UNO_QUERY_THROW );
184 
185 		if ( xActionListener.is() )
186 		{
187             xButton->addActionListener( xActionListener );
188 			xButton->setActionCommand( rName );
189 		}
190 		return xButton;
191 	}
192 	catch( Exception& )
193 	{
194 	}
195 	return xButton;
196 }
197 
198 // -----------------------------------------------------------------------------
199 
200 Reference< XFixedText > UnoDialog::insertFixedText( const OUString& rName, const Sequence< OUString > rPropertyNames, const Sequence< Any > rPropertyValues )
201 {
202 	Reference< XFixedText > xFixedText;
203 	try
204 	{
205 		Reference< XPropertySet > xPropertySet( insertControlModel( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlFixedTextModel" ) ),
206 			rName, rPropertyNames, rPropertyValues ), UNO_QUERY_THROW );
207         xPropertySet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ) ), Any( rName ) );
208 		xFixedText = Reference< XFixedText >( mxDialogControlContainer->getControl( rName ), UNO_QUERY_THROW );
209 	}
210 	catch ( Exception& )
211 	{
212     }
213 	return xFixedText;
214 }
215 
216 // -----------------------------------------------------------------------------
217 
218 Reference< XCheckBox > UnoDialog::insertCheckBox( const OUString& rName, const Sequence< OUString > rPropertyNames, const Sequence< Any > rPropertyValues )
219 {
220 	Reference< XCheckBox > xCheckBox;
221 	try
222 	{
223 		Reference< XPropertySet > xPropertySet( insertControlModel( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlCheckBoxModel" ) ),
224 			rName, rPropertyNames, rPropertyValues ), UNO_QUERY_THROW );
225         xPropertySet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ) ), Any( rName ) );
226 		xCheckBox = Reference< XCheckBox >( mxDialogControlContainer->getControl( rName ), UNO_QUERY_THROW );
227 	}
228 	catch ( Exception& )
229 	{
230     }
231 	return xCheckBox;
232 }
233 
234 // -----------------------------------------------------------------------------
235 
236 Reference< XControl > UnoDialog::insertFormattedField( const OUString& rName, const Sequence< OUString > rPropertyNames, const Sequence< Any > rPropertyValues )
237 {
238 	Reference< XControl > xControl;
239 	try
240 	{
241 		Reference< XPropertySet > xPropertySet( insertControlModel( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlFormattedFieldModel" ) ),
242 			rName, rPropertyNames, rPropertyValues ), UNO_QUERY_THROW );
243         xPropertySet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ) ), Any( rName ) );
244 		xControl = Reference< XControl >( mxDialogControlContainer->getControl( rName ), UNO_QUERY_THROW );
245 	}
246 	catch ( Exception& )
247 	{
248     }
249 	return xControl;
250 }
251 
252 // -----------------------------------------------------------------------------
253 
254 Reference< XComboBox > UnoDialog::insertComboBox( const OUString& rName, const Sequence< OUString > rPropertyNames, const Sequence< Any > rPropertyValues )
255 {
256 	Reference< XComboBox > xControl;
257 	try
258 	{
259 		Reference< XPropertySet > xPropertySet( insertControlModel( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlComboBoxModel" ) ),
260 			rName, rPropertyNames, rPropertyValues ), UNO_QUERY_THROW );
261         xPropertySet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ) ), Any( rName ) );
262 		xControl = Reference< XComboBox >( mxDialogControlContainer->getControl( rName ), UNO_QUERY_THROW );
263 	}
264 	catch ( Exception& )
265 	{
266 	}
267 	return xControl;
268 }
269 
270 // -----------------------------------------------------------------------------
271 
272 Reference< XRadioButton > UnoDialog::insertRadioButton( const OUString& rName, const Sequence< OUString > rPropertyNames, const Sequence< Any > rPropertyValues )
273 {
274 	Reference< XRadioButton > xControl;
275 	try
276 	{
277 		Reference< XPropertySet > xPropertySet( insertControlModel( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlRadioButtonModel" ) ),
278 			rName, rPropertyNames, rPropertyValues ), UNO_QUERY_THROW );
279         xPropertySet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ) ), Any( rName ) );
280 		xControl = Reference< XRadioButton >( mxDialogControlContainer->getControl( rName ), UNO_QUERY_THROW );
281 	}
282 	catch ( Exception& )
283 	{
284 	}
285 	return xControl;
286 }
287 
288 // -----------------------------------------------------------------------------
289 
290 Reference< XListBox > UnoDialog::insertListBox( const OUString& rName, const Sequence< OUString > rPropertyNames, const Sequence< Any > rPropertyValues )
291 {
292 	Reference< XListBox > xControl;
293 	try
294 	{
295 		Reference< XPropertySet > xPropertySet( insertControlModel( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlListBoxModel" ) ),
296 			rName, rPropertyNames, rPropertyValues ), UNO_QUERY_THROW );
297         xPropertySet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ) ), Any( rName ) );
298 		xControl = Reference< XListBox >( mxDialogControlContainer->getControl( rName ), UNO_QUERY_THROW );
299 	}
300 	catch ( Exception& )
301 	{
302 	}
303 	return xControl;
304 }
305 
306 // -----------------------------------------------------------------------------
307 
308 Reference< XControl > UnoDialog::insertImage( const OUString& rName, const Sequence< OUString > rPropertyNames, const Sequence< Any > rPropertyValues )
309 {
310 	Reference< XControl > xControl;
311 	try
312 	{
313 		Reference< XPropertySet > xPropertySet( insertControlModel( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlImageControlModel" ) ),
314 			rName, rPropertyNames, rPropertyValues ), UNO_QUERY_THROW );
315         xPropertySet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ) ), Any( rName ) );
316 		xControl = Reference< XControl >( mxDialogControlContainer->getControl( rName ), UNO_QUERY_THROW );
317 	}
318 	catch ( Exception& )
319 	{
320     }
321 	return xControl;
322 }
323 
324 // -----------------------------------------------------------------------------
325 
326 void UnoDialog::setControlProperty( const OUString& rControlName, const OUString& rPropertyName, const Any& rPropertyValue )
327 {
328     try
329 	{
330 		if ( mxDialogModelNameAccess->hasByName( rControlName ) )
331 		{
332 			Reference< XPropertySet > xPropertySet( mxDialogModelNameAccess->getByName( rControlName ), UNO_QUERY_THROW );
333 			xPropertySet->setPropertyValue( rPropertyName, rPropertyValue );
334 		}
335     }
336 	catch ( Exception& )
337 	{
338     }
339 }
340 
341 // -----------------------------------------------------------------------------
342 
343 sal_Int32 UnoDialog::getMapsFromPixels( sal_Int32 nPixels ) const
344 {
345 	double dMaps = 0;
346 	try
347 	{
348 		sal_Int32 nMapWidth = 0;
349 		const OUString sWidth( RTL_CONSTASCII_USTRINGPARAM( "Width" ) );
350 		if ( mxDialogModelPropertySet->getPropertyValue( sWidth  ) >>= nMapWidth )
351 		{
352 			Reference< XWindow > xWindow( mxDialog, UNO_QUERY_THROW );
353 			double pxWidth = xWindow->getPosSize().Width;
354 			double mapRatio = ( pxWidth / nMapWidth );
355 			dMaps = nPixels / mapRatio;
356 		}
357 	}
358 	catch ( Exception& )
359 	{
360 	}
361 	return static_cast< sal_Int32 >( dMaps );
362 }
363 
364 // -----------------------------------------------------------------------------
365 
366 Any UnoDialog::getControlProperty( const OUString& rControlName, const OUString& rPropertyName )
367 {
368 	Any aRet;
369     try
370 	{
371 		if ( mxDialogModelNameAccess->hasByName( rControlName ) )
372 		{
373 			Reference< XPropertySet > xPropertySet( mxDialogModelNameAccess->getByName( rControlName ), UNO_QUERY_THROW );
374 			aRet = xPropertySet->getPropertyValue( rPropertyName );
375 		}
376     }
377 	catch ( Exception& )
378 	{
379     }
380 	return aRet;
381 }
382 
383 // -----------------------------------------------------------------------------
384 
385 void UnoDialog::enableControl( const OUString& rControlName )
386 {
387 	const OUString sEnabled( RTL_CONSTASCII_USTRINGPARAM( "Enabled" ) );
388 	setControlProperty( rControlName, sEnabled, Any( sal_True ) );
389 }
390 
391 // -----------------------------------------------------------------------------
392 
393 void UnoDialog::disableControl( const OUString& rControlName )
394 {
395 	const OUString sEnabled( RTL_CONSTASCII_USTRINGPARAM( "Enabled" ) );
396 	setControlProperty( rControlName, sEnabled, Any( sal_False ) );
397 }
398 
399 // -----------------------------------------------------------------------------
400