Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / svx / source / unodraw / SvxXTextColumns.cxx
blob95d289632241475c9f5e7bfa2aac5ae1e6a72cc5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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 <sal/config.h>
22 #include <com/sun/star/beans/PropertyAttribute.hpp>
23 #include <com/sun/star/beans/PropertyVetoException.hpp>
24 #include <com/sun/star/beans/UnknownPropertyException.hpp>
25 #include <com/sun/star/beans/XPropertySet.hpp>
26 #include <com/sun/star/lang/IllegalArgumentException.hpp>
27 #include <com/sun/star/lang/XServiceInfo.hpp>
28 #include <com/sun/star/style/VerticalAlignment.hpp>
29 #include <com/sun/star/text/ColumnSeparatorStyle.hpp>
30 #include <com/sun/star/text/XTextColumns.hpp>
31 #include <com/sun/star/uno/Any.h>
32 #include <com/sun/star/util/Color.hpp>
34 #include <cppuhelper/supportsservice.hxx>
35 #include <o3tl/safeint.hxx>
36 #include <svl/itemprop.hxx>
37 #include <svx/SvxXTextColumns.hxx>
38 #include <tools/UnitConversion.hxx>
39 #include <vcl/svapp.hxx>
41 #include <numeric>
43 namespace
45 enum : sal_uInt16
47 WID_TXTCOL_IS_AUTOMATIC,
48 WID_TXTCOL_AUTO_DISTANCE,
49 WID_TXTCOL_LINE_WIDTH,
50 WID_TXTCOL_LINE_COLOR,
51 WID_TXTCOL_LINE_REL_HGT,
52 WID_TXTCOL_LINE_ALIGN,
53 WID_TXTCOL_LINE_IS_ON,
54 WID_TXTCOL_LINE_STYLE,
57 SfxItemPropertyMapEntry const saTextColumns_Impl[] = {
58 { u"IsAutomatic", WID_TXTCOL_IS_AUTOMATIC, cppu::UnoType<bool>::get(),
59 css::beans::PropertyAttribute::READONLY, 0 },
60 { u"AutomaticDistance", WID_TXTCOL_AUTO_DISTANCE, cppu::UnoType<sal_Int32>::get(), 0, 0 },
61 { u"SeparatorLineWidth", WID_TXTCOL_LINE_WIDTH, cppu::UnoType<sal_Int32>::get(), 0, 0 },
62 { u"SeparatorLineColor", WID_TXTCOL_LINE_COLOR,
63 cppu::UnoType<com::sun::star::util::Color>::get(), 0, 0 },
64 { u"SeparatorLineRelativeHeight", WID_TXTCOL_LINE_REL_HGT, cppu::UnoType<sal_Int32>::get(), 0,
65 0 },
66 { u"SeparatorLineVerticalAlignment", WID_TXTCOL_LINE_ALIGN,
67 cppu::UnoType<css::style::VerticalAlignment>::get(), 0, 0 },
68 { u"SeparatorLineIsOn", WID_TXTCOL_LINE_IS_ON, cppu::UnoType<bool>::get(), 0, 0 },
69 { u"SeparatorLineStyle", WID_TXTCOL_LINE_STYLE, cppu::UnoType<sal_Int16>::get(), 0, 0 },
72 class SvxXTextColumns final
73 : public cppu::WeakImplHelper<css::beans::XPropertySet, css::text::XTextColumns,
74 css::lang::XServiceInfo>
76 public:
77 SvxXTextColumns() = default;
79 // XTextColumns
80 virtual sal_Int32 SAL_CALL getReferenceValue() override;
81 virtual sal_Int16 SAL_CALL getColumnCount() override;
82 virtual void SAL_CALL setColumnCount(sal_Int16 nColumns) override;
83 virtual css::uno::Sequence<css::text::TextColumn> SAL_CALL getColumns() override;
84 virtual void SAL_CALL
85 setColumns(const css::uno::Sequence<css::text::TextColumn>& Columns) override;
87 // XPropertySet
88 virtual css::uno::Reference<css::beans::XPropertySetInfo>
89 SAL_CALL getPropertySetInfo() override;
90 virtual void SAL_CALL setPropertyValue(const OUString& aPropertyName,
91 const css::uno::Any& aValue) override;
92 virtual css::uno::Any SAL_CALL getPropertyValue(const OUString& PropertyName) override;
93 virtual void SAL_CALL addPropertyChangeListener(
94 const OUString& aPropertyName,
95 const css::uno::Reference<css::beans::XPropertyChangeListener>& xListener) override;
96 virtual void SAL_CALL removePropertyChangeListener(
97 const OUString& aPropertyName,
98 const css::uno::Reference<css::beans::XPropertyChangeListener>& aListener) override;
99 virtual void SAL_CALL addVetoableChangeListener(
100 const OUString& PropertyName,
101 const css::uno::Reference<css::beans::XVetoableChangeListener>& aListener) override;
102 virtual void SAL_CALL removeVetoableChangeListener(
103 const OUString& PropertyName,
104 const css::uno::Reference<css::beans::XVetoableChangeListener>& aListener) override;
106 // XServiceInfo
107 virtual OUString SAL_CALL getImplementationName() override;
108 virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) override;
109 virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
111 private:
112 sal_Int32 m_nReference = USHRT_MAX;
113 css::uno::Sequence<css::text::TextColumn> m_aTextColumns;
114 bool m_bIsAutomaticWidth = true;
115 sal_Int32 m_nAutoDistance = 0;
117 const SfxItemPropertySet m_aPropSet = { saTextColumns_Impl };
119 //separator line
120 sal_Int32 m_nSepLineWidth = 0;
121 com::sun::star::util::Color m_nSepLineColor = 0; // black
122 sal_Int32 m_nSepLineHeightRelative = 100; // full height
123 css::style::VerticalAlignment m_nSepLineVertAlign = css::style::VerticalAlignment_MIDDLE;
124 bool m_bSepLineIsOn = false;
125 sal_Int16 m_nSepLineStyle = css::text::ColumnSeparatorStyle::NONE;
128 OUString SvxXTextColumns::getImplementationName() { return "com.sun.star.comp.svx.TextColumns"; }
130 sal_Bool SvxXTextColumns::supportsService(const OUString& rServiceName)
132 return cppu::supportsService(this, rServiceName);
135 css::uno::Sequence<OUString> SvxXTextColumns::getSupportedServiceNames()
137 return { "com.sun.star.text.TextColumns" };
140 sal_Int32 SvxXTextColumns::getReferenceValue()
142 SolarMutexGuard aGuard;
143 return m_nReference;
146 sal_Int16 SvxXTextColumns::getColumnCount()
148 SolarMutexGuard aGuard;
149 return o3tl::narrowing<sal_Int16>(m_aTextColumns.getLength());
152 void SvxXTextColumns::setColumnCount(sal_Int16 nColumns)
154 SolarMutexGuard aGuard;
155 if (nColumns <= 0)
156 throw css::uno::RuntimeException();
157 m_bIsAutomaticWidth = true;
158 m_aTextColumns.realloc(nColumns);
159 css::text::TextColumn* pCols = m_aTextColumns.getArray();
160 m_nReference = USHRT_MAX;
161 sal_Int32 nWidth = m_nReference / nColumns;
162 sal_Int32 nDiff = m_nReference - nWidth * nColumns;
163 sal_Int32 nDist = m_nAutoDistance / 2;
164 for (sal_Int16 i = 0; i < nColumns; i++)
166 pCols[i].Width = nWidth;
167 pCols[i].LeftMargin = i == 0 ? 0 : nDist;
168 pCols[i].RightMargin = i == nColumns - 1 ? 0 : nDist;
170 pCols[nColumns - 1].Width += nDiff;
173 css::uno::Sequence<css::text::TextColumn> SvxXTextColumns::getColumns()
175 SolarMutexGuard aGuard;
176 return m_aTextColumns;
179 void SvxXTextColumns::setColumns(const css::uno::Sequence<css::text::TextColumn>& rColumns)
181 SolarMutexGuard aGuard;
182 sal_Int32 nReferenceTemp = std::accumulate(
183 rColumns.begin(), rColumns.end(), sal_Int32(0),
184 [](const sal_Int32 nSum, const css::text::TextColumn& rCol) { return nSum + rCol.Width; });
185 m_bIsAutomaticWidth = false;
186 m_nReference = !nReferenceTemp ? USHRT_MAX : nReferenceTemp;
187 m_aTextColumns = rColumns;
190 css::uno::Reference<css::beans::XPropertySetInfo> SvxXTextColumns::getPropertySetInfo()
192 return m_aPropSet.getPropertySetInfo();
195 void SvxXTextColumns::setPropertyValue(const OUString& rPropertyName, const css::uno::Any& aValue)
197 const SfxItemPropertyMapEntry* pEntry = m_aPropSet.getPropertyMap().getByName(rPropertyName);
198 if (!pEntry)
199 throw css::beans::UnknownPropertyException("Unknown property: " + rPropertyName,
200 static_cast<cppu::OWeakObject*>(this));
201 if (pEntry->nFlags & css::beans::PropertyAttribute::READONLY)
202 throw css::beans::PropertyVetoException("Property is read-only: " + rPropertyName,
203 static_cast<cppu::OWeakObject*>(this));
205 switch (pEntry->nWID)
207 case WID_TXTCOL_LINE_WIDTH:
209 sal_Int32 nTmp;
210 if (!(aValue >>= nTmp) || nTmp < 0)
211 throw css::lang::IllegalArgumentException();
212 m_nSepLineWidth = nTmp;
213 break;
215 case WID_TXTCOL_LINE_COLOR:
216 if (!(aValue >>= m_nSepLineColor))
217 throw css::lang::IllegalArgumentException();
218 break;
219 case WID_TXTCOL_LINE_STYLE:
220 if (!(aValue >>= m_nSepLineStyle))
221 throw css::lang::IllegalArgumentException();
222 break;
223 case WID_TXTCOL_LINE_REL_HGT:
225 sal_Int32 nTmp;
226 if (!(aValue >>= nTmp) || nTmp < 0)
227 throw css::lang::IllegalArgumentException();
228 m_nSepLineHeightRelative = nTmp;
229 break;
231 case WID_TXTCOL_LINE_ALIGN:
232 if (css::style::VerticalAlignment eAlign; aValue >>= eAlign)
233 m_nSepLineVertAlign = eAlign;
234 else if (sal_Int8 nTmp; aValue >>= nTmp)
235 m_nSepLineVertAlign = static_cast<css::style::VerticalAlignment>(nTmp);
236 else
237 throw css::lang::IllegalArgumentException();
238 break;
239 case WID_TXTCOL_LINE_IS_ON:
240 if (!(aValue >>= m_bSepLineIsOn))
241 throw css::lang::IllegalArgumentException();
242 break;
243 case WID_TXTCOL_AUTO_DISTANCE:
245 sal_Int32 nTmp;
246 if (!(aValue >>= nTmp) || nTmp < 0 || nTmp >= m_nReference)
247 throw css::lang::IllegalArgumentException();
248 m_nAutoDistance = nTmp;
249 sal_Int32 nColumns = m_aTextColumns.getLength();
250 css::text::TextColumn* pCols = m_aTextColumns.getArray();
251 sal_Int32 nDist = m_nAutoDistance / 2;
252 for (sal_Int32 i = 0; i < nColumns; i++)
254 pCols[i].LeftMargin = i == 0 ? 0 : nDist;
255 pCols[i].RightMargin = i == nColumns - 1 ? 0 : nDist;
257 break;
262 css::uno::Any SvxXTextColumns::getPropertyValue(const OUString& rPropertyName)
264 const SfxItemPropertyMapEntry* pEntry = m_aPropSet.getPropertyMap().getByName(rPropertyName);
265 if (!pEntry)
266 throw css::beans::UnknownPropertyException("Unknown property: " + rPropertyName,
267 static_cast<cppu::OWeakObject*>(this));
269 css::uno::Any aRet;
270 switch (pEntry->nWID)
272 case WID_TXTCOL_LINE_WIDTH:
273 aRet <<= m_nSepLineWidth;
274 break;
275 case WID_TXTCOL_LINE_COLOR:
276 aRet <<= m_nSepLineColor;
277 break;
278 case WID_TXTCOL_LINE_STYLE:
279 aRet <<= m_nSepLineStyle;
280 break;
281 case WID_TXTCOL_LINE_REL_HGT:
282 aRet <<= m_nSepLineHeightRelative;
283 break;
284 case WID_TXTCOL_LINE_ALIGN:
285 aRet <<= m_nSepLineVertAlign;
286 break;
287 case WID_TXTCOL_LINE_IS_ON:
288 aRet <<= m_bSepLineIsOn;
289 break;
290 case WID_TXTCOL_IS_AUTOMATIC:
291 aRet <<= m_bIsAutomaticWidth;
292 break;
293 case WID_TXTCOL_AUTO_DISTANCE:
294 aRet <<= m_nAutoDistance;
295 break;
297 return aRet;
300 void SvxXTextColumns::addPropertyChangeListener(
301 const OUString& /*rPropertyName*/,
302 const css::uno::Reference<css::beans::XPropertyChangeListener>& /*xListener*/)
306 void SvxXTextColumns::removePropertyChangeListener(
307 const OUString& /*rPropertyName*/,
308 const css::uno::Reference<css::beans::XPropertyChangeListener>& /*xListener*/)
312 void SvxXTextColumns::addVetoableChangeListener(
313 const OUString& /*rPropertyName*/,
314 const css::uno::Reference<css::beans::XVetoableChangeListener>& /*xListener*/)
318 void SvxXTextColumns::removeVetoableChangeListener(
319 const OUString& /*rPropertyName*/,
320 const css::uno::Reference<css::beans::XVetoableChangeListener>& /*xListener*/)
325 css::uno::Reference<css::uno::XInterface> SvxXTextColumns_createInstance() noexcept
327 return static_cast<cppu::OWeakObject*>(new SvxXTextColumns);
330 extern "C" SVXCORE_DLLPUBLIC css::uno::XInterface*
331 com_sun_star_comp_svx_TextColumns_get_implementation(css::uno::XComponentContext*,
332 css::uno::Sequence<css::uno::Any> const&)
334 return cppu::acquire(new SvxXTextColumns);
337 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */