LibreOffice Module sdext (master)  1
unodialog.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 
21 #include "unodialog.hxx"
22 
23 #include <com/sun/star/awt/Toolkit.hpp>
24 #include <com/sun/star/awt/UnoControlDialog.hpp>
25 #include <com/sun/star/beans/XPropertySet.hpp>
26 #include <com/sun/star/view/XSelectionSupplier.hpp>
27 
28 
29 using namespace ::com::sun::star::awt;
30 using namespace ::com::sun::star::uno;
31 using namespace ::com::sun::star::util;
32 using namespace ::com::sun::star::lang;
33 using namespace ::com::sun::star::view;
34 using namespace ::com::sun::star::frame;
35 using namespace ::com::sun::star::beans;
36 
37 UnoDialog::UnoDialog( const Reference< XComponentContext > &rxContext, Reference< XFrame > const & rxFrame ) :
38  mxContext( rxContext ),
39  mxController( rxFrame->getController() ),
40  mxDialogModel( mxContext->getServiceManager()->createInstanceWithContext(
41  "com.sun.star.awt.UnoControlDialogModel", mxContext ), UNO_SET_THROW ),
42  mxDialogModelMultiPropertySet( mxDialogModel, UNO_QUERY_THROW ),
43  mxDialogModelMSF( mxDialogModel, UNO_QUERY_THROW ),
44  mxDialogModelNameContainer( mxDialogModel, UNO_QUERY_THROW ),
45  mxDialogModelNameAccess( mxDialogModel, UNO_QUERY_THROW ),
46  mxControlModel( mxDialogModel, UNO_QUERY_THROW ),
47  mxDialog( UnoControlDialog::create(rxContext) ),
48  mxControl( mxDialog, UNO_QUERY_THROW ),
49  mbStatus( false )
50 {
51  mxControl->setModel( mxControlModel );
52 
53  Reference< XFrame > xFrame( mxController->getFrame() );
54  Reference< XWindow > xContainerWindow( xFrame->getContainerWindow() );
55  Reference< XWindowPeer > xWindowPeer( xContainerWindow, UNO_QUERY_THROW );
56 
57  // set the main loop handle to update GUI while busy
58  Reference< XToolkit > xToolkit( Toolkit::create( mxContext ), UNO_QUERY_THROW );
59  mxReschedule.set( xToolkit, UNO_QUERY );
60 
61  // allocate the real window resources
62  mxDialog->createPeer(xToolkit,
63  xWindowPeer.is() ? xWindowPeer : xToolkit->getDesktopWindow());
64 }
65 
67 {
68  // free the resources
69  Reference<XComponent> xDialogComponent(mxDialog, UNO_QUERY_THROW);
70  xDialogComponent->dispose();
71 }
72 
74 {
75  mxDialog->setEnable( true );
76  mxDialog->setVisible( true );
77  mxDialog->execute();
78 }
79 
80 void UnoDialog::endExecute( bool bStatus )
81 {
82  mbStatus = bStatus;
83  mxDialog->endExecute();
84 }
85 
86 Reference< XInterface > UnoDialog::insertControlModel( const OUString& rServiceName, const OUString& rName,
87  const Sequence< OUString >& rPropertyNames, const Sequence< Any >& rPropertyValues )
88 {
89  Reference< XInterface > xControlModel;
90  try
91  {
92  xControlModel = mxDialogModelMSF->createInstance( rServiceName );
93  Reference< XMultiPropertySet > xMultiPropSet( xControlModel, UNO_QUERY_THROW );
94  xMultiPropSet->setPropertyValues( rPropertyNames, rPropertyValues );
95  mxDialogModelNameContainer->insertByName( rName, Any( xControlModel ) );
96  }
97  catch( Exception& )
98  {
99  }
100  return xControlModel;
101 }
102 
103 
104 void UnoDialog::setVisible( const OUString& rName, bool bVisible )
105 {
106  try
107  {
108  Reference< XInterface > xControl( mxDialog->getControl( rName ), UNO_QUERY_THROW );
109  Reference< XWindow > xWindow( xControl, UNO_QUERY_THROW );
110  xWindow->setVisible( bVisible );
111  }
112  catch ( Exception& )
113  {
114  }
115 }
116 
117 
118 Reference< XButton > UnoDialog::insertButton( const OUString& rName, const Reference< XActionListener >& xActionListener,
119  const Sequence< OUString >& rPropertyNames, const Sequence< Any >& rPropertyValues )
120 {
121  Reference< XButton > xButton;
122  try
123  {
124  Reference< XInterface > xButtonModel( insertControlModel( "com.sun.star.awt.UnoControlButtonModel",
125  rName, rPropertyNames, rPropertyValues ) );
126  Reference< XPropertySet > xPropertySet( xButtonModel, UNO_QUERY_THROW );
127  xPropertySet->setPropertyValue("Name", Any( rName ) );
128  xButton.set( mxDialog->getControl( rName ), UNO_QUERY_THROW );
129 
130  if ( xActionListener.is() )
131  {
132  xButton->addActionListener( xActionListener );
133  xButton->setActionCommand( rName );
134  }
135  return xButton;
136  }
137  catch( Exception& )
138  {
139  }
140  return xButton;
141 }
142 
143 
144 Reference< XFixedText > UnoDialog::insertFixedText( const OUString& rName, const Sequence< OUString >& rPropertyNames, const Sequence< Any >& rPropertyValues )
145 {
146  Reference< XFixedText > xFixedText;
147  try
148  {
149  Reference< XPropertySet > xPropertySet( insertControlModel( "com.sun.star.awt.UnoControlFixedTextModel",
150  rName, rPropertyNames, rPropertyValues ), UNO_QUERY_THROW );
151  xPropertySet->setPropertyValue("Name", Any( rName ) );
152  xFixedText.set( mxDialog->getControl( rName ), UNO_QUERY_THROW );
153  }
154  catch ( Exception& )
155  {
156  }
157  return xFixedText;
158 }
159 
160 
161 Reference< XCheckBox > UnoDialog::insertCheckBox( const OUString& rName, const Sequence< OUString >& rPropertyNames, const Sequence< Any >& rPropertyValues )
162 {
163  Reference< XCheckBox > xCheckBox;
164  try
165  {
166  Reference< XPropertySet > xPropertySet( insertControlModel( "com.sun.star.awt.UnoControlCheckBoxModel",
167  rName, rPropertyNames, rPropertyValues ), UNO_QUERY_THROW );
168  xPropertySet->setPropertyValue("Name", Any( rName ) );
169  xCheckBox.set( mxDialog->getControl( rName ), UNO_QUERY_THROW );
170  }
171  catch ( Exception& )
172  {
173  }
174  return xCheckBox;
175 }
176 
177 
178 Reference< XControl > UnoDialog::insertFormattedField( const OUString& rName, const Sequence< OUString >& rPropertyNames, const Sequence< Any >& rPropertyValues )
179 {
180  Reference< XControl > xControl;
181  try
182  {
183  Reference< XPropertySet > xPropertySet( insertControlModel( "com.sun.star.awt.UnoControlFormattedFieldModel",
184  rName, rPropertyNames, rPropertyValues ), UNO_QUERY_THROW );
185  xPropertySet->setPropertyValue("Name", Any( rName ) );
186  xControl.set( mxDialog->getControl( rName ), UNO_SET_THROW );
187  }
188  catch ( Exception& )
189  {
190  }
191  return xControl;
192 }
193 
194 
195 Reference< XComboBox > UnoDialog::insertComboBox( const OUString& rName, const Sequence< OUString >& rPropertyNames, const Sequence< Any >& rPropertyValues )
196 {
197  Reference< XComboBox > xControl;
198  try
199  {
200  Reference< XPropertySet > xPropertySet( insertControlModel( "com.sun.star.awt.UnoControlComboBoxModel",
201  rName, rPropertyNames, rPropertyValues ), UNO_QUERY_THROW );
202  xPropertySet->setPropertyValue("Name", Any( rName ) );
203  xControl.set( mxDialog->getControl( rName ), UNO_QUERY_THROW );
204  }
205  catch ( Exception& )
206  {
207  }
208  return xControl;
209 }
210 
211 
212 Reference< XRadioButton > UnoDialog::insertRadioButton( const OUString& rName, const Sequence< OUString >& rPropertyNames, const Sequence< Any >& rPropertyValues )
213 {
214  Reference< XRadioButton > xControl;
215  try
216  {
217  Reference< XPropertySet > xPropertySet( insertControlModel( "com.sun.star.awt.UnoControlRadioButtonModel",
218  rName, rPropertyNames, rPropertyValues ), UNO_QUERY_THROW );
219  xPropertySet->setPropertyValue("Name", Any( rName ) );
220  xControl.set( mxDialog->getControl( rName ), UNO_QUERY_THROW );
221  }
222  catch ( Exception& )
223  {
224  }
225  return xControl;
226 }
227 
228 
229 Reference< XListBox > UnoDialog::insertListBox( const OUString& rName, const Sequence< OUString >& rPropertyNames, const Sequence< Any >& rPropertyValues )
230 {
231  Reference< XListBox > xControl;
232  try
233  {
234  Reference< XPropertySet > xPropertySet( insertControlModel( "com.sun.star.awt.UnoControlListBoxModel",
235  rName, rPropertyNames, rPropertyValues ), UNO_QUERY_THROW );
236  xPropertySet->setPropertyValue("Name", Any( rName ) );
237  xControl.set( mxDialog->getControl( rName ), UNO_QUERY_THROW );
238  }
239  catch ( Exception& )
240  {
241  }
242  return xControl;
243 }
244 
245 
246 Reference< XControl > UnoDialog::insertImage( const OUString& rName, const Sequence< OUString >& rPropertyNames, const Sequence< Any >& rPropertyValues )
247 {
248  Reference< XControl > xControl;
249  try
250  {
251  Reference< XPropertySet > xPropertySet( insertControlModel( "com.sun.star.awt.UnoControlImageControlModel",
252  rName, rPropertyNames, rPropertyValues ), UNO_QUERY_THROW );
253  xPropertySet->setPropertyValue("Name", Any( rName ) );
254  xControl.set( mxDialog->getControl( rName ), UNO_SET_THROW );
255  }
256  catch ( Exception& )
257  {
258  }
259  return xControl;
260 }
261 
262 
263 void UnoDialog::setControlProperty( const OUString& rControlName, const OUString& rPropertyName, const Any& rPropertyValue )
264 {
265  try
266  {
267  if ( mxDialogModelNameAccess->hasByName( rControlName ) )
268  {
269  Reference< XPropertySet > xPropertySet( mxDialogModelNameAccess->getByName( rControlName ), UNO_QUERY_THROW );
270  xPropertySet->setPropertyValue( rPropertyName, rPropertyValue );
271  }
272  }
273  catch ( Exception& )
274  {
275  }
276 }
277 
278 
279 Any UnoDialog::getControlProperty( const OUString& rControlName, const OUString& rPropertyName )
280 {
281  Any aRet;
282  try
283  {
284  if ( mxDialogModelNameAccess->hasByName( rControlName ) )
285  {
286  Reference< XPropertySet > xPropertySet( mxDialogModelNameAccess->getByName( rControlName ), UNO_QUERY_THROW );
287  aRet = xPropertySet->getPropertyValue( rPropertyName );
288  }
289  }
290  catch ( Exception& )
291  {
292  }
293  return aRet;
294 }
295 
296 
297 void UnoDialog::enableControl( const OUString& rControlName )
298 {
299  const OUString sEnabled( "Enabled" );
300  setControlProperty( rControlName, sEnabled, Any( true ) );
301 }
302 
303 
304 void UnoDialog::disableControl( const OUString& rControlName )
305 {
306  const OUString sEnabled( "Enabled" );
307  setControlProperty( rControlName, sEnabled, Any( false ) );
308 }
309 
310 
311 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void disableControl(const OUString &rControlName)
Definition: unodialog.cxx:304
css::uno::Any getControlProperty(const OUString &rControlName, const OUString &rPropertyName)
Definition: unodialog.cxx:279
css::uno::Reference< css::awt::XReschedule > mxReschedule
Definition: unodialog.hxx:102
Reference< XFrame > xFrame
css::uno::Reference< css::awt::XListBox > insertListBox(const OUString &rName, const css::uno::Sequence< OUString > &rPropertyNames, const css::uno::Sequence< css::uno::Any > &rPropertyValues)
Definition: unodialog.cxx:229
css::uno::Reference< css::awt::XControl > insertImage(const OUString &rName, const css::uno::Sequence< OUString > &rPropertyNames, const css::uno::Sequence< css::uno::Any > &rPropertyValues)
Definition: unodialog.cxx:246
void execute()
Definition: unodialog.cxx:73
css::uno::Reference< css::awt::XControl > insertFormattedField(const OUString &rName, const css::uno::Sequence< OUString > &rPropertyNames, const css::uno::Sequence< css::uno::Any > &rPropertyValues)
Definition: unodialog.cxx:178
void setControlProperty(const OUString &rControlName, const OUString &rPropertyName, const css::uno::Any &rPropertyValue)
Definition: unodialog.cxx:263
css::uno::Reference< css::awt::XUnoControlDialog > mxDialog
Definition: unodialog.hxx:111
bool mbStatus
Definition: unodialog.hxx:113
Reference< deployment::XPackageRegistry > create(Reference< deployment::XPackageRegistry > const &xRootRegistry, OUString const &context, OUString const &cachePath, Reference< XComponentContext > const &xComponentContext)
css::uno::Reference< css::awt::XControlModel > mxControlModel
Definition: unodialog.hxx:109
css::uno::Reference< css::awt::XFixedText > insertFixedText(const OUString &rName, const css::uno::Sequence< OUString > &rPropertyNames, const css::uno::Sequence< css::uno::Any > &rPropertyValues)
Definition: unodialog.cxx:144
css::uno::Reference< css::lang::XMultiServiceFactory > mxDialogModelMSF
Definition: unodialog.hxx:105
void endExecute(bool bStatus)
Definition: unodialog.cxx:80
css::uno::Reference< css::awt::XComboBox > insertComboBox(const OUString &rName, const css::uno::Sequence< OUString > &rPropertyNames, const css::uno::Sequence< css::uno::Any > &rPropertyValues)
Definition: unodialog.cxx:195
css::uno::Reference< css::awt::XRadioButton > insertRadioButton(const OUString &rName, const css::uno::Sequence< OUString > &rPropertyNames, const css::uno::Sequence< css::uno::Any > &rPropertyValues)
Definition: unodialog.cxx:212
void enableControl(const OUString &rControlName)
Definition: unodialog.cxx:297
css::uno::Reference< css::container::XNameAccess > mxDialogModelNameAccess
Definition: unodialog.hxx:107
css::uno::Reference< css::uno::XComponentContext > mxContext
Definition: unodialog.hxx:98
UnoDialog(const css::uno::Reference< css::uno::XComponentContext > &rxMSF, css::uno::Reference< css::frame::XFrame > const &rxFrame)
Definition: unodialog.cxx:37
css::uno::Reference< css::awt::XCheckBox > insertCheckBox(const OUString &rName, const css::uno::Sequence< OUString > &rPropertyNames, const css::uno::Sequence< css::uno::Any > &rPropertyValues)
Definition: unodialog.cxx:161
css::uno::Reference< css::frame::XController > mxController
Definition: unodialog.hxx:99
css::uno::Reference< css::container::XNameContainer > mxDialogModelNameContainer
Definition: unodialog.hxx:106
css::uno::Reference< css::awt::XControl > mxControl
Definition: unodialog.hxx:112
css::uno::Reference< css::uno::XInterface > insertControlModel(const OUString &rServiceName, const OUString &rName, const css::uno::Sequence< OUString > &rPropertyNames, const css::uno::Sequence< css::uno::Any > &rPropertyValues)
Definition: unodialog.cxx:86
void setVisible(const OUString &rName, bool bVisible)
Definition: unodialog.cxx:104
Reference< XComponentContext > mxContext
css::uno::Reference< css::awt::XButton > insertButton(const OUString &rName, const css::uno::Reference< css::awt::XActionListener > &xActionListener, const css::uno::Sequence< OUString > &rPropertyNames, const css::uno::Sequence< css::uno::Any > &rPropertyValues)
Definition: unodialog.cxx:118