update dev300-m58
[ooovba.git] / sdext / source / minimizer / unodialog.cxx
blobaa5101dfbf2006f42563e58f7d80ce747f610b48
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: unodialog.cxx,v $
11 * $Revision: 1.7 $
13 * This file is part of OpenOffice.org.
15 * OpenOffice.org is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License version 3
17 * only, as published by the Free Software Foundation.
19 * OpenOffice.org is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License version 3 for more details
23 * (a copy is included in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU Lesser General Public License
26 * version 3 along with OpenOffice.org. If not, see
27 * <http://www.openoffice.org/license.html>
28 * for a copy of the LGPLv3 License.
30 ************************************************************************/
32 // MARKER(update_precomp.py): autogen include statement, do not remove
33 #include "precompiled_sdext.hxx"
35 #include "unodialog.hxx"
36 #include <com/sun/star/text/XTextRange.hpp>
37 #include <com/sun/star/drawing/XShapes.hpp>
38 #include <com/sun/star/container/XIndexAccess.hpp>
39 #include <com/sun/star/view/XSelectionSupplier.hpp>
40 #include <com/sun/star/view/XControlAccess.hpp>
41 #include <com/sun/star/frame/XDispatch.hpp>
42 #include <com/sun/star/awt/XMessageBoxFactory.hpp>
43 #include <com/sun/star/awt/MessageBoxButtons.hpp>
45 // -------------
46 // - UnoDialog -
47 // -------------
49 using namespace ::rtl;
50 using namespace ::com::sun::star::awt;
51 using namespace ::com::sun::star::uno;
52 using namespace ::com::sun::star::util;
53 using namespace ::com::sun::star::lang;
54 using namespace ::com::sun::star::view;
55 using namespace ::com::sun::star::frame;
56 using namespace ::com::sun::star::beans;
57 using namespace ::com::sun::star::script;
59 UnoDialog::UnoDialog( const Reference< XComponentContext > &rxMSF, Reference< XFrame >& rxFrame ) :
60 mxMSF( rxMSF ),
61 mxController( rxFrame->getController() ),
62 mxDialogModel( mxMSF->getServiceManager()->createInstanceWithContext( OUString( RTL_CONSTASCII_USTRINGPARAM(
63 "com.sun.star.awt.UnoControlDialogModel" ) ), mxMSF ), UNO_QUERY_THROW ),
64 mxDialogModelMultiPropertySet( mxDialogModel, UNO_QUERY_THROW ),
65 mxDialogModelPropertySet( mxDialogModel, UNO_QUERY_THROW ),
66 mxDialogModelMSF( mxDialogModel, UNO_QUERY_THROW ),
67 mxDialogModelNameContainer( mxDialogModel, UNO_QUERY_THROW ),
68 mxDialogModelNameAccess( mxDialogModel, UNO_QUERY_THROW ),
69 mxControlModel( mxDialogModel, UNO_QUERY_THROW ),
70 mxDialog( mxMSF->getServiceManager()->createInstanceWithContext( OUString( RTL_CONSTASCII_USTRINGPARAM(
71 "com.sun.star.awt.UnoControlDialog" ) ), mxMSF ), UNO_QUERY_THROW ),
72 mxControl( mxDialog, UNO_QUERY_THROW ),
73 mbStatus( sal_False )
75 mxControl->setModel( mxControlModel );
76 mxDialogControlContainer = Reference< XControlContainer >( mxDialog, UNO_QUERY_THROW );
77 mxDialogComponent = Reference< XComponent >( mxDialog, UNO_QUERY_THROW );
78 mxDialogWindow = Reference< XWindow >( mxDialog, UNO_QUERY_THROW );
80 Reference< XFrame > xFrame( mxController->getFrame() );
81 Reference< XWindow > xContainerWindow( xFrame->getContainerWindow() );
82 mxWindowPeer = Reference< XWindowPeer >( xContainerWindow, UNO_QUERY_THROW );
83 createWindowPeer( mxWindowPeer );
86 // -----------------------------------------------------------------------------
88 UnoDialog::~UnoDialog()
93 // -----------------------------------------------------------------------------
95 void UnoDialog::execute()
97 mxDialogWindow->setEnable( sal_True );
98 mxDialogWindow->setVisible( sal_True );
99 mxDialog->execute();
102 void UnoDialog::endExecute( sal_Bool bStatus )
104 mbStatus = bStatus;
105 mxDialog->endExecute();
108 // -----------------------------------------------------------------------------
110 Reference< XWindowPeer > UnoDialog::createWindowPeer( Reference< XWindowPeer > xParentPeer )
111 throw ( Exception )
113 mxDialogWindow->setVisible( sal_False );
114 Reference< XToolkit > xToolkit( mxMSF->getServiceManager()->createInstanceWithContext( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.Toolkit" ) ), mxMSF ), UNO_QUERY_THROW );
115 if ( !xParentPeer.is() )
116 xParentPeer = xToolkit->getDesktopWindow();
117 mxReschedule = Reference< XReschedule >( xToolkit, UNO_QUERY );
118 mxControl->createPeer( xToolkit, xParentPeer );
119 // xWindowPeer = xControl.getPeer();
120 return mxControl->getPeer();
123 // -----------------------------------------------------------------------------
125 Reference< XInterface > UnoDialog::insertControlModel( const OUString& rServiceName, const OUString& rName,
126 const Sequence< OUString >& rPropertyNames, const Sequence< Any >& rPropertyValues )
128 Reference< XInterface > xControlModel;
131 xControlModel = mxDialogModelMSF->createInstance( rServiceName );
132 Reference< XMultiPropertySet > xMultiPropSet( xControlModel, UNO_QUERY_THROW );
133 xMultiPropSet->setPropertyValues( rPropertyNames, rPropertyValues );
134 mxDialogModelNameContainer->insertByName( rName, Any( xControlModel ) );
136 catch( Exception& )
139 return xControlModel;
142 // -----------------------------------------------------------------------------
144 void UnoDialog::setVisible( const OUString& rName, sal_Bool bVisible )
148 Reference< XInterface > xControl( mxDialogControlContainer->getControl( rName ), UNO_QUERY_THROW );
149 Reference< XWindow > xWindow( xControl, UNO_QUERY_THROW );
150 xWindow->setVisible( bVisible );
152 catch ( Exception& )
157 // -----------------------------------------------------------------------------
159 sal_Bool UnoDialog::isHighContrast()
161 sal_Bool bHighContrast = sal_False;
164 sal_Int32 nBackgroundColor = 0;
165 if ( mxDialogModelPropertySet->getPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "BackgroundColor" ) ) ) >>= nBackgroundColor )
167 sal_uInt8 nLum( static_cast< sal_uInt8 >( ( static_cast< sal_uInt8 >( nBackgroundColor >> 16 ) * 28 +
168 static_cast< sal_uInt8 >( nBackgroundColor >> 8 ) * 151 +
169 static_cast< sal_uInt8 >( nBackgroundColor ) * 77 ) >> 8 ) );
170 bHighContrast = nLum <= 38;
173 catch( Exception& )
176 return bHighContrast;
179 // -----------------------------------------------------------------------------
181 Reference< XButton > UnoDialog::insertButton( const OUString& rName, Reference< XActionListener > xActionListener,
182 const Sequence< OUString >& rPropertyNames, const Sequence< Any >& rPropertyValues )
184 Reference< XButton > xButton;
187 Reference< XInterface > xButtonModel( insertControlModel( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlButtonModel" ) ),
188 rName, rPropertyNames, rPropertyValues ) );
189 Reference< XPropertySet > xPropertySet( xButtonModel, UNO_QUERY_THROW );
190 xPropertySet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ) ), Any( rName ) );
191 xButton = Reference< XButton >( mxDialogControlContainer->getControl( rName ), UNO_QUERY_THROW );
193 if ( xActionListener.is() )
195 xButton->addActionListener( xActionListener );
196 xButton->setActionCommand( rName );
198 return xButton;
200 catch( Exception& )
203 return xButton;
206 // -----------------------------------------------------------------------------
208 Reference< XFixedText > UnoDialog::insertFixedText( const OUString& rName, const Sequence< OUString > rPropertyNames, const Sequence< Any > rPropertyValues )
210 Reference< XFixedText > xFixedText;
213 Reference< XPropertySet > xPropertySet( insertControlModel( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlFixedTextModel" ) ),
214 rName, rPropertyNames, rPropertyValues ), UNO_QUERY_THROW );
215 xPropertySet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ) ), Any( rName ) );
216 xFixedText = Reference< XFixedText >( mxDialogControlContainer->getControl( rName ), UNO_QUERY_THROW );
218 catch ( Exception& )
221 return xFixedText;
224 // -----------------------------------------------------------------------------
226 Reference< XCheckBox > UnoDialog::insertCheckBox( const OUString& rName, const Sequence< OUString > rPropertyNames, const Sequence< Any > rPropertyValues )
228 Reference< XCheckBox > xCheckBox;
231 Reference< XPropertySet > xPropertySet( insertControlModel( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlCheckBoxModel" ) ),
232 rName, rPropertyNames, rPropertyValues ), UNO_QUERY_THROW );
233 xPropertySet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ) ), Any( rName ) );
234 xCheckBox = Reference< XCheckBox >( mxDialogControlContainer->getControl( rName ), UNO_QUERY_THROW );
236 catch ( Exception& )
239 return xCheckBox;
242 // -----------------------------------------------------------------------------
244 Reference< XControl > UnoDialog::insertFormattedField( const OUString& rName, const Sequence< OUString > rPropertyNames, const Sequence< Any > rPropertyValues )
246 Reference< XControl > xControl;
249 Reference< XPropertySet > xPropertySet( insertControlModel( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlFormattedFieldModel" ) ),
250 rName, rPropertyNames, rPropertyValues ), UNO_QUERY_THROW );
251 xPropertySet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ) ), Any( rName ) );
252 xControl = Reference< XControl >( mxDialogControlContainer->getControl( rName ), UNO_QUERY_THROW );
254 catch ( Exception& )
257 return xControl;
260 // -----------------------------------------------------------------------------
262 Reference< XComboBox > UnoDialog::insertComboBox( const OUString& rName, const Sequence< OUString > rPropertyNames, const Sequence< Any > rPropertyValues )
264 Reference< XComboBox > xControl;
267 Reference< XPropertySet > xPropertySet( insertControlModel( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlComboBoxModel" ) ),
268 rName, rPropertyNames, rPropertyValues ), UNO_QUERY_THROW );
269 xPropertySet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ) ), Any( rName ) );
270 xControl = Reference< XComboBox >( mxDialogControlContainer->getControl( rName ), UNO_QUERY_THROW );
272 catch ( Exception& )
275 return xControl;
278 // -----------------------------------------------------------------------------
280 Reference< XRadioButton > UnoDialog::insertRadioButton( const OUString& rName, const Sequence< OUString > rPropertyNames, const Sequence< Any > rPropertyValues )
282 Reference< XRadioButton > xControl;
285 Reference< XPropertySet > xPropertySet( insertControlModel( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlRadioButtonModel" ) ),
286 rName, rPropertyNames, rPropertyValues ), UNO_QUERY_THROW );
287 xPropertySet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ) ), Any( rName ) );
288 xControl = Reference< XRadioButton >( mxDialogControlContainer->getControl( rName ), UNO_QUERY_THROW );
290 catch ( Exception& )
293 return xControl;
296 // -----------------------------------------------------------------------------
298 Reference< XListBox > UnoDialog::insertListBox( const OUString& rName, const Sequence< OUString > rPropertyNames, const Sequence< Any > rPropertyValues )
300 Reference< XListBox > xControl;
303 Reference< XPropertySet > xPropertySet( insertControlModel( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlListBoxModel" ) ),
304 rName, rPropertyNames, rPropertyValues ), UNO_QUERY_THROW );
305 xPropertySet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ) ), Any( rName ) );
306 xControl = Reference< XListBox >( mxDialogControlContainer->getControl( rName ), UNO_QUERY_THROW );
308 catch ( Exception& )
311 return xControl;
314 // -----------------------------------------------------------------------------
316 Reference< XControl > UnoDialog::insertImage( const OUString& rName, const Sequence< OUString > rPropertyNames, const Sequence< Any > rPropertyValues )
318 Reference< XControl > xControl;
321 Reference< XPropertySet > xPropertySet( insertControlModel( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlImageControlModel" ) ),
322 rName, rPropertyNames, rPropertyValues ), UNO_QUERY_THROW );
323 xPropertySet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ) ), Any( rName ) );
324 xControl = Reference< XControl >( mxDialogControlContainer->getControl( rName ), UNO_QUERY_THROW );
326 catch ( Exception& )
329 return xControl;
332 // -----------------------------------------------------------------------------
334 void UnoDialog::setControlProperty( const OUString& rControlName, const OUString& rPropertyName, const Any& rPropertyValue )
338 if ( mxDialogModelNameAccess->hasByName( rControlName ) )
340 Reference< XPropertySet > xPropertySet( mxDialogModelNameAccess->getByName( rControlName ), UNO_QUERY_THROW );
341 xPropertySet->setPropertyValue( rPropertyName, rPropertyValue );
344 catch ( Exception& )
349 // -----------------------------------------------------------------------------
350 #if 0
351 void UnoDialog::showMessageBox( const OUString& rTitle, const OUString& rMessage, sal_Bool bErrorBox ) const
355 Reference< XMessageBoxFactory > xMessageBoxFactory( mxMSF->getServiceManager()->createInstanceWithContext( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.Toolkit" ) ), mxMSF ), UNO_QUERY_THROW );
356 if ( xMessageBoxFactory.is() )
358 Rectangle aRectangle( 0, 0, 0, 0 );
359 Reference< XMessageBox > xMessageBox( xMessageBoxFactory->createMessageBox( mxWindowPeer, aRectangle,
360 bErrorBox ? OUString( RTL_CONSTASCII_USTRINGPARAM( "errorbox" ) ) : OUString( RTL_CONSTASCII_USTRINGPARAM( "querybox" ) ), MessageBoxButtons::BUTTONS_OK, rTitle, rMessage ) );
361 Reference< XComponent > xComponent( xMessageBox, UNO_QUERY_THROW );
362 /* sal_Int16 nResult = */ xMessageBox->execute();
363 xComponent->dispose();
366 catch ( Exception& )
371 public void showErrorMessageBox(XWindowPeer _xParentWindowPeer, String _sTitle, String _sMessage){
372 XComponent xComponent = null;
373 try {
374 Object oToolkit = m_xMCF.createInstanceWithContext("com.sun.star.awt.Toolkit", m_xContext);
375 XMessageBoxFactory xMessageBoxFactory = (XMessageBoxFactory) UnoRuntime.queryInterface(XMessageBoxFactory.class, oToolkit);
376 // rectangle may be empty if position is in the center of the parent peer
378 Rectangle aRectangle = new Rectangle();
379 XMessageBox xMessageBox = xMessageBoxFactory.createMessageBox(_xParentWindowPeer, aRectangle, "errorbox", com.sun.star.awt.MessageBoxButtons.BUTTONS_OK, _sTitle, _sMessage);
380 xComponent = (XComponent) UnoRuntime.queryInterface(XComponent.class, xMessageBox);
381 if (xMessageBox != null){
382 short nResult = xMessageBox.execute();
384 } catch (com.sun.star.uno.Exception ex) {
385 ex.printStackTrace(System.out);
387 finally{
388 //make sure always to dispose the component and free the memory!
389 if (xComponent != null){
390 xComponent.dispose();
396 #endif
398 // -----------------------------------------------------------------------------
400 sal_Int32 UnoDialog::getMapsFromPixels( sal_Int32 nPixels ) const
402 double dMaps = 0;
405 sal_Int32 nMapWidth = 0;
406 const OUString sWidth( RTL_CONSTASCII_USTRINGPARAM( "Width" ) );
407 if ( mxDialogModelPropertySet->getPropertyValue( sWidth ) >>= nMapWidth )
409 Reference< XWindow > xWindow( mxDialog, UNO_QUERY_THROW );
410 double pxWidth = xWindow->getPosSize().Width;
411 double mapRatio = ( pxWidth / nMapWidth );
412 dMaps = nPixels / mapRatio;
415 catch ( Exception& )
418 return static_cast< sal_Int32 >( dMaps );
421 // -----------------------------------------------------------------------------
423 Any UnoDialog::getControlProperty( const OUString& rControlName, const OUString& rPropertyName )
425 Any aRet;
428 if ( mxDialogModelNameAccess->hasByName( rControlName ) )
430 Reference< XPropertySet > xPropertySet( mxDialogModelNameAccess->getByName( rControlName ), UNO_QUERY_THROW );
431 aRet = xPropertySet->getPropertyValue( rPropertyName );
434 catch ( Exception& )
437 return aRet;
440 // -----------------------------------------------------------------------------
442 void UnoDialog::enableControl( const OUString& rControlName )
444 const OUString sEnabled( RTL_CONSTASCII_USTRINGPARAM( "Enabled" ) );
445 setControlProperty( rControlName, sEnabled, Any( sal_True ) );
448 // -----------------------------------------------------------------------------
450 void UnoDialog::disableControl( const OUString& rControlName )
452 const OUString sEnabled( RTL_CONSTASCII_USTRINGPARAM( "Enabled" ) );
453 setControlProperty( rControlName, sEnabled, Any( sal_False ) );
456 // -----------------------------------------------------------------------------