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 <frmmgr.hxx>
29 #include <frmfmt.hxx>
30 #include <colmgr.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 
130 // newly set size
131 void SwColMgr::SetActualWidth(sal_uInt16 nW)
132 {
133  nWidth = nW;
135 }
136 
137 // ctor
139  aFormatCol(rSet.Get(RES_COL))
140 {
141  nWidth = static_cast<sal_uInt16>(rSet.Get(RES_FRM_SIZE).GetWidth());
142  if (nWidth < MINLAY)
143  nWidth = USHRT_MAX;
144  const SvxLRSpaceItem &rLR = rSet.Get(RES_LR_SPACE);
145  nWidth = nWidth - static_cast<sal_uInt16>(rLR.GetLeft());
146  nWidth = nWidth - static_cast<sal_uInt16>(rLR.GetRight());
147  ::FitToActualSize(aFormatCol, nWidth);
148 }
149 
151 {
152 }
153 
155 {
156  aFormatCol.SetLineStyle(eStyle);
157  aFormatCol.SetLineWidth(nLWidth);
158  aFormatCol.SetLineColor(rCol);
159 }
160 
161 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
long GetLeft() const
void SetLineWidth(sal_uLong nLWidth)
Definition: fmtclds.hxx:130
void SetWishWidth(sal_uInt16 nNew)
Definition: fmtclds.hxx:134
#define RES_FRM_SIZE
Definition: hintids.hxx:196
#define RES_COL
Definition: hintids.hxx:216
void SetLineHeight(sal_uInt8 nNew)
Definition: fmtclds.hxx:132
std::vector< SwColumn > SwColumns
Definition: fmtclds.hxx:57
void FitToActualSize(SwFormatCol &rCol, sal_uInt16 nWidth)
Definition: colmgr.cxx:35
~SwColMgr()
Definition: colmgr.cxx:150
sal_uIntPtr sal_uLong
#define MINLAY
Definition: swtypes.hxx:66
void SetLineColor(const Color &rCol)
Definition: fmtclds.hxx:131
void SetActualWidth(sal_uInt16 nW)
Definition: colmgr.cxx:131
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
void SetGutterWidth(sal_uInt16 nWidth, sal_uInt16 nPos=USHRT_MAX)
Definition: colmgr.cxx:91
SwColMgr(const SfxItemSet &rSet)
Definition: colmgr.cxx:138
void SetLineStyle(SvxBorderLineStyle eStyle)
Definition: fmtclds.hxx:129
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:893
SwFormatCol aFormatCol
Definition: colmgr.hxx:69
int i
sal_uInt16 CalcColWidth(sal_uInt16 nCol, sal_uInt16 nAct) const
Calculates current width of column nCol.
Definition: atrfrm.cxx:966
void SetCount(sal_uInt16 nCount, sal_uInt16 nGutterWidth)
Definition: colmgr.cxx:70
#define RES_LR_SPACE
Definition: hintids.hxx:198
#define DEF_GUTTER_WIDTH
Definition: swtypes.hxx:69
sal_uInt16 CalcPrtColWidth(sal_uInt16 nCol, sal_uInt16 nAct) const
As above except that it.
Definition: atrfrm.cxx:980
const SfxPoolItem & Get(sal_uInt16 nWhich, bool bSrchInParent=true) const
const o3tl::enumarray< SvxAdjust, unsigned short > aSvxToUnoAdjust USHRT_MAX
Definition: unosett.cxx:261
long GetRight() const
void SetColWidth(sal_uInt16 nIdx, sal_uInt16 nWidth)
Definition: colmgr.cxx:123
SVX_DLLPUBLIC MSO_SPT Get(const OUString &)
void SetLineWidthAndColor(SvxBorderLineStyle eStyle, sal_uLong nWidth, const Color &rCol)
Definition: colmgr.cxx:154
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:944
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:924