Get the style color and number just once
[LibreOffice.git] / svtools / source / uno / genericunodialog.cxx
blob6a7f0a3be7410a10a5de0ceae69c916eae2793fb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <svtools/genericunodialog.hxx>
22 #include <com/sun/star/awt/XWindow.hpp>
23 #include <com/sun/star/beans/NamedValue.hpp>
24 #include <com/sun/star/beans/PropertyValue.hpp>
25 #include <com/sun/star/beans/PropertyAttribute.hpp>
26 #include <com/sun/star/ucb/AlreadyInitializedException.hpp>
28 #include <cppuhelper/supportsservice.hxx>
29 #include <cppuhelper/queryinterface.hxx>
30 #include <osl/diagnose.h>
31 #include <comphelper/diagnose_ex.hxx>
32 #include <osl/mutex.hxx>
33 #include <vcl/svapp.hxx>
35 using namespace css::uno;
36 using namespace css::lang;
37 using namespace css::beans;
38 using namespace css::ucb;
41 namespace svt
45 OGenericUnoDialog::OGenericUnoDialog(const Reference< XComponentContext >& _rxContext)
46 :OPropertyContainer(GetBroadcastHelper())
47 ,m_bExecuting(false)
48 ,m_bTitleAmbiguous(true)
49 ,m_bInitialized( false )
50 ,m_aContext(_rxContext)
52 registerProperty(UNODIALOG_PROPERTY_TITLE, UNODIALOG_PROPERTY_ID_TITLE, PropertyAttribute::TRANSIENT,
53 &m_sTitle, cppu::UnoType<decltype(m_sTitle)>::get());
54 registerProperty(UNODIALOG_PROPERTY_PARENT, UNODIALOG_PROPERTY_ID_PARENT, PropertyAttribute::TRANSIENT,
55 &m_xParent, cppu::UnoType<decltype(m_xParent)>::get());
59 OGenericUnoDialog::~OGenericUnoDialog()
61 if (m_xDialog)
63 SolarMutexGuard aSolarGuard;
64 ::osl::MutexGuard aGuard( m_aMutex );
65 if (m_xDialog)
66 destroyDialog();
71 Any SAL_CALL OGenericUnoDialog::queryInterface(const Type& _rType)
73 Any aReturn = OGenericUnoDialogBase::queryInterface(_rType);
75 if (!aReturn.hasValue())
76 aReturn = ::cppu::queryInterface(_rType
77 ,static_cast<XPropertySet*>(this)
78 ,static_cast<XMultiPropertySet*>(this)
79 ,static_cast<XFastPropertySet*>(this)
82 return aReturn;
86 Sequence<Type> SAL_CALL OGenericUnoDialog::getTypes( )
88 return ::comphelper::concatSequences(
89 OGenericUnoDialogBase::getTypes(),
90 getBaseTypes()
94 sal_Bool SAL_CALL OGenericUnoDialog::supportsService(const OUString& ServiceName)
96 return cppu::supportsService(this, ServiceName);
100 void OGenericUnoDialog::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const Any& rValue )
102 // TODO: need some handling if we're currently executing ...
104 OPropertyContainer::setFastPropertyValue_NoBroadcast(nHandle, rValue);
106 if (UNODIALOG_PROPERTY_ID_TITLE == nHandle)
108 // from now on m_sTitle is valid
109 m_bTitleAmbiguous = false;
111 if (m_xDialog)
112 m_xDialog->set_title(m_sTitle);
117 sal_Bool OGenericUnoDialog::convertFastPropertyValue( Any& rConvertedValue, Any& rOldValue, sal_Int32 nHandle, const Any& rValue)
119 switch (nHandle)
121 case UNODIALOG_PROPERTY_ID_PARENT:
123 Reference<css::awt::XWindow> xNew(rValue, css::uno::UNO_QUERY);
124 if (xNew != m_xParent)
126 rConvertedValue <<= xNew;
127 rOldValue <<= m_xParent;
128 return true;
130 return false;
133 return OPropertyContainer::convertFastPropertyValue(rConvertedValue, rOldValue, nHandle, rValue);
137 void SAL_CALL OGenericUnoDialog::setTitle( const OUString& _rTitle )
139 UnoDialogEntryGuard aGuard( *this );
143 setPropertyValue(UNODIALOG_PROPERTY_TITLE, Any(_rTitle));
145 catch(RuntimeException&)
147 // allowed to pass
148 throw;
150 catch( const Exception& )
152 DBG_UNHANDLED_EXCEPTION("svtools.uno");
153 // not allowed to pass
158 bool OGenericUnoDialog::impl_ensureDialog_lck()
160 if (m_xDialog)
161 return true;
163 // get the parameters for the dialog from the current settings
165 // the title
166 OUString sTitle = m_sTitle;
168 auto xDialog(createDialog(m_xParent));
169 OSL_ENSURE(xDialog, "OGenericUnoDialog::impl_ensureDialog_lck: createDialog returned nonsense!");
170 if (!xDialog)
171 return false;
173 // do some initialisations
174 if (!m_bTitleAmbiguous)
175 xDialog->set_title(sTitle);
177 m_xDialog = std::move(xDialog);
179 return true;
182 sal_Int16 SAL_CALL OGenericUnoDialog::execute()
184 // both creation and execution of the dialog must be guarded with the SolarMutex, so be generous here
185 SolarMutexGuard aSolarGuard;
187 // create the dialog, if necessary
189 UnoDialogEntryGuard aGuard( *this );
191 if (m_bExecuting)
192 throw RuntimeException(
193 u"already executing the dialog (recursive call)"_ustr,
194 *this
197 m_bExecuting = true;
199 if ( !impl_ensureDialog_lck() )
200 return 0;
203 // start execution
204 sal_Int16 nReturn(0);
205 if (m_xDialog)
206 nReturn = m_xDialog->run();
209 ::osl::MutexGuard aGuard(m_aMutex);
211 // get the settings of the dialog
212 executedDialog( nReturn );
214 m_bExecuting = false;
217 // outta here
218 return nReturn;
221 void OGenericUnoDialog::implInitialize(const Any& _rValue)
225 PropertyValue aProperty;
226 NamedValue aValue;
227 if ( _rValue >>= aProperty )
229 setPropertyValue( aProperty.Name, aProperty.Value );
231 else if ( _rValue >>= aValue )
233 setPropertyValue( aValue.Name, aValue.Value );
236 catch(const Exception&)
238 DBG_UNHANDLED_EXCEPTION("svtools.uno");
242 void SAL_CALL OGenericUnoDialog::initialize( const Sequence< Any >& aArguments )
244 ::osl::MutexGuard aGuard( m_aMutex );
245 if ( m_bInitialized )
246 throw AlreadyInitializedException( OUString(), *this );
248 for (const Any& rArgument : aArguments)
249 implInitialize(rArgument);
251 m_bInitialized = true;
254 void OGenericUnoDialog::destroyDialog()
256 SolarMutexGuard aSolarGuard;
257 m_xDialog.reset();
260 } // namespace svt
263 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */