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
35void 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
70void SwColMgr::SetCount(sal_uInt16 nCount, sal_uInt16 nGutterWidth)
71{
72 m_aFormatCol.Init(nCount, nGutterWidth, m_nWidth);
75}
76
77sal_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 = m_aFormatCol.GetColumns();
86 nRet = rCols[nPos].GetRight() + rCols[nPos + 1].GetLeft();
87 }
88 return nRet;
89}
90
91void SwColMgr::SetGutterWidth(sal_uInt16 nGutterWidth, sal_uInt16 nPos)
92{
93 if (nPos == USHRT_MAX)
95 else
96 {
97 OSL_ENSURE(nPos < GetCount() - 1, "column overindexed");
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>(m_aFormatCol.GetLineHeight());
109}
111{
112 OSL_ENSURE(nPercent <= 100, "line height may be at most 100%");
113 m_aFormatCol.SetLineHeight(static_cast<sal_uInt8>(nPercent));
114}
115
116// column width
117sal_uInt16 SwColMgr::GetColWidth(sal_uInt16 nIdx) const
118{
119 OSL_ENSURE(nIdx < GetCount(), "Column array overindexed.");
121}
122
123void SwColMgr::SetColWidth(sal_uInt16 nIdx, sal_uInt16 nWd)
124{
125 OSL_ENSURE(nIdx < GetCount(), "Column array overindexed.");
126 m_aFormatCol.GetColumns()[nIdx].SetWishWidth(nWd);
127}
128
129// newly set size
130void SwColMgr::SetActualWidth(sal_uInt16 nW)
131{
132 m_nWidth = nW;
134}
135
136// ctor
138 : m_aFormatCol(rSet.Get(RES_COL))
139{
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.GetLeft());
145 m_nWidth = m_nWidth - o3tl::narrowing<sal_uInt16>(rLR.GetRight());
147}
148
150
152{
154 m_aFormatCol.SetLineWidth(nLWidth);
156}
157
158/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SvxBorderLineStyle
const SfxPoolItem & Get(sal_uInt16 nWhich, bool bSrchInParent=true) const
tools::Long GetRight() const
tools::Long GetLeft() const
SwColMgr(const SfxItemSet &rSet)
Definition: colmgr.cxx:137
short GetLineHeightPercent() const
Definition: colmgr.cxx:106
sal_uInt16 GetCount() const
Definition: colmgr.hxx:73
sal_uInt16 GetColWidth(sal_uInt16 nIdx) const
Definition: colmgr.cxx:117
void SetLineWidthAndColor(SvxBorderLineStyle eStyle, sal_uLong nWidth, const Color &rCol)
Definition: colmgr.cxx:151
void SetGutterWidth(sal_uInt16 nWidth, sal_uInt16 nPos=USHRT_MAX)
Definition: colmgr.cxx:91
SwFormatCol m_aFormatCol
Definition: colmgr.hxx:69
void SetCount(sal_uInt16 nCount, sal_uInt16 nGutterWidth)
Definition: colmgr.cxx:70
sal_uInt16 m_nWidth
Definition: colmgr.hxx:70
sal_uInt16 GetGutterWidth(sal_uInt16 nPos=USHRT_MAX) const
Definition: colmgr.cxx:77
void SetActualWidth(sal_uInt16 nW)
Definition: colmgr.cxx:130
void SetColWidth(sal_uInt16 nIdx, sal_uInt16 nWidth)
Definition: colmgr.cxx:123
~SwColMgr()
Definition: colmgr.cxx:149
void SetLineHeightPercent(short nPercent)
Definition: colmgr.cxx:110
void SetLineStyle(SvxBorderLineStyle eStyle)
Definition: fmtclds.hxx:129
sal_uInt16 GetGutterWidth(bool bMin=false) const
Definition: atrfrm.cxx:918
sal_uInt16 CalcPrtColWidth(sal_uInt16 nCol, sal_uInt16 nAct) const
As above except that it.
Definition: atrfrm.cxx:1005
void SetGutterWidth(sal_uInt16 nNew, sal_uInt16 nAct)
Adjusts borders for columns in aColumns.
Definition: atrfrm.cxx:949
void SetLineWidth(sal_uLong nLWidth)
Definition: fmtclds.hxx:130
void SetLineHeight(sal_uInt8 nNew)
Definition: fmtclds.hxx:132
sal_uInt16 CalcColWidth(sal_uInt16 nCol, sal_uInt16 nAct) const
Calculates current width of column nCol.
Definition: atrfrm.cxx:991
void Init(sal_uInt16 nNumCols, sal_uInt16 nGutterWidth, sal_uInt16 nAct)
This function allows to (repeatedly) initialize the columns.
Definition: atrfrm.cxx:969
void SetWishWidth(sal_uInt16 nNew)
Definition: fmtclds.hxx:134
const SwColumns & GetColumns() const
Definition: fmtclds.hxx:112
sal_uInt8 GetLineHeight() const
Definition: fmtclds.hxx:123
void SetLineColor(const Color &rCol)
Definition: fmtclds.hxx:131
void FitToActualSize(SwFormatCol &rCol, sal_uInt16 nWidth)
Definition: colmgr.cxx:35
int nCount
std::vector< SwColumn > SwColumns
Definition: fmtclds.hxx:57
constexpr TypedWhichId< SwFormatCol > RES_COL(109)
constexpr TypedWhichId< SwFormatFrameSize > RES_FRM_SIZE(89)
constexpr TypedWhichId< SvxLRSpaceItem > RES_LR_SPACE(91)
sal_uInt16 nPos
SVXCORE_DLLPUBLIC MSO_SPT Get(const OUString &)
int i
SwNodeOffset min(const SwNodeOffset &a, const SwNodeOffset &b)
Definition: nodeoffset.hxx:35
static SfxItemSet & rSet
sal_uIntPtr sal_uLong
#define MINLAY
Definition: swtypes.hxx:62
constexpr SwTwips DEF_GUTTER_WIDTH
Definition: swtypes.hxx:65
unsigned char sal_uInt8