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