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 #include <sal/config.h>
21
22 #include <algorithm>
23
24 #include <hintids.hxx>
25 #include <editeng/lrspitem.hxx>
26 #include <osl/diagnose.h>
27
28 #include <colmgr.hxx>
29 #include <fmtfsize.hxx>
30 #include <swtypes.hxx>
31
32 // private methods
33
34 // set column width to current width
FitToActualSize(SwFormatCol & rCol,sal_uInt16 nWidth)35 void FitToActualSize(SwFormatCol& rCol, sal_uInt16 nWidth)
36 {
37 const sal_uInt16 nCount = rCol.GetColumns().size();
38 for (sal_uInt16 i = 0; i < nCount; ++i)
39 {
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)
47 {
48 auto const shrink = borders - nTmp;
49 auto const half = shrink / 2; // rounds down
50 if (col.GetLeft() < col.GetRight())
51 {
52 auto const shrinkLeft = std::min(sal_uInt32(col.GetLeft()), half);
53 col.SetLeft(col.GetLeft() - shrinkLeft);
54 col.SetRight(col.GetRight() - (shrink - shrinkLeft));
55 }
56 else
57 {
58 auto const shrinkRight = std::min(sal_uInt32(col.GetRight()), half);
59 col.SetLeft(col.GetLeft() - (shrink - shrinkRight));
60 col.SetRight(col.GetRight() - shrinkRight);
61 }
62 }
63 }
64 rCol.SetWishWidth(nWidth);
65 }
66
67 // public methods
68
69 // set column quantity and Gutterwidth
SetCount(sal_uInt16 nCount,sal_uInt16 nGutterWidth)70 void SwColMgr::SetCount(sal_uInt16 nCount, sal_uInt16 nGutterWidth)
71 {
72 aFormatCol.Init(nCount, nGutterWidth, nWidth);
73 aFormatCol.SetWishWidth(nWidth);
74 aFormatCol.SetGutterWidth(nGutterWidth, nWidth);
75 }
76
GetGutterWidth(sal_uInt16 nPos) const77 sal_uInt16 SwColMgr::GetGutterWidth(sal_uInt16 nPos) const
78 {
79 sal_uInt16 nRet;
80 if (nPos == USHRT_MAX)
81 nRet = GetCount() > 1 ? aFormatCol.GetGutterWidth() : DEF_GUTTER_WIDTH;
82 else
83 {
84 OSL_ENSURE(nPos < GetCount() - 1, "column overindexed");
85 const SwColumns& rCols = aFormatCol.GetColumns();
86 nRet = rCols[nPos].GetRight() + rCols[nPos + 1].GetLeft();
87 }
88 return nRet;
89 }
90
SetGutterWidth(sal_uInt16 nGutterWidth,sal_uInt16 nPos)91 void SwColMgr::SetGutterWidth(sal_uInt16 nGutterWidth, sal_uInt16 nPos)
92 {
93 if (nPos == USHRT_MAX)
94 aFormatCol.SetGutterWidth(nGutterWidth, nWidth);
95 else
96 {
97 OSL_ENSURE(nPos < GetCount() - 1, "column overindexed");
98 SwColumns& rCols = aFormatCol.GetColumns();
99 sal_uInt16 nGutterWidth2 = nGutterWidth / 2;
100 rCols[nPos].SetRight(nGutterWidth2);
101 rCols[nPos + 1].SetLeft(nGutterWidth2);
102 }
103 }
104
105 // height separation line
GetLineHeightPercent() const106 short SwColMgr::GetLineHeightPercent() const
107 {
108 return static_cast<short>(aFormatCol.GetLineHeight());
109 }
SetLineHeightPercent(short nPercent)110 void SwColMgr::SetLineHeightPercent(short nPercent)
111 {
112 OSL_ENSURE(nPercent <= 100, "line height may be at most 100%");
113 aFormatCol.SetLineHeight(static_cast<sal_uInt8>(nPercent));
114 }
115
116 // column width
GetColWidth(sal_uInt16 nIdx) const117 sal_uInt16 SwColMgr::GetColWidth(sal_uInt16 nIdx) const
118 {
119 OSL_ENSURE(nIdx < GetCount(), "Column array overindexed.");
120 return aFormatCol.CalcPrtColWidth(nIdx, nWidth);
121 }
122
SetColWidth(sal_uInt16 nIdx,sal_uInt16 nWd)123 void SwColMgr::SetColWidth(sal_uInt16 nIdx, sal_uInt16 nWd)
124 {
125 OSL_ENSURE(nIdx < GetCount(), "Column array overindexed.");
126 aFormatCol.GetColumns()[nIdx].SetWishWidth(nWd);
127 }
128
129 // newly set size
SetActualWidth(sal_uInt16 nW)130 void SwColMgr::SetActualWidth(sal_uInt16 nW)
131 {
132 nWidth = nW;
133 ::FitToActualSize(aFormatCol, nW);
134 }
135
136 // ctor
SwColMgr(const SfxItemSet & rSet)137 SwColMgr::SwColMgr(const SfxItemSet& rSet)
138 : aFormatCol(rSet.Get(RES_COL))
139 {
140 nWidth = o3tl::narrowing<sal_uInt16>(rSet.Get(RES_FRM_SIZE).GetWidth());
141 if (nWidth < MINLAY)
142 nWidth = USHRT_MAX;
143 const SvxLRSpaceItem& rLR = rSet.Get(RES_LR_SPACE);
144 nWidth = nWidth - o3tl::narrowing<sal_uInt16>(rLR.GetLeft());
145 nWidth = nWidth - o3tl::narrowing<sal_uInt16>(rLR.GetRight());
146 ::FitToActualSize(aFormatCol, nWidth);
147 }
148
~SwColMgr()149 SwColMgr::~SwColMgr() {}
150
SetLineWidthAndColor(SvxBorderLineStyle eStyle,sal_uLong nLWidth,const Color & rCol)151 void SwColMgr::SetLineWidthAndColor(SvxBorderLineStyle eStyle, sal_uLong nLWidth, const Color& rCol)
152 {
153 aFormatCol.SetLineStyle(eStyle);
154 aFormatCol.SetLineWidth(nLWidth);
155 aFormatCol.SetLineColor(rCol);
156 }
157
158 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
159