LibreOffice Module svtools (master) 1
colrdlg.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 <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>
27
31
32#include <svtools/colrdlg.hxx>
34#include <vcl/weld.hxx>
35#include <osl/diagnose.h>
36
37using namespace ::com::sun::star::uno;
38using namespace ::com::sun::star::lang;
39using namespace ::com::sun::star::beans;
40using namespace ::com::sun::star::ui::dialogs;
41
42constexpr OUStringLiteral sColor = u"Color";
43
46{
47}
48
50{
51}
52void SvColorDialog::SetColor( const Color& rColor )
53{
54 maColor = rColor;
55}
56
58{
59 meMode = eMode;
60}
61
63{
64 short ret = 0;
65 try
66 {
67 Reference< XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
68
69 Reference<css::awt::XWindow> xParent;
70 if (pParent)
71 xParent = pParent->GetXWindow();
72
73 Reference< XExecutableDialog > xDialog = css::cui::ColorPicker::createWithParent(xContext, xParent);
74 Reference< XPropertyAccess > xPropertyAccess( xDialog, UNO_QUERY_THROW );
75
76 Sequence< PropertyValue > props{
78 comphelper::makePropertyValue("Mode", static_cast<sal_Int16>(meMode))
79 };
80
81 xPropertyAccess->setPropertyValues( props );
82
83 ret = xDialog->execute();
84
85 if( ret )
86 {
87 props = xPropertyAccess->getPropertyValues();
88 for( const auto& rProp : std::as_const(props) )
89 {
90 if( rProp.Name == sColor )
91 {
92 rProp.Value >>= maColor;
93 }
94 }
95 }
96 }
97 catch(Exception&)
98 {
99 OSL_ASSERT(false);
100 }
101
102 return ret;
103}
104
105void SvColorDialog::ExecuteAsync(weld::Window* pParent, const std::function<void(sal_Int32)>& func)
106{
107 m_aResultFunc = func;
108
109 try
110 {
111 Reference< XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
112
113 Reference<css::awt::XWindow> xParent;
114 if (pParent)
115 xParent = pParent->GetXWindow();
116
117 mxDialog = css::cui::AsynchronousColorPicker::createWithParent(xContext, xParent);
118 Reference< XPropertyAccess > xPropertyAccess( mxDialog, UNO_QUERY_THROW );
119
120 Sequence< PropertyValue > props{
122 comphelper::makePropertyValue("Mode", static_cast<sal_Int16>(meMode))
123 };
124
125 xPropertyAccess->setPropertyValues( props );
126
127 rtl::Reference< ::svt::DialogClosedListener > pListener = new ::svt::DialogClosedListener();
128 pListener->SetDialogClosedLink( LINK( this, SvColorDialog, DialogClosedHdl ) );
129
130 mxDialog->startExecuteModal( pListener );
131 }
132 catch(Exception&)
133 {
134 TOOLS_WARN_EXCEPTION("svtools.dialogs", "SvColorDialog::ExecuteAsync");
135 }
136}
137
138IMPL_LINK( SvColorDialog, DialogClosedHdl, css::ui::dialogs::DialogClosedEvent*, pEvent, void )
139{
140 sal_Int32 nResult = 0;
141 sal_Int16 nDialogRet = pEvent->DialogResult;
142 if( nDialogRet == ExecutableDialogResults::OK )
143 {
144 nResult = RET_OK;
145
146 Reference< XPropertyAccess > xPropertyAccess( mxDialog, UNO_QUERY_THROW );
147 Sequence< PropertyValue > props = xPropertyAccess->getPropertyValues();
148
149 for( const auto& rProp : std::as_const(props) )
150 {
151 if( rProp.Name == sColor )
152 {
153 rProp.Value >>= maColor;
154 }
155 }
156 }
157
158 m_aResultFunc(nResult);
159 mxDialog.clear();
160}
161
162/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
basegfx::BColor maColor
void SetMode(svtools::ColorPickerMode eMode)
Definition: colrdlg.cxx:57
::com::sun::star::uno::Reference< ::com::sun::star::ui::dialogs::XAsynchronousExecutableDialog > mxDialog
Definition: colrdlg.hxx:55
short Execute(weld::Window *pParent)
Definition: colrdlg.cxx:62
void SetColor(const Color &rColor)
Definition: colrdlg.cxx:52
void ExecuteAsync(weld::Window *pParent, const std::function< void(sal_Int32)> &func)
Definition: colrdlg.cxx:105
std::function< void(sal_Int32)> m_aResultFunc
Definition: colrdlg.hxx:56
Color maColor
Definition: colrdlg.hxx:53
svtools::ColorPickerMode meMode
Definition: colrdlg.hxx:54
virtual css::uno::Reference< css::awt::XWindow > GetXWindow()=0
ColorMode meMode
constexpr OUStringLiteral sColor
Definition: colrdlg.cxx:42
IMPL_LINK(SvColorDialog, DialogClosedHdl, css::ui::dialogs::DialogClosedEvent *, pEvent, void)
Definition: colrdlg.cxx:138
#define TOOLS_WARN_EXCEPTION(area, stream)
float u
Mode eMode
@ Exception
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
dictionary props
ColorPickerMode
Definition: colrdlg.hxx:35
RET_OK