1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
21 #include <com/sun/star/awt/XWindow.hpp>
22 #include <com/sun/star/beans/XPropertyAccess.hpp>
23 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
24 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
25 #include <com/sun/star/cui/AsynchronousColorPicker.hpp>
26 #include <com/sun/star/cui/ColorPicker.hpp>
28 #include <comphelper/diagnose_ex.hxx>
29 #include <comphelper/processfactory.hxx>
30 #include <comphelper/propertyvalue.hxx>
32 #include <svtools/colrdlg.hxx>
33 #include <svtools/dialogclosedlistener.hxx>
34 #include <vcl/weld.hxx>
35 #include <osl/diagnose.h>
37 using namespace ::com::sun::star::uno
;
38 using namespace ::com::sun::star::lang
;
39 using namespace ::com::sun::star::beans
;
40 using namespace ::com::sun::star::ui::dialogs
;
42 const OUStringLiteral sColor
= u
"Color";
44 SvColorDialog::SvColorDialog()
45 : meMode(svtools::ColorPickerMode::Select
)
49 SvColorDialog::~SvColorDialog()
52 void SvColorDialog::SetColor( const Color
& rColor
)
57 void SvColorDialog::SetMode( svtools::ColorPickerMode eMode
)
62 short SvColorDialog::Execute(weld::Window
* pParent
)
67 Reference
< XComponentContext
> xContext( ::comphelper::getProcessComponentContext() );
69 Reference
<css::awt::XWindow
> xParent
;
71 xParent
= pParent
->GetXWindow();
73 Reference
< XExecutableDialog
> xDialog
= css::cui::ColorPicker::createWithParent(xContext
, xParent
);
74 Reference
< XPropertyAccess
> xPropertyAccess( xDialog
, UNO_QUERY_THROW
);
76 Sequence
< PropertyValue
> props
{
77 comphelper::makePropertyValue(OUString( sColor
), maColor
),
78 comphelper::makePropertyValue("Mode", static_cast<sal_Int16
>(meMode
))
81 xPropertyAccess
->setPropertyValues( props
);
83 ret
= xDialog
->execute();
87 props
= xPropertyAccess
->getPropertyValues();
88 for( const auto& rProp
: std::as_const(props
) )
90 if( rProp
.Name
== sColor
)
92 rProp
.Value
>>= maColor
;
105 void SvColorDialog::ExecuteAsync(weld::Window
* pParent
, const std::function
<void(sal_Int32
)>& func
)
107 m_aResultFunc
= func
;
111 Reference
< XComponentContext
> xContext( ::comphelper::getProcessComponentContext() );
113 Reference
<css::awt::XWindow
> xParent
;
115 xParent
= pParent
->GetXWindow();
117 mxDialog
= css::cui::AsynchronousColorPicker::createWithParent(xContext
, xParent
);
118 Reference
< XPropertyAccess
> xPropertyAccess( mxDialog
, UNO_QUERY_THROW
);
120 Sequence
< PropertyValue
> props
{
121 comphelper::makePropertyValue(OUString( sColor
), maColor
),
122 comphelper::makePropertyValue("Mode", static_cast<sal_Int16
>(meMode
))
125 xPropertyAccess
->setPropertyValues( props
);
127 rtl::Reference
< ::svt::DialogClosedListener
> pListener
= new ::svt::DialogClosedListener();
128 pListener
->SetDialogClosedLink( LINK( this, SvColorDialog
, DialogClosedHdl
) );
130 mxDialog
->startExecuteModal( pListener
);
134 TOOLS_WARN_EXCEPTION("svtools.dialogs", "SvColorDialog::ExecuteAsync");
138 IMPL_LINK( SvColorDialog
, DialogClosedHdl
, css::ui::dialogs::DialogClosedEvent
*, pEvent
, void )
140 sal_Int32 nResult
= 0;
141 sal_Int16 nDialogRet
= pEvent
->DialogResult
;
142 if( nDialogRet
== ExecutableDialogResults::OK
)
146 Reference
< XPropertyAccess
> xPropertyAccess( mxDialog
, UNO_QUERY_THROW
);
147 Sequence
< PropertyValue
> props
= xPropertyAccess
->getPropertyValues();
149 for( const auto& rProp
: std::as_const(props
) )
151 if( rProp
.Name
== sColor
)
153 rProp
.Value
>>= maColor
;
158 m_aResultFunc(nResult
);
162 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */