xref: /trunk/main/sdext/source/minimizer/unodialog.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 {
145     mxDialogWindow->setVisible( sal_False );
146 
147     // reuse the parent's toolkit
148     Reference< XToolkit > xToolkit;
149     if ( mxParent.is() )
150         xToolkit.set( mxParent->getToolkit() );
151 
152     if ( !xToolkit.is() )
153          xToolkit.set( mxContext->getServiceManager()->createInstanceWithContext(
154              OUString( RTL_CONSTASCII_USTRINGPARAM(
155                  "com.sun.star.awt.Toolkit" ) ), mxContext ),
156                     UNO_QUERY_THROW  );
157 
158     mxReschedule = Reference< XReschedule >( xToolkit, UNO_QUERY );
159     mxControl->createPeer( xToolkit, mxParent );
160 
161     return mxControl->getPeer();
162 }
163 
164 // -----------------------------------------------------------------------------
165 
insertControlModel(const OUString & rServiceName,const OUString & rName,const Sequence<OUString> & rPropertyNames,const Sequence<Any> & rPropertyValues)166 Reference< XInterface > UnoDialog::insertControlModel( const OUString& rServiceName, const OUString& rName,
167                                                         const Sequence< OUString >& rPropertyNames, const Sequence< Any >& rPropertyValues )
168 {
169     Reference< XInterface > xControlModel;
170     try
171     {
172         xControlModel = mxDialogModelMSF->createInstance( rServiceName );
173         Reference< XMultiPropertySet > xMultiPropSet( xControlModel, UNO_QUERY_THROW );
174         xMultiPropSet->setPropertyValues( rPropertyNames, rPropertyValues );
175         mxDialogModelNameContainer->insertByName( rName, Any( xControlModel ) );
176     }
177     catch( Exception& )
178     {
179     }
180     return xControlModel;
181 }
182 
183 // -----------------------------------------------------------------------------
184 
setVisible(const OUString & rName,sal_Bool bVisible)185 void UnoDialog::setVisible( const OUString& rName, sal_Bool bVisible )
186 {
187     try
188     {
189         Reference< XInterface > xControl( mxDialogControlContainer->getControl( rName ), UNO_QUERY_THROW );
190         Reference< XWindow > xWindow( xControl, UNO_QUERY_THROW );
191         xWindow->setVisible( bVisible );
192     }
193     catch ( Exception& )
194     {
195     }
196 }
197 
198 // -----------------------------------------------------------------------------
199 
isHighContrast()200 sal_Bool UnoDialog::isHighContrast()
201 {
202     sal_Bool bHighContrast = sal_False;
203     try
204     {
205         Reference< XStyleSettingsSupplier > xStyleSettingsSuppl( mxDialogWindow, UNO_QUERY_THROW );
206         Reference< XStyleSettings > xStyleSettings( xStyleSettingsSuppl->getStyleSettings() );
207         bHighContrast = xStyleSettings->getHighContrastMode();
208     }
209     catch( Exception& )
210     {
211     }
212     return bHighContrast;
213 }
214 
215 // -----------------------------------------------------------------------------
216 
insertButton(const OUString & rName,Reference<XActionListener> xActionListener,const Sequence<OUString> & rPropertyNames,const Sequence<Any> & rPropertyValues)217 Reference< XButton > UnoDialog::insertButton( const OUString& rName, Reference< XActionListener > xActionListener,
218             const Sequence< OUString >& rPropertyNames, const Sequence< Any >& rPropertyValues )
219 {
220     Reference< XButton > xButton;
221     try
222     {
223         Reference< XInterface > xButtonModel( insertControlModel( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlButtonModel" ) ),
224             rName, rPropertyNames, rPropertyValues ) );
225         Reference< XPropertySet > xPropertySet( xButtonModel, UNO_QUERY_THROW );
226         xPropertySet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ) ), Any( rName ) );
227         xButton = Reference< XButton >( mxDialogControlContainer->getControl( rName ), UNO_QUERY_THROW );
228 
229         if ( xActionListener.is() )
230         {
231             xButton->addActionListener( xActionListener );
232             xButton->setActionCommand( rName );
233         }
234         return xButton;
235     }
236     catch( Exception& )
237     {
238     }
239     return xButton;
240 }
241 
242 // -----------------------------------------------------------------------------
243 
insertFixedText(const OUString & rName,const Sequence<OUString> rPropertyNames,const Sequence<Any> rPropertyValues)244 Reference< XFixedText > UnoDialog::insertFixedText( const OUString& rName, const Sequence< OUString > rPropertyNames, const Sequence< Any > rPropertyValues )
245 {
246     Reference< XFixedText > xFixedText;
247     try
248     {
249         Reference< XPropertySet > xPropertySet( insertControlModel( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlFixedTextModel" ) ),
250             rName, rPropertyNames, rPropertyValues ), UNO_QUERY_THROW );
251         xPropertySet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ) ), Any( rName ) );
252         xFixedText = Reference< XFixedText >( mxDialogControlContainer->getControl( rName ), UNO_QUERY_THROW );
253     }
254     catch ( Exception& )
255     {
256     }
257     return xFixedText;
258 }
259 
260 // -----------------------------------------------------------------------------
261 
insertCheckBox(const OUString & rName,const Sequence<OUString> rPropertyNames,const Sequence<Any> rPropertyValues)262 Reference< XCheckBox > UnoDialog::insertCheckBox( const OUString& rName, const Sequence< OUString > rPropertyNames, const Sequence< Any > rPropertyValues )
263 {
264     Reference< XCheckBox > xCheckBox;
265     try
266     {
267         Reference< XPropertySet > xPropertySet( insertControlModel( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlCheckBoxModel" ) ),
268             rName, rPropertyNames, rPropertyValues ), UNO_QUERY_THROW );
269         xPropertySet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ) ), Any( rName ) );
270         xCheckBox = Reference< XCheckBox >( mxDialogControlContainer->getControl( rName ), UNO_QUERY_THROW );
271     }
272     catch ( Exception& )
273     {
274     }
275     return xCheckBox;
276 }
277 
278 // -----------------------------------------------------------------------------
279 
insertFormattedField(const OUString & rName,const Sequence<OUString> rPropertyNames,const Sequence<Any> rPropertyValues)280 Reference< XControl > UnoDialog::insertFormattedField( const OUString& rName, const Sequence< OUString > rPropertyNames, const Sequence< Any > rPropertyValues )
281 {
282     Reference< XControl > xControl;
283     try
284     {
285         Reference< XPropertySet > xPropertySet( insertControlModel( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlFormattedFieldModel" ) ),
286             rName, rPropertyNames, rPropertyValues ), UNO_QUERY_THROW );
287         xPropertySet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ) ), Any( rName ) );
288         xControl = Reference< XControl >( mxDialogControlContainer->getControl( rName ), UNO_QUERY_THROW );
289     }
290     catch ( Exception& )
291     {
292     }
293     return xControl;
294 }
295 
296 // -----------------------------------------------------------------------------
297 
insertComboBox(const OUString & rName,const Sequence<OUString> rPropertyNames,const Sequence<Any> rPropertyValues)298 Reference< XComboBox > UnoDialog::insertComboBox( const OUString& rName, const Sequence< OUString > rPropertyNames, const Sequence< Any > rPropertyValues )
299 {
300     Reference< XComboBox > xControl;
301     try
302     {
303         Reference< XPropertySet > xPropertySet( insertControlModel( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlComboBoxModel" ) ),
304             rName, rPropertyNames, rPropertyValues ), UNO_QUERY_THROW );
305         xPropertySet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ) ), Any( rName ) );
306         xControl = Reference< XComboBox >( mxDialogControlContainer->getControl( rName ), UNO_QUERY_THROW );
307     }
308     catch ( Exception& )
309     {
310     }
311     return xControl;
312 }
313 
314 // -----------------------------------------------------------------------------
315 
insertRadioButton(const OUString & rName,const Sequence<OUString> rPropertyNames,const Sequence<Any> rPropertyValues)316 Reference< XRadioButton > UnoDialog::insertRadioButton( const OUString& rName, const Sequence< OUString > rPropertyNames, const Sequence< Any > rPropertyValues )
317 {
318     Reference< XRadioButton > xControl;
319     try
320     {
321         Reference< XPropertySet > xPropertySet( insertControlModel( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlRadioButtonModel" ) ),
322             rName, rPropertyNames, rPropertyValues ), UNO_QUERY_THROW );
323         xPropertySet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ) ), Any( rName ) );
324         xControl = Reference< XRadioButton >( mxDialogControlContainer->getControl( rName ), UNO_QUERY_THROW );
325     }
326     catch ( Exception& )
327     {
328     }
329     return xControl;
330 }
331 
332 // -----------------------------------------------------------------------------
333 
insertListBox(const OUString & rName,const Sequence<OUString> rPropertyNames,const Sequence<Any> rPropertyValues)334 Reference< XListBox > UnoDialog::insertListBox( const OUString& rName, const Sequence< OUString > rPropertyNames, const Sequence< Any > rPropertyValues )
335 {
336     Reference< XListBox > xControl;
337     try
338     {
339         Reference< XPropertySet > xPropertySet( insertControlModel( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlListBoxModel" ) ),
340             rName, rPropertyNames, rPropertyValues ), UNO_QUERY_THROW );
341         xPropertySet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ) ), Any( rName ) );
342         xControl = Reference< XListBox >( mxDialogControlContainer->getControl( rName ), UNO_QUERY_THROW );
343     }
344     catch ( Exception& )
345     {
346     }
347     return xControl;
348 }
349 
350 // -----------------------------------------------------------------------------
351 
insertImage(const OUString & rName,const Sequence<OUString> rPropertyNames,const Sequence<Any> rPropertyValues)352 Reference< XControl > UnoDialog::insertImage( const OUString& rName, const Sequence< OUString > rPropertyNames, const Sequence< Any > rPropertyValues )
353 {
354     Reference< XControl > xControl;
355     try
356     {
357         Reference< XPropertySet > xPropertySet( insertControlModel( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlImageControlModel" ) ),
358             rName, rPropertyNames, rPropertyValues ), UNO_QUERY_THROW );
359         xPropertySet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ) ), Any( rName ) );
360         xControl = Reference< XControl >( mxDialogControlContainer->getControl( rName ), UNO_QUERY_THROW );
361     }
362     catch ( Exception& )
363     {
364     }
365     return xControl;
366 }
367 
368 // -----------------------------------------------------------------------------
369 
setControlProperty(const OUString & rControlName,const OUString & rPropertyName,const Any & rPropertyValue)370 void UnoDialog::setControlProperty( const OUString& rControlName, const OUString& rPropertyName, const Any& rPropertyValue )
371 {
372     try
373     {
374         if ( mxDialogModelNameAccess->hasByName( rControlName ) )
375         {
376             Reference< XPropertySet > xPropertySet( mxDialogModelNameAccess->getByName( rControlName ), UNO_QUERY_THROW );
377             xPropertySet->setPropertyValue( rPropertyName, rPropertyValue );
378         }
379     }
380     catch ( Exception& )
381     {
382     }
383 }
384 
385 // -----------------------------------------------------------------------------
386 
getMapsFromPixels(sal_Int32 nPixels) const387 sal_Int32 UnoDialog::getMapsFromPixels( sal_Int32 nPixels ) const
388 {
389     double dMaps = 0;
390     try
391     {
392         sal_Int32 nMapWidth = 0;
393         const OUString sWidth( RTL_CONSTASCII_USTRINGPARAM( "Width" ) );
394         if ( mxDialogModelPropertySet->getPropertyValue( sWidth  ) >>= nMapWidth )
395         {
396             Reference< XWindow > xWindow( mxDialog, UNO_QUERY_THROW );
397             double pxWidth = xWindow->getPosSize().Width;
398             double mapRatio = ( pxWidth / nMapWidth );
399             dMaps = nPixels / mapRatio;
400         }
401     }
402     catch ( Exception& )
403     {
404     }
405     return static_cast< sal_Int32 >( dMaps );
406 }
407 
408 // -----------------------------------------------------------------------------
409 
getControlProperty(const OUString & rControlName,const OUString & rPropertyName)410 Any UnoDialog::getControlProperty( const OUString& rControlName, const OUString& rPropertyName )
411 {
412     Any aRet;
413     try
414     {
415         if ( mxDialogModelNameAccess->hasByName( rControlName ) )
416         {
417             Reference< XPropertySet > xPropertySet( mxDialogModelNameAccess->getByName( rControlName ), UNO_QUERY_THROW );
418             aRet = xPropertySet->getPropertyValue( rPropertyName );
419         }
420     }
421     catch ( Exception& )
422     {
423     }
424     return aRet;
425 }
426 
427 // -----------------------------------------------------------------------------
428 
enableControl(const OUString & rControlName)429 void UnoDialog::enableControl( const OUString& rControlName )
430 {
431     const OUString sEnabled( RTL_CONSTASCII_USTRINGPARAM( "Enabled" ) );
432     setControlProperty( rControlName, sEnabled, Any( sal_True ) );
433 }
434 
435 // -----------------------------------------------------------------------------
436 
disableControl(const OUString & rControlName)437 void UnoDialog::disableControl( const OUString& rControlName )
438 {
439     const OUString sEnabled( RTL_CONSTASCII_USTRINGPARAM( "Enabled" ) );
440     setControlProperty( rControlName, sEnabled, Any( sal_False ) );
441 }
442 
443 // -----------------------------------------------------------------------------
444