Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sw / source / core / tox / ToxTabStopTokenHandler.cxx
blobb3943d6e5393f8a913ab55efa5e440a09216384b
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/.
8 */
10 #include <ToxTabStopTokenHandler.hxx>
12 #include <editeng/tstpitem.hxx>
13 #include <editeng/lrspitem.hxx>
14 #include <editeng/boxitem.hxx>
16 #include <cntfrm.hxx>
17 #include <fmtfsize.hxx>
18 #include <fmtpdsc.hxx>
19 #include <frmfmt.hxx>
20 #include <frmatr.hxx>
21 #include <ndtxt.hxx>
22 #include <pagedesc.hxx>
23 #include <pagefrm.hxx>
24 #include <swrect.hxx>
25 #include <tox.hxx>
27 namespace sw {
29 DefaultToxTabStopTokenHandler::DefaultToxTabStopTokenHandler(SwNodeOffset indexOfSectionNode,
30 const SwPageDesc& defaultPageDescription,
31 bool tabPositionIsRelativeToParagraphIndent,
32 TabStopReferencePolicy referencePolicy)
33 : mIndexOfSectionNode(indexOfSectionNode),
34 mDefaultPageDescription(defaultPageDescription),
35 mTabPositionIsRelativeToParagraphIndent(tabPositionIsRelativeToParagraphIndent),
36 mTabStopReferencePolicy(referencePolicy)
41 ToxTabStopTokenHandler::HandledTabStopToken
42 DefaultToxTabStopTokenHandler::HandleTabStopToken(
43 const SwFormToken& aToken, const SwTextNode& targetNode, const SwRootFrame *currentLayout) const
45 HandledTabStopToken result;
47 if (aToken.bWithTab) { // #i21237#
48 result.text = "\t";
51 // check whether a tab adjustment has been specified.
52 if (SvxTabAdjust::End > aToken.eTabAlign) {
53 SvxTextLeftMarginItem const& rTextLeftMargin(
54 targetNode.SwContentNode::GetAttr(RES_MARGIN_TEXTLEFT));
55 tools::Long nTabPosition = aToken.nTabStopPosition;
56 if (!mTabPositionIsRelativeToParagraphIndent && rTextLeftMargin.GetTextLeft() != 0)
58 nTabPosition -= rTextLeftMargin.GetTextLeft();
60 result.tabStop = SvxTabStop(nTabPosition, aToken.eTabAlign, cDfltDecimalChar, aToken.cTabFillChar);
61 return result;
64 SwRect aNdRect;
65 if (CanUseLayoutRectangle(targetNode, currentLayout)) {
66 aNdRect = targetNode.FindLayoutRect(true);
68 tools::Long nRightMargin;
69 if (aNdRect.IsEmpty()) {
70 nRightMargin = CalculatePageMarginFromPageDescription(targetNode);
71 } else {
72 nRightMargin = aNdRect.Width();
74 //#i24363# tab stops relative to indent
75 if (mTabStopReferencePolicy == TABSTOPS_RELATIVE_TO_INDENT) {
76 // left margin of paragraph style
77 SvxFirstLineIndentItem const& rFirstLine(
78 targetNode.GetTextColl()->GetFirstLineIndent());
79 SvxTextLeftMarginItem const& rTextLeftMargin(
80 targetNode.GetTextColl()->GetTextLeftMargin());
81 nRightMargin -= rTextLeftMargin.GetLeft(rFirstLine);
82 nRightMargin -= rFirstLine.GetTextFirstLineOffset();
85 result.tabStop = SvxTabStop(nRightMargin, SvxTabAdjust::Right, cDfltDecimalChar, aToken.cTabFillChar);
86 return result;
89 tools::Long
90 DefaultToxTabStopTokenHandler::CalculatePageMarginFromPageDescription(const SwTextNode& targetNode) const
92 SwNodeOffset nPgDescNdIdx = targetNode.GetIndex() + 1;
93 const SwPageDesc *pPageDesc = targetNode.FindPageDesc(&nPgDescNdIdx);
94 if (!pPageDesc || nPgDescNdIdx < mIndexOfSectionNode) {
95 // Use default page description, if none is found or the found one is given by a Node before the
96 // table-of-content section.
97 pPageDesc = &mDefaultPageDescription;
99 const SwFrameFormat& rPgDscFormat = pPageDesc->GetMaster();
100 tools::Long result = rPgDscFormat.GetFrameSize().GetWidth() - rPgDscFormat.GetLRSpace().GetLeft()
101 - rPgDscFormat.GetLRSpace().GetRight();
102 // Also consider borders
103 const SvxBoxItem& rBox = rPgDscFormat.GetBox();
104 result -= rBox.CalcLineSpace(SvxBoxItemLine::LEFT) + rBox.CalcLineSpace(SvxBoxItemLine::RIGHT);
105 return result;
109 /*static*/ bool
110 DefaultToxTabStopTokenHandler::CanUseLayoutRectangle(const SwTextNode& targetNode, const SwRootFrame *currentLayout)
112 const SwPageDesc* pageDescription =
113 targetNode.SwContentNode::GetAttr(RES_PAGEDESC).GetPageDesc();
115 if (!pageDescription) {
116 return false;
118 const SwFrame* pFrame = targetNode.getLayoutFrame(currentLayout);
119 if (!pFrame) {
120 return false;
122 pFrame = pFrame->FindPageFrame();
123 if (!pFrame) {
124 return false;
126 const SwPageFrame* pageFrame = static_cast<const SwPageFrame*>(pFrame);
127 return pageDescription == pageFrame->GetPageDesc();
132 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */