LibreOffice Module sw (master)  1
colmgr.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 #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
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
70 void SwColMgr::SetCount(sal_uInt16 nCount, sal_uInt16 nGutterWidth)
71 {
72  aFormatCol.Init(nCount, nGutterWidth, nWidth);
74  aFormatCol.SetGutterWidth(nGutterWidth, nWidth);
75 }
76 
77 sal_uInt16 SwColMgr::GetGutterWidth(sal_uInt16 nPos) const
78 {
79  sal_uInt16 nRet;
80  if (nPos == USHRT_MAX)
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 
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
107 {
108  return static_cast<short>(aFormatCol.GetLineHeight());
109 }
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
117 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 
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
130 void SwColMgr::SetActualWidth(sal_uInt16 nW)
131 {
132  nWidth = nW;
134 }
135 
136 // ctor
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 
150 
152 {
153  aFormatCol.SetLineStyle(eStyle);
154  aFormatCol.SetLineWidth(nLWidth);
155  aFormatCol.SetLineColor(rCol);
156 }
157 
158 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void SetLineWidth(sal_uLong nLWidth)
Definition: fmtclds.hxx:130
SwNodeOffset min(const SwNodeOffset &a, const SwNodeOffset &b)
Definition: nodeoffset.hxx:35
void SetWishWidth(sal_uInt16 nNew)
Definition: fmtclds.hxx:134
void SetLineHeight(sal_uInt8 nNew)
Definition: fmtclds.hxx:132
std::vector< SwColumn > SwColumns
Definition: fmtclds.hxx:57
constexpr TypedWhichId< SwFormatCol > RES_COL(109)
void FitToActualSize(SwFormatCol &rCol, sal_uInt16 nWidth)
Definition: colmgr.cxx:35
~SwColMgr()
Definition: colmgr.cxx:149
constexpr TypedWhichId< SwFormatFrameSize > RES_FRM_SIZE(89)
sal_uIntPtr sal_uLong
tools::Long GetRight() const
#define MINLAY
Definition: swtypes.hxx:63
void SetLineColor(const Color &rCol)
Definition: fmtclds.hxx:131
void SetActualWidth(sal_uInt16 nW)
Definition: colmgr.cxx:130
constexpr SwTwips DEF_GUTTER_WIDTH
Definition: swtypes.hxx:66
SvxBorderLineStyle
short GetLineHeightPercent() const
Definition: colmgr.cxx:106
sal_uInt16 GetColWidth(sal_uInt16 nIdx) const
Definition: colmgr.cxx:117
sal_uInt16 nWidth
Definition: colmgr.hxx:70
int nCount
void SetGutterWidth(sal_uInt16 nWidth, sal_uInt16 nPos=USHRT_MAX)
Definition: colmgr.cxx:91
SwColMgr(const SfxItemSet &rSet)
Definition: colmgr.cxx:137
void SetLineStyle(SvxBorderLineStyle eStyle)
Definition: fmtclds.hxx:129
int i
const SwColumns & GetColumns() const
Definition: fmtclds.hxx:112
sal_uInt8 GetLineHeight() const
Definition: fmtclds.hxx:123
sal_uInt16 GetGutterWidth(bool bMin=false) const
Definition: atrfrm.cxx:918
SwFormatCol aFormatCol
Definition: colmgr.hxx:69
sal_uInt16 CalcColWidth(sal_uInt16 nCol, sal_uInt16 nAct) const
Calculates current width of column nCol.
Definition: atrfrm.cxx:991
tools::Long GetLeft() const
void SetCount(sal_uInt16 nCount, sal_uInt16 nGutterWidth)
Definition: colmgr.cxx:70
sal_uInt16 CalcPrtColWidth(sal_uInt16 nCol, sal_uInt16 nAct) const
As above except that it.
Definition: atrfrm.cxx:1005
const SfxPoolItem & Get(sal_uInt16 nWhich, bool bSrchInParent=true) const
constexpr TypedWhichId< SvxLRSpaceItem > RES_LR_SPACE(91)
void SetColWidth(sal_uInt16 nIdx, sal_uInt16 nWidth)
Definition: colmgr.cxx:123
SVXCORE_DLLPUBLIC MSO_SPT Get(const OUString &)
void SetLineWidthAndColor(SvxBorderLineStyle eStyle, sal_uLong nWidth, const Color &rCol)
Definition: colmgr.cxx:151
sal_uInt16 GetGutterWidth(sal_uInt16 nPos=USHRT_MAX) const
Definition: colmgr.cxx:77
void Init(sal_uInt16 nNumCols, sal_uInt16 nGutterWidth, sal_uInt16 nAct)
This function allows to (repeatedly) initialize the columns.
Definition: atrfrm.cxx:969
sal_uInt16 GetCount() const
Definition: colmgr.hxx:73
void SetLineHeightPercent(short nPercent)
Definition: colmgr.cxx:110
void SetGutterWidth(sal_uInt16 nNew, sal_uInt16 nAct)
Adjusts borders for columns in aColumns.
Definition: atrfrm.cxx:949