1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
23 #include <IDocumentFieldsAccess.hxx>
32 #include <osl/diagnose.h>
36 SwInputFieldList::SwInputFieldList( SwEditShell
* pShell
, bool bBuildTmpLst
)
39 // create sorted list of all input fields
40 mpSrtLst
.reset( new SetGetExpFields
);
42 const SwFieldTypes
& rFieldTypes
= *mpSh
->GetDoc()->getIDocumentFieldsAccess().GetFieldTypes();
43 const size_t nSize
= rFieldTypes
.size();
45 // iterate over all types
46 std::vector
<SwFormatField
*> vFields
;
47 for(size_t i
= 0; i
< nSize
; ++i
)
49 SwFieldType
* pFieldType
= rFieldTypes
[ i
].get();
50 const SwFieldIds nType
= pFieldType
->Which();
51 if(SwFieldIds::SetExp
== nType
|| SwFieldIds::Input
== nType
|| SwFieldIds::Dropdown
== nType
)
52 pFieldType
->GatherFields(vFields
);
54 for(auto pFormatField
: vFields
)
56 auto pSetExpField
= dynamic_cast<SwSetExpField
*>(pFormatField
->GetField());
57 if(pSetExpField
&& !pSetExpField
->GetInputFlag())
59 const SwTextField
* pTextField
= pFormatField
->GetTextField();
61 maTmpLst
.insert(pTextField
);
64 SwNodeIndex
aIdx(pTextField
->GetTextNode());
65 std::unique_ptr
<SetGetExpField
> pNew(new SetGetExpField(aIdx
, pTextField
));
66 mpSrtLst
->insert(std::move(pNew
));
71 SwInputFieldList::~SwInputFieldList()
75 size_t SwInputFieldList::Count() const
77 return mpSrtLst
->size();
80 // get field from list in sorted order
81 SwField
* SwInputFieldList::GetField(size_t nId
)
83 const SwTextField
* pTextField
= (*mpSrtLst
)[ nId
]->GetTextField();
84 OSL_ENSURE( pTextField
, "no TextField" );
85 return const_cast<SwField
*>(pTextField
->GetFormatField().GetField());
89 void SwInputFieldList::PushCursor()
96 void SwInputFieldList::PopCursor()
98 mpSh
->Pop(SwCursorShell::PopMode::DeleteCurrent
);
101 /// go to position of a field
102 void SwInputFieldList::GotoFieldPos(size_t nId
)
104 mpSh
->StartAllAction();
105 (*mpSrtLst
)[ nId
]->GetPosOfContent( *mpSh
->GetCursor()->GetPoint() );
106 mpSh
->EndAllAction();
109 /** Compare TmpLst with current fields.
111 * All new ones are added to SortList so that they can be updated.
112 * For text blocks: update only input fields.
114 * @return true if not empty
116 bool SwInputFieldList::BuildSortLst()
118 const SwFieldTypes
& rFieldTypes
= *mpSh
->GetDoc()->getIDocumentFieldsAccess().GetFieldTypes();
119 const size_t nSize
= rFieldTypes
.size();
121 // iterate over all types
122 std::vector
<SwFormatField
*> vFields
;
123 for(size_t i
= 0; i
< nSize
; ++i
)
125 SwFieldType
* pFieldType
= rFieldTypes
[ i
].get();
126 const SwFieldIds nType
= pFieldType
->Which();
127 if(SwFieldIds::SetExp
== nType
|| SwFieldIds::Input
== nType
)
128 pFieldType
->GatherFields(vFields
);
130 for(auto pFormatField
: vFields
)
132 auto pSetExpField
= dynamic_cast<SwSetExpField
*>(pFormatField
->GetField());
133 if(pSetExpField
&& !pSetExpField
->GetInputFlag())
135 const SwTextField
* pTextField
= pFormatField
->GetTextField();
136 // not in TempList, thus add to SortList
137 auto it
= maTmpLst
.find(pTextField
);
138 if(maTmpLst
.end() == it
)
140 SwNodeIndex
aIdx(pTextField
->GetTextNode());
141 std::unique_ptr
<SetGetExpField
> pNew(new SetGetExpField(aIdx
, pTextField
));
142 mpSrtLst
->insert(std::move(pNew
));
148 // the pointers are not needed anymore
150 return !mpSrtLst
->empty();
153 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */