tdf#121119 master docs at-page anchor: clean-up renaming
[LibreOffice.git] / svx / source / unodraw / SvxXTextColumns.cxx
blob0ee73c49bf2860667de5d3a8bcdb5377d365a1b0
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 <vcl/svapp.hxx>
40 #include <numeric>
42 namespace
44 enum : sal_uInt16
46 WID_TXTCOL_IS_AUTOMATIC,
47 WID_TXTCOL_AUTO_DISTANCE,
48 WID_TXTCOL_LINE_WIDTH,
49 WID_TXTCOL_LINE_COLOR,
50 WID_TXTCOL_LINE_REL_HGT,
51 WID_TXTCOL_LINE_ALIGN,
52 WID_TXTCOL_LINE_IS_ON,
53 WID_TXTCOL_LINE_STYLE,
56 SfxItemPropertyMapEntry const saTextColumns_Impl[] = {
57 { u"IsAutomatic"_ustr, WID_TXTCOL_IS_AUTOMATIC, cppu::UnoType<bool>::get(),
58 css::beans::PropertyAttribute::READONLY, 0 },
59 { u"AutomaticDistance"_ustr, WID_TXTCOL_AUTO_DISTANCE, cppu::UnoType<sal_Int32>::get(), 0, 0 },
60 { u"SeparatorLineWidth"_ustr, WID_TXTCOL_LINE_WIDTH, cppu::UnoType<sal_Int32>::get(), 0, 0 },
61 { u"SeparatorLineColor"_ustr, WID_TXTCOL_LINE_COLOR, cppu::UnoType<css::util::Color>::get(), 0,
62 0 },
63 { u"SeparatorLineRelativeHeight"_ustr, WID_TXTCOL_LINE_REL_HGT, cppu::UnoType<sal_Int32>::get(),
64 0, 0 },
65 { u"SeparatorLineVerticalAlignment"_ustr, WID_TXTCOL_LINE_ALIGN,
66 cppu::UnoType<css::style::VerticalAlignment>::get(), 0, 0 },
67 { u"SeparatorLineIsOn"_ustr, WID_TXTCOL_LINE_IS_ON, cppu::UnoType<bool>::get(), 0, 0 },
68 { u"SeparatorLineStyle"_ustr, WID_TXTCOL_LINE_STYLE, cppu::UnoType<sal_Int16>::get(), 0, 0 },
71 class SvxXTextColumns final
72 : public cppu::WeakImplHelper<css::beans::XPropertySet, css::text::XTextColumns,
73 css::lang::XServiceInfo>
75 public:
76 SvxXTextColumns() = default;
78 // XTextColumns
79 virtual sal_Int32 SAL_CALL getReferenceValue() override;
80 virtual sal_Int16 SAL_CALL getColumnCount() override;
81 virtual void SAL_CALL setColumnCount(sal_Int16 nColumns) override;
82 virtual css::uno::Sequence<css::text::TextColumn> SAL_CALL getColumns() override;
83 virtual void SAL_CALL
84 setColumns(const css::uno::Sequence<css::text::TextColumn>& Columns) override;
86 // XPropertySet
87 virtual css::uno::Reference<css::beans::XPropertySetInfo>
88 SAL_CALL getPropertySetInfo() override;
89 virtual void SAL_CALL setPropertyValue(const OUString& aPropertyName,
90 const css::uno::Any& aValue) override;
91 virtual css::uno::Any SAL_CALL getPropertyValue(const OUString& PropertyName) override;
92 virtual void SAL_CALL addPropertyChangeListener(
93 const OUString& aPropertyName,
94 const css::uno::Reference<css::beans::XPropertyChangeListener>& xListener) override;
95 virtual void SAL_CALL removePropertyChangeListener(
96 const OUString& aPropertyName,
97 const css::uno::Reference<css::beans::XPropertyChangeListener>& aListener) override;
98 virtual void SAL_CALL addVetoableChangeListener(
99 const OUString& PropertyName,
100 const css::uno::Reference<css::beans::XVetoableChangeListener>& aListener) override;
101 virtual void SAL_CALL removeVetoableChangeListener(
102 const OUString& PropertyName,
103 const css::uno::Reference<css::beans::XVetoableChangeListener>& aListener) override;
105 // XServiceInfo
106 virtual OUString SAL_CALL getImplementationName() override;
107 virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) override;
108 virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
110 private:
111 sal_Int32 m_nReference = USHRT_MAX;
112 css::uno::Sequence<css::text::TextColumn> m_aTextColumns;
113 bool m_bIsAutomaticWidth = true;
114 sal_Int32 m_nAutoDistance = 0;
116 const SfxItemPropertySet m_aPropSet = { saTextColumns_Impl };
118 //separator line
119 sal_Int32 m_nSepLineWidth = 0;
120 css::util::Color m_nSepLineColor = 0; // black
121 sal_Int32 m_nSepLineHeightRelative = 100; // full height
122 css::style::VerticalAlignment m_nSepLineVertAlign = css::style::VerticalAlignment_MIDDLE;
123 bool m_bSepLineIsOn = false;
124 sal_Int16 m_nSepLineStyle = css::text::ColumnSeparatorStyle::NONE;
127 OUString SvxXTextColumns::getImplementationName()
129 return u"com.sun.star.comp.svx.TextColumns"_ustr;
132 sal_Bool SvxXTextColumns::supportsService(const OUString& rServiceName)
134 return cppu::supportsService(this, rServiceName);
137 css::uno::Sequence<OUString> SvxXTextColumns::getSupportedServiceNames()
139 return { u"com.sun.star.text.TextColumns"_ustr };
142 sal_Int32 SvxXTextColumns::getReferenceValue()
144 SolarMutexGuard aGuard;
145 return m_nReference;
148 sal_Int16 SvxXTextColumns::getColumnCount()
150 SolarMutexGuard aGuard;
151 return o3tl::narrowing<sal_Int16>(m_aTextColumns.getLength());
154 void SvxXTextColumns::setColumnCount(sal_Int16 nColumns)
156 SolarMutexGuard aGuard;
157 if (nColumns <= 0)
158 throw css::uno::RuntimeException();
159 m_bIsAutomaticWidth = true;
160 m_aTextColumns.realloc(nColumns);
161 css::text::TextColumn* pCols = m_aTextColumns.getArray();
162 m_nReference = USHRT_MAX;
163 sal_Int32 nWidth = m_nReference / nColumns;
164 sal_Int32 nDiff = m_nReference - nWidth * nColumns;
165 sal_Int32 nDist = m_nAutoDistance / 2;
166 for (sal_Int16 i = 0; i < nColumns; i++)
168 pCols[i].Width = nWidth;
169 pCols[i].LeftMargin = i == 0 ? 0 : nDist;
170 pCols[i].RightMargin = i == nColumns - 1 ? 0 : nDist;
172 pCols[nColumns - 1].Width += nDiff;
175 css::uno::Sequence<css::text::TextColumn> SvxXTextColumns::getColumns()
177 SolarMutexGuard aGuard;
178 return m_aTextColumns;
181 void SvxXTextColumns::setColumns(const css::uno::Sequence<css::text::TextColumn>& rColumns)
183 SolarMutexGuard aGuard;
184 sal_Int32 nReferenceTemp = std::accumulate(
185 rColumns.begin(), rColumns.end(), sal_Int32(0),
186 [](const sal_Int32 nSum, const css::text::TextColumn& rCol) { return nSum + rCol.Width; });
187 m_bIsAutomaticWidth = false;
188 m_nReference = !nReferenceTemp ? USHRT_MAX : nReferenceTemp;
189 m_aTextColumns = rColumns;
192 css::uno::Reference<css::beans::XPropertySetInfo> SvxXTextColumns::getPropertySetInfo()
194 return m_aPropSet.getPropertySetInfo();
197 void SvxXTextColumns::setPropertyValue(const OUString& rPropertyName, const css::uno::Any& aValue)
199 const SfxItemPropertyMapEntry* pEntry = m_aPropSet.getPropertyMap().getByName(rPropertyName);
200 if (!pEntry)
201 throw css::beans::UnknownPropertyException("Unknown property: " + rPropertyName,
202 getXWeak());
203 if (pEntry->nFlags & css::beans::PropertyAttribute::READONLY)
204 throw css::beans::PropertyVetoException("Property is read-only: " + rPropertyName,
205 getXWeak());
207 switch (pEntry->nWID)
209 case WID_TXTCOL_LINE_WIDTH:
211 sal_Int32 nTmp;
212 if (!(aValue >>= nTmp) || nTmp < 0)
213 throw css::lang::IllegalArgumentException();
214 m_nSepLineWidth = nTmp;
215 break;
217 case WID_TXTCOL_LINE_COLOR:
218 if (!(aValue >>= m_nSepLineColor))
219 throw css::lang::IllegalArgumentException();
220 break;
221 case WID_TXTCOL_LINE_STYLE:
222 if (!(aValue >>= m_nSepLineStyle))
223 throw css::lang::IllegalArgumentException();
224 break;
225 case WID_TXTCOL_LINE_REL_HGT:
227 sal_Int32 nTmp;
228 if (!(aValue >>= nTmp) || nTmp < 0)
229 throw css::lang::IllegalArgumentException();
230 m_nSepLineHeightRelative = nTmp;
231 break;
233 case WID_TXTCOL_LINE_ALIGN:
234 if (css::style::VerticalAlignment eAlign; aValue >>= eAlign)
235 m_nSepLineVertAlign = eAlign;
236 else if (sal_Int8 nTmp; aValue >>= nTmp)
237 m_nSepLineVertAlign = static_cast<css::style::VerticalAlignment>(nTmp);
238 else
239 throw css::lang::IllegalArgumentException();
240 break;
241 case WID_TXTCOL_LINE_IS_ON:
242 if (!(aValue >>= m_bSepLineIsOn))
243 throw css::lang::IllegalArgumentException();
244 break;
245 case WID_TXTCOL_AUTO_DISTANCE:
247 sal_Int32 nTmp;
248 if (!(aValue >>= nTmp) || nTmp < 0 || nTmp >= m_nReference)
249 throw css::lang::IllegalArgumentException();
250 m_nAutoDistance = nTmp;
251 sal_Int32 nColumns = m_aTextColumns.getLength();
252 css::text::TextColumn* pCols = m_aTextColumns.getArray();
253 sal_Int32 nDist = m_nAutoDistance / 2;
254 for (sal_Int32 i = 0; i < nColumns; i++)
256 pCols[i].LeftMargin = i == 0 ? 0 : nDist;
257 pCols[i].RightMargin = i == nColumns - 1 ? 0 : nDist;
259 break;
264 css::uno::Any SvxXTextColumns::getPropertyValue(const OUString& rPropertyName)
266 const SfxItemPropertyMapEntry* pEntry = m_aPropSet.getPropertyMap().getByName(rPropertyName);
267 if (!pEntry)
268 throw css::beans::UnknownPropertyException("Unknown property: " + rPropertyName,
269 getXWeak());
271 css::uno::Any aRet;
272 switch (pEntry->nWID)
274 case WID_TXTCOL_LINE_WIDTH:
275 aRet <<= m_nSepLineWidth;
276 break;
277 case WID_TXTCOL_LINE_COLOR:
278 aRet <<= m_nSepLineColor;
279 break;
280 case WID_TXTCOL_LINE_STYLE:
281 aRet <<= m_nSepLineStyle;
282 break;
283 case WID_TXTCOL_LINE_REL_HGT:
284 aRet <<= m_nSepLineHeightRelative;
285 break;
286 case WID_TXTCOL_LINE_ALIGN:
287 aRet <<= m_nSepLineVertAlign;
288 break;
289 case WID_TXTCOL_LINE_IS_ON:
290 aRet <<= m_bSepLineIsOn;
291 break;
292 case WID_TXTCOL_IS_AUTOMATIC:
293 aRet <<= m_bIsAutomaticWidth;
294 break;
295 case WID_TXTCOL_AUTO_DISTANCE:
296 aRet <<= m_nAutoDistance;
297 break;
299 return aRet;
302 void SvxXTextColumns::addPropertyChangeListener(
303 const OUString& /*rPropertyName*/,
304 const css::uno::Reference<css::beans::XPropertyChangeListener>& /*xListener*/)
308 void SvxXTextColumns::removePropertyChangeListener(
309 const OUString& /*rPropertyName*/,
310 const css::uno::Reference<css::beans::XPropertyChangeListener>& /*xListener*/)
314 void SvxXTextColumns::addVetoableChangeListener(
315 const OUString& /*rPropertyName*/,
316 const css::uno::Reference<css::beans::XVetoableChangeListener>& /*xListener*/)
320 void SvxXTextColumns::removeVetoableChangeListener(
321 const OUString& /*rPropertyName*/,
322 const css::uno::Reference<css::beans::XVetoableChangeListener>& /*xListener*/)
327 css::uno::Reference<css::uno::XInterface> SvxXTextColumns_createInstance() noexcept
329 return getXWeak(new SvxXTextColumns);
332 extern "C" SVXCORE_DLLPUBLIC css::uno::XInterface*
333 com_sun_star_comp_svx_TextColumns_get_implementation(css::uno::XComponentContext*,
334 css::uno::Sequence<css::uno::Any> const&)
336 return cppu::acquire(new SvxXTextColumns);
339 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */