docthemes: Save themes def. to a file when added to ColorSets
[LibreOffice.git] / sw / source / uibase / frmdlg / colmgr.cxx
blob495039efba7473f1bbe8528d37887a2df0616884
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <algorithm>
24 #include <hintids.hxx>
25 #include <editeng/lrspitem.hxx>
26 #include <osl/diagnose.h>
28 #include <colmgr.hxx>
29 #include <fmtfsize.hxx>
30 #include <swtypes.hxx>
32 // private methods
34 // set column width to current width
35 void FitToActualSize(SwFormatCol& rCol, sal_uInt16 nWidth)
37 const sal_uInt16 nCount = rCol.GetColumns().size();
38 for (sal_uInt16 i = 0; i < nCount; ++i)
40 const sal_uInt16 nTmp = rCol.CalcColWidth(i, nWidth);
41 auto& col = rCol.GetColumns()[i];
42 col.SetWishWidth(nTmp);
43 // If necessary, shrink borders (as equally as possible) to keep up the invariant that
44 // GetWishWidth() >= GetLeft() + GetRight():
45 sal_uInt32 const borders = col.GetLeft() + col.GetRight();
46 if (borders > nTmp)
48 auto const shrink = borders - nTmp;
49 auto const half = shrink / 2; // rounds down
50 if (col.GetLeft() < col.GetRight())
52 auto const shrinkLeft = std::min(sal_uInt32(col.GetLeft()), half);
53 col.SetLeft(col.GetLeft() - shrinkLeft);
54 col.SetRight(col.GetRight() - (shrink - shrinkLeft));
56 else
58 auto const shrinkRight = std::min(sal_uInt32(col.GetRight()), half);
59 col.SetLeft(col.GetLeft() - (shrink - shrinkRight));
60 col.SetRight(col.GetRight() - shrinkRight);
64 rCol.SetWishWidth(nWidth);
67 // public methods
69 // set column quantity and Gutterwidth
70 void SwColMgr::SetCount(sal_uInt16 nCount, sal_uInt16 nGutterWidth)
72 m_aFormatCol.Init(nCount, nGutterWidth, m_nWidth);
73 m_aFormatCol.SetWishWidth(m_nWidth);
74 m_aFormatCol.SetGutterWidth(nGutterWidth, m_nWidth);
77 sal_uInt16 SwColMgr::GetGutterWidth(sal_uInt16 nPos) const
79 sal_uInt16 nRet;
80 if (nPos == USHRT_MAX)
81 nRet = GetCount() > 1 ? m_aFormatCol.GetGutterWidth() : DEF_GUTTER_WIDTH;
82 else
84 OSL_ENSURE(nPos < GetCount() - 1, "column overindexed");
85 const SwColumns& rCols = m_aFormatCol.GetColumns();
86 nRet = rCols[nPos].GetRight() + rCols[nPos + 1].GetLeft();
88 return nRet;
91 void SwColMgr::SetGutterWidth(sal_uInt16 nGutterWidth, sal_uInt16 nPos)
93 if (nPos == USHRT_MAX)
94 m_aFormatCol.SetGutterWidth(nGutterWidth, m_nWidth);
95 else
97 OSL_ENSURE(nPos < GetCount() - 1, "column overindexed");
98 SwColumns& rCols = m_aFormatCol.GetColumns();
99 sal_uInt16 nGutterWidth2 = nGutterWidth / 2;
100 rCols[nPos].SetRight(nGutterWidth2);
101 rCols[nPos + 1].SetLeft(nGutterWidth2);
105 // height separation line
106 short SwColMgr::GetLineHeightPercent() const
108 return static_cast<short>(m_aFormatCol.GetLineHeight());
110 void SwColMgr::SetLineHeightPercent(short nPercent)
112 OSL_ENSURE(nPercent <= 100, "line height may be at most 100%");
113 m_aFormatCol.SetLineHeight(static_cast<sal_uInt8>(nPercent));
116 // column width
117 sal_uInt16 SwColMgr::GetColWidth(sal_uInt16 nIdx) const
119 OSL_ENSURE(nIdx < GetCount(), "Column array overindexed.");
120 return m_aFormatCol.CalcPrtColWidth(nIdx, m_nWidth);
123 void SwColMgr::SetColWidth(sal_uInt16 nIdx, sal_uInt16 nWd)
125 OSL_ENSURE(nIdx < GetCount(), "Column array overindexed.");
126 m_aFormatCol.GetColumns()[nIdx].SetWishWidth(nWd);
129 // newly set size
130 void SwColMgr::SetActualWidth(sal_uInt16 nW)
132 m_nWidth = nW;
133 ::FitToActualSize(m_aFormatCol, nW);
136 // ctor
137 SwColMgr::SwColMgr(const SfxItemSet& rSet)
138 : m_aFormatCol(rSet.Get(RES_COL))
140 m_nWidth = o3tl::narrowing<sal_uInt16>(rSet.Get(RES_FRM_SIZE).GetWidth());
141 if (m_nWidth < MINLAY)
142 m_nWidth = USHRT_MAX;
143 const SvxLRSpaceItem& rLR = rSet.Get(RES_LR_SPACE);
144 m_nWidth = m_nWidth - o3tl::narrowing<sal_uInt16>(rLR.ResolveLeft({}));
145 m_nWidth = m_nWidth - o3tl::narrowing<sal_uInt16>(rLR.ResolveRight({}));
146 ::FitToActualSize(m_aFormatCol, m_nWidth);
149 SwColMgr::~SwColMgr() {}
151 void SwColMgr::SetLineWidthAndColor(SvxBorderLineStyle eStyle, sal_uLong nLWidth, const Color& rCol)
153 m_aFormatCol.SetLineStyle(eStyle);
154 m_aFormatCol.SetLineWidth(nLWidth);
155 m_aFormatCol.SetLineColor(rCol);
158 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */