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::beans
;
39 using namespace ::com::sun::star::ui::dialogs
;
41 constexpr OUString sColor
= u
"Color"_ustr
;
43 SvColorDialog::SvColorDialog()
44 : meMode(svtools::ColorPickerMode::Select
)
48 SvColorDialog::~SvColorDialog()
51 void SvColorDialog::SetColor( const Color
& rColor
)
56 void SvColorDialog::SetMode( svtools::ColorPickerMode eMode
)
61 short SvColorDialog::Execute(weld::Window
* pParent
)
66 const Reference
< XComponentContext
>& xContext( ::comphelper::getProcessComponentContext() );
68 Reference
<css::awt::XWindow
> xParent
;
70 xParent
= pParent
->GetXWindow();
72 Reference
< XExecutableDialog
> xDialog
= css::cui::ColorPicker::createWithParent(xContext
, xParent
);
73 Reference
< XPropertyAccess
> xPropertyAccess( xDialog
, UNO_QUERY_THROW
);
75 Sequence
< PropertyValue
> props
{
76 comphelper::makePropertyValue(sColor
, maColor
),
77 comphelper::makePropertyValue(u
"Mode"_ustr
, static_cast<sal_Int16
>(meMode
))
80 xPropertyAccess
->setPropertyValues( props
);
82 ret
= xDialog
->execute();
86 props
= xPropertyAccess
->getPropertyValues();
87 for (const auto& rProp
: props
)
89 if( rProp
.Name
== sColor
)
91 rProp
.Value
>>= maColor
;
104 void SvColorDialog::ExecuteAsync(weld::Window
* pParent
, const std::function
<void(sal_Int32
)>& func
)
106 m_aResultFunc
= func
;
110 const Reference
< XComponentContext
>& xContext( ::comphelper::getProcessComponentContext() );
112 Reference
<css::awt::XWindow
> xParent
;
114 xParent
= pParent
->GetXWindow();
116 mxDialog
= css::cui::AsynchronousColorPicker::createWithParent(xContext
, xParent
);
117 Reference
< XPropertyAccess
> xPropertyAccess( mxDialog
, UNO_QUERY_THROW
);
119 Sequence
< PropertyValue
> props
{
120 comphelper::makePropertyValue(sColor
, maColor
),
121 comphelper::makePropertyValue(u
"Mode"_ustr
, static_cast<sal_Int16
>(meMode
))
124 xPropertyAccess
->setPropertyValues( props
);
126 rtl::Reference
< ::svt::DialogClosedListener
> pListener
= new ::svt::DialogClosedListener();
127 pListener
->SetDialogClosedLink( LINK( this, SvColorDialog
, DialogClosedHdl
) );
129 mxDialog
->startExecuteModal( pListener
);
133 TOOLS_WARN_EXCEPTION("svtools.dialogs", "SvColorDialog::ExecuteAsync");
137 IMPL_LINK( SvColorDialog
, DialogClosedHdl
, css::ui::dialogs::DialogClosedEvent
*, pEvent
, void )
139 sal_Int32 nResult
= 0;
140 sal_Int16 nDialogRet
= pEvent
->DialogResult
;
141 if( nDialogRet
== ExecutableDialogResults::OK
)
145 Reference
< XPropertyAccess
> xPropertyAccess( mxDialog
, UNO_QUERY_THROW
);
146 Sequence
< PropertyValue
> props
= xPropertyAccess
->getPropertyValues();
148 for (const auto& rProp
: props
)
150 if( rProp
.Name
== sColor
)
152 rProp
.Value
>>= maColor
;
157 m_aResultFunc(nResult
);
161 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */