Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sw / source / core / crsr / annotationmark.cxx
blob28e0bcf85b24c98118ef400b8653e72cc4ac9fed
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <algorithm>
21 #include <annotationmark.hxx>
23 #include <doc.hxx>
24 #include <IDocumentFieldsAccess.hxx>
25 #include <IDocumentState.hxx>
26 #include <fldbas.hxx>
27 #include <fmtfld.hxx>
28 #include <docufld.hxx>
29 #include <IDocumentUndoRedo.hxx>
30 #include <UndoBookmark.hxx>
31 #include <ndtxt.hxx>
32 #include <txtfld.hxx>
34 namespace sw::mark
36 AnnotationMark::AnnotationMark(
37 const SwPaM& rPaM,
38 const OUString& rName )
39 : MarkBase( rPaM, rName )
41 if ( rName.getLength() == 0 )
43 SetName( MarkBase::GenerateNewName(u"__Annotation__") );
47 AnnotationMark::~AnnotationMark()
51 void AnnotationMark::InitDoc(SwDoc& io_rDoc,
52 sw::mark::InsertMode const, SwPosition const*const)
54 SwTextNode *pTextNode = GetMarkEnd().GetNode().GetTextNode();
55 assert(pTextNode);
56 SwTextField *const pTextField = pTextNode->GetFieldTextAttrAt(
57 GetMarkEnd().GetContentIndex()-1, ::sw::GetTextAttrMode::Default);
58 assert(pTextField != nullptr);
59 auto pPostItField
60 = dynamic_cast<const SwPostItField*>(pTextField->GetFormatField().GetField());
61 assert(pPostItField);
62 // use the annotation mark's name as the annotation name, if
63 // - the annotation field has an empty annotation name or
64 // - the annotation mark's name differs (on mark creation a name clash had been detected)
65 if ( pPostItField->GetName().isEmpty()
66 || pPostItField->GetName() != GetName() )
68 const_cast<SwPostItField*>(pPostItField)->SetName( GetName() );
71 if (io_rDoc.GetIDocumentUndoRedo().DoesUndo())
73 io_rDoc.GetIDocumentUndoRedo().AppendUndo( std::make_unique<SwUndoInsBookmark>(*this) );
75 io_rDoc.getIDocumentState().SetModified();
78 const SwFormatField* AnnotationMark::GetAnnotationFormatField() const
80 SwDoc& rDoc = GetMarkPos().GetDoc();
82 const auto sName = GetName();
83 SwFieldType* pType = rDoc.getIDocumentFieldsAccess().GetFieldType( SwFieldIds::Postit, OUString(), false );
84 std::vector<SwFormatField*> vFields;
85 pType->GatherFields(vFields);
86 auto ppFound = std::find_if(vFields.begin(), vFields.end(), [&sName](SwFormatField* pF)
88 auto pPF = dynamic_cast<const SwPostItField*>(pF->GetField());
89 return pPF && pPF->GetName() == sName;
90 });
91 return ppFound != vFields.end() ? *ppFound : nullptr;
95 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */