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 <pamtyp.hxx>
21 #include <pam.hxx>
22 #include <txtfrm.hxx>
23 #include <ndtxt.hxx>
24 #include <memory>
25 
26 namespace sw {
27 
FindFormatImpl(SwPaM & rSearchPam,const SwFormat & rFormat,SwMoveFnCollection const & fnMove,const SwPaM & rRegion,bool bInReadOnly,SwRootFrame const * const pLayout)28 bool FindFormatImpl(SwPaM & rSearchPam,
29         const SwFormat& rFormat, SwMoveFnCollection const & fnMove,
30         const SwPaM &rRegion, bool bInReadOnly,
31         SwRootFrame const*const pLayout)
32 {
33     bool bFound = false;
34     const bool bSrchForward = &fnMove == &fnMoveForward;
35     std::unique_ptr<SwPaM> pPam(MakeRegion( fnMove, rRegion ));
36 
37     // if at beginning/end then move it out of the node
38     if( bSrchForward
39         ? pPam->GetPoint()->nContent.GetIndex() == pPam->GetContentNode()->Len()
40         : !pPam->GetPoint()->nContent.GetIndex() )
41     {
42         if( !(*fnMove.fnNds)( &pPam->GetPoint()->nNode, false ))
43         {
44             return false;
45         }
46         SwContentNode *pNd = pPam->GetPoint()->nNode.GetNode().GetContentNode();
47         pPam->GetPoint()->nContent.Assign( pNd, bSrchForward ? 0 : pNd->Len() );
48     }
49 
50     bool bFirst = true;
51     SwContentNode* pNode;
52     while (nullptr != (pNode = ::GetNode(*pPam, bFirst, fnMove, bInReadOnly, pLayout)))
53     {
54         SwTextFrame const*const pFrame(pLayout && pNode->IsTextNode()
55             ? static_cast<SwTextFrame const*>(pNode->getLayoutFrame(pLayout))
56             : nullptr);
57         assert(!pLayout || !pNode->IsTextNode() || pFrame);
58         SwContentNode const& rPropsNode(*(pFrame
59             ? pFrame->GetTextNodeForParaProps()
60             : pNode));
61 
62         if (rPropsNode.GetFormatColl() == &rFormat)
63         {
64             // if a FormatCollection is found then it is definitely a SwContentNode
65 
66             // FORWARD:  SPoint at the end, GetMark at the beginning of the node
67             // BACKWARD: SPoint at the beginning, GetMark at the end of the node
68             // always: incl. start and incl. end
69             if (pFrame)
70             {
71                 *rSearchPam.GetPoint() = *pPam->GetPoint();
72                 rSearchPam.SetMark();
73                 *rSearchPam.GetMark() = pFrame->MapViewToModelPos(
74                     TextFrameIndex(bSrchForward ? pFrame->GetText().getLength() : 0));
75             }
76             else
77             {
78                 *rSearchPam.GetPoint() = *pPam->GetPoint();
79                 rSearchPam.SetMark();
80                 pNode->MakeEndIndex( &rSearchPam.GetPoint()->nContent );
81                 rSearchPam.GetMark()->nContent = 0;
82             }
83 
84             // if backward search, switch point and mark
85             if( !bSrchForward )
86                 rSearchPam.Exchange();
87 
88             bFound = true;
89             break;
90         }
91     }
92     return bFound;
93 }
94 
95 }
96 
97 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
98